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>
33 #include <linux/dma-mapping.h>
34 #include <linux/iommu.h>
35 #include <linux/hmm.h>
36 #include <linux/pagemap.h>
37 #include <linux/sched/task.h>
38 #include <linux/sched/mm.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/swap.h>
42 #include <linux/swiotlb.h>
43 #include <linux/dma-buf.h>
44 #include <linux/sizes.h>
46 #include <drm/ttm/ttm_bo_api.h>
47 #include <drm/ttm/ttm_bo_driver.h>
48 #include <drm/ttm/ttm_placement.h>
49 #include <drm/ttm/ttm_module.h>
51 #include <drm/drm_debugfs.h>
52 #include <drm/amdgpu_drm.h>
55 #include "amdgpu_object.h"
56 #include "amdgpu_trace.h"
57 #include "amdgpu_amdkfd.h"
58 #include "amdgpu_sdma.h"
59 #include "amdgpu_ras.h"
60 #include "amdgpu_atomfirmware.h"
61 #include "bif/bif_4_1_d.h"
63 #define AMDGPU_TTM_VRAM_MAX_DW_READ (size_t)128
65 static int amdgpu_ttm_backend_bind(struct ttm_bo_device *bdev,
67 struct ttm_resource *bo_mem);
68 static void amdgpu_ttm_backend_unbind(struct ttm_bo_device *bdev,
71 static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
73 uint64_t size_in_page)
75 return ttm_range_man_init(&adev->mman.bdev, type,
80 * amdgpu_evict_flags - Compute placement flags
82 * @bo: The buffer object to evict
83 * @placement: Possible destination(s) for evicted BO
85 * Fill in placement data when ttm_bo_evict() is called
87 static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
88 struct ttm_placement *placement)
90 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
91 struct amdgpu_bo *abo;
92 static const struct ttm_place placements = {
95 .mem_type = TTM_PL_SYSTEM,
99 /* Don't handle scatter gather BOs */
100 if (bo->type == ttm_bo_type_sg) {
101 placement->num_placement = 0;
102 placement->num_busy_placement = 0;
106 /* Object isn't an AMDGPU object so ignore */
107 if (!amdgpu_bo_is_amdgpu_bo(bo)) {
108 placement->placement = &placements;
109 placement->busy_placement = &placements;
110 placement->num_placement = 1;
111 placement->num_busy_placement = 1;
115 abo = ttm_to_amdgpu_bo(bo);
116 switch (bo->mem.mem_type) {
120 placement->num_placement = 0;
121 placement->num_busy_placement = 0;
125 if (!adev->mman.buffer_funcs_enabled) {
126 /* Move to system memory */
127 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
128 } else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
129 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
130 amdgpu_bo_in_cpu_visible_vram(abo)) {
132 /* Try evicting to the CPU inaccessible part of VRAM
133 * first, but only set GTT as busy placement, so this
134 * BO will be evicted to GTT rather than causing other
135 * BOs to be evicted from VRAM
137 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
138 AMDGPU_GEM_DOMAIN_GTT);
139 abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
140 abo->placements[0].lpfn = 0;
141 abo->placement.busy_placement = &abo->placements[1];
142 abo->placement.num_busy_placement = 1;
144 /* Move to GTT memory */
145 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
150 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
153 *placement = abo->placement;
157 * amdgpu_verify_access - Verify access for a mmap call
159 * @bo: The buffer object to map
160 * @filp: The file pointer from the process performing the mmap
162 * This is called by ttm_bo_mmap() to verify whether a process
163 * has the right to mmap a BO to their process space.
165 static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
167 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
170 * Don't verify access for KFD BOs. They don't have a GEM
171 * object associated with them.
176 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
178 return drm_vma_node_verify_access(&abo->tbo.base.vma_node,
183 * amdgpu_mm_node_addr - Compute the GPU relative offset of a GTT buffer.
185 * @bo: The bo to assign the memory to.
186 * @mm_node: Memory manager node for drm allocator.
187 * @mem: The region where the bo resides.
190 static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
191 struct drm_mm_node *mm_node,
192 struct ttm_resource *mem)
196 if (mm_node->start != AMDGPU_BO_INVALID_OFFSET) {
197 addr = mm_node->start << PAGE_SHIFT;
198 addr += amdgpu_ttm_domain_start(amdgpu_ttm_adev(bo->bdev),
205 * amdgpu_find_mm_node - Helper function finds the drm_mm_node corresponding to
206 * @offset. It also modifies the offset to be within the drm_mm_node returned
208 * @mem: The region where the bo resides.
209 * @offset: The offset that drm_mm_node is used for finding.
212 static struct drm_mm_node *amdgpu_find_mm_node(struct ttm_resource *mem,
215 struct drm_mm_node *mm_node = mem->mm_node;
217 while (*offset >= (mm_node->size << PAGE_SHIFT)) {
218 *offset -= (mm_node->size << PAGE_SHIFT);
225 * amdgpu_ttm_map_buffer - Map memory into the GART windows
226 * @bo: buffer object to map
227 * @mem: memory object to map
228 * @mm_node: drm_mm node object to map
229 * @num_pages: number of pages to map
230 * @offset: offset into @mm_node where to start
231 * @window: which GART window to use
232 * @ring: DMA ring to use for the copy
233 * @tmz: if we should setup a TMZ enabled mapping
234 * @addr: resulting address inside the MC address space
236 * Setup one of the GART windows to access a specific piece of memory or return
237 * the physical address for local memory.
239 static int amdgpu_ttm_map_buffer(struct ttm_buffer_object *bo,
240 struct ttm_resource *mem,
241 struct drm_mm_node *mm_node,
242 unsigned num_pages, uint64_t offset,
243 unsigned window, struct amdgpu_ring *ring,
244 bool tmz, uint64_t *addr)
246 struct amdgpu_device *adev = ring->adev;
247 struct amdgpu_job *job;
248 unsigned num_dw, num_bytes;
249 struct dma_fence *fence;
250 uint64_t src_addr, dst_addr;
256 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
257 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
259 /* Map only what can't be accessed directly */
260 if (!tmz && mem->start != AMDGPU_BO_INVALID_OFFSET) {
261 *addr = amdgpu_mm_node_addr(bo, mm_node, mem) + offset;
265 *addr = adev->gmc.gart_start;
266 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
267 AMDGPU_GPU_PAGE_SIZE;
268 *addr += offset & ~PAGE_MASK;
270 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
271 num_bytes = num_pages * 8;
273 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes,
274 AMDGPU_IB_POOL_DELAYED, &job);
278 src_addr = num_dw * 4;
279 src_addr += job->ibs[0].gpu_addr;
281 dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
282 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
283 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
284 dst_addr, num_bytes, false);
286 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
287 WARN_ON(job->ibs[0].length_dw > num_dw);
289 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, mem);
291 flags |= AMDGPU_PTE_TMZ;
293 cpu_addr = &job->ibs[0].ptr[num_dw];
295 if (mem->mem_type == TTM_PL_TT) {
296 dma_addr_t *dma_address;
298 dma_address = &bo->ttm->dma_address[offset >> PAGE_SHIFT];
299 r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
304 dma_addr_t dma_address;
306 dma_address = (mm_node->start << PAGE_SHIFT) + offset;
307 dma_address += adev->vm_manager.vram_base_offset;
309 for (i = 0; i < num_pages; ++i) {
310 r = amdgpu_gart_map(adev, i << PAGE_SHIFT, 1,
311 &dma_address, flags, cpu_addr);
315 dma_address += PAGE_SIZE;
319 r = amdgpu_job_submit(job, &adev->mman.entity,
320 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
324 dma_fence_put(fence);
329 amdgpu_job_free(job);
334 * amdgpu_copy_ttm_mem_to_mem - Helper function for copy
335 * @adev: amdgpu device
336 * @src: buffer/address where to read from
337 * @dst: buffer/address where to write to
338 * @size: number of bytes to copy
339 * @tmz: if a secure copy should be used
340 * @resv: resv object to sync to
341 * @f: Returns the last fence if multiple jobs are submitted.
343 * The function copies @size bytes from {src->mem + src->offset} to
344 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
345 * move and different for a BO to BO copy.
348 int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
349 const struct amdgpu_copy_mem *src,
350 const struct amdgpu_copy_mem *dst,
351 uint64_t size, bool tmz,
352 struct dma_resv *resv,
353 struct dma_fence **f)
355 const uint32_t GTT_MAX_BYTES = (AMDGPU_GTT_MAX_TRANSFER_SIZE *
356 AMDGPU_GPU_PAGE_SIZE);
358 uint64_t src_node_size, dst_node_size, src_offset, dst_offset;
359 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
360 struct drm_mm_node *src_mm, *dst_mm;
361 struct dma_fence *fence = NULL;
364 if (!adev->mman.buffer_funcs_enabled) {
365 DRM_ERROR("Trying to move memory with ring turned off.\n");
369 src_offset = src->offset;
370 if (src->mem->mm_node) {
371 src_mm = amdgpu_find_mm_node(src->mem, &src_offset);
372 src_node_size = (src_mm->size << PAGE_SHIFT) - src_offset;
375 src_node_size = ULLONG_MAX;
378 dst_offset = dst->offset;
379 if (dst->mem->mm_node) {
380 dst_mm = amdgpu_find_mm_node(dst->mem, &dst_offset);
381 dst_node_size = (dst_mm->size << PAGE_SHIFT) - dst_offset;
384 dst_node_size = ULLONG_MAX;
387 mutex_lock(&adev->mman.gtt_window_lock);
390 uint32_t src_page_offset = src_offset & ~PAGE_MASK;
391 uint32_t dst_page_offset = dst_offset & ~PAGE_MASK;
392 struct dma_fence *next;
396 /* Copy size cannot exceed GTT_MAX_BYTES. So if src or dst
397 * begins at an offset, then adjust the size accordingly
399 cur_size = max(src_page_offset, dst_page_offset);
400 cur_size = min(min3(src_node_size, dst_node_size, size),
401 (uint64_t)(GTT_MAX_BYTES - cur_size));
403 /* Map src to window 0 and dst to window 1. */
404 r = amdgpu_ttm_map_buffer(src->bo, src->mem, src_mm,
405 PFN_UP(cur_size + src_page_offset),
406 src_offset, 0, ring, tmz, &from);
410 r = amdgpu_ttm_map_buffer(dst->bo, dst->mem, dst_mm,
411 PFN_UP(cur_size + dst_page_offset),
412 dst_offset, 1, ring, tmz, &to);
416 r = amdgpu_copy_buffer(ring, from, to, cur_size,
417 resv, &next, false, true, tmz);
421 dma_fence_put(fence);
428 src_node_size -= cur_size;
429 if (!src_node_size) {
431 src_node_size = src_mm->size << PAGE_SHIFT;
434 src_offset += cur_size;
437 dst_node_size -= cur_size;
438 if (!dst_node_size) {
440 dst_node_size = dst_mm->size << PAGE_SHIFT;
443 dst_offset += cur_size;
447 mutex_unlock(&adev->mman.gtt_window_lock);
449 *f = dma_fence_get(fence);
450 dma_fence_put(fence);
455 * amdgpu_move_blit - Copy an entire buffer to another buffer
457 * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
458 * help move buffers to and from VRAM.
460 static int amdgpu_move_blit(struct ttm_buffer_object *bo,
462 struct ttm_resource *new_mem,
463 struct ttm_resource *old_mem)
465 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
466 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
467 struct amdgpu_copy_mem src, dst;
468 struct dma_fence *fence = NULL;
478 r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
479 new_mem->num_pages << PAGE_SHIFT,
480 amdgpu_bo_encrypted(abo),
481 bo->base.resv, &fence);
485 /* clear the space being freed */
486 if (old_mem->mem_type == TTM_PL_VRAM &&
487 (abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)) {
488 struct dma_fence *wipe_fence = NULL;
490 r = amdgpu_fill_buffer(ttm_to_amdgpu_bo(bo), AMDGPU_POISON,
494 } else if (wipe_fence) {
495 dma_fence_put(fence);
500 /* Always block for VM page tables before committing the new location */
501 if (bo->type == ttm_bo_type_kernel)
502 r = ttm_bo_move_accel_cleanup(bo, fence, true, false, new_mem);
504 r = ttm_bo_move_accel_cleanup(bo, fence, evict, true, new_mem);
505 dma_fence_put(fence);
510 dma_fence_wait(fence, false);
511 dma_fence_put(fence);
516 * amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
518 * Called by amdgpu_bo_move()
520 static bool amdgpu_mem_visible(struct amdgpu_device *adev,
521 struct ttm_resource *mem)
523 struct drm_mm_node *nodes = mem->mm_node;
525 if (mem->mem_type == TTM_PL_SYSTEM ||
526 mem->mem_type == TTM_PL_TT)
528 if (mem->mem_type != TTM_PL_VRAM)
531 /* ttm_resource_ioremap only supports contiguous memory */
532 if (nodes->size != mem->num_pages)
535 return ((nodes->start + nodes->size) << PAGE_SHIFT)
536 <= adev->gmc.visible_vram_size;
540 * amdgpu_bo_move - Move a buffer object to a new memory location
542 * Called by ttm_bo_handle_move_mem()
544 static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
545 struct ttm_operation_ctx *ctx,
546 struct ttm_resource *new_mem,
547 struct ttm_place *hop)
549 struct amdgpu_device *adev;
550 struct amdgpu_bo *abo;
551 struct ttm_resource *old_mem = &bo->mem;
554 if ((old_mem->mem_type == TTM_PL_SYSTEM &&
555 new_mem->mem_type == TTM_PL_VRAM) ||
556 (old_mem->mem_type == TTM_PL_VRAM &&
557 new_mem->mem_type == TTM_PL_SYSTEM)) {
560 hop->mem_type = TTM_PL_TT;
565 if (new_mem->mem_type == TTM_PL_TT) {
566 r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, new_mem);
571 amdgpu_bo_move_notify(bo, evict, new_mem);
573 /* Can't move a pinned BO */
574 abo = ttm_to_amdgpu_bo(bo);
575 if (WARN_ON_ONCE(abo->tbo.pin_count > 0))
578 adev = amdgpu_ttm_adev(bo->bdev);
580 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
581 ttm_bo_move_null(bo, new_mem);
584 if (old_mem->mem_type == TTM_PL_SYSTEM &&
585 new_mem->mem_type == TTM_PL_TT) {
586 ttm_bo_move_null(bo, new_mem);
590 if (old_mem->mem_type == TTM_PL_TT &&
591 new_mem->mem_type == TTM_PL_SYSTEM) {
592 r = ttm_bo_wait_ctx(bo, ctx);
596 amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
597 ttm_resource_free(bo, &bo->mem);
598 ttm_bo_assign_mem(bo, new_mem);
602 if (old_mem->mem_type == AMDGPU_PL_GDS ||
603 old_mem->mem_type == AMDGPU_PL_GWS ||
604 old_mem->mem_type == AMDGPU_PL_OA ||
605 new_mem->mem_type == AMDGPU_PL_GDS ||
606 new_mem->mem_type == AMDGPU_PL_GWS ||
607 new_mem->mem_type == AMDGPU_PL_OA) {
608 /* Nothing to save here */
609 ttm_bo_move_null(bo, new_mem);
613 if (!adev->mman.buffer_funcs_enabled) {
618 r = amdgpu_move_blit(bo, evict, new_mem, old_mem);
621 /* Check that all memory is CPU accessible */
622 if (!amdgpu_mem_visible(adev, old_mem) ||
623 !amdgpu_mem_visible(adev, new_mem)) {
624 pr_err("Move buffer fallback to memcpy unavailable\n");
628 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
633 if (bo->type == ttm_bo_type_device &&
634 new_mem->mem_type == TTM_PL_VRAM &&
635 old_mem->mem_type != TTM_PL_VRAM) {
636 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
637 * accesses the BO after it's moved.
639 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
642 /* update statistics */
643 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
646 swap(*new_mem, bo->mem);
647 amdgpu_bo_move_notify(bo, false, new_mem);
648 swap(*new_mem, bo->mem);
653 * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
655 * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
657 static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *mem)
659 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
660 struct drm_mm_node *mm_node = mem->mm_node;
661 size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
663 switch (mem->mem_type) {
670 mem->bus.offset = mem->start << PAGE_SHIFT;
671 /* check if it's visible */
672 if ((mem->bus.offset + bus_size) > adev->gmc.visible_vram_size)
674 /* Only physically contiguous buffers apply. In a contiguous
675 * buffer, size of the first mm_node would match the number of
676 * pages in ttm_resource.
678 if (adev->mman.aper_base_kaddr &&
679 (mm_node->size == mem->num_pages))
680 mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr +
683 mem->bus.offset += adev->gmc.aper_base;
684 mem->bus.is_iomem = true;
685 mem->bus.caching = ttm_write_combined;
693 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
694 unsigned long page_offset)
696 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
697 uint64_t offset = (page_offset << PAGE_SHIFT);
698 struct drm_mm_node *mm;
700 mm = amdgpu_find_mm_node(&bo->mem, &offset);
701 offset += adev->gmc.aper_base;
702 return mm->start + (offset >> PAGE_SHIFT);
706 * amdgpu_ttm_domain_start - Returns GPU start address
707 * @adev: amdgpu device object
708 * @type: type of the memory
711 * GPU start address of a memory domain
714 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type)
718 return adev->gmc.gart_start;
720 return adev->gmc.vram_start;
727 * TTM backend functions.
729 struct amdgpu_ttm_tt {
731 struct drm_gem_object *gobj;
734 struct task_struct *usertask;
737 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
738 struct hmm_range *range;
742 #ifdef CONFIG_DRM_AMDGPU_USERPTR
744 * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user
745 * memory and start HMM tracking CPU page table update
747 * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only
748 * once afterwards to stop HMM tracking
750 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages)
752 struct ttm_tt *ttm = bo->tbo.ttm;
753 struct amdgpu_ttm_tt *gtt = (void *)ttm;
754 unsigned long start = gtt->userptr;
755 struct vm_area_struct *vma;
756 struct hmm_range *range;
757 unsigned long timeout;
758 struct mm_struct *mm;
762 mm = bo->notifier.mm;
764 DRM_DEBUG_DRIVER("BO is not registered?\n");
768 /* Another get_user_pages is running at the same time?? */
769 if (WARN_ON(gtt->range))
772 if (!mmget_not_zero(mm)) /* Happens during process shutdown */
775 range = kzalloc(sizeof(*range), GFP_KERNEL);
776 if (unlikely(!range)) {
780 range->notifier = &bo->notifier;
781 range->start = bo->notifier.interval_tree.start;
782 range->end = bo->notifier.interval_tree.last + 1;
783 range->default_flags = HMM_PFN_REQ_FAULT;
784 if (!amdgpu_ttm_tt_is_readonly(ttm))
785 range->default_flags |= HMM_PFN_REQ_WRITE;
787 range->hmm_pfns = kvmalloc_array(ttm->num_pages,
788 sizeof(*range->hmm_pfns), GFP_KERNEL);
789 if (unlikely(!range->hmm_pfns)) {
791 goto out_free_ranges;
795 vma = find_vma(mm, start);
796 if (unlikely(!vma || start < vma->vm_start)) {
800 if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) &&
805 mmap_read_unlock(mm);
806 timeout = jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
809 range->notifier_seq = mmu_interval_read_begin(&bo->notifier);
812 r = hmm_range_fault(range);
813 mmap_read_unlock(mm);
816 * FIXME: This timeout should encompass the retry from
817 * mmu_interval_read_retry() as well.
819 if (r == -EBUSY && !time_after(jiffies, timeout))
825 * Due to default_flags, all pages are HMM_PFN_VALID or
826 * hmm_range_fault() fails. FIXME: The pages cannot be touched outside
827 * the notifier_lock, and mmu_interval_read_retry() must be done first.
829 for (i = 0; i < ttm->num_pages; i++)
830 pages[i] = hmm_pfn_to_page(range->hmm_pfns[i]);
838 mmap_read_unlock(mm);
840 kvfree(range->hmm_pfns);
849 * amdgpu_ttm_tt_userptr_range_done - stop HMM track the CPU page table change
850 * Check if the pages backing this ttm range have been invalidated
852 * Returns: true if pages are still valid
854 bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm)
856 struct amdgpu_ttm_tt *gtt = (void *)ttm;
859 if (!gtt || !gtt->userptr)
862 DRM_DEBUG_DRIVER("user_pages_done 0x%llx pages 0x%x\n",
863 gtt->userptr, ttm->num_pages);
865 WARN_ONCE(!gtt->range || !gtt->range->hmm_pfns,
866 "No user pages to check\n");
870 * FIXME: Must always hold notifier_lock for this, and must
871 * not ignore the return code.
873 r = mmu_interval_read_retry(gtt->range->notifier,
874 gtt->range->notifier_seq);
875 kvfree(gtt->range->hmm_pfns);
885 * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
887 * Called by amdgpu_cs_list_validate(). This creates the page list
888 * that backs user memory and will ultimately be mapped into the device
891 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
895 for (i = 0; i < ttm->num_pages; ++i)
896 ttm->pages[i] = pages ? pages[i] : NULL;
900 * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages
902 * Called by amdgpu_ttm_backend_bind()
904 static int amdgpu_ttm_tt_pin_userptr(struct ttm_bo_device *bdev,
907 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
908 struct amdgpu_ttm_tt *gtt = (void *)ttm;
911 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
912 enum dma_data_direction direction = write ?
913 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
915 /* Allocate an SG array and squash pages into it */
916 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
917 ttm->num_pages << PAGE_SHIFT,
922 /* Map SG to device */
923 r = dma_map_sgtable(adev->dev, ttm->sg, direction, 0);
927 /* convert SG to linear array of pages and dma addresses */
928 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
929 gtt->ttm.dma_address, ttm->num_pages);
940 * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
942 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_bo_device *bdev,
945 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
946 struct amdgpu_ttm_tt *gtt = (void *)ttm;
948 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
949 enum dma_data_direction direction = write ?
950 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
952 /* double check that we don't free the table twice */
956 /* unmap the pages mapped to the device */
957 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
958 sg_free_table(ttm->sg);
960 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
964 for (i = 0; i < ttm->num_pages; i++) {
966 hmm_pfn_to_page(gtt->range->hmm_pfns[i]))
970 WARN((i == ttm->num_pages), "Missing get_user_page_done\n");
975 static int amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
976 struct ttm_buffer_object *tbo,
979 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo);
980 struct ttm_tt *ttm = tbo->ttm;
981 struct amdgpu_ttm_tt *gtt = (void *)ttm;
984 if (amdgpu_bo_encrypted(abo))
985 flags |= AMDGPU_PTE_TMZ;
987 if (abo->flags & AMDGPU_GEM_CREATE_CP_MQD_GFX9) {
988 uint64_t page_idx = 1;
990 r = amdgpu_gart_bind(adev, gtt->offset, page_idx,
991 ttm->pages, gtt->ttm.dma_address, flags);
995 /* The memory type of the first page defaults to UC. Now
996 * modify the memory type to NC from the second page of
999 flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
1000 flags |= AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC);
1002 r = amdgpu_gart_bind(adev,
1003 gtt->offset + (page_idx << PAGE_SHIFT),
1004 ttm->num_pages - page_idx,
1005 &ttm->pages[page_idx],
1006 &(gtt->ttm.dma_address[page_idx]), flags);
1008 r = amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
1009 ttm->pages, gtt->ttm.dma_address, flags);
1014 DRM_ERROR("failed to bind %u pages at 0x%08llX\n",
1015 ttm->num_pages, gtt->offset);
1021 * amdgpu_ttm_backend_bind - Bind GTT memory
1023 * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
1024 * This handles binding GTT memory to the device address space.
1026 static int amdgpu_ttm_backend_bind(struct ttm_bo_device *bdev,
1028 struct ttm_resource *bo_mem)
1030 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1031 struct amdgpu_ttm_tt *gtt = (void*)ttm;
1042 r = amdgpu_ttm_tt_pin_userptr(bdev, ttm);
1044 DRM_ERROR("failed to pin userptr\n");
1048 if (!ttm->num_pages) {
1049 WARN(1, "nothing to bind %u pages for mreg %p back %p!\n",
1050 ttm->num_pages, bo_mem, ttm);
1053 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
1054 bo_mem->mem_type == AMDGPU_PL_GWS ||
1055 bo_mem->mem_type == AMDGPU_PL_OA)
1058 if (!amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
1059 gtt->offset = AMDGPU_BO_INVALID_OFFSET;
1063 /* compute PTE flags relevant to this BO memory */
1064 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem);
1066 /* bind pages into GART page tables */
1067 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
1068 r = amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
1069 ttm->pages, gtt->ttm.dma_address, flags);
1072 DRM_ERROR("failed to bind %u pages at 0x%08llX\n",
1073 ttm->num_pages, gtt->offset);
1079 * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
1080 * through AGP or GART aperture.
1082 * If bo is accessible through AGP aperture, then use AGP aperture
1083 * to access bo; otherwise allocate logical space in GART aperture
1084 * and map bo to GART aperture.
1086 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
1088 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1089 struct ttm_operation_ctx ctx = { false, false };
1090 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
1091 struct ttm_resource tmp;
1092 struct ttm_placement placement;
1093 struct ttm_place placements;
1094 uint64_t addr, flags;
1097 if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET)
1100 addr = amdgpu_gmc_agp_addr(bo);
1101 if (addr != AMDGPU_BO_INVALID_OFFSET) {
1102 bo->mem.start = addr >> PAGE_SHIFT;
1105 /* allocate GART space */
1108 placement.num_placement = 1;
1109 placement.placement = &placements;
1110 placement.num_busy_placement = 1;
1111 placement.busy_placement = &placements;
1112 placements.fpfn = 0;
1113 placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
1114 placements.mem_type = TTM_PL_TT;
1115 placements.flags = bo->mem.placement;
1117 r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
1121 /* compute PTE flags for this buffer object */
1122 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, &tmp);
1125 gtt->offset = (u64)tmp.start << PAGE_SHIFT;
1126 r = amdgpu_ttm_gart_bind(adev, bo, flags);
1128 ttm_resource_free(bo, &tmp);
1132 ttm_resource_free(bo, &bo->mem);
1140 * amdgpu_ttm_recover_gart - Rebind GTT pages
1142 * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
1143 * rebind GTT pages during a GPU reset.
1145 int amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
1147 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
1154 flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, &tbo->mem);
1155 r = amdgpu_ttm_gart_bind(adev, tbo, flags);
1161 * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
1163 * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
1166 static void amdgpu_ttm_backend_unbind(struct ttm_bo_device *bdev,
1169 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1170 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1176 /* if the pages have userptr pinning then clear that first */
1178 amdgpu_ttm_tt_unpin_userptr(bdev, ttm);
1180 if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
1183 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1184 r = amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages);
1186 DRM_ERROR("failed to unbind %u pages at 0x%08llX\n",
1187 gtt->ttm.num_pages, gtt->offset);
1191 static void amdgpu_ttm_backend_destroy(struct ttm_bo_device *bdev,
1194 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1196 amdgpu_ttm_backend_unbind(bdev, ttm);
1197 ttm_tt_destroy_common(bdev, ttm);
1199 put_task_struct(gtt->usertask);
1201 ttm_tt_fini(>t->ttm);
1206 * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1208 * @bo: The buffer object to create a GTT ttm_tt object around
1210 * Called by ttm_tt_create().
1212 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
1213 uint32_t page_flags)
1215 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1216 struct amdgpu_ttm_tt *gtt;
1217 enum ttm_caching caching;
1219 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
1223 gtt->gobj = &bo->base;
1225 if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
1226 caching = ttm_write_combined;
1228 caching = ttm_cached;
1230 /* allocate space for the uninitialized page entries */
1231 if (ttm_sg_tt_init(>t->ttm, bo, page_flags, caching)) {
1239 * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1241 * Map the pages of a ttm_tt object to an address space visible
1242 * to the underlying device.
1244 static int amdgpu_ttm_tt_populate(struct ttm_bo_device *bdev,
1246 struct ttm_operation_ctx *ctx)
1248 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1249 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1251 /* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1252 if (gtt && gtt->userptr) {
1253 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
1257 ttm->page_flags |= TTM_PAGE_FLAG_SG;
1261 if (ttm->page_flags & TTM_PAGE_FLAG_SG) {
1263 struct dma_buf_attachment *attach;
1264 struct sg_table *sgt;
1266 attach = gtt->gobj->import_attach;
1267 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
1269 return PTR_ERR(sgt);
1274 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1275 gtt->ttm.dma_address,
1280 return ttm_pool_alloc(&adev->mman.bdev.pool, ttm, ctx);
1284 * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1286 * Unmaps pages of a ttm_tt object from the device address space and
1287 * unpopulates the page array backing it.
1289 static void amdgpu_ttm_tt_unpopulate(struct ttm_bo_device *bdev,
1292 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1293 struct amdgpu_device *adev;
1295 if (gtt && gtt->userptr) {
1296 amdgpu_ttm_tt_set_user_pages(ttm, NULL);
1298 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
1302 if (ttm->sg && gtt->gobj->import_attach) {
1303 struct dma_buf_attachment *attach;
1305 attach = gtt->gobj->import_attach;
1306 dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
1311 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
1314 adev = amdgpu_ttm_adev(bdev);
1315 return ttm_pool_free(&adev->mman.bdev.pool, ttm);
1319 * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1322 * @bo: The ttm_buffer_object to bind this userptr to
1323 * @addr: The address in the current tasks VM space to use
1324 * @flags: Requirements of userptr object.
1326 * Called by amdgpu_gem_userptr_ioctl() to bind userptr pages
1329 int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
1330 uint64_t addr, uint32_t flags)
1332 struct amdgpu_ttm_tt *gtt;
1335 /* TODO: We want a separate TTM object type for userptrs */
1336 bo->ttm = amdgpu_ttm_tt_create(bo, 0);
1337 if (bo->ttm == NULL)
1341 gtt = (void *)bo->ttm;
1342 gtt->userptr = addr;
1343 gtt->userflags = flags;
1346 put_task_struct(gtt->usertask);
1347 gtt->usertask = current->group_leader;
1348 get_task_struct(gtt->usertask);
1354 * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1356 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1358 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1363 if (gtt->usertask == NULL)
1366 return gtt->usertask->mm;
1370 * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1371 * address range for the current task.
1374 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1377 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1380 if (gtt == NULL || !gtt->userptr)
1383 /* Return false if no part of the ttm_tt object lies within
1386 size = (unsigned long)gtt->ttm.num_pages * PAGE_SIZE;
1387 if (gtt->userptr > end || gtt->userptr + size <= start)
1394 * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
1396 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
1398 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1400 if (gtt == NULL || !gtt->userptr)
1407 * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1409 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1411 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1416 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1420 * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object
1422 * @ttm: The ttm_tt object to compute the flags for
1423 * @mem: The memory registry backing this ttm_tt object
1425 * Figure out the flags to use for a VM PDE (Page Directory Entry).
1427 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem)
1431 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1432 flags |= AMDGPU_PTE_VALID;
1434 if (mem && mem->mem_type == TTM_PL_TT) {
1435 flags |= AMDGPU_PTE_SYSTEM;
1437 if (ttm->caching == ttm_cached)
1438 flags |= AMDGPU_PTE_SNOOPED;
1445 * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1447 * @ttm: The ttm_tt object to compute the flags for
1448 * @mem: The memory registry backing this ttm_tt object
1450 * Figure out the flags to use for a VM PTE (Page Table Entry).
1452 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1453 struct ttm_resource *mem)
1455 uint64_t flags = amdgpu_ttm_tt_pde_flags(ttm, mem);
1457 flags |= adev->gart.gart_pte_flags;
1458 flags |= AMDGPU_PTE_READABLE;
1460 if (!amdgpu_ttm_tt_is_readonly(ttm))
1461 flags |= AMDGPU_PTE_WRITEABLE;
1467 * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1470 * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1471 * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1472 * it can find space for a new object and by ttm_bo_force_list_clean() which is
1473 * used to clean out a memory space.
1475 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1476 const struct ttm_place *place)
1478 unsigned long num_pages = bo->mem.num_pages;
1479 struct drm_mm_node *node = bo->mem.mm_node;
1480 struct dma_resv_list *flist;
1481 struct dma_fence *f;
1484 if (bo->type == ttm_bo_type_kernel &&
1485 !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)))
1488 /* If bo is a KFD BO, check if the bo belongs to the current process.
1489 * If true, then return false as any KFD process needs all its BOs to
1490 * be resident to run successfully
1492 flist = dma_resv_get_list(bo->base.resv);
1494 for (i = 0; i < flist->shared_count; ++i) {
1495 f = rcu_dereference_protected(flist->shared[i],
1496 dma_resv_held(bo->base.resv));
1497 if (amdkfd_fence_check_mm(f, current->mm))
1502 switch (bo->mem.mem_type) {
1504 if (amdgpu_bo_is_amdgpu_bo(bo) &&
1505 amdgpu_bo_encrypted(ttm_to_amdgpu_bo(bo)))
1510 /* Check each drm MM node individually */
1512 if (place->fpfn < (node->start + node->size) &&
1513 !(place->lpfn && place->lpfn <= node->start))
1516 num_pages -= node->size;
1525 return ttm_bo_eviction_valuable(bo, place);
1529 * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1531 * @bo: The buffer object to read/write
1532 * @offset: Offset into buffer object
1533 * @buf: Secondary buffer to write/read from
1534 * @len: Length in bytes of access
1535 * @write: true if writing
1537 * This is used to access VRAM that backs a buffer object via MMIO
1538 * access for debugging purposes.
1540 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1541 unsigned long offset,
1542 void *buf, int len, int write)
1544 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1545 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1546 struct drm_mm_node *nodes;
1550 unsigned long flags;
1552 if (bo->mem.mem_type != TTM_PL_VRAM)
1556 nodes = amdgpu_find_mm_node(&abo->tbo.mem, &pos);
1557 pos += (nodes->start << PAGE_SHIFT);
1559 while (len && pos < adev->gmc.mc_vram_size) {
1560 uint64_t aligned_pos = pos & ~(uint64_t)3;
1561 uint64_t bytes = 4 - (pos & 3);
1562 uint32_t shift = (pos & 3) * 8;
1563 uint32_t mask = 0xffffffff << shift;
1566 mask &= 0xffffffff >> (bytes - len) * 8;
1570 if (mask != 0xffffffff) {
1571 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1572 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
1573 WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
1574 if (!write || mask != 0xffffffff)
1575 value = RREG32_NO_KIQ(mmMM_DATA);
1578 value |= (*(uint32_t *)buf << shift) & mask;
1579 WREG32_NO_KIQ(mmMM_DATA, value);
1581 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1583 value = (value & mask) >> shift;
1584 memcpy(buf, &value, bytes);
1587 bytes = (nodes->start + nodes->size) << PAGE_SHIFT;
1588 bytes = min(bytes - pos, (uint64_t)len & ~0x3ull);
1590 amdgpu_device_vram_access(adev, pos, (uint32_t *)buf,
1595 buf = (uint8_t *)buf + bytes;
1598 if (pos >= (nodes->start + nodes->size) << PAGE_SHIFT) {
1600 pos = (nodes->start << PAGE_SHIFT);
1608 amdgpu_bo_delete_mem_notify(struct ttm_buffer_object *bo)
1610 amdgpu_bo_move_notify(bo, false, NULL);
1613 static struct ttm_bo_driver amdgpu_bo_driver = {
1614 .ttm_tt_create = &amdgpu_ttm_tt_create,
1615 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1616 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1617 .ttm_tt_destroy = &amdgpu_ttm_backend_destroy,
1618 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1619 .evict_flags = &amdgpu_evict_flags,
1620 .move = &amdgpu_bo_move,
1621 .verify_access = &amdgpu_verify_access,
1622 .delete_mem_notify = &amdgpu_bo_delete_mem_notify,
1623 .release_notify = &amdgpu_bo_release_notify,
1624 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1625 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1626 .access_memory = &amdgpu_ttm_access_memory,
1627 .del_from_lru_notify = &amdgpu_vm_del_from_lru_notify
1631 * Firmware Reservation functions
1634 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1636 * @adev: amdgpu_device pointer
1638 * free fw reserved vram if it has been reserved.
1640 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1642 amdgpu_bo_free_kernel(&adev->mman.fw_vram_usage_reserved_bo,
1643 NULL, &adev->mman.fw_vram_usage_va);
1647 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1649 * @adev: amdgpu_device pointer
1651 * create bo vram reservation from fw.
1653 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1655 uint64_t vram_size = adev->gmc.visible_vram_size;
1657 adev->mman.fw_vram_usage_va = NULL;
1658 adev->mman.fw_vram_usage_reserved_bo = NULL;
1660 if (adev->mman.fw_vram_usage_size == 0 ||
1661 adev->mman.fw_vram_usage_size > vram_size)
1664 return amdgpu_bo_create_kernel_at(adev,
1665 adev->mman.fw_vram_usage_start_offset,
1666 adev->mman.fw_vram_usage_size,
1667 AMDGPU_GEM_DOMAIN_VRAM,
1668 &adev->mman.fw_vram_usage_reserved_bo,
1669 &adev->mman.fw_vram_usage_va);
1673 * Memoy training reservation functions
1677 * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram
1679 * @adev: amdgpu_device pointer
1681 * free memory training reserved vram if it has been reserved.
1683 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev)
1685 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1687 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
1688 amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL);
1694 static void amdgpu_ttm_training_data_block_init(struct amdgpu_device *adev)
1696 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1698 memset(ctx, 0, sizeof(*ctx));
1700 ctx->c2p_train_data_offset =
1701 ALIGN((adev->gmc.mc_vram_size - adev->mman.discovery_tmr_size - SZ_1M), SZ_1M);
1702 ctx->p2c_train_data_offset =
1703 (adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET);
1704 ctx->train_data_size =
1705 GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES;
1707 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
1708 ctx->train_data_size,
1709 ctx->p2c_train_data_offset,
1710 ctx->c2p_train_data_offset);
1714 * reserve TMR memory at the top of VRAM which holds
1715 * IP Discovery data and is protected by PSP.
1717 static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
1720 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1721 bool mem_train_support = false;
1723 if (!amdgpu_sriov_vf(adev)) {
1724 ret = amdgpu_mem_train_support(adev);
1726 mem_train_support = true;
1730 DRM_DEBUG("memory training does not support!\n");
1734 * Query reserved tmr size through atom firmwareinfo for Sienna_Cichlid and onwards for all
1735 * the use cases (IP discovery/G6 memory training/profiling/diagnostic data.etc)
1737 * Otherwise, fallback to legacy approach to check and reserve tmr block for ip
1738 * discovery data and G6 memory training data respectively
1740 adev->mman.discovery_tmr_size =
1741 amdgpu_atomfirmware_get_fw_reserved_fb_size(adev);
1742 if (!adev->mman.discovery_tmr_size)
1743 adev->mman.discovery_tmr_size = DISCOVERY_TMR_OFFSET;
1745 if (mem_train_support) {
1746 /* reserve vram for mem train according to TMR location */
1747 amdgpu_ttm_training_data_block_init(adev);
1748 ret = amdgpu_bo_create_kernel_at(adev,
1749 ctx->c2p_train_data_offset,
1750 ctx->train_data_size,
1751 AMDGPU_GEM_DOMAIN_VRAM,
1755 DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
1756 amdgpu_ttm_training_reserve_vram_fini(adev);
1759 ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS;
1762 ret = amdgpu_bo_create_kernel_at(adev,
1763 adev->gmc.real_vram_size - adev->mman.discovery_tmr_size,
1764 adev->mman.discovery_tmr_size,
1765 AMDGPU_GEM_DOMAIN_VRAM,
1766 &adev->mman.discovery_memory,
1769 DRM_ERROR("alloc tmr failed(%d)!\n", ret);
1770 amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1778 * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1779 * gtt/vram related fields.
1781 * This initializes all of the memory space pools that the TTM layer
1782 * will need such as the GTT space (system memory mapped to the device),
1783 * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1784 * can be mapped per VMID.
1786 int amdgpu_ttm_init(struct amdgpu_device *adev)
1792 mutex_init(&adev->mman.gtt_window_lock);
1794 /* No others user of address space so set it to 0 */
1795 r = ttm_bo_device_init(&adev->mman.bdev, &amdgpu_bo_driver, adev->dev,
1796 adev_to_drm(adev)->anon_inode->i_mapping,
1797 adev_to_drm(adev)->vma_offset_manager,
1799 dma_addressing_limited(adev->dev));
1801 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1804 adev->mman.initialized = true;
1806 /* Initialize VRAM pool with all of VRAM divided into pages */
1807 r = amdgpu_vram_mgr_init(adev);
1809 DRM_ERROR("Failed initializing VRAM heap.\n");
1813 /* Reduce size of CPU-visible VRAM if requested */
1814 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1815 if (amdgpu_vis_vram_limit > 0 &&
1816 vis_vram_limit <= adev->gmc.visible_vram_size)
1817 adev->gmc.visible_vram_size = vis_vram_limit;
1819 /* Change the size here instead of the init above so only lpfn is affected */
1820 amdgpu_ttm_set_buffer_funcs_status(adev, false);
1822 adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base,
1823 adev->gmc.visible_vram_size);
1827 *The reserved vram for firmware must be pinned to the specified
1828 *place on the VRAM, so reserve it early.
1830 r = amdgpu_ttm_fw_reserve_vram_init(adev);
1836 * only NAVI10 and onwards ASIC support for IP discovery.
1837 * If IP discovery enabled, a block of memory should be
1838 * reserved for IP discovey.
1840 if (adev->mman.discovery_bin) {
1841 r = amdgpu_ttm_reserve_tmr(adev);
1846 /* allocate memory as required for VGA
1847 * This is used for VGA emulation and pre-OS scanout buffers to
1848 * avoid display artifacts while transitioning between pre-OS
1850 r = amdgpu_bo_create_kernel_at(adev, 0, adev->mman.stolen_vga_size,
1851 AMDGPU_GEM_DOMAIN_VRAM,
1852 &adev->mman.stolen_vga_memory,
1856 r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
1857 adev->mman.stolen_extended_size,
1858 AMDGPU_GEM_DOMAIN_VRAM,
1859 &adev->mman.stolen_extended_memory,
1864 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1865 (unsigned) (adev->gmc.real_vram_size / (1024 * 1024)));
1867 /* Compute GTT size, either bsaed on 3/4th the size of RAM size
1868 * or whatever the user passed on module init */
1869 if (amdgpu_gtt_size == -1) {
1873 gtt_size = min(max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1874 adev->gmc.mc_vram_size),
1875 ((uint64_t)si.totalram * si.mem_unit * 3/4));
1878 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1880 /* Initialize GTT memory pool */
1881 r = amdgpu_gtt_mgr_init(adev, gtt_size);
1883 DRM_ERROR("Failed initializing GTT heap.\n");
1886 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1887 (unsigned)(gtt_size / (1024 * 1024)));
1889 /* Initialize various on-chip memory pools */
1890 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GDS, adev->gds.gds_size);
1892 DRM_ERROR("Failed initializing GDS heap.\n");
1896 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GWS, adev->gds.gws_size);
1898 DRM_ERROR("Failed initializing gws heap.\n");
1902 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_OA, adev->gds.oa_size);
1904 DRM_ERROR("Failed initializing oa heap.\n");
1912 * amdgpu_ttm_late_init - Handle any late initialization for amdgpu_ttm
1914 void amdgpu_ttm_late_init(struct amdgpu_device *adev)
1916 /* return the VGA stolen memory (if any) back to VRAM */
1917 if (!adev->mman.keep_stolen_vga_memory)
1918 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
1919 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
1923 * amdgpu_ttm_fini - De-initialize the TTM memory pools
1925 void amdgpu_ttm_fini(struct amdgpu_device *adev)
1927 if (!adev->mman.initialized)
1930 amdgpu_ttm_training_reserve_vram_fini(adev);
1931 /* return the stolen vga memory back to VRAM */
1932 if (adev->mman.keep_stolen_vga_memory)
1933 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
1934 /* return the IP Discovery TMR memory back to VRAM */
1935 amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1936 amdgpu_ttm_fw_reserve_vram_fini(adev);
1938 if (adev->mman.aper_base_kaddr)
1939 iounmap(adev->mman.aper_base_kaddr);
1940 adev->mman.aper_base_kaddr = NULL;
1942 amdgpu_vram_mgr_fini(adev);
1943 amdgpu_gtt_mgr_fini(adev);
1944 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GDS);
1945 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GWS);
1946 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
1947 ttm_bo_device_release(&adev->mman.bdev);
1948 adev->mman.initialized = false;
1949 DRM_INFO("amdgpu: ttm finalized\n");
1953 * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
1955 * @adev: amdgpu_device pointer
1956 * @enable: true when we can use buffer functions.
1958 * Enable/disable use of buffer functions during suspend/resume. This should
1959 * only be called at bootup or when userspace isn't running.
1961 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
1963 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
1967 if (!adev->mman.initialized || amdgpu_in_reset(adev) ||
1968 adev->mman.buffer_funcs_enabled == enable)
1972 struct amdgpu_ring *ring;
1973 struct drm_gpu_scheduler *sched;
1975 ring = adev->mman.buffer_funcs_ring;
1976 sched = &ring->sched;
1977 r = drm_sched_entity_init(&adev->mman.entity,
1978 DRM_SCHED_PRIORITY_KERNEL, &sched,
1981 DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
1986 drm_sched_entity_destroy(&adev->mman.entity);
1987 dma_fence_put(man->move);
1991 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1993 size = adev->gmc.real_vram_size;
1995 size = adev->gmc.visible_vram_size;
1996 man->size = size >> PAGE_SHIFT;
1997 adev->mman.buffer_funcs_enabled = enable;
2000 static vm_fault_t amdgpu_ttm_fault(struct vm_fault *vmf)
2002 struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
2005 ret = ttm_bo_vm_reserve(bo, vmf);
2009 ret = amdgpu_bo_fault_reserve_notify(bo);
2013 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
2014 TTM_BO_VM_NUM_PREFAULT, 1);
2015 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
2019 dma_resv_unlock(bo->base.resv);
2023 static struct vm_operations_struct amdgpu_ttm_vm_ops = {
2024 .fault = amdgpu_ttm_fault,
2025 .open = ttm_bo_vm_open,
2026 .close = ttm_bo_vm_close,
2027 .access = ttm_bo_vm_access
2030 int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
2032 struct drm_file *file_priv = filp->private_data;
2033 struct amdgpu_device *adev = drm_to_adev(file_priv->minor->dev);
2036 r = ttm_bo_mmap(filp, vma, &adev->mman.bdev);
2037 if (unlikely(r != 0))
2040 vma->vm_ops = &amdgpu_ttm_vm_ops;
2044 int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
2045 uint64_t dst_offset, uint32_t byte_count,
2046 struct dma_resv *resv,
2047 struct dma_fence **fence, bool direct_submit,
2048 bool vm_needs_flush, bool tmz)
2050 enum amdgpu_ib_pool_type pool = direct_submit ? AMDGPU_IB_POOL_DIRECT :
2051 AMDGPU_IB_POOL_DELAYED;
2052 struct amdgpu_device *adev = ring->adev;
2053 struct amdgpu_job *job;
2056 unsigned num_loops, num_dw;
2060 if (direct_submit && !ring->sched.ready) {
2061 DRM_ERROR("Trying to move memory with ring turned off.\n");
2065 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
2066 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
2067 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->copy_num_dw, 8);
2069 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, pool, &job);
2073 if (vm_needs_flush) {
2074 job->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gart.bo);
2075 job->vm_needs_flush = true;
2078 r = amdgpu_sync_resv(adev, &job->sync, resv,
2080 AMDGPU_FENCE_OWNER_UNDEFINED);
2082 DRM_ERROR("sync failed (%d).\n", r);
2087 for (i = 0; i < num_loops; i++) {
2088 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2090 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
2091 dst_offset, cur_size_in_bytes, tmz);
2093 src_offset += cur_size_in_bytes;
2094 dst_offset += cur_size_in_bytes;
2095 byte_count -= cur_size_in_bytes;
2098 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2099 WARN_ON(job->ibs[0].length_dw > num_dw);
2101 r = amdgpu_job_submit_direct(job, ring, fence);
2103 r = amdgpu_job_submit(job, &adev->mman.entity,
2104 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2111 amdgpu_job_free(job);
2112 DRM_ERROR("Error scheduling IBs (%d)\n", r);
2116 int amdgpu_fill_buffer(struct amdgpu_bo *bo,
2118 struct dma_resv *resv,
2119 struct dma_fence **fence)
2121 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2122 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
2123 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2125 struct drm_mm_node *mm_node;
2126 unsigned long num_pages;
2127 unsigned int num_loops, num_dw;
2129 struct amdgpu_job *job;
2132 if (!adev->mman.buffer_funcs_enabled) {
2133 DRM_ERROR("Trying to clear memory with ring turned off.\n");
2137 if (bo->tbo.mem.mem_type == TTM_PL_TT) {
2138 r = amdgpu_ttm_alloc_gart(&bo->tbo);
2143 num_pages = bo->tbo.num_pages;
2144 mm_node = bo->tbo.mem.mm_node;
2147 uint64_t byte_count = mm_node->size << PAGE_SHIFT;
2149 num_loops += DIV_ROUND_UP_ULL(byte_count, max_bytes);
2150 num_pages -= mm_node->size;
2153 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
2155 /* for IB padding */
2158 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, AMDGPU_IB_POOL_DELAYED,
2164 r = amdgpu_sync_resv(adev, &job->sync, resv,
2166 AMDGPU_FENCE_OWNER_UNDEFINED);
2168 DRM_ERROR("sync failed (%d).\n", r);
2173 num_pages = bo->tbo.num_pages;
2174 mm_node = bo->tbo.mem.mm_node;
2177 uint64_t byte_count = mm_node->size << PAGE_SHIFT;
2180 dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
2181 while (byte_count) {
2182 uint32_t cur_size_in_bytes = min_t(uint64_t, byte_count,
2185 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
2186 dst_addr, cur_size_in_bytes);
2188 dst_addr += cur_size_in_bytes;
2189 byte_count -= cur_size_in_bytes;
2192 num_pages -= mm_node->size;
2196 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2197 WARN_ON(job->ibs[0].length_dw > num_dw);
2198 r = amdgpu_job_submit(job, &adev->mman.entity,
2199 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2206 amdgpu_job_free(job);
2210 #if defined(CONFIG_DEBUG_FS)
2212 static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
2214 struct drm_info_node *node = (struct drm_info_node *)m->private;
2215 unsigned ttm_pl = (uintptr_t)node->info_ent->data;
2216 struct drm_device *dev = node->minor->dev;
2217 struct amdgpu_device *adev = drm_to_adev(dev);
2218 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, ttm_pl);
2219 struct drm_printer p = drm_seq_file_printer(m);
2221 man->func->debug(man, &p);
2225 static int amdgpu_ttm_pool_debugfs(struct seq_file *m, void *data)
2227 struct drm_info_node *node = (struct drm_info_node *)m->private;
2228 struct drm_device *dev = node->minor->dev;
2229 struct amdgpu_device *adev = drm_to_adev(dev);
2231 return ttm_pool_debugfs(&adev->mman.bdev.pool, m);
2234 static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
2235 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, (void *)TTM_PL_VRAM},
2236 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, (void *)TTM_PL_TT},
2237 {"amdgpu_gds_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_GDS},
2238 {"amdgpu_gws_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_GWS},
2239 {"amdgpu_oa_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_OA},
2240 {"ttm_page_pool", amdgpu_ttm_pool_debugfs, 0, NULL},
2244 * amdgpu_ttm_vram_read - Linear read access to VRAM
2246 * Accesses VRAM via MMIO for debugging purposes.
2248 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
2249 size_t size, loff_t *pos)
2251 struct amdgpu_device *adev = file_inode(f)->i_private;
2254 if (size & 0x3 || *pos & 0x3)
2257 if (*pos >= adev->gmc.mc_vram_size)
2260 size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos));
2262 size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4);
2263 uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ];
2265 amdgpu_device_vram_access(adev, *pos, value, bytes, false);
2266 if (copy_to_user(buf, value, bytes))
2279 * amdgpu_ttm_vram_write - Linear write access to VRAM
2281 * Accesses VRAM via MMIO for debugging purposes.
2283 static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
2284 size_t size, loff_t *pos)
2286 struct amdgpu_device *adev = file_inode(f)->i_private;
2290 if (size & 0x3 || *pos & 0x3)
2293 if (*pos >= adev->gmc.mc_vram_size)
2297 unsigned long flags;
2300 if (*pos >= adev->gmc.mc_vram_size)
2303 r = get_user(value, (uint32_t *)buf);
2307 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
2308 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
2309 WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
2310 WREG32_NO_KIQ(mmMM_DATA, value);
2311 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
2322 static const struct file_operations amdgpu_ttm_vram_fops = {
2323 .owner = THIS_MODULE,
2324 .read = amdgpu_ttm_vram_read,
2325 .write = amdgpu_ttm_vram_write,
2326 .llseek = default_llseek,
2329 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2332 * amdgpu_ttm_gtt_read - Linear read access to GTT memory
2334 static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
2335 size_t size, loff_t *pos)
2337 struct amdgpu_device *adev = file_inode(f)->i_private;
2342 loff_t p = *pos / PAGE_SIZE;
2343 unsigned off = *pos & ~PAGE_MASK;
2344 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
2348 if (p >= adev->gart.num_cpu_pages)
2351 page = adev->gart.pages[p];
2356 r = copy_to_user(buf, ptr, cur_size);
2357 kunmap(adev->gart.pages[p]);
2359 r = clear_user(buf, cur_size);
2373 static const struct file_operations amdgpu_ttm_gtt_fops = {
2374 .owner = THIS_MODULE,
2375 .read = amdgpu_ttm_gtt_read,
2376 .llseek = default_llseek
2382 * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2384 * This function is used to read memory that has been mapped to the
2385 * GPU and the known addresses are not physical addresses but instead
2386 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2388 static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
2389 size_t size, loff_t *pos)
2391 struct amdgpu_device *adev = file_inode(f)->i_private;
2392 struct iommu_domain *dom;
2396 /* retrieve the IOMMU domain if any for this device */
2397 dom = iommu_get_domain_for_dev(adev->dev);
2400 phys_addr_t addr = *pos & PAGE_MASK;
2401 loff_t off = *pos & ~PAGE_MASK;
2402 size_t bytes = PAGE_SIZE - off;
2407 bytes = bytes < size ? bytes : size;
2409 /* Translate the bus address to a physical address. If
2410 * the domain is NULL it means there is no IOMMU active
2411 * and the address translation is the identity
2413 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2415 pfn = addr >> PAGE_SHIFT;
2416 if (!pfn_valid(pfn))
2419 p = pfn_to_page(pfn);
2420 if (p->mapping != adev->mman.bdev.dev_mapping)
2424 r = copy_to_user(buf, ptr + off, bytes);
2438 * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2440 * This function is used to write memory that has been mapped to the
2441 * GPU and the known addresses are not physical addresses but instead
2442 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2444 static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
2445 size_t size, loff_t *pos)
2447 struct amdgpu_device *adev = file_inode(f)->i_private;
2448 struct iommu_domain *dom;
2452 dom = iommu_get_domain_for_dev(adev->dev);
2455 phys_addr_t addr = *pos & PAGE_MASK;
2456 loff_t off = *pos & ~PAGE_MASK;
2457 size_t bytes = PAGE_SIZE - off;
2462 bytes = bytes < size ? bytes : size;
2464 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2466 pfn = addr >> PAGE_SHIFT;
2467 if (!pfn_valid(pfn))
2470 p = pfn_to_page(pfn);
2471 if (p->mapping != adev->mman.bdev.dev_mapping)
2475 r = copy_from_user(ptr + off, buf, bytes);
2488 static const struct file_operations amdgpu_ttm_iomem_fops = {
2489 .owner = THIS_MODULE,
2490 .read = amdgpu_iomem_read,
2491 .write = amdgpu_iomem_write,
2492 .llseek = default_llseek
2495 static const struct {
2497 const struct file_operations *fops;
2499 } ttm_debugfs_entries[] = {
2500 { "amdgpu_vram", &amdgpu_ttm_vram_fops, TTM_PL_VRAM },
2501 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2502 { "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
2504 { "amdgpu_iomem", &amdgpu_ttm_iomem_fops, TTM_PL_SYSTEM },
2509 int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
2511 #if defined(CONFIG_DEBUG_FS)
2514 struct drm_minor *minor = adev_to_drm(adev)->primary;
2515 struct dentry *ent, *root = minor->debugfs_root;
2517 for (count = 0; count < ARRAY_SIZE(ttm_debugfs_entries); count++) {
2518 ent = debugfs_create_file(
2519 ttm_debugfs_entries[count].name,
2520 S_IFREG | S_IRUGO, root,
2522 ttm_debugfs_entries[count].fops);
2524 return PTR_ERR(ent);
2525 if (ttm_debugfs_entries[count].domain == TTM_PL_VRAM)
2526 i_size_write(ent->d_inode, adev->gmc.mc_vram_size);
2527 else if (ttm_debugfs_entries[count].domain == TTM_PL_TT)
2528 i_size_write(ent->d_inode, adev->gmc.gart_size);
2529 adev->mman.debugfs_entries[count] = ent;
2532 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
2533 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);