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