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/swiotlb.h>
42 #include <linux/dma-buf.h>
43 #include <linux/sizes.h>
44 #include <linux/module.h>
46 #include <drm/drm_drv.h>
47 #include <drm/ttm/ttm_bo_api.h>
48 #include <drm/ttm/ttm_bo_driver.h>
49 #include <drm/ttm/ttm_placement.h>
50 #include <drm/ttm/ttm_range_manager.h>
52 #include <drm/amdgpu_drm.h>
53 #include <drm/drm_drv.h>
56 #include "amdgpu_object.h"
57 #include "amdgpu_trace.h"
58 #include "amdgpu_amdkfd.h"
59 #include "amdgpu_sdma.h"
60 #include "amdgpu_ras.h"
61 #include "amdgpu_atomfirmware.h"
62 #include "amdgpu_res_cursor.h"
63 #include "bif/bif_4_1_d.h"
65 MODULE_IMPORT_NS(DMA_BUF);
67 #define AMDGPU_TTM_VRAM_MAX_DW_READ (size_t)128
69 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
71 struct ttm_resource *bo_mem);
72 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
75 static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
77 uint64_t size_in_page)
79 return ttm_range_man_init(&adev->mman.bdev, type,
84 * amdgpu_evict_flags - Compute placement flags
86 * @bo: The buffer object to evict
87 * @placement: Possible destination(s) for evicted BO
89 * Fill in placement data when ttm_bo_evict() is called
91 static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
92 struct ttm_placement *placement)
94 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
95 struct amdgpu_bo *abo;
96 static const struct ttm_place placements = {
99 .mem_type = TTM_PL_SYSTEM,
103 /* Don't handle scatter gather BOs */
104 if (bo->type == ttm_bo_type_sg) {
105 placement->num_placement = 0;
106 placement->num_busy_placement = 0;
110 /* Object isn't an AMDGPU object so ignore */
111 if (!amdgpu_bo_is_amdgpu_bo(bo)) {
112 placement->placement = &placements;
113 placement->busy_placement = &placements;
114 placement->num_placement = 1;
115 placement->num_busy_placement = 1;
119 abo = ttm_to_amdgpu_bo(bo);
120 if (abo->flags & AMDGPU_GEM_CREATE_DISCARDABLE) {
121 placement->num_placement = 0;
122 placement->num_busy_placement = 0;
126 switch (bo->resource->mem_type) {
130 placement->num_placement = 0;
131 placement->num_busy_placement = 0;
135 if (!adev->mman.buffer_funcs_enabled) {
136 /* Move to system memory */
137 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
138 } else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
139 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
140 amdgpu_bo_in_cpu_visible_vram(abo)) {
142 /* Try evicting to the CPU inaccessible part of VRAM
143 * first, but only set GTT as busy placement, so this
144 * BO will be evicted to GTT rather than causing other
145 * BOs to be evicted from VRAM
147 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
148 AMDGPU_GEM_DOMAIN_GTT |
149 AMDGPU_GEM_DOMAIN_CPU);
150 abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
151 abo->placements[0].lpfn = 0;
152 abo->placement.busy_placement = &abo->placements[1];
153 abo->placement.num_busy_placement = 1;
155 /* Move to GTT memory */
156 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT |
157 AMDGPU_GEM_DOMAIN_CPU);
161 case AMDGPU_PL_PREEMPT:
163 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
166 *placement = abo->placement;
170 * amdgpu_ttm_map_buffer - Map memory into the GART windows
171 * @bo: buffer object to map
172 * @mem: memory object to map
173 * @mm_cur: range to map
174 * @window: which GART window to use
175 * @ring: DMA ring to use for the copy
176 * @tmz: if we should setup a TMZ enabled mapping
177 * @size: in number of bytes to map, out number of bytes mapped
178 * @addr: resulting address inside the MC address space
180 * Setup one of the GART windows to access a specific piece of memory or return
181 * the physical address for local memory.
183 static int amdgpu_ttm_map_buffer(struct ttm_buffer_object *bo,
184 struct ttm_resource *mem,
185 struct amdgpu_res_cursor *mm_cur,
186 unsigned window, struct amdgpu_ring *ring,
187 bool tmz, uint64_t *size, uint64_t *addr)
189 struct amdgpu_device *adev = ring->adev;
190 unsigned offset, num_pages, num_dw, num_bytes;
191 uint64_t src_addr, dst_addr;
192 struct amdgpu_job *job;
198 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
199 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
201 if (WARN_ON(mem->mem_type == AMDGPU_PL_PREEMPT))
204 /* Map only what can't be accessed directly */
205 if (!tmz && mem->start != AMDGPU_BO_INVALID_OFFSET) {
206 *addr = amdgpu_ttm_domain_start(adev, mem->mem_type) +
213 * If start begins at an offset inside the page, then adjust the size
214 * and addr accordingly
216 offset = mm_cur->start & ~PAGE_MASK;
218 num_pages = PFN_UP(*size + offset);
219 num_pages = min_t(uint32_t, num_pages, AMDGPU_GTT_MAX_TRANSFER_SIZE);
221 *size = min(*size, (uint64_t)num_pages * PAGE_SIZE - offset);
223 *addr = adev->gmc.gart_start;
224 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
225 AMDGPU_GPU_PAGE_SIZE;
228 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
229 num_bytes = num_pages * 8 * AMDGPU_GPU_PAGES_IN_CPU_PAGE;
231 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.entity,
232 AMDGPU_FENCE_OWNER_UNDEFINED,
233 num_dw * 4 + num_bytes,
234 AMDGPU_IB_POOL_DELAYED, &job);
238 src_addr = num_dw * 4;
239 src_addr += job->ibs[0].gpu_addr;
241 dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
242 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
243 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
244 dst_addr, num_bytes, false);
246 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
247 WARN_ON(job->ibs[0].length_dw > num_dw);
249 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, mem);
251 flags |= AMDGPU_PTE_TMZ;
253 cpu_addr = &job->ibs[0].ptr[num_dw];
255 if (mem->mem_type == TTM_PL_TT) {
256 dma_addr_t *dma_addr;
258 dma_addr = &bo->ttm->dma_address[mm_cur->start >> PAGE_SHIFT];
259 amdgpu_gart_map(adev, 0, num_pages, dma_addr, flags, cpu_addr);
261 dma_addr_t dma_address;
263 dma_address = mm_cur->start;
264 dma_address += adev->vm_manager.vram_base_offset;
266 for (i = 0; i < num_pages; ++i) {
267 amdgpu_gart_map(adev, i << PAGE_SHIFT, 1, &dma_address,
269 dma_address += PAGE_SIZE;
273 dma_fence_put(amdgpu_job_submit(job));
278 * amdgpu_ttm_copy_mem_to_mem - Helper function for copy
279 * @adev: amdgpu device
280 * @src: buffer/address where to read from
281 * @dst: buffer/address where to write to
282 * @size: number of bytes to copy
283 * @tmz: if a secure copy should be used
284 * @resv: resv object to sync to
285 * @f: Returns the last fence if multiple jobs are submitted.
287 * The function copies @size bytes from {src->mem + src->offset} to
288 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
289 * move and different for a BO to BO copy.
292 int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
293 const struct amdgpu_copy_mem *src,
294 const struct amdgpu_copy_mem *dst,
295 uint64_t size, bool tmz,
296 struct dma_resv *resv,
297 struct dma_fence **f)
299 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
300 struct amdgpu_res_cursor src_mm, dst_mm;
301 struct dma_fence *fence = NULL;
304 if (!adev->mman.buffer_funcs_enabled) {
305 DRM_ERROR("Trying to move memory with ring turned off.\n");
309 amdgpu_res_first(src->mem, src->offset, size, &src_mm);
310 amdgpu_res_first(dst->mem, dst->offset, size, &dst_mm);
312 mutex_lock(&adev->mman.gtt_window_lock);
313 while (src_mm.remaining) {
314 uint64_t from, to, cur_size;
315 struct dma_fence *next;
317 /* Never copy more than 256MiB at once to avoid a timeout */
318 cur_size = min3(src_mm.size, dst_mm.size, 256ULL << 20);
320 /* Map src to window 0 and dst to window 1. */
321 r = amdgpu_ttm_map_buffer(src->bo, src->mem, &src_mm,
322 0, ring, tmz, &cur_size, &from);
326 r = amdgpu_ttm_map_buffer(dst->bo, dst->mem, &dst_mm,
327 1, ring, tmz, &cur_size, &to);
331 r = amdgpu_copy_buffer(ring, from, to, cur_size,
332 resv, &next, false, true, tmz);
336 dma_fence_put(fence);
339 amdgpu_res_next(&src_mm, cur_size);
340 amdgpu_res_next(&dst_mm, cur_size);
343 mutex_unlock(&adev->mman.gtt_window_lock);
345 *f = dma_fence_get(fence);
346 dma_fence_put(fence);
351 * amdgpu_move_blit - Copy an entire buffer to another buffer
353 * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
354 * help move buffers to and from VRAM.
356 static int amdgpu_move_blit(struct ttm_buffer_object *bo,
358 struct ttm_resource *new_mem,
359 struct ttm_resource *old_mem)
361 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
362 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
363 struct amdgpu_copy_mem src, dst;
364 struct dma_fence *fence = NULL;
374 r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
376 amdgpu_bo_encrypted(abo),
377 bo->base.resv, &fence);
381 /* clear the space being freed */
382 if (old_mem->mem_type == TTM_PL_VRAM &&
383 (abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)) {
384 struct dma_fence *wipe_fence = NULL;
386 r = amdgpu_fill_buffer(abo, AMDGPU_POISON, NULL, &wipe_fence);
389 } else if (wipe_fence) {
390 dma_fence_put(fence);
395 /* Always block for VM page tables before committing the new location */
396 if (bo->type == ttm_bo_type_kernel)
397 r = ttm_bo_move_accel_cleanup(bo, fence, true, false, new_mem);
399 r = ttm_bo_move_accel_cleanup(bo, fence, evict, true, new_mem);
400 dma_fence_put(fence);
405 dma_fence_wait(fence, false);
406 dma_fence_put(fence);
411 * amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
413 * Called by amdgpu_bo_move()
415 static bool amdgpu_mem_visible(struct amdgpu_device *adev,
416 struct ttm_resource *mem)
418 u64 mem_size = (u64)mem->size;
419 struct amdgpu_res_cursor cursor;
422 if (mem->mem_type == TTM_PL_SYSTEM ||
423 mem->mem_type == TTM_PL_TT)
425 if (mem->mem_type != TTM_PL_VRAM)
428 amdgpu_res_first(mem, 0, mem_size, &cursor);
429 end = cursor.start + cursor.size;
430 while (cursor.remaining) {
431 amdgpu_res_next(&cursor, cursor.size);
433 if (!cursor.remaining)
436 /* ttm_resource_ioremap only supports contiguous memory */
437 if (end != cursor.start)
440 end = cursor.start + cursor.size;
443 return end <= adev->gmc.visible_vram_size;
447 * amdgpu_bo_move - Move a buffer object to a new memory location
449 * Called by ttm_bo_handle_move_mem()
451 static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
452 struct ttm_operation_ctx *ctx,
453 struct ttm_resource *new_mem,
454 struct ttm_place *hop)
456 struct amdgpu_device *adev;
457 struct amdgpu_bo *abo;
458 struct ttm_resource *old_mem = bo->resource;
461 if (new_mem->mem_type == TTM_PL_TT ||
462 new_mem->mem_type == AMDGPU_PL_PREEMPT) {
463 r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, new_mem);
468 /* Can't move a pinned BO */
469 abo = ttm_to_amdgpu_bo(bo);
470 if (WARN_ON_ONCE(abo->tbo.pin_count > 0))
473 adev = amdgpu_ttm_adev(bo->bdev);
475 if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM &&
477 ttm_bo_move_null(bo, new_mem);
480 if (old_mem->mem_type == TTM_PL_SYSTEM &&
481 (new_mem->mem_type == TTM_PL_TT ||
482 new_mem->mem_type == AMDGPU_PL_PREEMPT)) {
483 ttm_bo_move_null(bo, new_mem);
486 if ((old_mem->mem_type == TTM_PL_TT ||
487 old_mem->mem_type == AMDGPU_PL_PREEMPT) &&
488 new_mem->mem_type == TTM_PL_SYSTEM) {
489 r = ttm_bo_wait_ctx(bo, ctx);
493 amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
494 ttm_resource_free(bo, &bo->resource);
495 ttm_bo_assign_mem(bo, new_mem);
499 if (old_mem->mem_type == AMDGPU_PL_GDS ||
500 old_mem->mem_type == AMDGPU_PL_GWS ||
501 old_mem->mem_type == AMDGPU_PL_OA ||
502 new_mem->mem_type == AMDGPU_PL_GDS ||
503 new_mem->mem_type == AMDGPU_PL_GWS ||
504 new_mem->mem_type == AMDGPU_PL_OA) {
505 /* Nothing to save here */
506 ttm_bo_move_null(bo, new_mem);
510 if (bo->type == ttm_bo_type_device &&
511 new_mem->mem_type == TTM_PL_VRAM &&
512 old_mem->mem_type != TTM_PL_VRAM) {
513 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
514 * accesses the BO after it's moved.
516 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
519 if (adev->mman.buffer_funcs_enabled) {
520 if (((old_mem->mem_type == TTM_PL_SYSTEM &&
521 new_mem->mem_type == TTM_PL_VRAM) ||
522 (old_mem->mem_type == TTM_PL_VRAM &&
523 new_mem->mem_type == TTM_PL_SYSTEM))) {
526 hop->mem_type = TTM_PL_TT;
527 hop->flags = TTM_PL_FLAG_TEMPORARY;
531 r = amdgpu_move_blit(bo, evict, new_mem, old_mem);
537 /* Check that all memory is CPU accessible */
538 if (!amdgpu_mem_visible(adev, old_mem) ||
539 !amdgpu_mem_visible(adev, new_mem)) {
540 pr_err("Move buffer fallback to memcpy unavailable\n");
544 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
550 /* update statistics */
551 atomic64_add(bo->base.size, &adev->num_bytes_moved);
552 amdgpu_bo_move_notify(bo, evict, new_mem);
557 * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
559 * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
561 static int amdgpu_ttm_io_mem_reserve(struct ttm_device *bdev,
562 struct ttm_resource *mem)
564 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
565 size_t bus_size = (size_t)mem->size;
567 switch (mem->mem_type) {
572 case AMDGPU_PL_PREEMPT:
575 mem->bus.offset = mem->start << PAGE_SHIFT;
576 /* check if it's visible */
577 if ((mem->bus.offset + bus_size) > adev->gmc.visible_vram_size)
580 if (adev->mman.aper_base_kaddr &&
581 mem->placement & TTM_PL_FLAG_CONTIGUOUS)
582 mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr +
585 mem->bus.offset += adev->gmc.aper_base;
586 mem->bus.is_iomem = true;
594 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
595 unsigned long page_offset)
597 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
598 struct amdgpu_res_cursor cursor;
600 amdgpu_res_first(bo->resource, (u64)page_offset << PAGE_SHIFT, 0,
602 return (adev->gmc.aper_base + cursor.start) >> PAGE_SHIFT;
606 * amdgpu_ttm_domain_start - Returns GPU start address
607 * @adev: amdgpu device object
608 * @type: type of the memory
611 * GPU start address of a memory domain
614 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type)
618 return adev->gmc.gart_start;
620 return adev->gmc.vram_start;
627 * TTM backend functions.
629 struct amdgpu_ttm_tt {
631 struct drm_gem_object *gobj;
634 struct task_struct *usertask;
637 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
638 struct hmm_range *range;
642 #define ttm_to_amdgpu_ttm_tt(ptr) container_of(ptr, struct amdgpu_ttm_tt, ttm)
644 #ifdef CONFIG_DRM_AMDGPU_USERPTR
646 * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user
647 * memory and start HMM tracking CPU page table update
649 * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only
650 * once afterwards to stop HMM tracking
652 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages)
654 struct ttm_tt *ttm = bo->tbo.ttm;
655 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
656 unsigned long start = gtt->userptr;
657 struct vm_area_struct *vma;
658 struct mm_struct *mm;
662 mm = bo->notifier.mm;
664 DRM_DEBUG_DRIVER("BO is not registered?\n");
668 /* Another get_user_pages is running at the same time?? */
669 if (WARN_ON(gtt->range))
672 if (!mmget_not_zero(mm)) /* Happens during process shutdown */
676 vma = vma_lookup(mm, start);
677 if (unlikely(!vma)) {
681 if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) &&
687 readonly = amdgpu_ttm_tt_is_readonly(ttm);
688 r = amdgpu_hmm_range_get_pages(&bo->notifier, mm, pages, start,
689 ttm->num_pages, >t->range, readonly,
692 mmap_read_unlock(mm);
694 pr_debug("failed %d to get user pages 0x%lx\n", r, start);
702 * amdgpu_ttm_tt_userptr_range_done - stop HMM track the CPU page table change
703 * Check if the pages backing this ttm range have been invalidated
705 * Returns: true if pages are still valid
707 bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm)
709 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
712 if (!gtt || !gtt->userptr)
715 DRM_DEBUG_DRIVER("user_pages_done 0x%llx pages 0x%x\n",
716 gtt->userptr, ttm->num_pages);
718 WARN_ONCE(!gtt->range || !gtt->range->hmm_pfns,
719 "No user pages to check\n");
723 * FIXME: Must always hold notifier_lock for this, and must
724 * not ignore the return code.
726 r = amdgpu_hmm_range_get_pages_done(gtt->range);
735 * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
737 * Called by amdgpu_cs_list_validate(). This creates the page list
738 * that backs user memory and will ultimately be mapped into the device
741 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
745 for (i = 0; i < ttm->num_pages; ++i)
746 ttm->pages[i] = pages ? pages[i] : NULL;
750 * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages
752 * Called by amdgpu_ttm_backend_bind()
754 static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev,
757 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
758 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
759 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
760 enum dma_data_direction direction = write ?
761 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
764 /* Allocate an SG array and squash pages into it */
765 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
766 (u64)ttm->num_pages << PAGE_SHIFT,
771 /* Map SG to device */
772 r = dma_map_sgtable(adev->dev, ttm->sg, direction, 0);
776 /* convert SG to linear array of pages and dma addresses */
777 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
789 * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
791 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_device *bdev,
794 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
795 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
796 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
797 enum dma_data_direction direction = write ?
798 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
800 /* double check that we don't free the table twice */
801 if (!ttm->sg || !ttm->sg->sgl)
804 /* unmap the pages mapped to the device */
805 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
806 sg_free_table(ttm->sg);
808 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
812 for (i = 0; i < ttm->num_pages; i++) {
814 hmm_pfn_to_page(gtt->range->hmm_pfns[i]))
818 WARN((i == ttm->num_pages), "Missing get_user_page_done\n");
823 static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
824 struct ttm_buffer_object *tbo,
827 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo);
828 struct ttm_tt *ttm = tbo->ttm;
829 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
831 if (amdgpu_bo_encrypted(abo))
832 flags |= AMDGPU_PTE_TMZ;
834 if (abo->flags & AMDGPU_GEM_CREATE_CP_MQD_GFX9) {
835 uint64_t page_idx = 1;
837 amdgpu_gart_bind(adev, gtt->offset, page_idx,
838 gtt->ttm.dma_address, flags);
840 /* The memory type of the first page defaults to UC. Now
841 * modify the memory type to NC from the second page of
844 flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
845 flags |= AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC);
847 amdgpu_gart_bind(adev, gtt->offset + (page_idx << PAGE_SHIFT),
848 ttm->num_pages - page_idx,
849 &(gtt->ttm.dma_address[page_idx]), flags);
851 amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
852 gtt->ttm.dma_address, flags);
857 * amdgpu_ttm_backend_bind - Bind GTT memory
859 * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
860 * This handles binding GTT memory to the device address space.
862 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
864 struct ttm_resource *bo_mem)
866 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
867 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
878 r = amdgpu_ttm_tt_pin_userptr(bdev, ttm);
880 DRM_ERROR("failed to pin userptr\n");
883 } else if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) {
885 struct dma_buf_attachment *attach;
886 struct sg_table *sgt;
888 attach = gtt->gobj->import_attach;
889 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
896 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
900 if (!ttm->num_pages) {
901 WARN(1, "nothing to bind %u pages for mreg %p back %p!\n",
902 ttm->num_pages, bo_mem, ttm);
905 if (bo_mem->mem_type != TTM_PL_TT ||
906 !amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
907 gtt->offset = AMDGPU_BO_INVALID_OFFSET;
911 /* compute PTE flags relevant to this BO memory */
912 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem);
914 /* bind pages into GART page tables */
915 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
916 amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
917 gtt->ttm.dma_address, flags);
923 * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
924 * through AGP or GART aperture.
926 * If bo is accessible through AGP aperture, then use AGP aperture
927 * to access bo; otherwise allocate logical space in GART aperture
928 * and map bo to GART aperture.
930 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
932 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
933 struct ttm_operation_ctx ctx = { false, false };
934 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
935 struct ttm_placement placement;
936 struct ttm_place placements;
937 struct ttm_resource *tmp;
938 uint64_t addr, flags;
941 if (bo->resource->start != AMDGPU_BO_INVALID_OFFSET)
944 addr = amdgpu_gmc_agp_addr(bo);
945 if (addr != AMDGPU_BO_INVALID_OFFSET) {
946 bo->resource->start = addr >> PAGE_SHIFT;
950 /* allocate GART space */
951 placement.num_placement = 1;
952 placement.placement = &placements;
953 placement.num_busy_placement = 1;
954 placement.busy_placement = &placements;
956 placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
957 placements.mem_type = TTM_PL_TT;
958 placements.flags = bo->resource->placement;
960 r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
964 /* compute PTE flags for this buffer object */
965 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, tmp);
968 gtt->offset = (u64)tmp->start << PAGE_SHIFT;
969 amdgpu_ttm_gart_bind(adev, bo, flags);
970 amdgpu_gart_invalidate_tlb(adev);
971 ttm_resource_free(bo, &bo->resource);
972 ttm_bo_assign_mem(bo, tmp);
978 * amdgpu_ttm_recover_gart - Rebind GTT pages
980 * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
981 * rebind GTT pages during a GPU reset.
983 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
985 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
991 flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, tbo->resource);
992 amdgpu_ttm_gart_bind(adev, tbo, flags);
996 * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
998 * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
1001 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
1004 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1005 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1007 /* if the pages have userptr pinning then clear that first */
1009 amdgpu_ttm_tt_unpin_userptr(bdev, ttm);
1010 } else if (ttm->sg && gtt->gobj->import_attach) {
1011 struct dma_buf_attachment *attach;
1013 attach = gtt->gobj->import_attach;
1014 dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
1021 if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
1024 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1025 amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages);
1029 static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev,
1032 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1035 put_task_struct(gtt->usertask);
1037 ttm_tt_fini(>t->ttm);
1042 * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1044 * @bo: The buffer object to create a GTT ttm_tt object around
1045 * @page_flags: Page flags to be added to the ttm_tt object
1047 * Called by ttm_tt_create().
1049 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
1050 uint32_t page_flags)
1052 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1053 struct amdgpu_ttm_tt *gtt;
1054 enum ttm_caching caching;
1056 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
1060 gtt->gobj = &bo->base;
1062 if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
1063 caching = ttm_write_combined;
1065 caching = ttm_cached;
1067 /* allocate space for the uninitialized page entries */
1068 if (ttm_sg_tt_init(>t->ttm, bo, page_flags, caching)) {
1076 * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1078 * Map the pages of a ttm_tt object to an address space visible
1079 * to the underlying device.
1081 static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
1083 struct ttm_operation_ctx *ctx)
1085 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1086 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1090 /* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1092 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
1098 if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
1101 ret = ttm_pool_alloc(&adev->mman.bdev.pool, ttm, ctx);
1105 for (i = 0; i < ttm->num_pages; ++i)
1106 ttm->pages[i]->mapping = bdev->dev_mapping;
1112 * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1114 * Unmaps pages of a ttm_tt object from the device address space and
1115 * unpopulates the page array backing it.
1117 static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
1120 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1121 struct amdgpu_device *adev;
1124 amdgpu_ttm_backend_unbind(bdev, ttm);
1127 amdgpu_ttm_tt_set_user_pages(ttm, NULL);
1133 if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
1136 for (i = 0; i < ttm->num_pages; ++i)
1137 ttm->pages[i]->mapping = NULL;
1139 adev = amdgpu_ttm_adev(bdev);
1140 return ttm_pool_free(&adev->mman.bdev.pool, ttm);
1144 * amdgpu_ttm_tt_get_userptr - Return the userptr GTT ttm_tt for the current
1147 * @tbo: The ttm_buffer_object that contains the userptr
1148 * @user_addr: The returned value
1150 int amdgpu_ttm_tt_get_userptr(const struct ttm_buffer_object *tbo,
1151 uint64_t *user_addr)
1153 struct amdgpu_ttm_tt *gtt;
1158 gtt = (void *)tbo->ttm;
1159 *user_addr = gtt->userptr;
1164 * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1167 * @bo: The ttm_buffer_object to bind this userptr to
1168 * @addr: The address in the current tasks VM space to use
1169 * @flags: Requirements of userptr object.
1171 * Called by amdgpu_gem_userptr_ioctl() to bind userptr pages
1174 int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
1175 uint64_t addr, uint32_t flags)
1177 struct amdgpu_ttm_tt *gtt;
1180 /* TODO: We want a separate TTM object type for userptrs */
1181 bo->ttm = amdgpu_ttm_tt_create(bo, 0);
1182 if (bo->ttm == NULL)
1186 /* Set TTM_TT_FLAG_EXTERNAL before populate but after create. */
1187 bo->ttm->page_flags |= TTM_TT_FLAG_EXTERNAL;
1189 gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
1190 gtt->userptr = addr;
1191 gtt->userflags = flags;
1194 put_task_struct(gtt->usertask);
1195 gtt->usertask = current->group_leader;
1196 get_task_struct(gtt->usertask);
1202 * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1204 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1206 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1211 if (gtt->usertask == NULL)
1214 return gtt->usertask->mm;
1218 * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1219 * address range for the current task.
1222 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1223 unsigned long end, unsigned long *userptr)
1225 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1228 if (gtt == NULL || !gtt->userptr)
1231 /* Return false if no part of the ttm_tt object lies within
1234 size = (unsigned long)gtt->ttm.num_pages * PAGE_SIZE;
1235 if (gtt->userptr > end || gtt->userptr + size <= start)
1239 *userptr = gtt->userptr;
1244 * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
1246 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
1248 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1250 if (gtt == NULL || !gtt->userptr)
1257 * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1259 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1261 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1266 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1270 * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object
1272 * @ttm: The ttm_tt object to compute the flags for
1273 * @mem: The memory registry backing this ttm_tt object
1275 * Figure out the flags to use for a VM PDE (Page Directory Entry).
1277 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem)
1281 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1282 flags |= AMDGPU_PTE_VALID;
1284 if (mem && (mem->mem_type == TTM_PL_TT ||
1285 mem->mem_type == AMDGPU_PL_PREEMPT)) {
1286 flags |= AMDGPU_PTE_SYSTEM;
1288 if (ttm->caching == ttm_cached)
1289 flags |= AMDGPU_PTE_SNOOPED;
1292 if (mem && mem->mem_type == TTM_PL_VRAM &&
1293 mem->bus.caching == ttm_cached)
1294 flags |= AMDGPU_PTE_SNOOPED;
1300 * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1302 * @adev: amdgpu_device pointer
1303 * @ttm: The ttm_tt object to compute the flags for
1304 * @mem: The memory registry backing this ttm_tt object
1306 * Figure out the flags to use for a VM PTE (Page Table Entry).
1308 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1309 struct ttm_resource *mem)
1311 uint64_t flags = amdgpu_ttm_tt_pde_flags(ttm, mem);
1313 flags |= adev->gart.gart_pte_flags;
1314 flags |= AMDGPU_PTE_READABLE;
1316 if (!amdgpu_ttm_tt_is_readonly(ttm))
1317 flags |= AMDGPU_PTE_WRITEABLE;
1323 * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1326 * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1327 * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1328 * it can find space for a new object and by ttm_bo_force_list_clean() which is
1329 * used to clean out a memory space.
1331 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1332 const struct ttm_place *place)
1334 struct dma_resv_iter resv_cursor;
1335 struct dma_fence *f;
1337 if (!amdgpu_bo_is_amdgpu_bo(bo))
1338 return ttm_bo_eviction_valuable(bo, place);
1341 if (bo->resource->mem_type == TTM_PL_SYSTEM)
1344 if (bo->type == ttm_bo_type_kernel &&
1345 !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)))
1348 /* If bo is a KFD BO, check if the bo belongs to the current process.
1349 * If true, then return false as any KFD process needs all its BOs to
1350 * be resident to run successfully
1352 dma_resv_for_each_fence(&resv_cursor, bo->base.resv,
1353 DMA_RESV_USAGE_BOOKKEEP, f) {
1354 if (amdkfd_fence_check_mm(f, current->mm))
1358 /* Preemptible BOs don't own system resources managed by the
1359 * driver (pages, VRAM, GART space). They point to resources
1360 * owned by someone else (e.g. pageable memory in user mode
1361 * or a DMABuf). They are used in a preemptible context so we
1362 * can guarantee no deadlocks and good QoS in case of MMU
1363 * notifiers or DMABuf move notifiers from the resource owner.
1365 if (bo->resource->mem_type == AMDGPU_PL_PREEMPT)
1368 if (bo->resource->mem_type == TTM_PL_TT &&
1369 amdgpu_bo_encrypted(ttm_to_amdgpu_bo(bo)))
1372 return ttm_bo_eviction_valuable(bo, place);
1375 static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
1376 void *buf, size_t size, bool write)
1379 uint64_t aligned_pos = ALIGN_DOWN(pos, 4);
1380 uint64_t bytes = 4 - (pos & 0x3);
1381 uint32_t shift = (pos & 0x3) * 8;
1382 uint32_t mask = 0xffffffff << shift;
1386 mask &= 0xffffffff >> (bytes - size) * 8;
1390 if (mask != 0xffffffff) {
1391 amdgpu_device_mm_access(adev, aligned_pos, &value, 4, false);
1394 value |= (*(uint32_t *)buf << shift) & mask;
1395 amdgpu_device_mm_access(adev, aligned_pos, &value, 4, true);
1397 value = (value & mask) >> shift;
1398 memcpy(buf, &value, bytes);
1401 amdgpu_device_mm_access(adev, aligned_pos, buf, 4, write);
1410 static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
1411 unsigned long offset, void *buf,
1414 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1415 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1416 struct amdgpu_res_cursor src_mm;
1417 struct amdgpu_job *job;
1418 struct dma_fence *fence;
1419 uint64_t src_addr, dst_addr;
1420 unsigned int num_dw;
1423 if (len != PAGE_SIZE)
1426 if (!adev->mman.sdma_access_ptr)
1429 if (!drm_dev_enter(adev_to_drm(adev), &idx))
1433 memcpy(adev->mman.sdma_access_ptr, buf, len);
1435 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
1436 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.entity,
1437 AMDGPU_FENCE_OWNER_UNDEFINED,
1438 num_dw * 4, AMDGPU_IB_POOL_DELAYED,
1443 amdgpu_res_first(abo->tbo.resource, offset, len, &src_mm);
1444 src_addr = amdgpu_ttm_domain_start(adev, bo->resource->mem_type) +
1446 dst_addr = amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
1448 swap(src_addr, dst_addr);
1450 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
1453 amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
1454 WARN_ON(job->ibs[0].length_dw > num_dw);
1456 fence = amdgpu_job_submit(job);
1458 if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
1460 dma_fence_put(fence);
1463 memcpy(buf, adev->mman.sdma_access_ptr, len);
1470 * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1472 * @bo: The buffer object to read/write
1473 * @offset: Offset into buffer object
1474 * @buf: Secondary buffer to write/read from
1475 * @len: Length in bytes of access
1476 * @write: true if writing
1478 * This is used to access VRAM that backs a buffer object via MMIO
1479 * access for debugging purposes.
1481 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1482 unsigned long offset, void *buf, int len,
1485 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1486 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1487 struct amdgpu_res_cursor cursor;
1490 if (bo->resource->mem_type != TTM_PL_VRAM)
1493 if (amdgpu_device_has_timeouts_enabled(adev) &&
1494 !amdgpu_ttm_access_memory_sdma(bo, offset, buf, len, write))
1497 amdgpu_res_first(bo->resource, offset, len, &cursor);
1498 while (cursor.remaining) {
1499 size_t count, size = cursor.size;
1500 loff_t pos = cursor.start;
1502 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
1505 /* using MM to access rest vram and handle un-aligned address */
1508 amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write);
1513 amdgpu_res_next(&cursor, cursor.size);
1520 amdgpu_bo_delete_mem_notify(struct ttm_buffer_object *bo)
1522 amdgpu_bo_move_notify(bo, false, NULL);
1525 static struct ttm_device_funcs amdgpu_bo_driver = {
1526 .ttm_tt_create = &amdgpu_ttm_tt_create,
1527 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1528 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1529 .ttm_tt_destroy = &amdgpu_ttm_backend_destroy,
1530 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1531 .evict_flags = &amdgpu_evict_flags,
1532 .move = &amdgpu_bo_move,
1533 .delete_mem_notify = &amdgpu_bo_delete_mem_notify,
1534 .release_notify = &amdgpu_bo_release_notify,
1535 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1536 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1537 .access_memory = &amdgpu_ttm_access_memory,
1541 * Firmware Reservation functions
1544 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1546 * @adev: amdgpu_device pointer
1548 * free fw reserved vram if it has been reserved.
1550 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1552 amdgpu_bo_free_kernel(&adev->mman.fw_vram_usage_reserved_bo,
1553 NULL, &adev->mman.fw_vram_usage_va);
1557 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1559 * @adev: amdgpu_device pointer
1561 * create bo vram reservation from fw.
1563 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1565 uint64_t vram_size = adev->gmc.visible_vram_size;
1567 adev->mman.fw_vram_usage_va = NULL;
1568 adev->mman.fw_vram_usage_reserved_bo = NULL;
1570 if (adev->mman.fw_vram_usage_size == 0 ||
1571 adev->mman.fw_vram_usage_size > vram_size)
1574 return amdgpu_bo_create_kernel_at(adev,
1575 adev->mman.fw_vram_usage_start_offset,
1576 adev->mman.fw_vram_usage_size,
1577 AMDGPU_GEM_DOMAIN_VRAM,
1578 &adev->mman.fw_vram_usage_reserved_bo,
1579 &adev->mman.fw_vram_usage_va);
1583 * Memoy training reservation functions
1587 * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram
1589 * @adev: amdgpu_device pointer
1591 * free memory training reserved vram if it has been reserved.
1593 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev)
1595 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1597 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
1598 amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL);
1604 static void amdgpu_ttm_training_data_block_init(struct amdgpu_device *adev)
1606 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1608 memset(ctx, 0, sizeof(*ctx));
1610 ctx->c2p_train_data_offset =
1611 ALIGN((adev->gmc.mc_vram_size - adev->mman.discovery_tmr_size - SZ_1M), SZ_1M);
1612 ctx->p2c_train_data_offset =
1613 (adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET);
1614 ctx->train_data_size =
1615 GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES;
1617 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
1618 ctx->train_data_size,
1619 ctx->p2c_train_data_offset,
1620 ctx->c2p_train_data_offset);
1624 * reserve TMR memory at the top of VRAM which holds
1625 * IP Discovery data and is protected by PSP.
1627 static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
1630 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1631 bool mem_train_support = false;
1633 if (!amdgpu_sriov_vf(adev)) {
1634 if (amdgpu_atomfirmware_mem_training_supported(adev))
1635 mem_train_support = true;
1637 DRM_DEBUG("memory training does not support!\n");
1641 * Query reserved tmr size through atom firmwareinfo for Sienna_Cichlid and onwards for all
1642 * the use cases (IP discovery/G6 memory training/profiling/diagnostic data.etc)
1644 * Otherwise, fallback to legacy approach to check and reserve tmr block for ip
1645 * discovery data and G6 memory training data respectively
1647 adev->mman.discovery_tmr_size =
1648 amdgpu_atomfirmware_get_fw_reserved_fb_size(adev);
1649 if (!adev->mman.discovery_tmr_size)
1650 adev->mman.discovery_tmr_size = DISCOVERY_TMR_OFFSET;
1652 if (mem_train_support) {
1653 /* reserve vram for mem train according to TMR location */
1654 amdgpu_ttm_training_data_block_init(adev);
1655 ret = amdgpu_bo_create_kernel_at(adev,
1656 ctx->c2p_train_data_offset,
1657 ctx->train_data_size,
1658 AMDGPU_GEM_DOMAIN_VRAM,
1662 DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
1663 amdgpu_ttm_training_reserve_vram_fini(adev);
1666 ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS;
1669 ret = amdgpu_bo_create_kernel_at(adev,
1670 adev->gmc.real_vram_size - adev->mman.discovery_tmr_size,
1671 adev->mman.discovery_tmr_size,
1672 AMDGPU_GEM_DOMAIN_VRAM,
1673 &adev->mman.discovery_memory,
1676 DRM_ERROR("alloc tmr failed(%d)!\n", ret);
1677 amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1685 * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1686 * gtt/vram related fields.
1688 * This initializes all of the memory space pools that the TTM layer
1689 * will need such as the GTT space (system memory mapped to the device),
1690 * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1691 * can be mapped per VMID.
1693 int amdgpu_ttm_init(struct amdgpu_device *adev)
1699 mutex_init(&adev->mman.gtt_window_lock);
1701 /* No others user of address space so set it to 0 */
1702 r = ttm_device_init(&adev->mman.bdev, &amdgpu_bo_driver, adev->dev,
1703 adev_to_drm(adev)->anon_inode->i_mapping,
1704 adev_to_drm(adev)->vma_offset_manager,
1706 dma_addressing_limited(adev->dev));
1708 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1711 adev->mman.initialized = true;
1713 /* Initialize VRAM pool with all of VRAM divided into pages */
1714 r = amdgpu_vram_mgr_init(adev);
1716 DRM_ERROR("Failed initializing VRAM heap.\n");
1720 /* Reduce size of CPU-visible VRAM if requested */
1721 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1722 if (amdgpu_vis_vram_limit > 0 &&
1723 vis_vram_limit <= adev->gmc.visible_vram_size)
1724 adev->gmc.visible_vram_size = vis_vram_limit;
1726 /* Change the size here instead of the init above so only lpfn is affected */
1727 amdgpu_ttm_set_buffer_funcs_status(adev, false);
1730 if (adev->gmc.xgmi.connected_to_cpu)
1731 adev->mman.aper_base_kaddr = ioremap_cache(adev->gmc.aper_base,
1732 adev->gmc.visible_vram_size);
1736 adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base,
1737 adev->gmc.visible_vram_size);
1741 *The reserved vram for firmware must be pinned to the specified
1742 *place on the VRAM, so reserve it early.
1744 r = amdgpu_ttm_fw_reserve_vram_init(adev);
1750 * only NAVI10 and onwards ASIC support for IP discovery.
1751 * If IP discovery enabled, a block of memory should be
1752 * reserved for IP discovey.
1754 if (adev->mman.discovery_bin) {
1755 r = amdgpu_ttm_reserve_tmr(adev);
1760 /* allocate memory as required for VGA
1761 * This is used for VGA emulation and pre-OS scanout buffers to
1762 * avoid display artifacts while transitioning between pre-OS
1764 r = amdgpu_bo_create_kernel_at(adev, 0, adev->mman.stolen_vga_size,
1765 AMDGPU_GEM_DOMAIN_VRAM,
1766 &adev->mman.stolen_vga_memory,
1770 r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
1771 adev->mman.stolen_extended_size,
1772 AMDGPU_GEM_DOMAIN_VRAM,
1773 &adev->mman.stolen_extended_memory,
1777 r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_reserved_offset,
1778 adev->mman.stolen_reserved_size,
1779 AMDGPU_GEM_DOMAIN_VRAM,
1780 &adev->mman.stolen_reserved_memory,
1785 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1786 (unsigned) (adev->gmc.real_vram_size / (1024 * 1024)));
1788 /* Compute GTT size, either based on 1/2 the size of RAM size
1789 * or whatever the user passed on module init */
1790 if (amdgpu_gtt_size == -1) {
1794 /* Certain GL unit tests for large textures can cause problems
1795 * with the OOM killer since there is no way to link this memory
1796 * to a process. This was originally mitigated (but not necessarily
1797 * eliminated) by limiting the GTT size. The problem is this limit
1798 * is often too low for many modern games so just make the limit 1/2
1799 * of system memory which aligns with TTM. The OOM accounting needs
1800 * to be addressed, but we shouldn't prevent common 3D applications
1801 * from being usable just to potentially mitigate that corner case.
1803 gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1804 (u64)si.totalram * si.mem_unit / 2);
1806 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1809 /* Initialize GTT memory pool */
1810 r = amdgpu_gtt_mgr_init(adev, gtt_size);
1812 DRM_ERROR("Failed initializing GTT heap.\n");
1815 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1816 (unsigned)(gtt_size / (1024 * 1024)));
1818 /* Initialize preemptible memory pool */
1819 r = amdgpu_preempt_mgr_init(adev);
1821 DRM_ERROR("Failed initializing PREEMPT heap.\n");
1825 /* Initialize various on-chip memory pools */
1826 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GDS, adev->gds.gds_size);
1828 DRM_ERROR("Failed initializing GDS heap.\n");
1832 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GWS, adev->gds.gws_size);
1834 DRM_ERROR("Failed initializing gws heap.\n");
1838 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_OA, adev->gds.oa_size);
1840 DRM_ERROR("Failed initializing oa heap.\n");
1844 if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
1845 AMDGPU_GEM_DOMAIN_GTT,
1846 &adev->mman.sdma_access_bo, NULL,
1847 &adev->mman.sdma_access_ptr))
1848 DRM_WARN("Debug VRAM access will use slowpath MM access\n");
1854 * amdgpu_ttm_fini - De-initialize the TTM memory pools
1856 void amdgpu_ttm_fini(struct amdgpu_device *adev)
1859 if (!adev->mman.initialized)
1862 amdgpu_ttm_training_reserve_vram_fini(adev);
1863 /* return the stolen vga memory back to VRAM */
1864 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
1865 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
1866 /* return the IP Discovery TMR memory back to VRAM */
1867 amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1868 if (adev->mman.stolen_reserved_size)
1869 amdgpu_bo_free_kernel(&adev->mman.stolen_reserved_memory,
1871 amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo, NULL,
1872 &adev->mman.sdma_access_ptr);
1873 amdgpu_ttm_fw_reserve_vram_fini(adev);
1875 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
1877 if (adev->mman.aper_base_kaddr)
1878 iounmap(adev->mman.aper_base_kaddr);
1879 adev->mman.aper_base_kaddr = NULL;
1884 amdgpu_vram_mgr_fini(adev);
1885 amdgpu_gtt_mgr_fini(adev);
1886 amdgpu_preempt_mgr_fini(adev);
1887 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GDS);
1888 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GWS);
1889 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
1890 ttm_device_fini(&adev->mman.bdev);
1891 adev->mman.initialized = false;
1892 DRM_INFO("amdgpu: ttm finalized\n");
1896 * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
1898 * @adev: amdgpu_device pointer
1899 * @enable: true when we can use buffer functions.
1901 * Enable/disable use of buffer functions during suspend/resume. This should
1902 * only be called at bootup or when userspace isn't running.
1904 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
1906 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
1910 if (!adev->mman.initialized || amdgpu_in_reset(adev) ||
1911 adev->mman.buffer_funcs_enabled == enable)
1915 struct amdgpu_ring *ring;
1916 struct drm_gpu_scheduler *sched;
1918 ring = adev->mman.buffer_funcs_ring;
1919 sched = &ring->sched;
1920 r = drm_sched_entity_init(&adev->mman.entity,
1921 DRM_SCHED_PRIORITY_KERNEL, &sched,
1924 DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
1929 drm_sched_entity_destroy(&adev->mman.entity);
1930 dma_fence_put(man->move);
1934 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1936 size = adev->gmc.real_vram_size;
1938 size = adev->gmc.visible_vram_size;
1940 adev->mman.buffer_funcs_enabled = enable;
1943 static int amdgpu_ttm_prepare_job(struct amdgpu_device *adev,
1945 unsigned int num_dw,
1946 struct dma_resv *resv,
1947 bool vm_needs_flush,
1948 struct amdgpu_job **job)
1950 enum amdgpu_ib_pool_type pool = direct_submit ?
1951 AMDGPU_IB_POOL_DIRECT :
1952 AMDGPU_IB_POOL_DELAYED;
1955 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.entity,
1956 AMDGPU_FENCE_OWNER_UNDEFINED,
1957 num_dw * 4, pool, job);
1961 if (vm_needs_flush) {
1962 (*job)->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gmc.pdb0_bo ?
1965 (*job)->vm_needs_flush = true;
1970 return drm_sched_job_add_resv_dependencies(&(*job)->base, resv,
1971 DMA_RESV_USAGE_BOOKKEEP);
1974 int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1975 uint64_t dst_offset, uint32_t byte_count,
1976 struct dma_resv *resv,
1977 struct dma_fence **fence, bool direct_submit,
1978 bool vm_needs_flush, bool tmz)
1980 struct amdgpu_device *adev = ring->adev;
1981 unsigned num_loops, num_dw;
1982 struct amdgpu_job *job;
1987 if (!direct_submit && !ring->sched.ready) {
1988 DRM_ERROR("Trying to move memory with ring turned off.\n");
1992 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1993 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1994 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->copy_num_dw, 8);
1995 r = amdgpu_ttm_prepare_job(adev, direct_submit, num_dw,
1996 resv, vm_needs_flush, &job);
2000 for (i = 0; i < num_loops; i++) {
2001 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2003 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
2004 dst_offset, cur_size_in_bytes, tmz);
2006 src_offset += cur_size_in_bytes;
2007 dst_offset += cur_size_in_bytes;
2008 byte_count -= cur_size_in_bytes;
2011 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2012 WARN_ON(job->ibs[0].length_dw > num_dw);
2014 r = amdgpu_job_submit_direct(job, ring, fence);
2016 *fence = amdgpu_job_submit(job);
2023 amdgpu_job_free(job);
2024 DRM_ERROR("Error scheduling IBs (%d)\n", r);
2028 static int amdgpu_ttm_fill_mem(struct amdgpu_ring *ring, uint32_t src_data,
2029 uint64_t dst_addr, uint32_t byte_count,
2030 struct dma_resv *resv,
2031 struct dma_fence **fence,
2032 bool vm_needs_flush)
2034 struct amdgpu_device *adev = ring->adev;
2035 unsigned int num_loops, num_dw;
2036 struct amdgpu_job *job;
2041 max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
2042 num_loops = DIV_ROUND_UP_ULL(byte_count, max_bytes);
2043 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->fill_num_dw, 8);
2044 r = amdgpu_ttm_prepare_job(adev, false, num_dw, resv, vm_needs_flush,
2049 for (i = 0; i < num_loops; i++) {
2050 uint32_t cur_size = min(byte_count, max_bytes);
2052 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data, dst_addr,
2055 dst_addr += cur_size;
2056 byte_count -= cur_size;
2059 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2060 WARN_ON(job->ibs[0].length_dw > num_dw);
2061 *fence = amdgpu_job_submit(job);
2065 int amdgpu_fill_buffer(struct amdgpu_bo *bo,
2067 struct dma_resv *resv,
2068 struct dma_fence **f)
2070 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2071 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2072 struct dma_fence *fence = NULL;
2073 struct amdgpu_res_cursor dst;
2076 if (!adev->mman.buffer_funcs_enabled) {
2077 DRM_ERROR("Trying to clear memory with ring turned off.\n");
2081 amdgpu_res_first(bo->tbo.resource, 0, amdgpu_bo_size(bo), &dst);
2083 mutex_lock(&adev->mman.gtt_window_lock);
2084 while (dst.remaining) {
2085 struct dma_fence *next;
2086 uint64_t cur_size, to;
2088 /* Never fill more than 256MiB at once to avoid timeouts */
2089 cur_size = min(dst.size, 256ULL << 20);
2091 r = amdgpu_ttm_map_buffer(&bo->tbo, bo->tbo.resource, &dst,
2092 1, ring, false, &cur_size, &to);
2096 r = amdgpu_ttm_fill_mem(ring, src_data, to, cur_size, resv,
2101 dma_fence_put(fence);
2104 amdgpu_res_next(&dst, cur_size);
2107 mutex_unlock(&adev->mman.gtt_window_lock);
2109 *f = dma_fence_get(fence);
2110 dma_fence_put(fence);
2115 * amdgpu_ttm_evict_resources - evict memory buffers
2116 * @adev: amdgpu device object
2117 * @mem_type: evicted BO's memory type
2119 * Evicts all @mem_type buffers on the lru list of the memory type.
2122 * 0 for success or a negative error code on failure.
2124 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type)
2126 struct ttm_resource_manager *man;
2134 man = ttm_manager_type(&adev->mman.bdev, mem_type);
2137 DRM_ERROR("Trying to evict invalid memory type\n");
2141 return ttm_resource_manager_evict_all(&adev->mman.bdev, man);
2144 #if defined(CONFIG_DEBUG_FS)
2146 static int amdgpu_ttm_page_pool_show(struct seq_file *m, void *unused)
2148 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
2150 return ttm_pool_debugfs(&adev->mman.bdev.pool, m);
2153 DEFINE_SHOW_ATTRIBUTE(amdgpu_ttm_page_pool);
2156 * amdgpu_ttm_vram_read - Linear read access to VRAM
2158 * Accesses VRAM via MMIO for debugging purposes.
2160 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
2161 size_t size, loff_t *pos)
2163 struct amdgpu_device *adev = file_inode(f)->i_private;
2166 if (size & 0x3 || *pos & 0x3)
2169 if (*pos >= adev->gmc.mc_vram_size)
2172 size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos));
2174 size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4);
2175 uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ];
2177 amdgpu_device_vram_access(adev, *pos, value, bytes, false);
2178 if (copy_to_user(buf, value, bytes))
2191 * amdgpu_ttm_vram_write - Linear write access to VRAM
2193 * Accesses VRAM via MMIO for debugging purposes.
2195 static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
2196 size_t size, loff_t *pos)
2198 struct amdgpu_device *adev = file_inode(f)->i_private;
2202 if (size & 0x3 || *pos & 0x3)
2205 if (*pos >= adev->gmc.mc_vram_size)
2211 if (*pos >= adev->gmc.mc_vram_size)
2214 r = get_user(value, (uint32_t *)buf);
2218 amdgpu_device_mm_access(adev, *pos, &value, 4, true);
2229 static const struct file_operations amdgpu_ttm_vram_fops = {
2230 .owner = THIS_MODULE,
2231 .read = amdgpu_ttm_vram_read,
2232 .write = amdgpu_ttm_vram_write,
2233 .llseek = default_llseek,
2237 * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2239 * This function is used to read memory that has been mapped to the
2240 * GPU and the known addresses are not physical addresses but instead
2241 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2243 static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
2244 size_t size, loff_t *pos)
2246 struct amdgpu_device *adev = file_inode(f)->i_private;
2247 struct iommu_domain *dom;
2251 /* retrieve the IOMMU domain if any for this device */
2252 dom = iommu_get_domain_for_dev(adev->dev);
2255 phys_addr_t addr = *pos & PAGE_MASK;
2256 loff_t off = *pos & ~PAGE_MASK;
2257 size_t bytes = PAGE_SIZE - off;
2262 bytes = bytes < size ? bytes : size;
2264 /* Translate the bus address to a physical address. If
2265 * the domain is NULL it means there is no IOMMU active
2266 * and the address translation is the identity
2268 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2270 pfn = addr >> PAGE_SHIFT;
2271 if (!pfn_valid(pfn))
2274 p = pfn_to_page(pfn);
2275 if (p->mapping != adev->mman.bdev.dev_mapping)
2278 ptr = kmap_local_page(p);
2279 r = copy_to_user(buf, ptr + off, bytes);
2293 * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2295 * This function is used to write memory that has been mapped to the
2296 * GPU and the known addresses are not physical addresses but instead
2297 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2299 static ssize_t amdgpu_iomem_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;
2303 struct iommu_domain *dom;
2307 dom = iommu_get_domain_for_dev(adev->dev);
2310 phys_addr_t addr = *pos & PAGE_MASK;
2311 loff_t off = *pos & ~PAGE_MASK;
2312 size_t bytes = PAGE_SIZE - off;
2317 bytes = bytes < size ? bytes : size;
2319 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2321 pfn = addr >> PAGE_SHIFT;
2322 if (!pfn_valid(pfn))
2325 p = pfn_to_page(pfn);
2326 if (p->mapping != adev->mman.bdev.dev_mapping)
2329 ptr = kmap_local_page(p);
2330 r = copy_from_user(ptr + off, buf, bytes);
2343 static const struct file_operations amdgpu_ttm_iomem_fops = {
2344 .owner = THIS_MODULE,
2345 .read = amdgpu_iomem_read,
2346 .write = amdgpu_iomem_write,
2347 .llseek = default_llseek
2352 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
2354 #if defined(CONFIG_DEBUG_FS)
2355 struct drm_minor *minor = adev_to_drm(adev)->primary;
2356 struct dentry *root = minor->debugfs_root;
2358 debugfs_create_file_size("amdgpu_vram", 0444, root, adev,
2359 &amdgpu_ttm_vram_fops, adev->gmc.mc_vram_size);
2360 debugfs_create_file("amdgpu_iomem", 0444, root, adev,
2361 &amdgpu_ttm_iomem_fops);
2362 debugfs_create_file("ttm_page_pool", 0444, root, adev,
2363 &amdgpu_ttm_page_pool_fops);
2364 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2366 root, "amdgpu_vram_mm");
2367 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2369 root, "amdgpu_gtt_mm");
2370 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2372 root, "amdgpu_gds_mm");
2373 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2375 root, "amdgpu_gws_mm");
2376 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2378 root, "amdgpu_oa_mm");