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