xref: /linux/drivers/gpu/drm/xe/xe_exec.c (revision 6dfafbd0299a60bfb5d5e277fdf100037c7ded07)
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], NULL, 0,
177 					  SYNC_PARSE_FLAG_EXEC |
178 					  (xe_vm_in_lr_mode(vm) ?
179 					   SYNC_PARSE_FLAG_LR_MODE : 0));
180 		if (err)
181 			goto err_syncs;
182 
183 		if (xe_sync_is_ufence(&syncs[num_syncs]))
184 			num_ufence++;
185 	}
186 
187 	if (XE_IOCTL_DBG(xe, num_ufence > 1)) {
188 		err = -EINVAL;
189 		goto err_syncs;
190 	}
191 
192 	if (xe_exec_queue_is_parallel(q)) {
193 		err = copy_from_user(addresses, addresses_user, sizeof(u64) *
194 				     q->width);
195 		if (err) {
196 			err = -EFAULT;
197 			goto err_syncs;
198 		}
199 	}
200 
201 	group = q->hwe->hw_engine_group;
202 	mode = xe_hw_engine_group_find_exec_mode(q);
203 
204 	if (mode == EXEC_MODE_DMA_FENCE) {
205 		err = xe_hw_engine_group_get_mode(group, mode, &previous_mode);
206 		if (err)
207 			goto err_syncs;
208 	}
209 
210 retry:
211 	if (!xe_vm_in_lr_mode(vm) && xe_vm_userptr_check_repin(vm)) {
212 		err = down_write_killable(&vm->lock);
213 		write_locked = true;
214 	} else {
215 		/* We don't allow execs while the VM is in error state */
216 		err = down_read_interruptible(&vm->lock);
217 		write_locked = false;
218 	}
219 	if (err)
220 		goto err_hw_exec_mode;
221 
222 	if (write_locked) {
223 		err = xe_vm_userptr_pin(vm);
224 		downgrade_write(&vm->lock);
225 		write_locked = false;
226 		if (err)
227 			goto err_unlock_list;
228 	}
229 
230 	if (!args->num_batch_buffer) {
231 		err = xe_vm_lock(vm, true);
232 		if (err)
233 			goto err_unlock_list;
234 
235 		if (!xe_vm_in_lr_mode(vm)) {
236 			struct dma_fence *fence;
237 
238 			fence = xe_sync_in_fence_get(syncs, num_syncs, q, vm);
239 			if (IS_ERR(fence)) {
240 				err = PTR_ERR(fence);
241 				xe_vm_unlock(vm);
242 				goto err_unlock_list;
243 			}
244 			for (i = 0; i < num_syncs; i++)
245 				xe_sync_entry_signal(&syncs[i], fence);
246 			xe_exec_queue_last_fence_set(q, vm, fence);
247 			dma_fence_put(fence);
248 		}
249 
250 		xe_vm_unlock(vm);
251 		goto err_unlock_list;
252 	}
253 
254 	/*
255 	 * It's OK to block interruptible here with the vm lock held, since
256 	 * on task freezing during suspend / hibernate, the call will
257 	 * return -ERESTARTSYS and the IOCTL will be rerun.
258 	 */
259 	err = xe_pm_block_on_suspend(xe);
260 	if (err)
261 		goto err_unlock_list;
262 
263 	if (!xe_vm_in_lr_mode(vm)) {
264 		vm_exec.vm = &vm->gpuvm;
265 		vm_exec.flags = DRM_EXEC_INTERRUPTIBLE_WAIT;
266 		err = xe_validation_exec_lock(&ctx, &vm_exec, &xe->val);
267 		if (err)
268 			goto err_unlock_list;
269 	}
270 
271 	if (xe_vm_is_closed_or_banned(q->vm)) {
272 		drm_warn(&xe->drm, "Trying to schedule after vm is closed or banned\n");
273 		err = -ECANCELED;
274 		goto err_exec;
275 	}
276 
277 	if (xe_exec_queue_uses_pxp(q)) {
278 		err = xe_vm_validate_protected(q->vm);
279 		if (err)
280 			goto err_exec;
281 	}
282 
283 	job = xe_sched_job_create(q, xe_exec_queue_is_parallel(q) ?
284 				  addresses : &args->address);
285 	if (IS_ERR(job)) {
286 		err = PTR_ERR(job);
287 		goto err_exec;
288 	}
289 
290 	/* Wait behind rebinds */
291 	if (!xe_vm_in_lr_mode(vm)) {
292 		err = xe_sched_job_add_deps(job,
293 					    xe_vm_resv(vm),
294 					    DMA_RESV_USAGE_KERNEL);
295 		if (err)
296 			goto err_put_job;
297 	}
298 
299 	for (i = 0; i < num_syncs && !err; i++)
300 		err = xe_sync_entry_add_deps(&syncs[i], job);
301 	if (err)
302 		goto err_put_job;
303 
304 	if (!xe_vm_in_lr_mode(vm)) {
305 		err = xe_svm_notifier_lock_interruptible(vm);
306 		if (err)
307 			goto err_put_job;
308 
309 		err = __xe_vm_userptr_needs_repin(vm);
310 		if (err)
311 			goto err_repin;
312 	}
313 
314 	/*
315 	 * Point of no return, if we error after this point just set an error on
316 	 * the job and let the DRM scheduler / backend clean up the job.
317 	 */
318 	xe_sched_job_arm(job);
319 	if (!xe_vm_in_lr_mode(vm))
320 		drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, &job->drm.s_fence->finished,
321 					 DMA_RESV_USAGE_BOOKKEEP,
322 					 DMA_RESV_USAGE_BOOKKEEP);
323 
324 	for (i = 0; i < num_syncs; i++) {
325 		xe_sync_entry_signal(&syncs[i], &job->drm.s_fence->finished);
326 		xe_sched_job_init_user_fence(job, &syncs[i]);
327 	}
328 
329 	if (!xe_vm_in_lr_mode(vm))
330 		xe_exec_queue_last_fence_set(q, vm, &job->drm.s_fence->finished);
331 	xe_sched_job_push(job);
332 	xe_vm_reactivate_rebind(vm);
333 
334 	if (!err && !xe_vm_in_lr_mode(vm)) {
335 		spin_lock(&xe->ttm.lru_lock);
336 		ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
337 		spin_unlock(&xe->ttm.lru_lock);
338 	}
339 
340 	if (mode == EXEC_MODE_LR)
341 		xe_hw_engine_group_resume_faulting_lr_jobs(group);
342 
343 err_repin:
344 	if (!xe_vm_in_lr_mode(vm))
345 		xe_svm_notifier_unlock(vm);
346 err_put_job:
347 	if (err)
348 		xe_sched_job_put(job);
349 err_exec:
350 	if (!xe_vm_in_lr_mode(vm))
351 		xe_validation_ctx_fini(&ctx);
352 err_unlock_list:
353 	up_read(&vm->lock);
354 	if (err == -EAGAIN)
355 		goto retry;
356 err_hw_exec_mode:
357 	if (mode == EXEC_MODE_DMA_FENCE)
358 		xe_hw_engine_group_put(group);
359 err_syncs:
360 	while (num_syncs--)
361 		xe_sync_entry_cleanup(&syncs[num_syncs]);
362 	kfree(syncs);
363 err_exec_queue:
364 	xe_exec_queue_put(q);
365 
366 	return err;
367 }
368