Skip to content

Commit cb1c814

Browse files
drm/ttm: flip the switch for driver allocated resources v2
Instead of both driver and TTM allocating memory finalize embedding the ttm_resource object as base into the driver backends. v2: fix typo in vmwgfx grid mgr and double init in amdgpu_vram_mgr.c Signed-off-by: Christian König <[email protected]> Reviewed-by: Matthew Auld <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent d3bcb4b commit cb1c814

16 files changed

+140
-189
lines changed

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ to_gtt_mgr(struct ttm_resource_manager *man)
4040
static inline struct amdgpu_gtt_node *
4141
to_amdgpu_gtt_node(struct ttm_resource *res)
4242
{
43-
return container_of(res->mm_node, struct amdgpu_gtt_node,
44-
base.mm_nodes[0]);
43+
return container_of(res, struct amdgpu_gtt_node, base.base);
4544
}
4645

4746
/**
@@ -102,13 +101,13 @@ const struct attribute_group amdgpu_gtt_mgr_attr_group = {
102101
/**
103102
* amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space
104103
*
105-
* @mem: the mem object to check
104+
* @res: the mem object to check
106105
*
107106
* Check if a mem object has already address space allocated.
108107
*/
109-
bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
108+
bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *res)
110109
{
111-
struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(mem);
110+
struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(res);
112111

113112
return drm_mm_node_allocated(&node->base.mm_nodes[0]);
114113
}
@@ -126,19 +125,20 @@ bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem)
126125
static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
127126
struct ttm_buffer_object *tbo,
128127
const struct ttm_place *place,
129-
struct ttm_resource *mem)
128+
struct ttm_resource **res)
130129
{
131130
struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
131+
uint32_t num_pages = PFN_UP(tbo->base.size);
132132
struct amdgpu_gtt_node *node;
133133
int r;
134134

135135
spin_lock(&mgr->lock);
136-
if ((tbo->resource == mem || tbo->resource->mem_type != TTM_PL_TT) &&
137-
atomic64_read(&mgr->available) < mem->num_pages) {
136+
if (tbo->resource && tbo->resource->mem_type != TTM_PL_TT &&
137+
atomic64_read(&mgr->available) < num_pages) {
138138
spin_unlock(&mgr->lock);
139139
return -ENOSPC;
140140
}
141-
atomic64_sub(mem->num_pages, &mgr->available);
141+
atomic64_sub(num_pages, &mgr->available);
142142
spin_unlock(&mgr->lock);
143143

144144
node = kzalloc(struct_size(node, base.mm_nodes, 1), GFP_KERNEL);
@@ -154,29 +154,28 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
154154
spin_lock(&mgr->lock);
155155
r = drm_mm_insert_node_in_range(&mgr->mm,
156156
&node->base.mm_nodes[0],
157-
mem->num_pages,
158-
tbo->page_alignment, 0,
159-
place->fpfn, place->lpfn,
157+
num_pages, tbo->page_alignment,
158+
0, place->fpfn, place->lpfn,
160159
DRM_MM_INSERT_BEST);
161160
spin_unlock(&mgr->lock);
162161
if (unlikely(r))
163162
goto err_free;
164163

165-
mem->start = node->base.mm_nodes[0].start;
164+
node->base.base.start = node->base.mm_nodes[0].start;
166165
} else {
167166
node->base.mm_nodes[0].start = 0;
168-
node->base.mm_nodes[0].size = mem->num_pages;
169-
mem->start = AMDGPU_BO_INVALID_OFFSET;
167+
node->base.mm_nodes[0].size = node->base.base.num_pages;
168+
node->base.base.start = AMDGPU_BO_INVALID_OFFSET;
170169
}
171170

172-
mem->mm_node = &node->base.mm_nodes[0];
171+
*res = &node->base.base;
173172
return 0;
174173

175174
err_free:
176175
kfree(node);
177176

178177
err_out:
179-
atomic64_add(mem->num_pages, &mgr->available);
178+
atomic64_add(num_pages, &mgr->available);
180179

181180
return r;
182181
}
@@ -190,21 +189,16 @@ static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man,
190189
* Free the allocated GTT again.
191190
*/
192191
static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
193-
struct ttm_resource *mem)
192+
struct ttm_resource *res)
194193
{
194+
struct amdgpu_gtt_node *node = to_amdgpu_gtt_node(res);
195195
struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man);
196-
struct amdgpu_gtt_node *node;
197-
198-
if (!mem->mm_node)
199-
return;
200-
201-
node = to_amdgpu_gtt_node(mem);
202196

203197
spin_lock(&mgr->lock);
204198
if (drm_mm_node_allocated(&node->base.mm_nodes[0]))
205199
drm_mm_remove_node(&node->base.mm_nodes[0]);
206200
spin_unlock(&mgr->lock);
207-
atomic64_add(mem->num_pages, &mgr->available);
201+
atomic64_add(res->num_pages, &mgr->available);
208202

209203
kfree(node);
210204
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)
12961296
if (bo->base.resv == &bo->base._resv)
12971297
amdgpu_amdkfd_remove_fence_on_pt_pd_bos(abo);
12981298

1299-
if (bo->resource->mem_type != TTM_PL_VRAM || !bo->resource->mm_node ||
1299+
if (bo->resource->mem_type != TTM_PL_VRAM ||
13001300
!(abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE))
13011301
return;
13021302

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <drm/drm_mm.h>
3030
#include <drm/ttm/ttm_resource.h>
31+
#include <drm/ttm/ttm_range_manager.h>
3132

3233
/* state back for walking over vram_mgr and gtt_mgr allocations */
3334
struct amdgpu_res_cursor {
@@ -53,7 +54,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
5354
{
5455
struct drm_mm_node *node;
5556

56-
if (!res || !res->mm_node) {
57+
if (!res) {
5758
cur->start = start;
5859
cur->size = size;
5960
cur->remaining = size;
@@ -63,7 +64,7 @@ static inline void amdgpu_res_first(struct ttm_resource *res,
6364

6465
BUG_ON(start + size > res->num_pages << PAGE_SHIFT);
6566

66-
node = res->mm_node;
67+
node = to_ttm_range_mgr_node(res)->mm_nodes;
6768
while (start >= node->size << PAGE_SHIFT)
6869
start -= node++->size << PAGE_SHIFT;
6970

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

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,20 @@ static u64 amdgpu_vram_mgr_vis_size(struct amdgpu_device *adev,
219219
u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo)
220220
{
221221
struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
222-
struct ttm_resource *mem = bo->tbo.resource;
223-
struct drm_mm_node *nodes = mem->mm_node;
224-
unsigned pages = mem->num_pages;
222+
struct ttm_resource *res = bo->tbo.resource;
223+
unsigned pages = res->num_pages;
224+
struct drm_mm_node *mm;
225225
u64 usage;
226226

227227
if (amdgpu_gmc_vram_full_visible(&adev->gmc))
228228
return amdgpu_bo_size(bo);
229229

230-
if (mem->start >= adev->gmc.visible_vram_size >> PAGE_SHIFT)
230+
if (res->start >= adev->gmc.visible_vram_size >> PAGE_SHIFT)
231231
return 0;
232232

233-
for (usage = 0; nodes && pages; pages -= nodes->size, nodes++)
234-
usage += amdgpu_vram_mgr_vis_size(adev, nodes);
233+
mm = &container_of(res, struct ttm_range_mgr_node, base)->mm_nodes[0];
234+
for (usage = 0; pages; pages -= mm->size, mm++)
235+
usage += amdgpu_vram_mgr_vis_size(adev, mm);
235236

236237
return usage;
237238
}
@@ -367,7 +368,7 @@ static void amdgpu_vram_mgr_virt_start(struct ttm_resource *mem,
367368
static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
368369
struct ttm_buffer_object *tbo,
369370
const struct ttm_place *place,
370-
struct ttm_resource *mem)
371+
struct ttm_resource **res)
371372
{
372373
unsigned long lpfn, num_nodes, pages_per_node, pages_left, pages;
373374
struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
@@ -388,7 +389,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
388389
max_bytes -= AMDGPU_VM_RESERVED_VRAM;
389390

390391
/* bail out quickly if there's likely not enough VRAM for this BO */
391-
mem_bytes = (u64)mem->num_pages << PAGE_SHIFT;
392+
mem_bytes = tbo->base.size;
392393
if (atomic64_add_return(mem_bytes, &mgr->usage) > max_bytes) {
393394
r = -ENOSPC;
394395
goto error_sub;
@@ -406,7 +407,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
406407
#endif
407408
pages_per_node = max_t(uint32_t, pages_per_node,
408409
tbo->page_alignment);
409-
num_nodes = DIV_ROUND_UP(mem->num_pages, pages_per_node);
410+
num_nodes = DIV_ROUND_UP(PFN_UP(mem_bytes), pages_per_node);
410411
}
411412

412413
node = kvmalloc(struct_size(node, mm_nodes, num_nodes),
@@ -422,8 +423,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
422423
if (place->flags & TTM_PL_FLAG_TOPDOWN)
423424
mode = DRM_MM_INSERT_HIGH;
424425

425-
mem->start = 0;
426-
pages_left = mem->num_pages;
426+
pages_left = node->base.num_pages;
427427

428428
/* Limit maximum size to 2GB due to SG table limitations */
429429
pages = min(pages_left, 2UL << (30 - PAGE_SHIFT));
@@ -451,7 +451,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
451451
}
452452

453453
vis_usage += amdgpu_vram_mgr_vis_size(adev, &node->mm_nodes[i]);
454-
amdgpu_vram_mgr_virt_start(mem, &node->mm_nodes[i]);
454+
amdgpu_vram_mgr_virt_start(&node->base, &node->mm_nodes[i]);
455455
pages_left -= pages;
456456
++i;
457457

@@ -461,10 +461,10 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
461461
spin_unlock(&mgr->lock);
462462

463463
if (i == 1)
464-
mem->placement |= TTM_PL_FLAG_CONTIGUOUS;
464+
node->base.placement |= TTM_PL_FLAG_CONTIGUOUS;
465465

466466
atomic64_add(vis_usage, &mgr->vis_usage);
467-
mem->mm_node = &node->mm_nodes[0];
467+
*res = &node->base;
468468
return 0;
469469

470470
error_free:
@@ -487,28 +487,22 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
487487
* Free the allocated VRAM again.
488488
*/
489489
static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
490-
struct ttm_resource *mem)
490+
struct ttm_resource *res)
491491
{
492+
struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res);
492493
struct amdgpu_vram_mgr *mgr = to_vram_mgr(man);
493494
struct amdgpu_device *adev = to_amdgpu_device(mgr);
494-
struct ttm_range_mgr_node *node;
495495
uint64_t usage = 0, vis_usage = 0;
496-
unsigned pages = mem->num_pages;
497-
struct drm_mm_node *nodes;
498-
499-
if (!mem->mm_node)
500-
return;
501-
502-
node = to_ttm_range_mgr_node(mem);
503-
nodes = &node->mm_nodes[0];
496+
unsigned i, pages;
504497

505498
spin_lock(&mgr->lock);
506-
while (pages) {
507-
pages -= nodes->size;
508-
drm_mm_remove_node(nodes);
509-
usage += nodes->size << PAGE_SHIFT;
510-
vis_usage += amdgpu_vram_mgr_vis_size(adev, nodes);
511-
++nodes;
499+
for (i = 0, pages = res->num_pages; pages;
500+
pages -= node->mm_nodes[i].size, ++i) {
501+
struct drm_mm_node *mm = &node->mm_nodes[i];
502+
503+
drm_mm_remove_node(mm);
504+
usage += mm->size << PAGE_SHIFT;
505+
vis_usage += amdgpu_vram_mgr_vis_size(adev, mm);
512506
}
513507
amdgpu_vram_mgr_do_reserve(man);
514508
spin_unlock(&mgr->lock);
@@ -533,7 +527,7 @@ static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
533527
* Allocate and fill a sg table from a VRAM allocation.
534528
*/
535529
int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
536-
struct ttm_resource *mem,
530+
struct ttm_resource *res,
537531
u64 offset, u64 length,
538532
struct device *dev,
539533
enum dma_data_direction dir,
@@ -549,7 +543,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
549543
return -ENOMEM;
550544

551545
/* Determine the number of DRM_MM nodes to export */
552-
amdgpu_res_first(mem, offset, length, &cursor);
546+
amdgpu_res_first(res, offset, length, &cursor);
553547
while (cursor.remaining) {
554548
num_entries++;
555549
amdgpu_res_next(&cursor, cursor.size);
@@ -569,7 +563,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
569563
* and the number of bytes from it. Access the following
570564
* DRM_MM node(s) if more buffer needs to exported
571565
*/
572-
amdgpu_res_first(mem, offset, length, &cursor);
566+
amdgpu_res_first(res, offset, length, &cursor);
573567
for_each_sgtable_sg((*sgt), sg, i) {
574568
phys_addr_t phys = cursor.start + adev->gmc.aper_base;
575569
size_t size = cursor.size;

drivers/gpu/drm/drm_gem_vram_helper.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ EXPORT_SYMBOL(drm_gem_vram_put);
250250
static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
251251
{
252252
/* Keep TTM behavior for now, remove when drivers are audited */
253-
if (WARN_ON_ONCE(!gbo->bo.resource->mm_node))
253+
if (WARN_ON_ONCE(!gbo->bo.resource ||
254+
gbo->bo.resource->mem_type == TTM_PL_SYSTEM))
254255
return 0;
255256

256257
return gbo->bo.resource->start;

drivers/gpu/drm/nouveau/nouveau_bo.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -918,12 +918,8 @@ static void nouveau_bo_move_ntfy(struct ttm_buffer_object *bo,
918918
}
919919
}
920920

921-
if (new_reg) {
922-
if (new_reg->mm_node)
923-
nvbo->offset = (new_reg->start << PAGE_SHIFT);
924-
else
925-
nvbo->offset = 0;
926-
}
921+
if (new_reg)
922+
nvbo->offset = (new_reg->start << PAGE_SHIFT);
927923

928924
}
929925

drivers/gpu/drm/nouveau/nouveau_mem.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,24 @@ void
178178
nouveau_mem_del(struct ttm_resource *reg)
179179
{
180180
struct nouveau_mem *mem = nouveau_mem(reg);
181-
if (!mem)
182-
return;
181+
183182
nouveau_mem_fini(mem);
184-
kfree(reg->mm_node);
185-
reg->mm_node = NULL;
183+
kfree(mem);
186184
}
187185

188186
int
189187
nouveau_mem_new(struct nouveau_cli *cli, u8 kind, u8 comp,
190-
struct ttm_resource *reg)
188+
struct ttm_resource **res)
191189
{
192190
struct nouveau_mem *mem;
193191

194192
if (!(mem = kzalloc(sizeof(*mem), GFP_KERNEL)))
195193
return -ENOMEM;
194+
196195
mem->cli = cli;
197196
mem->kind = kind;
198197
mem->comp = comp;
199198

200-
reg->mm_node = mem;
199+
*res = &mem->base;
201200
return 0;
202201
}

drivers/gpu/drm/nouveau/nouveau_mem.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ struct ttm_tt;
66
#include <nvif/mem.h>
77
#include <nvif/vmm.h>
88

9-
static inline struct nouveau_mem *
10-
nouveau_mem(struct ttm_resource *reg)
11-
{
12-
return reg->mm_node;
13-
}
14-
159
struct nouveau_mem {
1610
struct ttm_resource base;
1711
struct nouveau_cli *cli;
@@ -21,8 +15,14 @@ struct nouveau_mem {
2115
struct nvif_vma vma[2];
2216
};
2317

18+
static inline struct nouveau_mem *
19+
nouveau_mem(struct ttm_resource *reg)
20+
{
21+
return container_of(reg, struct nouveau_mem, base);
22+
}
23+
2424
int nouveau_mem_new(struct nouveau_cli *, u8 kind, u8 comp,
25-
struct ttm_resource *);
25+
struct ttm_resource **);
2626
void nouveau_mem_del(struct ttm_resource *);
2727
int nouveau_mem_vram(struct ttm_resource *, bool contig, u8 page);
2828
int nouveau_mem_host(struct ttm_resource *, struct ttm_tt *);

0 commit comments

Comments
 (0)