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 "bif/bif_4_1_d.h"
63 static int amdgpu_map_buffer(struct ttm_buffer_object
*bo
,
64 struct ttm_mem_reg
*mem
, unsigned num_pages
,
65 uint64_t offset
, unsigned window
,
66 struct amdgpu_ring
*ring
,
69 static int amdgpu_ttm_debugfs_init(struct amdgpu_device
*adev
);
70 static void amdgpu_ttm_debugfs_fini(struct amdgpu_device
*adev
);
72 static int amdgpu_invalidate_caches(struct ttm_bo_device
*bdev
, uint32_t flags
)
78 * amdgpu_init_mem_type - Initialize a memory manager for a specific type of
81 * @bdev: The TTM BO device object (contains a reference to amdgpu_device)
82 * @type: The type of memory requested
83 * @man: The memory type manager for each domain
85 * This is called by ttm_bo_init_mm() when a buffer object is being
88 static int amdgpu_init_mem_type(struct ttm_bo_device
*bdev
, uint32_t type
,
89 struct ttm_mem_type_manager
*man
)
91 struct amdgpu_device
*adev
;
93 adev
= amdgpu_ttm_adev(bdev
);
98 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
;
99 man
->available_caching
= TTM_PL_MASK_CACHING
;
100 man
->default_caching
= TTM_PL_FLAG_CACHED
;
104 man
->func
= &amdgpu_gtt_mgr_func
;
105 man
->gpu_offset
= adev
->gmc
.gart_start
;
106 man
->available_caching
= TTM_PL_MASK_CACHING
;
107 man
->default_caching
= TTM_PL_FLAG_CACHED
;
108 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
| TTM_MEMTYPE_FLAG_CMA
;
111 /* "On-card" video ram */
112 man
->func
= &amdgpu_vram_mgr_func
;
113 man
->gpu_offset
= adev
->gmc
.vram_start
;
114 man
->flags
= TTM_MEMTYPE_FLAG_FIXED
|
115 TTM_MEMTYPE_FLAG_MAPPABLE
;
116 man
->available_caching
= TTM_PL_FLAG_UNCACHED
| TTM_PL_FLAG_WC
;
117 man
->default_caching
= TTM_PL_FLAG_WC
;
122 /* On-chip GDS memory*/
123 man
->func
= &ttm_bo_manager_func
;
125 man
->flags
= TTM_MEMTYPE_FLAG_FIXED
| TTM_MEMTYPE_FLAG_CMA
;
126 man
->available_caching
= TTM_PL_FLAG_UNCACHED
;
127 man
->default_caching
= TTM_PL_FLAG_UNCACHED
;
130 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type
);
137 * amdgpu_evict_flags - Compute placement flags
139 * @bo: The buffer object to evict
140 * @placement: Possible destination(s) for evicted BO
142 * Fill in placement data when ttm_bo_evict() is called
144 static void amdgpu_evict_flags(struct ttm_buffer_object
*bo
,
145 struct ttm_placement
*placement
)
147 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->bdev
);
148 struct amdgpu_bo
*abo
;
149 static const struct ttm_place placements
= {
152 .flags
= TTM_PL_MASK_CACHING
| TTM_PL_FLAG_SYSTEM
155 /* Don't handle scatter gather BOs */
156 if (bo
->type
== ttm_bo_type_sg
) {
157 placement
->num_placement
= 0;
158 placement
->num_busy_placement
= 0;
162 /* Object isn't an AMDGPU object so ignore */
163 if (!amdgpu_bo_is_amdgpu_bo(bo
)) {
164 placement
->placement
= &placements
;
165 placement
->busy_placement
= &placements
;
166 placement
->num_placement
= 1;
167 placement
->num_busy_placement
= 1;
171 abo
= ttm_to_amdgpu_bo(bo
);
172 switch (bo
->mem
.mem_type
) {
176 placement
->num_placement
= 0;
177 placement
->num_busy_placement
= 0;
181 if (!adev
->mman
.buffer_funcs_enabled
) {
182 /* Move to system memory */
183 amdgpu_bo_placement_from_domain(abo
, AMDGPU_GEM_DOMAIN_CPU
);
184 } else if (!amdgpu_gmc_vram_full_visible(&adev
->gmc
) &&
185 !(abo
->flags
& AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
) &&
186 amdgpu_bo_in_cpu_visible_vram(abo
)) {
188 /* Try evicting to the CPU inaccessible part of VRAM
189 * first, but only set GTT as busy placement, so this
190 * BO will be evicted to GTT rather than causing other
191 * BOs to be evicted from VRAM
193 amdgpu_bo_placement_from_domain(abo
, AMDGPU_GEM_DOMAIN_VRAM
|
194 AMDGPU_GEM_DOMAIN_GTT
);
195 abo
->placements
[0].fpfn
= adev
->gmc
.visible_vram_size
>> PAGE_SHIFT
;
196 abo
->placements
[0].lpfn
= 0;
197 abo
->placement
.busy_placement
= &abo
->placements
[1];
198 abo
->placement
.num_busy_placement
= 1;
200 /* Move to GTT memory */
201 amdgpu_bo_placement_from_domain(abo
, AMDGPU_GEM_DOMAIN_GTT
);
206 amdgpu_bo_placement_from_domain(abo
, AMDGPU_GEM_DOMAIN_CPU
);
209 *placement
= abo
->placement
;
213 * amdgpu_verify_access - Verify access for a mmap call
215 * @bo: The buffer object to map
216 * @filp: The file pointer from the process performing the mmap
218 * This is called by ttm_bo_mmap() to verify whether a process
219 * has the right to mmap a BO to their process space.
221 static int amdgpu_verify_access(struct ttm_buffer_object
*bo
, struct file
*filp
)
223 struct amdgpu_bo
*abo
= ttm_to_amdgpu_bo(bo
);
226 * Don't verify access for KFD BOs. They don't have a GEM
227 * object associated with them.
232 if (amdgpu_ttm_tt_get_usermm(bo
->ttm
))
234 return drm_vma_node_verify_access(&abo
->tbo
.base
.vma_node
,
239 * amdgpu_move_null - Register memory for a buffer object
241 * @bo: The bo to assign the memory to
242 * @new_mem: The memory to be assigned.
244 * Assign the memory from new_mem to the memory of the buffer object bo.
246 static void amdgpu_move_null(struct ttm_buffer_object
*bo
,
247 struct ttm_mem_reg
*new_mem
)
249 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
251 BUG_ON(old_mem
->mm_node
!= NULL
);
253 new_mem
->mm_node
= NULL
;
257 * amdgpu_mm_node_addr - Compute the GPU relative offset of a GTT buffer.
259 * @bo: The bo to assign the memory to.
260 * @mm_node: Memory manager node for drm allocator.
261 * @mem: The region where the bo resides.
264 static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object
*bo
,
265 struct drm_mm_node
*mm_node
,
266 struct ttm_mem_reg
*mem
)
270 if (mm_node
->start
!= AMDGPU_BO_INVALID_OFFSET
) {
271 addr
= mm_node
->start
<< PAGE_SHIFT
;
272 addr
+= bo
->bdev
->man
[mem
->mem_type
].gpu_offset
;
278 * amdgpu_find_mm_node - Helper function finds the drm_mm_node corresponding to
279 * @offset. It also modifies the offset to be within the drm_mm_node returned
281 * @mem: The region where the bo resides.
282 * @offset: The offset that drm_mm_node is used for finding.
285 static struct drm_mm_node
*amdgpu_find_mm_node(struct ttm_mem_reg
*mem
,
286 unsigned long *offset
)
288 struct drm_mm_node
*mm_node
= mem
->mm_node
;
290 while (*offset
>= (mm_node
->size
<< PAGE_SHIFT
)) {
291 *offset
-= (mm_node
->size
<< PAGE_SHIFT
);
298 * amdgpu_copy_ttm_mem_to_mem - Helper function for copy
300 * The function copies @size bytes from {src->mem + src->offset} to
301 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
302 * move and different for a BO to BO copy.
304 * @f: Returns the last fence if multiple jobs are submitted.
306 int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device
*adev
,
307 struct amdgpu_copy_mem
*src
,
308 struct amdgpu_copy_mem
*dst
,
310 struct dma_resv
*resv
,
311 struct dma_fence
**f
)
313 struct amdgpu_ring
*ring
= adev
->mman
.buffer_funcs_ring
;
314 struct drm_mm_node
*src_mm
, *dst_mm
;
315 uint64_t src_node_start
, dst_node_start
, src_node_size
,
316 dst_node_size
, src_page_offset
, dst_page_offset
;
317 struct dma_fence
*fence
= NULL
;
319 const uint64_t GTT_MAX_BYTES
= (AMDGPU_GTT_MAX_TRANSFER_SIZE
*
320 AMDGPU_GPU_PAGE_SIZE
);
322 if (!adev
->mman
.buffer_funcs_enabled
) {
323 DRM_ERROR("Trying to move memory with ring turned off.\n");
327 src_mm
= amdgpu_find_mm_node(src
->mem
, &src
->offset
);
328 src_node_start
= amdgpu_mm_node_addr(src
->bo
, src_mm
, src
->mem
) +
330 src_node_size
= (src_mm
->size
<< PAGE_SHIFT
) - src
->offset
;
331 src_page_offset
= src_node_start
& (PAGE_SIZE
- 1);
333 dst_mm
= amdgpu_find_mm_node(dst
->mem
, &dst
->offset
);
334 dst_node_start
= amdgpu_mm_node_addr(dst
->bo
, dst_mm
, dst
->mem
) +
336 dst_node_size
= (dst_mm
->size
<< PAGE_SHIFT
) - dst
->offset
;
337 dst_page_offset
= dst_node_start
& (PAGE_SIZE
- 1);
339 mutex_lock(&adev
->mman
.gtt_window_lock
);
342 unsigned long cur_size
;
343 uint64_t from
= src_node_start
, to
= dst_node_start
;
344 struct dma_fence
*next
;
346 /* Copy size cannot exceed GTT_MAX_BYTES. So if src or dst
347 * begins at an offset, then adjust the size accordingly
349 cur_size
= min3(min(src_node_size
, dst_node_size
), size
,
351 if (cur_size
+ src_page_offset
> GTT_MAX_BYTES
||
352 cur_size
+ dst_page_offset
> GTT_MAX_BYTES
)
353 cur_size
-= max(src_page_offset
, dst_page_offset
);
355 /* Map only what needs to be accessed. Map src to window 0 and
358 if (src
->mem
->start
== AMDGPU_BO_INVALID_OFFSET
) {
359 r
= amdgpu_map_buffer(src
->bo
, src
->mem
,
360 PFN_UP(cur_size
+ src_page_offset
),
361 src_node_start
, 0, ring
,
365 /* Adjust the offset because amdgpu_map_buffer returns
366 * start of mapped page
368 from
+= src_page_offset
;
371 if (dst
->mem
->start
== AMDGPU_BO_INVALID_OFFSET
) {
372 r
= amdgpu_map_buffer(dst
->bo
, dst
->mem
,
373 PFN_UP(cur_size
+ dst_page_offset
),
374 dst_node_start
, 1, ring
,
378 to
+= dst_page_offset
;
381 r
= amdgpu_copy_buffer(ring
, from
, to
, cur_size
,
382 resv
, &next
, false, true);
386 dma_fence_put(fence
);
393 src_node_size
-= cur_size
;
394 if (!src_node_size
) {
395 src_node_start
= amdgpu_mm_node_addr(src
->bo
, ++src_mm
,
397 src_node_size
= (src_mm
->size
<< PAGE_SHIFT
);
400 src_node_start
+= cur_size
;
401 src_page_offset
= src_node_start
& (PAGE_SIZE
- 1);
403 dst_node_size
-= cur_size
;
404 if (!dst_node_size
) {
405 dst_node_start
= amdgpu_mm_node_addr(dst
->bo
, ++dst_mm
,
407 dst_node_size
= (dst_mm
->size
<< PAGE_SHIFT
);
410 dst_node_start
+= cur_size
;
411 dst_page_offset
= dst_node_start
& (PAGE_SIZE
- 1);
415 mutex_unlock(&adev
->mman
.gtt_window_lock
);
417 *f
= dma_fence_get(fence
);
418 dma_fence_put(fence
);
423 * amdgpu_move_blit - Copy an entire buffer to another buffer
425 * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
426 * help move buffers to and from VRAM.
428 static int amdgpu_move_blit(struct ttm_buffer_object
*bo
,
429 bool evict
, bool no_wait_gpu
,
430 struct ttm_mem_reg
*new_mem
,
431 struct ttm_mem_reg
*old_mem
)
433 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->bdev
);
434 struct amdgpu_copy_mem src
, dst
;
435 struct dma_fence
*fence
= NULL
;
445 r
= amdgpu_ttm_copy_mem_to_mem(adev
, &src
, &dst
,
446 new_mem
->num_pages
<< PAGE_SHIFT
,
447 bo
->base
.resv
, &fence
);
451 /* clear the space being freed */
452 if (old_mem
->mem_type
== TTM_PL_VRAM
&&
453 (ttm_to_amdgpu_bo(bo
)->flags
&
454 AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE
)) {
455 struct dma_fence
*wipe_fence
= NULL
;
457 r
= amdgpu_fill_buffer(ttm_to_amdgpu_bo(bo
), AMDGPU_POISON
,
461 } else if (wipe_fence
) {
462 dma_fence_put(fence
);
467 /* Always block for VM page tables before committing the new location */
468 if (bo
->type
== ttm_bo_type_kernel
)
469 r
= ttm_bo_move_accel_cleanup(bo
, fence
, true, new_mem
);
471 r
= ttm_bo_pipeline_move(bo
, fence
, evict
, new_mem
);
472 dma_fence_put(fence
);
477 dma_fence_wait(fence
, false);
478 dma_fence_put(fence
);
483 * amdgpu_move_vram_ram - Copy VRAM buffer to RAM buffer
485 * Called by amdgpu_bo_move().
487 static int amdgpu_move_vram_ram(struct ttm_buffer_object
*bo
, bool evict
,
488 struct ttm_operation_ctx
*ctx
,
489 struct ttm_mem_reg
*new_mem
)
491 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
492 struct ttm_mem_reg tmp_mem
;
493 struct ttm_place placements
;
494 struct ttm_placement placement
;
497 /* create space/pages for new_mem in GTT space */
499 tmp_mem
.mm_node
= NULL
;
500 placement
.num_placement
= 1;
501 placement
.placement
= &placements
;
502 placement
.num_busy_placement
= 1;
503 placement
.busy_placement
= &placements
;
506 placements
.flags
= TTM_PL_MASK_CACHING
| TTM_PL_FLAG_TT
;
507 r
= ttm_bo_mem_space(bo
, &placement
, &tmp_mem
, ctx
);
509 pr_err("Failed to find GTT space for blit from VRAM\n");
513 /* set caching flags */
514 r
= ttm_tt_set_placement_caching(bo
->ttm
, tmp_mem
.placement
);
519 /* Bind the memory to the GTT space */
520 r
= ttm_tt_bind(bo
->ttm
, &tmp_mem
, ctx
);
525 /* blit VRAM to GTT */
526 r
= amdgpu_move_blit(bo
, evict
, ctx
->no_wait_gpu
, &tmp_mem
, old_mem
);
531 /* move BO (in tmp_mem) to new_mem */
532 r
= ttm_bo_move_ttm(bo
, ctx
, new_mem
);
534 ttm_bo_mem_put(bo
, &tmp_mem
);
539 * amdgpu_move_ram_vram - Copy buffer from RAM to VRAM
541 * Called by amdgpu_bo_move().
543 static int amdgpu_move_ram_vram(struct ttm_buffer_object
*bo
, bool evict
,
544 struct ttm_operation_ctx
*ctx
,
545 struct ttm_mem_reg
*new_mem
)
547 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
548 struct ttm_mem_reg tmp_mem
;
549 struct ttm_placement placement
;
550 struct ttm_place placements
;
553 /* make space in GTT for old_mem buffer */
555 tmp_mem
.mm_node
= NULL
;
556 placement
.num_placement
= 1;
557 placement
.placement
= &placements
;
558 placement
.num_busy_placement
= 1;
559 placement
.busy_placement
= &placements
;
562 placements
.flags
= TTM_PL_MASK_CACHING
| TTM_PL_FLAG_TT
;
563 r
= ttm_bo_mem_space(bo
, &placement
, &tmp_mem
, ctx
);
565 pr_err("Failed to find GTT space for blit to VRAM\n");
569 /* move/bind old memory to GTT space */
570 r
= ttm_bo_move_ttm(bo
, ctx
, &tmp_mem
);
576 r
= amdgpu_move_blit(bo
, evict
, ctx
->no_wait_gpu
, new_mem
, old_mem
);
581 ttm_bo_mem_put(bo
, &tmp_mem
);
586 * amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
588 * Called by amdgpu_bo_move()
590 static bool amdgpu_mem_visible(struct amdgpu_device
*adev
,
591 struct ttm_mem_reg
*mem
)
593 struct drm_mm_node
*nodes
= mem
->mm_node
;
595 if (mem
->mem_type
== TTM_PL_SYSTEM
||
596 mem
->mem_type
== TTM_PL_TT
)
598 if (mem
->mem_type
!= TTM_PL_VRAM
)
601 /* ttm_mem_reg_ioremap only supports contiguous memory */
602 if (nodes
->size
!= mem
->num_pages
)
605 return ((nodes
->start
+ nodes
->size
) << PAGE_SHIFT
)
606 <= adev
->gmc
.visible_vram_size
;
610 * amdgpu_bo_move - Move a buffer object to a new memory location
612 * Called by ttm_bo_handle_move_mem()
614 static int amdgpu_bo_move(struct ttm_buffer_object
*bo
, bool evict
,
615 struct ttm_operation_ctx
*ctx
,
616 struct ttm_mem_reg
*new_mem
)
618 struct amdgpu_device
*adev
;
619 struct amdgpu_bo
*abo
;
620 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
623 /* Can't move a pinned BO */
624 abo
= ttm_to_amdgpu_bo(bo
);
625 if (WARN_ON_ONCE(abo
->pin_count
> 0))
628 adev
= amdgpu_ttm_adev(bo
->bdev
);
630 if (old_mem
->mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
631 amdgpu_move_null(bo
, new_mem
);
634 if ((old_mem
->mem_type
== TTM_PL_TT
&&
635 new_mem
->mem_type
== TTM_PL_SYSTEM
) ||
636 (old_mem
->mem_type
== TTM_PL_SYSTEM
&&
637 new_mem
->mem_type
== TTM_PL_TT
)) {
639 amdgpu_move_null(bo
, new_mem
);
642 if (old_mem
->mem_type
== AMDGPU_PL_GDS
||
643 old_mem
->mem_type
== AMDGPU_PL_GWS
||
644 old_mem
->mem_type
== AMDGPU_PL_OA
||
645 new_mem
->mem_type
== AMDGPU_PL_GDS
||
646 new_mem
->mem_type
== AMDGPU_PL_GWS
||
647 new_mem
->mem_type
== AMDGPU_PL_OA
) {
648 /* Nothing to save here */
649 amdgpu_move_null(bo
, new_mem
);
653 if (!adev
->mman
.buffer_funcs_enabled
) {
658 if (old_mem
->mem_type
== TTM_PL_VRAM
&&
659 new_mem
->mem_type
== TTM_PL_SYSTEM
) {
660 r
= amdgpu_move_vram_ram(bo
, evict
, ctx
, new_mem
);
661 } else if (old_mem
->mem_type
== TTM_PL_SYSTEM
&&
662 new_mem
->mem_type
== TTM_PL_VRAM
) {
663 r
= amdgpu_move_ram_vram(bo
, evict
, ctx
, new_mem
);
665 r
= amdgpu_move_blit(bo
, evict
, ctx
->no_wait_gpu
,
671 /* Check that all memory is CPU accessible */
672 if (!amdgpu_mem_visible(adev
, old_mem
) ||
673 !amdgpu_mem_visible(adev
, new_mem
)) {
674 pr_err("Move buffer fallback to memcpy unavailable\n");
678 r
= ttm_bo_move_memcpy(bo
, ctx
, new_mem
);
683 if (bo
->type
== ttm_bo_type_device
&&
684 new_mem
->mem_type
== TTM_PL_VRAM
&&
685 old_mem
->mem_type
!= TTM_PL_VRAM
) {
686 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
687 * accesses the BO after it's moved.
689 abo
->flags
&= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
;
692 /* update statistics */
693 atomic64_add((u64
)bo
->num_pages
<< PAGE_SHIFT
, &adev
->num_bytes_moved
);
698 * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
700 * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
702 static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
704 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
705 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bdev
);
706 struct drm_mm_node
*mm_node
= mem
->mm_node
;
708 mem
->bus
.addr
= NULL
;
710 mem
->bus
.size
= mem
->num_pages
<< PAGE_SHIFT
;
712 mem
->bus
.is_iomem
= false;
713 if (!(man
->flags
& TTM_MEMTYPE_FLAG_MAPPABLE
))
715 switch (mem
->mem_type
) {
722 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
723 /* check if it's visible */
724 if ((mem
->bus
.offset
+ mem
->bus
.size
) > adev
->gmc
.visible_vram_size
)
726 /* Only physically contiguous buffers apply. In a contiguous
727 * buffer, size of the first mm_node would match the number of
728 * pages in ttm_mem_reg.
730 if (adev
->mman
.aper_base_kaddr
&&
731 (mm_node
->size
== mem
->num_pages
))
732 mem
->bus
.addr
= (u8
*)adev
->mman
.aper_base_kaddr
+
735 mem
->bus
.base
= adev
->gmc
.aper_base
;
736 mem
->bus
.is_iomem
= true;
744 static void amdgpu_ttm_io_mem_free(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
748 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object
*bo
,
749 unsigned long page_offset
)
751 struct drm_mm_node
*mm
;
752 unsigned long offset
= (page_offset
<< PAGE_SHIFT
);
754 mm
= amdgpu_find_mm_node(&bo
->mem
, &offset
);
755 return (bo
->mem
.bus
.base
>> PAGE_SHIFT
) + mm
->start
+
756 (offset
>> PAGE_SHIFT
);
760 * TTM backend functions.
762 struct amdgpu_ttm_tt
{
763 struct ttm_dma_tt ttm
;
764 struct drm_gem_object
*gobj
;
767 struct task_struct
*usertask
;
769 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
770 struct hmm_range
*range
;
774 #ifdef CONFIG_DRM_AMDGPU_USERPTR
775 /* flags used by HMM internal, not related to CPU/GPU PTE flags */
776 static const uint64_t hmm_range_flags
[HMM_PFN_FLAG_MAX
] = {
777 (1 << 0), /* HMM_PFN_VALID */
778 (1 << 1), /* HMM_PFN_WRITE */
779 0 /* HMM_PFN_DEVICE_PRIVATE */
782 static const uint64_t hmm_range_values
[HMM_PFN_VALUE_MAX
] = {
783 0xfffffffffffffffeUL
, /* HMM_PFN_ERROR */
784 0, /* HMM_PFN_NONE */
785 0xfffffffffffffffcUL
/* HMM_PFN_SPECIAL */
789 * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user
790 * memory and start HMM tracking CPU page table update
792 * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only
793 * once afterwards to stop HMM tracking
795 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo
*bo
, struct page
**pages
)
797 struct ttm_tt
*ttm
= bo
->tbo
.ttm
;
798 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
799 unsigned long start
= gtt
->userptr
;
800 struct vm_area_struct
*vma
;
801 struct hmm_range
*range
;
802 unsigned long timeout
;
803 struct mm_struct
*mm
;
807 mm
= bo
->notifier
.mm
;
809 DRM_DEBUG_DRIVER("BO is not registered?\n");
813 /* Another get_user_pages is running at the same time?? */
814 if (WARN_ON(gtt
->range
))
817 if (!mmget_not_zero(mm
)) /* Happens during process shutdown */
820 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
821 if (unlikely(!range
)) {
825 range
->notifier
= &bo
->notifier
;
826 range
->flags
= hmm_range_flags
;
827 range
->values
= hmm_range_values
;
828 range
->pfn_shift
= PAGE_SHIFT
;
829 range
->start
= bo
->notifier
.interval_tree
.start
;
830 range
->end
= bo
->notifier
.interval_tree
.last
+ 1;
831 range
->default_flags
= hmm_range_flags
[HMM_PFN_VALID
];
832 if (!amdgpu_ttm_tt_is_readonly(ttm
))
833 range
->default_flags
|= range
->flags
[HMM_PFN_WRITE
];
835 range
->pfns
= kvmalloc_array(ttm
->num_pages
, sizeof(*range
->pfns
),
837 if (unlikely(!range
->pfns
)) {
839 goto out_free_ranges
;
842 down_read(&mm
->mmap_sem
);
843 vma
= find_vma(mm
, start
);
844 if (unlikely(!vma
|| start
< vma
->vm_start
)) {
848 if (unlikely((gtt
->userflags
& AMDGPU_GEM_USERPTR_ANONONLY
) &&
853 up_read(&mm
->mmap_sem
);
854 timeout
= jiffies
+ msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT
);
857 range
->notifier_seq
= mmu_interval_read_begin(&bo
->notifier
);
859 down_read(&mm
->mmap_sem
);
860 r
= hmm_range_fault(range
, 0);
861 up_read(&mm
->mmap_sem
);
862 if (unlikely(r
<= 0)) {
864 * FIXME: This timeout should encompass the retry from
865 * mmu_interval_read_retry() as well.
867 if ((r
== 0 || r
== -EBUSY
) && !time_after(jiffies
, timeout
))
872 for (i
= 0; i
< ttm
->num_pages
; i
++) {
873 /* FIXME: The pages cannot be touched outside the notifier_lock */
874 pages
[i
] = hmm_device_entry_to_page(range
, range
->pfns
[i
]);
875 if (unlikely(!pages
[i
])) {
876 pr_err("Page fault failed for pfn[%lu] = 0x%llx\n",
890 up_read(&mm
->mmap_sem
);
901 * amdgpu_ttm_tt_userptr_range_done - stop HMM track the CPU page table change
902 * Check if the pages backing this ttm range have been invalidated
904 * Returns: true if pages are still valid
906 bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt
*ttm
)
908 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
911 if (!gtt
|| !gtt
->userptr
)
914 DRM_DEBUG_DRIVER("user_pages_done 0x%llx pages 0x%lx\n",
915 gtt
->userptr
, ttm
->num_pages
);
917 WARN_ONCE(!gtt
->range
|| !gtt
->range
->pfns
,
918 "No user pages to check\n");
922 * FIXME: Must always hold notifier_lock for this, and must
923 * not ignore the return code.
925 r
= mmu_interval_read_retry(gtt
->range
->notifier
,
926 gtt
->range
->notifier_seq
);
927 kvfree(gtt
->range
->pfns
);
937 * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
939 * Called by amdgpu_cs_list_validate(). This creates the page list
940 * that backs user memory and will ultimately be mapped into the device
943 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt
*ttm
, struct page
**pages
)
947 for (i
= 0; i
< ttm
->num_pages
; ++i
)
948 ttm
->pages
[i
] = pages
? pages
[i
] : NULL
;
952 * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages
954 * Called by amdgpu_ttm_backend_bind()
956 static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt
*ttm
)
958 struct amdgpu_device
*adev
= amdgpu_ttm_adev(ttm
->bdev
);
959 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
963 int write
= !(gtt
->userflags
& AMDGPU_GEM_USERPTR_READONLY
);
964 enum dma_data_direction direction
= write
?
965 DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
;
967 /* Allocate an SG array and squash pages into it */
968 r
= sg_alloc_table_from_pages(ttm
->sg
, ttm
->pages
, ttm
->num_pages
, 0,
969 ttm
->num_pages
<< PAGE_SHIFT
,
974 /* Map SG to device */
976 nents
= dma_map_sg(adev
->dev
, ttm
->sg
->sgl
, ttm
->sg
->nents
, direction
);
977 if (nents
!= ttm
->sg
->nents
)
980 /* convert SG to linear array of pages and dma addresses */
981 drm_prime_sg_to_page_addr_arrays(ttm
->sg
, ttm
->pages
,
982 gtt
->ttm
.dma_address
, ttm
->num_pages
);
992 * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
994 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt
*ttm
)
996 struct amdgpu_device
*adev
= amdgpu_ttm_adev(ttm
->bdev
);
997 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
999 int write
= !(gtt
->userflags
& AMDGPU_GEM_USERPTR_READONLY
);
1000 enum dma_data_direction direction
= write
?
1001 DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
;
1003 /* double check that we don't free the table twice */
1007 /* unmap the pages mapped to the device */
1008 dma_unmap_sg(adev
->dev
, ttm
->sg
->sgl
, ttm
->sg
->nents
, direction
);
1010 sg_free_table(ttm
->sg
);
1012 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
1016 for (i
= 0; i
< ttm
->num_pages
; i
++) {
1017 if (ttm
->pages
[i
] !=
1018 hmm_device_entry_to_page(gtt
->range
,
1019 gtt
->range
->pfns
[i
]))
1023 WARN((i
== ttm
->num_pages
), "Missing get_user_page_done\n");
1028 int amdgpu_ttm_gart_bind(struct amdgpu_device
*adev
,
1029 struct ttm_buffer_object
*tbo
,
1032 struct amdgpu_bo
*abo
= ttm_to_amdgpu_bo(tbo
);
1033 struct ttm_tt
*ttm
= tbo
->ttm
;
1034 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1037 if (abo
->flags
& AMDGPU_GEM_CREATE_MQD_GFX9
) {
1038 uint64_t page_idx
= 1;
1040 r
= amdgpu_gart_bind(adev
, gtt
->offset
, page_idx
,
1041 ttm
->pages
, gtt
->ttm
.dma_address
, flags
);
1043 goto gart_bind_fail
;
1045 /* Patch mtype of the second part BO */
1046 flags
&= ~AMDGPU_PTE_MTYPE_VG10_MASK
;
1047 flags
|= AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC
);
1049 r
= amdgpu_gart_bind(adev
,
1050 gtt
->offset
+ (page_idx
<< PAGE_SHIFT
),
1051 ttm
->num_pages
- page_idx
,
1052 &ttm
->pages
[page_idx
],
1053 &(gtt
->ttm
.dma_address
[page_idx
]), flags
);
1055 r
= amdgpu_gart_bind(adev
, gtt
->offset
, ttm
->num_pages
,
1056 ttm
->pages
, gtt
->ttm
.dma_address
, flags
);
1061 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
1062 ttm
->num_pages
, gtt
->offset
);
1068 * amdgpu_ttm_backend_bind - Bind GTT memory
1070 * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
1071 * This handles binding GTT memory to the device address space.
1073 static int amdgpu_ttm_backend_bind(struct ttm_tt
*ttm
,
1074 struct ttm_mem_reg
*bo_mem
)
1076 struct amdgpu_device
*adev
= amdgpu_ttm_adev(ttm
->bdev
);
1077 struct amdgpu_ttm_tt
*gtt
= (void*)ttm
;
1082 r
= amdgpu_ttm_tt_pin_userptr(ttm
);
1084 DRM_ERROR("failed to pin userptr\n");
1088 if (!ttm
->num_pages
) {
1089 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
1090 ttm
->num_pages
, bo_mem
, ttm
);
1093 if (bo_mem
->mem_type
== AMDGPU_PL_GDS
||
1094 bo_mem
->mem_type
== AMDGPU_PL_GWS
||
1095 bo_mem
->mem_type
== AMDGPU_PL_OA
)
1098 if (!amdgpu_gtt_mgr_has_gart_addr(bo_mem
)) {
1099 gtt
->offset
= AMDGPU_BO_INVALID_OFFSET
;
1103 /* compute PTE flags relevant to this BO memory */
1104 flags
= amdgpu_ttm_tt_pte_flags(adev
, ttm
, bo_mem
);
1106 /* bind pages into GART page tables */
1107 gtt
->offset
= (u64
)bo_mem
->start
<< PAGE_SHIFT
;
1108 r
= amdgpu_gart_bind(adev
, gtt
->offset
, ttm
->num_pages
,
1109 ttm
->pages
, gtt
->ttm
.dma_address
, flags
);
1112 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
1113 ttm
->num_pages
, gtt
->offset
);
1118 * amdgpu_ttm_alloc_gart - Allocate GART memory for buffer object
1120 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object
*bo
)
1122 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->bdev
);
1123 struct ttm_operation_ctx ctx
= { false, false };
1124 struct amdgpu_ttm_tt
*gtt
= (void*)bo
->ttm
;
1125 struct ttm_mem_reg tmp
;
1126 struct ttm_placement placement
;
1127 struct ttm_place placements
;
1128 uint64_t addr
, flags
;
1131 if (bo
->mem
.start
!= AMDGPU_BO_INVALID_OFFSET
)
1134 addr
= amdgpu_gmc_agp_addr(bo
);
1135 if (addr
!= AMDGPU_BO_INVALID_OFFSET
) {
1136 bo
->mem
.start
= addr
>> PAGE_SHIFT
;
1139 /* allocate GART space */
1142 placement
.num_placement
= 1;
1143 placement
.placement
= &placements
;
1144 placement
.num_busy_placement
= 1;
1145 placement
.busy_placement
= &placements
;
1146 placements
.fpfn
= 0;
1147 placements
.lpfn
= adev
->gmc
.gart_size
>> PAGE_SHIFT
;
1148 placements
.flags
= (bo
->mem
.placement
& ~TTM_PL_MASK_MEM
) |
1151 r
= ttm_bo_mem_space(bo
, &placement
, &tmp
, &ctx
);
1155 /* compute PTE flags for this buffer object */
1156 flags
= amdgpu_ttm_tt_pte_flags(adev
, bo
->ttm
, &tmp
);
1159 gtt
->offset
= (u64
)tmp
.start
<< PAGE_SHIFT
;
1160 r
= amdgpu_ttm_gart_bind(adev
, bo
, flags
);
1162 ttm_bo_mem_put(bo
, &tmp
);
1166 ttm_bo_mem_put(bo
, &bo
->mem
);
1170 bo
->offset
= (bo
->mem
.start
<< PAGE_SHIFT
) +
1171 bo
->bdev
->man
[bo
->mem
.mem_type
].gpu_offset
;
1177 * amdgpu_ttm_recover_gart - Rebind GTT pages
1179 * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
1180 * rebind GTT pages during a GPU reset.
1182 int amdgpu_ttm_recover_gart(struct ttm_buffer_object
*tbo
)
1184 struct amdgpu_device
*adev
= amdgpu_ttm_adev(tbo
->bdev
);
1191 flags
= amdgpu_ttm_tt_pte_flags(adev
, tbo
->ttm
, &tbo
->mem
);
1192 r
= amdgpu_ttm_gart_bind(adev
, tbo
, flags
);
1198 * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
1200 * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
1203 static int amdgpu_ttm_backend_unbind(struct ttm_tt
*ttm
)
1205 struct amdgpu_device
*adev
= amdgpu_ttm_adev(ttm
->bdev
);
1206 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1209 /* if the pages have userptr pinning then clear that first */
1211 amdgpu_ttm_tt_unpin_userptr(ttm
);
1213 if (gtt
->offset
== AMDGPU_BO_INVALID_OFFSET
)
1216 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1217 r
= amdgpu_gart_unbind(adev
, gtt
->offset
, ttm
->num_pages
);
1219 DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
1220 gtt
->ttm
.ttm
.num_pages
, gtt
->offset
);
1224 static void amdgpu_ttm_backend_destroy(struct ttm_tt
*ttm
)
1226 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1229 put_task_struct(gtt
->usertask
);
1231 ttm_dma_tt_fini(>t
->ttm
);
1235 static struct ttm_backend_func amdgpu_backend_func
= {
1236 .bind
= &amdgpu_ttm_backend_bind
,
1237 .unbind
= &amdgpu_ttm_backend_unbind
,
1238 .destroy
= &amdgpu_ttm_backend_destroy
,
1242 * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1244 * @bo: The buffer object to create a GTT ttm_tt object around
1246 * Called by ttm_tt_create().
1248 static struct ttm_tt
*amdgpu_ttm_tt_create(struct ttm_buffer_object
*bo
,
1249 uint32_t page_flags
)
1251 struct amdgpu_ttm_tt
*gtt
;
1253 gtt
= kzalloc(sizeof(struct amdgpu_ttm_tt
), GFP_KERNEL
);
1257 gtt
->ttm
.ttm
.func
= &amdgpu_backend_func
;
1258 gtt
->gobj
= &bo
->base
;
1260 /* allocate space for the uninitialized page entries */
1261 if (ttm_sg_tt_init(>t
->ttm
, bo
, page_flags
)) {
1265 return >t
->ttm
.ttm
;
1269 * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1271 * Map the pages of a ttm_tt object to an address space visible
1272 * to the underlying device.
1274 static int amdgpu_ttm_tt_populate(struct ttm_tt
*ttm
,
1275 struct ttm_operation_ctx
*ctx
)
1277 struct amdgpu_device
*adev
= amdgpu_ttm_adev(ttm
->bdev
);
1278 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1280 /* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1281 if (gtt
&& gtt
->userptr
) {
1282 ttm
->sg
= kzalloc(sizeof(struct sg_table
), GFP_KERNEL
);
1286 ttm
->page_flags
|= TTM_PAGE_FLAG_SG
;
1287 ttm
->state
= tt_unbound
;
1291 if (ttm
->page_flags
& TTM_PAGE_FLAG_SG
) {
1293 struct dma_buf_attachment
*attach
;
1294 struct sg_table
*sgt
;
1296 attach
= gtt
->gobj
->import_attach
;
1297 sgt
= dma_buf_map_attachment(attach
, DMA_BIDIRECTIONAL
);
1299 return PTR_ERR(sgt
);
1304 drm_prime_sg_to_page_addr_arrays(ttm
->sg
, ttm
->pages
,
1305 gtt
->ttm
.dma_address
,
1307 ttm
->state
= tt_unbound
;
1311 #ifdef CONFIG_SWIOTLB
1312 if (adev
->need_swiotlb
&& swiotlb_nr_tbl()) {
1313 return ttm_dma_populate(>t
->ttm
, adev
->dev
, ctx
);
1317 /* fall back to generic helper to populate the page array
1318 * and map them to the device */
1319 return ttm_populate_and_map_pages(adev
->dev
, >t
->ttm
, ctx
);
1323 * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1325 * Unmaps pages of a ttm_tt object from the device address space and
1326 * unpopulates the page array backing it.
1328 static void amdgpu_ttm_tt_unpopulate(struct ttm_tt
*ttm
)
1330 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1331 struct amdgpu_device
*adev
;
1333 if (gtt
&& gtt
->userptr
) {
1334 amdgpu_ttm_tt_set_user_pages(ttm
, NULL
);
1336 ttm
->page_flags
&= ~TTM_PAGE_FLAG_SG
;
1340 if (ttm
->sg
&& gtt
->gobj
->import_attach
) {
1341 struct dma_buf_attachment
*attach
;
1343 attach
= gtt
->gobj
->import_attach
;
1344 dma_buf_unmap_attachment(attach
, ttm
->sg
, DMA_BIDIRECTIONAL
);
1349 if (ttm
->page_flags
& TTM_PAGE_FLAG_SG
)
1352 adev
= amdgpu_ttm_adev(ttm
->bdev
);
1354 #ifdef CONFIG_SWIOTLB
1355 if (adev
->need_swiotlb
&& swiotlb_nr_tbl()) {
1356 ttm_dma_unpopulate(>t
->ttm
, adev
->dev
);
1361 /* fall back to generic helper to unmap and unpopulate array */
1362 ttm_unmap_and_unpopulate_pages(adev
->dev
, >t
->ttm
);
1366 * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1369 * @ttm: The ttm_tt object to bind this userptr object to
1370 * @addr: The address in the current tasks VM space to use
1371 * @flags: Requirements of userptr object.
1373 * Called by amdgpu_gem_userptr_ioctl() to bind userptr pages
1376 int amdgpu_ttm_tt_set_userptr(struct ttm_tt
*ttm
, uint64_t addr
,
1379 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1384 gtt
->userptr
= addr
;
1385 gtt
->userflags
= flags
;
1388 put_task_struct(gtt
->usertask
);
1389 gtt
->usertask
= current
->group_leader
;
1390 get_task_struct(gtt
->usertask
);
1396 * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1398 struct mm_struct
*amdgpu_ttm_tt_get_usermm(struct ttm_tt
*ttm
)
1400 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1405 if (gtt
->usertask
== NULL
)
1408 return gtt
->usertask
->mm
;
1412 * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1413 * address range for the current task.
1416 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt
*ttm
, unsigned long start
,
1419 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1422 if (gtt
== NULL
|| !gtt
->userptr
)
1425 /* Return false if no part of the ttm_tt object lies within
1428 size
= (unsigned long)gtt
->ttm
.ttm
.num_pages
* PAGE_SIZE
;
1429 if (gtt
->userptr
> end
|| gtt
->userptr
+ size
<= start
)
1436 * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
1438 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt
*ttm
)
1440 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1442 if (gtt
== NULL
|| !gtt
->userptr
)
1449 * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1451 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt
*ttm
)
1453 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
1458 return !!(gtt
->userflags
& AMDGPU_GEM_USERPTR_READONLY
);
1462 * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object
1464 * @ttm: The ttm_tt object to compute the flags for
1465 * @mem: The memory registry backing this ttm_tt object
1467 * Figure out the flags to use for a VM PDE (Page Directory Entry).
1469 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt
*ttm
, struct ttm_mem_reg
*mem
)
1473 if (mem
&& mem
->mem_type
!= TTM_PL_SYSTEM
)
1474 flags
|= AMDGPU_PTE_VALID
;
1476 if (mem
&& mem
->mem_type
== TTM_PL_TT
) {
1477 flags
|= AMDGPU_PTE_SYSTEM
;
1479 if (ttm
->caching_state
== tt_cached
)
1480 flags
|= AMDGPU_PTE_SNOOPED
;
1487 * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1489 * @ttm: The ttm_tt object to compute the flags for
1490 * @mem: The memory registry backing this ttm_tt object
1492 * Figure out the flags to use for a VM PTE (Page Table Entry).
1494 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device
*adev
, struct ttm_tt
*ttm
,
1495 struct ttm_mem_reg
*mem
)
1497 uint64_t flags
= amdgpu_ttm_tt_pde_flags(ttm
, mem
);
1499 flags
|= adev
->gart
.gart_pte_flags
;
1500 flags
|= AMDGPU_PTE_READABLE
;
1502 if (!amdgpu_ttm_tt_is_readonly(ttm
))
1503 flags
|= AMDGPU_PTE_WRITEABLE
;
1509 * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1512 * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1513 * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1514 * it can find space for a new object and by ttm_bo_force_list_clean() which is
1515 * used to clean out a memory space.
1517 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object
*bo
,
1518 const struct ttm_place
*place
)
1520 unsigned long num_pages
= bo
->mem
.num_pages
;
1521 struct drm_mm_node
*node
= bo
->mem
.mm_node
;
1522 struct dma_resv_list
*flist
;
1523 struct dma_fence
*f
;
1526 if (bo
->type
== ttm_bo_type_kernel
&&
1527 !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo
)))
1530 /* If bo is a KFD BO, check if the bo belongs to the current process.
1531 * If true, then return false as any KFD process needs all its BOs to
1532 * be resident to run successfully
1534 flist
= dma_resv_get_list(bo
->base
.resv
);
1536 for (i
= 0; i
< flist
->shared_count
; ++i
) {
1537 f
= rcu_dereference_protected(flist
->shared
[i
],
1538 dma_resv_held(bo
->base
.resv
));
1539 if (amdkfd_fence_check_mm(f
, current
->mm
))
1544 switch (bo
->mem
.mem_type
) {
1549 /* Check each drm MM node individually */
1551 if (place
->fpfn
< (node
->start
+ node
->size
) &&
1552 !(place
->lpfn
&& place
->lpfn
<= node
->start
))
1555 num_pages
-= node
->size
;
1564 return ttm_bo_eviction_valuable(bo
, place
);
1568 * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1570 * @bo: The buffer object to read/write
1571 * @offset: Offset into buffer object
1572 * @buf: Secondary buffer to write/read from
1573 * @len: Length in bytes of access
1574 * @write: true if writing
1576 * This is used to access VRAM that backs a buffer object via MMIO
1577 * access for debugging purposes.
1579 static int amdgpu_ttm_access_memory(struct ttm_buffer_object
*bo
,
1580 unsigned long offset
,
1581 void *buf
, int len
, int write
)
1583 struct amdgpu_bo
*abo
= ttm_to_amdgpu_bo(bo
);
1584 struct amdgpu_device
*adev
= amdgpu_ttm_adev(abo
->tbo
.bdev
);
1585 struct drm_mm_node
*nodes
;
1589 unsigned long flags
;
1591 if (bo
->mem
.mem_type
!= TTM_PL_VRAM
)
1594 nodes
= amdgpu_find_mm_node(&abo
->tbo
.mem
, &offset
);
1595 pos
= (nodes
->start
<< PAGE_SHIFT
) + offset
;
1597 while (len
&& pos
< adev
->gmc
.mc_vram_size
) {
1598 uint64_t aligned_pos
= pos
& ~(uint64_t)3;
1599 uint32_t bytes
= 4 - (pos
& 3);
1600 uint32_t shift
= (pos
& 3) * 8;
1601 uint32_t mask
= 0xffffffff << shift
;
1604 mask
&= 0xffffffff >> (bytes
- len
) * 8;
1608 spin_lock_irqsave(&adev
->mmio_idx_lock
, flags
);
1609 WREG32_NO_KIQ(mmMM_INDEX
, ((uint32_t)aligned_pos
) | 0x80000000);
1610 WREG32_NO_KIQ(mmMM_INDEX_HI
, aligned_pos
>> 31);
1611 if (!write
|| mask
!= 0xffffffff)
1612 value
= RREG32_NO_KIQ(mmMM_DATA
);
1615 value
|= (*(uint32_t *)buf
<< shift
) & mask
;
1616 WREG32_NO_KIQ(mmMM_DATA
, value
);
1618 spin_unlock_irqrestore(&adev
->mmio_idx_lock
, flags
);
1620 value
= (value
& mask
) >> shift
;
1621 memcpy(buf
, &value
, bytes
);
1625 buf
= (uint8_t *)buf
+ bytes
;
1628 if (pos
>= (nodes
->start
+ nodes
->size
) << PAGE_SHIFT
) {
1630 pos
= (nodes
->start
<< PAGE_SHIFT
);
1637 static struct ttm_bo_driver amdgpu_bo_driver
= {
1638 .ttm_tt_create
= &amdgpu_ttm_tt_create
,
1639 .ttm_tt_populate
= &amdgpu_ttm_tt_populate
,
1640 .ttm_tt_unpopulate
= &amdgpu_ttm_tt_unpopulate
,
1641 .invalidate_caches
= &amdgpu_invalidate_caches
,
1642 .init_mem_type
= &amdgpu_init_mem_type
,
1643 .eviction_valuable
= amdgpu_ttm_bo_eviction_valuable
,
1644 .evict_flags
= &amdgpu_evict_flags
,
1645 .move
= &amdgpu_bo_move
,
1646 .verify_access
= &amdgpu_verify_access
,
1647 .move_notify
= &amdgpu_bo_move_notify
,
1648 .release_notify
= &amdgpu_bo_release_notify
,
1649 .fault_reserve_notify
= &amdgpu_bo_fault_reserve_notify
,
1650 .io_mem_reserve
= &amdgpu_ttm_io_mem_reserve
,
1651 .io_mem_free
= &amdgpu_ttm_io_mem_free
,
1652 .io_mem_pfn
= amdgpu_ttm_io_mem_pfn
,
1653 .access_memory
= &amdgpu_ttm_access_memory
,
1654 .del_from_lru_notify
= &amdgpu_vm_del_from_lru_notify
1658 * Firmware Reservation functions
1661 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1663 * @adev: amdgpu_device pointer
1665 * free fw reserved vram if it has been reserved.
1667 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device
*adev
)
1669 amdgpu_bo_free_kernel(&adev
->fw_vram_usage
.reserved_bo
,
1670 NULL
, &adev
->fw_vram_usage
.va
);
1674 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1676 * @adev: amdgpu_device pointer
1678 * create bo vram reservation from fw.
1680 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device
*adev
)
1682 uint64_t vram_size
= adev
->gmc
.visible_vram_size
;
1684 adev
->fw_vram_usage
.va
= NULL
;
1685 adev
->fw_vram_usage
.reserved_bo
= NULL
;
1687 if (adev
->fw_vram_usage
.size
== 0 ||
1688 adev
->fw_vram_usage
.size
> vram_size
)
1691 return amdgpu_bo_create_kernel_at(adev
,
1692 adev
->fw_vram_usage
.start_offset
,
1693 adev
->fw_vram_usage
.size
,
1694 AMDGPU_GEM_DOMAIN_VRAM
,
1695 &adev
->fw_vram_usage
.reserved_bo
,
1696 &adev
->fw_vram_usage
.va
);
1700 * Memoy training reservation functions
1704 * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram
1706 * @adev: amdgpu_device pointer
1708 * free memory training reserved vram if it has been reserved.
1710 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device
*adev
)
1712 struct psp_memory_training_context
*ctx
= &adev
->psp
.mem_train_ctx
;
1714 ctx
->init
= PSP_MEM_TRAIN_NOT_SUPPORT
;
1715 amdgpu_bo_free_kernel(&ctx
->c2p_bo
, NULL
, NULL
);
1721 static u64
amdgpu_ttm_training_get_c2p_offset(u64 vram_size
)
1723 if ((vram_size
& (SZ_1M
- 1)) < (SZ_4K
+ 1) )
1726 return ALIGN(vram_size
, SZ_1M
);
1730 * amdgpu_ttm_training_reserve_vram_init - create bo vram reservation from memory training
1732 * @adev: amdgpu_device pointer
1734 * create bo vram reservation from memory training.
1736 static int amdgpu_ttm_training_reserve_vram_init(struct amdgpu_device
*adev
)
1739 struct psp_memory_training_context
*ctx
= &adev
->psp
.mem_train_ctx
;
1741 memset(ctx
, 0, sizeof(*ctx
));
1742 if (!adev
->fw_vram_usage
.mem_train_support
) {
1743 DRM_DEBUG("memory training does not support!\n");
1747 ctx
->c2p_train_data_offset
= amdgpu_ttm_training_get_c2p_offset(adev
->gmc
.mc_vram_size
);
1748 ctx
->p2c_train_data_offset
= (adev
->gmc
.mc_vram_size
- GDDR6_MEM_TRAINING_OFFSET
);
1749 ctx
->train_data_size
= GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES
;
1751 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
1752 ctx
->train_data_size
,
1753 ctx
->p2c_train_data_offset
,
1754 ctx
->c2p_train_data_offset
);
1756 ret
= amdgpu_bo_create_kernel_at(adev
,
1757 ctx
->c2p_train_data_offset
,
1758 ctx
->train_data_size
,
1759 AMDGPU_GEM_DOMAIN_VRAM
,
1763 DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret
);
1764 amdgpu_ttm_training_reserve_vram_fini(adev
);
1768 ctx
->init
= PSP_MEM_TRAIN_RESERVE_SUCCESS
;
1773 * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1774 * gtt/vram related fields.
1776 * This initializes all of the memory space pools that the TTM layer
1777 * will need such as the GTT space (system memory mapped to the device),
1778 * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1779 * can be mapped per VMID.
1781 int amdgpu_ttm_init(struct amdgpu_device
*adev
)
1786 void *stolen_vga_buf
;
1788 mutex_init(&adev
->mman
.gtt_window_lock
);
1790 /* No others user of address space so set it to 0 */
1791 r
= ttm_bo_device_init(&adev
->mman
.bdev
,
1793 adev
->ddev
->anon_inode
->i_mapping
,
1794 adev
->ddev
->vma_offset_manager
,
1795 dma_addressing_limited(adev
->dev
));
1797 DRM_ERROR("failed initializing buffer object driver(%d).\n", r
);
1800 adev
->mman
.initialized
= true;
1802 /* We opt to avoid OOM on system pages allocations */
1803 adev
->mman
.bdev
.no_retry
= true;
1805 /* Initialize VRAM pool with all of VRAM divided into pages */
1806 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, TTM_PL_VRAM
,
1807 adev
->gmc
.real_vram_size
>> PAGE_SHIFT
);
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 *The reserved vram for memory training must be pinned to the specified
1837 *place on the VRAM, so reserve it early.
1839 r
= amdgpu_ttm_training_reserve_vram_init(adev
);
1843 /* allocate memory as required for VGA
1844 * This is used for VGA emulation and pre-OS scanout buffers to
1845 * avoid display artifacts while transitioning between pre-OS
1847 r
= amdgpu_bo_create_kernel(adev
, adev
->gmc
.stolen_size
, PAGE_SIZE
,
1848 AMDGPU_GEM_DOMAIN_VRAM
,
1849 &adev
->stolen_vga_memory
,
1850 NULL
, &stolen_vga_buf
);
1855 * reserve one TMR (64K) memory at the top of VRAM which holds
1856 * IP Discovery data and is protected by PSP.
1858 r
= amdgpu_bo_create_kernel_at(adev
,
1859 adev
->gmc
.real_vram_size
- DISCOVERY_TMR_SIZE
,
1861 AMDGPU_GEM_DOMAIN_VRAM
,
1862 &adev
->discovery_memory
,
1867 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1868 (unsigned) (adev
->gmc
.real_vram_size
/ (1024 * 1024)));
1870 /* Compute GTT size, either bsaed on 3/4th the size of RAM size
1871 * or whatever the user passed on module init */
1872 if (amdgpu_gtt_size
== -1) {
1876 gtt_size
= min(max((AMDGPU_DEFAULT_GTT_SIZE_MB
<< 20),
1877 adev
->gmc
.mc_vram_size
),
1878 ((uint64_t)si
.totalram
* si
.mem_unit
* 3/4));
1881 gtt_size
= (uint64_t)amdgpu_gtt_size
<< 20;
1883 /* Initialize GTT memory pool */
1884 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, TTM_PL_TT
, gtt_size
>> PAGE_SHIFT
);
1886 DRM_ERROR("Failed initializing GTT heap.\n");
1889 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1890 (unsigned)(gtt_size
/ (1024 * 1024)));
1892 /* Initialize various on-chip memory pools */
1893 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, AMDGPU_PL_GDS
,
1894 adev
->gds
.gds_size
);
1896 DRM_ERROR("Failed initializing GDS heap.\n");
1900 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, AMDGPU_PL_GWS
,
1901 adev
->gds
.gws_size
);
1903 DRM_ERROR("Failed initializing gws heap.\n");
1907 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, AMDGPU_PL_OA
,
1910 DRM_ERROR("Failed initializing oa heap.\n");
1914 /* Register debugfs entries for amdgpu_ttm */
1915 r
= amdgpu_ttm_debugfs_init(adev
);
1917 DRM_ERROR("Failed to init debugfs\n");
1924 * amdgpu_ttm_late_init - Handle any late initialization for amdgpu_ttm
1926 void amdgpu_ttm_late_init(struct amdgpu_device
*adev
)
1928 void *stolen_vga_buf
;
1929 /* return the VGA stolen memory (if any) back to VRAM */
1930 amdgpu_bo_free_kernel(&adev
->stolen_vga_memory
, NULL
, &stolen_vga_buf
);
1934 * amdgpu_ttm_fini - De-initialize the TTM memory pools
1936 void amdgpu_ttm_fini(struct amdgpu_device
*adev
)
1938 if (!adev
->mman
.initialized
)
1941 amdgpu_ttm_debugfs_fini(adev
);
1942 amdgpu_ttm_training_reserve_vram_fini(adev
);
1943 /* return the IP Discovery TMR memory back to VRAM */
1944 amdgpu_bo_free_kernel(&adev
->discovery_memory
, NULL
, NULL
);
1945 amdgpu_ttm_fw_reserve_vram_fini(adev
);
1947 if (adev
->mman
.aper_base_kaddr
)
1948 iounmap(adev
->mman
.aper_base_kaddr
);
1949 adev
->mman
.aper_base_kaddr
= NULL
;
1951 ttm_bo_clean_mm(&adev
->mman
.bdev
, TTM_PL_VRAM
);
1952 ttm_bo_clean_mm(&adev
->mman
.bdev
, TTM_PL_TT
);
1953 ttm_bo_clean_mm(&adev
->mman
.bdev
, AMDGPU_PL_GDS
);
1954 ttm_bo_clean_mm(&adev
->mman
.bdev
, AMDGPU_PL_GWS
);
1955 ttm_bo_clean_mm(&adev
->mman
.bdev
, AMDGPU_PL_OA
);
1956 ttm_bo_device_release(&adev
->mman
.bdev
);
1957 adev
->mman
.initialized
= false;
1958 DRM_INFO("amdgpu: ttm finalized\n");
1962 * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
1964 * @adev: amdgpu_device pointer
1965 * @enable: true when we can use buffer functions.
1967 * Enable/disable use of buffer functions during suspend/resume. This should
1968 * only be called at bootup or when userspace isn't running.
1970 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device
*adev
, bool enable
)
1972 struct ttm_mem_type_manager
*man
= &adev
->mman
.bdev
.man
[TTM_PL_VRAM
];
1976 if (!adev
->mman
.initialized
|| adev
->in_gpu_reset
||
1977 adev
->mman
.buffer_funcs_enabled
== enable
)
1981 struct amdgpu_ring
*ring
;
1982 struct drm_gpu_scheduler
*sched
;
1984 ring
= adev
->mman
.buffer_funcs_ring
;
1985 sched
= &ring
->sched
;
1986 r
= drm_sched_entity_init(&adev
->mman
.entity
,
1987 DRM_SCHED_PRIORITY_KERNEL
, &sched
,
1990 DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
1995 drm_sched_entity_destroy(&adev
->mman
.entity
);
1996 dma_fence_put(man
->move
);
2000 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
2002 size
= adev
->gmc
.real_vram_size
;
2004 size
= adev
->gmc
.visible_vram_size
;
2005 man
->size
= size
>> PAGE_SHIFT
;
2006 adev
->mman
.buffer_funcs_enabled
= enable
;
2009 int amdgpu_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
2011 struct drm_file
*file_priv
= filp
->private_data
;
2012 struct amdgpu_device
*adev
= file_priv
->minor
->dev
->dev_private
;
2017 return ttm_bo_mmap(filp
, vma
, &adev
->mman
.bdev
);
2020 static int amdgpu_map_buffer(struct ttm_buffer_object
*bo
,
2021 struct ttm_mem_reg
*mem
, unsigned num_pages
,
2022 uint64_t offset
, unsigned window
,
2023 struct amdgpu_ring
*ring
,
2026 struct amdgpu_ttm_tt
*gtt
= (void *)bo
->ttm
;
2027 struct amdgpu_device
*adev
= ring
->adev
;
2028 struct ttm_tt
*ttm
= bo
->ttm
;
2029 struct amdgpu_job
*job
;
2030 unsigned num_dw
, num_bytes
;
2031 dma_addr_t
*dma_address
;
2032 struct dma_fence
*fence
;
2033 uint64_t src_addr
, dst_addr
;
2037 BUG_ON(adev
->mman
.buffer_funcs
->copy_max_bytes
<
2038 AMDGPU_GTT_MAX_TRANSFER_SIZE
* 8);
2040 *addr
= adev
->gmc
.gart_start
;
2041 *addr
+= (u64
)window
* AMDGPU_GTT_MAX_TRANSFER_SIZE
*
2042 AMDGPU_GPU_PAGE_SIZE
;
2044 num_dw
= ALIGN(adev
->mman
.buffer_funcs
->copy_num_dw
, 8);
2045 num_bytes
= num_pages
* 8;
2047 r
= amdgpu_job_alloc_with_ib(adev
, num_dw
* 4 + num_bytes
, &job
);
2051 src_addr
= num_dw
* 4;
2052 src_addr
+= job
->ibs
[0].gpu_addr
;
2054 dst_addr
= amdgpu_bo_gpu_offset(adev
->gart
.bo
);
2055 dst_addr
+= window
* AMDGPU_GTT_MAX_TRANSFER_SIZE
* 8;
2056 amdgpu_emit_copy_buffer(adev
, &job
->ibs
[0], src_addr
,
2057 dst_addr
, num_bytes
);
2059 amdgpu_ring_pad_ib(ring
, &job
->ibs
[0]);
2060 WARN_ON(job
->ibs
[0].length_dw
> num_dw
);
2062 dma_address
= >t
->ttm
.dma_address
[offset
>> PAGE_SHIFT
];
2063 flags
= amdgpu_ttm_tt_pte_flags(adev
, ttm
, mem
);
2064 r
= amdgpu_gart_map(adev
, 0, num_pages
, dma_address
, flags
,
2065 &job
->ibs
[0].ptr
[num_dw
]);
2069 r
= amdgpu_job_submit(job
, &adev
->mman
.entity
,
2070 AMDGPU_FENCE_OWNER_UNDEFINED
, &fence
);
2074 dma_fence_put(fence
);
2079 amdgpu_job_free(job
);
2083 int amdgpu_copy_buffer(struct amdgpu_ring
*ring
, uint64_t src_offset
,
2084 uint64_t dst_offset
, uint32_t byte_count
,
2085 struct dma_resv
*resv
,
2086 struct dma_fence
**fence
, bool direct_submit
,
2087 bool vm_needs_flush
)
2089 struct amdgpu_device
*adev
= ring
->adev
;
2090 struct amdgpu_job
*job
;
2093 unsigned num_loops
, num_dw
;
2097 if (direct_submit
&& !ring
->sched
.ready
) {
2098 DRM_ERROR("Trying to move memory with ring turned off.\n");
2102 max_bytes
= adev
->mman
.buffer_funcs
->copy_max_bytes
;
2103 num_loops
= DIV_ROUND_UP(byte_count
, max_bytes
);
2104 num_dw
= ALIGN(num_loops
* adev
->mman
.buffer_funcs
->copy_num_dw
, 8);
2106 r
= amdgpu_job_alloc_with_ib(adev
, num_dw
* 4, &job
);
2110 if (vm_needs_flush
) {
2111 job
->vm_pd_addr
= amdgpu_gmc_pd_addr(adev
->gart
.bo
);
2112 job
->vm_needs_flush
= true;
2115 r
= amdgpu_sync_resv(adev
, &job
->sync
, resv
,
2116 AMDGPU_FENCE_OWNER_UNDEFINED
,
2119 DRM_ERROR("sync failed (%d).\n", r
);
2124 for (i
= 0; i
< num_loops
; i
++) {
2125 uint32_t cur_size_in_bytes
= min(byte_count
, max_bytes
);
2127 amdgpu_emit_copy_buffer(adev
, &job
->ibs
[0], src_offset
,
2128 dst_offset
, cur_size_in_bytes
);
2130 src_offset
+= cur_size_in_bytes
;
2131 dst_offset
+= cur_size_in_bytes
;
2132 byte_count
-= cur_size_in_bytes
;
2135 amdgpu_ring_pad_ib(ring
, &job
->ibs
[0]);
2136 WARN_ON(job
->ibs
[0].length_dw
> num_dw
);
2138 r
= amdgpu_job_submit_direct(job
, ring
, fence
);
2140 r
= amdgpu_job_submit(job
, &adev
->mman
.entity
,
2141 AMDGPU_FENCE_OWNER_UNDEFINED
, fence
);
2148 amdgpu_job_free(job
);
2149 DRM_ERROR("Error scheduling IBs (%d)\n", r
);
2153 int amdgpu_fill_buffer(struct amdgpu_bo
*bo
,
2155 struct dma_resv
*resv
,
2156 struct dma_fence
**fence
)
2158 struct amdgpu_device
*adev
= amdgpu_ttm_adev(bo
->tbo
.bdev
);
2159 uint32_t max_bytes
= adev
->mman
.buffer_funcs
->fill_max_bytes
;
2160 struct amdgpu_ring
*ring
= adev
->mman
.buffer_funcs_ring
;
2162 struct drm_mm_node
*mm_node
;
2163 unsigned long num_pages
;
2164 unsigned int num_loops
, num_dw
;
2166 struct amdgpu_job
*job
;
2169 if (!adev
->mman
.buffer_funcs_enabled
) {
2170 DRM_ERROR("Trying to clear memory with ring turned off.\n");
2174 if (bo
->tbo
.mem
.mem_type
== TTM_PL_TT
) {
2175 r
= amdgpu_ttm_alloc_gart(&bo
->tbo
);
2180 num_pages
= bo
->tbo
.num_pages
;
2181 mm_node
= bo
->tbo
.mem
.mm_node
;
2184 uint64_t byte_count
= mm_node
->size
<< PAGE_SHIFT
;
2186 num_loops
+= DIV_ROUND_UP_ULL(byte_count
, max_bytes
);
2187 num_pages
-= mm_node
->size
;
2190 num_dw
= num_loops
* adev
->mman
.buffer_funcs
->fill_num_dw
;
2192 /* for IB padding */
2195 r
= amdgpu_job_alloc_with_ib(adev
, num_dw
* 4, &job
);
2200 r
= amdgpu_sync_resv(adev
, &job
->sync
, resv
,
2201 AMDGPU_FENCE_OWNER_UNDEFINED
, false);
2203 DRM_ERROR("sync failed (%d).\n", r
);
2208 num_pages
= bo
->tbo
.num_pages
;
2209 mm_node
= bo
->tbo
.mem
.mm_node
;
2212 uint64_t byte_count
= mm_node
->size
<< PAGE_SHIFT
;
2215 dst_addr
= amdgpu_mm_node_addr(&bo
->tbo
, mm_node
, &bo
->tbo
.mem
);
2216 while (byte_count
) {
2217 uint32_t cur_size_in_bytes
= min_t(uint64_t, byte_count
,
2220 amdgpu_emit_fill_buffer(adev
, &job
->ibs
[0], src_data
,
2221 dst_addr
, cur_size_in_bytes
);
2223 dst_addr
+= cur_size_in_bytes
;
2224 byte_count
-= cur_size_in_bytes
;
2227 num_pages
-= mm_node
->size
;
2231 amdgpu_ring_pad_ib(ring
, &job
->ibs
[0]);
2232 WARN_ON(job
->ibs
[0].length_dw
> num_dw
);
2233 r
= amdgpu_job_submit(job
, &adev
->mman
.entity
,
2234 AMDGPU_FENCE_OWNER_UNDEFINED
, fence
);
2241 amdgpu_job_free(job
);
2245 #if defined(CONFIG_DEBUG_FS)
2247 static int amdgpu_mm_dump_table(struct seq_file
*m
, void *data
)
2249 struct drm_info_node
*node
= (struct drm_info_node
*)m
->private;
2250 unsigned ttm_pl
= (uintptr_t)node
->info_ent
->data
;
2251 struct drm_device
*dev
= node
->minor
->dev
;
2252 struct amdgpu_device
*adev
= dev
->dev_private
;
2253 struct ttm_mem_type_manager
*man
= &adev
->mman
.bdev
.man
[ttm_pl
];
2254 struct drm_printer p
= drm_seq_file_printer(m
);
2256 man
->func
->debug(man
, &p
);
2260 static const struct drm_info_list amdgpu_ttm_debugfs_list
[] = {
2261 {"amdgpu_vram_mm", amdgpu_mm_dump_table
, 0, (void *)TTM_PL_VRAM
},
2262 {"amdgpu_gtt_mm", amdgpu_mm_dump_table
, 0, (void *)TTM_PL_TT
},
2263 {"amdgpu_gds_mm", amdgpu_mm_dump_table
, 0, (void *)AMDGPU_PL_GDS
},
2264 {"amdgpu_gws_mm", amdgpu_mm_dump_table
, 0, (void *)AMDGPU_PL_GWS
},
2265 {"amdgpu_oa_mm", amdgpu_mm_dump_table
, 0, (void *)AMDGPU_PL_OA
},
2266 {"ttm_page_pool", ttm_page_alloc_debugfs
, 0, NULL
},
2267 #ifdef CONFIG_SWIOTLB
2268 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs
, 0, NULL
}
2273 * amdgpu_ttm_vram_read - Linear read access to VRAM
2275 * Accesses VRAM via MMIO for debugging purposes.
2277 static ssize_t
amdgpu_ttm_vram_read(struct file
*f
, char __user
*buf
,
2278 size_t size
, loff_t
*pos
)
2280 struct amdgpu_device
*adev
= file_inode(f
)->i_private
;
2284 if (size
& 0x3 || *pos
& 0x3)
2287 if (*pos
>= adev
->gmc
.mc_vram_size
)
2291 unsigned long flags
;
2294 if (*pos
>= adev
->gmc
.mc_vram_size
)
2297 spin_lock_irqsave(&adev
->mmio_idx_lock
, flags
);
2298 WREG32_NO_KIQ(mmMM_INDEX
, ((uint32_t)*pos
) | 0x80000000);
2299 WREG32_NO_KIQ(mmMM_INDEX_HI
, *pos
>> 31);
2300 value
= RREG32_NO_KIQ(mmMM_DATA
);
2301 spin_unlock_irqrestore(&adev
->mmio_idx_lock
, flags
);
2303 r
= put_user(value
, (uint32_t *)buf
);
2317 * amdgpu_ttm_vram_write - Linear write access to VRAM
2319 * Accesses VRAM via MMIO for debugging purposes.
2321 static ssize_t
amdgpu_ttm_vram_write(struct file
*f
, const char __user
*buf
,
2322 size_t size
, loff_t
*pos
)
2324 struct amdgpu_device
*adev
= file_inode(f
)->i_private
;
2328 if (size
& 0x3 || *pos
& 0x3)
2331 if (*pos
>= adev
->gmc
.mc_vram_size
)
2335 unsigned long flags
;
2338 if (*pos
>= adev
->gmc
.mc_vram_size
)
2341 r
= get_user(value
, (uint32_t *)buf
);
2345 spin_lock_irqsave(&adev
->mmio_idx_lock
, flags
);
2346 WREG32_NO_KIQ(mmMM_INDEX
, ((uint32_t)*pos
) | 0x80000000);
2347 WREG32_NO_KIQ(mmMM_INDEX_HI
, *pos
>> 31);
2348 WREG32_NO_KIQ(mmMM_DATA
, value
);
2349 spin_unlock_irqrestore(&adev
->mmio_idx_lock
, flags
);
2360 static const struct file_operations amdgpu_ttm_vram_fops
= {
2361 .owner
= THIS_MODULE
,
2362 .read
= amdgpu_ttm_vram_read
,
2363 .write
= amdgpu_ttm_vram_write
,
2364 .llseek
= default_llseek
,
2367 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2370 * amdgpu_ttm_gtt_read - Linear read access to GTT memory
2372 static ssize_t
amdgpu_ttm_gtt_read(struct file
*f
, char __user
*buf
,
2373 size_t size
, loff_t
*pos
)
2375 struct amdgpu_device
*adev
= file_inode(f
)->i_private
;
2380 loff_t p
= *pos
/ PAGE_SIZE
;
2381 unsigned off
= *pos
& ~PAGE_MASK
;
2382 size_t cur_size
= min_t(size_t, size
, PAGE_SIZE
- off
);
2386 if (p
>= adev
->gart
.num_cpu_pages
)
2389 page
= adev
->gart
.pages
[p
];
2394 r
= copy_to_user(buf
, ptr
, cur_size
);
2395 kunmap(adev
->gart
.pages
[p
]);
2397 r
= clear_user(buf
, cur_size
);
2411 static const struct file_operations amdgpu_ttm_gtt_fops
= {
2412 .owner
= THIS_MODULE
,
2413 .read
= amdgpu_ttm_gtt_read
,
2414 .llseek
= default_llseek
2420 * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2422 * This function is used to read memory that has been mapped to the
2423 * GPU and the known addresses are not physical addresses but instead
2424 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2426 static ssize_t
amdgpu_iomem_read(struct file
*f
, char __user
*buf
,
2427 size_t size
, loff_t
*pos
)
2429 struct amdgpu_device
*adev
= file_inode(f
)->i_private
;
2430 struct iommu_domain
*dom
;
2434 /* retrieve the IOMMU domain if any for this device */
2435 dom
= iommu_get_domain_for_dev(adev
->dev
);
2438 phys_addr_t addr
= *pos
& PAGE_MASK
;
2439 loff_t off
= *pos
& ~PAGE_MASK
;
2440 size_t bytes
= PAGE_SIZE
- off
;
2445 bytes
= bytes
< size
? bytes
: size
;
2447 /* Translate the bus address to a physical address. If
2448 * the domain is NULL it means there is no IOMMU active
2449 * and the address translation is the identity
2451 addr
= dom
? iommu_iova_to_phys(dom
, addr
) : addr
;
2453 pfn
= addr
>> PAGE_SHIFT
;
2454 if (!pfn_valid(pfn
))
2457 p
= pfn_to_page(pfn
);
2458 if (p
->mapping
!= adev
->mman
.bdev
.dev_mapping
)
2462 r
= copy_to_user(buf
, ptr
+ off
, bytes
);
2476 * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2478 * This function is used to write memory that has been mapped to the
2479 * GPU and the known addresses are not physical addresses but instead
2480 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2482 static ssize_t
amdgpu_iomem_write(struct file
*f
, const char __user
*buf
,
2483 size_t size
, loff_t
*pos
)
2485 struct amdgpu_device
*adev
= file_inode(f
)->i_private
;
2486 struct iommu_domain
*dom
;
2490 dom
= iommu_get_domain_for_dev(adev
->dev
);
2493 phys_addr_t addr
= *pos
& PAGE_MASK
;
2494 loff_t off
= *pos
& ~PAGE_MASK
;
2495 size_t bytes
= PAGE_SIZE
- off
;
2500 bytes
= bytes
< size
? bytes
: size
;
2502 addr
= dom
? iommu_iova_to_phys(dom
, addr
) : addr
;
2504 pfn
= addr
>> PAGE_SHIFT
;
2505 if (!pfn_valid(pfn
))
2508 p
= pfn_to_page(pfn
);
2509 if (p
->mapping
!= adev
->mman
.bdev
.dev_mapping
)
2513 r
= copy_from_user(ptr
+ off
, buf
, bytes
);
2526 static const struct file_operations amdgpu_ttm_iomem_fops
= {
2527 .owner
= THIS_MODULE
,
2528 .read
= amdgpu_iomem_read
,
2529 .write
= amdgpu_iomem_write
,
2530 .llseek
= default_llseek
2533 static const struct {
2535 const struct file_operations
*fops
;
2537 } ttm_debugfs_entries
[] = {
2538 { "amdgpu_vram", &amdgpu_ttm_vram_fops
, TTM_PL_VRAM
},
2539 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2540 { "amdgpu_gtt", &amdgpu_ttm_gtt_fops
, TTM_PL_TT
},
2542 { "amdgpu_iomem", &amdgpu_ttm_iomem_fops
, TTM_PL_SYSTEM
},
2547 static int amdgpu_ttm_debugfs_init(struct amdgpu_device
*adev
)
2549 #if defined(CONFIG_DEBUG_FS)
2552 struct drm_minor
*minor
= adev
->ddev
->primary
;
2553 struct dentry
*ent
, *root
= minor
->debugfs_root
;
2555 for (count
= 0; count
< ARRAY_SIZE(ttm_debugfs_entries
); count
++) {
2556 ent
= debugfs_create_file(
2557 ttm_debugfs_entries
[count
].name
,
2558 S_IFREG
| S_IRUGO
, root
,
2560 ttm_debugfs_entries
[count
].fops
);
2562 return PTR_ERR(ent
);
2563 if (ttm_debugfs_entries
[count
].domain
== TTM_PL_VRAM
)
2564 i_size_write(ent
->d_inode
, adev
->gmc
.mc_vram_size
);
2565 else if (ttm_debugfs_entries
[count
].domain
== TTM_PL_TT
)
2566 i_size_write(ent
->d_inode
, adev
->gmc
.gart_size
);
2567 adev
->mman
.debugfs_entries
[count
] = ent
;
2570 count
= ARRAY_SIZE(amdgpu_ttm_debugfs_list
);
2572 #ifdef CONFIG_SWIOTLB
2573 if (!(adev
->need_swiotlb
&& swiotlb_nr_tbl()))
2577 return amdgpu_debugfs_add_files(adev
, amdgpu_ttm_debugfs_list
, count
);
2583 static void amdgpu_ttm_debugfs_fini(struct amdgpu_device
*adev
)
2585 #if defined(CONFIG_DEBUG_FS)
2588 for (i
= 0; i
< ARRAY_SIZE(ttm_debugfs_entries
); i
++)
2589 debugfs_remove(adev
->mman
.debugfs_entries
[i
]);