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 #define AMDGPU_TTM_VRAM_MAX_DW_READ (size_t)128
65 static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
69 struct ttm_mem_type_manager *man = &adev->mman.bdev.man[type];
71 man->available_caching = TTM_PL_FLAG_UNCACHED;
72 man->default_caching = TTM_PL_FLAG_UNCACHED;
74 return ttm_range_man_init(&adev->mman.bdev, man, size >> PAGE_SHIFT);
78 * amdgpu_evict_flags - Compute placement flags
80 * @bo: The buffer object to evict
81 * @placement: Possible destination(s) for evicted BO
83 * Fill in placement data when ttm_bo_evict() is called
85 static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
86 struct ttm_placement *placement)
88 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
89 struct amdgpu_bo *abo;
90 static const struct ttm_place placements = {
93 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
96 /* Don't handle scatter gather BOs */
97 if (bo->type == ttm_bo_type_sg) {
98 placement->num_placement = 0;
99 placement->num_busy_placement = 0;
103 /* Object isn't an AMDGPU object so ignore */
104 if (!amdgpu_bo_is_amdgpu_bo(bo)) {
105 placement->placement = &placements;
106 placement->busy_placement = &placements;
107 placement->num_placement = 1;
108 placement->num_busy_placement = 1;
112 abo = ttm_to_amdgpu_bo(bo);
113 switch (bo->mem.mem_type) {
117 placement->num_placement = 0;
118 placement->num_busy_placement = 0;
122 if (!adev->mman.buffer_funcs_enabled) {
123 /* Move to system memory */
124 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
125 } else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
126 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
127 amdgpu_bo_in_cpu_visible_vram(abo)) {
129 /* Try evicting to the CPU inaccessible part of VRAM
130 * first, but only set GTT as busy placement, so this
131 * BO will be evicted to GTT rather than causing other
132 * BOs to be evicted from VRAM
134 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
135 AMDGPU_GEM_DOMAIN_GTT);
136 abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
137 abo->placements[0].lpfn = 0;
138 abo->placement.busy_placement = &abo->placements[1];
139 abo->placement.num_busy_placement = 1;
141 /* Move to GTT memory */
142 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
147 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
150 *placement = abo->placement;
154 * amdgpu_verify_access - Verify access for a mmap call
156 * @bo: The buffer object to map
157 * @filp: The file pointer from the process performing the mmap
159 * This is called by ttm_bo_mmap() to verify whether a process
160 * has the right to mmap a BO to their process space.
162 static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
164 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
167 * Don't verify access for KFD BOs. They don't have a GEM
168 * object associated with them.
173 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
175 return drm_vma_node_verify_access(&abo->tbo.base.vma_node,
180 * amdgpu_move_null - Register memory for a buffer object
182 * @bo: The bo to assign the memory to
183 * @new_mem: The memory to be assigned.
185 * Assign the memory from new_mem to the memory of the buffer object bo.
187 static void amdgpu_move_null(struct ttm_buffer_object *bo,
188 struct ttm_mem_reg *new_mem)
190 struct ttm_mem_reg *old_mem = &bo->mem;
192 BUG_ON(old_mem->mm_node != NULL);
194 new_mem->mm_node = NULL;
198 * amdgpu_mm_node_addr - Compute the GPU relative offset of a GTT buffer.
200 * @bo: The bo to assign the memory to.
201 * @mm_node: Memory manager node for drm allocator.
202 * @mem: The region where the bo resides.
205 static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
206 struct drm_mm_node *mm_node,
207 struct ttm_mem_reg *mem)
211 if (mm_node->start != AMDGPU_BO_INVALID_OFFSET) {
212 addr = mm_node->start << PAGE_SHIFT;
213 addr += amdgpu_ttm_domain_start(amdgpu_ttm_adev(bo->bdev),
220 * amdgpu_find_mm_node - Helper function finds the drm_mm_node corresponding to
221 * @offset. It also modifies the offset to be within the drm_mm_node returned
223 * @mem: The region where the bo resides.
224 * @offset: The offset that drm_mm_node is used for finding.
227 static struct drm_mm_node *amdgpu_find_mm_node(struct ttm_mem_reg *mem,
230 struct drm_mm_node *mm_node = mem->mm_node;
232 while (*offset >= (mm_node->size << PAGE_SHIFT)) {
233 *offset -= (mm_node->size << PAGE_SHIFT);
240 * amdgpu_ttm_map_buffer - Map memory into the GART windows
241 * @bo: buffer object to map
242 * @mem: memory object to map
243 * @mm_node: drm_mm node object to map
244 * @num_pages: number of pages to map
245 * @offset: offset into @mm_node where to start
246 * @window: which GART window to use
247 * @ring: DMA ring to use for the copy
248 * @tmz: if we should setup a TMZ enabled mapping
249 * @addr: resulting address inside the MC address space
251 * Setup one of the GART windows to access a specific piece of memory or return
252 * the physical address for local memory.
254 static int amdgpu_ttm_map_buffer(struct ttm_buffer_object *bo,
255 struct ttm_mem_reg *mem,
256 struct drm_mm_node *mm_node,
257 unsigned num_pages, uint64_t offset,
258 unsigned window, struct amdgpu_ring *ring,
259 bool tmz, uint64_t *addr)
261 struct amdgpu_device *adev = ring->adev;
262 struct amdgpu_job *job;
263 unsigned num_dw, num_bytes;
264 struct dma_fence *fence;
265 uint64_t src_addr, dst_addr;
271 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
272 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
274 /* Map only what can't be accessed directly */
275 if (!tmz && mem->start != AMDGPU_BO_INVALID_OFFSET) {
276 *addr = amdgpu_mm_node_addr(bo, mm_node, mem) + offset;
280 *addr = adev->gmc.gart_start;
281 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
282 AMDGPU_GPU_PAGE_SIZE;
283 *addr += offset & ~PAGE_MASK;
285 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
286 num_bytes = num_pages * 8;
288 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes,
289 AMDGPU_IB_POOL_DELAYED, &job);
293 src_addr = num_dw * 4;
294 src_addr += job->ibs[0].gpu_addr;
296 dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
297 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
298 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
299 dst_addr, num_bytes, false);
301 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
302 WARN_ON(job->ibs[0].length_dw > num_dw);
304 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, mem);
306 flags |= AMDGPU_PTE_TMZ;
308 cpu_addr = &job->ibs[0].ptr[num_dw];
310 if (mem->mem_type == TTM_PL_TT) {
311 struct ttm_dma_tt *dma;
312 dma_addr_t *dma_address;
314 dma = container_of(bo->ttm, struct ttm_dma_tt, ttm);
315 dma_address = &dma->dma_address[offset >> PAGE_SHIFT];
316 r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
321 dma_addr_t dma_address;
323 dma_address = (mm_node->start << PAGE_SHIFT) + offset;
324 dma_address += adev->vm_manager.vram_base_offset;
326 for (i = 0; i < num_pages; ++i) {
327 r = amdgpu_gart_map(adev, i << PAGE_SHIFT, 1,
328 &dma_address, flags, cpu_addr);
332 dma_address += PAGE_SIZE;
336 r = amdgpu_job_submit(job, &adev->mman.entity,
337 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
341 dma_fence_put(fence);
346 amdgpu_job_free(job);
351 * amdgpu_copy_ttm_mem_to_mem - Helper function for copy
352 * @adev: amdgpu device
353 * @src: buffer/address where to read from
354 * @dst: buffer/address where to write to
355 * @size: number of bytes to copy
356 * @tmz: if a secure copy should be used
357 * @resv: resv object to sync to
358 * @f: Returns the last fence if multiple jobs are submitted.
360 * The function copies @size bytes from {src->mem + src->offset} to
361 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
362 * move and different for a BO to BO copy.
365 int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
366 const struct amdgpu_copy_mem *src,
367 const struct amdgpu_copy_mem *dst,
368 uint64_t size, bool tmz,
369 struct dma_resv *resv,
370 struct dma_fence **f)
372 const uint32_t GTT_MAX_BYTES = (AMDGPU_GTT_MAX_TRANSFER_SIZE *
373 AMDGPU_GPU_PAGE_SIZE);
375 uint64_t src_node_size, dst_node_size, src_offset, dst_offset;
376 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
377 struct drm_mm_node *src_mm, *dst_mm;
378 struct dma_fence *fence = NULL;
381 if (!adev->mman.buffer_funcs_enabled) {
382 DRM_ERROR("Trying to move memory with ring turned off.\n");
386 src_offset = src->offset;
387 if (src->mem->mm_node) {
388 src_mm = amdgpu_find_mm_node(src->mem, &src_offset);
389 src_node_size = (src_mm->size << PAGE_SHIFT) - src_offset;
392 src_node_size = ULLONG_MAX;
395 dst_offset = dst->offset;
396 if (dst->mem->mm_node) {
397 dst_mm = amdgpu_find_mm_node(dst->mem, &dst_offset);
398 dst_node_size = (dst_mm->size << PAGE_SHIFT) - dst_offset;
401 dst_node_size = ULLONG_MAX;
404 mutex_lock(&adev->mman.gtt_window_lock);
407 uint32_t src_page_offset = src_offset & ~PAGE_MASK;
408 uint32_t dst_page_offset = dst_offset & ~PAGE_MASK;
409 struct dma_fence *next;
413 /* Copy size cannot exceed GTT_MAX_BYTES. So if src or dst
414 * begins at an offset, then adjust the size accordingly
416 cur_size = max(src_page_offset, dst_page_offset);
417 cur_size = min(min3(src_node_size, dst_node_size, size),
418 (uint64_t)(GTT_MAX_BYTES - cur_size));
420 /* Map src to window 0 and dst to window 1. */
421 r = amdgpu_ttm_map_buffer(src->bo, src->mem, src_mm,
422 PFN_UP(cur_size + src_page_offset),
423 src_offset, 0, ring, tmz, &from);
427 r = amdgpu_ttm_map_buffer(dst->bo, dst->mem, dst_mm,
428 PFN_UP(cur_size + dst_page_offset),
429 dst_offset, 1, ring, tmz, &to);
433 r = amdgpu_copy_buffer(ring, from, to, cur_size,
434 resv, &next, false, true, tmz);
438 dma_fence_put(fence);
445 src_node_size -= cur_size;
446 if (!src_node_size) {
448 src_node_size = src_mm->size << PAGE_SHIFT;
451 src_offset += cur_size;
454 dst_node_size -= cur_size;
455 if (!dst_node_size) {
457 dst_node_size = dst_mm->size << PAGE_SHIFT;
460 dst_offset += cur_size;
464 mutex_unlock(&adev->mman.gtt_window_lock);
466 *f = dma_fence_get(fence);
467 dma_fence_put(fence);
472 * amdgpu_move_blit - Copy an entire buffer to another buffer
474 * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
475 * help move buffers to and from VRAM.
477 static int amdgpu_move_blit(struct ttm_buffer_object *bo,
478 bool evict, bool no_wait_gpu,
479 struct ttm_mem_reg *new_mem,
480 struct ttm_mem_reg *old_mem)
482 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
483 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
484 struct amdgpu_copy_mem src, dst;
485 struct dma_fence *fence = NULL;
495 r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
496 new_mem->num_pages << PAGE_SHIFT,
497 amdgpu_bo_encrypted(abo),
498 bo->base.resv, &fence);
502 /* clear the space being freed */
503 if (old_mem->mem_type == TTM_PL_VRAM &&
504 (abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)) {
505 struct dma_fence *wipe_fence = NULL;
507 r = amdgpu_fill_buffer(ttm_to_amdgpu_bo(bo), AMDGPU_POISON,
511 } else if (wipe_fence) {
512 dma_fence_put(fence);
517 /* Always block for VM page tables before committing the new location */
518 if (bo->type == ttm_bo_type_kernel)
519 r = ttm_bo_move_accel_cleanup(bo, fence, true, new_mem);
521 r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
522 dma_fence_put(fence);
527 dma_fence_wait(fence, false);
528 dma_fence_put(fence);
533 * amdgpu_move_vram_ram - Copy VRAM buffer to RAM buffer
535 * Called by amdgpu_bo_move().
537 static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo, bool evict,
538 struct ttm_operation_ctx *ctx,
539 struct ttm_mem_reg *new_mem)
541 struct ttm_mem_reg *old_mem = &bo->mem;
542 struct ttm_mem_reg tmp_mem;
543 struct ttm_place placements;
544 struct ttm_placement placement;
547 /* create space/pages for new_mem in GTT space */
549 tmp_mem.mm_node = NULL;
550 placement.num_placement = 1;
551 placement.placement = &placements;
552 placement.num_busy_placement = 1;
553 placement.busy_placement = &placements;
556 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
557 r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
559 pr_err("Failed to find GTT space for blit from VRAM\n");
563 /* set caching flags */
564 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
569 /* Bind the memory to the GTT space */
570 r = ttm_tt_bind(bo->ttm, &tmp_mem, ctx);
575 /* blit VRAM to GTT */
576 r = amdgpu_move_blit(bo, evict, ctx->no_wait_gpu, &tmp_mem, old_mem);
581 /* move BO (in tmp_mem) to new_mem */
582 r = ttm_bo_move_ttm(bo, ctx, new_mem);
584 ttm_bo_mem_put(bo, &tmp_mem);
589 * amdgpu_move_ram_vram - Copy buffer from RAM to VRAM
591 * Called by amdgpu_bo_move().
593 static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo, bool evict,
594 struct ttm_operation_ctx *ctx,
595 struct ttm_mem_reg *new_mem)
597 struct ttm_mem_reg *old_mem = &bo->mem;
598 struct ttm_mem_reg tmp_mem;
599 struct ttm_placement placement;
600 struct ttm_place placements;
603 /* make space in GTT for old_mem buffer */
605 tmp_mem.mm_node = NULL;
606 placement.num_placement = 1;
607 placement.placement = &placements;
608 placement.num_busy_placement = 1;
609 placement.busy_placement = &placements;
612 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
613 r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
615 pr_err("Failed to find GTT space for blit to VRAM\n");
619 /* move/bind old memory to GTT space */
620 r = ttm_bo_move_ttm(bo, ctx, &tmp_mem);
626 r = amdgpu_move_blit(bo, evict, ctx->no_wait_gpu, new_mem, old_mem);
631 ttm_bo_mem_put(bo, &tmp_mem);
636 * amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
638 * Called by amdgpu_bo_move()
640 static bool amdgpu_mem_visible(struct amdgpu_device *adev,
641 struct ttm_mem_reg *mem)
643 struct drm_mm_node *nodes = mem->mm_node;
645 if (mem->mem_type == TTM_PL_SYSTEM ||
646 mem->mem_type == TTM_PL_TT)
648 if (mem->mem_type != TTM_PL_VRAM)
651 /* ttm_mem_reg_ioremap only supports contiguous memory */
652 if (nodes->size != mem->num_pages)
655 return ((nodes->start + nodes->size) << PAGE_SHIFT)
656 <= adev->gmc.visible_vram_size;
660 * amdgpu_bo_move - Move a buffer object to a new memory location
662 * Called by ttm_bo_handle_move_mem()
664 static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
665 struct ttm_operation_ctx *ctx,
666 struct ttm_mem_reg *new_mem)
668 struct amdgpu_device *adev;
669 struct amdgpu_bo *abo;
670 struct ttm_mem_reg *old_mem = &bo->mem;
673 /* Can't move a pinned BO */
674 abo = ttm_to_amdgpu_bo(bo);
675 if (WARN_ON_ONCE(abo->pin_count > 0))
678 adev = amdgpu_ttm_adev(bo->bdev);
680 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
681 amdgpu_move_null(bo, new_mem);
684 if ((old_mem->mem_type == TTM_PL_TT &&
685 new_mem->mem_type == TTM_PL_SYSTEM) ||
686 (old_mem->mem_type == TTM_PL_SYSTEM &&
687 new_mem->mem_type == TTM_PL_TT)) {
689 amdgpu_move_null(bo, new_mem);
692 if (old_mem->mem_type == AMDGPU_PL_GDS ||
693 old_mem->mem_type == AMDGPU_PL_GWS ||
694 old_mem->mem_type == AMDGPU_PL_OA ||
695 new_mem->mem_type == AMDGPU_PL_GDS ||
696 new_mem->mem_type == AMDGPU_PL_GWS ||
697 new_mem->mem_type == AMDGPU_PL_OA) {
698 /* Nothing to save here */
699 amdgpu_move_null(bo, new_mem);
703 if (!adev->mman.buffer_funcs_enabled) {
708 if (old_mem->mem_type == TTM_PL_VRAM &&
709 new_mem->mem_type == TTM_PL_SYSTEM) {
710 r = amdgpu_move_vram_ram(bo, evict, ctx, new_mem);
711 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
712 new_mem->mem_type == TTM_PL_VRAM) {
713 r = amdgpu_move_ram_vram(bo, evict, ctx, new_mem);
715 r = amdgpu_move_blit(bo, evict, ctx->no_wait_gpu,
721 /* Check that all memory is CPU accessible */
722 if (!amdgpu_mem_visible(adev, old_mem) ||
723 !amdgpu_mem_visible(adev, new_mem)) {
724 pr_err("Move buffer fallback to memcpy unavailable\n");
728 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
733 if (bo->type == ttm_bo_type_device &&
734 new_mem->mem_type == TTM_PL_VRAM &&
735 old_mem->mem_type != TTM_PL_VRAM) {
736 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
737 * accesses the BO after it's moved.
739 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
742 /* update statistics */
743 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
748 * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
750 * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
752 static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
754 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
755 struct drm_mm_node *mm_node = mem->mm_node;
757 mem->bus.addr = NULL;
759 mem->bus.size = mem->num_pages << PAGE_SHIFT;
761 mem->bus.is_iomem = false;
763 switch (mem->mem_type) {
770 mem->bus.offset = mem->start << PAGE_SHIFT;
771 /* check if it's visible */
772 if ((mem->bus.offset + mem->bus.size) > adev->gmc.visible_vram_size)
774 /* Only physically contiguous buffers apply. In a contiguous
775 * buffer, size of the first mm_node would match the number of
776 * pages in ttm_mem_reg.
778 if (adev->mman.aper_base_kaddr &&
779 (mm_node->size == mem->num_pages))
780 mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr +
783 mem->bus.base = adev->gmc.aper_base;
784 mem->bus.is_iomem = true;
792 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
793 unsigned long page_offset)
795 uint64_t offset = (page_offset << PAGE_SHIFT);
796 struct drm_mm_node *mm;
798 mm = amdgpu_find_mm_node(&bo->mem, &offset);
799 return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start +
800 (offset >> PAGE_SHIFT);
804 * amdgpu_ttm_domain_start - Returns GPU start address
805 * @adev: amdgpu device object
806 * @type: type of the memory
809 * GPU start address of a memory domain
812 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type)
816 return adev->gmc.gart_start;
818 return adev->gmc.vram_start;
825 * TTM backend functions.
827 struct amdgpu_ttm_tt {
828 struct ttm_dma_tt ttm;
829 struct drm_gem_object *gobj;
832 struct task_struct *usertask;
834 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
835 struct hmm_range *range;
839 #ifdef CONFIG_DRM_AMDGPU_USERPTR
841 * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user
842 * memory and start HMM tracking CPU page table update
844 * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only
845 * once afterwards to stop HMM tracking
847 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages)
849 struct ttm_tt *ttm = bo->tbo.ttm;
850 struct amdgpu_ttm_tt *gtt = (void *)ttm;
851 unsigned long start = gtt->userptr;
852 struct vm_area_struct *vma;
853 struct hmm_range *range;
854 unsigned long timeout;
855 struct mm_struct *mm;
859 mm = bo->notifier.mm;
861 DRM_DEBUG_DRIVER("BO is not registered?\n");
865 /* Another get_user_pages is running at the same time?? */
866 if (WARN_ON(gtt->range))
869 if (!mmget_not_zero(mm)) /* Happens during process shutdown */
872 range = kzalloc(sizeof(*range), GFP_KERNEL);
873 if (unlikely(!range)) {
877 range->notifier = &bo->notifier;
878 range->start = bo->notifier.interval_tree.start;
879 range->end = bo->notifier.interval_tree.last + 1;
880 range->default_flags = HMM_PFN_REQ_FAULT;
881 if (!amdgpu_ttm_tt_is_readonly(ttm))
882 range->default_flags |= HMM_PFN_REQ_WRITE;
884 range->hmm_pfns = kvmalloc_array(ttm->num_pages,
885 sizeof(*range->hmm_pfns), GFP_KERNEL);
886 if (unlikely(!range->hmm_pfns)) {
888 goto out_free_ranges;
892 vma = find_vma(mm, start);
893 if (unlikely(!vma || start < vma->vm_start)) {
897 if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) &&
902 mmap_read_unlock(mm);
903 timeout = jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
906 range->notifier_seq = mmu_interval_read_begin(&bo->notifier);
909 r = hmm_range_fault(range);
910 mmap_read_unlock(mm);
913 * FIXME: This timeout should encompass the retry from
914 * mmu_interval_read_retry() as well.
916 if (r == -EBUSY && !time_after(jiffies, timeout))
922 * Due to default_flags, all pages are HMM_PFN_VALID or
923 * hmm_range_fault() fails. FIXME: The pages cannot be touched outside
924 * the notifier_lock, and mmu_interval_read_retry() must be done first.
926 for (i = 0; i < ttm->num_pages; i++)
927 pages[i] = hmm_pfn_to_page(range->hmm_pfns[i]);
935 mmap_read_unlock(mm);
937 kvfree(range->hmm_pfns);
946 * amdgpu_ttm_tt_userptr_range_done - stop HMM track the CPU page table change
947 * Check if the pages backing this ttm range have been invalidated
949 * Returns: true if pages are still valid
951 bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm)
953 struct amdgpu_ttm_tt *gtt = (void *)ttm;
956 if (!gtt || !gtt->userptr)
959 DRM_DEBUG_DRIVER("user_pages_done 0x%llx pages 0x%lx\n",
960 gtt->userptr, ttm->num_pages);
962 WARN_ONCE(!gtt->range || !gtt->range->hmm_pfns,
963 "No user pages to check\n");
967 * FIXME: Must always hold notifier_lock for this, and must
968 * not ignore the return code.
970 r = mmu_interval_read_retry(gtt->range->notifier,
971 gtt->range->notifier_seq);
972 kvfree(gtt->range->hmm_pfns);
982 * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
984 * Called by amdgpu_cs_list_validate(). This creates the page list
985 * that backs user memory and will ultimately be mapped into the device
988 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
992 for (i = 0; i < ttm->num_pages; ++i)
993 ttm->pages[i] = pages ? pages[i] : NULL;
997 * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages
999 * Called by amdgpu_ttm_backend_bind()
1001 static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
1003 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
1004 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1007 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1008 enum dma_data_direction direction = write ?
1009 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
1011 /* Allocate an SG array and squash pages into it */
1012 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
1013 ttm->num_pages << PAGE_SHIFT,
1018 /* Map SG to device */
1019 r = dma_map_sgtable(adev->dev, ttm->sg, direction, 0);
1023 /* convert SG to linear array of pages and dma addresses */
1024 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1025 gtt->ttm.dma_address, ttm->num_pages);
1035 * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
1037 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
1039 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
1040 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1042 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1043 enum dma_data_direction direction = write ?
1044 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
1046 /* double check that we don't free the table twice */
1050 /* unmap the pages mapped to the device */
1051 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
1052 sg_free_table(ttm->sg);
1054 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
1058 for (i = 0; i < ttm->num_pages; i++) {
1059 if (ttm->pages[i] !=
1060 hmm_pfn_to_page(gtt->range->hmm_pfns[i]))
1064 WARN((i == ttm->num_pages), "Missing get_user_page_done\n");
1069 int amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
1070 struct ttm_buffer_object *tbo,
1073 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo);
1074 struct ttm_tt *ttm = tbo->ttm;
1075 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1078 if (amdgpu_bo_encrypted(abo))
1079 flags |= AMDGPU_PTE_TMZ;
1081 if (abo->flags & AMDGPU_GEM_CREATE_CP_MQD_GFX9) {
1082 uint64_t page_idx = 1;
1084 r = amdgpu_gart_bind(adev, gtt->offset, page_idx,
1085 ttm->pages, gtt->ttm.dma_address, flags);
1087 goto gart_bind_fail;
1089 /* The memory type of the first page defaults to UC. Now
1090 * modify the memory type to NC from the second page of
1093 flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
1094 flags |= AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC);
1096 r = amdgpu_gart_bind(adev,
1097 gtt->offset + (page_idx << PAGE_SHIFT),
1098 ttm->num_pages - page_idx,
1099 &ttm->pages[page_idx],
1100 &(gtt->ttm.dma_address[page_idx]), flags);
1102 r = amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
1103 ttm->pages, gtt->ttm.dma_address, flags);
1108 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
1109 ttm->num_pages, gtt->offset);
1115 * amdgpu_ttm_backend_bind - Bind GTT memory
1117 * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
1118 * This handles binding GTT memory to the device address space.
1120 static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
1121 struct ttm_mem_reg *bo_mem)
1123 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
1124 struct amdgpu_ttm_tt *gtt = (void*)ttm;
1129 r = amdgpu_ttm_tt_pin_userptr(ttm);
1131 DRM_ERROR("failed to pin userptr\n");
1135 if (!ttm->num_pages) {
1136 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
1137 ttm->num_pages, bo_mem, ttm);
1140 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
1141 bo_mem->mem_type == AMDGPU_PL_GWS ||
1142 bo_mem->mem_type == AMDGPU_PL_OA)
1145 if (!amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
1146 gtt->offset = AMDGPU_BO_INVALID_OFFSET;
1150 /* compute PTE flags relevant to this BO memory */
1151 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem);
1153 /* bind pages into GART page tables */
1154 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
1155 r = amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
1156 ttm->pages, gtt->ttm.dma_address, flags);
1159 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
1160 ttm->num_pages, gtt->offset);
1165 * amdgpu_ttm_alloc_gart - Allocate GART memory for buffer object
1167 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
1169 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1170 struct ttm_operation_ctx ctx = { false, false };
1171 struct amdgpu_ttm_tt *gtt = (void*)bo->ttm;
1172 struct ttm_mem_reg tmp;
1173 struct ttm_placement placement;
1174 struct ttm_place placements;
1175 uint64_t addr, flags;
1178 if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET)
1181 addr = amdgpu_gmc_agp_addr(bo);
1182 if (addr != AMDGPU_BO_INVALID_OFFSET) {
1183 bo->mem.start = addr >> PAGE_SHIFT;
1186 /* allocate GART space */
1189 placement.num_placement = 1;
1190 placement.placement = &placements;
1191 placement.num_busy_placement = 1;
1192 placement.busy_placement = &placements;
1193 placements.fpfn = 0;
1194 placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
1195 placements.flags = (bo->mem.placement & ~TTM_PL_MASK_MEM) |
1198 r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
1202 /* compute PTE flags for this buffer object */
1203 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, &tmp);
1206 gtt->offset = (u64)tmp.start << PAGE_SHIFT;
1207 r = amdgpu_ttm_gart_bind(adev, bo, flags);
1209 ttm_bo_mem_put(bo, &tmp);
1213 ttm_bo_mem_put(bo, &bo->mem);
1221 * amdgpu_ttm_recover_gart - Rebind GTT pages
1223 * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
1224 * rebind GTT pages during a GPU reset.
1226 int amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
1228 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
1235 flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, &tbo->mem);
1236 r = amdgpu_ttm_gart_bind(adev, tbo, flags);
1242 * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
1244 * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
1247 static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
1249 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
1250 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1253 /* if the pages have userptr pinning then clear that first */
1255 amdgpu_ttm_tt_unpin_userptr(ttm);
1257 if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
1260 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1261 r = amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages);
1263 DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
1264 gtt->ttm.ttm.num_pages, gtt->offset);
1268 static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
1270 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1273 put_task_struct(gtt->usertask);
1275 ttm_dma_tt_fini(>t->ttm);
1279 static struct ttm_backend_func amdgpu_backend_func = {
1280 .bind = &amdgpu_ttm_backend_bind,
1281 .unbind = &amdgpu_ttm_backend_unbind,
1282 .destroy = &amdgpu_ttm_backend_destroy,
1286 * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1288 * @bo: The buffer object to create a GTT ttm_tt object around
1290 * Called by ttm_tt_create().
1292 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
1293 uint32_t page_flags)
1295 struct amdgpu_ttm_tt *gtt;
1297 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
1301 gtt->ttm.ttm.func = &amdgpu_backend_func;
1302 gtt->gobj = &bo->base;
1304 /* allocate space for the uninitialized page entries */
1305 if (ttm_sg_tt_init(>t->ttm, bo, page_flags)) {
1309 return >t->ttm.ttm;
1313 * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1315 * Map the pages of a ttm_tt object to an address space visible
1316 * to the underlying device.
1318 static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm,
1319 struct ttm_operation_ctx *ctx)
1321 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
1322 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1324 /* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1325 if (gtt && gtt->userptr) {
1326 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
1330 ttm->page_flags |= TTM_PAGE_FLAG_SG;
1331 ttm->state = tt_unbound;
1335 if (ttm->page_flags & TTM_PAGE_FLAG_SG) {
1337 struct dma_buf_attachment *attach;
1338 struct sg_table *sgt;
1340 attach = gtt->gobj->import_attach;
1341 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
1343 return PTR_ERR(sgt);
1348 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1349 gtt->ttm.dma_address,
1351 ttm->state = tt_unbound;
1355 #ifdef CONFIG_SWIOTLB
1356 if (adev->need_swiotlb && swiotlb_nr_tbl()) {
1357 return ttm_dma_populate(>t->ttm, adev->dev, ctx);
1361 /* fall back to generic helper to populate the page array
1362 * and map them to the device */
1363 return ttm_populate_and_map_pages(adev->dev, >t->ttm, ctx);
1367 * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1369 * Unmaps pages of a ttm_tt object from the device address space and
1370 * unpopulates the page array backing it.
1372 static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
1374 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1375 struct amdgpu_device *adev;
1377 if (gtt && gtt->userptr) {
1378 amdgpu_ttm_tt_set_user_pages(ttm, NULL);
1380 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
1384 if (ttm->sg && gtt->gobj->import_attach) {
1385 struct dma_buf_attachment *attach;
1387 attach = gtt->gobj->import_attach;
1388 dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
1393 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
1396 adev = amdgpu_ttm_adev(ttm->bdev);
1398 #ifdef CONFIG_SWIOTLB
1399 if (adev->need_swiotlb && swiotlb_nr_tbl()) {
1400 ttm_dma_unpopulate(>t->ttm, adev->dev);
1405 /* fall back to generic helper to unmap and unpopulate array */
1406 ttm_unmap_and_unpopulate_pages(adev->dev, >t->ttm);
1410 * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1413 * @ttm: The ttm_tt object to bind this userptr object to
1414 * @addr: The address in the current tasks VM space to use
1415 * @flags: Requirements of userptr object.
1417 * Called by amdgpu_gem_userptr_ioctl() to bind userptr pages
1420 int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
1423 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1428 gtt->userptr = addr;
1429 gtt->userflags = flags;
1432 put_task_struct(gtt->usertask);
1433 gtt->usertask = current->group_leader;
1434 get_task_struct(gtt->usertask);
1440 * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1442 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1444 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1449 if (gtt->usertask == NULL)
1452 return gtt->usertask->mm;
1456 * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1457 * address range for the current task.
1460 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1463 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1466 if (gtt == NULL || !gtt->userptr)
1469 /* Return false if no part of the ttm_tt object lies within
1472 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
1473 if (gtt->userptr > end || gtt->userptr + size <= start)
1480 * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
1482 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
1484 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1486 if (gtt == NULL || !gtt->userptr)
1493 * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1495 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1497 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1502 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1506 * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object
1508 * @ttm: The ttm_tt object to compute the flags for
1509 * @mem: The memory registry backing this ttm_tt object
1511 * Figure out the flags to use for a VM PDE (Page Directory Entry).
1513 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
1517 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1518 flags |= AMDGPU_PTE_VALID;
1520 if (mem && mem->mem_type == TTM_PL_TT) {
1521 flags |= AMDGPU_PTE_SYSTEM;
1523 if (ttm->caching_state == tt_cached)
1524 flags |= AMDGPU_PTE_SNOOPED;
1531 * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1533 * @ttm: The ttm_tt object to compute the flags for
1534 * @mem: The memory registry backing this ttm_tt object
1536 * Figure out the flags to use for a VM PTE (Page Table Entry).
1538 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1539 struct ttm_mem_reg *mem)
1541 uint64_t flags = amdgpu_ttm_tt_pde_flags(ttm, mem);
1543 flags |= adev->gart.gart_pte_flags;
1544 flags |= AMDGPU_PTE_READABLE;
1546 if (!amdgpu_ttm_tt_is_readonly(ttm))
1547 flags |= AMDGPU_PTE_WRITEABLE;
1553 * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1556 * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1557 * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1558 * it can find space for a new object and by ttm_bo_force_list_clean() which is
1559 * used to clean out a memory space.
1561 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1562 const struct ttm_place *place)
1564 unsigned long num_pages = bo->mem.num_pages;
1565 struct drm_mm_node *node = bo->mem.mm_node;
1566 struct dma_resv_list *flist;
1567 struct dma_fence *f;
1570 if (bo->type == ttm_bo_type_kernel &&
1571 !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)))
1574 /* If bo is a KFD BO, check if the bo belongs to the current process.
1575 * If true, then return false as any KFD process needs all its BOs to
1576 * be resident to run successfully
1578 flist = dma_resv_get_list(bo->base.resv);
1580 for (i = 0; i < flist->shared_count; ++i) {
1581 f = rcu_dereference_protected(flist->shared[i],
1582 dma_resv_held(bo->base.resv));
1583 if (amdkfd_fence_check_mm(f, current->mm))
1588 switch (bo->mem.mem_type) {
1590 if (amdgpu_bo_is_amdgpu_bo(bo) &&
1591 amdgpu_bo_encrypted(ttm_to_amdgpu_bo(bo)))
1596 /* Check each drm MM node individually */
1598 if (place->fpfn < (node->start + node->size) &&
1599 !(place->lpfn && place->lpfn <= node->start))
1602 num_pages -= node->size;
1611 return ttm_bo_eviction_valuable(bo, place);
1615 * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1617 * @bo: The buffer object to read/write
1618 * @offset: Offset into buffer object
1619 * @buf: Secondary buffer to write/read from
1620 * @len: Length in bytes of access
1621 * @write: true if writing
1623 * This is used to access VRAM that backs a buffer object via MMIO
1624 * access for debugging purposes.
1626 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1627 unsigned long offset,
1628 void *buf, int len, int write)
1630 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1631 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1632 struct drm_mm_node *nodes;
1636 unsigned long flags;
1638 if (bo->mem.mem_type != TTM_PL_VRAM)
1642 nodes = amdgpu_find_mm_node(&abo->tbo.mem, &pos);
1643 pos += (nodes->start << PAGE_SHIFT);
1645 while (len && pos < adev->gmc.mc_vram_size) {
1646 uint64_t aligned_pos = pos & ~(uint64_t)3;
1647 uint64_t bytes = 4 - (pos & 3);
1648 uint32_t shift = (pos & 3) * 8;
1649 uint32_t mask = 0xffffffff << shift;
1652 mask &= 0xffffffff >> (bytes - len) * 8;
1656 if (mask != 0xffffffff) {
1657 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1658 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
1659 WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
1660 if (!write || mask != 0xffffffff)
1661 value = RREG32_NO_KIQ(mmMM_DATA);
1664 value |= (*(uint32_t *)buf << shift) & mask;
1665 WREG32_NO_KIQ(mmMM_DATA, value);
1667 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1669 value = (value & mask) >> shift;
1670 memcpy(buf, &value, bytes);
1673 bytes = (nodes->start + nodes->size) << PAGE_SHIFT;
1674 bytes = min(bytes - pos, (uint64_t)len & ~0x3ull);
1676 amdgpu_device_vram_access(adev, pos, (uint32_t *)buf,
1681 buf = (uint8_t *)buf + bytes;
1684 if (pos >= (nodes->start + nodes->size) << PAGE_SHIFT) {
1686 pos = (nodes->start << PAGE_SHIFT);
1693 static struct ttm_bo_driver amdgpu_bo_driver = {
1694 .ttm_tt_create = &amdgpu_ttm_tt_create,
1695 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1696 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1697 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1698 .evict_flags = &amdgpu_evict_flags,
1699 .move = &amdgpu_bo_move,
1700 .verify_access = &amdgpu_verify_access,
1701 .move_notify = &amdgpu_bo_move_notify,
1702 .release_notify = &amdgpu_bo_release_notify,
1703 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1704 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1705 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1706 .access_memory = &amdgpu_ttm_access_memory,
1707 .del_from_lru_notify = &amdgpu_vm_del_from_lru_notify
1711 * Firmware Reservation functions
1714 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1716 * @adev: amdgpu_device pointer
1718 * free fw reserved vram if it has been reserved.
1720 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1722 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
1723 NULL, &adev->fw_vram_usage.va);
1727 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1729 * @adev: amdgpu_device pointer
1731 * create bo vram reservation from fw.
1733 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1735 uint64_t vram_size = adev->gmc.visible_vram_size;
1737 adev->fw_vram_usage.va = NULL;
1738 adev->fw_vram_usage.reserved_bo = NULL;
1740 if (adev->fw_vram_usage.size == 0 ||
1741 adev->fw_vram_usage.size > vram_size)
1744 return amdgpu_bo_create_kernel_at(adev,
1745 adev->fw_vram_usage.start_offset,
1746 adev->fw_vram_usage.size,
1747 AMDGPU_GEM_DOMAIN_VRAM,
1748 &adev->fw_vram_usage.reserved_bo,
1749 &adev->fw_vram_usage.va);
1753 * Memoy training reservation functions
1757 * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram
1759 * @adev: amdgpu_device pointer
1761 * free memory training reserved vram if it has been reserved.
1763 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev)
1765 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1767 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
1768 amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL);
1774 static u64 amdgpu_ttm_training_get_c2p_offset(u64 vram_size)
1776 if ((vram_size & (SZ_1M - 1)) < (SZ_4K + 1) )
1779 return ALIGN(vram_size, SZ_1M);
1783 * amdgpu_ttm_training_reserve_vram_init - create bo vram reservation from memory training
1785 * @adev: amdgpu_device pointer
1787 * create bo vram reservation from memory training.
1789 static int amdgpu_ttm_training_reserve_vram_init(struct amdgpu_device *adev)
1792 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1794 memset(ctx, 0, sizeof(*ctx));
1795 if (!adev->fw_vram_usage.mem_train_support) {
1796 DRM_DEBUG("memory training does not support!\n");
1800 ctx->c2p_train_data_offset = amdgpu_ttm_training_get_c2p_offset(adev->gmc.mc_vram_size);
1801 ctx->p2c_train_data_offset = (adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET);
1802 ctx->train_data_size = GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES;
1804 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
1805 ctx->train_data_size,
1806 ctx->p2c_train_data_offset,
1807 ctx->c2p_train_data_offset);
1809 ret = amdgpu_bo_create_kernel_at(adev,
1810 ctx->c2p_train_data_offset,
1811 ctx->train_data_size,
1812 AMDGPU_GEM_DOMAIN_VRAM,
1816 DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
1817 amdgpu_ttm_training_reserve_vram_fini(adev);
1821 ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS;
1826 * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1827 * gtt/vram related fields.
1829 * This initializes all of the memory space pools that the TTM layer
1830 * will need such as the GTT space (system memory mapped to the device),
1831 * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1832 * can be mapped per VMID.
1834 int amdgpu_ttm_init(struct amdgpu_device *adev)
1839 void *stolen_vga_buf;
1841 mutex_init(&adev->mman.gtt_window_lock);
1843 /* No others user of address space so set it to 0 */
1844 r = ttm_bo_device_init(&adev->mman.bdev,
1846 adev->ddev->anon_inode->i_mapping,
1847 adev->ddev->vma_offset_manager,
1848 dma_addressing_limited(adev->dev));
1850 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1853 adev->mman.initialized = true;
1855 /* We opt to avoid OOM on system pages allocations */
1856 adev->mman.bdev.no_retry = true;
1858 /* Initialize VRAM pool with all of VRAM divided into pages */
1859 r = amdgpu_vram_mgr_init(adev);
1861 DRM_ERROR("Failed initializing VRAM heap.\n");
1865 /* Reduce size of CPU-visible VRAM if requested */
1866 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1867 if (amdgpu_vis_vram_limit > 0 &&
1868 vis_vram_limit <= adev->gmc.visible_vram_size)
1869 adev->gmc.visible_vram_size = vis_vram_limit;
1871 /* Change the size here instead of the init above so only lpfn is affected */
1872 amdgpu_ttm_set_buffer_funcs_status(adev, false);
1874 adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base,
1875 adev->gmc.visible_vram_size);
1879 *The reserved vram for firmware must be pinned to the specified
1880 *place on the VRAM, so reserve it early.
1882 r = amdgpu_ttm_fw_reserve_vram_init(adev);
1888 *The reserved vram for memory training must be pinned to the specified
1889 *place on the VRAM, so reserve it early.
1891 if (!amdgpu_sriov_vf(adev)) {
1892 r = amdgpu_ttm_training_reserve_vram_init(adev);
1897 /* allocate memory as required for VGA
1898 * This is used for VGA emulation and pre-OS scanout buffers to
1899 * avoid display artifacts while transitioning between pre-OS
1901 r = amdgpu_bo_create_kernel(adev, adev->gmc.stolen_size, PAGE_SIZE,
1902 AMDGPU_GEM_DOMAIN_VRAM,
1903 &adev->stolen_vga_memory,
1904 NULL, &stolen_vga_buf);
1909 * reserve TMR memory at the top of VRAM which holds
1910 * IP Discovery data and is protected by PSP.
1912 if (adev->discovery_tmr_size > 0) {
1913 r = amdgpu_bo_create_kernel_at(adev,
1914 adev->gmc.real_vram_size - adev->discovery_tmr_size,
1915 adev->discovery_tmr_size,
1916 AMDGPU_GEM_DOMAIN_VRAM,
1917 &adev->discovery_memory,
1923 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1924 (unsigned) (adev->gmc.real_vram_size / (1024 * 1024)));
1926 /* Compute GTT size, either bsaed on 3/4th the size of RAM size
1927 * or whatever the user passed on module init */
1928 if (amdgpu_gtt_size == -1) {
1932 gtt_size = min(max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1933 adev->gmc.mc_vram_size),
1934 ((uint64_t)si.totalram * si.mem_unit * 3/4));
1937 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1939 /* Initialize GTT memory pool */
1940 r = amdgpu_gtt_mgr_init(adev, gtt_size);
1942 DRM_ERROR("Failed initializing GTT heap.\n");
1945 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1946 (unsigned)(gtt_size / (1024 * 1024)));
1948 /* Initialize various on-chip memory pools */
1949 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GDS, adev->gds.gds_size);
1951 DRM_ERROR("Failed initializing GDS heap.\n");
1955 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GWS, adev->gds.gws_size);
1957 DRM_ERROR("Failed initializing gws heap.\n");
1961 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_OA, adev->gds.oa_size);
1963 DRM_ERROR("Failed initializing oa heap.\n");
1971 * amdgpu_ttm_late_init - Handle any late initialization for amdgpu_ttm
1973 void amdgpu_ttm_late_init(struct amdgpu_device *adev)
1975 void *stolen_vga_buf;
1976 /* return the VGA stolen memory (if any) back to VRAM */
1977 amdgpu_bo_free_kernel(&adev->stolen_vga_memory, NULL, &stolen_vga_buf);
1981 * amdgpu_ttm_fini - De-initialize the TTM memory pools
1983 void amdgpu_ttm_fini(struct amdgpu_device *adev)
1985 if (!adev->mman.initialized)
1988 amdgpu_ttm_training_reserve_vram_fini(adev);
1989 /* return the IP Discovery TMR memory back to VRAM */
1990 amdgpu_bo_free_kernel(&adev->discovery_memory, NULL, NULL);
1991 amdgpu_ttm_fw_reserve_vram_fini(adev);
1993 if (adev->mman.aper_base_kaddr)
1994 iounmap(adev->mman.aper_base_kaddr);
1995 adev->mman.aper_base_kaddr = NULL;
1997 amdgpu_vram_mgr_fini(adev);
1998 amdgpu_gtt_mgr_fini(adev);
1999 ttm_range_man_fini(&adev->mman.bdev, &adev->mman.bdev.man[AMDGPU_PL_GDS]);
2000 ttm_range_man_fini(&adev->mman.bdev, &adev->mman.bdev.man[AMDGPU_PL_GWS]);
2001 ttm_range_man_fini(&adev->mman.bdev, &adev->mman.bdev.man[AMDGPU_PL_OA]);
2002 ttm_bo_device_release(&adev->mman.bdev);
2003 adev->mman.initialized = false;
2004 DRM_INFO("amdgpu: ttm finalized\n");
2008 * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
2010 * @adev: amdgpu_device pointer
2011 * @enable: true when we can use buffer functions.
2013 * Enable/disable use of buffer functions during suspend/resume. This should
2014 * only be called at bootup or when userspace isn't running.
2016 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
2018 struct ttm_mem_type_manager *man = &adev->mman.bdev.man[TTM_PL_VRAM];
2022 if (!adev->mman.initialized || adev->in_gpu_reset ||
2023 adev->mman.buffer_funcs_enabled == enable)
2027 struct amdgpu_ring *ring;
2028 struct drm_gpu_scheduler *sched;
2030 ring = adev->mman.buffer_funcs_ring;
2031 sched = &ring->sched;
2032 r = drm_sched_entity_init(&adev->mman.entity,
2033 DRM_SCHED_PRIORITY_KERNEL, &sched,
2036 DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
2041 drm_sched_entity_destroy(&adev->mman.entity);
2042 dma_fence_put(man->move);
2046 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
2048 size = adev->gmc.real_vram_size;
2050 size = adev->gmc.visible_vram_size;
2051 man->size = size >> PAGE_SHIFT;
2052 adev->mman.buffer_funcs_enabled = enable;
2055 int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
2057 struct drm_file *file_priv = filp->private_data;
2058 struct amdgpu_device *adev = file_priv->minor->dev->dev_private;
2063 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
2066 int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
2067 uint64_t dst_offset, uint32_t byte_count,
2068 struct dma_resv *resv,
2069 struct dma_fence **fence, bool direct_submit,
2070 bool vm_needs_flush, bool tmz)
2072 enum amdgpu_ib_pool_type pool = direct_submit ? AMDGPU_IB_POOL_DIRECT :
2073 AMDGPU_IB_POOL_DELAYED;
2074 struct amdgpu_device *adev = ring->adev;
2075 struct amdgpu_job *job;
2078 unsigned num_loops, num_dw;
2082 if (direct_submit && !ring->sched.ready) {
2083 DRM_ERROR("Trying to move memory with ring turned off.\n");
2087 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
2088 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
2089 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->copy_num_dw, 8);
2091 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, pool, &job);
2095 if (vm_needs_flush) {
2096 job->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gart.bo);
2097 job->vm_needs_flush = true;
2100 r = amdgpu_sync_resv(adev, &job->sync, resv,
2102 AMDGPU_FENCE_OWNER_UNDEFINED);
2104 DRM_ERROR("sync failed (%d).\n", r);
2109 for (i = 0; i < num_loops; i++) {
2110 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2112 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
2113 dst_offset, cur_size_in_bytes, tmz);
2115 src_offset += cur_size_in_bytes;
2116 dst_offset += cur_size_in_bytes;
2117 byte_count -= cur_size_in_bytes;
2120 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2121 WARN_ON(job->ibs[0].length_dw > num_dw);
2123 r = amdgpu_job_submit_direct(job, ring, fence);
2125 r = amdgpu_job_submit(job, &adev->mman.entity,
2126 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2133 amdgpu_job_free(job);
2134 DRM_ERROR("Error scheduling IBs (%d)\n", r);
2138 int amdgpu_fill_buffer(struct amdgpu_bo *bo,
2140 struct dma_resv *resv,
2141 struct dma_fence **fence)
2143 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2144 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
2145 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2147 struct drm_mm_node *mm_node;
2148 unsigned long num_pages;
2149 unsigned int num_loops, num_dw;
2151 struct amdgpu_job *job;
2154 if (!adev->mman.buffer_funcs_enabled) {
2155 DRM_ERROR("Trying to clear memory with ring turned off.\n");
2159 if (bo->tbo.mem.mem_type == TTM_PL_TT) {
2160 r = amdgpu_ttm_alloc_gart(&bo->tbo);
2165 num_pages = bo->tbo.num_pages;
2166 mm_node = bo->tbo.mem.mm_node;
2169 uint64_t byte_count = mm_node->size << PAGE_SHIFT;
2171 num_loops += DIV_ROUND_UP_ULL(byte_count, max_bytes);
2172 num_pages -= mm_node->size;
2175 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
2177 /* for IB padding */
2180 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, AMDGPU_IB_POOL_DELAYED,
2186 r = amdgpu_sync_resv(adev, &job->sync, resv,
2188 AMDGPU_FENCE_OWNER_UNDEFINED);
2190 DRM_ERROR("sync failed (%d).\n", r);
2195 num_pages = bo->tbo.num_pages;
2196 mm_node = bo->tbo.mem.mm_node;
2199 uint64_t byte_count = mm_node->size << PAGE_SHIFT;
2202 dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
2203 while (byte_count) {
2204 uint32_t cur_size_in_bytes = min_t(uint64_t, byte_count,
2207 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
2208 dst_addr, cur_size_in_bytes);
2210 dst_addr += cur_size_in_bytes;
2211 byte_count -= cur_size_in_bytes;
2214 num_pages -= mm_node->size;
2218 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2219 WARN_ON(job->ibs[0].length_dw > num_dw);
2220 r = amdgpu_job_submit(job, &adev->mman.entity,
2221 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2228 amdgpu_job_free(job);
2232 #if defined(CONFIG_DEBUG_FS)
2234 static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
2236 struct drm_info_node *node = (struct drm_info_node *)m->private;
2237 unsigned ttm_pl = (uintptr_t)node->info_ent->data;
2238 struct drm_device *dev = node->minor->dev;
2239 struct amdgpu_device *adev = dev->dev_private;
2240 struct ttm_mem_type_manager *man = &adev->mman.bdev.man[ttm_pl];
2241 struct drm_printer p = drm_seq_file_printer(m);
2243 man->func->debug(man, &p);
2247 static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
2248 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, (void *)TTM_PL_VRAM},
2249 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, (void *)TTM_PL_TT},
2250 {"amdgpu_gds_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_GDS},
2251 {"amdgpu_gws_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_GWS},
2252 {"amdgpu_oa_mm", amdgpu_mm_dump_table, 0, (void *)AMDGPU_PL_OA},
2253 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
2254 #ifdef CONFIG_SWIOTLB
2255 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
2260 * amdgpu_ttm_vram_read - Linear read access to VRAM
2262 * Accesses VRAM via MMIO for debugging purposes.
2264 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
2265 size_t size, loff_t *pos)
2267 struct amdgpu_device *adev = file_inode(f)->i_private;
2270 if (size & 0x3 || *pos & 0x3)
2273 if (*pos >= adev->gmc.mc_vram_size)
2276 size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos));
2278 size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4);
2279 uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ];
2281 amdgpu_device_vram_access(adev, *pos, value, bytes, false);
2282 if (copy_to_user(buf, value, bytes))
2295 * amdgpu_ttm_vram_write - Linear write access to VRAM
2297 * Accesses VRAM via MMIO for debugging purposes.
2299 static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
2300 size_t size, loff_t *pos)
2302 struct amdgpu_device *adev = file_inode(f)->i_private;
2306 if (size & 0x3 || *pos & 0x3)
2309 if (*pos >= adev->gmc.mc_vram_size)
2313 unsigned long flags;
2316 if (*pos >= adev->gmc.mc_vram_size)
2319 r = get_user(value, (uint32_t *)buf);
2323 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
2324 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
2325 WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
2326 WREG32_NO_KIQ(mmMM_DATA, value);
2327 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
2338 static const struct file_operations amdgpu_ttm_vram_fops = {
2339 .owner = THIS_MODULE,
2340 .read = amdgpu_ttm_vram_read,
2341 .write = amdgpu_ttm_vram_write,
2342 .llseek = default_llseek,
2345 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2348 * amdgpu_ttm_gtt_read - Linear read access to GTT memory
2350 static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
2351 size_t size, loff_t *pos)
2353 struct amdgpu_device *adev = file_inode(f)->i_private;
2358 loff_t p = *pos / PAGE_SIZE;
2359 unsigned off = *pos & ~PAGE_MASK;
2360 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
2364 if (p >= adev->gart.num_cpu_pages)
2367 page = adev->gart.pages[p];
2372 r = copy_to_user(buf, ptr, cur_size);
2373 kunmap(adev->gart.pages[p]);
2375 r = clear_user(buf, cur_size);
2389 static const struct file_operations amdgpu_ttm_gtt_fops = {
2390 .owner = THIS_MODULE,
2391 .read = amdgpu_ttm_gtt_read,
2392 .llseek = default_llseek
2398 * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2400 * This function is used to read memory that has been mapped to the
2401 * GPU and the known addresses are not physical addresses but instead
2402 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2404 static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
2405 size_t size, loff_t *pos)
2407 struct amdgpu_device *adev = file_inode(f)->i_private;
2408 struct iommu_domain *dom;
2412 /* retrieve the IOMMU domain if any for this device */
2413 dom = iommu_get_domain_for_dev(adev->dev);
2416 phys_addr_t addr = *pos & PAGE_MASK;
2417 loff_t off = *pos & ~PAGE_MASK;
2418 size_t bytes = PAGE_SIZE - off;
2423 bytes = bytes < size ? bytes : size;
2425 /* Translate the bus address to a physical address. If
2426 * the domain is NULL it means there is no IOMMU active
2427 * and the address translation is the identity
2429 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2431 pfn = addr >> PAGE_SHIFT;
2432 if (!pfn_valid(pfn))
2435 p = pfn_to_page(pfn);
2436 if (p->mapping != adev->mman.bdev.dev_mapping)
2440 r = copy_to_user(buf, ptr + off, bytes);
2454 * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2456 * This function is used to write memory that has been mapped to the
2457 * GPU and the known addresses are not physical addresses but instead
2458 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2460 static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
2461 size_t size, loff_t *pos)
2463 struct amdgpu_device *adev = file_inode(f)->i_private;
2464 struct iommu_domain *dom;
2468 dom = iommu_get_domain_for_dev(adev->dev);
2471 phys_addr_t addr = *pos & PAGE_MASK;
2472 loff_t off = *pos & ~PAGE_MASK;
2473 size_t bytes = PAGE_SIZE - off;
2478 bytes = bytes < size ? bytes : size;
2480 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2482 pfn = addr >> PAGE_SHIFT;
2483 if (!pfn_valid(pfn))
2486 p = pfn_to_page(pfn);
2487 if (p->mapping != adev->mman.bdev.dev_mapping)
2491 r = copy_from_user(ptr + off, buf, bytes);
2504 static const struct file_operations amdgpu_ttm_iomem_fops = {
2505 .owner = THIS_MODULE,
2506 .read = amdgpu_iomem_read,
2507 .write = amdgpu_iomem_write,
2508 .llseek = default_llseek
2511 static const struct {
2513 const struct file_operations *fops;
2515 } ttm_debugfs_entries[] = {
2516 { "amdgpu_vram", &amdgpu_ttm_vram_fops, TTM_PL_VRAM },
2517 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2518 { "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
2520 { "amdgpu_iomem", &amdgpu_ttm_iomem_fops, TTM_PL_SYSTEM },
2525 int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
2527 #if defined(CONFIG_DEBUG_FS)
2530 struct drm_minor *minor = adev->ddev->primary;
2531 struct dentry *ent, *root = minor->debugfs_root;
2533 for (count = 0; count < ARRAY_SIZE(ttm_debugfs_entries); count++) {
2534 ent = debugfs_create_file(
2535 ttm_debugfs_entries[count].name,
2536 S_IFREG | S_IRUGO, root,
2538 ttm_debugfs_entries[count].fops);
2540 return PTR_ERR(ent);
2541 if (ttm_debugfs_entries[count].domain == TTM_PL_VRAM)
2542 i_size_write(ent->d_inode, adev->gmc.mc_vram_size);
2543 else if (ttm_debugfs_entries[count].domain == TTM_PL_TT)
2544 i_size_write(ent->d_inode, adev->gmc.gart_size);
2545 adev->mman.debugfs_entries[count] = ent;
2548 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
2550 #ifdef CONFIG_SWIOTLB
2551 if (!(adev->need_swiotlb && swiotlb_nr_tbl()))
2555 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);