Skip to content

Commit a6ff969

Browse files
ChristianKoenigAMDalexdeucher
authored andcommitted
drm/amdgpu: fix visible VRAM handling during faults
When we removed the hacky start code check we actually didn't took into account that *all* VRAM pages needs to be CPU accessible. Clean up the code and unify the handling into a single helper which checks if the whole resource is CPU accessible. The only place where a partial check would make sense is during eviction, but that is neglitible. Signed-off-by: Christian König <[email protected]> Fixes: aed01a6 ("drm/amdgpu: Remove TTM resource->start visible VRAM condition v2") Reviewed-by: Alex Deucher <[email protected]> Signed-off-by: Alex Deucher <[email protected]> CC: [email protected]
1 parent 6fef2d4 commit a6ff969

File tree

5 files changed

+53
-57
lines changed

5 files changed

+53
-57
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo)
819819

820820
p->bytes_moved += ctx.bytes_moved;
821821
if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
822-
amdgpu_bo_in_cpu_visible_vram(bo))
822+
amdgpu_res_cpu_visible(adev, bo->tbo.resource))
823823
p->bytes_moved_vis += ctx.bytes_moved;
824824

825825
if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,7 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
617617
return r;
618618

619619
if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
620-
bo->tbo.resource->mem_type == TTM_PL_VRAM &&
621-
amdgpu_bo_in_cpu_visible_vram(bo))
620+
amdgpu_res_cpu_visible(adev, bo->tbo.resource))
622621
amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
623622
ctx.bytes_moved);
624623
else
@@ -1272,23 +1271,25 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, bool evict)
12721271
void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
12731272
struct amdgpu_mem_stats *stats)
12741273
{
1274+
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
1275+
struct ttm_resource *res = bo->tbo.resource;
12751276
uint64_t size = amdgpu_bo_size(bo);
12761277
struct drm_gem_object *obj;
12771278
unsigned int domain;
12781279
bool shared;
12791280

12801281
/* Abort if the BO doesn't currently have a backing store */
1281-
if (!bo->tbo.resource)
1282+
if (!res)
12821283
return;
12831284

12841285
obj = &bo->tbo.base;
12851286
shared = drm_gem_object_is_shared_for_memory_stats(obj);
12861287

1287-
domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
1288+
domain = amdgpu_mem_type_to_domain(res->mem_type);
12881289
switch (domain) {
12891290
case AMDGPU_GEM_DOMAIN_VRAM:
12901291
stats->vram += size;
1291-
if (amdgpu_bo_in_cpu_visible_vram(bo))
1292+
if (amdgpu_res_cpu_visible(adev, bo->tbo.resource))
12921293
stats->visible_vram += size;
12931294
if (shared)
12941295
stats->vram_shared += size;
@@ -1389,10 +1390,7 @@ vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
13891390
/* Remember that this BO was accessed by the CPU */
13901391
abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
13911392

1392-
if (bo->resource->mem_type != TTM_PL_VRAM)
1393-
return 0;
1394-
1395-
if (amdgpu_bo_in_cpu_visible_vram(abo))
1393+
if (amdgpu_res_cpu_visible(adev, bo->resource))
13961394
return 0;
13971395

13981396
/* Can't move a pinned BO to visible VRAM */
@@ -1415,7 +1413,7 @@ vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
14151413

14161414
/* this should never happen */
14171415
if (bo->resource->mem_type == TTM_PL_VRAM &&
1418-
!amdgpu_bo_in_cpu_visible_vram(abo))
1416+
!amdgpu_res_cpu_visible(adev, bo->resource))
14191417
return VM_FAULT_SIGBUS;
14201418

14211419
ttm_bo_move_to_lru_tail_unlocked(bo);
@@ -1579,6 +1577,7 @@ uint32_t amdgpu_bo_get_preferred_domain(struct amdgpu_device *adev,
15791577
*/
15801578
u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m)
15811579
{
1580+
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
15821581
struct dma_buf_attachment *attachment;
15831582
struct dma_buf *dma_buf;
15841583
const char *placement;
@@ -1587,10 +1586,11 @@ u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m)
15871586

15881587
if (dma_resv_trylock(bo->tbo.base.resv)) {
15891588
unsigned int domain;
1589+
15901590
domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
15911591
switch (domain) {
15921592
case AMDGPU_GEM_DOMAIN_VRAM:
1593-
if (amdgpu_bo_in_cpu_visible_vram(bo))
1593+
if (amdgpu_res_cpu_visible(adev, bo->tbo.resource))
15941594
placement = "VRAM VISIBLE";
15951595
else
15961596
placement = "VRAM";

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -250,28 +250,6 @@ static inline u64 amdgpu_bo_mmap_offset(struct amdgpu_bo *bo)
250250
return drm_vma_node_offset_addr(&bo->tbo.base.vma_node);
251251
}
252252

253-
/**
254-
* amdgpu_bo_in_cpu_visible_vram - check if BO is (partly) in visible VRAM
255-
*/
256-
static inline bool amdgpu_bo_in_cpu_visible_vram(struct amdgpu_bo *bo)
257-
{
258-
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
259-
struct amdgpu_res_cursor cursor;
260-
261-
if (!bo->tbo.resource || bo->tbo.resource->mem_type != TTM_PL_VRAM)
262-
return false;
263-
264-
amdgpu_res_first(bo->tbo.resource, 0, amdgpu_bo_size(bo), &cursor);
265-
while (cursor.remaining) {
266-
if (cursor.start < adev->gmc.visible_vram_size)
267-
return true;
268-
269-
amdgpu_res_next(&cursor, cursor.size);
270-
}
271-
272-
return false;
273-
}
274-
275253
/**
276254
* amdgpu_bo_explicit_sync - return whether the bo is explicitly synced
277255
*/

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
133133

134134
} else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
135135
!(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
136-
amdgpu_bo_in_cpu_visible_vram(abo)) {
136+
amdgpu_res_cpu_visible(adev, bo->resource)) {
137137

138138
/* Try evicting to the CPU inaccessible part of VRAM
139139
* first, but only set GTT as busy placement, so this
@@ -403,40 +403,55 @@ static int amdgpu_move_blit(struct ttm_buffer_object *bo,
403403
return r;
404404
}
405405

406-
/*
407-
* amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
406+
/**
407+
* amdgpu_res_cpu_visible - Check that resource can be accessed by CPU
408+
* @adev: amdgpu device
409+
* @res: the resource to check
408410
*
409-
* Called by amdgpu_bo_move()
411+
* Returns: true if the full resource is CPU visible, false otherwise.
410412
*/
411-
static bool amdgpu_mem_visible(struct amdgpu_device *adev,
412-
struct ttm_resource *mem)
413+
bool amdgpu_res_cpu_visible(struct amdgpu_device *adev,
414+
struct ttm_resource *res)
413415
{
414-
u64 mem_size = (u64)mem->size;
415416
struct amdgpu_res_cursor cursor;
416-
u64 end;
417417

418-
if (mem->mem_type == TTM_PL_SYSTEM ||
419-
mem->mem_type == TTM_PL_TT)
418+
if (!res)
419+
return false;
420+
421+
if (res->mem_type == TTM_PL_SYSTEM || res->mem_type == TTM_PL_TT ||
422+
res->mem_type == AMDGPU_PL_PREEMPT)
420423
return true;
421-
if (mem->mem_type != TTM_PL_VRAM)
424+
425+
if (res->mem_type != TTM_PL_VRAM)
422426
return false;
423427

424-
amdgpu_res_first(mem, 0, mem_size, &cursor);
425-
end = cursor.start + cursor.size;
428+
amdgpu_res_first(res, 0, res->size, &cursor);
426429
while (cursor.remaining) {
430+
if ((cursor.start + cursor.size) >= adev->gmc.visible_vram_size)
431+
return false;
427432
amdgpu_res_next(&cursor, cursor.size);
433+
}
428434

429-
if (!cursor.remaining)
430-
break;
435+
return true;
436+
}
431437

432-
/* ttm_resource_ioremap only supports contiguous memory */
433-
if (end != cursor.start)
434-
return false;
438+
/*
439+
* amdgpu_res_copyable - Check that memory can be accessed by ttm_bo_move_memcpy
440+
*
441+
* Called by amdgpu_bo_move()
442+
*/
443+
static bool amdgpu_res_copyable(struct amdgpu_device *adev,
444+
struct ttm_resource *mem)
445+
{
446+
if (!amdgpu_res_cpu_visible(adev, mem))
447+
return false;
435448

436-
end = cursor.start + cursor.size;
437-
}
449+
/* ttm_resource_ioremap only supports contiguous memory */
450+
if (mem->mem_type == TTM_PL_VRAM &&
451+
!(mem->placement & TTM_PL_FLAG_CONTIGUOUS))
452+
return false;
438453

439-
return end <= adev->gmc.visible_vram_size;
454+
return true;
440455
}
441456

442457
/*
@@ -529,8 +544,8 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
529544

530545
if (r) {
531546
/* Check that all memory is CPU accessible */
532-
if (!amdgpu_mem_visible(adev, old_mem) ||
533-
!amdgpu_mem_visible(adev, new_mem)) {
547+
if (!amdgpu_res_copyable(adev, old_mem) ||
548+
!amdgpu_res_copyable(adev, new_mem)) {
534549
pr_err("Move buffer fallback to memcpy unavailable\n");
535550
return r;
536551
}

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ int amdgpu_vram_mgr_reserve_range(struct amdgpu_vram_mgr *mgr,
139139
int amdgpu_vram_mgr_query_page_status(struct amdgpu_vram_mgr *mgr,
140140
uint64_t start);
141141

142+
bool amdgpu_res_cpu_visible(struct amdgpu_device *adev,
143+
struct ttm_resource *res);
144+
142145
int amdgpu_ttm_init(struct amdgpu_device *adev);
143146
void amdgpu_ttm_fini(struct amdgpu_device *adev);
144147
void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev,

0 commit comments

Comments
 (0)