2 * Copyright 2009 Jerome Glisse.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
33 #include <linux/dma-mapping.h>
34 #include <linux/iommu.h>
35 #include <linux/pagemap.h>
36 #include <linux/sched/task.h>
37 #include <linux/sched/mm.h>
38 #include <linux/seq_file.h>
39 #include <linux/slab.h>
40 #include <linux/swap.h>
41 #include <linux/dma-buf.h>
42 #include <linux/sizes.h>
43 #include <linux/module.h>
45 #include <drm/drm_drv.h>
46 #include <drm/ttm/ttm_bo.h>
47 #include <drm/ttm/ttm_placement.h>
48 #include <drm/ttm/ttm_range_manager.h>
49 #include <drm/ttm/ttm_tt.h>
51 #include <drm/amdgpu_drm.h>
54 #include "amdgpu_object.h"
55 #include "amdgpu_trace.h"
56 #include "amdgpu_amdkfd.h"
57 #include "amdgpu_sdma.h"
58 #include "amdgpu_ras.h"
59 #include "amdgpu_hmm.h"
60 #include "amdgpu_atomfirmware.h"
61 #include "amdgpu_res_cursor.h"
62 #include "bif/bif_4_1_d.h"
64 MODULE_IMPORT_NS(DMA_BUF);
66 #define AMDGPU_TTM_VRAM_MAX_DW_READ ((size_t)128)
68 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
70 struct ttm_resource *bo_mem);
71 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
74 static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
76 uint64_t size_in_page)
78 return ttm_range_man_init(&adev->mman.bdev, type,
83 * amdgpu_evict_flags - Compute placement flags
85 * @bo: The buffer object to evict
86 * @placement: Possible destination(s) for evicted BO
88 * Fill in placement data when ttm_bo_evict() is called
90 static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
91 struct ttm_placement *placement)
93 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
94 struct amdgpu_bo *abo;
95 static const struct ttm_place placements = {
98 .mem_type = TTM_PL_SYSTEM,
102 /* Don't handle scatter gather BOs */
103 if (bo->type == ttm_bo_type_sg) {
104 placement->num_placement = 0;
108 /* Object isn't an AMDGPU object so ignore */
109 if (!amdgpu_bo_is_amdgpu_bo(bo)) {
110 placement->placement = &placements;
111 placement->num_placement = 1;
115 abo = ttm_to_amdgpu_bo(bo);
116 if (abo->flags & AMDGPU_GEM_CREATE_DISCARDABLE) {
117 placement->num_placement = 0;
121 switch (bo->resource->mem_type) {
125 case AMDGPU_PL_DOORBELL:
126 placement->num_placement = 0;
130 if (!adev->mman.buffer_funcs_enabled) {
131 /* Move to system memory */
132 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
134 } else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
135 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
136 amdgpu_res_cpu_visible(adev, bo->resource)) {
138 /* Try evicting to the CPU inaccessible part of VRAM
139 * first, but only set GTT as busy placement, so this
140 * BO will be evicted to GTT rather than causing other
141 * BOs to be evicted from VRAM
143 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
144 AMDGPU_GEM_DOMAIN_GTT |
145 AMDGPU_GEM_DOMAIN_CPU);
146 abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
147 abo->placements[0].lpfn = 0;
148 abo->placements[0].flags |= TTM_PL_FLAG_DESIRED;
150 /* Move to GTT memory */
151 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT |
152 AMDGPU_GEM_DOMAIN_CPU);
156 case AMDGPU_PL_PREEMPT:
158 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
161 *placement = abo->placement;
165 * amdgpu_ttm_map_buffer - Map memory into the GART windows
166 * @bo: buffer object to map
167 * @mem: memory object to map
168 * @mm_cur: range to map
169 * @window: which GART window to use
170 * @ring: DMA ring to use for the copy
171 * @tmz: if we should setup a TMZ enabled mapping
172 * @size: in number of bytes to map, out number of bytes mapped
173 * @addr: resulting address inside the MC address space
175 * Setup one of the GART windows to access a specific piece of memory or return
176 * the physical address for local memory.
178 static int amdgpu_ttm_map_buffer(struct ttm_buffer_object *bo,
179 struct ttm_resource *mem,
180 struct amdgpu_res_cursor *mm_cur,
181 unsigned int window, struct amdgpu_ring *ring,
182 bool tmz, uint64_t *size, uint64_t *addr)
184 struct amdgpu_device *adev = ring->adev;
185 unsigned int offset, num_pages, num_dw, num_bytes;
186 uint64_t src_addr, dst_addr;
187 struct amdgpu_job *job;
193 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
194 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
196 if (WARN_ON(mem->mem_type == AMDGPU_PL_PREEMPT))
199 /* Map only what can't be accessed directly */
200 if (!tmz && mem->start != AMDGPU_BO_INVALID_OFFSET) {
201 *addr = amdgpu_ttm_domain_start(adev, mem->mem_type) +
208 * If start begins at an offset inside the page, then adjust the size
209 * and addr accordingly
211 offset = mm_cur->start & ~PAGE_MASK;
213 num_pages = PFN_UP(*size + offset);
214 num_pages = min_t(uint32_t, num_pages, AMDGPU_GTT_MAX_TRANSFER_SIZE);
216 *size = min(*size, (uint64_t)num_pages * PAGE_SIZE - offset);
218 *addr = adev->gmc.gart_start;
219 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
220 AMDGPU_GPU_PAGE_SIZE;
223 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
224 num_bytes = num_pages * 8 * AMDGPU_GPU_PAGES_IN_CPU_PAGE;
226 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.high_pr,
227 AMDGPU_FENCE_OWNER_UNDEFINED,
228 num_dw * 4 + num_bytes,
229 AMDGPU_IB_POOL_DELAYED, &job);
233 src_addr = num_dw * 4;
234 src_addr += job->ibs[0].gpu_addr;
236 dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
237 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
238 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
239 dst_addr, num_bytes, false);
241 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
242 WARN_ON(job->ibs[0].length_dw > num_dw);
244 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, mem);
246 flags |= AMDGPU_PTE_TMZ;
248 cpu_addr = &job->ibs[0].ptr[num_dw];
250 if (mem->mem_type == TTM_PL_TT) {
251 dma_addr_t *dma_addr;
253 dma_addr = &bo->ttm->dma_address[mm_cur->start >> PAGE_SHIFT];
254 amdgpu_gart_map(adev, 0, num_pages, dma_addr, flags, cpu_addr);
256 dma_addr_t dma_address;
258 dma_address = mm_cur->start;
259 dma_address += adev->vm_manager.vram_base_offset;
261 for (i = 0; i < num_pages; ++i) {
262 amdgpu_gart_map(adev, i << PAGE_SHIFT, 1, &dma_address,
264 dma_address += PAGE_SIZE;
268 dma_fence_put(amdgpu_job_submit(job));
273 * amdgpu_ttm_copy_mem_to_mem - Helper function for copy
274 * @adev: amdgpu device
275 * @src: buffer/address where to read from
276 * @dst: buffer/address where to write to
277 * @size: number of bytes to copy
278 * @tmz: if a secure copy should be used
279 * @resv: resv object to sync to
280 * @f: Returns the last fence if multiple jobs are submitted.
282 * The function copies @size bytes from {src->mem + src->offset} to
283 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
284 * move and different for a BO to BO copy.
287 int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
288 const struct amdgpu_copy_mem *src,
289 const struct amdgpu_copy_mem *dst,
290 uint64_t size, bool tmz,
291 struct dma_resv *resv,
292 struct dma_fence **f)
294 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
295 struct amdgpu_res_cursor src_mm, dst_mm;
296 struct dma_fence *fence = NULL;
299 if (!adev->mman.buffer_funcs_enabled) {
300 DRM_ERROR("Trying to move memory with ring turned off.\n");
304 amdgpu_res_first(src->mem, src->offset, size, &src_mm);
305 amdgpu_res_first(dst->mem, dst->offset, size, &dst_mm);
307 mutex_lock(&adev->mman.gtt_window_lock);
308 while (src_mm.remaining) {
309 uint64_t from, to, cur_size;
310 struct dma_fence *next;
312 /* Never copy more than 256MiB at once to avoid a timeout */
313 cur_size = min3(src_mm.size, dst_mm.size, 256ULL << 20);
315 /* Map src to window 0 and dst to window 1. */
316 r = amdgpu_ttm_map_buffer(src->bo, src->mem, &src_mm,
317 0, ring, tmz, &cur_size, &from);
321 r = amdgpu_ttm_map_buffer(dst->bo, dst->mem, &dst_mm,
322 1, ring, tmz, &cur_size, &to);
326 r = amdgpu_copy_buffer(ring, from, to, cur_size,
327 resv, &next, false, true, tmz);
331 dma_fence_put(fence);
334 amdgpu_res_next(&src_mm, cur_size);
335 amdgpu_res_next(&dst_mm, cur_size);
338 mutex_unlock(&adev->mman.gtt_window_lock);
340 *f = dma_fence_get(fence);
341 dma_fence_put(fence);
346 * amdgpu_move_blit - Copy an entire buffer to another buffer
348 * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
349 * help move buffers to and from VRAM.
351 static int amdgpu_move_blit(struct ttm_buffer_object *bo,
353 struct ttm_resource *new_mem,
354 struct ttm_resource *old_mem)
356 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
357 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
358 struct amdgpu_copy_mem src, dst;
359 struct dma_fence *fence = NULL;
369 r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
371 amdgpu_bo_encrypted(abo),
372 bo->base.resv, &fence);
376 /* clear the space being freed */
377 if (old_mem->mem_type == TTM_PL_VRAM &&
378 (abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)) {
379 struct dma_fence *wipe_fence = NULL;
381 r = amdgpu_fill_buffer(abo, 0, NULL, &wipe_fence,
385 } else if (wipe_fence) {
386 amdgpu_vram_mgr_set_cleared(bo->resource);
387 dma_fence_put(fence);
392 /* Always block for VM page tables before committing the new location */
393 if (bo->type == ttm_bo_type_kernel)
394 r = ttm_bo_move_accel_cleanup(bo, fence, true, false, new_mem);
396 r = ttm_bo_move_accel_cleanup(bo, fence, evict, true, new_mem);
397 dma_fence_put(fence);
402 dma_fence_wait(fence, false);
403 dma_fence_put(fence);
408 * amdgpu_res_cpu_visible - Check that resource can be accessed by CPU
409 * @adev: amdgpu device
410 * @res: the resource to check
412 * Returns: true if the full resource is CPU visible, false otherwise.
414 bool amdgpu_res_cpu_visible(struct amdgpu_device *adev,
415 struct ttm_resource *res)
417 struct amdgpu_res_cursor cursor;
422 if (res->mem_type == TTM_PL_SYSTEM || res->mem_type == TTM_PL_TT ||
423 res->mem_type == AMDGPU_PL_PREEMPT)
426 if (res->mem_type != TTM_PL_VRAM)
429 amdgpu_res_first(res, 0, res->size, &cursor);
430 while (cursor.remaining) {
431 if ((cursor.start + cursor.size) >= adev->gmc.visible_vram_size)
433 amdgpu_res_next(&cursor, cursor.size);
440 * amdgpu_res_copyable - Check that memory can be accessed by ttm_bo_move_memcpy
442 * Called by amdgpu_bo_move()
444 static bool amdgpu_res_copyable(struct amdgpu_device *adev,
445 struct ttm_resource *mem)
447 if (!amdgpu_res_cpu_visible(adev, mem))
450 /* ttm_resource_ioremap only supports contiguous memory */
451 if (mem->mem_type == TTM_PL_VRAM &&
452 !(mem->placement & TTM_PL_FLAG_CONTIGUOUS))
459 * amdgpu_bo_move - Move a buffer object to a new memory location
461 * Called by ttm_bo_handle_move_mem()
463 static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
464 struct ttm_operation_ctx *ctx,
465 struct ttm_resource *new_mem,
466 struct ttm_place *hop)
468 struct amdgpu_device *adev;
469 struct amdgpu_bo *abo;
470 struct ttm_resource *old_mem = bo->resource;
473 if (new_mem->mem_type == TTM_PL_TT ||
474 new_mem->mem_type == AMDGPU_PL_PREEMPT) {
475 r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, new_mem);
480 abo = ttm_to_amdgpu_bo(bo);
481 adev = amdgpu_ttm_adev(bo->bdev);
483 if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM &&
485 ttm_bo_move_null(bo, new_mem);
488 if (old_mem->mem_type == TTM_PL_SYSTEM &&
489 (new_mem->mem_type == TTM_PL_TT ||
490 new_mem->mem_type == AMDGPU_PL_PREEMPT)) {
491 ttm_bo_move_null(bo, new_mem);
494 if ((old_mem->mem_type == TTM_PL_TT ||
495 old_mem->mem_type == AMDGPU_PL_PREEMPT) &&
496 new_mem->mem_type == TTM_PL_SYSTEM) {
497 r = ttm_bo_wait_ctx(bo, ctx);
501 amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
502 ttm_resource_free(bo, &bo->resource);
503 ttm_bo_assign_mem(bo, new_mem);
507 if (old_mem->mem_type == AMDGPU_PL_GDS ||
508 old_mem->mem_type == AMDGPU_PL_GWS ||
509 old_mem->mem_type == AMDGPU_PL_OA ||
510 old_mem->mem_type == AMDGPU_PL_DOORBELL ||
511 new_mem->mem_type == AMDGPU_PL_GDS ||
512 new_mem->mem_type == AMDGPU_PL_GWS ||
513 new_mem->mem_type == AMDGPU_PL_OA ||
514 new_mem->mem_type == AMDGPU_PL_DOORBELL) {
515 /* Nothing to save here */
516 ttm_bo_move_null(bo, new_mem);
520 if (bo->type == ttm_bo_type_device &&
521 new_mem->mem_type == TTM_PL_VRAM &&
522 old_mem->mem_type != TTM_PL_VRAM) {
523 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
524 * accesses the BO after it's moved.
526 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
529 if (adev->mman.buffer_funcs_enabled) {
530 if (((old_mem->mem_type == TTM_PL_SYSTEM &&
531 new_mem->mem_type == TTM_PL_VRAM) ||
532 (old_mem->mem_type == TTM_PL_VRAM &&
533 new_mem->mem_type == TTM_PL_SYSTEM))) {
536 hop->mem_type = TTM_PL_TT;
537 hop->flags = TTM_PL_FLAG_TEMPORARY;
541 r = amdgpu_move_blit(bo, evict, new_mem, old_mem);
547 /* Check that all memory is CPU accessible */
548 if (!amdgpu_res_copyable(adev, old_mem) ||
549 !amdgpu_res_copyable(adev, new_mem)) {
550 pr_err("Move buffer fallback to memcpy unavailable\n");
554 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
559 trace_amdgpu_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
561 /* update statistics */
562 atomic64_add(bo->base.size, &adev->num_bytes_moved);
563 amdgpu_bo_move_notify(bo, evict);
568 * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
570 * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
572 static int amdgpu_ttm_io_mem_reserve(struct ttm_device *bdev,
573 struct ttm_resource *mem)
575 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
577 switch (mem->mem_type) {
582 case AMDGPU_PL_PREEMPT:
585 mem->bus.offset = mem->start << PAGE_SHIFT;
587 if (adev->mman.aper_base_kaddr &&
588 mem->placement & TTM_PL_FLAG_CONTIGUOUS)
589 mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr +
592 mem->bus.offset += adev->gmc.aper_base;
593 mem->bus.is_iomem = true;
595 case AMDGPU_PL_DOORBELL:
596 mem->bus.offset = mem->start << PAGE_SHIFT;
597 mem->bus.offset += adev->doorbell.base;
598 mem->bus.is_iomem = true;
599 mem->bus.caching = ttm_uncached;
607 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
608 unsigned long page_offset)
610 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
611 struct amdgpu_res_cursor cursor;
613 amdgpu_res_first(bo->resource, (u64)page_offset << PAGE_SHIFT, 0,
616 if (bo->resource->mem_type == AMDGPU_PL_DOORBELL)
617 return ((uint64_t)(adev->doorbell.base + cursor.start)) >> PAGE_SHIFT;
619 return (adev->gmc.aper_base + cursor.start) >> PAGE_SHIFT;
623 * amdgpu_ttm_domain_start - Returns GPU start address
624 * @adev: amdgpu device object
625 * @type: type of the memory
628 * GPU start address of a memory domain
631 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type)
635 return adev->gmc.gart_start;
637 return adev->gmc.vram_start;
644 * TTM backend functions.
646 struct amdgpu_ttm_tt {
648 struct drm_gem_object *gobj;
651 struct task_struct *usertask;
657 #define ttm_to_amdgpu_ttm_tt(ptr) container_of(ptr, struct amdgpu_ttm_tt, ttm)
659 #ifdef CONFIG_DRM_AMDGPU_USERPTR
661 * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user
662 * memory and start HMM tracking CPU page table update
664 * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only
665 * once afterwards to stop HMM tracking
667 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages,
668 struct hmm_range **range)
670 struct ttm_tt *ttm = bo->tbo.ttm;
671 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
672 unsigned long start = gtt->userptr;
673 struct vm_area_struct *vma;
674 struct mm_struct *mm;
678 /* Make sure get_user_pages_done() can cleanup gracefully */
681 mm = bo->notifier.mm;
683 DRM_DEBUG_DRIVER("BO is not registered?\n");
687 if (!mmget_not_zero(mm)) /* Happens during process shutdown */
691 vma = vma_lookup(mm, start);
692 if (unlikely(!vma)) {
696 if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) &&
702 readonly = amdgpu_ttm_tt_is_readonly(ttm);
703 r = amdgpu_hmm_range_get_pages(&bo->notifier, start, ttm->num_pages,
704 readonly, NULL, pages, range);
706 mmap_read_unlock(mm);
708 pr_debug("failed %d to get user pages 0x%lx\n", r, start);
715 /* amdgpu_ttm_tt_discard_user_pages - Discard range and pfn array allocations
717 void amdgpu_ttm_tt_discard_user_pages(struct ttm_tt *ttm,
718 struct hmm_range *range)
720 struct amdgpu_ttm_tt *gtt = (void *)ttm;
722 if (gtt && gtt->userptr && range)
723 amdgpu_hmm_range_get_pages_done(range);
727 * amdgpu_ttm_tt_get_user_pages_done - stop HMM track the CPU page table change
728 * Check if the pages backing this ttm range have been invalidated
730 * Returns: true if pages are still valid
732 bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm,
733 struct hmm_range *range)
735 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
737 if (!gtt || !gtt->userptr || !range)
740 DRM_DEBUG_DRIVER("user_pages_done 0x%llx pages 0x%x\n",
741 gtt->userptr, ttm->num_pages);
743 WARN_ONCE(!range->hmm_pfns, "No user pages to check\n");
745 return !amdgpu_hmm_range_get_pages_done(range);
750 * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
752 * Called by amdgpu_cs_list_validate(). This creates the page list
753 * that backs user memory and will ultimately be mapped into the device
756 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
760 for (i = 0; i < ttm->num_pages; ++i)
761 ttm->pages[i] = pages ? pages[i] : NULL;
765 * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages
767 * Called by amdgpu_ttm_backend_bind()
769 static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev,
772 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
773 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
774 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
775 enum dma_data_direction direction = write ?
776 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
779 /* Allocate an SG array and squash pages into it */
780 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
781 (u64)ttm->num_pages << PAGE_SHIFT,
786 /* Map SG to device */
787 r = dma_map_sgtable(adev->dev, ttm->sg, direction, 0);
791 /* convert SG to linear array of pages and dma addresses */
792 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
804 * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
806 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_device *bdev,
809 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
810 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
811 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
812 enum dma_data_direction direction = write ?
813 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
815 /* double check that we don't free the table twice */
816 if (!ttm->sg || !ttm->sg->sgl)
819 /* unmap the pages mapped to the device */
820 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
821 sg_free_table(ttm->sg);
825 * total_pages is constructed as MQD0+CtrlStack0 + MQD1+CtrlStack1 + ...
826 * MQDn+CtrlStackn where n is the number of XCCs per partition.
827 * pages_per_xcc is the size of one MQD+CtrlStack. The first page is MQD
828 * and uses memory type default, UC. The rest of pages_per_xcc are
829 * Ctrl stack and modify their memory type to NC.
831 static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
832 struct ttm_tt *ttm, uint64_t flags)
834 struct amdgpu_ttm_tt *gtt = (void *)ttm;
835 uint64_t total_pages = ttm->num_pages;
836 int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
837 uint64_t page_idx, pages_per_xcc;
839 uint64_t ctrl_flags = (flags & ~AMDGPU_PTE_MTYPE_VG10_MASK) |
840 AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC);
842 pages_per_xcc = total_pages;
843 do_div(pages_per_xcc, num_xcc);
845 for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
846 /* MQD page: use default flags */
847 amdgpu_gart_bind(adev,
848 gtt->offset + (page_idx << PAGE_SHIFT),
849 1, >t->ttm.dma_address[page_idx], flags);
851 * Ctrl pages - modify the memory type to NC (ctrl_flags) from
852 * the second page of the BO onward.
854 amdgpu_gart_bind(adev,
855 gtt->offset + ((page_idx + 1) << PAGE_SHIFT),
857 >t->ttm.dma_address[page_idx + 1],
862 static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
863 struct ttm_buffer_object *tbo,
866 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo);
867 struct ttm_tt *ttm = tbo->ttm;
868 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
870 if (amdgpu_bo_encrypted(abo))
871 flags |= AMDGPU_PTE_TMZ;
873 if (abo->flags & AMDGPU_GEM_CREATE_CP_MQD_GFX9) {
874 amdgpu_ttm_gart_bind_gfx9_mqd(adev, ttm, flags);
876 amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
877 gtt->ttm.dma_address, flags);
883 * amdgpu_ttm_backend_bind - Bind GTT memory
885 * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
886 * This handles binding GTT memory to the device address space.
888 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
890 struct ttm_resource *bo_mem)
892 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
893 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
904 r = amdgpu_ttm_tt_pin_userptr(bdev, ttm);
906 DRM_ERROR("failed to pin userptr\n");
909 } else if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) {
911 struct dma_buf_attachment *attach;
912 struct sg_table *sgt;
914 attach = gtt->gobj->import_attach;
915 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
922 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
926 if (!ttm->num_pages) {
927 WARN(1, "nothing to bind %u pages for mreg %p back %p!\n",
928 ttm->num_pages, bo_mem, ttm);
931 if (bo_mem->mem_type != TTM_PL_TT ||
932 !amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
933 gtt->offset = AMDGPU_BO_INVALID_OFFSET;
937 /* compute PTE flags relevant to this BO memory */
938 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem);
940 /* bind pages into GART page tables */
941 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
942 amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
943 gtt->ttm.dma_address, flags);
949 * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
950 * through AGP or GART aperture.
952 * If bo is accessible through AGP aperture, then use AGP aperture
953 * to access bo; otherwise allocate logical space in GART aperture
954 * and map bo to GART aperture.
956 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
958 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
959 struct ttm_operation_ctx ctx = { false, false };
960 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
961 struct ttm_placement placement;
962 struct ttm_place placements;
963 struct ttm_resource *tmp;
964 uint64_t addr, flags;
967 if (bo->resource->start != AMDGPU_BO_INVALID_OFFSET)
970 addr = amdgpu_gmc_agp_addr(bo);
971 if (addr != AMDGPU_BO_INVALID_OFFSET)
974 /* allocate GART space */
975 placement.num_placement = 1;
976 placement.placement = &placements;
978 placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
979 placements.mem_type = TTM_PL_TT;
980 placements.flags = bo->resource->placement;
982 r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
986 /* compute PTE flags for this buffer object */
987 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, tmp);
990 gtt->offset = (u64)tmp->start << PAGE_SHIFT;
991 amdgpu_ttm_gart_bind(adev, bo, flags);
992 amdgpu_gart_invalidate_tlb(adev);
993 ttm_resource_free(bo, &bo->resource);
994 ttm_bo_assign_mem(bo, tmp);
1000 * amdgpu_ttm_recover_gart - Rebind GTT pages
1002 * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
1003 * rebind GTT pages during a GPU reset.
1005 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
1007 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
1013 flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, tbo->resource);
1014 amdgpu_ttm_gart_bind(adev, tbo, flags);
1018 * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
1020 * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
1023 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
1026 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1027 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1029 /* if the pages have userptr pinning then clear that first */
1031 amdgpu_ttm_tt_unpin_userptr(bdev, ttm);
1032 } else if (ttm->sg && gtt->gobj->import_attach) {
1033 struct dma_buf_attachment *attach;
1035 attach = gtt->gobj->import_attach;
1036 dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
1043 if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
1046 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1047 amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages);
1051 static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev,
1054 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1057 put_task_struct(gtt->usertask);
1059 ttm_tt_fini(>t->ttm);
1064 * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1066 * @bo: The buffer object to create a GTT ttm_tt object around
1067 * @page_flags: Page flags to be added to the ttm_tt object
1069 * Called by ttm_tt_create().
1071 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
1072 uint32_t page_flags)
1074 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1075 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1076 struct amdgpu_ttm_tt *gtt;
1077 enum ttm_caching caching;
1079 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
1083 gtt->gobj = &bo->base;
1084 if (adev->gmc.mem_partitions && abo->xcp_id >= 0)
1085 gtt->pool_id = KFD_XCP_MEM_ID(adev, abo->xcp_id);
1087 gtt->pool_id = abo->xcp_id;
1089 if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
1090 caching = ttm_write_combined;
1092 caching = ttm_cached;
1094 /* allocate space for the uninitialized page entries */
1095 if (ttm_sg_tt_init(>t->ttm, bo, page_flags, caching)) {
1103 * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1105 * Map the pages of a ttm_tt object to an address space visible
1106 * to the underlying device.
1108 static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
1110 struct ttm_operation_ctx *ctx)
1112 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1113 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1114 struct ttm_pool *pool;
1118 /* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1120 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
1126 if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
1129 if (adev->mman.ttm_pools && gtt->pool_id >= 0)
1130 pool = &adev->mman.ttm_pools[gtt->pool_id];
1132 pool = &adev->mman.bdev.pool;
1133 ret = ttm_pool_alloc(pool, ttm, ctx);
1137 for (i = 0; i < ttm->num_pages; ++i)
1138 ttm->pages[i]->mapping = bdev->dev_mapping;
1144 * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1146 * Unmaps pages of a ttm_tt object from the device address space and
1147 * unpopulates the page array backing it.
1149 static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
1152 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1153 struct amdgpu_device *adev;
1154 struct ttm_pool *pool;
1157 amdgpu_ttm_backend_unbind(bdev, ttm);
1160 amdgpu_ttm_tt_set_user_pages(ttm, NULL);
1166 if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
1169 for (i = 0; i < ttm->num_pages; ++i)
1170 ttm->pages[i]->mapping = NULL;
1172 adev = amdgpu_ttm_adev(bdev);
1174 if (adev->mman.ttm_pools && gtt->pool_id >= 0)
1175 pool = &adev->mman.ttm_pools[gtt->pool_id];
1177 pool = &adev->mman.bdev.pool;
1179 return ttm_pool_free(pool, ttm);
1183 * amdgpu_ttm_tt_get_userptr - Return the userptr GTT ttm_tt for the current
1186 * @tbo: The ttm_buffer_object that contains the userptr
1187 * @user_addr: The returned value
1189 int amdgpu_ttm_tt_get_userptr(const struct ttm_buffer_object *tbo,
1190 uint64_t *user_addr)
1192 struct amdgpu_ttm_tt *gtt;
1197 gtt = (void *)tbo->ttm;
1198 *user_addr = gtt->userptr;
1203 * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1206 * @bo: The ttm_buffer_object to bind this userptr to
1207 * @addr: The address in the current tasks VM space to use
1208 * @flags: Requirements of userptr object.
1210 * Called by amdgpu_gem_userptr_ioctl() and kfd_ioctl_alloc_memory_of_gpu() to
1211 * bind userptr pages to current task and by kfd_ioctl_acquire_vm() to
1212 * initialize GPU VM for a KFD process.
1214 int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
1215 uint64_t addr, uint32_t flags)
1217 struct amdgpu_ttm_tt *gtt;
1220 /* TODO: We want a separate TTM object type for userptrs */
1221 bo->ttm = amdgpu_ttm_tt_create(bo, 0);
1222 if (bo->ttm == NULL)
1226 /* Set TTM_TT_FLAG_EXTERNAL before populate but after create. */
1227 bo->ttm->page_flags |= TTM_TT_FLAG_EXTERNAL;
1229 gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
1230 gtt->userptr = addr;
1231 gtt->userflags = flags;
1234 put_task_struct(gtt->usertask);
1235 gtt->usertask = current->group_leader;
1236 get_task_struct(gtt->usertask);
1242 * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1244 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1246 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1251 if (gtt->usertask == NULL)
1254 return gtt->usertask->mm;
1258 * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1259 * address range for the current task.
1262 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1263 unsigned long end, unsigned long *userptr)
1265 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1268 if (gtt == NULL || !gtt->userptr)
1271 /* Return false if no part of the ttm_tt object lies within
1274 size = (unsigned long)gtt->ttm.num_pages * PAGE_SIZE;
1275 if (gtt->userptr > end || gtt->userptr + size <= start)
1279 *userptr = gtt->userptr;
1284 * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
1286 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
1288 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1290 if (gtt == NULL || !gtt->userptr)
1297 * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1299 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1301 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1306 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1310 * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object
1312 * @ttm: The ttm_tt object to compute the flags for
1313 * @mem: The memory registry backing this ttm_tt object
1315 * Figure out the flags to use for a VM PDE (Page Directory Entry).
1317 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem)
1321 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1322 flags |= AMDGPU_PTE_VALID;
1324 if (mem && (mem->mem_type == TTM_PL_TT ||
1325 mem->mem_type == AMDGPU_PL_DOORBELL ||
1326 mem->mem_type == AMDGPU_PL_PREEMPT)) {
1327 flags |= AMDGPU_PTE_SYSTEM;
1329 if (ttm->caching == ttm_cached)
1330 flags |= AMDGPU_PTE_SNOOPED;
1333 if (mem && mem->mem_type == TTM_PL_VRAM &&
1334 mem->bus.caching == ttm_cached)
1335 flags |= AMDGPU_PTE_SNOOPED;
1341 * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1343 * @adev: amdgpu_device pointer
1344 * @ttm: The ttm_tt object to compute the flags for
1345 * @mem: The memory registry backing this ttm_tt object
1347 * Figure out the flags to use for a VM PTE (Page Table Entry).
1349 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1350 struct ttm_resource *mem)
1352 uint64_t flags = amdgpu_ttm_tt_pde_flags(ttm, mem);
1354 flags |= adev->gart.gart_pte_flags;
1355 flags |= AMDGPU_PTE_READABLE;
1357 if (!amdgpu_ttm_tt_is_readonly(ttm))
1358 flags |= AMDGPU_PTE_WRITEABLE;
1364 * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1367 * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1368 * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1369 * it can find space for a new object and by ttm_bo_force_list_clean() which is
1370 * used to clean out a memory space.
1372 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1373 const struct ttm_place *place)
1375 struct dma_resv_iter resv_cursor;
1376 struct dma_fence *f;
1378 if (!amdgpu_bo_is_amdgpu_bo(bo))
1379 return ttm_bo_eviction_valuable(bo, place);
1382 if (bo->resource->mem_type == TTM_PL_SYSTEM)
1385 if (bo->type == ttm_bo_type_kernel &&
1386 !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)))
1389 /* If bo is a KFD BO, check if the bo belongs to the current process.
1390 * If true, then return false as any KFD process needs all its BOs to
1391 * be resident to run successfully
1393 dma_resv_for_each_fence(&resv_cursor, bo->base.resv,
1394 DMA_RESV_USAGE_BOOKKEEP, f) {
1395 if (amdkfd_fence_check_mm(f, current->mm))
1399 /* Preemptible BOs don't own system resources managed by the
1400 * driver (pages, VRAM, GART space). They point to resources
1401 * owned by someone else (e.g. pageable memory in user mode
1402 * or a DMABuf). They are used in a preemptible context so we
1403 * can guarantee no deadlocks and good QoS in case of MMU
1404 * notifiers or DMABuf move notifiers from the resource owner.
1406 if (bo->resource->mem_type == AMDGPU_PL_PREEMPT)
1409 if (bo->resource->mem_type == TTM_PL_TT &&
1410 amdgpu_bo_encrypted(ttm_to_amdgpu_bo(bo)))
1413 return ttm_bo_eviction_valuable(bo, place);
1416 static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
1417 void *buf, size_t size, bool write)
1420 uint64_t aligned_pos = ALIGN_DOWN(pos, 4);
1421 uint64_t bytes = 4 - (pos & 0x3);
1422 uint32_t shift = (pos & 0x3) * 8;
1423 uint32_t mask = 0xffffffff << shift;
1427 mask &= 0xffffffff >> (bytes - size) * 8;
1431 if (mask != 0xffffffff) {
1432 amdgpu_device_mm_access(adev, aligned_pos, &value, 4, false);
1435 value |= (*(uint32_t *)buf << shift) & mask;
1436 amdgpu_device_mm_access(adev, aligned_pos, &value, 4, true);
1438 value = (value & mask) >> shift;
1439 memcpy(buf, &value, bytes);
1442 amdgpu_device_mm_access(adev, aligned_pos, buf, 4, write);
1451 static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
1452 unsigned long offset, void *buf,
1455 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1456 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1457 struct amdgpu_res_cursor src_mm;
1458 struct amdgpu_job *job;
1459 struct dma_fence *fence;
1460 uint64_t src_addr, dst_addr;
1461 unsigned int num_dw;
1464 if (len != PAGE_SIZE)
1467 if (!adev->mman.sdma_access_ptr)
1470 if (!drm_dev_enter(adev_to_drm(adev), &idx))
1474 memcpy(adev->mman.sdma_access_ptr, buf, len);
1476 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
1477 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.high_pr,
1478 AMDGPU_FENCE_OWNER_UNDEFINED,
1479 num_dw * 4, AMDGPU_IB_POOL_DELAYED,
1484 amdgpu_res_first(abo->tbo.resource, offset, len, &src_mm);
1485 src_addr = amdgpu_ttm_domain_start(adev, bo->resource->mem_type) +
1487 dst_addr = amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
1489 swap(src_addr, dst_addr);
1491 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
1494 amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
1495 WARN_ON(job->ibs[0].length_dw > num_dw);
1497 fence = amdgpu_job_submit(job);
1499 if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
1501 dma_fence_put(fence);
1504 memcpy(buf, adev->mman.sdma_access_ptr, len);
1511 * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1513 * @bo: The buffer object to read/write
1514 * @offset: Offset into buffer object
1515 * @buf: Secondary buffer to write/read from
1516 * @len: Length in bytes of access
1517 * @write: true if writing
1519 * This is used to access VRAM that backs a buffer object via MMIO
1520 * access for debugging purposes.
1522 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1523 unsigned long offset, void *buf, int len,
1526 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1527 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1528 struct amdgpu_res_cursor cursor;
1531 if (bo->resource->mem_type != TTM_PL_VRAM)
1534 if (amdgpu_device_has_timeouts_enabled(adev) &&
1535 !amdgpu_ttm_access_memory_sdma(bo, offset, buf, len, write))
1538 amdgpu_res_first(bo->resource, offset, len, &cursor);
1539 while (cursor.remaining) {
1540 size_t count, size = cursor.size;
1541 loff_t pos = cursor.start;
1543 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
1546 /* using MM to access rest vram and handle un-aligned address */
1549 amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write);
1554 amdgpu_res_next(&cursor, cursor.size);
1561 amdgpu_bo_delete_mem_notify(struct ttm_buffer_object *bo)
1563 amdgpu_bo_move_notify(bo, false);
1566 static struct ttm_device_funcs amdgpu_bo_driver = {
1567 .ttm_tt_create = &amdgpu_ttm_tt_create,
1568 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1569 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1570 .ttm_tt_destroy = &amdgpu_ttm_backend_destroy,
1571 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1572 .evict_flags = &amdgpu_evict_flags,
1573 .move = &amdgpu_bo_move,
1574 .delete_mem_notify = &amdgpu_bo_delete_mem_notify,
1575 .release_notify = &amdgpu_bo_release_notify,
1576 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1577 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1578 .access_memory = &amdgpu_ttm_access_memory,
1582 * Firmware Reservation functions
1585 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1587 * @adev: amdgpu_device pointer
1589 * free fw reserved vram if it has been reserved.
1591 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1593 amdgpu_bo_free_kernel(&adev->mman.fw_vram_usage_reserved_bo,
1594 NULL, &adev->mman.fw_vram_usage_va);
1598 * Driver Reservation functions
1601 * amdgpu_ttm_drv_reserve_vram_fini - free drv reserved vram
1603 * @adev: amdgpu_device pointer
1605 * free drv reserved vram if it has been reserved.
1607 static void amdgpu_ttm_drv_reserve_vram_fini(struct amdgpu_device *adev)
1609 amdgpu_bo_free_kernel(&adev->mman.drv_vram_usage_reserved_bo,
1611 &adev->mman.drv_vram_usage_va);
1615 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1617 * @adev: amdgpu_device pointer
1619 * create bo vram reservation from fw.
1621 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1623 uint64_t vram_size = adev->gmc.visible_vram_size;
1625 adev->mman.fw_vram_usage_va = NULL;
1626 adev->mman.fw_vram_usage_reserved_bo = NULL;
1628 if (adev->mman.fw_vram_usage_size == 0 ||
1629 adev->mman.fw_vram_usage_size > vram_size)
1632 return amdgpu_bo_create_kernel_at(adev,
1633 adev->mman.fw_vram_usage_start_offset,
1634 adev->mman.fw_vram_usage_size,
1635 &adev->mman.fw_vram_usage_reserved_bo,
1636 &adev->mman.fw_vram_usage_va);
1640 * amdgpu_ttm_drv_reserve_vram_init - create bo vram reservation from driver
1642 * @adev: amdgpu_device pointer
1644 * create bo vram reservation from drv.
1646 static int amdgpu_ttm_drv_reserve_vram_init(struct amdgpu_device *adev)
1648 u64 vram_size = adev->gmc.visible_vram_size;
1650 adev->mman.drv_vram_usage_va = NULL;
1651 adev->mman.drv_vram_usage_reserved_bo = NULL;
1653 if (adev->mman.drv_vram_usage_size == 0 ||
1654 adev->mman.drv_vram_usage_size > vram_size)
1657 return amdgpu_bo_create_kernel_at(adev,
1658 adev->mman.drv_vram_usage_start_offset,
1659 adev->mman.drv_vram_usage_size,
1660 &adev->mman.drv_vram_usage_reserved_bo,
1661 &adev->mman.drv_vram_usage_va);
1665 * Memoy training reservation functions
1669 * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram
1671 * @adev: amdgpu_device pointer
1673 * free memory training reserved vram if it has been reserved.
1675 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev)
1677 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1679 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
1680 amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL);
1686 static void amdgpu_ttm_training_data_block_init(struct amdgpu_device *adev,
1687 uint32_t reserve_size)
1689 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1691 memset(ctx, 0, sizeof(*ctx));
1693 ctx->c2p_train_data_offset =
1694 ALIGN((adev->gmc.mc_vram_size - reserve_size - SZ_1M), SZ_1M);
1695 ctx->p2c_train_data_offset =
1696 (adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET);
1697 ctx->train_data_size =
1698 GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES;
1700 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
1701 ctx->train_data_size,
1702 ctx->p2c_train_data_offset,
1703 ctx->c2p_train_data_offset);
1707 * reserve TMR memory at the top of VRAM which holds
1708 * IP Discovery data and is protected by PSP.
1710 static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
1712 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1713 bool mem_train_support = false;
1714 uint32_t reserve_size = 0;
1717 if (adev->bios && !amdgpu_sriov_vf(adev)) {
1718 if (amdgpu_atomfirmware_mem_training_supported(adev))
1719 mem_train_support = true;
1721 DRM_DEBUG("memory training does not support!\n");
1725 * Query reserved tmr size through atom firmwareinfo for Sienna_Cichlid and onwards for all
1726 * the use cases (IP discovery/G6 memory training/profiling/diagnostic data.etc)
1728 * Otherwise, fallback to legacy approach to check and reserve tmr block for ip
1729 * discovery data and G6 memory training data respectively
1733 amdgpu_atomfirmware_get_fw_reserved_fb_size(adev);
1736 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3))
1737 reserve_size = max(reserve_size, (uint32_t)280 << 20);
1738 else if (!reserve_size)
1739 reserve_size = DISCOVERY_TMR_OFFSET;
1741 if (mem_train_support) {
1742 /* reserve vram for mem train according to TMR location */
1743 amdgpu_ttm_training_data_block_init(adev, reserve_size);
1744 ret = amdgpu_bo_create_kernel_at(adev,
1745 ctx->c2p_train_data_offset,
1746 ctx->train_data_size,
1750 DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
1751 amdgpu_ttm_training_reserve_vram_fini(adev);
1754 ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS;
1757 if (!adev->gmc.is_app_apu) {
1758 ret = amdgpu_bo_create_kernel_at(
1759 adev, adev->gmc.real_vram_size - reserve_size,
1760 reserve_size, &adev->mman.fw_reserved_memory, NULL);
1762 DRM_ERROR("alloc tmr failed(%d)!\n", ret);
1763 amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory,
1768 DRM_DEBUG_DRIVER("backdoor fw loading path for PSP TMR, no reservation needed\n");
1774 static int amdgpu_ttm_pools_init(struct amdgpu_device *adev)
1778 if (!adev->gmc.is_app_apu || !adev->gmc.num_mem_partitions)
1781 adev->mman.ttm_pools = kcalloc(adev->gmc.num_mem_partitions,
1782 sizeof(*adev->mman.ttm_pools),
1784 if (!adev->mman.ttm_pools)
1787 for (i = 0; i < adev->gmc.num_mem_partitions; i++) {
1788 ttm_pool_init(&adev->mman.ttm_pools[i], adev->dev,
1789 adev->gmc.mem_partitions[i].numa.node,
1795 static void amdgpu_ttm_pools_fini(struct amdgpu_device *adev)
1799 if (!adev->gmc.is_app_apu || !adev->mman.ttm_pools)
1802 for (i = 0; i < adev->gmc.num_mem_partitions; i++)
1803 ttm_pool_fini(&adev->mman.ttm_pools[i]);
1805 kfree(adev->mman.ttm_pools);
1806 adev->mman.ttm_pools = NULL;
1810 * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1811 * gtt/vram related fields.
1813 * This initializes all of the memory space pools that the TTM layer
1814 * will need such as the GTT space (system memory mapped to the device),
1815 * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1816 * can be mapped per VMID.
1818 int amdgpu_ttm_init(struct amdgpu_device *adev)
1823 mutex_init(&adev->mman.gtt_window_lock);
1825 /* No others user of address space so set it to 0 */
1826 r = ttm_device_init(&adev->mman.bdev, &amdgpu_bo_driver, adev->dev,
1827 adev_to_drm(adev)->anon_inode->i_mapping,
1828 adev_to_drm(adev)->vma_offset_manager,
1830 dma_addressing_limited(adev->dev));
1832 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1836 r = amdgpu_ttm_pools_init(adev);
1838 DRM_ERROR("failed to init ttm pools(%d).\n", r);
1841 adev->mman.initialized = true;
1843 /* Initialize VRAM pool with all of VRAM divided into pages */
1844 r = amdgpu_vram_mgr_init(adev);
1846 DRM_ERROR("Failed initializing VRAM heap.\n");
1850 /* Change the size here instead of the init above so only lpfn is affected */
1851 amdgpu_ttm_set_buffer_funcs_status(adev, false);
1854 if (adev->gmc.xgmi.connected_to_cpu)
1855 adev->mman.aper_base_kaddr = ioremap_cache(adev->gmc.aper_base,
1856 adev->gmc.visible_vram_size);
1858 else if (adev->gmc.is_app_apu)
1860 "No need to ioremap when real vram size is 0\n");
1863 adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base,
1864 adev->gmc.visible_vram_size);
1868 *The reserved vram for firmware must be pinned to the specified
1869 *place on the VRAM, so reserve it early.
1871 r = amdgpu_ttm_fw_reserve_vram_init(adev);
1876 *The reserved vram for driver must be pinned to the specified
1877 *place on the VRAM, so reserve it early.
1879 r = amdgpu_ttm_drv_reserve_vram_init(adev);
1884 * only NAVI10 and onwards ASIC support for IP discovery.
1885 * If IP discovery enabled, a block of memory should be
1886 * reserved for IP discovey.
1888 if (adev->mman.discovery_bin) {
1889 r = amdgpu_ttm_reserve_tmr(adev);
1894 /* allocate memory as required for VGA
1895 * This is used for VGA emulation and pre-OS scanout buffers to
1896 * avoid display artifacts while transitioning between pre-OS
1899 if (!adev->gmc.is_app_apu) {
1900 r = amdgpu_bo_create_kernel_at(adev, 0,
1901 adev->mman.stolen_vga_size,
1902 &adev->mman.stolen_vga_memory,
1907 r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
1908 adev->mman.stolen_extended_size,
1909 &adev->mman.stolen_extended_memory,
1915 r = amdgpu_bo_create_kernel_at(adev,
1916 adev->mman.stolen_reserved_offset,
1917 adev->mman.stolen_reserved_size,
1918 &adev->mman.stolen_reserved_memory,
1923 DRM_DEBUG_DRIVER("Skipped stolen memory reservation\n");
1926 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1927 (unsigned int)(adev->gmc.real_vram_size / (1024 * 1024)));
1929 /* Compute GTT size, either based on TTM limit
1930 * or whatever the user passed on module init.
1932 if (amdgpu_gtt_size == -1)
1933 gtt_size = ttm_tt_pages_limit() << PAGE_SHIFT;
1935 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1937 /* Initialize GTT memory pool */
1938 r = amdgpu_gtt_mgr_init(adev, gtt_size);
1940 DRM_ERROR("Failed initializing GTT heap.\n");
1943 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1944 (unsigned int)(gtt_size / (1024 * 1024)));
1946 /* Initiailize doorbell pool on PCI BAR */
1947 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_DOORBELL, adev->doorbell.size / PAGE_SIZE);
1949 DRM_ERROR("Failed initializing doorbell heap.\n");
1953 /* Create a boorbell page for kernel usages */
1954 r = amdgpu_doorbell_create_kernel_doorbells(adev);
1956 DRM_ERROR("Failed to initialize kernel doorbells.\n");
1960 /* Initialize preemptible memory pool */
1961 r = amdgpu_preempt_mgr_init(adev);
1963 DRM_ERROR("Failed initializing PREEMPT heap.\n");
1967 /* Initialize various on-chip memory pools */
1968 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GDS, adev->gds.gds_size);
1970 DRM_ERROR("Failed initializing GDS heap.\n");
1974 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GWS, adev->gds.gws_size);
1976 DRM_ERROR("Failed initializing gws heap.\n");
1980 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_OA, adev->gds.oa_size);
1982 DRM_ERROR("Failed initializing oa heap.\n");
1985 if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
1986 AMDGPU_GEM_DOMAIN_GTT,
1987 &adev->mman.sdma_access_bo, NULL,
1988 &adev->mman.sdma_access_ptr))
1989 DRM_WARN("Debug VRAM access will use slowpath MM access\n");
1995 * amdgpu_ttm_fini - De-initialize the TTM memory pools
1997 void amdgpu_ttm_fini(struct amdgpu_device *adev)
2001 if (!adev->mman.initialized)
2004 amdgpu_ttm_pools_fini(adev);
2006 amdgpu_ttm_training_reserve_vram_fini(adev);
2007 /* return the stolen vga memory back to VRAM */
2008 if (!adev->gmc.is_app_apu) {
2009 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
2010 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
2011 /* return the FW reserved memory back to VRAM */
2012 amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory, NULL,
2014 if (adev->mman.stolen_reserved_size)
2015 amdgpu_bo_free_kernel(&adev->mman.stolen_reserved_memory,
2018 amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo, NULL,
2019 &adev->mman.sdma_access_ptr);
2020 amdgpu_ttm_fw_reserve_vram_fini(adev);
2021 amdgpu_ttm_drv_reserve_vram_fini(adev);
2023 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
2025 if (adev->mman.aper_base_kaddr)
2026 iounmap(adev->mman.aper_base_kaddr);
2027 adev->mman.aper_base_kaddr = NULL;
2032 amdgpu_vram_mgr_fini(adev);
2033 amdgpu_gtt_mgr_fini(adev);
2034 amdgpu_preempt_mgr_fini(adev);
2035 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GDS);
2036 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GWS);
2037 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
2038 ttm_device_fini(&adev->mman.bdev);
2039 adev->mman.initialized = false;
2040 DRM_INFO("amdgpu: ttm finalized\n");
2044 * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
2046 * @adev: amdgpu_device pointer
2047 * @enable: true when we can use buffer functions.
2049 * Enable/disable use of buffer functions during suspend/resume. This should
2050 * only be called at bootup or when userspace isn't running.
2052 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
2054 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
2058 if (!adev->mman.initialized || amdgpu_in_reset(adev) ||
2059 adev->mman.buffer_funcs_enabled == enable || adev->gmc.is_app_apu)
2063 struct amdgpu_ring *ring;
2064 struct drm_gpu_scheduler *sched;
2066 ring = adev->mman.buffer_funcs_ring;
2067 sched = &ring->sched;
2068 r = drm_sched_entity_init(&adev->mman.high_pr,
2069 DRM_SCHED_PRIORITY_KERNEL, &sched,
2072 DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
2077 r = drm_sched_entity_init(&adev->mman.low_pr,
2078 DRM_SCHED_PRIORITY_NORMAL, &sched,
2081 DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
2083 goto error_free_entity;
2086 drm_sched_entity_destroy(&adev->mman.high_pr);
2087 drm_sched_entity_destroy(&adev->mman.low_pr);
2088 dma_fence_put(man->move);
2092 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
2094 size = adev->gmc.real_vram_size;
2096 size = adev->gmc.visible_vram_size;
2098 adev->mman.buffer_funcs_enabled = enable;
2103 drm_sched_entity_destroy(&adev->mman.high_pr);
2106 static int amdgpu_ttm_prepare_job(struct amdgpu_device *adev,
2108 unsigned int num_dw,
2109 struct dma_resv *resv,
2110 bool vm_needs_flush,
2111 struct amdgpu_job **job,
2114 enum amdgpu_ib_pool_type pool = direct_submit ?
2115 AMDGPU_IB_POOL_DIRECT :
2116 AMDGPU_IB_POOL_DELAYED;
2118 struct drm_sched_entity *entity = delayed ? &adev->mman.low_pr :
2119 &adev->mman.high_pr;
2120 r = amdgpu_job_alloc_with_ib(adev, entity,
2121 AMDGPU_FENCE_OWNER_UNDEFINED,
2122 num_dw * 4, pool, job);
2126 if (vm_needs_flush) {
2127 (*job)->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gmc.pdb0_bo ?
2130 (*job)->vm_needs_flush = true;
2135 return drm_sched_job_add_resv_dependencies(&(*job)->base, resv,
2136 DMA_RESV_USAGE_BOOKKEEP);
2139 int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
2140 uint64_t dst_offset, uint32_t byte_count,
2141 struct dma_resv *resv,
2142 struct dma_fence **fence, bool direct_submit,
2143 bool vm_needs_flush, bool tmz)
2145 struct amdgpu_device *adev = ring->adev;
2146 unsigned int num_loops, num_dw;
2147 struct amdgpu_job *job;
2152 if (!direct_submit && !ring->sched.ready) {
2153 DRM_ERROR("Trying to move memory with ring turned off.\n");
2157 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
2158 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
2159 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->copy_num_dw, 8);
2160 r = amdgpu_ttm_prepare_job(adev, direct_submit, num_dw,
2161 resv, vm_needs_flush, &job, false);
2165 for (i = 0; i < num_loops; i++) {
2166 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2168 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
2169 dst_offset, cur_size_in_bytes, tmz);
2171 src_offset += cur_size_in_bytes;
2172 dst_offset += cur_size_in_bytes;
2173 byte_count -= cur_size_in_bytes;
2176 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2177 WARN_ON(job->ibs[0].length_dw > num_dw);
2179 r = amdgpu_job_submit_direct(job, ring, fence);
2181 *fence = amdgpu_job_submit(job);
2188 amdgpu_job_free(job);
2189 DRM_ERROR("Error scheduling IBs (%d)\n", r);
2193 static int amdgpu_ttm_fill_mem(struct amdgpu_ring *ring, uint32_t src_data,
2194 uint64_t dst_addr, uint32_t byte_count,
2195 struct dma_resv *resv,
2196 struct dma_fence **fence,
2197 bool vm_needs_flush, bool delayed)
2199 struct amdgpu_device *adev = ring->adev;
2200 unsigned int num_loops, num_dw;
2201 struct amdgpu_job *job;
2206 max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
2207 num_loops = DIV_ROUND_UP_ULL(byte_count, max_bytes);
2208 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->fill_num_dw, 8);
2209 r = amdgpu_ttm_prepare_job(adev, false, num_dw, resv, vm_needs_flush,
2214 for (i = 0; i < num_loops; i++) {
2215 uint32_t cur_size = min(byte_count, max_bytes);
2217 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data, dst_addr,
2220 dst_addr += cur_size;
2221 byte_count -= cur_size;
2224 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2225 WARN_ON(job->ibs[0].length_dw > num_dw);
2226 *fence = amdgpu_job_submit(job);
2231 * amdgpu_ttm_clear_buffer - clear memory buffers
2232 * @bo: amdgpu buffer object
2233 * @resv: reservation object
2234 * @fence: dma_fence associated with the operation
2236 * Clear the memory buffer resource.
2239 * 0 for success or a negative error code on failure.
2241 int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo,
2242 struct dma_resv *resv,
2243 struct dma_fence **fence)
2245 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2246 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2247 struct amdgpu_res_cursor cursor;
2251 if (!adev->mman.buffer_funcs_enabled)
2257 *fence = dma_fence_get_stub();
2259 amdgpu_res_first(bo->tbo.resource, 0, amdgpu_bo_size(bo), &cursor);
2261 mutex_lock(&adev->mman.gtt_window_lock);
2262 while (cursor.remaining) {
2263 struct dma_fence *next = NULL;
2266 if (amdgpu_res_cleared(&cursor)) {
2267 amdgpu_res_next(&cursor, cursor.size);
2271 /* Never clear more than 256MiB at once to avoid timeouts */
2272 size = min(cursor.size, 256ULL << 20);
2274 r = amdgpu_ttm_map_buffer(&bo->tbo, bo->tbo.resource, &cursor,
2275 1, ring, false, &size, &addr);
2279 r = amdgpu_ttm_fill_mem(ring, 0, addr, size, resv,
2284 dma_fence_put(*fence);
2287 amdgpu_res_next(&cursor, size);
2290 mutex_unlock(&adev->mman.gtt_window_lock);
2295 int amdgpu_fill_buffer(struct amdgpu_bo *bo,
2297 struct dma_resv *resv,
2298 struct dma_fence **f,
2301 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2302 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2303 struct dma_fence *fence = NULL;
2304 struct amdgpu_res_cursor dst;
2307 if (!adev->mman.buffer_funcs_enabled) {
2308 DRM_ERROR("Trying to clear memory with ring turned off.\n");
2312 amdgpu_res_first(bo->tbo.resource, 0, amdgpu_bo_size(bo), &dst);
2314 mutex_lock(&adev->mman.gtt_window_lock);
2315 while (dst.remaining) {
2316 struct dma_fence *next;
2317 uint64_t cur_size, to;
2319 /* Never fill more than 256MiB at once to avoid timeouts */
2320 cur_size = min(dst.size, 256ULL << 20);
2322 r = amdgpu_ttm_map_buffer(&bo->tbo, bo->tbo.resource, &dst,
2323 1, ring, false, &cur_size, &to);
2327 r = amdgpu_ttm_fill_mem(ring, src_data, to, cur_size, resv,
2328 &next, true, delayed);
2332 dma_fence_put(fence);
2335 amdgpu_res_next(&dst, cur_size);
2338 mutex_unlock(&adev->mman.gtt_window_lock);
2340 *f = dma_fence_get(fence);
2341 dma_fence_put(fence);
2346 * amdgpu_ttm_evict_resources - evict memory buffers
2347 * @adev: amdgpu device object
2348 * @mem_type: evicted BO's memory type
2350 * Evicts all @mem_type buffers on the lru list of the memory type.
2353 * 0 for success or a negative error code on failure.
2355 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type)
2357 struct ttm_resource_manager *man;
2365 man = ttm_manager_type(&adev->mman.bdev, mem_type);
2368 DRM_ERROR("Trying to evict invalid memory type\n");
2372 return ttm_resource_manager_evict_all(&adev->mman.bdev, man);
2375 #if defined(CONFIG_DEBUG_FS)
2377 static int amdgpu_ttm_page_pool_show(struct seq_file *m, void *unused)
2379 struct amdgpu_device *adev = m->private;
2381 return ttm_pool_debugfs(&adev->mman.bdev.pool, m);
2384 DEFINE_SHOW_ATTRIBUTE(amdgpu_ttm_page_pool);
2387 * amdgpu_ttm_vram_read - Linear read access to VRAM
2389 * Accesses VRAM via MMIO for debugging purposes.
2391 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
2392 size_t size, loff_t *pos)
2394 struct amdgpu_device *adev = file_inode(f)->i_private;
2397 if (size & 0x3 || *pos & 0x3)
2400 if (*pos >= adev->gmc.mc_vram_size)
2403 size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos));
2405 size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4);
2406 uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ];
2408 amdgpu_device_vram_access(adev, *pos, value, bytes, false);
2409 if (copy_to_user(buf, value, bytes))
2422 * amdgpu_ttm_vram_write - Linear write access to VRAM
2424 * Accesses VRAM via MMIO for debugging purposes.
2426 static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
2427 size_t size, loff_t *pos)
2429 struct amdgpu_device *adev = file_inode(f)->i_private;
2433 if (size & 0x3 || *pos & 0x3)
2436 if (*pos >= adev->gmc.mc_vram_size)
2442 if (*pos >= adev->gmc.mc_vram_size)
2445 r = get_user(value, (uint32_t *)buf);
2449 amdgpu_device_mm_access(adev, *pos, &value, 4, true);
2460 static const struct file_operations amdgpu_ttm_vram_fops = {
2461 .owner = THIS_MODULE,
2462 .read = amdgpu_ttm_vram_read,
2463 .write = amdgpu_ttm_vram_write,
2464 .llseek = default_llseek,
2468 * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2470 * This function is used to read memory that has been mapped to the
2471 * GPU and the known addresses are not physical addresses but instead
2472 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2474 static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
2475 size_t size, loff_t *pos)
2477 struct amdgpu_device *adev = file_inode(f)->i_private;
2478 struct iommu_domain *dom;
2482 /* retrieve the IOMMU domain if any for this device */
2483 dom = iommu_get_domain_for_dev(adev->dev);
2486 phys_addr_t addr = *pos & PAGE_MASK;
2487 loff_t off = *pos & ~PAGE_MASK;
2488 size_t bytes = PAGE_SIZE - off;
2493 bytes = min(bytes, size);
2495 /* Translate the bus address to a physical address. If
2496 * the domain is NULL it means there is no IOMMU active
2497 * and the address translation is the identity
2499 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2501 pfn = addr >> PAGE_SHIFT;
2502 if (!pfn_valid(pfn))
2505 p = pfn_to_page(pfn);
2506 if (p->mapping != adev->mman.bdev.dev_mapping)
2509 ptr = kmap_local_page(p);
2510 r = copy_to_user(buf, ptr + off, bytes);
2524 * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2526 * This function is used to write memory that has been mapped to the
2527 * GPU and the known addresses are not physical addresses but instead
2528 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2530 static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
2531 size_t size, loff_t *pos)
2533 struct amdgpu_device *adev = file_inode(f)->i_private;
2534 struct iommu_domain *dom;
2538 dom = iommu_get_domain_for_dev(adev->dev);
2541 phys_addr_t addr = *pos & PAGE_MASK;
2542 loff_t off = *pos & ~PAGE_MASK;
2543 size_t bytes = PAGE_SIZE - off;
2548 bytes = min(bytes, size);
2550 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2552 pfn = addr >> PAGE_SHIFT;
2553 if (!pfn_valid(pfn))
2556 p = pfn_to_page(pfn);
2557 if (p->mapping != adev->mman.bdev.dev_mapping)
2560 ptr = kmap_local_page(p);
2561 r = copy_from_user(ptr + off, buf, bytes);
2574 static const struct file_operations amdgpu_ttm_iomem_fops = {
2575 .owner = THIS_MODULE,
2576 .read = amdgpu_iomem_read,
2577 .write = amdgpu_iomem_write,
2578 .llseek = default_llseek
2583 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
2585 #if defined(CONFIG_DEBUG_FS)
2586 struct drm_minor *minor = adev_to_drm(adev)->primary;
2587 struct dentry *root = minor->debugfs_root;
2589 debugfs_create_file_size("amdgpu_vram", 0444, root, adev,
2590 &amdgpu_ttm_vram_fops, adev->gmc.mc_vram_size);
2591 debugfs_create_file("amdgpu_iomem", 0444, root, adev,
2592 &amdgpu_ttm_iomem_fops);
2593 debugfs_create_file("ttm_page_pool", 0444, root, adev,
2594 &amdgpu_ttm_page_pool_fops);
2595 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2597 root, "amdgpu_vram_mm");
2598 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2600 root, "amdgpu_gtt_mm");
2601 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2603 root, "amdgpu_gds_mm");
2604 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2606 root, "amdgpu_gws_mm");
2607 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2609 root, "amdgpu_oa_mm");