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>
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>
50 #include <drm/ttm/ttm_page_alloc.h>
52 #include <drm/drm_debugfs.h>
53 #include <drm/amdgpu_drm.h>
56 #include "amdgpu_object.h"
57 #include "amdgpu_trace.h"
58 #include "amdgpu_amdkfd.h"
59 #include "amdgpu_sdma.h"
60 #include "amdgpu_ras.h"
61 #include "amdgpu_atomfirmware.h"
62 #include "bif/bif_4_1_d.h"
64 #define AMDGPU_TTM_VRAM_MAX_DW_READ (size_t)128
66 static int amdgpu_ttm_backend_bind(struct ttm_bo_device *bdev,
68 struct ttm_resource *bo_mem);
70 static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
74 return ttm_range_man_init(&adev->mman.bdev, type,
75 false, size >> PAGE_SHIFT);
79 * amdgpu_evict_flags - Compute placement flags
81 * @bo: The buffer object to evict
82 * @placement: Possible destination(s) for evicted BO
84 * Fill in placement data when ttm_bo_evict() is called
86 static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
87 struct ttm_placement *placement)
89 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
90 struct amdgpu_bo *abo;
91 static const struct ttm_place placements = {
94 .mem_type = TTM_PL_SYSTEM,
98 /* Don't handle scatter gather BOs */
99 if (bo->type == ttm_bo_type_sg) {
100 placement->num_placement = 0;
101 placement->num_busy_placement = 0;
105 /* Object isn't an AMDGPU object so ignore */
106 if (!amdgpu_bo_is_amdgpu_bo(bo)) {
107 placement->placement = &placements;
108 placement->busy_placement = &placements;
109 placement->num_placement = 1;
110 placement->num_busy_placement = 1;
114 abo = ttm_to_amdgpu_bo(bo);
115 switch (bo->mem.mem_type) {
119 placement->num_placement = 0;
120 placement->num_busy_placement = 0;
124 if (!adev->mman.buffer_funcs_enabled) {
125 /* Move to system memory */
126 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
127 } else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
128 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
129 amdgpu_bo_in_cpu_visible_vram(abo)) {
131 /* Try evicting to the CPU inaccessible part of VRAM
132 * first, but only set GTT as busy placement, so this
133 * BO will be evicted to GTT rather than causing other
134 * BOs to be evicted from VRAM
136 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
137 AMDGPU_GEM_DOMAIN_GTT);
138 abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
139 abo->placements[0].lpfn = 0;
140 abo->placement.busy_placement = &abo->placements[1];
141 abo->placement.num_busy_placement = 1;
143 /* Move to GTT memory */
144 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
149 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
152 *placement = abo->placement;
156 * amdgpu_verify_access - Verify access for a mmap call
158 * @bo: The buffer object to map
159 * @filp: The file pointer from the process performing the mmap
161 * This is called by ttm_bo_mmap() to verify whether a process
162 * has the right to mmap a BO to their process space.
164 static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
166 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
169 * Don't verify access for KFD BOs. They don't have a GEM
170 * object associated with them.
175 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
177 return drm_vma_node_verify_access(&abo->tbo.base.vma_node,
182 * amdgpu_mm_node_addr - Compute the GPU relative offset of a GTT buffer.
184 * @bo: The bo to assign the memory to.
185 * @mm_node: Memory manager node for drm allocator.
186 * @mem: The region where the bo resides.
189 static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
190 struct drm_mm_node *mm_node,
191 struct ttm_resource *mem)
195 if (mm_node->start != AMDGPU_BO_INVALID_OFFSET) {
196 addr = mm_node->start << PAGE_SHIFT;
197 addr += amdgpu_ttm_domain_start(amdgpu_ttm_adev(bo->bdev),
204 * amdgpu_find_mm_node - Helper function finds the drm_mm_node corresponding to
205 * @offset. It also modifies the offset to be within the drm_mm_node returned
207 * @mem: The region where the bo resides.
208 * @offset: The offset that drm_mm_node is used for finding.
211 static struct drm_mm_node *amdgpu_find_mm_node(struct ttm_resource *mem,
214 struct drm_mm_node *mm_node = mem->mm_node;
216 while (*offset >= (mm_node->size << PAGE_SHIFT)) {
217 *offset -= (mm_node->size << PAGE_SHIFT);
224 * amdgpu_ttm_map_buffer - Map memory into the GART windows
225 * @bo: buffer object to map
226 * @mem: memory object to map
227 * @mm_node: drm_mm node object to map
228 * @num_pages: number of pages to map
229 * @offset: offset into @mm_node where to start
230 * @window: which GART window to use
231 * @ring: DMA ring to use for the copy
232 * @tmz: if we should setup a TMZ enabled mapping
233 * @addr: resulting address inside the MC address space
235 * Setup one of the GART windows to access a specific piece of memory or return
236 * the physical address for local memory.
238 static int amdgpu_ttm_map_buffer(struct ttm_buffer_object *bo,
239 struct ttm_resource *mem,
240 struct drm_mm_node *mm_node,
241 unsigned num_pages, uint64_t offset,
242 unsigned window, struct amdgpu_ring *ring,
243 bool tmz, uint64_t *addr)
245 struct amdgpu_device *adev = ring->adev;
246 struct amdgpu_job *job;
247 unsigned num_dw, num_bytes;
248 struct dma_fence *fence;
249 uint64_t src_addr, dst_addr;
255 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
256 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
258 /* Map only what can't be accessed directly */
259 if (!tmz && mem->start != AMDGPU_BO_INVALID_OFFSET) {
260 *addr = amdgpu_mm_node_addr(bo, mm_node, mem) + offset;
264 *addr = adev->gmc.gart_start;
265 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
266 AMDGPU_GPU_PAGE_SIZE;
267 *addr += offset & ~PAGE_MASK;
269 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
270 num_bytes = num_pages * 8;
272 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes,
273 AMDGPU_IB_POOL_DELAYED, &job);
277 src_addr = num_dw * 4;
278 src_addr += job->ibs[0].gpu_addr;
280 dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
281 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
282 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
283 dst_addr, num_bytes, false);
285 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
286 WARN_ON(job->ibs[0].length_dw > num_dw);
288 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, mem);
290 flags |= AMDGPU_PTE_TMZ;
292 cpu_addr = &job->ibs[0].ptr[num_dw];
294 if (mem->mem_type == TTM_PL_TT) {
295 struct ttm_dma_tt *dma;
296 dma_addr_t *dma_address;
298 dma = container_of(bo->ttm, struct ttm_dma_tt, ttm);
299 dma_address = &dma->dma_address[offset >> PAGE_SHIFT];
300 r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
305 dma_addr_t dma_address;
307 dma_address = (mm_node->start << PAGE_SHIFT) + offset;
308 dma_address += adev->vm_manager.vram_base_offset;
310 for (i = 0; i < num_pages; ++i) {
311 r = amdgpu_gart_map(adev, i << PAGE_SHIFT, 1,
312 &dma_address, flags, cpu_addr);
316 dma_address += PAGE_SIZE;
320 r = amdgpu_job_submit(job, &adev->mman.entity,
321 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
325 dma_fence_put(fence);
330 amdgpu_job_free(job);
335 * amdgpu_copy_ttm_mem_to_mem - Helper function for copy
336 * @adev: amdgpu device
337 * @src: buffer/address where to read from
338 * @dst: buffer/address where to write to
339 * @size: number of bytes to copy
340 * @tmz: if a secure copy should be used
341 * @resv: resv object to sync to
342 * @f: Returns the last fence if multiple jobs are submitted.
344 * The function copies @size bytes from {src->mem + src->offset} to
345 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
346 * move and different for a BO to BO copy.
349 int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
350 const struct amdgpu_copy_mem *src,
351 const struct amdgpu_copy_mem *dst,
352 uint64_t size, bool tmz,
353 struct dma_resv *resv,
354 struct dma_fence **f)
356 const uint32_t GTT_MAX_BYTES = (AMDGPU_GTT_MAX_TRANSFER_SIZE *
357 AMDGPU_GPU_PAGE_SIZE);
359 uint64_t src_node_size, dst_node_size, src_offset, dst_offset;
360 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
361 struct drm_mm_node *src_mm, *dst_mm;
362 struct dma_fence *fence = NULL;
365 if (!adev->mman.buffer_funcs_enabled) {
366 DRM_ERROR("Trying to move memory with ring turned off.\n");
370 src_offset = src->offset;
371 if (src->mem->mm_node) {
372 src_mm = amdgpu_find_mm_node(src->mem, &src_offset);
373 src_node_size = (src_mm->size << PAGE_SHIFT) - src_offset;
376 src_node_size = ULLONG_MAX;
379 dst_offset = dst->offset;
380 if (dst->mem->mm_node) {
381 dst_mm = amdgpu_find_mm_node(dst->mem, &dst_offset);
382 dst_node_size = (dst_mm->size << PAGE_SHIFT) - dst_offset;
385 dst_node_size = ULLONG_MAX;
388 mutex_lock(&adev->mman.gtt_window_lock);
391 uint32_t src_page_offset = src_offset & ~PAGE_MASK;
392 uint32_t dst_page_offset = dst_offset & ~PAGE_MASK;
393 struct dma_fence *next;
397 /* Copy size cannot exceed GTT_MAX_BYTES. So if src or dst
398 * begins at an offset, then adjust the size accordingly
400 cur_size = max(src_page_offset, dst_page_offset);
401 cur_size = min(min3(src_node_size, dst_node_size, size),
402 (uint64_t)(GTT_MAX_BYTES - cur_size));
404 /* Map src to window 0 and dst to window 1. */
405 r = amdgpu_ttm_map_buffer(src->bo, src->mem, src_mm,
406 PFN_UP(cur_size + src_page_offset),
407 src_offset, 0, ring, tmz, &from);
411 r = amdgpu_ttm_map_buffer(dst->bo, dst->mem, dst_mm,
412 PFN_UP(cur_size + dst_page_offset),
413 dst_offset, 1, ring, tmz, &to);
417 r = amdgpu_copy_buffer(ring, from, to, cur_size,
418 resv, &next, false, true, tmz);
422 dma_fence_put(fence);
429 src_node_size -= cur_size;
430 if (!src_node_size) {
432 src_node_size = src_mm->size << PAGE_SHIFT;
435 src_offset += cur_size;
438 dst_node_size -= cur_size;
439 if (!dst_node_size) {
441 dst_node_size = dst_mm->size << PAGE_SHIFT;
444 dst_offset += cur_size;
448 mutex_unlock(&adev->mman.gtt_window_lock);
450 *f = dma_fence_get(fence);
451 dma_fence_put(fence);
456 * amdgpu_move_blit - Copy an entire buffer to another buffer
458 * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
459 * help move buffers to and from VRAM.
461 static int amdgpu_move_blit(struct ttm_buffer_object *bo,
463 struct ttm_resource *new_mem,
464 struct ttm_resource *old_mem)
466 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
467 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
468 struct amdgpu_copy_mem src, dst;
469 struct dma_fence *fence = NULL;
479 r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
480 new_mem->num_pages << PAGE_SHIFT,
481 amdgpu_bo_encrypted(abo),
482 bo->base.resv, &fence);
486 /* clear the space being freed */
487 if (old_mem->mem_type == TTM_PL_VRAM &&
488 (abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)) {
489 struct dma_fence *wipe_fence = NULL;
491 r = amdgpu_fill_buffer(ttm_to_amdgpu_bo(bo), AMDGPU_POISON,
495 } else if (wipe_fence) {
496 dma_fence_put(fence);
501 /* Always block for VM page tables before committing the new location */
502 if (bo->type == ttm_bo_type_kernel)
503 r = ttm_bo_move_accel_cleanup(bo, fence, true, false, new_mem);
505 r = ttm_bo_move_accel_cleanup(bo, fence, evict, true, new_mem);
506 dma_fence_put(fence);
511 dma_fence_wait(fence, false);
512 dma_fence_put(fence);
517 * amdgpu_move_vram_ram - Copy VRAM buffer to RAM buffer
519 * Called by amdgpu_bo_move().
521 static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo, bool evict,
522 struct ttm_operation_ctx *ctx,
523 struct ttm_resource *new_mem)
525 struct ttm_resource *old_mem = &bo->mem;
526 struct ttm_resource tmp_mem;
527 struct ttm_place placements;
528 struct ttm_placement placement;
531 /* create space/pages for new_mem in GTT space */
533 tmp_mem.mm_node = NULL;
534 placement.num_placement = 1;
535 placement.placement = &placements;
536 placement.num_busy_placement = 1;
537 placement.busy_placement = &placements;
540 placements.mem_type = TTM_PL_TT;
541 placements.flags = 0;
542 r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
544 pr_err("Failed to find GTT space for blit from VRAM\n");
548 r = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
552 /* Bind the memory to the GTT space */
553 r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, &tmp_mem);
558 /* blit VRAM to GTT */
559 r = amdgpu_move_blit(bo, evict, &tmp_mem, old_mem);
564 /* move BO (in tmp_mem) to new_mem */
565 r = ttm_bo_move_ttm(bo, ctx, new_mem);
567 ttm_resource_free(bo, &tmp_mem);
572 * amdgpu_move_ram_vram - Copy buffer from RAM to VRAM
574 * Called by amdgpu_bo_move().
576 static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo, bool evict,
577 struct ttm_operation_ctx *ctx,
578 struct ttm_resource *new_mem)
580 struct ttm_resource *old_mem = &bo->mem;
581 struct ttm_resource tmp_mem;
582 struct ttm_placement placement;
583 struct ttm_place placements;
586 /* make space in GTT for old_mem buffer */
588 tmp_mem.mm_node = NULL;
589 placement.num_placement = 1;
590 placement.placement = &placements;
591 placement.num_busy_placement = 1;
592 placement.busy_placement = &placements;
595 placements.mem_type = TTM_PL_TT;
596 placements.flags = 0;
597 r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
599 pr_err("Failed to find GTT space for blit to VRAM\n");
603 /* move/bind old memory to GTT space */
604 r = ttm_bo_move_to_new_tt_mem(bo, ctx, &tmp_mem);
608 ttm_bo_assign_mem(bo, &tmp_mem);
610 r = amdgpu_move_blit(bo, evict, new_mem, old_mem);
615 ttm_resource_free(bo, &tmp_mem);
620 * amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
622 * Called by amdgpu_bo_move()
624 static bool amdgpu_mem_visible(struct amdgpu_device *adev,
625 struct ttm_resource *mem)
627 struct drm_mm_node *nodes = mem->mm_node;
629 if (mem->mem_type == TTM_PL_SYSTEM ||
630 mem->mem_type == TTM_PL_TT)
632 if (mem->mem_type != TTM_PL_VRAM)
635 /* ttm_resource_ioremap only supports contiguous memory */
636 if (nodes->size != mem->num_pages)
639 return ((nodes->start + nodes->size) << PAGE_SHIFT)
640 <= adev->gmc.visible_vram_size;
644 * amdgpu_bo_move - Move a buffer object to a new memory location
646 * Called by ttm_bo_handle_move_mem()
648 static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
649 struct ttm_operation_ctx *ctx,
650 struct ttm_resource *new_mem)
652 struct amdgpu_device *adev;
653 struct amdgpu_bo *abo;
654 struct ttm_resource *old_mem = &bo->mem;
657 /* Can't move a pinned BO */
658 abo = ttm_to_amdgpu_bo(bo);
659 if (WARN_ON_ONCE(abo->tbo.pin_count > 0))
662 adev = amdgpu_ttm_adev(bo->bdev);
664 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
665 ttm_bo_move_null(bo, new_mem);
668 if (old_mem->mem_type == TTM_PL_SYSTEM &&
669 new_mem->mem_type == TTM_PL_TT) {
670 ttm_bo_move_null(bo, new_mem);
674 if (old_mem->mem_type == TTM_PL_TT &&
675 new_mem->mem_type == TTM_PL_SYSTEM)
676 return ttm_bo_move_ttm(bo, ctx, new_mem);
678 if (old_mem->mem_type == AMDGPU_PL_GDS ||
679 old_mem->mem_type == AMDGPU_PL_GWS ||
680 old_mem->mem_type == AMDGPU_PL_OA ||
681 new_mem->mem_type == AMDGPU_PL_GDS ||
682 new_mem->mem_type == AMDGPU_PL_GWS ||
683 new_mem->mem_type == AMDGPU_PL_OA) {
684 /* Nothing to save here */
685 ttm_bo_move_null(bo, new_mem);
689 if (!adev->mman.buffer_funcs_enabled) {
694 if (old_mem->mem_type == TTM_PL_VRAM &&
695 new_mem->mem_type == TTM_PL_SYSTEM) {
696 r = amdgpu_move_vram_ram(bo, evict, ctx, new_mem);
697 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
698 new_mem->mem_type == TTM_PL_VRAM) {
699 r = amdgpu_move_ram_vram(bo, evict, ctx, new_mem);
701 r = amdgpu_move_blit(bo, evict,
707 /* Check that all memory is CPU accessible */
708 if (!amdgpu_mem_visible(adev, old_mem) ||
709 !amdgpu_mem_visible(adev, new_mem)) {
710 pr_err("Move buffer fallback to memcpy unavailable\n");
714 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
719 if (bo->type == ttm_bo_type_device &&
720 new_mem->mem_type == TTM_PL_VRAM &&
721 old_mem->mem_type != TTM_PL_VRAM) {
722 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
723 * accesses the BO after it's moved.
725 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
728 /* update statistics */
729 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
734 * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
736 * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
738 static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *mem)
740 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
741 struct drm_mm_node *mm_node = mem->mm_node;
742 size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
744 switch (mem->mem_type) {
751 mem->bus.offset = mem->start << PAGE_SHIFT;
752 /* check if it's visible */
753 if ((mem->bus.offset + bus_size) > adev->gmc.visible_vram_size)
755 /* Only physically contiguous buffers apply. In a contiguous
756 * buffer, size of the first mm_node would match the number of
757 * pages in ttm_resource.
759 if (adev->mman.aper_base_kaddr &&
760 (mm_node->size == mem->num_pages))
761 mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr +
764 mem->bus.offset += adev->gmc.aper_base;
765 mem->bus.is_iomem = true;
766 mem->bus.caching = ttm_write_combined;
774 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
775 unsigned long page_offset)
777 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
778 uint64_t offset = (page_offset << PAGE_SHIFT);
779 struct drm_mm_node *mm;
781 mm = amdgpu_find_mm_node(&bo->mem, &offset);
782 offset += adev->gmc.aper_base;
783 return mm->start + (offset >> PAGE_SHIFT);
787 * amdgpu_ttm_domain_start - Returns GPU start address
788 * @adev: amdgpu device object
789 * @type: type of the memory
792 * GPU start address of a memory domain
795 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type)
799 return adev->gmc.gart_start;
801 return adev->gmc.vram_start;
808 * TTM backend functions.
810 struct amdgpu_ttm_tt {
811 struct ttm_dma_tt ttm;
812 struct drm_gem_object *gobj;
815 struct task_struct *usertask;
818 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
819 struct hmm_range *range;
823 #ifdef CONFIG_DRM_AMDGPU_USERPTR
825 * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user
826 * memory and start HMM tracking CPU page table update
828 * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only
829 * once afterwards to stop HMM tracking
831 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages)
833 struct ttm_tt *ttm = bo->tbo.ttm;
834 struct amdgpu_ttm_tt *gtt = (void *)ttm;
835 unsigned long start = gtt->userptr;
836 struct vm_area_struct *vma;
837 struct hmm_range *range;
838 unsigned long timeout;
839 struct mm_struct *mm;
843 mm = bo->notifier.mm;
845 DRM_DEBUG_DRIVER("BO is not registered?\n");
849 /* Another get_user_pages is running at the same time?? */
850 if (WARN_ON(gtt->range))
853 if (!mmget_not_zero(mm)) /* Happens during process shutdown */
856 range = kzalloc(sizeof(*range), GFP_KERNEL);
857 if (unlikely(!range)) {
861 range->notifier = &bo->notifier;
862 range->start = bo->notifier.interval_tree.start;
863 range->end = bo->notifier.interval_tree.last + 1;
864 range->default_flags = HMM_PFN_REQ_FAULT;
865 if (!amdgpu_ttm_tt_is_readonly(ttm))
866 range->default_flags |= HMM_PFN_REQ_WRITE;
868 range->hmm_pfns = kvmalloc_array(ttm->num_pages,
869 sizeof(*range->hmm_pfns), GFP_KERNEL);
870 if (unlikely(!range->hmm_pfns)) {
872 goto out_free_ranges;
876 vma = find_vma(mm, start);
877 if (unlikely(!vma || start < vma->vm_start)) {
881 if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) &&
886 mmap_read_unlock(mm);
887 timeout = jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
890 range->notifier_seq = mmu_interval_read_begin(&bo->notifier);
893 r = hmm_range_fault(range);
894 mmap_read_unlock(mm);
897 * FIXME: This timeout should encompass the retry from
898 * mmu_interval_read_retry() as well.
900 if (r == -EBUSY && !time_after(jiffies, timeout))
906 * Due to default_flags, all pages are HMM_PFN_VALID or
907 * hmm_range_fault() fails. FIXME: The pages cannot be touched outside
908 * the notifier_lock, and mmu_interval_read_retry() must be done first.
910 for (i = 0; i < ttm->num_pages; i++)
911 pages[i] = hmm_pfn_to_page(range->hmm_pfns[i]);
919 mmap_read_unlock(mm);
921 kvfree(range->hmm_pfns);
930 * amdgpu_ttm_tt_userptr_range_done - stop HMM track the CPU page table change
931 * Check if the pages backing this ttm range have been invalidated
933 * Returns: true if pages are still valid
935 bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm)
937 struct amdgpu_ttm_tt *gtt = (void *)ttm;
940 if (!gtt || !gtt->userptr)
943 DRM_DEBUG_DRIVER("user_pages_done 0x%llx pages 0x%lx\n",
944 gtt->userptr, ttm->num_pages);
946 WARN_ONCE(!gtt->range || !gtt->range->hmm_pfns,
947 "No user pages to check\n");
951 * FIXME: Must always hold notifier_lock for this, and must
952 * not ignore the return code.
954 r = mmu_interval_read_retry(gtt->range->notifier,
955 gtt->range->notifier_seq);
956 kvfree(gtt->range->hmm_pfns);
966 * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
968 * Called by amdgpu_cs_list_validate(). This creates the page list
969 * that backs user memory and will ultimately be mapped into the device
972 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
976 for (i = 0; i < ttm->num_pages; ++i)
977 ttm->pages[i] = pages ? pages[i] : NULL;
981 * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages
983 * Called by amdgpu_ttm_backend_bind()
985 static int amdgpu_ttm_tt_pin_userptr(struct ttm_bo_device *bdev,
988 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
989 struct amdgpu_ttm_tt *gtt = (void *)ttm;
992 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
993 enum dma_data_direction direction = write ?
994 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
996 /* Allocate an SG array and squash pages into it */
997 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
998 ttm->num_pages << PAGE_SHIFT,
1003 /* Map SG to device */
1004 r = dma_map_sgtable(adev->dev, ttm->sg, direction, 0);
1008 /* convert SG to linear array of pages and dma addresses */
1009 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1010 gtt->ttm.dma_address, ttm->num_pages);
1020 * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
1022 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_bo_device *bdev,
1025 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1026 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1028 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1029 enum dma_data_direction direction = write ?
1030 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
1032 /* double check that we don't free the table twice */
1036 /* unmap the pages mapped to the device */
1037 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
1038 sg_free_table(ttm->sg);
1040 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
1044 for (i = 0; i < ttm->num_pages; i++) {
1045 if (ttm->pages[i] !=
1046 hmm_pfn_to_page(gtt->range->hmm_pfns[i]))
1050 WARN((i == ttm->num_pages), "Missing get_user_page_done\n");
1055 static int amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
1056 struct ttm_buffer_object *tbo,
1059 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo);
1060 struct ttm_tt *ttm = tbo->ttm;
1061 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1064 if (amdgpu_bo_encrypted(abo))
1065 flags |= AMDGPU_PTE_TMZ;
1067 if (abo->flags & AMDGPU_GEM_CREATE_CP_MQD_GFX9) {
1068 uint64_t page_idx = 1;
1070 r = amdgpu_gart_bind(adev, gtt->offset, page_idx,
1071 ttm->pages, gtt->ttm.dma_address, flags);
1073 goto gart_bind_fail;
1075 /* The memory type of the first page defaults to UC. Now
1076 * modify the memory type to NC from the second page of
1079 flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
1080 flags |= AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC);
1082 r = amdgpu_gart_bind(adev,
1083 gtt->offset + (page_idx << PAGE_SHIFT),
1084 ttm->num_pages - page_idx,
1085 &ttm->pages[page_idx],
1086 &(gtt->ttm.dma_address[page_idx]), flags);
1088 r = amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
1089 ttm->pages, gtt->ttm.dma_address, flags);
1094 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
1095 ttm->num_pages, gtt->offset);
1101 * amdgpu_ttm_backend_bind - Bind GTT memory
1103 * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
1104 * This handles binding GTT memory to the device address space.
1106 static int amdgpu_ttm_backend_bind(struct ttm_bo_device *bdev,
1108 struct ttm_resource *bo_mem)
1110 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1111 struct amdgpu_ttm_tt *gtt = (void*)ttm;
1122 r = amdgpu_ttm_tt_pin_userptr(bdev, ttm);
1124 DRM_ERROR("failed to pin userptr\n");
1128 if (!ttm->num_pages) {
1129 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
1130 ttm->num_pages, bo_mem, ttm);
1133 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
1134 bo_mem->mem_type == AMDGPU_PL_GWS ||
1135 bo_mem->mem_type == AMDGPU_PL_OA)
1138 if (!amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
1139 gtt->offset = AMDGPU_BO_INVALID_OFFSET;
1143 /* compute PTE flags relevant to this BO memory */
1144 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem);
1146 /* bind pages into GART page tables */
1147 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
1148 r = amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
1149 ttm->pages, gtt->ttm.dma_address, flags);
1152 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
1153 ttm->num_pages, gtt->offset);
1159 * amdgpu_ttm_alloc_gart - Allocate GART memory for buffer object
1161 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
1163 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1164 struct ttm_operation_ctx ctx = { false, false };
1165 struct amdgpu_ttm_tt *gtt = (void*)bo->ttm;
1166 struct ttm_resource tmp;
1167 struct ttm_placement placement;
1168 struct ttm_place placements;
1169 uint64_t addr, flags;
1172 if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET)
1175 addr = amdgpu_gmc_agp_addr(bo);
1176 if (addr != AMDGPU_BO_INVALID_OFFSET) {
1177 bo->mem.start = addr >> PAGE_SHIFT;
1180 /* allocate GART space */
1183 placement.num_placement = 1;
1184 placement.placement = &placements;
1185 placement.num_busy_placement = 1;
1186 placement.busy_placement = &placements;
1187 placements.fpfn = 0;
1188 placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
1189 placements.mem_type = TTM_PL_TT;
1190 placements.flags = bo->mem.placement;
1192 r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
1196 /* compute PTE flags for this buffer object */
1197 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, &tmp);
1200 gtt->offset = (u64)tmp.start << PAGE_SHIFT;
1201 r = amdgpu_ttm_gart_bind(adev, bo, flags);
1203 ttm_resource_free(bo, &tmp);
1207 ttm_resource_free(bo, &bo->mem);
1215 * amdgpu_ttm_recover_gart - Rebind GTT pages
1217 * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
1218 * rebind GTT pages during a GPU reset.
1220 int amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
1222 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
1229 flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, &tbo->mem);
1230 r = amdgpu_ttm_gart_bind(adev, tbo, flags);
1236 * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
1238 * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
1241 static void amdgpu_ttm_backend_unbind(struct ttm_bo_device *bdev,
1244 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1245 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1251 /* if the pages have userptr pinning then clear that first */
1253 amdgpu_ttm_tt_unpin_userptr(bdev, ttm);
1255 if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
1258 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1259 r = amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages);
1261 DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
1262 gtt->ttm.ttm.num_pages, gtt->offset);
1266 static void amdgpu_ttm_backend_destroy(struct ttm_bo_device *bdev,
1269 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1271 amdgpu_ttm_backend_unbind(bdev, ttm);
1272 ttm_tt_destroy_common(bdev, ttm);
1274 put_task_struct(gtt->usertask);
1276 ttm_dma_tt_fini(>t->ttm);
1281 * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1283 * @bo: The buffer object to create a GTT ttm_tt object around
1285 * Called by ttm_tt_create().
1287 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
1288 uint32_t page_flags)
1290 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1291 struct amdgpu_ttm_tt *gtt;
1292 enum ttm_caching caching;
1294 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
1298 gtt->gobj = &bo->base;
1300 if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
1301 caching = ttm_write_combined;
1303 caching = ttm_cached;
1305 /* allocate space for the uninitialized page entries */
1306 if (ttm_sg_tt_init(>t->ttm, bo, page_flags, caching)) {
1310 return >t->ttm.ttm;
1314 * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1316 * Map the pages of a ttm_tt object to an address space visible
1317 * to the underlying device.
1319 static int amdgpu_ttm_tt_populate(struct ttm_bo_device *bdev,
1321 struct ttm_operation_ctx *ctx)
1323 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1324 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1326 /* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1327 if (gtt && gtt->userptr) {
1328 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
1332 ttm->page_flags |= TTM_PAGE_FLAG_SG;
1333 ttm_tt_set_populated(ttm);
1337 if (ttm->page_flags & TTM_PAGE_FLAG_SG) {
1339 struct dma_buf_attachment *attach;
1340 struct sg_table *sgt;
1342 attach = gtt->gobj->import_attach;
1343 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
1345 return PTR_ERR(sgt);
1350 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1351 gtt->ttm.dma_address,
1353 ttm_tt_set_populated(ttm);
1357 #ifdef CONFIG_SWIOTLB
1358 if (adev->need_swiotlb && swiotlb_nr_tbl()) {
1359 return ttm_dma_populate(>t->ttm, adev->dev, ctx);
1363 /* fall back to generic helper to populate the page array
1364 * and map them to the device */
1365 return ttm_populate_and_map_pages(adev->dev, >t->ttm, ctx);
1369 * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1371 * Unmaps pages of a ttm_tt object from the device address space and
1372 * unpopulates the page array backing it.
1374 static void amdgpu_ttm_tt_unpopulate(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
1376 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1377 struct amdgpu_device *adev;
1379 if (gtt && gtt->userptr) {
1380 amdgpu_ttm_tt_set_user_pages(ttm, NULL);
1382 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
1386 if (ttm->sg && gtt->gobj->import_attach) {
1387 struct dma_buf_attachment *attach;
1389 attach = gtt->gobj->import_attach;
1390 dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
1395 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
1398 adev = amdgpu_ttm_adev(bdev);
1400 #ifdef CONFIG_SWIOTLB
1401 if (adev->need_swiotlb && swiotlb_nr_tbl()) {
1402 ttm_dma_unpopulate(>t->ttm, adev->dev);
1407 /* fall back to generic helper to unmap and unpopulate array */
1408 ttm_unmap_and_unpopulate_pages(adev->dev, >t->ttm);
1412 * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1415 * @bo: The ttm_buffer_object to bind this userptr to
1416 * @addr: The address in the current tasks VM space to use
1417 * @flags: Requirements of userptr object.
1419 * Called by amdgpu_gem_userptr_ioctl() to bind userptr pages
1422 int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
1423 uint64_t addr, uint32_t flags)
1425 struct amdgpu_ttm_tt *gtt;
1428 /* TODO: We want a separate TTM object type for userptrs */
1429 bo->ttm = amdgpu_ttm_tt_create(bo, 0);
1430 if (bo->ttm == NULL)
1434 gtt = (void*)bo->ttm;
1435 gtt->userptr = addr;
1436 gtt->userflags = flags;
1439 put_task_struct(gtt->usertask);
1440 gtt->usertask = current->group_leader;
1441 get_task_struct(gtt->usertask);
1447 * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1449 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1451 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1456 if (gtt->usertask == NULL)
1459 return gtt->usertask->mm;
1463 * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1464 * address range for the current task.
1467 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1470 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1473 if (gtt == NULL || !gtt->userptr)
1476 /* Return false if no part of the ttm_tt object lies within
1479 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
1480 if (gtt->userptr > end || gtt->userptr + size <= start)
1487 * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
1489 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
1491 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1493 if (gtt == NULL || !gtt->userptr)
1500 * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1502 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1504 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1509 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1513 * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object
1515 * @ttm: The ttm_tt object to compute the flags for
1516 * @mem: The memory registry backing this ttm_tt object
1518 * Figure out the flags to use for a VM PDE (Page Directory Entry).
1520 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem)
1524 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1525 flags |= AMDGPU_PTE_VALID;
1527 if (mem && mem->mem_type == TTM_PL_TT) {
1528 flags |= AMDGPU_PTE_SYSTEM;
1530 if (ttm->caching == ttm_cached)
1531 flags |= AMDGPU_PTE_SNOOPED;
1538 * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1540 * @ttm: The ttm_tt object to compute the flags for
1541 * @mem: The memory registry backing this ttm_tt object
1543 * Figure out the flags to use for a VM PTE (Page Table Entry).
1545 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1546 struct ttm_resource *mem)
1548 uint64_t flags = amdgpu_ttm_tt_pde_flags(ttm, mem);
1550 flags |= adev->gart.gart_pte_flags;
1551 flags |= AMDGPU_PTE_READABLE;
1553 if (!amdgpu_ttm_tt_is_readonly(ttm))
1554 flags |= AMDGPU_PTE_WRITEABLE;
1560 * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1563 * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1564 * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1565 * it can find space for a new object and by ttm_bo_force_list_clean() which is
1566 * used to clean out a memory space.
1568 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1569 const struct ttm_place *place)
1571 unsigned long num_pages = bo->mem.num_pages;
1572 struct drm_mm_node *node = bo->mem.mm_node;
1573 struct dma_resv_list *flist;
1574 struct dma_fence *f;
1577 if (bo->type == ttm_bo_type_kernel &&
1578 !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)))
1581 /* If bo is a KFD BO, check if the bo belongs to the current process.
1582 * If true, then return false as any KFD process needs all its BOs to
1583 * be resident to run successfully
1585 flist = dma_resv_get_list(bo->base.resv);
1587 for (i = 0; i < flist->shared_count; ++i) {
1588 f = rcu_dereference_protected(flist->shared[i],
1589 dma_resv_held(bo->base.resv));
1590 if (amdkfd_fence_check_mm(f, current->mm))
1595 switch (bo->mem.mem_type) {
1597 if (amdgpu_bo_is_amdgpu_bo(bo) &&
1598 amdgpu_bo_encrypted(ttm_to_amdgpu_bo(bo)))
1603 /* Check each drm MM node individually */
1605 if (place->fpfn < (node->start + node->size) &&
1606 !(place->lpfn && place->lpfn <= node->start))
1609 num_pages -= node->size;
1618 return ttm_bo_eviction_valuable(bo, place);
1622 * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1624 * @bo: The buffer object to read/write
1625 * @offset: Offset into buffer object
1626 * @buf: Secondary buffer to write/read from
1627 * @len: Length in bytes of access
1628 * @write: true if writing
1630 * This is used to access VRAM that backs a buffer object via MMIO
1631 * access for debugging purposes.
1633 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1634 unsigned long offset,
1635 void *buf, int len, int write)
1637 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1638 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1639 struct drm_mm_node *nodes;
1643 unsigned long flags;
1645 if (bo->mem.mem_type != TTM_PL_VRAM)
1649 nodes = amdgpu_find_mm_node(&abo->tbo.mem, &pos);
1650 pos += (nodes->start << PAGE_SHIFT);
1652 while (len && pos < adev->gmc.mc_vram_size) {
1653 uint64_t aligned_pos = pos & ~(uint64_t)3;
1654 uint64_t bytes = 4 - (pos & 3);
1655 uint32_t shift = (pos & 3) * 8;
1656 uint32_t mask = 0xffffffff << shift;
1659 mask &= 0xffffffff >> (bytes - len) * 8;
1663 if (mask != 0xffffffff) {
1664 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1665 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
1666 WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
1667 if (!write || mask != 0xffffffff)
1668 value = RREG32_NO_KIQ(mmMM_DATA);
1671 value |= (*(uint32_t *)buf << shift) & mask;
1672 WREG32_NO_KIQ(mmMM_DATA, value);
1674 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1676 value = (value & mask) >> shift;
1677 memcpy(buf, &value, bytes);
1680 bytes = (nodes->start + nodes->size) << PAGE_SHIFT;
1681 bytes = min(bytes - pos, (uint64_t)len & ~0x3ull);
1683 amdgpu_device_vram_access(adev, pos, (uint32_t *)buf,
1688 buf = (uint8_t *)buf + bytes;
1691 if (pos >= (nodes->start + nodes->size) << PAGE_SHIFT) {
1693 pos = (nodes->start << PAGE_SHIFT);
1700 static struct ttm_bo_driver amdgpu_bo_driver = {
1701 .ttm_tt_create = &amdgpu_ttm_tt_create,
1702 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1703 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1704 .ttm_tt_bind = &amdgpu_ttm_backend_bind,
1705 .ttm_tt_unbind = &amdgpu_ttm_backend_unbind,
1706 .ttm_tt_destroy = &amdgpu_ttm_backend_destroy,
1707 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1708 .evict_flags = &amdgpu_evict_flags,
1709 .move = &amdgpu_bo_move,
1710 .verify_access = &amdgpu_verify_access,
1711 .move_notify = &amdgpu_bo_move_notify,
1712 .release_notify = &amdgpu_bo_release_notify,
1713 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1714 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1715 .access_memory = &amdgpu_ttm_access_memory,
1716 .del_from_lru_notify = &amdgpu_vm_del_from_lru_notify
1720 * Firmware Reservation functions
1723 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1725 * @adev: amdgpu_device pointer
1727 * free fw reserved vram if it has been reserved.
1729 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1731 amdgpu_bo_free_kernel(&adev->mman.fw_vram_usage_reserved_bo,
1732 NULL, &adev->mman.fw_vram_usage_va);
1736 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1738 * @adev: amdgpu_device pointer
1740 * create bo vram reservation from fw.
1742 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1744 uint64_t vram_size = adev->gmc.visible_vram_size;
1746 adev->mman.fw_vram_usage_va = NULL;
1747 adev->mman.fw_vram_usage_reserved_bo = NULL;
1749 if (adev->mman.fw_vram_usage_size == 0 ||
1750 adev->mman.fw_vram_usage_size > vram_size)
1753 return amdgpu_bo_create_kernel_at(adev,
1754 adev->mman.fw_vram_usage_start_offset,
1755 adev->mman.fw_vram_usage_size,
1756 AMDGPU_GEM_DOMAIN_VRAM,
1757 &adev->mman.fw_vram_usage_reserved_bo,
1758 &adev->mman.fw_vram_usage_va);
1762 * Memoy training reservation functions
1766 * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram
1768 * @adev: amdgpu_device pointer
1770 * free memory training reserved vram if it has been reserved.
1772 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev)
1774 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1776 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
1777 amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL);
1783 static void amdgpu_ttm_training_data_block_init(struct amdgpu_device *adev)
1785 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1787 memset(ctx, 0, sizeof(*ctx));
1789 ctx->c2p_train_data_offset =
1790 ALIGN((adev->gmc.mc_vram_size - adev->mman.discovery_tmr_size - SZ_1M), SZ_1M);
1791 ctx->p2c_train_data_offset =
1792 (adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET);
1793 ctx->train_data_size =
1794 GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES;
1796 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
1797 ctx->train_data_size,
1798 ctx->p2c_train_data_offset,
1799 ctx->c2p_train_data_offset);
1803 * reserve TMR memory at the top of VRAM which holds
1804 * IP Discovery data and is protected by PSP.
1806 static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
1809 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1810 bool mem_train_support = false;
1812 if (!amdgpu_sriov_vf(adev)) {
1813 ret = amdgpu_mem_train_support(adev);
1815 mem_train_support = true;
1819 DRM_DEBUG("memory training does not support!\n");
1823 * Query reserved tmr size through atom firmwareinfo for Sienna_Cichlid and onwards for all
1824 * the use cases (IP discovery/G6 memory training/profiling/diagnostic data.etc)
1826 * Otherwise, fallback to legacy approach to check and reserve tmr block for ip
1827 * discovery data and G6 memory training data respectively
1829 adev->mman.discovery_tmr_size =
1830 amdgpu_atomfirmware_get_fw_reserved_fb_size(adev);
1831 if (!adev->mman.discovery_tmr_size)
1832 adev->mman.discovery_tmr_size = DISCOVERY_TMR_OFFSET;
1834 if (mem_train_support) {
1835 /* reserve vram for mem train according to TMR location */
1836 amdgpu_ttm_training_data_block_init(adev);
1837 ret = amdgpu_bo_create_kernel_at(adev,
1838 ctx->c2p_train_data_offset,
1839 ctx->train_data_size,
1840 AMDGPU_GEM_DOMAIN_VRAM,
1844 DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
1845 amdgpu_ttm_training_reserve_vram_fini(adev);
1848 ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS;
1851 ret = amdgpu_bo_create_kernel_at(adev,
1852 adev->gmc.real_vram_size - adev->mman.discovery_tmr_size,
1853 adev->mman.discovery_tmr_size,
1854 AMDGPU_GEM_DOMAIN_VRAM,
1855 &adev->mman.discovery_memory,
1858 DRM_ERROR("alloc tmr failed(%d)!\n", ret);
1859 amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1867 * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1868 * gtt/vram related fields.
1870 * This initializes all of the memory space pools that the TTM layer
1871 * will need such as the GTT space (system memory mapped to the device),
1872 * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1873 * can be mapped per VMID.
1875 int amdgpu_ttm_init(struct amdgpu_device *adev)
1881 mutex_init(&adev->mman.gtt_window_lock);
1883 /* No others user of address space so set it to 0 */
1884 r = ttm_bo_device_init(&adev->mman.bdev,
1886 adev_to_drm(adev)->anon_inode->i_mapping,
1887 adev_to_drm(adev)->vma_offset_manager,
1888 dma_addressing_limited(adev->dev));
1890 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1893 adev->mman.initialized = true;
1895 /* We opt to avoid OOM on system pages allocations */
1896 adev->mman.bdev.no_retry = true;
1898 /* Initialize VRAM pool with all of VRAM divided into pages */
1899 r = amdgpu_vram_mgr_init(adev);
1901 DRM_ERROR("Failed initializing VRAM heap.\n");
1905 /* Reduce size of CPU-visible VRAM if requested */
1906 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1907 if (amdgpu_vis_vram_limit > 0 &&
1908 vis_vram_limit <= adev->gmc.visible_vram_size)
1909 adev->gmc.visible_vram_size = vis_vram_limit;
1911 /* Change the size here instead of the init above so only lpfn is affected */
1912 amdgpu_ttm_set_buffer_funcs_status(adev, false);
1914 adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base,
1915 adev->gmc.visible_vram_size);
1919 *The reserved vram for firmware must be pinned to the specified
1920 *place on the VRAM, so reserve it early.
1922 r = amdgpu_ttm_fw_reserve_vram_init(adev);
1928 * only NAVI10 and onwards ASIC support for IP discovery.
1929 * If IP discovery enabled, a block of memory should be
1930 * reserved for IP discovey.
1932 if (adev->mman.discovery_bin) {
1933 r = amdgpu_ttm_reserve_tmr(adev);
1938 /* allocate memory as required for VGA
1939 * This is used for VGA emulation and pre-OS scanout buffers to
1940 * avoid display artifacts while transitioning between pre-OS
1942 r = amdgpu_bo_create_kernel_at(adev, 0, adev->mman.stolen_vga_size,
1943 AMDGPU_GEM_DOMAIN_VRAM,
1944 &adev->mman.stolen_vga_memory,
1948 r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
1949 adev->mman.stolen_extended_size,
1950 AMDGPU_GEM_DOMAIN_VRAM,
1951 &adev->mman.stolen_extended_memory,
1956 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1957 (unsigned) (adev->gmc.real_vram_size / (1024 * 1024)));
1959 /* Compute GTT size, either bsaed on 3/4th the size of RAM size
1960 * or whatever the user passed on module init */
1961 if (amdgpu_gtt_size == -1) {
1965 gtt_size = min(max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1966 adev->gmc.mc_vram_size),
1967 ((uint64_t)si.totalram * si.mem_unit * 3/4));
1970 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1972 /* Initialize GTT memory pool */
1973 r = amdgpu_gtt_mgr_init(adev, gtt_size);
1975 DRM_ERROR("Failed initializing GTT heap.\n");
1978 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1979 (unsigned)(gtt_size / (1024 * 1024)));
1981 /* Initialize various on-chip memory pools */
1982 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GDS, adev->gds.gds_size);
1984 DRM_ERROR("Failed initializing GDS heap.\n");
1988 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GWS, adev->gds.gws_size);
1990 DRM_ERROR("Failed initializing gws heap.\n");
1994 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_OA, adev->gds.oa_size);
1996 DRM_ERROR("Failed initializing oa heap.\n");
2004 * amdgpu_ttm_late_init - Handle any late initialization for amdgpu_ttm
2006 void amdgpu_ttm_late_init(struct amdgpu_device *adev)
2008 /* return the VGA stolen memory (if any) back to VRAM */
2009 if (!adev->mman.keep_stolen_vga_memory)
2010 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
2011 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
2015 * amdgpu_ttm_fini - De-initialize the TTM memory pools
2017 void amdgpu_ttm_fini(struct amdgpu_device *adev)
2019 if (!adev->mman.initialized)
2022 amdgpu_ttm_training_reserve_vram_fini(adev);
2023 /* return the stolen vga memory back to VRAM */
2024 if (adev->mman.keep_stolen_vga_memory)
2025 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
2026 /* return the IP Discovery TMR memory back to VRAM */
2027 amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
2028 amdgpu_ttm_fw_reserve_vram_fini(adev);
2030 if (adev->mman.aper_base_kaddr)
2031 iounmap(adev->mman.aper_base_kaddr);
2032 adev->mman.aper_base_kaddr = NULL;
2034 amdgpu_vram_mgr_fini(adev);
2035 amdgpu_gtt_mgr_fini(adev);
2036 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GDS);
2037 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GWS);
2038 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
2039 ttm_bo_device_release(&adev->mman.bdev);
2040 adev->mman.initialized = false;
2041 DRM_INFO("amdgpu: ttm finalized\n");
2045 * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
2047 * @adev: amdgpu_device pointer
2048 * @enable: true when we can use buffer functions.
2050 * Enable/disable use of buffer functions during suspend/resume. This should
2051 * only be called at bootup or when userspace isn't running.
2053 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
2055 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
2059 if (!adev->mman.initialized || amdgpu_in_reset(adev) ||
2060 adev->mman.buffer_funcs_enabled == enable)
2064 struct amdgpu_ring *ring;
2065 struct drm_gpu_scheduler *sched;
2067 ring = adev->mman.buffer_funcs_ring;
2068 sched = &ring->sched;
2069 r = drm_sched_entity_init(&adev->mman.entity,
2070 DRM_SCHED_PRIORITY_KERNEL, &sched,
2073 DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
2078 drm_sched_entity_destroy(&adev->mman.entity);
2079 dma_fence_put(man->move);
2083 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
2085 size = adev->gmc.real_vram_size;
2087 size = adev->gmc.visible_vram_size;
2088 man->size = size >> PAGE_SHIFT;
2089 adev->mman.buffer_funcs_enabled = enable;
2092 static vm_fault_t amdgpu_ttm_fault(struct vm_fault *vmf)
2094 struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
2097 ret = ttm_bo_vm_reserve(bo, vmf);
2101 ret = amdgpu_bo_fault_reserve_notify(bo);
2105 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
2106 TTM_BO_VM_NUM_PREFAULT, 1);
2107 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
2111 dma_resv_unlock(bo->base.resv);
2115 static struct vm_operations_struct amdgpu_ttm_vm_ops = {
2116 .fault = amdgpu_ttm_fault,
2117 .open = ttm_bo_vm_open,
2118 .close = ttm_bo_vm_close,
2119 .access = ttm_bo_vm_access
2122 int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
2124 struct drm_file *file_priv = filp->private_data;
2125 struct amdgpu_device *adev = drm_to_adev(file_priv->minor->dev);
2128 r = ttm_bo_mmap(filp, vma, &adev->mman.bdev);
2129 if (unlikely(r != 0))
2132 vma->vm_ops = &amdgpu_ttm_vm_ops;
2136 int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
2137 uint64_t dst_offset, uint32_t byte_count,
2138 struct dma_resv *resv,
2139 struct dma_fence **fence, bool direct_submit,
2140 bool vm_needs_flush, bool tmz)
2142 enum amdgpu_ib_pool_type pool = direct_submit ? AMDGPU_IB_POOL_DIRECT :
2143 AMDGPU_IB_POOL_DELAYED;
2144 struct amdgpu_device *adev = ring->adev;
2145 struct amdgpu_job *job;
2148 unsigned num_loops, num_dw;
2152 if (direct_submit && !ring->sched.ready) {
2153 DRM_ERROR("Trying to move memory with ring turned off.\n");
2157 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
2158 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
2159 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->copy_num_dw, 8);
2161 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, pool, &job);
2165 if (vm_needs_flush) {
2166 job->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gart.bo);
2167 job->vm_needs_flush = true;
2170 r = amdgpu_sync_resv(adev, &job->sync, resv,
2172 AMDGPU_FENCE_OWNER_UNDEFINED);
2174 DRM_ERROR("sync failed (%d).\n", r);
2179 for (i = 0; i < num_loops; i++) {
2180 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2182 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
2183 dst_offset, cur_size_in_bytes, tmz);
2185 src_offset += cur_size_in_bytes;
2186 dst_offset += cur_size_in_bytes;
2187 byte_count -= cur_size_in_bytes;
2190 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2191 WARN_ON(job->ibs[0].length_dw > num_dw);
2193 r = amdgpu_job_submit_direct(job, ring, fence);
2195 r = amdgpu_job_submit(job, &adev->mman.entity,
2196 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2203 amdgpu_job_free(job);
2204 DRM_ERROR("Error scheduling IBs (%d)\n", r);
2208 int amdgpu_fill_buffer(struct amdgpu_bo *bo,
2210 struct dma_resv *resv,
2211 struct dma_fence **fence)
2213 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2214 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
2215 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2217 struct drm_mm_node *mm_node;
2218 unsigned long num_pages;
2219 unsigned int num_loops, num_dw;
2221 struct amdgpu_job *job;
2224 if (!adev->mman.buffer_funcs_enabled) {
2225 DRM_ERROR("Trying to clear memory with ring turned off.\n");
2229 if (bo->tbo.mem.mem_type == TTM_PL_TT) {
2230 r = amdgpu_ttm_alloc_gart(&bo->tbo);
2235 num_pages = bo->tbo.num_pages;
2236 mm_node = bo->tbo.mem.mm_node;
2239 uint64_t byte_count = mm_node->size << PAGE_SHIFT;
2241 num_loops += DIV_ROUND_UP_ULL(byte_count, max_bytes);
2242 num_pages -= mm_node->size;
2245 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
2247 /* for IB padding */
2250 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, AMDGPU_IB_POOL_DELAYED,
2256 r = amdgpu_sync_resv(adev, &job->sync, resv,
2258 AMDGPU_FENCE_OWNER_UNDEFINED);
2260 DRM_ERROR("sync failed (%d).\n", r);
2265 num_pages = bo->tbo.num_pages;
2266 mm_node = bo->tbo.mem.mm_node;
2269 uint64_t byte_count = mm_node->size << PAGE_SHIFT;
2272 dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
2273 while (byte_count) {
2274 uint32_t cur_size_in_bytes = min_t(uint64_t, byte_count,
2277 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
2278 dst_addr, cur_size_in_bytes);
2280 dst_addr += cur_size_in_bytes;
2281 byte_count -= cur_size_in_bytes;
2284 num_pages -= mm_node->size;
2288 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2289 WARN_ON(job->ibs[0].length_dw > num_dw);
2290 r = amdgpu_job_submit(job, &adev->mman.entity,
2291 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2298 amdgpu_job_free(job);
2302 #if defined(CONFIG_DEBUG_FS)
2304 static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
2306 struct drm_info_node *node = (struct drm_info_node *)m->private;
2307 unsigned ttm_pl = (uintptr_t)node->info_ent->data;
2308 struct drm_device *dev = node->minor->dev;
2309 struct amdgpu_device *adev = drm_to_adev(dev);
2310 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, ttm_pl);
2311 struct drm_printer p = drm_seq_file_printer(m);
2313 man->func->debug(man, &p);
2317 static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
2318 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, (void *)TTM_PL_VRAM},
2319 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, (void *)TTM_PL_TT},
2320 {"amdgpu_gds_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_GDS},
2321 {"amdgpu_gws_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_GWS},
2322 {"amdgpu_oa_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_OA},
2323 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
2324 #ifdef CONFIG_SWIOTLB
2325 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
2330 * amdgpu_ttm_vram_read - Linear read access to VRAM
2332 * Accesses VRAM via MMIO for debugging purposes.
2334 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
2335 size_t size, loff_t *pos)
2337 struct amdgpu_device *adev = file_inode(f)->i_private;
2340 if (size & 0x3 || *pos & 0x3)
2343 if (*pos >= adev->gmc.mc_vram_size)
2346 size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos));
2348 size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4);
2349 uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ];
2351 amdgpu_device_vram_access(adev, *pos, value, bytes, false);
2352 if (copy_to_user(buf, value, bytes))
2365 * amdgpu_ttm_vram_write - Linear write access to VRAM
2367 * Accesses VRAM via MMIO for debugging purposes.
2369 static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
2370 size_t size, loff_t *pos)
2372 struct amdgpu_device *adev = file_inode(f)->i_private;
2376 if (size & 0x3 || *pos & 0x3)
2379 if (*pos >= adev->gmc.mc_vram_size)
2383 unsigned long flags;
2386 if (*pos >= adev->gmc.mc_vram_size)
2389 r = get_user(value, (uint32_t *)buf);
2393 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
2394 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
2395 WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
2396 WREG32_NO_KIQ(mmMM_DATA, value);
2397 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
2408 static const struct file_operations amdgpu_ttm_vram_fops = {
2409 .owner = THIS_MODULE,
2410 .read = amdgpu_ttm_vram_read,
2411 .write = amdgpu_ttm_vram_write,
2412 .llseek = default_llseek,
2415 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2418 * amdgpu_ttm_gtt_read - Linear read access to GTT memory
2420 static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
2421 size_t size, loff_t *pos)
2423 struct amdgpu_device *adev = file_inode(f)->i_private;
2428 loff_t p = *pos / PAGE_SIZE;
2429 unsigned off = *pos & ~PAGE_MASK;
2430 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
2434 if (p >= adev->gart.num_cpu_pages)
2437 page = adev->gart.pages[p];
2442 r = copy_to_user(buf, ptr, cur_size);
2443 kunmap(adev->gart.pages[p]);
2445 r = clear_user(buf, cur_size);
2459 static const struct file_operations amdgpu_ttm_gtt_fops = {
2460 .owner = THIS_MODULE,
2461 .read = amdgpu_ttm_gtt_read,
2462 .llseek = default_llseek
2468 * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2470 * This function is used to read memory that has been mapped to the
2471 * GPU and the known addresses are not physical addresses but instead
2472 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2474 static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
2475 size_t size, loff_t *pos)
2477 struct amdgpu_device *adev = file_inode(f)->i_private;
2478 struct iommu_domain *dom;
2482 /* retrieve the IOMMU domain if any for this device */
2483 dom = iommu_get_domain_for_dev(adev->dev);
2486 phys_addr_t addr = *pos & PAGE_MASK;
2487 loff_t off = *pos & ~PAGE_MASK;
2488 size_t bytes = PAGE_SIZE - off;
2493 bytes = bytes < size ? bytes : size;
2495 /* Translate the bus address to a physical address. If
2496 * the domain is NULL it means there is no IOMMU active
2497 * and the address translation is the identity
2499 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2501 pfn = addr >> PAGE_SHIFT;
2502 if (!pfn_valid(pfn))
2505 p = pfn_to_page(pfn);
2506 if (p->mapping != adev->mman.bdev.dev_mapping)
2510 r = copy_to_user(buf, ptr + off, bytes);
2524 * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2526 * This function is used to write memory that has been mapped to the
2527 * GPU and the known addresses are not physical addresses but instead
2528 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2530 static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
2531 size_t size, loff_t *pos)
2533 struct amdgpu_device *adev = file_inode(f)->i_private;
2534 struct iommu_domain *dom;
2538 dom = iommu_get_domain_for_dev(adev->dev);
2541 phys_addr_t addr = *pos & PAGE_MASK;
2542 loff_t off = *pos & ~PAGE_MASK;
2543 size_t bytes = PAGE_SIZE - off;
2548 bytes = bytes < size ? bytes : size;
2550 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2552 pfn = addr >> PAGE_SHIFT;
2553 if (!pfn_valid(pfn))
2556 p = pfn_to_page(pfn);
2557 if (p->mapping != adev->mman.bdev.dev_mapping)
2561 r = copy_from_user(ptr + off, buf, bytes);
2574 static const struct file_operations amdgpu_ttm_iomem_fops = {
2575 .owner = THIS_MODULE,
2576 .read = amdgpu_iomem_read,
2577 .write = amdgpu_iomem_write,
2578 .llseek = default_llseek
2581 static const struct {
2583 const struct file_operations *fops;
2585 } ttm_debugfs_entries[] = {
2586 { "amdgpu_vram", &amdgpu_ttm_vram_fops, TTM_PL_VRAM },
2587 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2588 { "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
2590 { "amdgpu_iomem", &amdgpu_ttm_iomem_fops, TTM_PL_SYSTEM },
2595 int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
2597 #if defined(CONFIG_DEBUG_FS)
2600 struct drm_minor *minor = adev_to_drm(adev)->primary;
2601 struct dentry *ent, *root = minor->debugfs_root;
2603 for (count = 0; count < ARRAY_SIZE(ttm_debugfs_entries); count++) {
2604 ent = debugfs_create_file(
2605 ttm_debugfs_entries[count].name,
2606 S_IFREG | S_IRUGO, root,
2608 ttm_debugfs_entries[count].fops);
2610 return PTR_ERR(ent);
2611 if (ttm_debugfs_entries[count].domain == TTM_PL_VRAM)
2612 i_size_write(ent->d_inode, adev->gmc.mc_vram_size);
2613 else if (ttm_debugfs_entries[count].domain == TTM_PL_TT)
2614 i_size_write(ent->d_inode, adev->gmc.gart_size);
2615 adev->mman.debugfs_entries[count] = ent;
2618 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
2620 #ifdef CONFIG_SWIOTLB
2621 if (!(adev->need_swiotlb && swiotlb_nr_tbl()))
2625 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);