2 * Copyright 2009 Jerome Glisse.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32 #include <ttm/ttm_bo_api.h>
33 #include <ttm/ttm_bo_driver.h>
34 #include <ttm/ttm_placement.h>
35 #include <ttm/ttm_module.h>
36 #include <ttm/ttm_page_alloc.h>
38 #include <drm/amdgpu_drm.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/swiotlb.h>
42 #include <linux/swap.h>
43 #include <linux/pagemap.h>
44 #include <linux/debugfs.h>
46 #include "bif/bif_4_1_d.h"
48 #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
50 static int amdgpu_ttm_debugfs_init(struct amdgpu_device
*adev
);
51 static void amdgpu_ttm_debugfs_fini(struct amdgpu_device
*adev
);
53 static struct amdgpu_device
*amdgpu_get_adev(struct ttm_bo_device
*bdev
)
55 struct amdgpu_mman
*mman
;
56 struct amdgpu_device
*adev
;
58 mman
= container_of(bdev
, struct amdgpu_mman
, bdev
);
59 adev
= container_of(mman
, struct amdgpu_device
, mman
);
67 static int amdgpu_ttm_mem_global_init(struct drm_global_reference
*ref
)
69 return ttm_mem_global_init(ref
->object
);
72 static void amdgpu_ttm_mem_global_release(struct drm_global_reference
*ref
)
74 ttm_mem_global_release(ref
->object
);
77 static int amdgpu_ttm_global_init(struct amdgpu_device
*adev
)
79 struct drm_global_reference
*global_ref
;
82 adev
->mman
.mem_global_referenced
= false;
83 global_ref
= &adev
->mman
.mem_global_ref
;
84 global_ref
->global_type
= DRM_GLOBAL_TTM_MEM
;
85 global_ref
->size
= sizeof(struct ttm_mem_global
);
86 global_ref
->init
= &amdgpu_ttm_mem_global_init
;
87 global_ref
->release
= &amdgpu_ttm_mem_global_release
;
88 r
= drm_global_item_ref(global_ref
);
90 DRM_ERROR("Failed setting up TTM memory accounting "
95 adev
->mman
.bo_global_ref
.mem_glob
=
96 adev
->mman
.mem_global_ref
.object
;
97 global_ref
= &adev
->mman
.bo_global_ref
.ref
;
98 global_ref
->global_type
= DRM_GLOBAL_TTM_BO
;
99 global_ref
->size
= sizeof(struct ttm_bo_global
);
100 global_ref
->init
= &ttm_bo_global_init
;
101 global_ref
->release
= &ttm_bo_global_release
;
102 r
= drm_global_item_ref(global_ref
);
104 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
105 drm_global_item_unref(&adev
->mman
.mem_global_ref
);
109 adev
->mman
.mem_global_referenced
= true;
113 static void amdgpu_ttm_global_fini(struct amdgpu_device
*adev
)
115 if (adev
->mman
.mem_global_referenced
) {
116 drm_global_item_unref(&adev
->mman
.bo_global_ref
.ref
);
117 drm_global_item_unref(&adev
->mman
.mem_global_ref
);
118 adev
->mman
.mem_global_referenced
= false;
122 static int amdgpu_invalidate_caches(struct ttm_bo_device
*bdev
, uint32_t flags
)
127 static int amdgpu_init_mem_type(struct ttm_bo_device
*bdev
, uint32_t type
,
128 struct ttm_mem_type_manager
*man
)
130 struct amdgpu_device
*adev
;
132 adev
= amdgpu_get_adev(bdev
);
137 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
;
138 man
->available_caching
= TTM_PL_MASK_CACHING
;
139 man
->default_caching
= TTM_PL_FLAG_CACHED
;
142 man
->func
= &ttm_bo_manager_func
;
143 man
->gpu_offset
= adev
->mc
.gtt_start
;
144 man
->available_caching
= TTM_PL_MASK_CACHING
;
145 man
->default_caching
= TTM_PL_FLAG_CACHED
;
146 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
| TTM_MEMTYPE_FLAG_CMA
;
149 /* "On-card" video ram */
150 man
->func
= &ttm_bo_manager_func
;
151 man
->gpu_offset
= adev
->mc
.vram_start
;
152 man
->flags
= TTM_MEMTYPE_FLAG_FIXED
|
153 TTM_MEMTYPE_FLAG_MAPPABLE
;
154 man
->available_caching
= TTM_PL_FLAG_UNCACHED
| TTM_PL_FLAG_WC
;
155 man
->default_caching
= TTM_PL_FLAG_WC
;
160 /* On-chip GDS memory*/
161 man
->func
= &ttm_bo_manager_func
;
163 man
->flags
= TTM_MEMTYPE_FLAG_FIXED
| TTM_MEMTYPE_FLAG_CMA
;
164 man
->available_caching
= TTM_PL_FLAG_UNCACHED
;
165 man
->default_caching
= TTM_PL_FLAG_UNCACHED
;
168 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type
);
174 static void amdgpu_evict_flags(struct ttm_buffer_object
*bo
,
175 struct ttm_placement
*placement
)
177 struct amdgpu_bo
*rbo
;
178 static struct ttm_place placements
= {
181 .flags
= TTM_PL_MASK_CACHING
| TTM_PL_FLAG_SYSTEM
184 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo
)) {
185 placement
->placement
= &placements
;
186 placement
->busy_placement
= &placements
;
187 placement
->num_placement
= 1;
188 placement
->num_busy_placement
= 1;
191 rbo
= container_of(bo
, struct amdgpu_bo
, tbo
);
192 switch (bo
->mem
.mem_type
) {
194 if (rbo
->adev
->mman
.buffer_funcs_ring
->ready
== false)
195 amdgpu_ttm_placement_from_domain(rbo
, AMDGPU_GEM_DOMAIN_CPU
);
197 amdgpu_ttm_placement_from_domain(rbo
, AMDGPU_GEM_DOMAIN_GTT
);
201 amdgpu_ttm_placement_from_domain(rbo
, AMDGPU_GEM_DOMAIN_CPU
);
203 *placement
= rbo
->placement
;
206 static int amdgpu_verify_access(struct ttm_buffer_object
*bo
, struct file
*filp
)
208 struct amdgpu_bo
*rbo
= container_of(bo
, struct amdgpu_bo
, tbo
);
210 return drm_vma_node_verify_access(&rbo
->gem_base
.vma_node
, filp
);
213 static void amdgpu_move_null(struct ttm_buffer_object
*bo
,
214 struct ttm_mem_reg
*new_mem
)
216 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
218 BUG_ON(old_mem
->mm_node
!= NULL
);
220 new_mem
->mm_node
= NULL
;
223 static int amdgpu_move_blit(struct ttm_buffer_object
*bo
,
224 bool evict
, bool no_wait_gpu
,
225 struct ttm_mem_reg
*new_mem
,
226 struct ttm_mem_reg
*old_mem
)
228 struct amdgpu_device
*adev
;
229 struct amdgpu_ring
*ring
;
230 uint64_t old_start
, new_start
;
234 adev
= amdgpu_get_adev(bo
->bdev
);
235 ring
= adev
->mman
.buffer_funcs_ring
;
236 old_start
= old_mem
->start
<< PAGE_SHIFT
;
237 new_start
= new_mem
->start
<< PAGE_SHIFT
;
239 switch (old_mem
->mem_type
) {
241 old_start
+= adev
->mc
.vram_start
;
244 old_start
+= adev
->mc
.gtt_start
;
247 DRM_ERROR("Unknown placement %d\n", old_mem
->mem_type
);
250 switch (new_mem
->mem_type
) {
252 new_start
+= adev
->mc
.vram_start
;
255 new_start
+= adev
->mc
.gtt_start
;
258 DRM_ERROR("Unknown placement %d\n", old_mem
->mem_type
);
262 DRM_ERROR("Trying to move memory with ring turned off.\n");
266 BUILD_BUG_ON((PAGE_SIZE
% AMDGPU_GPU_PAGE_SIZE
) != 0);
268 r
= amdgpu_copy_buffer(ring
, old_start
, new_start
,
269 new_mem
->num_pages
* PAGE_SIZE
, /* bytes */
271 /* FIXME: handle copy error */
272 r
= ttm_bo_move_accel_cleanup(bo
, fence
,
273 evict
, no_wait_gpu
, new_mem
);
278 static int amdgpu_move_vram_ram(struct ttm_buffer_object
*bo
,
279 bool evict
, bool interruptible
,
281 struct ttm_mem_reg
*new_mem
)
283 struct amdgpu_device
*adev
;
284 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
285 struct ttm_mem_reg tmp_mem
;
286 struct ttm_place placements
;
287 struct ttm_placement placement
;
290 adev
= amdgpu_get_adev(bo
->bdev
);
292 tmp_mem
.mm_node
= NULL
;
293 placement
.num_placement
= 1;
294 placement
.placement
= &placements
;
295 placement
.num_busy_placement
= 1;
296 placement
.busy_placement
= &placements
;
299 placements
.flags
= TTM_PL_MASK_CACHING
| TTM_PL_FLAG_TT
;
300 r
= ttm_bo_mem_space(bo
, &placement
, &tmp_mem
,
301 interruptible
, no_wait_gpu
);
306 r
= ttm_tt_set_placement_caching(bo
->ttm
, tmp_mem
.placement
);
311 r
= ttm_tt_bind(bo
->ttm
, &tmp_mem
);
315 r
= amdgpu_move_blit(bo
, true, no_wait_gpu
, &tmp_mem
, old_mem
);
319 r
= ttm_bo_move_ttm(bo
, true, no_wait_gpu
, new_mem
);
321 ttm_bo_mem_put(bo
, &tmp_mem
);
325 static int amdgpu_move_ram_vram(struct ttm_buffer_object
*bo
,
326 bool evict
, bool interruptible
,
328 struct ttm_mem_reg
*new_mem
)
330 struct amdgpu_device
*adev
;
331 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
332 struct ttm_mem_reg tmp_mem
;
333 struct ttm_placement placement
;
334 struct ttm_place placements
;
337 adev
= amdgpu_get_adev(bo
->bdev
);
339 tmp_mem
.mm_node
= NULL
;
340 placement
.num_placement
= 1;
341 placement
.placement
= &placements
;
342 placement
.num_busy_placement
= 1;
343 placement
.busy_placement
= &placements
;
346 placements
.flags
= TTM_PL_MASK_CACHING
| TTM_PL_FLAG_TT
;
347 r
= ttm_bo_mem_space(bo
, &placement
, &tmp_mem
,
348 interruptible
, no_wait_gpu
);
352 r
= ttm_bo_move_ttm(bo
, true, no_wait_gpu
, &tmp_mem
);
356 r
= amdgpu_move_blit(bo
, true, no_wait_gpu
, new_mem
, old_mem
);
361 ttm_bo_mem_put(bo
, &tmp_mem
);
365 static int amdgpu_bo_move(struct ttm_buffer_object
*bo
,
366 bool evict
, bool interruptible
,
368 struct ttm_mem_reg
*new_mem
)
370 struct amdgpu_device
*adev
;
371 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
374 adev
= amdgpu_get_adev(bo
->bdev
);
375 if (old_mem
->mem_type
== TTM_PL_SYSTEM
&& bo
->ttm
== NULL
) {
376 amdgpu_move_null(bo
, new_mem
);
379 if ((old_mem
->mem_type
== TTM_PL_TT
&&
380 new_mem
->mem_type
== TTM_PL_SYSTEM
) ||
381 (old_mem
->mem_type
== TTM_PL_SYSTEM
&&
382 new_mem
->mem_type
== TTM_PL_TT
)) {
384 amdgpu_move_null(bo
, new_mem
);
387 if (adev
->mman
.buffer_funcs
== NULL
||
388 adev
->mman
.buffer_funcs_ring
== NULL
||
389 !adev
->mman
.buffer_funcs_ring
->ready
) {
394 if (old_mem
->mem_type
== TTM_PL_VRAM
&&
395 new_mem
->mem_type
== TTM_PL_SYSTEM
) {
396 r
= amdgpu_move_vram_ram(bo
, evict
, interruptible
,
397 no_wait_gpu
, new_mem
);
398 } else if (old_mem
->mem_type
== TTM_PL_SYSTEM
&&
399 new_mem
->mem_type
== TTM_PL_VRAM
) {
400 r
= amdgpu_move_ram_vram(bo
, evict
, interruptible
,
401 no_wait_gpu
, new_mem
);
403 r
= amdgpu_move_blit(bo
, evict
, no_wait_gpu
, new_mem
, old_mem
);
408 r
= ttm_bo_move_memcpy(bo
, evict
, no_wait_gpu
, new_mem
);
414 /* update statistics */
415 atomic64_add((u64
)bo
->num_pages
<< PAGE_SHIFT
, &adev
->num_bytes_moved
);
419 static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
421 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
422 struct amdgpu_device
*adev
= amdgpu_get_adev(bdev
);
424 mem
->bus
.addr
= NULL
;
426 mem
->bus
.size
= mem
->num_pages
<< PAGE_SHIFT
;
428 mem
->bus
.is_iomem
= false;
429 if (!(man
->flags
& TTM_MEMTYPE_FLAG_MAPPABLE
))
431 switch (mem
->mem_type
) {
438 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
439 /* check if it's visible */
440 if ((mem
->bus
.offset
+ mem
->bus
.size
) > adev
->mc
.visible_vram_size
)
442 mem
->bus
.base
= adev
->mc
.aper_base
;
443 mem
->bus
.is_iomem
= true;
446 * Alpha: use bus.addr to hold the ioremap() return,
447 * so we can modify bus.base below.
449 if (mem
->placement
& TTM_PL_FLAG_WC
)
451 ioremap_wc(mem
->bus
.base
+ mem
->bus
.offset
,
455 ioremap_nocache(mem
->bus
.base
+ mem
->bus
.offset
,
459 * Alpha: Use just the bus offset plus
460 * the hose/domain memory base for bus.base.
461 * It then can be used to build PTEs for VRAM
462 * access, as done in ttm_bo_vm_fault().
464 mem
->bus
.base
= (mem
->bus
.base
& 0x0ffffffffUL
) +
465 adev
->ddev
->hose
->dense_mem_base
;
474 static void amdgpu_ttm_io_mem_free(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
479 * TTM backend functions.
481 struct amdgpu_ttm_tt
{
482 struct ttm_dma_tt ttm
;
483 struct amdgpu_device
*adev
;
486 struct mm_struct
*usermm
;
490 /* prepare the sg table with the user pages */
491 static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt
*ttm
)
493 struct amdgpu_device
*adev
= amdgpu_get_adev(ttm
->bdev
);
494 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
495 unsigned pinned
= 0, nents
;
498 int write
= !(gtt
->userflags
& AMDGPU_GEM_USERPTR_READONLY
);
499 enum dma_data_direction direction
= write
?
500 DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
;
502 if (current
->mm
!= gtt
->usermm
)
505 if (gtt
->userflags
& AMDGPU_GEM_USERPTR_ANONONLY
) {
506 /* check that we only pin down anonymous memory
507 to prevent problems with writeback */
508 unsigned long end
= gtt
->userptr
+ ttm
->num_pages
* PAGE_SIZE
;
509 struct vm_area_struct
*vma
;
511 vma
= find_vma(gtt
->usermm
, gtt
->userptr
);
512 if (!vma
|| vma
->vm_file
|| vma
->vm_end
< end
)
517 unsigned num_pages
= ttm
->num_pages
- pinned
;
518 uint64_t userptr
= gtt
->userptr
+ pinned
* PAGE_SIZE
;
519 struct page
**pages
= ttm
->pages
+ pinned
;
521 r
= get_user_pages(current
, current
->mm
, userptr
, num_pages
,
522 write
, 0, pages
, NULL
);
528 } while (pinned
< ttm
->num_pages
);
530 r
= sg_alloc_table_from_pages(ttm
->sg
, ttm
->pages
, ttm
->num_pages
, 0,
531 ttm
->num_pages
<< PAGE_SHIFT
,
537 nents
= dma_map_sg(adev
->dev
, ttm
->sg
->sgl
, ttm
->sg
->nents
, direction
);
538 if (nents
!= ttm
->sg
->nents
)
541 drm_prime_sg_to_page_addr_arrays(ttm
->sg
, ttm
->pages
,
542 gtt
->ttm
.dma_address
, ttm
->num_pages
);
550 release_pages(ttm
->pages
, pinned
, 0);
554 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt
*ttm
)
556 struct amdgpu_device
*adev
= amdgpu_get_adev(ttm
->bdev
);
557 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
558 struct sg_page_iter sg_iter
;
560 int write
= !(gtt
->userflags
& AMDGPU_GEM_USERPTR_READONLY
);
561 enum dma_data_direction direction
= write
?
562 DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
;
564 /* double check that we don't free the table twice */
568 /* free the sg table and pages again */
569 dma_unmap_sg(adev
->dev
, ttm
->sg
->sgl
, ttm
->sg
->nents
, direction
);
571 for_each_sg_page(ttm
->sg
->sgl
, &sg_iter
, ttm
->sg
->nents
, 0) {
572 struct page
*page
= sg_page_iter_page(&sg_iter
);
573 if (!(gtt
->userflags
& AMDGPU_GEM_USERPTR_READONLY
))
574 set_page_dirty(page
);
576 mark_page_accessed(page
);
577 page_cache_release(page
);
580 sg_free_table(ttm
->sg
);
583 static int amdgpu_ttm_backend_bind(struct ttm_tt
*ttm
,
584 struct ttm_mem_reg
*bo_mem
)
586 struct amdgpu_ttm_tt
*gtt
= (void*)ttm
;
587 uint32_t flags
= amdgpu_ttm_tt_pte_flags(gtt
->adev
, ttm
, bo_mem
);
591 r
= amdgpu_ttm_tt_pin_userptr(ttm
);
593 DRM_ERROR("failed to pin userptr\n");
597 gtt
->offset
= (unsigned long)(bo_mem
->start
<< PAGE_SHIFT
);
598 if (!ttm
->num_pages
) {
599 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
600 ttm
->num_pages
, bo_mem
, ttm
);
603 if (bo_mem
->mem_type
== AMDGPU_PL_GDS
||
604 bo_mem
->mem_type
== AMDGPU_PL_GWS
||
605 bo_mem
->mem_type
== AMDGPU_PL_OA
)
608 r
= amdgpu_gart_bind(gtt
->adev
, gtt
->offset
, ttm
->num_pages
,
609 ttm
->pages
, gtt
->ttm
.dma_address
, flags
);
612 DRM_ERROR("failed to bind %lu pages at 0x%08X\n",
613 ttm
->num_pages
, (unsigned)gtt
->offset
);
619 static int amdgpu_ttm_backend_unbind(struct ttm_tt
*ttm
)
621 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
623 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
624 if (gtt
->adev
->gart
.ready
)
625 amdgpu_gart_unbind(gtt
->adev
, gtt
->offset
, ttm
->num_pages
);
628 amdgpu_ttm_tt_unpin_userptr(ttm
);
633 static void amdgpu_ttm_backend_destroy(struct ttm_tt
*ttm
)
635 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
637 ttm_dma_tt_fini(>t
->ttm
);
641 static struct ttm_backend_func amdgpu_backend_func
= {
642 .bind
= &amdgpu_ttm_backend_bind
,
643 .unbind
= &amdgpu_ttm_backend_unbind
,
644 .destroy
= &amdgpu_ttm_backend_destroy
,
647 static struct ttm_tt
*amdgpu_ttm_tt_create(struct ttm_bo_device
*bdev
,
648 unsigned long size
, uint32_t page_flags
,
649 struct page
*dummy_read_page
)
651 struct amdgpu_device
*adev
;
652 struct amdgpu_ttm_tt
*gtt
;
654 adev
= amdgpu_get_adev(bdev
);
656 gtt
= kzalloc(sizeof(struct amdgpu_ttm_tt
), GFP_KERNEL
);
660 gtt
->ttm
.ttm
.func
= &amdgpu_backend_func
;
662 if (ttm_dma_tt_init(>t
->ttm
, bdev
, size
, page_flags
, dummy_read_page
)) {
666 return >t
->ttm
.ttm
;
669 static int amdgpu_ttm_tt_populate(struct ttm_tt
*ttm
)
671 struct amdgpu_device
*adev
;
672 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
675 bool slave
= !!(ttm
->page_flags
& TTM_PAGE_FLAG_SG
);
677 if (ttm
->state
!= tt_unpopulated
)
680 if (gtt
&& gtt
->userptr
) {
681 ttm
->sg
= kzalloc(sizeof(struct sg_table
), GFP_KERNEL
);
685 ttm
->page_flags
|= TTM_PAGE_FLAG_SG
;
686 ttm
->state
= tt_unbound
;
690 if (slave
&& ttm
->sg
) {
691 drm_prime_sg_to_page_addr_arrays(ttm
->sg
, ttm
->pages
,
692 gtt
->ttm
.dma_address
, ttm
->num_pages
);
693 ttm
->state
= tt_unbound
;
697 adev
= amdgpu_get_adev(ttm
->bdev
);
699 #ifdef CONFIG_SWIOTLB
700 if (swiotlb_nr_tbl()) {
701 return ttm_dma_populate(>t
->ttm
, adev
->dev
);
705 r
= ttm_pool_populate(ttm
);
710 for (i
= 0; i
< ttm
->num_pages
; i
++) {
711 gtt
->ttm
.dma_address
[i
] = pci_map_page(adev
->pdev
, ttm
->pages
[i
],
713 PCI_DMA_BIDIRECTIONAL
);
714 if (pci_dma_mapping_error(adev
->pdev
, gtt
->ttm
.dma_address
[i
])) {
716 pci_unmap_page(adev
->pdev
, gtt
->ttm
.dma_address
[i
],
717 PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
718 gtt
->ttm
.dma_address
[i
] = 0;
720 ttm_pool_unpopulate(ttm
);
727 static void amdgpu_ttm_tt_unpopulate(struct ttm_tt
*ttm
)
729 struct amdgpu_device
*adev
;
730 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
732 bool slave
= !!(ttm
->page_flags
& TTM_PAGE_FLAG_SG
);
734 if (gtt
&& gtt
->userptr
) {
736 ttm
->page_flags
&= ~TTM_PAGE_FLAG_SG
;
743 adev
= amdgpu_get_adev(ttm
->bdev
);
745 #ifdef CONFIG_SWIOTLB
746 if (swiotlb_nr_tbl()) {
747 ttm_dma_unpopulate(>t
->ttm
, adev
->dev
);
752 for (i
= 0; i
< ttm
->num_pages
; i
++) {
753 if (gtt
->ttm
.dma_address
[i
]) {
754 pci_unmap_page(adev
->pdev
, gtt
->ttm
.dma_address
[i
],
755 PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
759 ttm_pool_unpopulate(ttm
);
762 int amdgpu_ttm_tt_set_userptr(struct ttm_tt
*ttm
, uint64_t addr
,
765 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
771 gtt
->usermm
= current
->mm
;
772 gtt
->userflags
= flags
;
776 bool amdgpu_ttm_tt_has_userptr(struct ttm_tt
*ttm
)
778 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
783 return !!gtt
->userptr
;
786 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt
*ttm
)
788 struct amdgpu_ttm_tt
*gtt
= (void *)ttm
;
793 return !!(gtt
->userflags
& AMDGPU_GEM_USERPTR_READONLY
);
796 uint32_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device
*adev
, struct ttm_tt
*ttm
,
797 struct ttm_mem_reg
*mem
)
801 if (mem
&& mem
->mem_type
!= TTM_PL_SYSTEM
)
802 flags
|= AMDGPU_PTE_VALID
;
804 if (mem
&& mem
->mem_type
== TTM_PL_TT
) {
805 flags
|= AMDGPU_PTE_SYSTEM
;
807 if (ttm
->caching_state
== tt_cached
)
808 flags
|= AMDGPU_PTE_SNOOPED
;
811 if (adev
->asic_type
>= CHIP_TOPAZ
)
812 flags
|= AMDGPU_PTE_EXECUTABLE
;
814 flags
|= AMDGPU_PTE_READABLE
;
816 if (!amdgpu_ttm_tt_is_readonly(ttm
))
817 flags
|= AMDGPU_PTE_WRITEABLE
;
822 static struct ttm_bo_driver amdgpu_bo_driver
= {
823 .ttm_tt_create
= &amdgpu_ttm_tt_create
,
824 .ttm_tt_populate
= &amdgpu_ttm_tt_populate
,
825 .ttm_tt_unpopulate
= &amdgpu_ttm_tt_unpopulate
,
826 .invalidate_caches
= &amdgpu_invalidate_caches
,
827 .init_mem_type
= &amdgpu_init_mem_type
,
828 .evict_flags
= &amdgpu_evict_flags
,
829 .move
= &amdgpu_bo_move
,
830 .verify_access
= &amdgpu_verify_access
,
831 .move_notify
= &amdgpu_bo_move_notify
,
832 .fault_reserve_notify
= &amdgpu_bo_fault_reserve_notify
,
833 .io_mem_reserve
= &amdgpu_ttm_io_mem_reserve
,
834 .io_mem_free
= &amdgpu_ttm_io_mem_free
,
837 int amdgpu_ttm_init(struct amdgpu_device
*adev
)
841 r
= amdgpu_ttm_global_init(adev
);
845 /* No others user of address space so set it to 0 */
846 r
= ttm_bo_device_init(&adev
->mman
.bdev
,
847 adev
->mman
.bo_global_ref
.ref
.object
,
849 adev
->ddev
->anon_inode
->i_mapping
,
850 DRM_FILE_PAGE_OFFSET
,
853 DRM_ERROR("failed initializing buffer object driver(%d).\n", r
);
856 adev
->mman
.initialized
= true;
857 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, TTM_PL_VRAM
,
858 adev
->mc
.real_vram_size
>> PAGE_SHIFT
);
860 DRM_ERROR("Failed initializing VRAM heap.\n");
863 /* Change the size here instead of the init above so only lpfn is affected */
864 amdgpu_ttm_set_active_vram_size(adev
, adev
->mc
.visible_vram_size
);
866 r
= amdgpu_bo_create(adev
, 256 * 1024, PAGE_SIZE
, true,
867 AMDGPU_GEM_DOMAIN_VRAM
,
868 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
,
869 NULL
, NULL
, &adev
->stollen_vga_memory
);
873 r
= amdgpu_bo_reserve(adev
->stollen_vga_memory
, false);
876 r
= amdgpu_bo_pin(adev
->stollen_vga_memory
, AMDGPU_GEM_DOMAIN_VRAM
, NULL
);
877 amdgpu_bo_unreserve(adev
->stollen_vga_memory
);
879 amdgpu_bo_unref(&adev
->stollen_vga_memory
);
882 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
883 (unsigned) (adev
->mc
.real_vram_size
/ (1024 * 1024)));
884 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, TTM_PL_TT
,
885 adev
->mc
.gtt_size
>> PAGE_SHIFT
);
887 DRM_ERROR("Failed initializing GTT heap.\n");
890 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
891 (unsigned)(adev
->mc
.gtt_size
/ (1024 * 1024)));
893 adev
->gds
.mem
.total_size
= adev
->gds
.mem
.total_size
<< AMDGPU_GDS_SHIFT
;
894 adev
->gds
.mem
.gfx_partition_size
= adev
->gds
.mem
.gfx_partition_size
<< AMDGPU_GDS_SHIFT
;
895 adev
->gds
.mem
.cs_partition_size
= adev
->gds
.mem
.cs_partition_size
<< AMDGPU_GDS_SHIFT
;
896 adev
->gds
.gws
.total_size
= adev
->gds
.gws
.total_size
<< AMDGPU_GWS_SHIFT
;
897 adev
->gds
.gws
.gfx_partition_size
= adev
->gds
.gws
.gfx_partition_size
<< AMDGPU_GWS_SHIFT
;
898 adev
->gds
.gws
.cs_partition_size
= adev
->gds
.gws
.cs_partition_size
<< AMDGPU_GWS_SHIFT
;
899 adev
->gds
.oa
.total_size
= adev
->gds
.oa
.total_size
<< AMDGPU_OA_SHIFT
;
900 adev
->gds
.oa
.gfx_partition_size
= adev
->gds
.oa
.gfx_partition_size
<< AMDGPU_OA_SHIFT
;
901 adev
->gds
.oa
.cs_partition_size
= adev
->gds
.oa
.cs_partition_size
<< AMDGPU_OA_SHIFT
;
903 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, AMDGPU_PL_GDS
,
904 adev
->gds
.mem
.total_size
>> PAGE_SHIFT
);
906 DRM_ERROR("Failed initializing GDS heap.\n");
911 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, AMDGPU_PL_GWS
,
912 adev
->gds
.gws
.total_size
>> PAGE_SHIFT
);
914 DRM_ERROR("Failed initializing gws heap.\n");
919 r
= ttm_bo_init_mm(&adev
->mman
.bdev
, AMDGPU_PL_OA
,
920 adev
->gds
.oa
.total_size
>> PAGE_SHIFT
);
922 DRM_ERROR("Failed initializing oa heap.\n");
926 r
= amdgpu_ttm_debugfs_init(adev
);
928 DRM_ERROR("Failed to init debugfs\n");
934 void amdgpu_ttm_fini(struct amdgpu_device
*adev
)
938 if (!adev
->mman
.initialized
)
940 amdgpu_ttm_debugfs_fini(adev
);
941 if (adev
->stollen_vga_memory
) {
942 r
= amdgpu_bo_reserve(adev
->stollen_vga_memory
, false);
944 amdgpu_bo_unpin(adev
->stollen_vga_memory
);
945 amdgpu_bo_unreserve(adev
->stollen_vga_memory
);
947 amdgpu_bo_unref(&adev
->stollen_vga_memory
);
949 ttm_bo_clean_mm(&adev
->mman
.bdev
, TTM_PL_VRAM
);
950 ttm_bo_clean_mm(&adev
->mman
.bdev
, TTM_PL_TT
);
951 ttm_bo_clean_mm(&adev
->mman
.bdev
, AMDGPU_PL_GDS
);
952 ttm_bo_clean_mm(&adev
->mman
.bdev
, AMDGPU_PL_GWS
);
953 ttm_bo_clean_mm(&adev
->mman
.bdev
, AMDGPU_PL_OA
);
954 ttm_bo_device_release(&adev
->mman
.bdev
);
955 amdgpu_gart_fini(adev
);
956 amdgpu_ttm_global_fini(adev
);
957 adev
->mman
.initialized
= false;
958 DRM_INFO("amdgpu: ttm finalized\n");
961 /* this should only be called at bootup or when userspace
963 void amdgpu_ttm_set_active_vram_size(struct amdgpu_device
*adev
, u64 size
)
965 struct ttm_mem_type_manager
*man
;
967 if (!adev
->mman
.initialized
)
970 man
= &adev
->mman
.bdev
.man
[TTM_PL_VRAM
];
971 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
972 man
->size
= size
>> PAGE_SHIFT
;
975 int amdgpu_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
977 struct drm_file
*file_priv
;
978 struct amdgpu_device
*adev
;
980 if (unlikely(vma
->vm_pgoff
< DRM_FILE_PAGE_OFFSET
))
983 file_priv
= filp
->private_data
;
984 adev
= file_priv
->minor
->dev
->dev_private
;
988 return ttm_bo_mmap(filp
, vma
, &adev
->mman
.bdev
);
991 int amdgpu_copy_buffer(struct amdgpu_ring
*ring
,
995 struct reservation_object
*resv
,
996 struct fence
**fence
)
998 struct amdgpu_device
*adev
= ring
->adev
;
1000 unsigned num_loops
, num_dw
;
1001 struct amdgpu_ib
*ib
;
1005 max_bytes
= adev
->mman
.buffer_funcs
->copy_max_bytes
;
1006 num_loops
= DIV_ROUND_UP(byte_count
, max_bytes
);
1007 num_dw
= num_loops
* adev
->mman
.buffer_funcs
->copy_num_dw
;
1009 /* for IB padding */
1010 while (num_dw
& 0x7)
1013 ib
= kzalloc(sizeof(struct amdgpu_ib
), GFP_KERNEL
);
1017 r
= amdgpu_ib_get(ring
, NULL
, num_dw
* 4, ib
);
1026 r
= amdgpu_sync_resv(adev
, &ib
->sync
, resv
,
1027 AMDGPU_FENCE_OWNER_UNDEFINED
);
1029 DRM_ERROR("sync failed (%d).\n", r
);
1034 for (i
= 0; i
< num_loops
; i
++) {
1035 uint32_t cur_size_in_bytes
= min(byte_count
, max_bytes
);
1037 amdgpu_emit_copy_buffer(adev
, ib
, src_offset
, dst_offset
,
1040 src_offset
+= cur_size_in_bytes
;
1041 dst_offset
+= cur_size_in_bytes
;
1042 byte_count
-= cur_size_in_bytes
;
1045 amdgpu_vm_pad_ib(adev
, ib
);
1046 WARN_ON(ib
->length_dw
> num_dw
);
1047 r
= amdgpu_sched_ib_submit_kernel_helper(adev
, ring
, ib
, 1,
1048 &amdgpu_vm_free_job
,
1049 AMDGPU_FENCE_OWNER_UNDEFINED
,
1054 if (!amdgpu_enable_scheduler
) {
1055 amdgpu_ib_free(adev
, ib
);
1060 amdgpu_ib_free(adev
, ib
);
1065 #if defined(CONFIG_DEBUG_FS)
1067 static int amdgpu_mm_dump_table(struct seq_file
*m
, void *data
)
1069 struct drm_info_node
*node
= (struct drm_info_node
*)m
->private;
1070 unsigned ttm_pl
= *(int *)node
->info_ent
->data
;
1071 struct drm_device
*dev
= node
->minor
->dev
;
1072 struct amdgpu_device
*adev
= dev
->dev_private
;
1073 struct drm_mm
*mm
= (struct drm_mm
*)adev
->mman
.bdev
.man
[ttm_pl
].priv
;
1075 struct ttm_bo_global
*glob
= adev
->mman
.bdev
.glob
;
1077 spin_lock(&glob
->lru_lock
);
1078 ret
= drm_mm_dump_table(m
, mm
);
1079 spin_unlock(&glob
->lru_lock
);
1080 if (ttm_pl
== TTM_PL_VRAM
)
1081 seq_printf(m
, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
1082 adev
->mman
.bdev
.man
[ttm_pl
].size
,
1083 (u64
)atomic64_read(&adev
->vram_usage
) >> 20,
1084 (u64
)atomic64_read(&adev
->vram_vis_usage
) >> 20);
1088 static int ttm_pl_vram
= TTM_PL_VRAM
;
1089 static int ttm_pl_tt
= TTM_PL_TT
;
1091 static struct drm_info_list amdgpu_ttm_debugfs_list
[] = {
1092 {"amdgpu_vram_mm", amdgpu_mm_dump_table
, 0, &ttm_pl_vram
},
1093 {"amdgpu_gtt_mm", amdgpu_mm_dump_table
, 0, &ttm_pl_tt
},
1094 {"ttm_page_pool", ttm_page_alloc_debugfs
, 0, NULL
},
1095 #ifdef CONFIG_SWIOTLB
1096 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs
, 0, NULL
}
1100 static ssize_t
amdgpu_ttm_vram_read(struct file
*f
, char __user
*buf
,
1101 size_t size
, loff_t
*pos
)
1103 struct amdgpu_device
*adev
= f
->f_inode
->i_private
;
1107 if (size
& 0x3 || *pos
& 0x3)
1111 unsigned long flags
;
1114 if (*pos
>= adev
->mc
.mc_vram_size
)
1117 spin_lock_irqsave(&adev
->mmio_idx_lock
, flags
);
1118 WREG32(mmMM_INDEX
, ((uint32_t)*pos
) | 0x80000000);
1119 WREG32(mmMM_INDEX_HI
, *pos
>> 31);
1120 value
= RREG32(mmMM_DATA
);
1121 spin_unlock_irqrestore(&adev
->mmio_idx_lock
, flags
);
1123 r
= put_user(value
, (uint32_t *)buf
);
1136 static const struct file_operations amdgpu_ttm_vram_fops
= {
1137 .owner
= THIS_MODULE
,
1138 .read
= amdgpu_ttm_vram_read
,
1139 .llseek
= default_llseek
1142 static ssize_t
amdgpu_ttm_gtt_read(struct file
*f
, char __user
*buf
,
1143 size_t size
, loff_t
*pos
)
1145 struct amdgpu_device
*adev
= f
->f_inode
->i_private
;
1150 loff_t p
= *pos
/ PAGE_SIZE
;
1151 unsigned off
= *pos
& ~PAGE_MASK
;
1152 size_t cur_size
= min_t(size_t, size
, PAGE_SIZE
- off
);
1156 if (p
>= adev
->gart
.num_cpu_pages
)
1159 page
= adev
->gart
.pages
[p
];
1164 r
= copy_to_user(buf
, ptr
, cur_size
);
1165 kunmap(adev
->gart
.pages
[p
]);
1167 r
= clear_user(buf
, cur_size
);
1181 static const struct file_operations amdgpu_ttm_gtt_fops
= {
1182 .owner
= THIS_MODULE
,
1183 .read
= amdgpu_ttm_gtt_read
,
1184 .llseek
= default_llseek
1189 static int amdgpu_ttm_debugfs_init(struct amdgpu_device
*adev
)
1191 #if defined(CONFIG_DEBUG_FS)
1194 struct drm_minor
*minor
= adev
->ddev
->primary
;
1195 struct dentry
*ent
, *root
= minor
->debugfs_root
;
1197 ent
= debugfs_create_file("amdgpu_vram", S_IFREG
| S_IRUGO
, root
,
1198 adev
, &amdgpu_ttm_vram_fops
);
1200 return PTR_ERR(ent
);
1201 i_size_write(ent
->d_inode
, adev
->mc
.mc_vram_size
);
1202 adev
->mman
.vram
= ent
;
1204 ent
= debugfs_create_file("amdgpu_gtt", S_IFREG
| S_IRUGO
, root
,
1205 adev
, &amdgpu_ttm_gtt_fops
);
1207 return PTR_ERR(ent
);
1208 i_size_write(ent
->d_inode
, adev
->mc
.gtt_size
);
1209 adev
->mman
.gtt
= ent
;
1211 count
= ARRAY_SIZE(amdgpu_ttm_debugfs_list
);
1213 #ifdef CONFIG_SWIOTLB
1214 if (!swiotlb_nr_tbl())
1218 return amdgpu_debugfs_add_files(adev
, amdgpu_ttm_debugfs_list
, count
);
1225 static void amdgpu_ttm_debugfs_fini(struct amdgpu_device
*adev
)
1227 #if defined(CONFIG_DEBUG_FS)
1229 debugfs_remove(adev
->mman
.vram
);
1230 adev
->mman
.vram
= NULL
;
1232 debugfs_remove(adev
->mman
.gtt
);
1233 adev
->mman
.gtt
= NULL
;