Skip to content

Commit d1e6efd

Browse files
committed
drm/xe: Add unbind to SVM garbage collector
Add unbind to SVM garbage collector. To facilitate add unbind support function to VM layer which unbinds a SVM range. Also teach PT layer to understand unbinds of SVM ranges. v3: - s/INVALID_VMA/XE_INVALID_VMA (Thomas) - Kernel doc (Thomas) - New GPU SVM range structure (Thomas) - s/DRM_GPUVA_OP_USER/DRM_GPUVA_OP_DRIVER (Thomas) v4: - Use xe_vma_op_unmap_range (Himal) v5: - s/PY/PT (Thomas) Signed-off-by: Matthew Brost <[email protected]> Reviewed-by: Thomas Hellström <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 63f6e48 commit d1e6efd

File tree

5 files changed

+176
-18
lines changed

5 files changed

+176
-18
lines changed

drivers/gpu/drm/xe/xe_pt.c

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -964,10 +964,16 @@ static void xe_pt_cancel_bind(struct xe_vma *vma,
964964
}
965965
}
966966

967+
#define XE_INVALID_VMA ((struct xe_vma *)(0xdeaddeadull))
968+
967969
static void xe_pt_commit_prepare_locks_assert(struct xe_vma *vma)
968970
{
969-
struct xe_vm *vm = xe_vma_vm(vma);
971+
struct xe_vm *vm;
970972

973+
if (vma == XE_INVALID_VMA)
974+
return;
975+
976+
vm = xe_vma_vm(vma);
971977
lockdep_assert_held(&vm->lock);
972978

973979
if (!xe_vma_has_no_bo(vma))
@@ -978,8 +984,12 @@ static void xe_pt_commit_prepare_locks_assert(struct xe_vma *vma)
978984

979985
static void xe_pt_commit_locks_assert(struct xe_vma *vma)
980986
{
981-
struct xe_vm *vm = xe_vma_vm(vma);
987+
struct xe_vm *vm;
982988

989+
if (vma == XE_INVALID_VMA)
990+
return;
991+
992+
vm = xe_vma_vm(vma);
983993
xe_pt_commit_prepare_locks_assert(vma);
984994

985995
if (xe_vma_is_userptr(vma))
@@ -1007,7 +1017,8 @@ static void xe_pt_commit(struct xe_vma *vma,
10071017
int j_ = j + entries[i].ofs;
10081018

10091019
pt_dir->children[j_] = pt_dir->staging[j_];
1010-
xe_pt_destroy(oldpte, xe_vma_vm(vma)->flags, deferred);
1020+
xe_pt_destroy(oldpte, (vma == XE_INVALID_VMA) ? 0 :
1021+
xe_vma_vm(vma)->flags, deferred);
10111022
}
10121023
}
10131024
}
@@ -1420,6 +1431,9 @@ static int xe_pt_svm_pre_commit(struct xe_migrate_pt_update *pt_update)
14201431
list_for_each_entry(op, &vops->list, link) {
14211432
struct xe_svm_range *range = op->map_range.range;
14221433

1434+
if (op->subop == XE_VMA_SUBOP_UNMAP_RANGE)
1435+
continue;
1436+
14231437
xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(op->map_range.vma));
14241438
xe_assert(vm->xe, op->subop == XE_VMA_SUBOP_MAP_RANGE);
14251439

@@ -1617,7 +1631,9 @@ static const struct xe_pt_walk_ops xe_pt_stage_unbind_ops = {
16171631
* xe_pt_stage_unbind() - Build page-table update structures for an unbind
16181632
* operation
16191633
* @tile: The tile we're unbinding for.
1634+
* @vm: The vm
16201635
* @vma: The vma we're unbinding.
1636+
* @range: The range we're unbinding.
16211637
* @entries: Caller-provided storage for the update structures.
16221638
*
16231639
* Builds page-table update structures for an unbind operation. The function
@@ -1627,9 +1643,14 @@ static const struct xe_pt_walk_ops xe_pt_stage_unbind_ops = {
16271643
*
16281644
* Return: The number of entries used.
16291645
*/
1630-
static unsigned int xe_pt_stage_unbind(struct xe_tile *tile, struct xe_vma *vma,
1646+
static unsigned int xe_pt_stage_unbind(struct xe_tile *tile,
1647+
struct xe_vm *vm,
1648+
struct xe_vma *vma,
1649+
struct xe_svm_range *range,
16311650
struct xe_vm_pgtable_update *entries)
16321651
{
1652+
u64 start = range ? range->base.itree.start : xe_vma_start(vma);
1653+
u64 end = range ? range->base.itree.last + 1 : xe_vma_end(vma);
16331654
struct xe_pt_stage_unbind_walk xe_walk = {
16341655
.base = {
16351656
.ops = &xe_pt_stage_unbind_ops,
@@ -1638,14 +1659,14 @@ static unsigned int xe_pt_stage_unbind(struct xe_tile *tile, struct xe_vma *vma,
16381659
.staging = true,
16391660
},
16401661
.tile = tile,
1641-
.modified_start = xe_vma_start(vma),
1642-
.modified_end = xe_vma_end(vma),
1662+
.modified_start = start,
1663+
.modified_end = end,
16431664
.wupd.entries = entries,
16441665
};
1645-
struct xe_pt *pt = xe_vma_vm(vma)->pt_root[tile->id];
1666+
struct xe_pt *pt = vm->pt_root[tile->id];
16461667

1647-
(void)xe_pt_walk_shared(&pt->base, pt->level, xe_vma_start(vma),
1648-
xe_vma_end(vma), &xe_walk.base);
1668+
(void)xe_pt_walk_shared(&pt->base, pt->level, start, end,
1669+
&xe_walk.base);
16491670

16501671
return xe_walk.wupd.num_used_entries;
16511672
}
@@ -1887,13 +1908,6 @@ static int unbind_op_prepare(struct xe_tile *tile,
18871908
"Preparing unbind, with range [%llx...%llx)\n",
18881909
xe_vma_start(vma), xe_vma_end(vma) - 1);
18891910

1890-
/*
1891-
* Wait for invalidation to complete. Can corrupt internal page table
1892-
* state if an invalidation is running while preparing an unbind.
1893-
*/
1894-
if (xe_vma_is_userptr(vma) && xe_vm_in_fault_mode(xe_vma_vm(vma)))
1895-
mmu_interval_read_begin(&to_userptr_vma(vma)->userptr.notifier);
1896-
18971911
pt_op->vma = vma;
18981912
pt_op->bind = false;
18991913
pt_op->rebind = false;
@@ -1902,7 +1916,8 @@ static int unbind_op_prepare(struct xe_tile *tile,
19021916
if (err)
19031917
return err;
19041918

1905-
pt_op->num_entries = xe_pt_stage_unbind(tile, vma, pt_op->entries);
1919+
pt_op->num_entries = xe_pt_stage_unbind(tile, xe_vma_vm(vma),
1920+
vma, NULL, pt_op->entries);
19061921

19071922
xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries,
19081923
pt_op->num_entries, false);
@@ -1917,6 +1932,42 @@ static int unbind_op_prepare(struct xe_tile *tile,
19171932
return 0;
19181933
}
19191934

1935+
static int unbind_range_prepare(struct xe_vm *vm,
1936+
struct xe_tile *tile,
1937+
struct xe_vm_pgtable_update_ops *pt_update_ops,
1938+
struct xe_svm_range *range)
1939+
{
1940+
u32 current_op = pt_update_ops->current_op;
1941+
struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op];
1942+
1943+
if (!(range->tile_present & BIT(tile->id)))
1944+
return 0;
1945+
1946+
vm_dbg(&vm->xe->drm,
1947+
"Preparing unbind, with range [%lx...%lx)\n",
1948+
range->base.itree.start, range->base.itree.last);
1949+
1950+
pt_op->vma = XE_INVALID_VMA;
1951+
pt_op->bind = false;
1952+
pt_op->rebind = false;
1953+
1954+
pt_op->num_entries = xe_pt_stage_unbind(tile, vm, NULL, range,
1955+
pt_op->entries);
1956+
1957+
xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries,
1958+
pt_op->num_entries, false);
1959+
xe_pt_update_ops_rfence_interval(pt_update_ops, range->base.itree.start,
1960+
range->base.itree.last + 1);
1961+
++pt_update_ops->current_op;
1962+
pt_update_ops->needs_svm_lock = true;
1963+
pt_update_ops->needs_invalidation = true;
1964+
1965+
xe_pt_commit_prepare_unbind(XE_INVALID_VMA, pt_op->entries,
1966+
pt_op->num_entries);
1967+
1968+
return 0;
1969+
}
1970+
19201971
static int op_prepare(struct xe_vm *vm,
19211972
struct xe_tile *tile,
19221973
struct xe_vm_pgtable_update_ops *pt_update_ops,
@@ -1984,6 +2035,9 @@ static int op_prepare(struct xe_vm *vm,
19842035
err = bind_range_prepare(vm, tile, pt_update_ops,
19852036
op->map_range.vma,
19862037
op->map_range.range);
2038+
} else if (op->subop == XE_VMA_SUBOP_UNMAP_RANGE) {
2039+
err = unbind_range_prepare(vm, tile, pt_update_ops,
2040+
op->unmap_range.range);
19872041
}
19882042
break;
19892043
default:
@@ -2173,6 +2227,8 @@ static void op_commit(struct xe_vm *vm,
21732227
if (op->subop == XE_VMA_SUBOP_MAP_RANGE) {
21742228
op->map_range.range->tile_present |= BIT(tile->id);
21752229
op->map_range.range->tile_invalidated &= ~BIT(tile->id);
2230+
} else if (op->subop == XE_VMA_SUBOP_UNMAP_RANGE) {
2231+
op->unmap_range.range->tile_present &= ~BIT(tile->id);
21762232
}
21772233
break;
21782234
}

drivers/gpu/drm/xe/xe_svm.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,14 @@ static void xe_svm_invalidate(struct drm_gpusvm *gpusvm,
222222
static int __xe_svm_garbage_collector(struct xe_vm *vm,
223223
struct xe_svm_range *range)
224224
{
225-
/* TODO: Do unbind */
225+
struct dma_fence *fence;
226+
227+
xe_vm_lock(vm, false);
228+
fence = xe_vm_range_unbind(vm, range);
229+
xe_vm_unlock(vm);
230+
if (IS_ERR(fence))
231+
return PTR_ERR(fence);
232+
dma_fence_put(fence);
226233

227234
drm_gpusvm_range_remove(&vm->svm.gpusvm, &range->base);
228235

drivers/gpu/drm/xe/xe_vm.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,89 @@ struct dma_fence *xe_vm_range_rebind(struct xe_vm *vm,
10401040
return fence;
10411041
}
10421042

1043+
static void xe_vm_populate_range_unbind(struct xe_vma_op *op,
1044+
struct xe_svm_range *range)
1045+
{
1046+
INIT_LIST_HEAD(&op->link);
1047+
op->tile_mask = range->tile_present;
1048+
op->base.op = DRM_GPUVA_OP_DRIVER;
1049+
op->subop = XE_VMA_SUBOP_UNMAP_RANGE;
1050+
op->unmap_range.range = range;
1051+
}
1052+
1053+
static int
1054+
xe_vm_ops_add_range_unbind(struct xe_vma_ops *vops,
1055+
struct xe_svm_range *range)
1056+
{
1057+
struct xe_vma_op *op;
1058+
1059+
op = kzalloc(sizeof(*op), GFP_KERNEL);
1060+
if (!op)
1061+
return -ENOMEM;
1062+
1063+
xe_vm_populate_range_unbind(op, range);
1064+
list_add_tail(&op->link, &vops->list);
1065+
xe_vma_ops_incr_pt_update_ops(vops, range->tile_present);
1066+
1067+
return 0;
1068+
}
1069+
1070+
/**
1071+
* xe_vm_range_unbind() - VM range unbind
1072+
* @vm: The VM which the range belongs to.
1073+
* @range: SVM range to rebind.
1074+
*
1075+
* Unbind SVM range removing the GPU page tables for the range.
1076+
*
1077+
* Return: dma fence for unbind to signal completion on succees, ERR_PTR on
1078+
* failure
1079+
*/
1080+
struct dma_fence *xe_vm_range_unbind(struct xe_vm *vm,
1081+
struct xe_svm_range *range)
1082+
{
1083+
struct dma_fence *fence = NULL;
1084+
struct xe_vma_ops vops;
1085+
struct xe_vma_op *op, *next_op;
1086+
struct xe_tile *tile;
1087+
u8 id;
1088+
int err;
1089+
1090+
lockdep_assert_held(&vm->lock);
1091+
xe_vm_assert_held(vm);
1092+
xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
1093+
1094+
if (!range->tile_present)
1095+
return dma_fence_get_stub();
1096+
1097+
xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
1098+
for_each_tile(tile, vm->xe, id) {
1099+
vops.pt_update_ops[id].wait_vm_bookkeep = true;
1100+
vops.pt_update_ops[tile->id].q =
1101+
xe_tile_migrate_exec_queue(tile);
1102+
}
1103+
1104+
err = xe_vm_ops_add_range_unbind(&vops, range);
1105+
if (err)
1106+
return ERR_PTR(err);
1107+
1108+
err = xe_vma_ops_alloc(&vops, false);
1109+
if (err) {
1110+
fence = ERR_PTR(err);
1111+
goto free_ops;
1112+
}
1113+
1114+
fence = ops_execute(vm, &vops);
1115+
1116+
free_ops:
1117+
list_for_each_entry_safe(op, next_op, &vops.list, link) {
1118+
list_del(&op->link);
1119+
kfree(op);
1120+
}
1121+
xe_vma_ops_fini(&vops);
1122+
1123+
return fence;
1124+
}
1125+
10431126
static void xe_vma_free(struct xe_vma *vma)
10441127
{
10451128
if (xe_vma_is_userptr(vma))

drivers/gpu/drm/xe/xe_vm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ struct dma_fence *xe_vm_range_rebind(struct xe_vm *vm,
223223
struct xe_vma *vma,
224224
struct xe_svm_range *range,
225225
u8 tile_mask);
226+
struct dma_fence *xe_vm_range_unbind(struct xe_vm *vm,
227+
struct xe_svm_range *range);
226228

227229
int xe_vm_invalidate_vma(struct xe_vma *vma);
228230

drivers/gpu/drm/xe/xe_vm_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ struct xe_vma_op_map_range {
366366
struct xe_svm_range *range;
367367
};
368368

369+
/** struct xe_vma_op_unmap_range - VMA unmap range operation */
370+
struct xe_vma_op_unmap_range {
371+
/** @range: SVM range to unmap */
372+
struct xe_svm_range *range;
373+
};
374+
369375
/** enum xe_vma_op_flags - flags for VMA operation */
370376
enum xe_vma_op_flags {
371377
/** @XE_VMA_OP_COMMITTED: VMA operation committed */
@@ -380,6 +386,8 @@ enum xe_vma_op_flags {
380386
enum xe_vma_subop {
381387
/** @XE_VMA_SUBOP_MAP_RANGE: Map range */
382388
XE_VMA_SUBOP_MAP_RANGE,
389+
/** @XE_VMA_SUBOP_UNMAP_RANGE: Unmap range */
390+
XE_VMA_SUBOP_UNMAP_RANGE,
383391
};
384392

385393
/** struct xe_vma_op - VMA operation */
@@ -404,6 +412,8 @@ struct xe_vma_op {
404412
struct xe_vma_op_prefetch prefetch;
405413
/** @map_range: VMA map range operation specific data */
406414
struct xe_vma_op_map_range map_range;
415+
/** @unmap_range: VMA unmap range operation specific data */
416+
struct xe_vma_op_unmap_range unmap_range;
407417
};
408418
};
409419

0 commit comments

Comments
 (0)