Commit 7477c4bd authored by Matthew Auld's avatar Matthew Auld
Browse files

drm/xe/pt: unify xe_pt_svm_pre_commit with userptr



We now use the same notifier lock for SVM and userptr, with that we can
combine xe_pt_userptr_pre_commit and xe_pt_svm_pre_commit.

v2: (Matt B)
  - Re-use xe_svm_notifier_lock/unlock for userptr.
  - Combine svm/userptr handling further down into op_check_svm_userptr.
v3:
  - Only hide the ops if we lack DRM_GPUSVM, since we also need them for
    userptr.

Suggested-by: default avatarMatthew Brost <matthew.brost@intel.com>
Signed-off-by: default avatarMatthew Auld <matthew.auld@intel.com>
Cc: Himal Prasad Ghimiray <himal.prasad.ghimiray@intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: default avatarMatthew Brost <matthew.brost@intel.com>
Link: https://lore.kernel.org/r/20250828142430.615826-18-matthew.auld@intel.com
parent 9e978741
Loading
Loading
Loading
Loading
+47 −76
Original line number Diff line number Diff line
@@ -1377,6 +1377,7 @@ static int xe_pt_pre_commit(struct xe_migrate_pt_update *pt_update)
				     pt_update_ops, rftree);
}

#if IS_ENABLED(CONFIG_DRM_GPUSVM)
#ifdef CONFIG_DRM_XE_USERPTR_INVAL_INJECT

static bool xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma)
@@ -1432,7 +1433,7 @@ static int vma_check_userptr(struct xe_vm *vm, struct xe_vma *vma,
	return 0;
}

static int op_check_userptr(struct xe_vm *vm, struct xe_vma_op *op,
static int op_check_svm_userptr(struct xe_vm *vm, struct xe_vma_op *op,
				struct xe_vm_pgtable_update_ops *pt_update)
{
	int err = 0;
@@ -1455,49 +1456,54 @@ static int op_check_userptr(struct xe_vm *vm, struct xe_vma_op *op,
	case DRM_GPUVA_OP_UNMAP:
		break;
	case DRM_GPUVA_OP_PREFETCH:
		err = vma_check_userptr(vm, gpuva_to_vma(op->base.prefetch.va),
					pt_update);
		break;
	default:
		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
	}
		if (xe_vma_is_cpu_addr_mirror(gpuva_to_vma(op->base.prefetch.va))) {
			struct xe_svm_range *range = op->map_range.range;
			unsigned long i;

	return err;
}
			xe_assert(vm->xe,
				  xe_vma_is_cpu_addr_mirror(gpuva_to_vma(op->base.prefetch.va)));
			xa_for_each(&op->prefetch_range.range, i, range) {
				xe_svm_range_debug(range, "PRE-COMMIT");

static int xe_pt_userptr_pre_commit(struct xe_migrate_pt_update *pt_update)
{
	struct xe_vm *vm = pt_update->vops->vm;
	struct xe_vma_ops *vops = pt_update->vops;
	struct xe_vm_pgtable_update_ops *pt_update_ops =
		&vops->pt_update_ops[pt_update->tile_id];
	struct xe_vma_op *op;
	int err;
				if (!xe_svm_range_pages_valid(range)) {
					xe_svm_range_debug(range, "PRE-COMMIT - RETRY");
					return -ENODATA;
				}
			}
		} else {
			err = vma_check_userptr(vm, gpuva_to_vma(op->base.prefetch.va), pt_update);
		}
		break;
#if IS_ENABLED(CONFIG_DRM_XE_GPUSVM)
	case DRM_GPUVA_OP_DRIVER:
		if (op->subop == XE_VMA_SUBOP_MAP_RANGE) {
			struct xe_svm_range *range = op->map_range.range;

	err = xe_pt_pre_commit(pt_update);
	if (err)
		return err;
			xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(op->map_range.vma));

	down_read(&vm->svm.gpusvm.notifier_lock);
			xe_svm_range_debug(range, "PRE-COMMIT");

	list_for_each_entry(op, &vops->list, link) {
		err = op_check_userptr(vm, op, pt_update_ops);
		if (err) {
			up_read(&vm->svm.gpusvm.notifier_lock);
			break;
			if (!xe_svm_range_pages_valid(range)) {
				xe_svm_range_debug(range, "PRE-COMMIT - RETRY");
				return -EAGAIN;
			}
		}
		break;
#endif
	default:
		drm_warn(&vm->xe->drm, "NOT POSSIBLE");
	}

	return err;
}

#if IS_ENABLED(CONFIG_DRM_XE_GPUSVM)
static int xe_pt_svm_pre_commit(struct xe_migrate_pt_update *pt_update)
static int xe_pt_svm_userptr_pre_commit(struct xe_migrate_pt_update *pt_update)
{
	struct xe_vm *vm = pt_update->vops->vm;
	struct xe_vma_ops *vops = pt_update->vops;
	struct xe_vm_pgtable_update_ops *pt_update_ops =
		&vops->pt_update_ops[pt_update->tile_id];
	struct xe_vma_op *op;
	unsigned long i;
	int err;

	err = xe_pt_pre_commit(pt_update);
@@ -1507,39 +1513,14 @@ static int xe_pt_svm_pre_commit(struct xe_migrate_pt_update *pt_update)
	xe_svm_notifier_lock(vm);

	list_for_each_entry(op, &vops->list, link) {
		struct xe_svm_range *range = NULL;

		if (op->subop == XE_VMA_SUBOP_UNMAP_RANGE)
			continue;

		if (op->base.op == DRM_GPUVA_OP_PREFETCH) {
			xe_assert(vm->xe,
				  xe_vma_is_cpu_addr_mirror(gpuva_to_vma(op->base.prefetch.va)));
			xa_for_each(&op->prefetch_range.range, i, range) {
				xe_svm_range_debug(range, "PRE-COMMIT");

				if (!xe_svm_range_pages_valid(range)) {
					xe_svm_range_debug(range, "PRE-COMMIT - RETRY");
					xe_svm_notifier_unlock(vm);
					return -ENODATA;
				}
			}
		} else {
			xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(op->map_range.vma));
			xe_assert(vm->xe, op->subop == XE_VMA_SUBOP_MAP_RANGE);
			range = op->map_range.range;

			xe_svm_range_debug(range, "PRE-COMMIT");

			if (!xe_svm_range_pages_valid(range)) {
				xe_svm_range_debug(range, "PRE-COMMIT - RETRY");
		err = op_check_svm_userptr(vm, op, pt_update_ops);
		if (err) {
			xe_svm_notifier_unlock(vm);
				return -EAGAIN;
			}
			break;
		}
	}

	return 0;
	return err;
}
#endif

@@ -1844,7 +1825,7 @@ static int bind_op_prepare(struct xe_vm *vm, struct xe_tile *tile,
						 xe_vma_start(vma),
						 xe_vma_end(vma));
		++pt_update_ops->current_op;
		pt_update_ops->needs_userptr_lock |= xe_vma_is_userptr(vma);
		pt_update_ops->needs_svm_lock |= xe_vma_is_userptr(vma);

		/*
		 * If rebind, we have to invalidate TLB on !LR vms to invalidate
@@ -1952,7 +1933,7 @@ static int unbind_op_prepare(struct xe_tile *tile,
	xe_pt_update_ops_rfence_interval(pt_update_ops, xe_vma_start(vma),
					 xe_vma_end(vma));
	++pt_update_ops->current_op;
	pt_update_ops->needs_userptr_lock |= xe_vma_is_userptr(vma);
	pt_update_ops->needs_svm_lock |= xe_vma_is_userptr(vma);
	pt_update_ops->needs_invalidation = true;

	xe_pt_commit_prepare_unbind(vma, pt_op->entries, pt_op->num_entries);
@@ -2339,20 +2320,14 @@ static const struct xe_migrate_pt_update_ops migrate_ops = {
	.pre_commit = xe_pt_pre_commit,
};

static const struct xe_migrate_pt_update_ops userptr_migrate_ops = {
	.populate = xe_vm_populate_pgtable,
	.clear = xe_migrate_clear_pgtable_callback,
	.pre_commit = xe_pt_userptr_pre_commit,
};

#if IS_ENABLED(CONFIG_DRM_XE_GPUSVM)
static const struct xe_migrate_pt_update_ops svm_migrate_ops = {
#if IS_ENABLED(CONFIG_DRM_GPUSVM)
static const struct xe_migrate_pt_update_ops svm_userptr_migrate_ops = {
	.populate = xe_vm_populate_pgtable,
	.clear = xe_migrate_clear_pgtable_callback,
	.pre_commit = xe_pt_svm_pre_commit,
	.pre_commit = xe_pt_svm_userptr_pre_commit,
};
#else
static const struct xe_migrate_pt_update_ops svm_migrate_ops;
static const struct xe_migrate_pt_update_ops svm_userptr_migrate_ops;
#endif

static struct xe_dep_scheduler *to_dep_scheduler(struct xe_exec_queue *q,
@@ -2390,9 +2365,7 @@ xe_pt_update_ops_run(struct xe_tile *tile, struct xe_vma_ops *vops)
	int err = 0, i;
	struct xe_migrate_pt_update update = {
		.ops = pt_update_ops->needs_svm_lock ?
			&svm_migrate_ops :
			pt_update_ops->needs_userptr_lock ?
			&userptr_migrate_ops :
			&svm_userptr_migrate_ops :
			&migrate_ops,
		.vops = vops,
		.tile_id = tile->id,
@@ -2534,8 +2507,6 @@ xe_pt_update_ops_run(struct xe_tile *tile, struct xe_vma_ops *vops)

	if (pt_update_ops->needs_svm_lock)
		xe_svm_notifier_unlock(vm);
	if (pt_update_ops->needs_userptr_lock)
		up_read(&vm->svm.gpusvm.notifier_lock);

	xe_tlb_inval_job_put(mjob);
	xe_tlb_inval_job_put(ijob);
+0 −2
Original line number Diff line number Diff line
@@ -105,8 +105,6 @@ struct xe_vm_pgtable_update_ops {
	u32 current_op;
	/** @needs_svm_lock: Needs SVM lock */
	bool needs_svm_lock;
	/** @needs_userptr_lock: Needs userptr lock */
	bool needs_userptr_lock;
	/** @needs_invalidation: Needs invalidation */
	bool needs_invalidation;
	/**