Preliminary support for separate VRAM region for kernel allocations on tiles
Subsystems (2)
v2 cover Preliminary support for separate VRAM region for kernel allocations on tiles
From: Piotr Piórkowski <[email protected]>
Until now, all VRAM BO allocations, whether from userspace or the kernel, were placed in the same VRAM region assigned to a tile. However, in some scenarios, it may be necessary to separate these allocations and place kernel BOs in a dedicated VRAM region.
This series introduces initial support for such separation by allowing an additional VRAM region for kernel BOs to be assigned to tiles. In this implementation, such BOs are identified by the ttm_bo_type_kernel flag.
To maintain flexibility, this series introduces a new BO flag:XE_BO_FLAG_FORCE_USER_VRAM. It forces BO allocations into the general-purpose VRAM region, overriding the new default behavior. This override currently applies to BOs associated with user contexts and BOs used for VF memory.
To preserve backward compatibility on platforms that do not have a dedicated kernel VRAM region, the kernel_vram pointer will fall back to the general-purpose VRAM region.
v1 -> v2:
- update commit message of "drm/xe: Introduce new BO flag XE_BO_FLAG_FORCE_USER_VRAM"
- replaced for_each_bo_flag_vram with the improved for_each_set_bo_vram_flag helper
- moved the VRAM flag iteration macro definition into xe_bo.c
- drop unused bo_flgas from bo_vram_flags_to_vram_placement in
"drm/xe: Add initial support for separate kernel VRAM region on the tile"
- use hweight32 helper in __xe_bo_fixed_placement for readability
Piotr Piórkowski (4):
drm/xe: Add initial support for separate kernel VRAM region on the
tile
drm/xe: Introduce new BO flag XE_BO_FLAG_FORCE_USER_VRAM drm/xe: Force user context allocations in user VRAM drm/xe/pf: Force use user VRAM for LMEM provisioning
drivers/gpu/drm/xe/xe_bo.c | 90 ++++++++++++++++------ drivers/gpu/drm/xe/xe_bo.h | 3 +- drivers/gpu/drm/xe/xe_device_types.h | 10 ++- drivers/gpu/drm/xe/xe_exec_queue.c | 7 +- drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c | 3 +- drivers/gpu/drm/xe/xe_lrc.c | 3 + drivers/gpu/drm/xe/xe_lrc.h | 6 +- drivers/gpu/drm/xe/xe_pt.c | 2 +- drivers/gpu/drm/xe/xe_tile.c | 8 ++ drivers/gpu/drm/xe/xe_tile.h | 5 ++ drivers/gpu/drm/xe/xe_vram.c | 6 +- 11 files changed, 109 insertions(+), 34 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/4 drm/xe: Add initial support for separate kernel VRAM region on the tile
From: Piotr Piórkowski <[email protected]>
So far, kernel and userspace allocations have shared the same VRAM region. However, in some scenarios, it may be necessary to reserve a separate VRAM area exclusively for kernel allocations.
Let's add preliminary support for such a configuration.
v2:
- replaced for_each_bo_flag_vram with the improved for_each_set_bo_vram_flag helper (Matthew)
- moved the VRAM flag iteration macro definition into xe_bo.c (Matthew)
- drop unused bo_flgas from bo_vram_flags_to_vram_placement (Matthew)
- use hweight32 helper in __xe_bo_fixed_placement for readability (Matthew)
--- drivers/gpu/drm/xe/xe_bo.c | 89 ++++++++++++++++++++-------- drivers/gpu/drm/xe/xe_bo.h | 2 +- drivers/gpu/drm/xe/xe_device_types.h | 10 +++- drivers/gpu/drm/xe/xe_tile.c | 8 +++ drivers/gpu/drm/xe/xe_tile.h | 5 ++ drivers/gpu/drm/xe/xe_vram.c | 6 +- 6 files changed, 92 insertions(+), 28 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +64 -25
@@ -34,6 +34,7 @@#include "xe_res_cursor.h" #include "xe_shrinker.h" #include "xe_sriov_vf_ccs.h" +#include "xe_tile.h" #include "xe_trace_bo.h" #include "xe_ttm_stolen_mgr.h" #include "xe_vm.h"
@@ -81,6 +82,10 @@ static struct ttm_placement tt_placement = { .placement = tt_placement_flags, }; +#define for_each_set_bo_vram_flag (bit__, bo_flags__) \ + for (unsigned int __bit_tmp = BIT(0); __bit_tmp <= XE_BO_FLAG_VRAM_MASK; __bit_tmp <<= 1) \ + for_each_if(((bit__) = __bit_tmp) & (bo_flags__) & XE_BO_FLAG_VRAM_MASK) +bool mem_type_is_vram (u32 mem_type) {return mem_type >=XE_PL_VRAM0 && mem_type !=XE_PL_STOLEN ;
@@ -213,6 +218,27 @@ static bool force_contiguous(u32 bo_flags) bo_flags &XE_BO_FLAG_PINNED ; } +static u8 vram_bo_flag_to_tile_id (struct xe_device *xe,u32 vram_bo_flag) +{ +xe_assert (xe, vram_bo_flag &XE_BO_FLAG_VRAM_MASK ); +xe_assert (xe, (vram_bo_flag & (vram_bo_flag -1 )) ==0 ); + +return __ffs (vram_bo_flag >> (__ffs (XE_BO_FLAG_VRAM0 ) -1 )) -1 ; +} + +static u32 bo_vram_flags_to_vram_placement (struct xe_device *xe,u32 vram_flag, +enum ttm_bo_type type) +{ +u8 tile_id =vram_bo_flag_to_tile_id (xe, vram_flag); + +xe_assert (xe, tile_id < xe->info.tile_count); + +if (type == ttm_bo_type_kernel) +return xe->tiles[tile_id].mem.kernel_vram->placement; +else +return xe->tiles[tile_id].mem.vram->placement; +} +static void add_vram (struct xe_device *xe,struct xe_bo *bo,struct ttm_place *places,u32 bo_flags,u32 mem_type,u32 *c) {
@@ -245,12 +271,15 @@ static void add_vram(struct xe_device *xe, struct xe_bo *bo, }static void try_add_vram (struct xe_device *xe,struct xe_bo *bo, -u32 bo_flags,u32 *c) +u32 bo_flags,enum ttm_bo_type type,u32 *c) { -if (bo_flags &XE_BO_FLAG_VRAM0 ) -add_vram (xe, bo, bo->placements, bo_flags,XE_PL_VRAM0 , c); -if (bo_flags &XE_BO_FLAG_VRAM1 ) -add_vram (xe, bo, bo->placements, bo_flags,XE_PL_VRAM1 , c); +u32 vram_flag; + +for_each_set_bo_vram_flag (vram_flag, bo_flags) { +u32 pl =bo_vram_flags_to_vram_placement (xe, vram_flag, type); + +add_vram (xe, bo, bo->placements, bo_flags, pl, c); + } } static void try_add_stolen(struct xe_device *xe, struct xe_bo *bo,
@@ -269,11 +298,11 @@ static void try_add_stolen(struct xe_device *xe, struct xe_bo *bo, }static int __xe_bo_placement_for_flags (struct xe_device *xe,struct xe_bo *bo, -u32 bo_flags) +u32 bo_flags,enum ttm_bo_type type) {u32 c =0 ; -try_add_vram (xe, bo, bo_flags, &c); +try_add_vram (xe, bo, bo_flags, type, &c);try_add_system (xe, bo, bo_flags, &c);try_add_stolen (xe, bo, bo_flags, &c);
@@ -289,10 +318,10 @@ static int __xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo, }int xe_bo_placement_for_flags (struct xe_device *xe,struct xe_bo *bo, -u32 bo_flags) +u32 bo_flags,enum ttm_bo_type type) {xe_bo_assert_held (bo); -return __xe_bo_placement_for_flags (xe, bo, bo_flags); +return __xe_bo_placement_for_flags (xe, bo, bo_flags, type); } static void xe_evict_flags(struct ttm_buffer_object *tbo,
@@ -1938,7 +1967,7 @@ struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo, }if (!(flags &XE_BO_FLAG_FIXED_PLACEMENT )) { - err =__xe_bo_placement_for_flags (xe, bo, bo->flags); + err =__xe_bo_placement_for_flags (xe, bo, bo->flags, type);if (WARN_ON (err)) {xe_ttm_bo_destroy (&bo->ttm);return ERR_PTR (err);
@@ -1996,34 +2025,33 @@ struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo, }static int __xe_bo_fixed_placement (struct xe_device *xe, -struct xe_bo *bo, +struct xe_bo *bo,enum ttm_bo_type type,u32 flags,u64 start,u64 end,u64 size) {struct ttm_place *place = bo->placements; +u32 vram_flag, vram_stolen_flags;if (flags & (XE_BO_FLAG_USER |XE_BO_FLAG_SYSTEM ))return -EINVAL ; + vram_flag = flags &XE_BO_FLAG_VRAM_MASK ; + vram_stolen_flags = (flags & (XE_BO_FLAG_STOLEN )) | vram_flag; + +/* check if more than one VRAM/STOLEN flag is set */ +if (hweight32 (vram_stolen_flags) >1 ) +return -EINVAL ; + place->flags =TTM_PL_FLAG_CONTIGUOUS ; place->fpfn = start >>PAGE_SHIFT ; place->lpfn = end >>PAGE_SHIFT ; -switch (flags & (XE_BO_FLAG_STOLEN |XE_BO_FLAG_VRAM_MASK )) { -case XE_BO_FLAG_VRAM0 : - place->mem_type =XE_PL_VRAM0 ; -break ; -case XE_BO_FLAG_VRAM1 : - place->mem_type =XE_PL_VRAM1 ; -break ; -case XE_BO_FLAG_STOLEN : +if (flags &XE_BO_FLAG_STOLEN ) place->mem_type =XE_PL_STOLEN ; -break ; - -default : -/* 0 or multiple of the above set */ +else if (vram_flag) + place->mem_type =bo_vram_flags_to_vram_placement (xe, vram_flag, type); +else return -EINVAL ; - } bo->placement = (struct ttm_placement ) { .num_placement =1 ,
@@ -2046,13 +2074,24 @@ __xe_bo_create_locked(struct xe_device *xe,if (vm)xe_vm_assert_held (vm); +/* +* In the case of kernel allocations, if the tile has dedicated kernel +* VRAM region, and tile->id does not match to tile->vram_id, it means +* that we are using unified VRAM and we need fix VRAM BO flags. +*/ +if (tile && type == ttm_bo_type_kernel &&xe_tile_has_separate_kernel_vram (tile) && + (flags &XE_BO_FLAG_VRAM_MASK ) && tile->mem.vram->id != tile->mem.kernel_vram->id) { + flags &= ~XE_BO_FLAG_VRAM_MASK ; + flags |= (XE_BO_FLAG_VRAM0 << tile->mem.kernel_vram->id); + } +if (start || end != ~0ULL ) { bo =xe_bo_alloc ();if (IS_ERR (bo))return bo; flags |=XE_BO_FLAG_FIXED_PLACEMENT ; - err =__xe_bo_fixed_placement (xe, bo, flags, start, end, size); + err =__xe_bo_fixed_placement (xe, bo, type, flags, start, end, size);if (err) {xe_bo_free (bo);return ERR_PTR (err);
drivers/gpu/drm/xe/xe_bo.h +1 -1
@@ -127,7 +127,7 @@ struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tilint xe_managed_bo_reinit_in_vram (struct xe_device *xe,struct xe_tile *tile,struct xe_bo **src);int xe_bo_placement_for_flags (struct xe_device *xe,struct xe_bo *bo, -u32 bo_flags); +u32 bo_flags,enum ttm_bo_type type);static inline struct xe_bo *ttm_to_xe_bo (const struct ttm_buffer_object *bo) {
drivers/gpu/drm/xe/xe_device_types.h +9 -1
@@ -156,7 +156,15 @@ struct xe_tile {/** @mem: memory management info for tile */ struct {/** -* @mem.vram: VRAM info for tile. +* @mem.kernel_vram: kernel-dedicated VRAM info for tile. +* +* Although VRAM is associated with a specific tile, it can +* still be accessed by all tiles' GTs. +*/ +struct xe_vram_region *kernel_vram; + +/** +* @mem.vram: general purpose VRAM info for tile. * * Although VRAM is associated with a specific tile, it can * still be accessed by all tiles' GTs.
drivers/gpu/drm/xe/xe_tile.c +8 -0
@@ -124,6 +124,14 @@ int xe_tile_alloc_vram(struct xe_tile *tile)return -ENOMEM ; tile->mem.vram = vram; +/* +* If the kernel_vram is not already allocated, +* it means that tile has common VRAM region for +* kernel and user space. +*/ +if (!tile->mem.kernel_vram) + tile->mem.kernel_vram = tile->mem.vram; +return 0 ; }
drivers/gpu/drm/xe/xe_tile.h +5 -0
@@ -23,4 +23,9 @@ static inline bool xe_tile_is_root(struct xe_tile *tile) return tile->id == 0; } +static inline bool xe_tile_has_separate_kernel_vram (const struct xe_tile *tile) +{ +return tile->mem.vram != tile->mem.kernel_vram; +} +#endif
drivers/gpu/drm/xe/xe_vram.c +5 -1
@@ -13,6 +13,7 @@#include "regs/xe_gt_regs.h" #include "regs/xe_regs.h" #include "xe_assert.h" +#include "xe_bo.h" #include "xe_device.h" #include "xe_force_wake.h" #include "xe_gt_mcr.h"
@@ -283,8 +284,11 @@ static void vram_fini(void *arg) xe->mem.vram->mapping =NULL ; -for_each_tile (tile, xe, id) +for_each_tile (tile, xe, id) { tile->mem.vram->mapping =NULL ; +if (tile->mem.kernel_vram) + tile->mem.kernel_vram->mapping =NULL ; + } }struct xe_vram_region *xe_vram_region_alloc (struct xe_device *xe,u8 id,u32 placement)
Signature · 1 line
2.34.1
PATCH v2 2/4 drm/xe: Introduce new BO flag XE_BO_FLAG_FORCE_USER_VRAM
In reply to Piórkowski, PiotrFrom: Piotr Piórkowski <[email protected]>
When using a separate VRAM region for kernel allocations, some kernel structures, such as context userspace data, should not reside in the VRAM region dedicated to the kernel. The VRAM kernel region is intended only for allocations necessary for driver operation. Allocations created via ioctl are long-lived and not easily evictable. If this region runs out of space, there may not be a fallback, which could cause failures.
To prevent this, add a new BO flag that explicitly forces the BO to be allocated in the general-purpose VRAM region accessible to userspace, avoiding the kernel-only VRAM region.
v2:
- update commit message (Matthew)
--- drivers/gpu/drm/xe/xe_bo.c | 11 ++++++----- drivers/gpu/drm/xe/xe_bo.h | 1 + 2 files changed, 7 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +6 -5
@@ -226,14 +226,14 @@ static u8 vram_bo_flag_to_tile_id(struct xe_device *xe, u32 vram_bo_flag)return __ffs (vram_bo_flag >> (__ffs (XE_BO_FLAG_VRAM0 ) -1 )) -1 ; } -static u32 bo_vram_flags_to_vram_placement (struct xe_device *xe,u32 vram_flag, +static u32 bo_vram_flags_to_vram_placement (struct xe_device *xe,u32 bo_flags,u32 vram_flag,enum ttm_bo_type type) {u8 tile_id =vram_bo_flag_to_tile_id (xe, vram_flag);xe_assert (xe, tile_id < xe->info.tile_count); -if (type == ttm_bo_type_kernel) +if (type == ttm_bo_type_kernel && !(bo_flags &XE_BO_FLAG_FORCE_USER_VRAM ))return xe->tiles[tile_id].mem.kernel_vram->placement;else return xe->tiles[tile_id].mem.vram->placement;
@@ -276,7 +276,7 @@ static void try_add_vram(struct xe_device *xe, struct xe_bo *bo,u32 vram_flag;for_each_set_bo_vram_flag (vram_flag, bo_flags) { -u32 pl =bo_vram_flags_to_vram_placement (xe, vram_flag, type); +u32 pl =bo_vram_flags_to_vram_placement (xe, bo_flags, vram_flag, type);add_vram (xe, bo, bo->placements, bo_flags, pl, c); }
@@ -2049,7 +2049,7 @@ static int __xe_bo_fixed_placement(struct xe_device *xe,if (flags &XE_BO_FLAG_STOLEN ) place->mem_type =XE_PL_STOLEN ;else if (vram_flag) - place->mem_type =bo_vram_flags_to_vram_placement (xe, vram_flag, type); + place->mem_type =bo_vram_flags_to_vram_placement (xe, flags, vram_flag, type);else return -EINVAL ;
@@ -2080,7 +2080,8 @@ __xe_bo_create_locked(struct xe_device *xe,* that we are using unified VRAM and we need fix VRAM BO flags. */ if (tile && type == ttm_bo_type_kernel &&xe_tile_has_separate_kernel_vram (tile) && - (flags &XE_BO_FLAG_VRAM_MASK ) && tile->mem.vram->id != tile->mem.kernel_vram->id) { + (flags &XE_BO_FLAG_VRAM_MASK ) && tile->mem.vram->id != tile->mem.kernel_vram->id && + !(flags &XE_BO_FLAG_FORCE_USER_VRAM )) { flags &= ~XE_BO_FLAG_VRAM_MASK ; flags |= (XE_BO_FLAG_VRAM0 << tile->mem.kernel_vram->id); }
drivers/gpu/drm/xe/xe_bo.h +1 -0
@@ -48,6 +48,7 @@#define XE_BO_FLAG_GGTT2 BIT(22)#define XE_BO_FLAG_GGTT3 BIT(23)#define XE_BO_FLAG_CPU_ADDR_MIRROR BIT(24) +#define XE_BO_FLAG_FORCE_USER_VRAM BIT(25)/* this one is trigger internally only */ #define XE_BO_FLAG_INTERNAL_TEST BIT(30)
Signature · 1 line
2.34.1
PATCH v2 3/4 drm/xe: Force user context allocations in user VRAM
In reply to Piórkowski, PiotrFrom: Piotr Piórkowski <[email protected]>
In general, kernel structures should be allocated in the kernel-dedicated VRAM region. However, userspace context data - while used by the kernel - does not need to reside there.
Let's force the allocation of such data in the general-purpose VRAM region accessible to userspace.
--- drivers/gpu/drm/xe/xe_exec_queue.c | 7 +++++-- drivers/gpu/drm/xe/xe_lrc.c | 3 +++ drivers/gpu/drm/xe/xe_lrc.h | 6 ++++-- drivers/gpu/drm/xe/xe_pt.c | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-)
drivers/gpu/drm/xe/xe_exec_queue.c +5 -2
@@ -160,7 +160,7 @@ static struct xe_exec_queue *__xe_exec_queue_alloc(struct xe_device *xe,return q; } -static int __xe_exec_queue_init (struct xe_exec_queue *q) +static int __xe_exec_queue_init (struct xe_exec_queue *q,u32 exec_queue_flags) {int i, err;u32 flags =0 ;
@@ -179,6 +179,9 @@ static int __xe_exec_queue_init(struct xe_exec_queue *q) flags |=XE_LRC_CREATE_RUNALONE ; } +if (!(exec_queue_flags &EXEC_QUEUE_FLAG_KERNEL )) + flags |=XE_LRC_CREATE_USER_CTX ; +for (i =0 ; i < q->width; ++i) { q->lrc[i] =xe_lrc_create (q->hwe, q->vm,SZ_16K , q->msix_vec, flags);if (IS_ERR (q->lrc[i])) {
@@ -215,7 +218,7 @@ struct xe_exec_queue *xe_exec_queue_create(struct xe_device *xe, struct xe_vm *vif (IS_ERR (q))return q; - err =__xe_exec_queue_init (q); + err =__xe_exec_queue_init (q, flags);if (err) goto err_post_alloc;
drivers/gpu/drm/xe/xe_lrc.c +3 -0
@@ -1340,6 +1340,9 @@ static int xe_lrc_init(struct xe_lrc *lrc, struct xe_hw_engine *hwe,if (vm && vm->xef)/* userspace */ bo_flags |=XE_BO_FLAG_PINNED_LATE_RESTORE ; +if (init_flags &XE_LRC_CREATE_USER_CTX ) + bo_flags |=XE_BO_FLAG_FORCE_USER_VRAM ; + lrc->bo =xe_bo_create_pin_map (xe, tile,NULL , bo_size, ttm_bo_type_kernel, bo_flags);
drivers/gpu/drm/xe/xe_lrc.h +4 -2
@@ -44,8 +44,10 @@ struct xe_lrc_snapshot {#define LRC_WA_BB_SIZE SZ_4K -#define XE_LRC_CREATE_RUNALONE 0x1 -#define XE_LRC_CREATE_PXP 0x2 +#define XE_LRC_CREATE_RUNALONE BIT(0) +#define XE_LRC_CREATE_PXP BIT(1) +#define XE_LRC_CREATE_USER_CTX BIT(2) +struct xe_lrc *xe_lrc_create (struct xe_hw_engine *hwe,struct xe_vm *vm,u32 ring_size,u16 msix_vec,u32 flags);void xe_lrc_destroy (struct kref *ref);
drivers/gpu/drm/xe/xe_pt.c +1 -1
@@ -122,7 +122,7 @@ struct xe_pt *xe_pt_create(struct xe_vm *vm, struct xe_tile *tile,XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE |XE_BO_FLAG_NO_RESV_EVICT |XE_BO_FLAG_PAGETABLE ;if (vm->xef)/* userspace */ - bo_flags |=XE_BO_FLAG_PINNED_LATE_RESTORE ; + bo_flags |=XE_BO_FLAG_PINNED_LATE_RESTORE |XE_BO_FLAG_FORCE_USER_VRAM ; pt->level = level; bo = xe_bo_create_pin_map(vm->xe, tile, vm, SZ_4K,
Signature · 1 line
2.34.1
PATCH v2 4/4 drm/xe/pf: Force use user VRAM for LMEM provisioning
In reply to Piórkowski, PiotrFrom: Piotr Piórkowski <[email protected]>
The LMEM assigned to VFs should be allocated from the general-purpose VRAM pool, not from the kernel-reserved region.
Let's force the use of general-purpose VRAM for BOs intended for VFs.
--- drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_gt_sriov_pf_config.c +2 -1
@@ -1484,7 +1484,8 @@ static int pf_provision_vf_lmem(struct xe_gt *gt, unsigned int vfid, u64 size)XE_BO_FLAG_VRAM_IF_DGFX (tile) |XE_BO_FLAG_NEEDS_2M |XE_BO_FLAG_PINNED | - XE_BO_FLAG_PINNED_LATE_RESTORE); +XE_BO_FLAG_PINNED_LATE_RESTORE | + XE_BO_FLAG_FORCE_USER_VRAM);if (IS_ERR (bo))return PTR_ERR (bo);
Signature · 1 line
2.34.1