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