Skip to content

Commit bfa3357

Browse files
drm/ttm: allocate resource object instead of embedding it v2
To improve the handling we want the establish the resource object as base class for the backend allocations. v2: add missing error handling Signed-off-by: Christian König <[email protected]> Acked-by: Thomas Hellström <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 4e56600 commit bfa3357

File tree

11 files changed

+110
-126
lines changed

11 files changed

+110
-126
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,14 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
362362
if (cpu_addr)
363363
amdgpu_bo_kunmap(*bo_ptr);
364364

365-
ttm_resource_free(&(*bo_ptr)->tbo, (*bo_ptr)->tbo.resource);
365+
ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
366366

367367
for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
368368
(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
369369
(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
370370
}
371371
r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
372-
(*bo_ptr)->tbo.resource, &ctx);
372+
&(*bo_ptr)->tbo.resource, &ctx);
373373
if (r)
374374
goto error;
375375

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
491491
return r;
492492

493493
amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
494-
ttm_resource_free(bo, bo->resource);
494+
ttm_resource_free(bo, &bo->resource);
495495
ttm_bo_assign_mem(bo, new_mem);
496496
goto out;
497497
}
@@ -950,9 +950,9 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
950950
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
951951
struct ttm_operation_ctx ctx = { false, false };
952952
struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
953-
struct ttm_resource tmp;
954953
struct ttm_placement placement;
955954
struct ttm_place placements;
955+
struct ttm_resource *tmp;
956956
uint64_t addr, flags;
957957
int r;
958958

@@ -962,37 +962,37 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
962962
addr = amdgpu_gmc_agp_addr(bo);
963963
if (addr != AMDGPU_BO_INVALID_OFFSET) {
964964
bo->resource->start = addr >> PAGE_SHIFT;
965-
} else {
965+
return 0;
966+
}
966967

967-
/* allocate GART space */
968-
placement.num_placement = 1;
969-
placement.placement = &placements;
970-
placement.num_busy_placement = 1;
971-
placement.busy_placement = &placements;
972-
placements.fpfn = 0;
973-
placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
974-
placements.mem_type = TTM_PL_TT;
975-
placements.flags = bo->resource->placement;
976-
977-
r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
978-
if (unlikely(r))
979-
return r;
968+
/* allocate GART space */
969+
placement.num_placement = 1;
970+
placement.placement = &placements;
971+
placement.num_busy_placement = 1;
972+
placement.busy_placement = &placements;
973+
placements.fpfn = 0;
974+
placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
975+
placements.mem_type = TTM_PL_TT;
976+
placements.flags = bo->resource->placement;
980977

981-
/* compute PTE flags for this buffer object */
982-
flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, &tmp);
978+
r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
979+
if (unlikely(r))
980+
return r;
983981

984-
/* Bind pages */
985-
gtt->offset = (u64)tmp.start << PAGE_SHIFT;
986-
r = amdgpu_ttm_gart_bind(adev, bo, flags);
987-
if (unlikely(r)) {
988-
ttm_resource_free(bo, &tmp);
989-
return r;
990-
}
982+
/* compute PTE flags for this buffer object */
983+
flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, tmp);
991984

992-
ttm_resource_free(bo, bo->resource);
993-
ttm_bo_assign_mem(bo, &tmp);
985+
/* Bind pages */
986+
gtt->offset = (u64)tmp->start << PAGE_SHIFT;
987+
r = amdgpu_ttm_gart_bind(adev, bo, flags);
988+
if (unlikely(r)) {
989+
ttm_resource_free(bo, &tmp);
990+
return r;
994991
}
995992

993+
ttm_resource_free(bo, &bo->resource);
994+
ttm_bo_assign_mem(bo, tmp);
995+
996996
return 0;
997997
}
998998

drivers/gpu/drm/nouveau/nouveau_bo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
10091009
if (old_reg->mem_type == TTM_PL_TT &&
10101010
new_reg->mem_type == TTM_PL_SYSTEM) {
10111011
nouveau_ttm_tt_unbind(bo->bdev, bo->ttm);
1012-
ttm_resource_free(bo, bo->resource);
1012+
ttm_resource_free(bo, &bo->resource);
10131013
ttm_bo_assign_mem(bo, new_reg);
10141014
goto out;
10151015
}

drivers/gpu/drm/radeon/radeon_ttm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static int radeon_bo_move(struct ttm_buffer_object *bo, bool evict,
229229
if (old_mem->mem_type == TTM_PL_TT &&
230230
new_mem->mem_type == TTM_PL_SYSTEM) {
231231
radeon_ttm_tt_unbind(bo->bdev, bo->ttm);
232-
ttm_resource_free(bo, bo->resource);
232+
ttm_resource_free(bo, &bo->resource);
233233
ttm_bo_assign_mem(bo, new_mem);
234234
goto out;
235235
}

drivers/gpu/drm/ttm/ttm_bo.c

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo)
223223
bo->bdev->funcs->delete_mem_notify(bo);
224224

225225
ttm_bo_tt_destroy(bo);
226-
ttm_resource_free(bo, bo->resource);
226+
ttm_resource_free(bo, &bo->resource);
227227
}
228228

229229
static int ttm_bo_individualize_resv(struct ttm_buffer_object *bo)
@@ -489,7 +489,7 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
489489
struct ttm_operation_ctx *ctx)
490490
{
491491
struct ttm_device *bdev = bo->bdev;
492-
struct ttm_resource evict_mem;
492+
struct ttm_resource *evict_mem;
493493
struct ttm_placement placement;
494494
struct ttm_place hop;
495495
int ret = 0;
@@ -519,7 +519,7 @@ static int ttm_bo_evict(struct ttm_buffer_object *bo,
519519
goto out;
520520
}
521521

522-
ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, ctx, &hop);
522+
ret = ttm_bo_handle_move_mem(bo, evict_mem, true, ctx, &hop);
523523
if (unlikely(ret)) {
524524
WARN(ret == -EMULTIHOP, "Unexpected multihop in eviction - likely driver bug\n");
525525
if (ret != -ERESTARTSYS)
@@ -728,14 +728,15 @@ static int ttm_bo_add_move_fence(struct ttm_buffer_object *bo,
728728
*/
729729
static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
730730
const struct ttm_place *place,
731-
struct ttm_resource *mem,
731+
struct ttm_resource **mem,
732732
struct ttm_operation_ctx *ctx)
733733
{
734734
struct ttm_device *bdev = bo->bdev;
735-
struct ttm_resource_manager *man = ttm_manager_type(bdev, mem->mem_type);
735+
struct ttm_resource_manager *man;
736736
struct ww_acquire_ctx *ticket;
737737
int ret;
738738

739+
man = ttm_manager_type(bdev, (*mem)->mem_type);
739740
ticket = dma_resv_locking_ctx(bo->base.resv);
740741
do {
741742
ret = ttm_resource_alloc(bo, place, mem);
@@ -749,37 +750,7 @@ static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo,
749750
return ret;
750751
} while (1);
751752

752-
return ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
753-
}
754-
755-
/**
756-
* ttm_bo_mem_placement - check if placement is compatible
757-
* @bo: BO to find memory for
758-
* @place: where to search
759-
* @mem: the memory object to fill in
760-
*
761-
* Check if placement is compatible and fill in mem structure.
762-
* Returns -EBUSY if placement won't work or negative error code.
763-
* 0 when placement can be used.
764-
*/
765-
static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
766-
const struct ttm_place *place,
767-
struct ttm_resource *mem)
768-
{
769-
struct ttm_device *bdev = bo->bdev;
770-
struct ttm_resource_manager *man;
771-
772-
man = ttm_manager_type(bdev, place->mem_type);
773-
if (!man || !ttm_resource_manager_used(man))
774-
return -EBUSY;
775-
776-
mem->mem_type = place->mem_type;
777-
mem->placement = place->flags;
778-
779-
spin_lock(&bo->bdev->lru_lock);
780-
ttm_bo_move_to_lru_tail(bo, mem, NULL);
781-
spin_unlock(&bo->bdev->lru_lock);
782-
return 0;
753+
return ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
783754
}
784755

785756
/*
@@ -792,7 +763,7 @@ static int ttm_bo_mem_placement(struct ttm_buffer_object *bo,
792763
*/
793764
int ttm_bo_mem_space(struct ttm_buffer_object *bo,
794765
struct ttm_placement *placement,
795-
struct ttm_resource *mem,
766+
struct ttm_resource **mem,
796767
struct ttm_operation_ctx *ctx)
797768
{
798769
struct ttm_device *bdev = bo->bdev;
@@ -807,8 +778,8 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
807778
const struct ttm_place *place = &placement->placement[i];
808779
struct ttm_resource_manager *man;
809780

810-
ret = ttm_bo_mem_placement(bo, place, mem);
811-
if (ret)
781+
man = ttm_manager_type(bdev, place->mem_type);
782+
if (!man || !ttm_resource_manager_used(man))
812783
continue;
813784

814785
type_found = true;
@@ -818,8 +789,7 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
818789
if (unlikely(ret))
819790
goto error;
820791

821-
man = ttm_manager_type(bdev, mem->mem_type);
822-
ret = ttm_bo_add_move_fence(bo, man, mem, ctx->no_wait_gpu);
792+
ret = ttm_bo_add_move_fence(bo, man, *mem, ctx->no_wait_gpu);
823793
if (unlikely(ret)) {
824794
ttm_resource_free(bo, mem);
825795
if (ret == -EBUSY)
@@ -832,9 +802,10 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
832802

833803
for (i = 0; i < placement->num_busy_placement; ++i) {
834804
const struct ttm_place *place = &placement->busy_placement[i];
805+
struct ttm_resource_manager *man;
835806

836-
ret = ttm_bo_mem_placement(bo, place, mem);
837-
if (ret)
807+
man = ttm_manager_type(bdev, place->mem_type);
808+
if (!man || !ttm_resource_manager_used(man))
838809
continue;
839810

840811
type_found = true;
@@ -861,12 +832,12 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
861832
EXPORT_SYMBOL(ttm_bo_mem_space);
862833

863834
static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
864-
struct ttm_resource *mem,
835+
struct ttm_resource **mem,
865836
struct ttm_operation_ctx *ctx,
866837
struct ttm_place *hop)
867838
{
868839
struct ttm_placement hop_placement;
869-
struct ttm_resource hop_mem;
840+
struct ttm_resource *hop_mem;
870841
int ret;
871842

872843
hop_placement.num_placement = hop_placement.num_busy_placement = 1;
@@ -877,7 +848,7 @@ static int ttm_bo_bounce_temp_buffer(struct ttm_buffer_object *bo,
877848
if (ret)
878849
return ret;
879850
/* move to the bounce domain */
880-
ret = ttm_bo_handle_move_mem(bo, &hop_mem, false, ctx, NULL);
851+
ret = ttm_bo_handle_move_mem(bo, hop_mem, false, ctx, NULL);
881852
if (ret) {
882853
ttm_resource_free(bo, &hop_mem);
883854
return ret;
@@ -889,14 +860,12 @@ static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
889860
struct ttm_placement *placement,
890861
struct ttm_operation_ctx *ctx)
891862
{
863+
struct ttm_resource *mem;
892864
struct ttm_place hop;
893-
struct ttm_resource mem;
894865
int ret;
895866

896867
dma_resv_assert_held(bo->base.resv);
897868

898-
memset(&hop, 0, sizeof(hop));
899-
900869
/*
901870
* Determine where to move the buffer.
902871
*
@@ -910,7 +879,7 @@ static int ttm_bo_move_buffer(struct ttm_buffer_object *bo,
910879
if (ret)
911880
return ret;
912881
bounce:
913-
ret = ttm_bo_handle_move_mem(bo, &mem, false, ctx, &hop);
882+
ret = ttm_bo_handle_move_mem(bo, mem, false, ctx, &hop);
914883
if (ret == -EMULTIHOP) {
915884
ret = ttm_bo_bounce_temp_buffer(bo, &mem, ctx, &hop);
916885
if (ret)
@@ -1019,7 +988,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
1019988
{
1020989
static const struct ttm_place sys_mem = { .mem_type = TTM_PL_SYSTEM };
1021990
bool locked;
1022-
int ret = 0;
991+
int ret;
1023992

1024993
bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
1025994

@@ -1029,8 +998,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
1029998
bo->bdev = bdev;
1030999
bo->type = type;
10311000
bo->page_alignment = page_alignment;
1032-
bo->resource = &bo->_mem;
1033-
ttm_resource_alloc(bo, &sys_mem, bo->resource);
10341001
bo->moving = NULL;
10351002
bo->pin_count = 0;
10361003
bo->sg = sg;
@@ -1042,6 +1009,12 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
10421009
}
10431010
atomic_inc(&ttm_glob.bo_count);
10441011

1012+
ret = ttm_resource_alloc(bo, &sys_mem, &bo->resource);
1013+
if (unlikely(ret)) {
1014+
ttm_bo_put(bo);
1015+
return ret;
1016+
}
1017+
10451018
/*
10461019
* For ttm_bo_type_device buffers, allocate
10471020
* address space from the device.
@@ -1170,7 +1143,7 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
11701143
*/
11711144
if (bo->resource->mem_type != TTM_PL_SYSTEM) {
11721145
struct ttm_operation_ctx ctx = { false, false };
1173-
struct ttm_resource evict_mem;
1146+
struct ttm_resource *evict_mem;
11741147
struct ttm_place place, hop;
11751148

11761149
memset(&place, 0, sizeof(place));
@@ -1182,7 +1155,7 @@ int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
11821155
if (unlikely(ret))
11831156
goto out;
11841157

1185-
ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, &ctx, &hop);
1158+
ret = ttm_bo_handle_move_mem(bo, evict_mem, true, &ctx, &hop);
11861159
if (unlikely(ret != 0)) {
11871160
WARN(ret == -EMULTIHOP, "Unexpected multihop in swaput - likely driver bug.\n");
11881161
goto out;

0 commit comments

Comments
 (0)