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.h>
48 #include <drm/ttm/ttm_placement.h>
49 #include <drm/ttm/ttm_range_manager.h>
50 #include <drm/ttm/ttm_tt.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_hmm.h"
62 #include "amdgpu_atomfirmware.h"
63 #include "amdgpu_res_cursor.h"
64 #include "bif/bif_4_1_d.h"
66 MODULE_IMPORT_NS(DMA_BUF);
68 #define AMDGPU_TTM_VRAM_MAX_DW_READ (size_t)128
70 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
72 struct ttm_resource *bo_mem);
73 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
76 static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
78 uint64_t size_in_page)
80 return ttm_range_man_init(&adev->mman.bdev, type,
85 * amdgpu_evict_flags - Compute placement flags
87 * @bo: The buffer object to evict
88 * @placement: Possible destination(s) for evicted BO
90 * Fill in placement data when ttm_bo_evict() is called
92 static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
93 struct ttm_placement *placement)
95 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
96 struct amdgpu_bo *abo;
97 static const struct ttm_place placements = {
100 .mem_type = TTM_PL_SYSTEM,
104 /* Don't handle scatter gather BOs */
105 if (bo->type == ttm_bo_type_sg) {
106 placement->num_placement = 0;
107 placement->num_busy_placement = 0;
111 /* Object isn't an AMDGPU object so ignore */
112 if (!amdgpu_bo_is_amdgpu_bo(bo)) {
113 placement->placement = &placements;
114 placement->busy_placement = &placements;
115 placement->num_placement = 1;
116 placement->num_busy_placement = 1;
120 abo = ttm_to_amdgpu_bo(bo);
121 if (abo->flags & AMDGPU_GEM_CREATE_DISCARDABLE) {
122 placement->num_placement = 0;
123 placement->num_busy_placement = 0;
127 switch (bo->resource->mem_type) {
131 placement->num_placement = 0;
132 placement->num_busy_placement = 0;
136 if (!adev->mman.buffer_funcs_enabled) {
137 /* Move to system memory */
138 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
139 } else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
140 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
141 amdgpu_bo_in_cpu_visible_vram(abo)) {
143 /* Try evicting to the CPU inaccessible part of VRAM
144 * first, but only set GTT as busy placement, so this
145 * BO will be evicted to GTT rather than causing other
146 * BOs to be evicted from VRAM
148 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
149 AMDGPU_GEM_DOMAIN_GTT |
150 AMDGPU_GEM_DOMAIN_CPU);
151 abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
152 abo->placements[0].lpfn = 0;
153 abo->placement.busy_placement = &abo->placements[1];
154 abo->placement.num_busy_placement = 1;
156 /* Move to GTT memory */
157 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT |
158 AMDGPU_GEM_DOMAIN_CPU);
162 case AMDGPU_PL_PREEMPT:
164 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
167 *placement = abo->placement;
171 * amdgpu_ttm_map_buffer - Map memory into the GART windows
172 * @bo: buffer object to map
173 * @mem: memory object to map
174 * @mm_cur: range to map
175 * @window: which GART window to use
176 * @ring: DMA ring to use for the copy
177 * @tmz: if we should setup a TMZ enabled mapping
178 * @size: in number of bytes to map, out number of bytes mapped
179 * @addr: resulting address inside the MC address space
181 * Setup one of the GART windows to access a specific piece of memory or return
182 * the physical address for local memory.
184 static int amdgpu_ttm_map_buffer(struct ttm_buffer_object *bo,
185 struct ttm_resource *mem,
186 struct amdgpu_res_cursor *mm_cur,
187 unsigned window, struct amdgpu_ring *ring,
188 bool tmz, uint64_t *size, uint64_t *addr)
190 struct amdgpu_device *adev = ring->adev;
191 unsigned offset, num_pages, num_dw, num_bytes;
192 uint64_t src_addr, dst_addr;
193 struct amdgpu_job *job;
199 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
200 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
202 if (WARN_ON(mem->mem_type == AMDGPU_PL_PREEMPT))
205 /* Map only what can't be accessed directly */
206 if (!tmz && mem->start != AMDGPU_BO_INVALID_OFFSET) {
207 *addr = amdgpu_ttm_domain_start(adev, mem->mem_type) +
214 * If start begins at an offset inside the page, then adjust the size
215 * and addr accordingly
217 offset = mm_cur->start & ~PAGE_MASK;
219 num_pages = PFN_UP(*size + offset);
220 num_pages = min_t(uint32_t, num_pages, AMDGPU_GTT_MAX_TRANSFER_SIZE);
222 *size = min(*size, (uint64_t)num_pages * PAGE_SIZE - offset);
224 *addr = adev->gmc.gart_start;
225 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
226 AMDGPU_GPU_PAGE_SIZE;
229 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
230 num_bytes = num_pages * 8 * AMDGPU_GPU_PAGES_IN_CPU_PAGE;
232 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.entity,
233 AMDGPU_FENCE_OWNER_UNDEFINED,
234 num_dw * 4 + num_bytes,
235 AMDGPU_IB_POOL_DELAYED, &job);
239 src_addr = num_dw * 4;
240 src_addr += job->ibs[0].gpu_addr;
242 dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
243 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
244 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
245 dst_addr, num_bytes, false);
247 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
248 WARN_ON(job->ibs[0].length_dw > num_dw);
250 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, mem);
252 flags |= AMDGPU_PTE_TMZ;
254 cpu_addr = &job->ibs[0].ptr[num_dw];
256 if (mem->mem_type == TTM_PL_TT) {
257 dma_addr_t *dma_addr;
259 dma_addr = &bo->ttm->dma_address[mm_cur->start >> PAGE_SHIFT];
260 amdgpu_gart_map(adev, 0, num_pages, dma_addr, flags, cpu_addr);
262 dma_addr_t dma_address;
264 dma_address = mm_cur->start;
265 dma_address += adev->vm_manager.vram_base_offset;
267 for (i = 0; i < num_pages; ++i) {
268 amdgpu_gart_map(adev, i << PAGE_SHIFT, 1, &dma_address,
270 dma_address += PAGE_SIZE;
274 dma_fence_put(amdgpu_job_submit(job));
279 * amdgpu_ttm_copy_mem_to_mem - Helper function for copy
280 * @adev: amdgpu device
281 * @src: buffer/address where to read from
282 * @dst: buffer/address where to write to
283 * @size: number of bytes to copy
284 * @tmz: if a secure copy should be used
285 * @resv: resv object to sync to
286 * @f: Returns the last fence if multiple jobs are submitted.
288 * The function copies @size bytes from {src->mem + src->offset} to
289 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
290 * move and different for a BO to BO copy.
293 int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
294 const struct amdgpu_copy_mem *src,
295 const struct amdgpu_copy_mem *dst,
296 uint64_t size, bool tmz,
297 struct dma_resv *resv,
298 struct dma_fence **f)
300 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
301 struct amdgpu_res_cursor src_mm, dst_mm;
302 struct dma_fence *fence = NULL;
305 if (!adev->mman.buffer_funcs_enabled) {
306 DRM_ERROR("Trying to move memory with ring turned off.\n");
310 amdgpu_res_first(src->mem, src->offset, size, &src_mm);
311 amdgpu_res_first(dst->mem, dst->offset, size, &dst_mm);
313 mutex_lock(&adev->mman.gtt_window_lock);
314 while (src_mm.remaining) {
315 uint64_t from, to, cur_size;
316 struct dma_fence *next;
318 /* Never copy more than 256MiB at once to avoid a timeout */
319 cur_size = min3(src_mm.size, dst_mm.size, 256ULL << 20);
321 /* Map src to window 0 and dst to window 1. */
322 r = amdgpu_ttm_map_buffer(src->bo, src->mem, &src_mm,
323 0, ring, tmz, &cur_size, &from);
327 r = amdgpu_ttm_map_buffer(dst->bo, dst->mem, &dst_mm,
328 1, ring, tmz, &cur_size, &to);
332 r = amdgpu_copy_buffer(ring, from, to, cur_size,
333 resv, &next, false, true, tmz);
337 dma_fence_put(fence);
340 amdgpu_res_next(&src_mm, cur_size);
341 amdgpu_res_next(&dst_mm, cur_size);
344 mutex_unlock(&adev->mman.gtt_window_lock);
346 *f = dma_fence_get(fence);
347 dma_fence_put(fence);
352 * amdgpu_move_blit - Copy an entire buffer to another buffer
354 * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
355 * help move buffers to and from VRAM.
357 static int amdgpu_move_blit(struct ttm_buffer_object *bo,
359 struct ttm_resource *new_mem,
360 struct ttm_resource *old_mem)
362 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
363 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
364 struct amdgpu_copy_mem src, dst;
365 struct dma_fence *fence = NULL;
375 r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
377 amdgpu_bo_encrypted(abo),
378 bo->base.resv, &fence);
382 /* clear the space being freed */
383 if (old_mem->mem_type == TTM_PL_VRAM &&
384 (abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)) {
385 struct dma_fence *wipe_fence = NULL;
387 r = amdgpu_fill_buffer(abo, AMDGPU_POISON, NULL, &wipe_fence);
390 } else if (wipe_fence) {
391 dma_fence_put(fence);
396 /* Always block for VM page tables before committing the new location */
397 if (bo->type == ttm_bo_type_kernel)
398 r = ttm_bo_move_accel_cleanup(bo, fence, true, false, new_mem);
400 r = ttm_bo_move_accel_cleanup(bo, fence, evict, true, new_mem);
401 dma_fence_put(fence);
406 dma_fence_wait(fence, false);
407 dma_fence_put(fence);
412 * amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
414 * Called by amdgpu_bo_move()
416 static bool amdgpu_mem_visible(struct amdgpu_device *adev,
417 struct ttm_resource *mem)
419 u64 mem_size = (u64)mem->size;
420 struct amdgpu_res_cursor cursor;
423 if (mem->mem_type == TTM_PL_SYSTEM ||
424 mem->mem_type == TTM_PL_TT)
426 if (mem->mem_type != TTM_PL_VRAM)
429 amdgpu_res_first(mem, 0, mem_size, &cursor);
430 end = cursor.start + cursor.size;
431 while (cursor.remaining) {
432 amdgpu_res_next(&cursor, cursor.size);
434 if (!cursor.remaining)
437 /* ttm_resource_ioremap only supports contiguous memory */
438 if (end != cursor.start)
441 end = cursor.start + cursor.size;
444 return end <= adev->gmc.visible_vram_size;
448 * amdgpu_bo_move - Move a buffer object to a new memory location
450 * Called by ttm_bo_handle_move_mem()
452 static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
453 struct ttm_operation_ctx *ctx,
454 struct ttm_resource *new_mem,
455 struct ttm_place *hop)
457 struct amdgpu_device *adev;
458 struct amdgpu_bo *abo;
459 struct ttm_resource *old_mem = bo->resource;
462 if (new_mem->mem_type == TTM_PL_TT ||
463 new_mem->mem_type == AMDGPU_PL_PREEMPT) {
464 r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, new_mem);
469 abo = ttm_to_amdgpu_bo(bo);
470 adev = amdgpu_ttm_adev(bo->bdev);
472 if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM &&
474 ttm_bo_move_null(bo, new_mem);
477 if (old_mem->mem_type == TTM_PL_SYSTEM &&
478 (new_mem->mem_type == TTM_PL_TT ||
479 new_mem->mem_type == AMDGPU_PL_PREEMPT)) {
480 ttm_bo_move_null(bo, new_mem);
483 if ((old_mem->mem_type == TTM_PL_TT ||
484 old_mem->mem_type == AMDGPU_PL_PREEMPT) &&
485 new_mem->mem_type == TTM_PL_SYSTEM) {
486 r = ttm_bo_wait_ctx(bo, ctx);
490 amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
491 ttm_resource_free(bo, &bo->resource);
492 ttm_bo_assign_mem(bo, new_mem);
496 if (old_mem->mem_type == AMDGPU_PL_GDS ||
497 old_mem->mem_type == AMDGPU_PL_GWS ||
498 old_mem->mem_type == AMDGPU_PL_OA ||
499 new_mem->mem_type == AMDGPU_PL_GDS ||
500 new_mem->mem_type == AMDGPU_PL_GWS ||
501 new_mem->mem_type == AMDGPU_PL_OA) {
502 /* Nothing to save here */
503 ttm_bo_move_null(bo, new_mem);
507 if (bo->type == ttm_bo_type_device &&
508 new_mem->mem_type == TTM_PL_VRAM &&
509 old_mem->mem_type != TTM_PL_VRAM) {
510 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
511 * accesses the BO after it's moved.
513 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
516 if (adev->mman.buffer_funcs_enabled) {
517 if (((old_mem->mem_type == TTM_PL_SYSTEM &&
518 new_mem->mem_type == TTM_PL_VRAM) ||
519 (old_mem->mem_type == TTM_PL_VRAM &&
520 new_mem->mem_type == TTM_PL_SYSTEM))) {
523 hop->mem_type = TTM_PL_TT;
524 hop->flags = TTM_PL_FLAG_TEMPORARY;
528 r = amdgpu_move_blit(bo, evict, new_mem, old_mem);
534 /* Check that all memory is CPU accessible */
535 if (!amdgpu_mem_visible(adev, old_mem) ||
536 !amdgpu_mem_visible(adev, new_mem)) {
537 pr_err("Move buffer fallback to memcpy unavailable\n");
541 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
547 /* update statistics */
548 atomic64_add(bo->base.size, &adev->num_bytes_moved);
549 amdgpu_bo_move_notify(bo, evict, new_mem);
554 * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
556 * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
558 static int amdgpu_ttm_io_mem_reserve(struct ttm_device *bdev,
559 struct ttm_resource *mem)
561 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
562 size_t bus_size = (size_t)mem->size;
564 switch (mem->mem_type) {
569 case AMDGPU_PL_PREEMPT:
572 mem->bus.offset = mem->start << PAGE_SHIFT;
573 /* check if it's visible */
574 if ((mem->bus.offset + bus_size) > adev->gmc.visible_vram_size)
577 if (adev->mman.aper_base_kaddr &&
578 mem->placement & TTM_PL_FLAG_CONTIGUOUS)
579 mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr +
582 mem->bus.offset += adev->gmc.aper_base;
583 mem->bus.is_iomem = true;
591 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
592 unsigned long page_offset)
594 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
595 struct amdgpu_res_cursor cursor;
597 amdgpu_res_first(bo->resource, (u64)page_offset << PAGE_SHIFT, 0,
599 return (adev->gmc.aper_base + cursor.start) >> PAGE_SHIFT;
603 * amdgpu_ttm_domain_start - Returns GPU start address
604 * @adev: amdgpu device object
605 * @type: type of the memory
608 * GPU start address of a memory domain
611 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type)
615 return adev->gmc.gart_start;
617 return adev->gmc.vram_start;
624 * TTM backend functions.
626 struct amdgpu_ttm_tt {
628 struct drm_gem_object *gobj;
631 struct task_struct *usertask;
636 #define ttm_to_amdgpu_ttm_tt(ptr) container_of(ptr, struct amdgpu_ttm_tt, ttm)
638 #ifdef CONFIG_DRM_AMDGPU_USERPTR
640 * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user
641 * memory and start HMM tracking CPU page table update
643 * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only
644 * once afterwards to stop HMM tracking
646 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages,
647 struct hmm_range **range)
649 struct ttm_tt *ttm = bo->tbo.ttm;
650 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
651 unsigned long start = gtt->userptr;
652 struct vm_area_struct *vma;
653 struct mm_struct *mm;
657 /* Make sure get_user_pages_done() can cleanup gracefully */
660 mm = bo->notifier.mm;
662 DRM_DEBUG_DRIVER("BO is not registered?\n");
666 if (!mmget_not_zero(mm)) /* Happens during process shutdown */
670 vma = vma_lookup(mm, start);
671 if (unlikely(!vma)) {
675 if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) &&
681 readonly = amdgpu_ttm_tt_is_readonly(ttm);
682 r = amdgpu_hmm_range_get_pages(&bo->notifier, start, ttm->num_pages,
683 readonly, NULL, pages, range);
685 mmap_read_unlock(mm);
687 pr_debug("failed %d to get user pages 0x%lx\n", r, start);
694 /* amdgpu_ttm_tt_discard_user_pages - Discard range and pfn array allocations
696 void amdgpu_ttm_tt_discard_user_pages(struct ttm_tt *ttm,
697 struct hmm_range *range)
699 struct amdgpu_ttm_tt *gtt = (void *)ttm;
701 if (gtt && gtt->userptr && range)
702 amdgpu_hmm_range_get_pages_done(range);
706 * amdgpu_ttm_tt_get_user_pages_done - stop HMM track the CPU page table change
707 * Check if the pages backing this ttm range have been invalidated
709 * Returns: true if pages are still valid
711 bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm,
712 struct hmm_range *range)
714 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
716 if (!gtt || !gtt->userptr || !range)
719 DRM_DEBUG_DRIVER("user_pages_done 0x%llx pages 0x%x\n",
720 gtt->userptr, ttm->num_pages);
722 WARN_ONCE(!range->hmm_pfns, "No user pages to check\n");
724 return !amdgpu_hmm_range_get_pages_done(range);
729 * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
731 * Called by amdgpu_cs_list_validate(). This creates the page list
732 * that backs user memory and will ultimately be mapped into the device
735 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
739 for (i = 0; i < ttm->num_pages; ++i)
740 ttm->pages[i] = pages ? pages[i] : NULL;
744 * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages
746 * Called by amdgpu_ttm_backend_bind()
748 static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev,
751 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
752 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
753 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
754 enum dma_data_direction direction = write ?
755 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
758 /* Allocate an SG array and squash pages into it */
759 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
760 (u64)ttm->num_pages << PAGE_SHIFT,
765 /* Map SG to device */
766 r = dma_map_sgtable(adev->dev, ttm->sg, direction, 0);
770 /* convert SG to linear array of pages and dma addresses */
771 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
783 * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
785 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_device *bdev,
788 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
789 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
790 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
791 enum dma_data_direction direction = write ?
792 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
794 /* double check that we don't free the table twice */
795 if (!ttm->sg || !ttm->sg->sgl)
798 /* unmap the pages mapped to the device */
799 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
800 sg_free_table(ttm->sg);
803 static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
804 struct ttm_buffer_object *tbo,
807 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo);
808 struct ttm_tt *ttm = tbo->ttm;
809 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
811 if (amdgpu_bo_encrypted(abo))
812 flags |= AMDGPU_PTE_TMZ;
814 if (abo->flags & AMDGPU_GEM_CREATE_CP_MQD_GFX9) {
815 uint64_t page_idx = 1;
817 amdgpu_gart_bind(adev, gtt->offset, page_idx,
818 gtt->ttm.dma_address, flags);
820 /* The memory type of the first page defaults to UC. Now
821 * modify the memory type to NC from the second page of
824 flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
825 flags |= AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC);
827 amdgpu_gart_bind(adev, gtt->offset + (page_idx << PAGE_SHIFT),
828 ttm->num_pages - page_idx,
829 &(gtt->ttm.dma_address[page_idx]), flags);
831 amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
832 gtt->ttm.dma_address, flags);
837 * amdgpu_ttm_backend_bind - Bind GTT memory
839 * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
840 * This handles binding GTT memory to the device address space.
842 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
844 struct ttm_resource *bo_mem)
846 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
847 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
858 r = amdgpu_ttm_tt_pin_userptr(bdev, ttm);
860 DRM_ERROR("failed to pin userptr\n");
863 } else if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) {
865 struct dma_buf_attachment *attach;
866 struct sg_table *sgt;
868 attach = gtt->gobj->import_attach;
869 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
876 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
880 if (!ttm->num_pages) {
881 WARN(1, "nothing to bind %u pages for mreg %p back %p!\n",
882 ttm->num_pages, bo_mem, ttm);
885 if (bo_mem->mem_type != TTM_PL_TT ||
886 !amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
887 gtt->offset = AMDGPU_BO_INVALID_OFFSET;
891 /* compute PTE flags relevant to this BO memory */
892 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem);
894 /* bind pages into GART page tables */
895 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
896 amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
897 gtt->ttm.dma_address, flags);
903 * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
904 * through AGP or GART aperture.
906 * If bo is accessible through AGP aperture, then use AGP aperture
907 * to access bo; otherwise allocate logical space in GART aperture
908 * and map bo to GART aperture.
910 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
912 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
913 struct ttm_operation_ctx ctx = { false, false };
914 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
915 struct ttm_placement placement;
916 struct ttm_place placements;
917 struct ttm_resource *tmp;
918 uint64_t addr, flags;
921 if (bo->resource->start != AMDGPU_BO_INVALID_OFFSET)
924 addr = amdgpu_gmc_agp_addr(bo);
925 if (addr != AMDGPU_BO_INVALID_OFFSET) {
926 bo->resource->start = addr >> PAGE_SHIFT;
930 /* allocate GART space */
931 placement.num_placement = 1;
932 placement.placement = &placements;
933 placement.num_busy_placement = 1;
934 placement.busy_placement = &placements;
936 placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
937 placements.mem_type = TTM_PL_TT;
938 placements.flags = bo->resource->placement;
940 r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
944 /* compute PTE flags for this buffer object */
945 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, tmp);
948 gtt->offset = (u64)tmp->start << PAGE_SHIFT;
949 amdgpu_ttm_gart_bind(adev, bo, flags);
950 amdgpu_gart_invalidate_tlb(adev);
951 ttm_resource_free(bo, &bo->resource);
952 ttm_bo_assign_mem(bo, tmp);
958 * amdgpu_ttm_recover_gart - Rebind GTT pages
960 * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
961 * rebind GTT pages during a GPU reset.
963 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
965 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
971 flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, tbo->resource);
972 amdgpu_ttm_gart_bind(adev, tbo, flags);
976 * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
978 * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
981 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
984 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
985 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
987 /* if the pages have userptr pinning then clear that first */
989 amdgpu_ttm_tt_unpin_userptr(bdev, ttm);
990 } else if (ttm->sg && gtt->gobj->import_attach) {
991 struct dma_buf_attachment *attach;
993 attach = gtt->gobj->import_attach;
994 dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
1001 if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
1004 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1005 amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages);
1009 static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev,
1012 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1015 put_task_struct(gtt->usertask);
1017 ttm_tt_fini(>t->ttm);
1022 * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1024 * @bo: The buffer object to create a GTT ttm_tt object around
1025 * @page_flags: Page flags to be added to the ttm_tt object
1027 * Called by ttm_tt_create().
1029 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
1030 uint32_t page_flags)
1032 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1033 struct amdgpu_ttm_tt *gtt;
1034 enum ttm_caching caching;
1036 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
1040 gtt->gobj = &bo->base;
1042 if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
1043 caching = ttm_write_combined;
1045 caching = ttm_cached;
1047 /* allocate space for the uninitialized page entries */
1048 if (ttm_sg_tt_init(>t->ttm, bo, page_flags, caching)) {
1056 * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1058 * Map the pages of a ttm_tt object to an address space visible
1059 * to the underlying device.
1061 static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
1063 struct ttm_operation_ctx *ctx)
1065 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1066 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1070 /* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1072 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
1078 if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
1081 ret = ttm_pool_alloc(&adev->mman.bdev.pool, ttm, ctx);
1085 for (i = 0; i < ttm->num_pages; ++i)
1086 ttm->pages[i]->mapping = bdev->dev_mapping;
1092 * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1094 * Unmaps pages of a ttm_tt object from the device address space and
1095 * unpopulates the page array backing it.
1097 static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
1100 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1101 struct amdgpu_device *adev;
1104 amdgpu_ttm_backend_unbind(bdev, ttm);
1107 amdgpu_ttm_tt_set_user_pages(ttm, NULL);
1113 if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
1116 for (i = 0; i < ttm->num_pages; ++i)
1117 ttm->pages[i]->mapping = NULL;
1119 adev = amdgpu_ttm_adev(bdev);
1120 return ttm_pool_free(&adev->mman.bdev.pool, ttm);
1124 * amdgpu_ttm_tt_get_userptr - Return the userptr GTT ttm_tt for the current
1127 * @tbo: The ttm_buffer_object that contains the userptr
1128 * @user_addr: The returned value
1130 int amdgpu_ttm_tt_get_userptr(const struct ttm_buffer_object *tbo,
1131 uint64_t *user_addr)
1133 struct amdgpu_ttm_tt *gtt;
1138 gtt = (void *)tbo->ttm;
1139 *user_addr = gtt->userptr;
1144 * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1147 * @bo: The ttm_buffer_object to bind this userptr to
1148 * @addr: The address in the current tasks VM space to use
1149 * @flags: Requirements of userptr object.
1151 * Called by amdgpu_gem_userptr_ioctl() and kfd_ioctl_alloc_memory_of_gpu() to
1152 * bind userptr pages to current task and by kfd_ioctl_acquire_vm() to
1153 * initialize GPU VM for a KFD process.
1155 int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
1156 uint64_t addr, uint32_t flags)
1158 struct amdgpu_ttm_tt *gtt;
1161 /* TODO: We want a separate TTM object type for userptrs */
1162 bo->ttm = amdgpu_ttm_tt_create(bo, 0);
1163 if (bo->ttm == NULL)
1167 /* Set TTM_TT_FLAG_EXTERNAL before populate but after create. */
1168 bo->ttm->page_flags |= TTM_TT_FLAG_EXTERNAL;
1170 gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
1171 gtt->userptr = addr;
1172 gtt->userflags = flags;
1175 put_task_struct(gtt->usertask);
1176 gtt->usertask = current->group_leader;
1177 get_task_struct(gtt->usertask);
1183 * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1185 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1187 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1192 if (gtt->usertask == NULL)
1195 return gtt->usertask->mm;
1199 * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1200 * address range for the current task.
1203 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1204 unsigned long end, unsigned long *userptr)
1206 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1209 if (gtt == NULL || !gtt->userptr)
1212 /* Return false if no part of the ttm_tt object lies within
1215 size = (unsigned long)gtt->ttm.num_pages * PAGE_SIZE;
1216 if (gtt->userptr > end || gtt->userptr + size <= start)
1220 *userptr = gtt->userptr;
1225 * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
1227 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
1229 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1231 if (gtt == NULL || !gtt->userptr)
1238 * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1240 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1242 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1247 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1251 * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object
1253 * @ttm: The ttm_tt object to compute the flags for
1254 * @mem: The memory registry backing this ttm_tt object
1256 * Figure out the flags to use for a VM PDE (Page Directory Entry).
1258 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem)
1262 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1263 flags |= AMDGPU_PTE_VALID;
1265 if (mem && (mem->mem_type == TTM_PL_TT ||
1266 mem->mem_type == AMDGPU_PL_PREEMPT)) {
1267 flags |= AMDGPU_PTE_SYSTEM;
1269 if (ttm->caching == ttm_cached)
1270 flags |= AMDGPU_PTE_SNOOPED;
1273 if (mem && mem->mem_type == TTM_PL_VRAM &&
1274 mem->bus.caching == ttm_cached)
1275 flags |= AMDGPU_PTE_SNOOPED;
1281 * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1283 * @adev: amdgpu_device pointer
1284 * @ttm: The ttm_tt object to compute the flags for
1285 * @mem: The memory registry backing this ttm_tt object
1287 * Figure out the flags to use for a VM PTE (Page Table Entry).
1289 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1290 struct ttm_resource *mem)
1292 uint64_t flags = amdgpu_ttm_tt_pde_flags(ttm, mem);
1294 flags |= adev->gart.gart_pte_flags;
1295 flags |= AMDGPU_PTE_READABLE;
1297 if (!amdgpu_ttm_tt_is_readonly(ttm))
1298 flags |= AMDGPU_PTE_WRITEABLE;
1304 * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1307 * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1308 * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1309 * it can find space for a new object and by ttm_bo_force_list_clean() which is
1310 * used to clean out a memory space.
1312 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1313 const struct ttm_place *place)
1315 struct dma_resv_iter resv_cursor;
1316 struct dma_fence *f;
1318 if (!amdgpu_bo_is_amdgpu_bo(bo))
1319 return ttm_bo_eviction_valuable(bo, place);
1322 if (bo->resource->mem_type == TTM_PL_SYSTEM)
1325 if (bo->type == ttm_bo_type_kernel &&
1326 !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)))
1329 /* If bo is a KFD BO, check if the bo belongs to the current process.
1330 * If true, then return false as any KFD process needs all its BOs to
1331 * be resident to run successfully
1333 dma_resv_for_each_fence(&resv_cursor, bo->base.resv,
1334 DMA_RESV_USAGE_BOOKKEEP, f) {
1335 if (amdkfd_fence_check_mm(f, current->mm))
1339 /* Preemptible BOs don't own system resources managed by the
1340 * driver (pages, VRAM, GART space). They point to resources
1341 * owned by someone else (e.g. pageable memory in user mode
1342 * or a DMABuf). They are used in a preemptible context so we
1343 * can guarantee no deadlocks and good QoS in case of MMU
1344 * notifiers or DMABuf move notifiers from the resource owner.
1346 if (bo->resource->mem_type == AMDGPU_PL_PREEMPT)
1349 if (bo->resource->mem_type == TTM_PL_TT &&
1350 amdgpu_bo_encrypted(ttm_to_amdgpu_bo(bo)))
1353 return ttm_bo_eviction_valuable(bo, place);
1356 static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
1357 void *buf, size_t size, bool write)
1360 uint64_t aligned_pos = ALIGN_DOWN(pos, 4);
1361 uint64_t bytes = 4 - (pos & 0x3);
1362 uint32_t shift = (pos & 0x3) * 8;
1363 uint32_t mask = 0xffffffff << shift;
1367 mask &= 0xffffffff >> (bytes - size) * 8;
1371 if (mask != 0xffffffff) {
1372 amdgpu_device_mm_access(adev, aligned_pos, &value, 4, false);
1375 value |= (*(uint32_t *)buf << shift) & mask;
1376 amdgpu_device_mm_access(adev, aligned_pos, &value, 4, true);
1378 value = (value & mask) >> shift;
1379 memcpy(buf, &value, bytes);
1382 amdgpu_device_mm_access(adev, aligned_pos, buf, 4, write);
1391 static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
1392 unsigned long offset, void *buf,
1395 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1396 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1397 struct amdgpu_res_cursor src_mm;
1398 struct amdgpu_job *job;
1399 struct dma_fence *fence;
1400 uint64_t src_addr, dst_addr;
1401 unsigned int num_dw;
1404 if (len != PAGE_SIZE)
1407 if (!adev->mman.sdma_access_ptr)
1410 if (!drm_dev_enter(adev_to_drm(adev), &idx))
1414 memcpy(adev->mman.sdma_access_ptr, buf, len);
1416 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
1417 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.entity,
1418 AMDGPU_FENCE_OWNER_UNDEFINED,
1419 num_dw * 4, AMDGPU_IB_POOL_DELAYED,
1424 amdgpu_res_first(abo->tbo.resource, offset, len, &src_mm);
1425 src_addr = amdgpu_ttm_domain_start(adev, bo->resource->mem_type) +
1427 dst_addr = amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
1429 swap(src_addr, dst_addr);
1431 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
1434 amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
1435 WARN_ON(job->ibs[0].length_dw > num_dw);
1437 fence = amdgpu_job_submit(job);
1439 if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
1441 dma_fence_put(fence);
1444 memcpy(buf, adev->mman.sdma_access_ptr, len);
1451 * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1453 * @bo: The buffer object to read/write
1454 * @offset: Offset into buffer object
1455 * @buf: Secondary buffer to write/read from
1456 * @len: Length in bytes of access
1457 * @write: true if writing
1459 * This is used to access VRAM that backs a buffer object via MMIO
1460 * access for debugging purposes.
1462 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1463 unsigned long offset, void *buf, int len,
1466 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1467 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1468 struct amdgpu_res_cursor cursor;
1471 if (bo->resource->mem_type != TTM_PL_VRAM)
1474 if (amdgpu_device_has_timeouts_enabled(adev) &&
1475 !amdgpu_ttm_access_memory_sdma(bo, offset, buf, len, write))
1478 amdgpu_res_first(bo->resource, offset, len, &cursor);
1479 while (cursor.remaining) {
1480 size_t count, size = cursor.size;
1481 loff_t pos = cursor.start;
1483 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
1486 /* using MM to access rest vram and handle un-aligned address */
1489 amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write);
1494 amdgpu_res_next(&cursor, cursor.size);
1501 amdgpu_bo_delete_mem_notify(struct ttm_buffer_object *bo)
1503 amdgpu_bo_move_notify(bo, false, NULL);
1506 static struct ttm_device_funcs amdgpu_bo_driver = {
1507 .ttm_tt_create = &amdgpu_ttm_tt_create,
1508 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1509 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1510 .ttm_tt_destroy = &amdgpu_ttm_backend_destroy,
1511 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1512 .evict_flags = &amdgpu_evict_flags,
1513 .move = &amdgpu_bo_move,
1514 .delete_mem_notify = &amdgpu_bo_delete_mem_notify,
1515 .release_notify = &amdgpu_bo_release_notify,
1516 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1517 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1518 .access_memory = &amdgpu_ttm_access_memory,
1522 * Firmware Reservation functions
1525 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1527 * @adev: amdgpu_device pointer
1529 * free fw reserved vram if it has been reserved.
1531 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1533 amdgpu_bo_free_kernel(&adev->mman.fw_vram_usage_reserved_bo,
1534 NULL, &adev->mman.fw_vram_usage_va);
1538 * Driver Reservation functions
1541 * amdgpu_ttm_drv_reserve_vram_fini - free drv reserved vram
1543 * @adev: amdgpu_device pointer
1545 * free drv reserved vram if it has been reserved.
1547 static void amdgpu_ttm_drv_reserve_vram_fini(struct amdgpu_device *adev)
1549 amdgpu_bo_free_kernel(&adev->mman.drv_vram_usage_reserved_bo,
1551 &adev->mman.drv_vram_usage_va);
1555 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1557 * @adev: amdgpu_device pointer
1559 * create bo vram reservation from fw.
1561 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1563 uint64_t vram_size = adev->gmc.visible_vram_size;
1565 adev->mman.fw_vram_usage_va = NULL;
1566 adev->mman.fw_vram_usage_reserved_bo = NULL;
1568 if (adev->mman.fw_vram_usage_size == 0 ||
1569 adev->mman.fw_vram_usage_size > vram_size)
1572 return amdgpu_bo_create_kernel_at(adev,
1573 adev->mman.fw_vram_usage_start_offset,
1574 adev->mman.fw_vram_usage_size,
1575 &adev->mman.fw_vram_usage_reserved_bo,
1576 &adev->mman.fw_vram_usage_va);
1580 * amdgpu_ttm_drv_reserve_vram_init - create bo vram reservation from driver
1582 * @adev: amdgpu_device pointer
1584 * create bo vram reservation from drv.
1586 static int amdgpu_ttm_drv_reserve_vram_init(struct amdgpu_device *adev)
1588 u64 vram_size = adev->gmc.visible_vram_size;
1590 adev->mman.drv_vram_usage_va = NULL;
1591 adev->mman.drv_vram_usage_reserved_bo = NULL;
1593 if (adev->mman.drv_vram_usage_size == 0 ||
1594 adev->mman.drv_vram_usage_size > vram_size)
1597 return amdgpu_bo_create_kernel_at(adev,
1598 adev->mman.drv_vram_usage_start_offset,
1599 adev->mman.drv_vram_usage_size,
1600 &adev->mman.drv_vram_usage_reserved_bo,
1601 &adev->mman.drv_vram_usage_va);
1605 * Memoy training reservation functions
1609 * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram
1611 * @adev: amdgpu_device pointer
1613 * free memory training reserved vram if it has been reserved.
1615 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev)
1617 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1619 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
1620 amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL);
1626 static void amdgpu_ttm_training_data_block_init(struct amdgpu_device *adev)
1628 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1630 memset(ctx, 0, sizeof(*ctx));
1632 ctx->c2p_train_data_offset =
1633 ALIGN((adev->gmc.mc_vram_size - adev->mman.discovery_tmr_size - SZ_1M), SZ_1M);
1634 ctx->p2c_train_data_offset =
1635 (adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET);
1636 ctx->train_data_size =
1637 GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES;
1639 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
1640 ctx->train_data_size,
1641 ctx->p2c_train_data_offset,
1642 ctx->c2p_train_data_offset);
1646 * reserve TMR memory at the top of VRAM which holds
1647 * IP Discovery data and is protected by PSP.
1649 static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
1652 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1653 bool mem_train_support = false;
1655 if (!amdgpu_sriov_vf(adev)) {
1656 if (amdgpu_atomfirmware_mem_training_supported(adev))
1657 mem_train_support = true;
1659 DRM_DEBUG("memory training does not support!\n");
1663 * Query reserved tmr size through atom firmwareinfo for Sienna_Cichlid and onwards for all
1664 * the use cases (IP discovery/G6 memory training/profiling/diagnostic data.etc)
1666 * Otherwise, fallback to legacy approach to check and reserve tmr block for ip
1667 * discovery data and G6 memory training data respectively
1669 adev->mman.discovery_tmr_size =
1670 amdgpu_atomfirmware_get_fw_reserved_fb_size(adev);
1671 if (!adev->mman.discovery_tmr_size)
1672 adev->mman.discovery_tmr_size = DISCOVERY_TMR_OFFSET;
1674 if (mem_train_support) {
1675 /* reserve vram for mem train according to TMR location */
1676 amdgpu_ttm_training_data_block_init(adev);
1677 ret = amdgpu_bo_create_kernel_at(adev,
1678 ctx->c2p_train_data_offset,
1679 ctx->train_data_size,
1683 DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
1684 amdgpu_ttm_training_reserve_vram_fini(adev);
1687 ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS;
1690 ret = amdgpu_bo_create_kernel_at(adev,
1691 adev->gmc.real_vram_size - adev->mman.discovery_tmr_size,
1692 adev->mman.discovery_tmr_size,
1693 &adev->mman.discovery_memory,
1696 DRM_ERROR("alloc tmr failed(%d)!\n", ret);
1697 amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1705 * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1706 * gtt/vram related fields.
1708 * This initializes all of the memory space pools that the TTM layer
1709 * will need such as the GTT space (system memory mapped to the device),
1710 * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1711 * can be mapped per VMID.
1713 int amdgpu_ttm_init(struct amdgpu_device *adev)
1718 mutex_init(&adev->mman.gtt_window_lock);
1720 /* No others user of address space so set it to 0 */
1721 r = ttm_device_init(&adev->mman.bdev, &amdgpu_bo_driver, adev->dev,
1722 adev_to_drm(adev)->anon_inode->i_mapping,
1723 adev_to_drm(adev)->vma_offset_manager,
1725 dma_addressing_limited(adev->dev));
1727 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1730 adev->mman.initialized = true;
1732 /* Initialize VRAM pool with all of VRAM divided into pages */
1733 r = amdgpu_vram_mgr_init(adev);
1735 DRM_ERROR("Failed initializing VRAM heap.\n");
1739 /* Change the size here instead of the init above so only lpfn is affected */
1740 amdgpu_ttm_set_buffer_funcs_status(adev, false);
1743 if (adev->gmc.xgmi.connected_to_cpu)
1744 adev->mman.aper_base_kaddr = ioremap_cache(adev->gmc.aper_base,
1745 adev->gmc.visible_vram_size);
1749 adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base,
1750 adev->gmc.visible_vram_size);
1754 *The reserved vram for firmware must be pinned to the specified
1755 *place on the VRAM, so reserve it early.
1757 r = amdgpu_ttm_fw_reserve_vram_init(adev);
1763 *The reserved vram for driver must be pinned to the specified
1764 *place on the VRAM, so reserve it early.
1766 r = amdgpu_ttm_drv_reserve_vram_init(adev);
1771 * only NAVI10 and onwards ASIC support for IP discovery.
1772 * If IP discovery enabled, a block of memory should be
1773 * reserved for IP discovey.
1775 if (adev->mman.discovery_bin) {
1776 r = amdgpu_ttm_reserve_tmr(adev);
1781 /* allocate memory as required for VGA
1782 * This is used for VGA emulation and pre-OS scanout buffers to
1783 * avoid display artifacts while transitioning between pre-OS
1785 r = amdgpu_bo_create_kernel_at(adev, 0, adev->mman.stolen_vga_size,
1786 &adev->mman.stolen_vga_memory,
1790 r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
1791 adev->mman.stolen_extended_size,
1792 &adev->mman.stolen_extended_memory,
1796 r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_reserved_offset,
1797 adev->mman.stolen_reserved_size,
1798 &adev->mman.stolen_reserved_memory,
1803 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1804 (unsigned) (adev->gmc.real_vram_size / (1024 * 1024)));
1806 /* Compute GTT size, either based on 1/2 the size of RAM size
1807 * or whatever the user passed on module init */
1808 if (amdgpu_gtt_size == -1) {
1812 /* Certain GL unit tests for large textures can cause problems
1813 * with the OOM killer since there is no way to link this memory
1814 * to a process. This was originally mitigated (but not necessarily
1815 * eliminated) by limiting the GTT size. The problem is this limit
1816 * is often too low for many modern games so just make the limit 1/2
1817 * of system memory which aligns with TTM. The OOM accounting needs
1818 * to be addressed, but we shouldn't prevent common 3D applications
1819 * from being usable just to potentially mitigate that corner case.
1821 gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1822 (u64)si.totalram * si.mem_unit / 2);
1824 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1827 /* Initialize GTT memory pool */
1828 r = amdgpu_gtt_mgr_init(adev, gtt_size);
1830 DRM_ERROR("Failed initializing GTT heap.\n");
1833 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1834 (unsigned)(gtt_size / (1024 * 1024)));
1836 /* Initialize preemptible memory pool */
1837 r = amdgpu_preempt_mgr_init(adev);
1839 DRM_ERROR("Failed initializing PREEMPT heap.\n");
1843 /* Initialize various on-chip memory pools */
1844 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GDS, adev->gds.gds_size);
1846 DRM_ERROR("Failed initializing GDS heap.\n");
1850 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GWS, adev->gds.gws_size);
1852 DRM_ERROR("Failed initializing gws heap.\n");
1856 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_OA, adev->gds.oa_size);
1858 DRM_ERROR("Failed initializing oa heap.\n");
1862 if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
1863 AMDGPU_GEM_DOMAIN_GTT,
1864 &adev->mman.sdma_access_bo, NULL,
1865 &adev->mman.sdma_access_ptr))
1866 DRM_WARN("Debug VRAM access will use slowpath MM access\n");
1872 * amdgpu_ttm_fini - De-initialize the TTM memory pools
1874 void amdgpu_ttm_fini(struct amdgpu_device *adev)
1877 if (!adev->mman.initialized)
1880 amdgpu_ttm_training_reserve_vram_fini(adev);
1881 /* return the stolen vga memory back to VRAM */
1882 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
1883 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
1884 /* return the IP Discovery TMR memory back to VRAM */
1885 amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1886 if (adev->mman.stolen_reserved_size)
1887 amdgpu_bo_free_kernel(&adev->mman.stolen_reserved_memory,
1889 amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo, NULL,
1890 &adev->mman.sdma_access_ptr);
1891 amdgpu_ttm_fw_reserve_vram_fini(adev);
1892 amdgpu_ttm_drv_reserve_vram_fini(adev);
1894 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
1896 if (adev->mman.aper_base_kaddr)
1897 iounmap(adev->mman.aper_base_kaddr);
1898 adev->mman.aper_base_kaddr = NULL;
1903 amdgpu_vram_mgr_fini(adev);
1904 amdgpu_gtt_mgr_fini(adev);
1905 amdgpu_preempt_mgr_fini(adev);
1906 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GDS);
1907 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GWS);
1908 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
1909 ttm_device_fini(&adev->mman.bdev);
1910 adev->mman.initialized = false;
1911 DRM_INFO("amdgpu: ttm finalized\n");
1915 * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
1917 * @adev: amdgpu_device pointer
1918 * @enable: true when we can use buffer functions.
1920 * Enable/disable use of buffer functions during suspend/resume. This should
1921 * only be called at bootup or when userspace isn't running.
1923 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
1925 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
1929 if (!adev->mman.initialized || amdgpu_in_reset(adev) ||
1930 adev->mman.buffer_funcs_enabled == enable)
1934 struct amdgpu_ring *ring;
1935 struct drm_gpu_scheduler *sched;
1937 ring = adev->mman.buffer_funcs_ring;
1938 sched = &ring->sched;
1939 r = drm_sched_entity_init(&adev->mman.entity,
1940 DRM_SCHED_PRIORITY_KERNEL, &sched,
1943 DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
1948 drm_sched_entity_destroy(&adev->mman.entity);
1949 dma_fence_put(man->move);
1953 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1955 size = adev->gmc.real_vram_size;
1957 size = adev->gmc.visible_vram_size;
1959 adev->mman.buffer_funcs_enabled = enable;
1962 static int amdgpu_ttm_prepare_job(struct amdgpu_device *adev,
1964 unsigned int num_dw,
1965 struct dma_resv *resv,
1966 bool vm_needs_flush,
1967 struct amdgpu_job **job)
1969 enum amdgpu_ib_pool_type pool = direct_submit ?
1970 AMDGPU_IB_POOL_DIRECT :
1971 AMDGPU_IB_POOL_DELAYED;
1974 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.entity,
1975 AMDGPU_FENCE_OWNER_UNDEFINED,
1976 num_dw * 4, pool, job);
1980 if (vm_needs_flush) {
1981 (*job)->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gmc.pdb0_bo ?
1984 (*job)->vm_needs_flush = true;
1989 return drm_sched_job_add_resv_dependencies(&(*job)->base, resv,
1990 DMA_RESV_USAGE_BOOKKEEP);
1993 int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1994 uint64_t dst_offset, uint32_t byte_count,
1995 struct dma_resv *resv,
1996 struct dma_fence **fence, bool direct_submit,
1997 bool vm_needs_flush, bool tmz)
1999 struct amdgpu_device *adev = ring->adev;
2000 unsigned num_loops, num_dw;
2001 struct amdgpu_job *job;
2006 if (!direct_submit && !ring->sched.ready) {
2007 DRM_ERROR("Trying to move memory with ring turned off.\n");
2011 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
2012 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
2013 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->copy_num_dw, 8);
2014 r = amdgpu_ttm_prepare_job(adev, direct_submit, num_dw,
2015 resv, vm_needs_flush, &job);
2019 for (i = 0; i < num_loops; i++) {
2020 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2022 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
2023 dst_offset, cur_size_in_bytes, tmz);
2025 src_offset += cur_size_in_bytes;
2026 dst_offset += cur_size_in_bytes;
2027 byte_count -= cur_size_in_bytes;
2030 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2031 WARN_ON(job->ibs[0].length_dw > num_dw);
2033 r = amdgpu_job_submit_direct(job, ring, fence);
2035 *fence = amdgpu_job_submit(job);
2042 amdgpu_job_free(job);
2043 DRM_ERROR("Error scheduling IBs (%d)\n", r);
2047 static int amdgpu_ttm_fill_mem(struct amdgpu_ring *ring, uint32_t src_data,
2048 uint64_t dst_addr, uint32_t byte_count,
2049 struct dma_resv *resv,
2050 struct dma_fence **fence,
2051 bool vm_needs_flush)
2053 struct amdgpu_device *adev = ring->adev;
2054 unsigned int num_loops, num_dw;
2055 struct amdgpu_job *job;
2060 max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
2061 num_loops = DIV_ROUND_UP_ULL(byte_count, max_bytes);
2062 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->fill_num_dw, 8);
2063 r = amdgpu_ttm_prepare_job(adev, false, num_dw, resv, vm_needs_flush,
2068 for (i = 0; i < num_loops; i++) {
2069 uint32_t cur_size = min(byte_count, max_bytes);
2071 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data, dst_addr,
2074 dst_addr += cur_size;
2075 byte_count -= cur_size;
2078 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2079 WARN_ON(job->ibs[0].length_dw > num_dw);
2080 *fence = amdgpu_job_submit(job);
2084 int amdgpu_fill_buffer(struct amdgpu_bo *bo,
2086 struct dma_resv *resv,
2087 struct dma_fence **f)
2089 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2090 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2091 struct dma_fence *fence = NULL;
2092 struct amdgpu_res_cursor dst;
2095 if (!adev->mman.buffer_funcs_enabled) {
2096 DRM_ERROR("Trying to clear memory with ring turned off.\n");
2100 amdgpu_res_first(bo->tbo.resource, 0, amdgpu_bo_size(bo), &dst);
2102 mutex_lock(&adev->mman.gtt_window_lock);
2103 while (dst.remaining) {
2104 struct dma_fence *next;
2105 uint64_t cur_size, to;
2107 /* Never fill more than 256MiB at once to avoid timeouts */
2108 cur_size = min(dst.size, 256ULL << 20);
2110 r = amdgpu_ttm_map_buffer(&bo->tbo, bo->tbo.resource, &dst,
2111 1, ring, false, &cur_size, &to);
2115 r = amdgpu_ttm_fill_mem(ring, src_data, to, cur_size, resv,
2120 dma_fence_put(fence);
2123 amdgpu_res_next(&dst, cur_size);
2126 mutex_unlock(&adev->mman.gtt_window_lock);
2128 *f = dma_fence_get(fence);
2129 dma_fence_put(fence);
2134 * amdgpu_ttm_evict_resources - evict memory buffers
2135 * @adev: amdgpu device object
2136 * @mem_type: evicted BO's memory type
2138 * Evicts all @mem_type buffers on the lru list of the memory type.
2141 * 0 for success or a negative error code on failure.
2143 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type)
2145 struct ttm_resource_manager *man;
2153 man = ttm_manager_type(&adev->mman.bdev, mem_type);
2156 DRM_ERROR("Trying to evict invalid memory type\n");
2160 return ttm_resource_manager_evict_all(&adev->mman.bdev, man);
2163 #if defined(CONFIG_DEBUG_FS)
2165 static int amdgpu_ttm_page_pool_show(struct seq_file *m, void *unused)
2167 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
2169 return ttm_pool_debugfs(&adev->mman.bdev.pool, m);
2172 DEFINE_SHOW_ATTRIBUTE(amdgpu_ttm_page_pool);
2175 * amdgpu_ttm_vram_read - Linear read access to VRAM
2177 * Accesses VRAM via MMIO for debugging purposes.
2179 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
2180 size_t size, loff_t *pos)
2182 struct amdgpu_device *adev = file_inode(f)->i_private;
2185 if (size & 0x3 || *pos & 0x3)
2188 if (*pos >= adev->gmc.mc_vram_size)
2191 size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos));
2193 size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4);
2194 uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ];
2196 amdgpu_device_vram_access(adev, *pos, value, bytes, false);
2197 if (copy_to_user(buf, value, bytes))
2210 * amdgpu_ttm_vram_write - Linear write access to VRAM
2212 * Accesses VRAM via MMIO for debugging purposes.
2214 static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
2215 size_t size, loff_t *pos)
2217 struct amdgpu_device *adev = file_inode(f)->i_private;
2221 if (size & 0x3 || *pos & 0x3)
2224 if (*pos >= adev->gmc.mc_vram_size)
2230 if (*pos >= adev->gmc.mc_vram_size)
2233 r = get_user(value, (uint32_t *)buf);
2237 amdgpu_device_mm_access(adev, *pos, &value, 4, true);
2248 static const struct file_operations amdgpu_ttm_vram_fops = {
2249 .owner = THIS_MODULE,
2250 .read = amdgpu_ttm_vram_read,
2251 .write = amdgpu_ttm_vram_write,
2252 .llseek = default_llseek,
2256 * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2258 * This function is used to read memory that has been mapped to the
2259 * GPU and the known addresses are not physical addresses but instead
2260 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2262 static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
2263 size_t size, loff_t *pos)
2265 struct amdgpu_device *adev = file_inode(f)->i_private;
2266 struct iommu_domain *dom;
2270 /* retrieve the IOMMU domain if any for this device */
2271 dom = iommu_get_domain_for_dev(adev->dev);
2274 phys_addr_t addr = *pos & PAGE_MASK;
2275 loff_t off = *pos & ~PAGE_MASK;
2276 size_t bytes = PAGE_SIZE - off;
2281 bytes = bytes < size ? bytes : size;
2283 /* Translate the bus address to a physical address. If
2284 * the domain is NULL it means there is no IOMMU active
2285 * and the address translation is the identity
2287 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2289 pfn = addr >> PAGE_SHIFT;
2290 if (!pfn_valid(pfn))
2293 p = pfn_to_page(pfn);
2294 if (p->mapping != adev->mman.bdev.dev_mapping)
2297 ptr = kmap_local_page(p);
2298 r = copy_to_user(buf, ptr + off, bytes);
2312 * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2314 * This function is used to write memory that has been mapped to the
2315 * GPU and the known addresses are not physical addresses but instead
2316 * bus addresses (e.g., what you'd put in an IB or ring buffer).
2318 static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
2319 size_t size, loff_t *pos)
2321 struct amdgpu_device *adev = file_inode(f)->i_private;
2322 struct iommu_domain *dom;
2326 dom = iommu_get_domain_for_dev(adev->dev);
2329 phys_addr_t addr = *pos & PAGE_MASK;
2330 loff_t off = *pos & ~PAGE_MASK;
2331 size_t bytes = PAGE_SIZE - off;
2336 bytes = bytes < size ? bytes : size;
2338 addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2340 pfn = addr >> PAGE_SHIFT;
2341 if (!pfn_valid(pfn))
2344 p = pfn_to_page(pfn);
2345 if (p->mapping != adev->mman.bdev.dev_mapping)
2348 ptr = kmap_local_page(p);
2349 r = copy_from_user(ptr + off, buf, bytes);
2362 static const struct file_operations amdgpu_ttm_iomem_fops = {
2363 .owner = THIS_MODULE,
2364 .read = amdgpu_iomem_read,
2365 .write = amdgpu_iomem_write,
2366 .llseek = default_llseek
2371 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
2373 #if defined(CONFIG_DEBUG_FS)
2374 struct drm_minor *minor = adev_to_drm(adev)->primary;
2375 struct dentry *root = minor->debugfs_root;
2377 debugfs_create_file_size("amdgpu_vram", 0444, root, adev,
2378 &amdgpu_ttm_vram_fops, adev->gmc.mc_vram_size);
2379 debugfs_create_file("amdgpu_iomem", 0444, root, adev,
2380 &amdgpu_ttm_iomem_fops);
2381 debugfs_create_file("ttm_page_pool", 0444, root, adev,
2382 &amdgpu_ttm_page_pool_fops);
2383 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2385 root, "amdgpu_vram_mm");
2386 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2388 root, "amdgpu_gtt_mm");
2389 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2391 root, "amdgpu_gds_mm");
2392 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2394 root, "amdgpu_gws_mm");
2395 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2397 root, "amdgpu_oa_mm");