Skip to content

Commit 75521e8

Browse files
author
Thomas Hellström
committed
drm/xe: Perform dma_map when moving system buffer objects to TT
Currently we dma_map on ttm_tt population and dma_unmap when the pages are released in ttm_tt unpopulate. Strictly, the dma_map is not needed until the bo is moved to the XE_PL_TT placement, so perform the dma_mapping on such moves instead, and remove the dma_mappig when moving to XE_PL_SYSTEM. This is desired for the upcoming shrinker series where shrinking of a ttm_tt might fail. That would lead to an odd construct where we first dma_unmap, then shrink and if shrinking fails dma_map again. If dma_mapping instead is performed on move like this, shrinking does not need to care at all about dma mapping. Finally, where a ttm_tt is destroyed while bound to a different memory type than XE_PL_SYSTEM, we keep the dma_unmap in unpopulate(). v2: - Don't accidently unmap the dma-buf's sgtable. Signed-off-by: Thomas Hellström <[email protected]> Reviewed-by: Matthew Brost <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 8ad0e18 commit 75521e8

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

drivers/gpu/drm/xe/xe_bo.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,18 @@ static int xe_tt_map_sg(struct ttm_tt *tt)
302302
return 0;
303303
}
304304

305+
static void xe_tt_unmap_sg(struct ttm_tt *tt)
306+
{
307+
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
308+
309+
if (xe_tt->sg) {
310+
dma_unmap_sgtable(xe_tt->dev, xe_tt->sg,
311+
DMA_BIDIRECTIONAL, 0);
312+
sg_free_table(xe_tt->sg);
313+
xe_tt->sg = NULL;
314+
}
315+
}
316+
305317
struct sg_table *xe_bo_sg(struct xe_bo *bo)
306318
{
307319
struct ttm_tt *tt = bo->ttm.ttm;
@@ -377,27 +389,15 @@ static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt,
377389
if (err)
378390
return err;
379391

380-
/* A follow up may move this xe_bo_move when BO is moved to XE_PL_TT */
381-
err = xe_tt_map_sg(tt);
382-
if (err)
383-
ttm_pool_free(&ttm_dev->pool, tt);
384-
385392
return err;
386393
}
387394

388395
static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
389396
{
390-
struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
391-
392397
if (tt->page_flags & TTM_TT_FLAG_EXTERNAL)
393398
return;
394399

395-
if (xe_tt->sg) {
396-
dma_unmap_sgtable(xe_tt->dev, xe_tt->sg,
397-
DMA_BIDIRECTIONAL, 0);
398-
sg_free_table(xe_tt->sg);
399-
xe_tt->sg = NULL;
400-
}
400+
xe_tt_unmap_sg(tt);
401401

402402
return ttm_pool_free(&ttm_dev->pool, tt);
403403
}
@@ -628,17 +628,21 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
628628
bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) &&
629629
ttm && ttm_tt_is_populated(ttm)) ? true : false;
630630
int ret = 0;
631+
631632
/* Bo creation path, moving to system or TT. */
632633
if ((!old_mem && ttm) && !handle_system_ccs) {
633-
ttm_bo_move_null(ttm_bo, new_mem);
634-
return 0;
634+
if (new_mem->mem_type == XE_PL_TT)
635+
ret = xe_tt_map_sg(ttm);
636+
if (!ret)
637+
ttm_bo_move_null(ttm_bo, new_mem);
638+
goto out;
635639
}
636640

637641
if (ttm_bo->type == ttm_bo_type_sg) {
638642
ret = xe_bo_move_notify(bo, ctx);
639643
if (!ret)
640644
ret = xe_bo_move_dmabuf(ttm_bo, new_mem);
641-
goto out;
645+
return ret;
642646
}
643647

644648
tt_has_data = ttm && (ttm_tt_is_populated(ttm) ||
@@ -650,6 +654,12 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
650654
needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
651655
(!ttm && ttm_bo->type == ttm_bo_type_device);
652656

657+
if (new_mem->mem_type == XE_PL_TT) {
658+
ret = xe_tt_map_sg(ttm);
659+
if (ret)
660+
goto out;
661+
}
662+
653663
if ((move_lacks_source && !needs_clear)) {
654664
ttm_bo_move_null(ttm_bo, new_mem);
655665
goto out;
@@ -786,8 +796,11 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
786796
xe_pm_runtime_put(xe);
787797

788798
out:
789-
return ret;
799+
if ((!ttm_bo->resource || ttm_bo->resource->mem_type == XE_PL_SYSTEM) &&
800+
ttm_bo->ttm)
801+
xe_tt_unmap_sg(ttm_bo->ttm);
790802

803+
return ret;
791804
}
792805

793806
/**

0 commit comments

Comments
 (0)