xref: /linux/drivers/gpu/drm/xe/xe_exec.c (revision 47c3ea3359d14ffa4ff94511ae905978d86bb5dd)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_exec.h"
7 
8 #include <drm/drm_device.h>
9 #include <drm/drm_exec.h>
10 #include <drm/drm_file.h>
11 #include <uapi/drm/xe_drm.h>
12 #include <linux/delay.h>
13 
14 #include "xe_bo.h"
15 #include "xe_device.h"
16 #include "xe_exec_queue.h"
17 #include "xe_hw_engine_group.h"
18 #include "xe_macros.h"
19 #include "xe_pm.h"
20 #include "xe_ring_ops_types.h"
21 #include "xe_sched_job.h"
22 #include "xe_sync.h"
23 #include "xe_svm.h"
24 #include "xe_trace.h"
25 #include "xe_vm.h"
26 
27 /**
28  * DOC: Execbuf (User GPU command submission)
29  *
30  * Execs have historically been rather complicated in DRM drivers (at least in
31  * the i915) because a few things:
32  *
33  * - Passing in a list BO which are read / written to creating implicit syncs
34  * - Binding at exec time
35  * - Flow controlling the ring at exec time
36  *
37  * In Xe we avoid all of this complication by not allowing a BO list to be
38  * passed into an exec, using the dma-buf implicit sync uAPI, have binds as
39  * separate operations, and using the DRM scheduler to flow control the ring.
40  * Let's deep dive on each of these.
41  *
42  * We can get away from a BO list by forcing the user to use in / out fences on
43  * every exec rather than the kernel tracking dependencies of BO (e.g. if the
44  * user knows an exec writes to a BO and reads from the BO in the next exec, it
45  * is the user's responsibility to pass in / out fence between the two execs).
46  *
47  * We do not allow a user to trigger a bind at exec time rather we have a VM
48  * bind IOCTL which uses the same in / out fence interface as exec. In that
49  * sense, a VM bind is basically the same operation as an exec from the user
50  * perspective. e.g. If an exec depends on a VM bind use the in / out fence
51  * interface (struct drm_xe_sync) to synchronize like syncing between two
52  * dependent execs.
53  *
54  * Although a user cannot trigger a bind, we still have to rebind userptrs in
55  * the VM that have been invalidated since the last exec, likewise we also have
56  * to rebind BOs that have been evicted by the kernel. We schedule these rebinds
57  * behind any pending kernel operations on any external BOs in VM or any BOs
58  * private to the VM. This is accomplished by the rebinds waiting on BOs
59  * DMA_RESV_USAGE_KERNEL slot (kernel ops) and kernel ops waiting on all BOs
60  * slots (inflight execs are in the DMA_RESV_USAGE_BOOKKEEP for private BOs and
61  * for external BOs).
62  *
63  * Rebinds / dma-resv usage applies to non-compute mode VMs only as for compute
64  * mode VMs we use preempt fences and a rebind worker (TODO: add link).
65  *
66  * There is no need to flow control the ring in the exec as we write the ring at
67  * submission time and set the DRM scheduler max job limit SIZE_OF_RING /
68  * MAX_JOB_SIZE. The DRM scheduler will then hold all jobs until space in the
69  * ring is available.
70  *
71  * All of this results in a rather simple exec implementation.
72  *
73  * Flow
74  * ~~~~
75  *
76  * .. code-block::
77  *
78  *	Parse input arguments
79  *	Wait for any async VM bind passed as in-fences to start
80  *	<----------------------------------------------------------------------|
81  *	Lock global VM lock in read mode                                       |
82  *	Pin userptrs (also finds userptr invalidated since last exec)          |
83  *	Lock exec (VM dma-resv lock, external BOs dma-resv locks)              |
84  *	Validate BOs that have been evicted                                    |
85  *	Create job                                                             |
86  *	Rebind invalidated userptrs + evicted BOs (non-compute-mode)           |
87  *	Add rebind fence dependency to job                                     |
88  *	Add job VM dma-resv bookkeeping slot (non-compute mode)                |
89  *	Add job to external BOs dma-resv write slots (non-compute mode)        |
90  *	Check if any userptrs invalidated since pin ------ Drop locks ---------|
91  *	Install in / out fences for job
92  *	Submit job
93  *	Unlock all
94  */
95 
96 /*
97  * Add validation and rebinding to the drm_exec locking loop, since both can
98  * trigger eviction which may require sleeping dma_resv locks.
99  */
100 static int xe_exec_fn(struct drm_gpuvm_exec *vm_exec)
101 {
102 	struct xe_vm *vm = container_of(vm_exec->vm, struct xe_vm, gpuvm);
103 	int ret;
104 
105 	/* The fence slot added here is intended for the exec sched job. */
106 	xe_vm_set_validation_exec(vm, &vm_exec->exec);
107 	ret = xe_vm_validate_rebind(vm, &vm_exec->exec, 1);
108 	xe_vm_set_validation_exec(vm, NULL);
109 	return ret;
110 }
111 
112 int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
113 {
114 	struct xe_device *xe = to_xe_device(dev);
115 	struct xe_file *xef = to_xe_file(file);
116 	struct drm_xe_exec *args = data;
117 	struct drm_xe_sync __user *syncs_user = u64_to_user_ptr(args->syncs);
118 	u64 __user *addresses_user = u64_to_user_ptr(args->address);
119 	struct xe_exec_queue *q;
120 	struct xe_sync_entry *syncs = NULL;
121 	u64 addresses[XE_HW_ENGINE_MAX_INSTANCE];
122 	struct drm_gpuvm_exec vm_exec = {.extra.fn = xe_exec_fn};
123 	struct drm_exec *exec = &vm_exec.exec;
124 	u32 i, num_syncs, num_ufence = 0;
125 	struct xe_validation_ctx ctx;
126 	struct xe_sched_job *job;
127 	struct xe_vm *vm;
128 	bool write_locked;
129 	int err = 0;
130 	struct xe_hw_engine_group *group;
131 	enum xe_hw_engine_group_execution_mode mode, previous_mode;
132 
133 	if (XE_IOCTL_DBG(xe, args->extensions) ||
134 	    XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) ||
135 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
136 		return -EINVAL;
137 
138 	q = xe_exec_queue_lookup(xef, args->exec_queue_id);
139 	if (XE_IOCTL_DBG(xe, !q))
140 		return -ENOENT;
141 
142 	if (XE_IOCTL_DBG(xe, q->flags & EXEC_QUEUE_FLAG_VM)) {
143 		err = -EINVAL;
144 		goto err_exec_queue;
145 	}
146 
147 	if (XE_IOCTL_DBG(xe, args->num_batch_buffer &&
148 			 q->width != args->num_batch_buffer)) {
149 		err = -EINVAL;
150 		goto err_exec_queue;
151 	}
152 
153 	if (XE_IOCTL_DBG(xe, q->ops->reset_status(q))) {
154 		err = -ECANCELED;
155 		goto err_exec_queue;
156 	}
157 
158 	if (atomic_read(&q->job_cnt) >= XE_MAX_JOB_COUNT_PER_EXEC_QUEUE) {
159 		trace_xe_exec_queue_reach_max_job_count(q, XE_MAX_JOB_COUNT_PER_EXEC_QUEUE);
160 		err = -EAGAIN;
161 		goto err_exec_queue;
162 	}
163 
164 	if (args->num_syncs) {
165 		syncs = kcalloc(args->num_syncs, sizeof(*syncs), GFP_KERNEL);
166 		if (!syncs) {
167 			err = -ENOMEM;
168 			goto err_exec_queue;
169 		}
170 	}
171 
172 	vm = q->vm;
173 
174 	for (num_syncs = 0; num_syncs < args->num_syncs; num_syncs++) {
175 		err = xe_sync_entry_parse(xe, xef, &syncs[num_syncs],
176 					  &syncs_user[num_syncs], SYNC_PARSE_FLAG_EXEC |
177 					  (xe_vm_in_lr_mode(vm) ?
178 					   SYNC_PARSE_FLAG_LR_MODE : 0));
179 		if (err)
180 			goto err_syncs;
181 
182 		if (xe_sync_is_ufence(&syncs[num_syncs]))
183 			num_ufence++;
184 	}
185 
186 	if (XE_IOCTL_DBG(xe, num_ufence > 1)) {
187 		err = -EINVAL;
188 		goto err_syncs;
189 	}
190 
191 	if (xe_exec_queue_is_parallel(q)) {
192 		err = copy_from_user(addresses, addresses_user, sizeof(u64) *
193 				     q->width);
194 		if (err) {
195 			err = -EFAULT;
196 			goto err_syncs;
197 		}
198 	}
199 
200 	group = q->hwe->hw_engine_group;
201 	mode = xe_hw_engine_group_find_exec_mode(q);
202 
203 	if (mode == EXEC_MODE_DMA_FENCE) {
204 		err = xe_hw_engine_group_get_mode(group, mode, &previous_mode);
205 		if (err)
206 			goto err_syncs;
207 	}
208 
209 retry:
210 	if (!xe_vm_in_lr_mode(vm) && xe_vm_userptr_check_repin(vm)) {
211 		err = down_write_killable(&vm->lock);
212 		write_locked = true;
213 	} else {
214 		/* We don't allow execs while the VM is in error state */
215 		err = down_read_interruptible(&vm->lock);
216 		write_locked = false;
217 	}
218 	if (err)
219 		goto err_hw_exec_mode;
220 
221 	if (write_locked) {
222 		err = xe_vm_userptr_pin(vm);
223 		downgrade_write(&vm->lock);
224 		write_locked = false;
225 		if (err)
226 			goto err_unlock_list;
227 	}
228 
229 	if (!args->num_batch_buffer) {
230 		err = xe_vm_lock(vm, true);
231 		if (err)
232 			goto err_unlock_list;
233 
234 		if (!xe_vm_in_lr_mode(vm)) {
235 			struct dma_fence *fence;
236 
237 			fence = xe_sync_in_fence_get(syncs, num_syncs, q, vm);
238 			if (IS_ERR(fence)) {
239 				err = PTR_ERR(fence);
240 				xe_vm_unlock(vm);
241 				goto err_unlock_list;
242 			}
243 			for (i = 0; i < num_syncs; i++)
244 				xe_sync_entry_signal(&syncs[i], fence);
245 			xe_exec_queue_last_fence_set(q, vm, fence);
246 			dma_fence_put(fence);
247 		}
248 
249 		xe_vm_unlock(vm);
250 		goto err_unlock_list;
251 	}
252 
253 	/*
254 	 * It's OK to block interruptible here with the vm lock held, since
255 	 * on task freezing during suspend / hibernate, the call will
256 	 * return -ERESTARTSYS and the IOCTL will be rerun.
257 	 */
258 	err = xe_pm_block_on_suspend(xe);
259 	if (err)
260 		goto err_unlock_list;
261 
262 	if (!xe_vm_in_lr_mode(vm)) {
263 		vm_exec.vm = &vm->gpuvm;
264 		vm_exec.flags = DRM_EXEC_INTERRUPTIBLE_WAIT;
265 		err = xe_validation_exec_lock(&ctx, &vm_exec, &xe->val);
266 		if (err)
267 			goto err_unlock_list;
268 	}
269 
270 	if (xe_vm_is_closed_or_banned(q->vm)) {
271 		drm_warn(&xe->drm, "Trying to schedule after vm is closed or banned\n");
272 		err = -ECANCELED;
273 		goto err_exec;
274 	}
275 
276 	if (xe_exec_queue_uses_pxp(q)) {
277 		err = xe_vm_validate_protected(q->vm);
278 		if (err)
279 			goto err_exec;
280 	}
281 
282 	job = xe_sched_job_create(q, xe_exec_queue_is_parallel(q) ?
283 				  addresses : &args->address);
284 	if (IS_ERR(job)) {
285 		err = PTR_ERR(job);
286 		goto err_exec;
287 	}
288 
289 	/* Wait behind rebinds */
290 	if (!xe_vm_in_lr_mode(vm)) {
291 		err = xe_sched_job_add_deps(job,
292 					    xe_vm_resv(vm),
293 					    DMA_RESV_USAGE_KERNEL);
294 		if (err)
295 			goto err_put_job;
296 	}
297 
298 	for (i = 0; i < num_syncs && !err; i++)
299 		err = xe_sync_entry_add_deps(&syncs[i], job);
300 	if (err)
301 		goto err_put_job;
302 
303 	if (!xe_vm_in_lr_mode(vm)) {
304 		err = xe_sched_job_last_fence_add_dep(job, vm);
305 		if (err)
306 			goto err_put_job;
307 
308 		err = xe_svm_notifier_lock_interruptible(vm);
309 		if (err)
310 			goto err_put_job;
311 
312 		err = __xe_vm_userptr_needs_repin(vm);
313 		if (err)
314 			goto err_repin;
315 	}
316 
317 	/*
318 	 * Point of no return, if we error after this point just set an error on
319 	 * the job and let the DRM scheduler / backend clean up the job.
320 	 */
321 	xe_sched_job_arm(job);
322 	if (!xe_vm_in_lr_mode(vm))
323 		drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, &job->drm.s_fence->finished,
324 					 DMA_RESV_USAGE_BOOKKEEP,
325 					 DMA_RESV_USAGE_BOOKKEEP);
326 
327 	for (i = 0; i < num_syncs; i++) {
328 		xe_sync_entry_signal(&syncs[i], &job->drm.s_fence->finished);
329 		xe_sched_job_init_user_fence(job, &syncs[i]);
330 	}
331 
332 	if (!xe_vm_in_lr_mode(vm))
333 		xe_exec_queue_last_fence_set(q, vm, &job->drm.s_fence->finished);
334 	xe_sched_job_push(job);
335 	xe_vm_reactivate_rebind(vm);
336 
337 	if (!err && !xe_vm_in_lr_mode(vm)) {
338 		spin_lock(&xe->ttm.lru_lock);
339 		ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
340 		spin_unlock(&xe->ttm.lru_lock);
341 	}
342 
343 	if (mode == EXEC_MODE_LR)
344 		xe_hw_engine_group_resume_faulting_lr_jobs(group);
345 
346 err_repin:
347 	if (!xe_vm_in_lr_mode(vm))
348 		xe_svm_notifier_unlock(vm);
349 err_put_job:
350 	if (err)
351 		xe_sched_job_put(job);
352 err_exec:
353 	if (!xe_vm_in_lr_mode(vm))
354 		xe_validation_ctx_fini(&ctx);
355 err_unlock_list:
356 	up_read(&vm->lock);
357 	if (err == -EAGAIN)
358 		goto retry;
359 err_hw_exec_mode:
360 	if (mode == EXEC_MODE_DMA_FENCE)
361 		xe_hw_engine_group_put(group);
362 err_syncs:
363 	while (num_syncs--)
364 		xe_sync_entry_cleanup(&syncs[num_syncs]);
365 	kfree(syncs);
366 err_exec_queue:
367 	xe_exec_queue_put(q);
368 
369 	return err;
370 }
371