Skip to content

Commit 5a99274

Browse files
piorkovgregkh
authored andcommitted
drm/xe: Unify the initialization of VRAM regions
[ Upstream commit 4b0a5f5ce7849aab7a67ba9f113ed75626f6de36 ] Currently in the drivers we have defined VRAM regions per device and per tile. Initialization of these regions is done in two completely different ways. To simplify the logic of the code and make it easier to add new regions in the future, let's unify the way we initialize VRAM regions. v2: - fix doc comments in struct xe_vram_region - remove unnecessary includes (Jani) v3: - move code from xe_vram_init_regions_managers to xe_tile_init_noalloc (Matthew) - replace ioremap_wc to devm_ioremap_wc for mapping VRAM BAR (Matthew) - Replace the tile id parameter with vram region in the xe_pf_begin function. v4: - remove tile back pointer from struct xe_vram_region - add new back pointers: xe and migarte to xe_vram_region Signed-off-by: Piotr Piórkowski <[email protected]> Cc: Stuart Summers <[email protected]> Cc: Matthew Auld <[email protected]> Cc: Jani Nikula <[email protected]> Reviewed-by: Matthew Auld <[email protected]> # rev3 Acked-by: Matthew Brost <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lucas De Marchi <[email protected]> Stable-dep-of: d30203739be7 ("drm/xe: Move rebar to be done earlier") Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 1426f15 commit 5a99274

10 files changed

Lines changed: 164 additions & 125 deletions

File tree

drivers/gpu/drm/xe/xe_bo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "xe_macros.h"
1313
#include "xe_vm_types.h"
1414
#include "xe_vm.h"
15+
#include "xe_vram_types.h"
1516

1617
#define XE_DEFAULT_GTT_SIZE_MB 3072ULL /* 3GB by default */
1718

@@ -23,8 +24,9 @@
2324
#define XE_BO_FLAG_VRAM_MASK (XE_BO_FLAG_VRAM0 | XE_BO_FLAG_VRAM1)
2425
/* -- */
2526
#define XE_BO_FLAG_STOLEN BIT(4)
27+
#define XE_BO_FLAG_VRAM(vram) (XE_BO_FLAG_VRAM0 << ((vram)->id))
2628
#define XE_BO_FLAG_VRAM_IF_DGFX(tile) (IS_DGFX(tile_to_xe(tile)) ? \
27-
XE_BO_FLAG_VRAM0 << (tile)->id : \
29+
XE_BO_FLAG_VRAM((tile)->mem.vram) : \
2830
XE_BO_FLAG_SYSTEM)
2931
#define XE_BO_FLAG_GGTT BIT(5)
3032
#define XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE BIT(6)

drivers/gpu/drm/xe/xe_gt_pagefault.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "xe_svm.h"
2424
#include "xe_trace_bo.h"
2525
#include "xe_vm.h"
26+
#include "xe_vram_types.h"
2627

2728
struct pagefault {
2829
u64 page_addr;
@@ -74,7 +75,7 @@ static bool vma_is_valid(struct xe_tile *tile, struct xe_vma *vma)
7475
}
7576

7677
static int xe_pf_begin(struct drm_exec *exec, struct xe_vma *vma,
77-
bool atomic, unsigned int id)
78+
bool atomic, struct xe_vram_region *vram)
7879
{
7980
struct xe_bo *bo = xe_vma_bo(vma);
8081
struct xe_vm *vm = xe_vma_vm(vma);
@@ -84,14 +85,16 @@ static int xe_pf_begin(struct drm_exec *exec, struct xe_vma *vma,
8485
if (err)
8586
return err;
8687

87-
if (atomic && IS_DGFX(vm->xe)) {
88+
if (atomic && vram) {
89+
xe_assert(vm->xe, IS_DGFX(vm->xe));
90+
8891
if (xe_vma_is_userptr(vma)) {
8992
err = -EACCES;
9093
return err;
9194
}
9295

9396
/* Migrate to VRAM, move should invalidate the VMA first */
94-
err = xe_bo_migrate(bo, XE_PL_VRAM0 + id);
97+
err = xe_bo_migrate(bo, vram->placement);
9598
if (err)
9699
return err;
97100
} else if (bo) {
@@ -138,7 +141,7 @@ static int handle_vma_pagefault(struct xe_gt *gt, struct xe_vma *vma,
138141
/* Lock VM and BOs dma-resv */
139142
drm_exec_init(&exec, 0, 0);
140143
drm_exec_until_all_locked(&exec) {
141-
err = xe_pf_begin(&exec, vma, atomic, tile->id);
144+
err = xe_pf_begin(&exec, vma, atomic, tile->mem.vram);
142145
drm_exec_retry_on_contention(&exec);
143146
if (xe_vm_validate_should_retry(&exec, err, &end))
144147
err = -EAGAIN;
@@ -573,7 +576,7 @@ static int handle_acc(struct xe_gt *gt, struct acc *acc)
573576
/* Lock VM and BOs dma-resv */
574577
drm_exec_init(&exec, 0, 0);
575578
drm_exec_until_all_locked(&exec) {
576-
ret = xe_pf_begin(&exec, vma, true, tile->id);
579+
ret = xe_pf_begin(&exec, vma, true, tile->mem.vram);
577580
drm_exec_retry_on_contention(&exec);
578581
if (ret)
579582
break;

drivers/gpu/drm/xe/xe_query.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "xe_oa.h"
2828
#include "xe_pxp.h"
2929
#include "xe_ttm_vram_mgr.h"
30+
#include "xe_vram_types.h"
3031
#include "xe_wa.h"
3132

3233
static const u16 xe_to_user_engine_class[] = {
@@ -407,7 +408,7 @@ static int query_gt_list(struct xe_device *xe, struct drm_xe_device_query *query
407408
gt_list->gt_list[iter].near_mem_regions = 0x1;
408409
else
409410
gt_list->gt_list[iter].near_mem_regions =
410-
BIT(gt_to_tile(gt)->id) << 1;
411+
BIT(gt_to_tile(gt)->mem.vram->id) << 1;
411412
gt_list->gt_list[iter].far_mem_regions = xe->info.mem_region_mask ^
412413
gt_list->gt_list[iter].near_mem_regions;
413414

drivers/gpu/drm/xe/xe_svm.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,11 @@ static u64 xe_vram_region_page_to_dpa(struct xe_vram_region *vr,
311311
struct page *page)
312312
{
313313
u64 dpa;
314-
struct xe_tile *tile = vr->tile;
315314
u64 pfn = page_to_pfn(page);
316315
u64 offset;
317316

318-
xe_tile_assert(tile, is_device_private_page(page));
319-
xe_tile_assert(tile, (pfn << PAGE_SHIFT) >= vr->hpa_base);
317+
xe_assert(vr->xe, is_device_private_page(page));
318+
xe_assert(vr->xe, (pfn << PAGE_SHIFT) >= vr->hpa_base);
320319

321320
offset = (pfn << PAGE_SHIFT) - vr->hpa_base;
322321
dpa = vr->dpa_base + offset;
@@ -333,7 +332,7 @@ static int xe_svm_copy(struct page **pages, dma_addr_t *dma_addr,
333332
unsigned long npages, const enum xe_svm_copy_dir dir)
334333
{
335334
struct xe_vram_region *vr = NULL;
336-
struct xe_tile *tile;
335+
struct xe_device *xe;
337336
struct dma_fence *fence = NULL;
338337
unsigned long i;
339338
#define XE_VRAM_ADDR_INVALID ~0x0ull
@@ -366,7 +365,7 @@ static int xe_svm_copy(struct page **pages, dma_addr_t *dma_addr,
366365

367366
if (!vr && spage) {
368367
vr = page_to_vr(spage);
369-
tile = vr->tile;
368+
xe = vr->xe;
370369
}
371370
XE_WARN_ON(spage && page_to_vr(spage) != vr);
372371

@@ -398,18 +397,18 @@ static int xe_svm_copy(struct page **pages, dma_addr_t *dma_addr,
398397

399398
if (vram_addr != XE_VRAM_ADDR_INVALID) {
400399
if (sram) {
401-
vm_dbg(&tile->xe->drm,
400+
vm_dbg(&xe->drm,
402401
"COPY TO SRAM - 0x%016llx -> 0x%016llx, NPAGES=%ld",
403402
vram_addr, (u64)dma_addr[pos], i - pos + incr);
404-
__fence = xe_migrate_from_vram(tile->migrate,
403+
__fence = xe_migrate_from_vram(vr->migrate,
405404
i - pos + incr,
406405
vram_addr,
407406
dma_addr + pos);
408407
} else {
409-
vm_dbg(&tile->xe->drm,
408+
vm_dbg(&xe->drm,
410409
"COPY TO VRAM - 0x%016llx -> 0x%016llx, NPAGES=%ld",
411410
(u64)dma_addr[pos], vram_addr, i - pos + incr);
412-
__fence = xe_migrate_to_vram(tile->migrate,
411+
__fence = xe_migrate_to_vram(vr->migrate,
413412
i - pos + incr,
414413
dma_addr + pos,
415414
vram_addr);
@@ -434,17 +433,17 @@ static int xe_svm_copy(struct page **pages, dma_addr_t *dma_addr,
434433
/* Extra mismatched device page, copy it */
435434
if (!match && last && vram_addr != XE_VRAM_ADDR_INVALID) {
436435
if (sram) {
437-
vm_dbg(&tile->xe->drm,
436+
vm_dbg(&xe->drm,
438437
"COPY TO SRAM - 0x%016llx -> 0x%016llx, NPAGES=%d",
439438
vram_addr, (u64)dma_addr[pos], 1);
440-
__fence = xe_migrate_from_vram(tile->migrate, 1,
439+
__fence = xe_migrate_from_vram(vr->migrate, 1,
441440
vram_addr,
442441
dma_addr + pos);
443442
} else {
444-
vm_dbg(&tile->xe->drm,
443+
vm_dbg(&xe->drm,
445444
"COPY TO VRAM - 0x%016llx -> 0x%016llx, NPAGES=%d",
446445
(u64)dma_addr[pos], vram_addr, 1);
447-
__fence = xe_migrate_to_vram(tile->migrate, 1,
446+
__fence = xe_migrate_to_vram(vr->migrate, 1,
448447
dma_addr + pos,
449448
vram_addr);
450449
}
@@ -502,9 +501,9 @@ static u64 block_offset_to_pfn(struct xe_vram_region *vr, u64 offset)
502501
return PHYS_PFN(offset + vr->hpa_base);
503502
}
504503

505-
static struct drm_buddy *tile_to_buddy(struct xe_tile *tile)
504+
static struct drm_buddy *vram_to_buddy(struct xe_vram_region *vram)
506505
{
507-
return &tile->mem.vram->ttm.mm;
506+
return &vram->ttm.mm;
508507
}
509508

510509
static int xe_svm_populate_devmem_pfn(struct drm_pagemap_devmem *devmem_allocation,
@@ -518,8 +517,7 @@ static int xe_svm_populate_devmem_pfn(struct drm_pagemap_devmem *devmem_allocati
518517

519518
list_for_each_entry(block, blocks, link) {
520519
struct xe_vram_region *vr = block->private;
521-
struct xe_tile *tile = vr->tile;
522-
struct drm_buddy *buddy = tile_to_buddy(tile);
520+
struct drm_buddy *buddy = vram_to_buddy(vr);
523521
u64 block_pfn = block_offset_to_pfn(vr, drm_buddy_block_offset(block));
524522
int i;
525523

@@ -685,8 +683,7 @@ static int xe_drm_pagemap_populate_mm(struct drm_pagemap *dpagemap,
685683
unsigned long timeslice_ms)
686684
{
687685
struct xe_vram_region *vr = container_of(dpagemap, typeof(*vr), dpagemap);
688-
struct xe_tile *tile = vr->tile;
689-
struct xe_device *xe = tile_to_xe(tile);
686+
struct xe_device *xe = vr->xe;
690687
struct device *dev = xe->drm.dev;
691688
struct drm_buddy_block *block;
692689
struct list_head *blocks;
@@ -700,9 +697,9 @@ static int xe_drm_pagemap_populate_mm(struct drm_pagemap *dpagemap,
700697
xe_pm_runtime_get(xe);
701698

702699
retry:
703-
bo = xe_bo_create_locked(tile_to_xe(tile), NULL, NULL, end - start,
700+
bo = xe_bo_create_locked(vr->xe, NULL, NULL, end - start,
704701
ttm_bo_type_device,
705-
XE_BO_FLAG_VRAM_IF_DGFX(tile) |
702+
(IS_DGFX(xe) ? XE_BO_FLAG_VRAM(vr) : XE_BO_FLAG_SYSTEM) |
706703
XE_BO_FLAG_CPU_ADDR_MIRROR);
707704
if (IS_ERR(bo)) {
708705
err = PTR_ERR(bo);
@@ -712,9 +709,7 @@ static int xe_drm_pagemap_populate_mm(struct drm_pagemap *dpagemap,
712709
}
713710

714711
drm_pagemap_devmem_init(&bo->devmem_allocation, dev, mm,
715-
&dpagemap_devmem_ops,
716-
&tile->mem.vram->dpagemap,
717-
end - start);
712+
&dpagemap_devmem_ops, dpagemap, end - start);
718713

719714
blocks = &to_xe_ttm_vram_mgr_resource(bo->ttm.resource)->blocks;
720715
list_for_each_entry(block, blocks, link)

drivers/gpu/drm/xe/xe_tile.c

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <drm/drm_managed.h>
99

10+
#include "xe_bo.h"
1011
#include "xe_device.h"
1112
#include "xe_ggtt.h"
1213
#include "xe_gt.h"
@@ -114,11 +115,9 @@ int xe_tile_alloc_vram(struct xe_tile *tile)
114115
if (!IS_DGFX(xe))
115116
return 0;
116117

117-
vram = drmm_kzalloc(&xe->drm, sizeof(*vram), GFP_KERNEL);
118-
if (!vram)
119-
return -ENOMEM;
120-
121-
vram->tile = tile;
118+
vram = xe_vram_region_alloc(xe, tile->id, XE_PL_VRAM0 + tile->id);
119+
if (IS_ERR(vram))
120+
return PTR_ERR(vram);
122121
tile->mem.vram = vram;
123122

124123
return 0;
@@ -156,21 +155,6 @@ int xe_tile_init_early(struct xe_tile *tile, struct xe_device *xe, u8 id)
156155
}
157156
ALLOW_ERROR_INJECTION(xe_tile_init_early, ERRNO); /* See xe_pci_probe() */
158157

159-
static int tile_ttm_mgr_init(struct xe_tile *tile)
160-
{
161-
struct xe_device *xe = tile_to_xe(tile);
162-
int err;
163-
164-
if (tile->mem.vram) {
165-
err = xe_ttm_vram_mgr_init(tile, &tile->mem.vram->ttm);
166-
if (err)
167-
return err;
168-
xe->info.mem_region_mask |= BIT(tile->id) << 1;
169-
}
170-
171-
return 0;
172-
}
173-
174158
/**
175159
* xe_tile_init_noalloc - Init tile up to the point where allocations can happen.
176160
* @tile: The tile to initialize.
@@ -188,17 +172,20 @@ static int tile_ttm_mgr_init(struct xe_tile *tile)
188172
int xe_tile_init_noalloc(struct xe_tile *tile)
189173
{
190174
struct xe_device *xe = tile_to_xe(tile);
191-
int err;
192-
193-
err = tile_ttm_mgr_init(tile);
194-
if (err)
195-
return err;
196175

197176
xe_wa_apply_tile_workarounds(tile);
198177

199178
if (xe->info.has_usm && IS_DGFX(xe))
200179
xe_devm_add(tile, tile->mem.vram);
201180

181+
if (IS_DGFX(xe) && !ttm_resource_manager_used(&tile->mem.vram->ttm.manager)) {
182+
int err = xe_ttm_vram_mgr_init(xe, tile->mem.vram);
183+
184+
if (err)
185+
return err;
186+
xe->info.mem_region_mask |= BIT(tile->mem.vram->id) << 1;
187+
}
188+
202189
return xe_tile_sysfs_init(tile);
203190
}
204191

drivers/gpu/drm/xe/xe_ttm_vram_mgr.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,18 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr,
338338
return drmm_add_action_or_reset(&xe->drm, ttm_vram_mgr_fini, mgr);
339339
}
340340

341-
int xe_ttm_vram_mgr_init(struct xe_tile *tile, struct xe_ttm_vram_mgr *mgr)
341+
/**
342+
* xe_ttm_vram_mgr_init - initialize TTM VRAM region
343+
* @xe: pointer to Xe device
344+
* @vram: pointer to xe_vram_region that contains the memory region attributes
345+
*
346+
* Initialize the Xe TTM for given @vram region using the given parameters.
347+
*
348+
* Returns 0 for success, negative error code otherwise.
349+
*/
350+
int xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_vram_region *vram)
342351
{
343-
struct xe_device *xe = tile_to_xe(tile);
344-
struct xe_vram_region *vram = tile->mem.vram;
345-
346-
return __xe_ttm_vram_mgr_init(xe, mgr, XE_PL_VRAM0 + tile->id,
352+
return __xe_ttm_vram_mgr_init(xe, &vram->ttm, vram->placement,
347353
xe_vram_region_usable_size(vram),
348354
xe_vram_region_io_size(vram),
349355
PAGE_SIZE);

drivers/gpu/drm/xe/xe_ttm_vram_mgr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
enum dma_data_direction;
1212
struct xe_device;
1313
struct xe_tile;
14+
struct xe_vram_region;
1415

1516
int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr,
1617
u32 mem_type, u64 size, u64 io_size,
1718
u64 default_page_size);
18-
int xe_ttm_vram_mgr_init(struct xe_tile *tile, struct xe_ttm_vram_mgr *mgr);
19+
int xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_vram_region *vram);
1920
int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
2021
struct ttm_resource *res,
2122
u64 offset, u64 length,

0 commit comments

Comments
 (0)