1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2021 Intel Corporation
4 */
5
6 #include "xe_vm.h"
7
8 #include <linux/dma-fence-array.h>
9 #include <linux/nospec.h>
10
11 #include <drm/drm_drv.h>
12 #include <drm/drm_exec.h>
13 #include <drm/drm_print.h>
14 #include <drm/ttm/ttm_tt.h>
15 #include <uapi/drm/xe_drm.h>
16 #include <linux/ascii85.h>
17 #include <linux/delay.h>
18 #include <linux/kthread.h>
19 #include <linux/mm.h>
20 #include <linux/swap.h>
21
22 #include <generated/xe_wa_oob.h>
23
24 #include "regs/xe_gtt_defs.h"
25 #include "xe_assert.h"
26 #include "xe_bo.h"
27 #include "xe_device.h"
28 #include "xe_drm_client.h"
29 #include "xe_exec_queue.h"
30 #include "xe_gt.h"
31 #include "xe_migrate.h"
32 #include "xe_pat.h"
33 #include "xe_pm.h"
34 #include "xe_preempt_fence.h"
35 #include "xe_pt.h"
36 #include "xe_pxp.h"
37 #include "xe_sriov_vf.h"
38 #include "xe_svm.h"
39 #include "xe_sync.h"
40 #include "xe_tile.h"
41 #include "xe_tlb_inval.h"
42 #include "xe_trace_bo.h"
43 #include "xe_vm_madvise.h"
44 #include "xe_wa.h"
45
xe_vm_obj(struct xe_vm * vm)46 static struct drm_gem_object *xe_vm_obj(struct xe_vm *vm)
47 {
48 return vm->gpuvm.r_obj;
49 }
50
51 /**
52 * xe_vm_drm_exec_lock() - Lock the vm's resv with a drm_exec transaction
53 * @vm: The vm whose resv is to be locked.
54 * @exec: The drm_exec transaction.
55 *
56 * Helper to lock the vm's resv as part of a drm_exec transaction.
57 *
58 * Return: %0 on success. See drm_exec_lock_obj() for error codes.
59 */
xe_vm_drm_exec_lock(struct xe_vm * vm,struct drm_exec * exec)60 int xe_vm_drm_exec_lock(struct xe_vm *vm, struct drm_exec *exec)
61 {
62 return drm_exec_lock_obj(exec, xe_vm_obj(vm));
63 }
64
preempt_fences_waiting(struct xe_vm * vm)65 static bool preempt_fences_waiting(struct xe_vm *vm)
66 {
67 struct xe_exec_queue *q;
68
69 lockdep_assert_held(&vm->lock);
70 xe_vm_assert_held(vm);
71
72 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
73 if (!q->lr.pfence ||
74 test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
75 &q->lr.pfence->flags)) {
76 return true;
77 }
78 }
79
80 return false;
81 }
82
free_preempt_fences(struct list_head * list)83 static void free_preempt_fences(struct list_head *list)
84 {
85 struct list_head *link, *next;
86
87 list_for_each_safe(link, next, list)
88 xe_preempt_fence_free(to_preempt_fence_from_link(link));
89 }
90
alloc_preempt_fences(struct xe_vm * vm,struct list_head * list,unsigned int * count)91 static int alloc_preempt_fences(struct xe_vm *vm, struct list_head *list,
92 unsigned int *count)
93 {
94 lockdep_assert_held(&vm->lock);
95 xe_vm_assert_held(vm);
96
97 if (*count >= vm->preempt.num_exec_queues)
98 return 0;
99
100 for (; *count < vm->preempt.num_exec_queues; ++(*count)) {
101 struct xe_preempt_fence *pfence = xe_preempt_fence_alloc();
102
103 if (IS_ERR(pfence))
104 return PTR_ERR(pfence);
105
106 list_move_tail(xe_preempt_fence_link(pfence), list);
107 }
108
109 return 0;
110 }
111
wait_for_existing_preempt_fences(struct xe_vm * vm)112 static int wait_for_existing_preempt_fences(struct xe_vm *vm)
113 {
114 struct xe_exec_queue *q;
115 bool vf_migration = IS_SRIOV_VF(vm->xe) &&
116 xe_sriov_vf_migration_supported(vm->xe);
117 signed long wait_time = vf_migration ? HZ / 5 : MAX_SCHEDULE_TIMEOUT;
118
119 xe_vm_assert_held(vm);
120
121 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
122 if (q->lr.pfence) {
123 long timeout;
124
125 timeout = dma_fence_wait_timeout(q->lr.pfence, false,
126 wait_time);
127 if (!timeout) {
128 xe_assert(vm->xe, vf_migration);
129 return -EAGAIN;
130 }
131
132 /* Only -ETIME on fence indicates VM needs to be killed */
133 if (timeout < 0 || q->lr.pfence->error == -ETIME)
134 return -ETIME;
135
136 dma_fence_put(q->lr.pfence);
137 q->lr.pfence = NULL;
138 }
139 }
140
141 return 0;
142 }
143
xe_vm_is_idle(struct xe_vm * vm)144 static bool xe_vm_is_idle(struct xe_vm *vm)
145 {
146 struct xe_exec_queue *q;
147
148 xe_vm_assert_held(vm);
149 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
150 if (!xe_exec_queue_is_idle(q))
151 return false;
152 }
153
154 return true;
155 }
156
arm_preempt_fences(struct xe_vm * vm,struct list_head * list)157 static void arm_preempt_fences(struct xe_vm *vm, struct list_head *list)
158 {
159 struct list_head *link;
160 struct xe_exec_queue *q;
161
162 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
163 struct dma_fence *fence;
164
165 link = list->next;
166 xe_assert(vm->xe, link != list);
167
168 fence = xe_preempt_fence_arm(to_preempt_fence_from_link(link),
169 q, q->lr.context,
170 ++q->lr.seqno);
171 dma_fence_put(q->lr.pfence);
172 q->lr.pfence = fence;
173 }
174 }
175
add_preempt_fences(struct xe_vm * vm,struct xe_bo * bo)176 static int add_preempt_fences(struct xe_vm *vm, struct xe_bo *bo)
177 {
178 struct xe_exec_queue *q;
179 int err;
180
181 xe_bo_assert_held(bo);
182
183 if (!vm->preempt.num_exec_queues)
184 return 0;
185
186 err = dma_resv_reserve_fences(bo->ttm.base.resv, vm->preempt.num_exec_queues);
187 if (err)
188 return err;
189
190 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link)
191 if (q->lr.pfence) {
192 dma_resv_add_fence(bo->ttm.base.resv,
193 q->lr.pfence,
194 DMA_RESV_USAGE_BOOKKEEP);
195 }
196
197 return 0;
198 }
199
resume_and_reinstall_preempt_fences(struct xe_vm * vm,struct drm_exec * exec)200 static void resume_and_reinstall_preempt_fences(struct xe_vm *vm,
201 struct drm_exec *exec)
202 {
203 struct xe_exec_queue *q;
204
205 lockdep_assert_held(&vm->lock);
206 xe_vm_assert_held(vm);
207
208 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
209 /*
210 * Only resume queues whose suspend() actually succeeded. A
211 * failed suspend() (e.g. killed/banned/wedged) leaves the queue
212 * un-suspended, so it must not be resumed.
213 *
214 * Also skip queues that have since been reset/killed/banned/
215 * wedged: their suspend may not have completed (suspend_pending
216 * can still be set, e.g. a preempt fence signalled with -ENOENT
217 * without waiting), so resuming would trip the !suspend_pending
218 * assert in the backend. Such queues are being torn down anyway,
219 * so leave them marked suspended and let teardown resolve their
220 * state.
221 */
222 if (READ_ONCE(q->lr.suspended) && !q->ops->reset_status(q)) {
223 WRITE_ONCE(q->lr.suspended, false);
224 q->ops->resume(q);
225 }
226
227 drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, q->lr.pfence,
228 DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
229 }
230 }
231
xe_vm_add_compute_exec_queue(struct xe_vm * vm,struct xe_exec_queue * q)232 int xe_vm_add_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
233 {
234 struct drm_gpuvm_exec vm_exec = {
235 .vm = &vm->gpuvm,
236 .flags = DRM_EXEC_INTERRUPTIBLE_WAIT,
237 .num_fences = 1,
238 };
239 struct drm_exec *exec = &vm_exec.exec;
240 struct xe_validation_ctx ctx;
241 struct dma_fence *pfence;
242 int err;
243 bool wait;
244
245 xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm));
246
247 down_write(&vm->lock);
248 err = xe_validation_exec_lock(&ctx, &vm_exec, &vm->xe->val);
249 if (err)
250 goto out_up_write;
251
252 pfence = xe_preempt_fence_create(q, q->lr.context,
253 ++q->lr.seqno);
254 if (IS_ERR(pfence)) {
255 err = PTR_ERR(pfence);
256 goto out_fini;
257 }
258
259 list_add(&q->lr.link, &vm->preempt.exec_queues);
260 ++vm->preempt.num_exec_queues;
261 q->lr.pfence = pfence;
262
263 xe_svm_notifier_lock(vm);
264
265 drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, pfence,
266 DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
267
268 /*
269 * Check to see if a preemption on VM is in flight or userptr
270 * invalidation, if so trigger this preempt fence to sync state with
271 * other preempt fences on the VM.
272 */
273 wait = __xe_vm_userptr_needs_repin(vm) || preempt_fences_waiting(vm);
274 if (wait)
275 dma_fence_enable_signaling(pfence);
276
277 xe_svm_notifier_unlock(vm);
278
279 out_fini:
280 xe_validation_ctx_fini(&ctx);
281 out_up_write:
282 up_write(&vm->lock);
283
284 return err;
285 }
286 ALLOW_ERROR_INJECTION(xe_vm_add_compute_exec_queue, ERRNO);
287
288 /**
289 * xe_vm_remove_compute_exec_queue() - Remove compute exec queue from VM
290 * @vm: The VM.
291 * @q: The exec_queue
292 *
293 * Note that this function might be called multiple times on the same queue.
294 */
xe_vm_remove_compute_exec_queue(struct xe_vm * vm,struct xe_exec_queue * q)295 void xe_vm_remove_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
296 {
297 if (!xe_vm_in_preempt_fence_mode(vm))
298 return;
299
300 down_write(&vm->lock);
301 if (!list_empty(&q->lr.link)) {
302 list_del_init(&q->lr.link);
303 --vm->preempt.num_exec_queues;
304 }
305 if (q->lr.pfence) {
306 dma_fence_enable_signaling(q->lr.pfence);
307 dma_fence_put(q->lr.pfence);
308 q->lr.pfence = NULL;
309 }
310 up_write(&vm->lock);
311 }
312
313 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
314
315 /**
316 * xe_vm_kill() - VM Kill
317 * @vm: The VM.
318 * @unlocked: Flag indicates the VM's dma-resv is not held
319 *
320 * Kill the VM by setting banned flag indicated VM is no longer available for
321 * use. If in preempt fence mode, also kill all exec queue attached to the VM.
322 */
xe_vm_kill(struct xe_vm * vm,bool unlocked)323 void xe_vm_kill(struct xe_vm *vm, bool unlocked)
324 {
325 struct xe_exec_queue *q;
326
327 lockdep_assert_held(&vm->lock);
328
329 if (unlocked)
330 xe_vm_lock(vm, false);
331
332 vm->flags |= XE_VM_FLAG_BANNED;
333 trace_xe_vm_kill(vm);
334
335 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link)
336 q->ops->kill(q);
337
338 if (unlocked)
339 xe_vm_unlock(vm);
340
341 /* TODO: Inform user the VM is banned */
342 }
343
xe_gpuvm_validate(struct drm_gpuvm_bo * vm_bo,struct drm_exec * exec)344 static int xe_gpuvm_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec)
345 {
346 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
347 struct xe_bo *bo = gem_to_xe_bo(vm_bo->obj);
348 struct drm_gpuva *gpuva;
349 int ret;
350
351 lockdep_assert_held(&vm->lock);
352 drm_gpuvm_bo_for_each_va(gpuva, vm_bo)
353 list_move_tail(&gpuva_to_vma(gpuva)->combined_links.rebind,
354 &vm->rebind_list);
355
356 /* Skip re-populating purged BOs, rebind maps scratch pages. */
357 if (xe_bo_is_purged(bo)) {
358 vm_bo->evicted = false;
359 return 0;
360 }
361
362 if (!try_wait_for_completion(&vm->xe->pm_block))
363 return -EAGAIN;
364
365 ret = xe_bo_validate(bo, vm, false, exec);
366 if (ret)
367 return ret;
368
369 vm_bo->evicted = false;
370 return 0;
371 }
372
373 /**
374 * xe_vm_validate_rebind() - Validate buffer objects and rebind vmas
375 * @vm: The vm for which we are rebinding.
376 * @exec: The struct drm_exec with the locked GEM objects.
377 * @num_fences: The number of fences to reserve for the operation, not
378 * including rebinds and validations.
379 *
380 * Validates all evicted gem objects and rebinds their vmas. Note that
381 * rebindings may cause evictions and hence the validation-rebind
382 * sequence is rerun until there are no more objects to validate.
383 *
384 * Return: 0 on success, negative error code on error. In particular,
385 * may return -EINTR or -ERESTARTSYS if interrupted, and -EDEADLK if
386 * the drm_exec transaction needs to be restarted.
387 */
xe_vm_validate_rebind(struct xe_vm * vm,struct drm_exec * exec,unsigned int num_fences)388 int xe_vm_validate_rebind(struct xe_vm *vm, struct drm_exec *exec,
389 unsigned int num_fences)
390 {
391 struct drm_gem_object *obj;
392 int ret;
393
394 do {
395 ret = drm_gpuvm_validate(&vm->gpuvm, exec);
396 if (ret)
397 return ret;
398
399 ret = xe_vm_rebind(vm, false);
400 if (ret)
401 return ret;
402 } while (!list_empty(&vm->gpuvm.evict.list));
403
404 drm_exec_for_each_locked_object(exec, obj) {
405 ret = dma_resv_reserve_fences(obj->resv, num_fences);
406 if (ret)
407 return ret;
408 }
409
410 return 0;
411 }
412
xe_preempt_work_begin(struct drm_exec * exec,struct xe_vm * vm,bool * done)413 static int xe_preempt_work_begin(struct drm_exec *exec, struct xe_vm *vm,
414 bool *done)
415 {
416 int err;
417
418 err = drm_gpuvm_prepare_vm(&vm->gpuvm, exec, 0);
419 if (err)
420 return err;
421
422 if (xe_vm_is_idle(vm)) {
423 vm->preempt.rebind_deactivated = true;
424 *done = true;
425 return 0;
426 }
427
428 if (!preempt_fences_waiting(vm)) {
429 *done = true;
430 return 0;
431 }
432
433 err = drm_gpuvm_prepare_objects(&vm->gpuvm, exec, 0);
434 if (err)
435 return err;
436
437 err = wait_for_existing_preempt_fences(vm);
438 if (err)
439 return err;
440
441 /*
442 * Add validation and rebinding to the locking loop since both can
443 * cause evictions which may require blocing dma_resv locks.
444 * The fence reservation here is intended for the new preempt fences
445 * we attach at the end of the rebind work.
446 */
447 return xe_vm_validate_rebind(vm, exec, vm->preempt.num_exec_queues);
448 }
449
vm_suspend_rebind_worker(struct xe_vm * vm)450 static bool vm_suspend_rebind_worker(struct xe_vm *vm)
451 {
452 struct xe_device *xe = vm->xe;
453 bool ret = false;
454
455 mutex_lock(&xe->rebind_resume_lock);
456 if (!try_wait_for_completion(&vm->xe->pm_block)) {
457 ret = true;
458 list_move_tail(&vm->preempt.pm_activate_link, &xe->rebind_resume_list);
459 }
460 mutex_unlock(&xe->rebind_resume_lock);
461
462 return ret;
463 }
464
465 /**
466 * xe_vm_resume_rebind_worker() - Resume the rebind worker.
467 * @vm: The vm whose preempt worker to resume.
468 *
469 * Resume a preempt worker that was previously suspended by
470 * vm_suspend_rebind_worker().
471 */
xe_vm_resume_rebind_worker(struct xe_vm * vm)472 void xe_vm_resume_rebind_worker(struct xe_vm *vm)
473 {
474 queue_work(vm->xe->ordered_wq, &vm->preempt.rebind_work);
475 }
476
preempt_rebind_work_func(struct work_struct * w)477 static void preempt_rebind_work_func(struct work_struct *w)
478 {
479 struct xe_vm *vm = container_of(w, struct xe_vm, preempt.rebind_work);
480 struct xe_validation_ctx ctx;
481 struct drm_exec exec;
482 unsigned int fence_count = 0;
483 LIST_HEAD(preempt_fences);
484 int err = 0;
485 long wait;
486 int __maybe_unused tries = 0;
487
488 xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm));
489 trace_xe_vm_rebind_worker_enter(vm);
490
491 down_write(&vm->lock);
492
493 if (xe_vm_is_closed_or_banned(vm)) {
494 up_write(&vm->lock);
495 trace_xe_vm_rebind_worker_exit(vm);
496 return;
497 }
498
499 retry:
500 if (!try_wait_for_completion(&vm->xe->pm_block) && vm_suspend_rebind_worker(vm)) {
501 up_write(&vm->lock);
502 /* We don't actually block but don't make progress. */
503 xe_pm_might_block_on_suspend();
504 return;
505 }
506
507 if (xe_vm_userptr_check_repin(vm)) {
508 err = xe_vm_userptr_pin(vm);
509 if (err)
510 goto out_unlock_outer;
511 }
512
513 err = xe_validation_ctx_init(&ctx, &vm->xe->val, &exec,
514 (struct xe_val_flags) {.interruptible = true});
515 if (err)
516 goto out_unlock_outer;
517
518 drm_exec_until_all_locked(&exec) {
519 bool done = false;
520
521 err = xe_preempt_work_begin(&exec, vm, &done);
522 drm_exec_retry_on_contention(&exec);
523 xe_validation_retry_on_oom(&ctx, &err);
524 if (err || done) {
525 xe_validation_ctx_fini(&ctx);
526 goto out_unlock_outer;
527 }
528 }
529
530 err = alloc_preempt_fences(vm, &preempt_fences, &fence_count);
531 if (err)
532 goto out_unlock;
533
534 xe_vm_set_validation_exec(vm, &exec);
535 err = xe_vm_rebind(vm, true);
536 xe_vm_set_validation_exec(vm, NULL);
537 if (err)
538 goto out_unlock;
539
540 /* Wait on rebinds and munmap style VM unbinds */
541 wait = dma_resv_wait_timeout(xe_vm_resv(vm),
542 DMA_RESV_USAGE_KERNEL,
543 false, MAX_SCHEDULE_TIMEOUT);
544 if (wait <= 0) {
545 err = -ETIME;
546 goto out_unlock;
547 }
548
549 #define retry_required(__tries, __vm) \
550 (IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT) ? \
551 (!(__tries)++ || __xe_vm_userptr_needs_repin(__vm)) : \
552 __xe_vm_userptr_needs_repin(__vm))
553
554 xe_svm_notifier_lock(vm);
555 if (retry_required(tries, vm)) {
556 xe_svm_notifier_unlock(vm);
557 err = -EAGAIN;
558 goto out_unlock;
559 }
560
561 #undef retry_required
562
563 spin_lock(&vm->xe->ttm.lru_lock);
564 ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
565 spin_unlock(&vm->xe->ttm.lru_lock);
566
567 /* Point of no return. */
568 arm_preempt_fences(vm, &preempt_fences);
569 resume_and_reinstall_preempt_fences(vm, &exec);
570 xe_svm_notifier_unlock(vm);
571
572 out_unlock:
573 xe_validation_ctx_fini(&ctx);
574 out_unlock_outer:
575 if (err == -EAGAIN) {
576 trace_xe_vm_rebind_worker_retry(vm);
577
578 /*
579 * We can't block in workers on a VF which supports migration
580 * given this can block the VF post-migration workers from
581 * getting scheduled.
582 */
583 if (IS_SRIOV_VF(vm->xe) &&
584 xe_sriov_vf_migration_supported(vm->xe)) {
585 up_write(&vm->lock);
586 xe_vm_queue_rebind_worker(vm);
587 return;
588 }
589
590 goto retry;
591 }
592
593 if (err) {
594 drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
595 xe_vm_kill(vm, true);
596 }
597 up_write(&vm->lock);
598
599 free_preempt_fences(&preempt_fences);
600
601 trace_xe_vm_rebind_worker_exit(vm);
602 }
603
604 /**
605 * xe_vm_add_fault_entry_pf() - Add pagefault to vm fault list
606 * @vm: The VM.
607 * @pf: The pagefault.
608 *
609 * This function takes the data from the pagefault @pf and saves it to @vm->faults.list.
610 *
611 * The function exits silently if the list is full, and reports a warning if the pagefault
612 * could not be saved to the list.
613 */
xe_vm_add_fault_entry_pf(struct xe_vm * vm,struct xe_pagefault * pf)614 void xe_vm_add_fault_entry_pf(struct xe_vm *vm, struct xe_pagefault *pf)
615 {
616 struct xe_vm_fault_entry *e;
617 struct xe_hw_engine *hwe;
618
619 /* Do not report faults on reserved engines */
620 hwe = xe_gt_hw_engine(pf->gt, pf->consumer.engine_class,
621 pf->consumer.engine_instance, false);
622 if (!hwe || xe_hw_engine_is_reserved(hwe))
623 return;
624
625 e = kzalloc_obj(*e);
626 if (!e) {
627 drm_warn(&vm->xe->drm,
628 "Could not allocate memory for fault!\n");
629 return;
630 }
631
632 guard(spinlock)(&vm->faults.lock);
633
634 /*
635 * Limit the number of faults in the fault list to prevent
636 * memory overuse.
637 */
638 if (vm->faults.len >= MAX_FAULTS_SAVED_PER_VM) {
639 kfree(e);
640 return;
641 }
642
643 e->address = pf->consumer.page_addr;
644 /*
645 * TODO:
646 * Address precision is currently always SZ_4K, but this may change
647 * in the future.
648 */
649 e->address_precision = SZ_4K;
650 e->access_type = pf->consumer.access_type;
651 e->fault_type = FIELD_GET(XE_PAGEFAULT_TYPE_MASK,
652 pf->consumer.fault_type_level);
653 e->fault_level = FIELD_GET(XE_PAGEFAULT_LEVEL_MASK,
654 pf->consumer.fault_type_level);
655
656 list_add_tail(&e->list, &vm->faults.list);
657 vm->faults.len++;
658 }
659
xe_vm_clear_fault_entries(struct xe_vm * vm)660 static void xe_vm_clear_fault_entries(struct xe_vm *vm)
661 {
662 struct xe_vm_fault_entry *e, *tmp;
663
664 guard(spinlock)(&vm->faults.lock);
665 list_for_each_entry_safe(e, tmp, &vm->faults.list, list) {
666 list_del(&e->list);
667 kfree(e);
668 }
669 vm->faults.len = 0;
670 }
671
xe_vma_ops_alloc(struct xe_vma_ops * vops,bool array_of_binds)672 static int xe_vma_ops_alloc(struct xe_vma_ops *vops, bool array_of_binds)
673 {
674 int i;
675
676 for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i) {
677 if (!vops->pt_update_ops[i].num_ops)
678 continue;
679
680 vops->pt_update_ops[i].ops =
681 kmalloc_objs(*vops->pt_update_ops[i].ops,
682 vops->pt_update_ops[i].num_ops,
683 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
684 if (!vops->pt_update_ops[i].ops)
685 return array_of_binds ? -ENOBUFS : -ENOMEM;
686 }
687
688 return 0;
689 }
690 ALLOW_ERROR_INJECTION(xe_vma_ops_alloc, ERRNO);
691
xe_vma_svm_prefetch_op_fini(struct xe_vma_op * op)692 static void xe_vma_svm_prefetch_op_fini(struct xe_vma_op *op)
693 {
694 struct xe_vma *vma;
695
696 vma = gpuva_to_vma(op->base.prefetch.va);
697
698 if (op->base.op == DRM_GPUVA_OP_PREFETCH && xe_vma_is_cpu_addr_mirror(vma))
699 xa_destroy(&op->prefetch_range.range);
700 }
701
xe_vma_svm_prefetch_ops_fini(struct xe_vma_ops * vops)702 static void xe_vma_svm_prefetch_ops_fini(struct xe_vma_ops *vops)
703 {
704 struct xe_vma_op *op;
705
706 if (!(vops->flags & XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH))
707 return;
708
709 list_for_each_entry(op, &vops->list, link)
710 xe_vma_svm_prefetch_op_fini(op);
711 }
712
xe_vma_ops_fini(struct xe_vma_ops * vops)713 static void xe_vma_ops_fini(struct xe_vma_ops *vops)
714 {
715 int i;
716
717 xe_vma_svm_prefetch_ops_fini(vops);
718
719 for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
720 kfree(vops->pt_update_ops[i].ops);
721 }
722
xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops * vops,u8 tile_mask,int inc_val)723 static void xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops *vops, u8 tile_mask, int inc_val)
724 {
725 int i;
726
727 if (!inc_val)
728 return;
729
730 for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
731 if (BIT(i) & tile_mask)
732 vops->pt_update_ops[i].num_ops += inc_val;
733 }
734
735 #define XE_VMA_CREATE_MASK ( \
736 XE_VMA_READ_ONLY | \
737 XE_VMA_DUMPABLE | \
738 XE_VMA_SYSTEM_ALLOCATOR | \
739 DRM_GPUVA_SPARSE | \
740 XE_VMA_MADV_AUTORESET)
741
xe_vm_populate_rebind(struct xe_vma_op * op,struct xe_vma * vma,u8 tile_mask)742 static void xe_vm_populate_rebind(struct xe_vma_op *op, struct xe_vma *vma,
743 u8 tile_mask)
744 {
745 INIT_LIST_HEAD(&op->link);
746 op->tile_mask = tile_mask;
747 op->base.op = DRM_GPUVA_OP_MAP;
748 op->base.map.va.addr = vma->gpuva.va.addr;
749 op->base.map.va.range = vma->gpuva.va.range;
750 op->base.map.gem.obj = vma->gpuva.gem.obj;
751 op->base.map.gem.offset = vma->gpuva.gem.offset;
752 op->map.vma = vma;
753 op->map.immediate = true;
754 op->map.vma_flags = vma->gpuva.flags & XE_VMA_CREATE_MASK;
755 }
756
xe_vm_ops_add_rebind(struct xe_vma_ops * vops,struct xe_vma * vma,u8 tile_mask)757 static int xe_vm_ops_add_rebind(struct xe_vma_ops *vops, struct xe_vma *vma,
758 u8 tile_mask)
759 {
760 struct xe_vma_op *op;
761
762 op = kzalloc_obj(*op);
763 if (!op)
764 return -ENOMEM;
765
766 xe_vm_populate_rebind(op, vma, tile_mask);
767 list_add_tail(&op->link, &vops->list);
768 xe_vma_ops_incr_pt_update_ops(vops, tile_mask, 1);
769
770 return 0;
771 }
772
773 static struct dma_fence *ops_execute(struct xe_vm *vm,
774 struct xe_vma_ops *vops);
775 static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
776 struct xe_exec_queue *q,
777 struct xe_sync_entry *syncs, u32 num_syncs);
778
xe_vm_rebind(struct xe_vm * vm,bool rebind_worker)779 int xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
780 {
781 struct dma_fence *fence;
782 struct xe_vma *vma, *next;
783 struct xe_vma_ops vops;
784 struct xe_vma_op *op, *next_op;
785 int err, i;
786
787 lockdep_assert_held(&vm->lock);
788 if ((xe_vm_in_lr_mode(vm) && !rebind_worker) ||
789 list_empty(&vm->rebind_list))
790 return 0;
791
792 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
793 for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
794 vops.pt_update_ops[i].wait_vm_bookkeep = true;
795
796 xe_vm_assert_held(vm);
797 list_for_each_entry(vma, &vm->rebind_list, combined_links.rebind) {
798 xe_assert(vm->xe, vma->tile_present);
799
800 if (rebind_worker)
801 trace_xe_vma_rebind_worker(vma);
802 else
803 trace_xe_vma_rebind_exec(vma);
804
805 err = xe_vm_ops_add_rebind(&vops, vma,
806 vma->tile_present);
807 if (err)
808 goto free_ops;
809 }
810
811 err = xe_vma_ops_alloc(&vops, false);
812 if (err)
813 goto free_ops;
814
815 fence = ops_execute(vm, &vops);
816 if (IS_ERR(fence)) {
817 err = PTR_ERR(fence);
818 } else {
819 dma_fence_put(fence);
820 list_for_each_entry_safe(vma, next, &vm->rebind_list,
821 combined_links.rebind)
822 list_del_init(&vma->combined_links.rebind);
823 }
824 free_ops:
825 list_for_each_entry_safe(op, next_op, &vops.list, link) {
826 list_del(&op->link);
827 kfree(op);
828 }
829 xe_vma_ops_fini(&vops);
830
831 return err;
832 }
833
xe_vma_rebind(struct xe_vm * vm,struct xe_vma * vma,u8 tile_mask)834 struct dma_fence *xe_vma_rebind(struct xe_vm *vm, struct xe_vma *vma, u8 tile_mask)
835 {
836 struct dma_fence *fence = NULL;
837 struct xe_vma_ops vops;
838 struct xe_vma_op *op, *next_op;
839 struct xe_tile *tile;
840 u8 id;
841 int err;
842
843 lockdep_assert_held(&vm->lock);
844 xe_vm_assert_held(vm);
845 xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
846
847 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
848 vops.flags |= XE_VMA_OPS_FLAG_SKIP_TLB_WAIT;
849 for_each_tile(tile, vm->xe, id) {
850 vops.pt_update_ops[id].wait_vm_bookkeep = true;
851 vops.pt_update_ops[tile->id].q =
852 xe_migrate_exec_queue(tile->migrate);
853 }
854
855 err = xe_vm_ops_add_rebind(&vops, vma, tile_mask);
856 if (err)
857 return ERR_PTR(err);
858
859 err = xe_vma_ops_alloc(&vops, false);
860 if (err) {
861 fence = ERR_PTR(err);
862 goto free_ops;
863 }
864
865 fence = ops_execute(vm, &vops);
866
867 free_ops:
868 list_for_each_entry_safe(op, next_op, &vops.list, link) {
869 list_del(&op->link);
870 kfree(op);
871 }
872 xe_vma_ops_fini(&vops);
873
874 return fence;
875 }
876
xe_vm_populate_range_rebind(struct xe_vma_op * op,struct xe_vma * vma,struct xe_svm_range * range,u8 tile_mask)877 static void xe_vm_populate_range_rebind(struct xe_vma_op *op,
878 struct xe_vma *vma,
879 struct xe_svm_range *range,
880 u8 tile_mask)
881 {
882 INIT_LIST_HEAD(&op->link);
883 op->tile_mask = tile_mask;
884 op->base.op = DRM_GPUVA_OP_DRIVER;
885 op->subop = XE_VMA_SUBOP_MAP_RANGE;
886 op->map_range.vma = vma;
887 op->map_range.range = range;
888 }
889
890 static int
xe_vm_ops_add_range_rebind(struct xe_vma_ops * vops,struct xe_vma * vma,struct xe_svm_range * range,u8 tile_mask)891 xe_vm_ops_add_range_rebind(struct xe_vma_ops *vops,
892 struct xe_vma *vma,
893 struct xe_svm_range *range,
894 u8 tile_mask)
895 {
896 struct xe_vma_op *op;
897
898 op = kzalloc_obj(*op);
899 if (!op)
900 return -ENOMEM;
901
902 xe_vm_populate_range_rebind(op, vma, range, tile_mask);
903 list_add_tail(&op->link, &vops->list);
904 xe_vma_ops_incr_pt_update_ops(vops, tile_mask, 1);
905
906 return 0;
907 }
908
909 /**
910 * xe_vm_range_rebind() - VM range (re)bind
911 * @vm: The VM which the range belongs to.
912 * @vma: The VMA which the range belongs to.
913 * @range: SVM range to rebind.
914 * @tile_mask: Tile mask to bind the range to.
915 *
916 * (re)bind SVM range setting up GPU page tables for the range.
917 *
918 * Return: dma fence for rebind to signal completion on success, ERR_PTR on
919 * failure
920 */
xe_vm_range_rebind(struct xe_vm * vm,struct xe_vma * vma,struct xe_svm_range * range,u8 tile_mask)921 struct dma_fence *xe_vm_range_rebind(struct xe_vm *vm,
922 struct xe_vma *vma,
923 struct xe_svm_range *range,
924 u8 tile_mask)
925 {
926 struct dma_fence *fence = NULL;
927 struct xe_vma_ops vops;
928 struct xe_vma_op *op, *next_op;
929 struct xe_tile *tile;
930 u8 id;
931 int err;
932
933 lockdep_assert_held(&vm->lock);
934 xe_vm_assert_held(vm);
935 xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
936 xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(vma));
937
938 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
939 vops.flags |= XE_VMA_OPS_FLAG_SKIP_TLB_WAIT;
940 for_each_tile(tile, vm->xe, id) {
941 vops.pt_update_ops[id].wait_vm_bookkeep = true;
942 vops.pt_update_ops[tile->id].q =
943 xe_migrate_exec_queue(tile->migrate);
944 }
945
946 err = xe_vm_ops_add_range_rebind(&vops, vma, range, tile_mask);
947 if (err)
948 return ERR_PTR(err);
949
950 err = xe_vma_ops_alloc(&vops, false);
951 if (err) {
952 fence = ERR_PTR(err);
953 goto free_ops;
954 }
955
956 fence = ops_execute(vm, &vops);
957
958 free_ops:
959 list_for_each_entry_safe(op, next_op, &vops.list, link) {
960 list_del(&op->link);
961 kfree(op);
962 }
963 xe_vma_ops_fini(&vops);
964
965 return fence;
966 }
967
xe_vm_populate_range_unbind(struct xe_vma_op * op,struct xe_svm_range * range)968 static void xe_vm_populate_range_unbind(struct xe_vma_op *op,
969 struct xe_svm_range *range)
970 {
971 INIT_LIST_HEAD(&op->link);
972 op->tile_mask = range->tile_present;
973 op->base.op = DRM_GPUVA_OP_DRIVER;
974 op->subop = XE_VMA_SUBOP_UNMAP_RANGE;
975 op->unmap_range.range = range;
976 }
977
978 static int
xe_vm_ops_add_range_unbind(struct xe_vma_ops * vops,struct xe_svm_range * range)979 xe_vm_ops_add_range_unbind(struct xe_vma_ops *vops,
980 struct xe_svm_range *range)
981 {
982 struct xe_vma_op *op;
983
984 op = kzalloc_obj(*op);
985 if (!op)
986 return -ENOMEM;
987
988 xe_vm_populate_range_unbind(op, range);
989 list_add_tail(&op->link, &vops->list);
990 xe_vma_ops_incr_pt_update_ops(vops, range->tile_present, 1);
991
992 return 0;
993 }
994
995 /**
996 * xe_vm_range_unbind() - VM range unbind
997 * @vm: The VM which the range belongs to.
998 * @range: SVM range to rebind.
999 *
1000 * Unbind SVM range removing the GPU page tables for the range.
1001 *
1002 * Return: dma fence for unbind to signal completion on success, ERR_PTR on
1003 * failure
1004 */
xe_vm_range_unbind(struct xe_vm * vm,struct xe_svm_range * range)1005 struct dma_fence *xe_vm_range_unbind(struct xe_vm *vm,
1006 struct xe_svm_range *range)
1007 {
1008 struct dma_fence *fence = NULL;
1009 struct xe_vma_ops vops;
1010 struct xe_vma_op *op, *next_op;
1011 struct xe_tile *tile;
1012 u8 id;
1013 int err;
1014
1015 lockdep_assert_held(&vm->lock);
1016 xe_vm_assert_held(vm);
1017 xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
1018
1019 if (!range->tile_present)
1020 return dma_fence_get_stub();
1021
1022 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
1023 for_each_tile(tile, vm->xe, id) {
1024 vops.pt_update_ops[id].wait_vm_bookkeep = true;
1025 vops.pt_update_ops[tile->id].q =
1026 xe_migrate_exec_queue(tile->migrate);
1027 }
1028
1029 err = xe_vm_ops_add_range_unbind(&vops, range);
1030 if (err)
1031 return ERR_PTR(err);
1032
1033 err = xe_vma_ops_alloc(&vops, false);
1034 if (err) {
1035 fence = ERR_PTR(err);
1036 goto free_ops;
1037 }
1038
1039 fence = ops_execute(vm, &vops);
1040
1041 free_ops:
1042 list_for_each_entry_safe(op, next_op, &vops.list, link) {
1043 list_del(&op->link);
1044 kfree(op);
1045 }
1046 xe_vma_ops_fini(&vops);
1047
1048 return fence;
1049 }
1050
xe_vma_mem_attr_fini(struct xe_vma_mem_attr * attr)1051 static void xe_vma_mem_attr_fini(struct xe_vma_mem_attr *attr)
1052 {
1053 drm_pagemap_put(attr->preferred_loc.dpagemap);
1054 }
1055
xe_vma_free(struct xe_vma * vma)1056 static void xe_vma_free(struct xe_vma *vma)
1057 {
1058 xe_vma_mem_attr_fini(&vma->attr);
1059
1060 if (xe_vma_is_userptr(vma))
1061 kfree(to_userptr_vma(vma));
1062 else
1063 kfree(vma);
1064 }
1065
1066 /**
1067 * xe_vma_mem_attr_copy() - copy an xe_vma_mem_attr structure.
1068 * @to: Destination.
1069 * @from: Source.
1070 *
1071 * Copies an xe_vma_mem_attr structure taking care to get reference
1072 * counting of individual members right.
1073 */
xe_vma_mem_attr_copy(struct xe_vma_mem_attr * to,struct xe_vma_mem_attr * from)1074 void xe_vma_mem_attr_copy(struct xe_vma_mem_attr *to, struct xe_vma_mem_attr *from)
1075 {
1076 xe_vma_mem_attr_fini(to);
1077 *to = *from;
1078 if (to->preferred_loc.dpagemap)
1079 drm_pagemap_get(to->preferred_loc.dpagemap);
1080 }
1081
xe_vma_create(struct xe_vm * vm,struct xe_bo * bo,u64 bo_offset_or_userptr,u64 start,u64 end,struct xe_vma_mem_attr * attr,unsigned int flags)1082 static struct xe_vma *xe_vma_create(struct xe_vm *vm,
1083 struct xe_bo *bo,
1084 u64 bo_offset_or_userptr,
1085 u64 start, u64 end,
1086 struct xe_vma_mem_attr *attr,
1087 unsigned int flags)
1088 {
1089 struct xe_vma *vma;
1090 struct xe_tile *tile;
1091 u8 id;
1092 bool is_null = (flags & DRM_GPUVA_SPARSE);
1093 bool is_cpu_addr_mirror = (flags & XE_VMA_SYSTEM_ALLOCATOR);
1094
1095 xe_assert(vm->xe, start < end);
1096 xe_assert(vm->xe, end < vm->size);
1097
1098 /*
1099 * Allocate and ensure that the xe_vma_is_userptr() return
1100 * matches what was allocated.
1101 */
1102 if (!bo && !is_null && !is_cpu_addr_mirror) {
1103 struct xe_userptr_vma *uvma = kzalloc_obj(*uvma);
1104
1105 if (!uvma)
1106 return ERR_PTR(-ENOMEM);
1107
1108 vma = &uvma->vma;
1109 } else {
1110 vma = kzalloc_obj(*vma);
1111 if (!vma)
1112 return ERR_PTR(-ENOMEM);
1113
1114 if (bo)
1115 vma->gpuva.gem.obj = &bo->ttm.base;
1116 }
1117
1118 INIT_LIST_HEAD(&vma->combined_links.rebind);
1119
1120 INIT_LIST_HEAD(&vma->gpuva.gem.entry);
1121 vma->gpuva.vm = &vm->gpuvm;
1122 vma->gpuva.va.addr = start;
1123 vma->gpuva.va.range = end - start + 1;
1124 vma->gpuva.flags = flags;
1125
1126 for_each_tile(tile, vm->xe, id)
1127 vma->tile_mask |= 0x1 << id;
1128
1129 if (vm->xe->info.has_atomic_enable_pte_bit)
1130 vma->gpuva.flags |= XE_VMA_ATOMIC_PTE_BIT;
1131
1132 xe_vma_mem_attr_copy(&vma->attr, attr);
1133 if (bo) {
1134 struct drm_gpuvm_bo *vm_bo;
1135
1136 xe_bo_assert_held(bo);
1137
1138 /*
1139 * Reject only WILLNEED mappings on DONTNEED/PURGED BOs. This
1140 * gates new vm_bind ioctls (user supplies WILLNEED) while
1141 * still allowing partial-unbind / remap splits whose new VMAs
1142 * inherit the parent's DONTNEED attr. It must also run before
1143 * xe_bo_willneed_get_locked() below so a 0->1 holder bump
1144 * cannot silently promote DONTNEED back to WILLNEED.
1145 */
1146 if (vma->attr.purgeable_state == XE_MADV_PURGEABLE_WILLNEED) {
1147 if (xe_bo_madv_is_dontneed(bo)) {
1148 xe_vma_free(vma);
1149 return ERR_PTR(-EBUSY);
1150 }
1151 if (xe_bo_is_purged(bo)) {
1152 xe_vma_free(vma);
1153 return ERR_PTR(-EINVAL);
1154 }
1155 }
1156
1157 vm_bo = drm_gpuvm_bo_obtain_locked(vma->gpuva.vm, &bo->ttm.base);
1158 if (IS_ERR(vm_bo)) {
1159 xe_vma_free(vma);
1160 return ERR_CAST(vm_bo);
1161 }
1162
1163 drm_gpuvm_bo_extobj_add(vm_bo);
1164 drm_gem_object_get(&bo->ttm.base);
1165 vma->gpuva.gem.offset = bo_offset_or_userptr;
1166 drm_gpuva_link(&vma->gpuva, vm_bo);
1167 drm_gpuvm_bo_put(vm_bo);
1168
1169 xe_bo_vma_count_inc_locked(bo);
1170 if (vma->attr.purgeable_state == XE_MADV_PURGEABLE_WILLNEED)
1171 xe_bo_willneed_get_locked(bo);
1172 } else /* userptr or null */ {
1173 if (!is_null && !is_cpu_addr_mirror) {
1174 struct xe_userptr_vma *uvma = to_userptr_vma(vma);
1175 u64 size = end - start + 1;
1176 int err;
1177
1178 vma->gpuva.gem.offset = bo_offset_or_userptr;
1179
1180 err = xe_userptr_setup(uvma, xe_vma_userptr(vma), size);
1181 if (err) {
1182 xe_vma_free(vma);
1183 return ERR_PTR(err);
1184 }
1185 }
1186
1187 xe_vm_get(vm);
1188 }
1189
1190 return vma;
1191 }
1192
xe_vma_destroy_late(struct xe_vma * vma)1193 static void xe_vma_destroy_late(struct xe_vma *vma)
1194 {
1195 struct xe_vm *vm = xe_vma_vm(vma);
1196 struct xe_bo *bo = xe_vma_bo(vma);
1197
1198 if (vma->ufence) {
1199 xe_sync_ufence_put(vma->ufence);
1200 vma->ufence = NULL;
1201 }
1202
1203 if (xe_vma_is_userptr(vma)) {
1204 struct xe_userptr_vma *uvma = to_userptr_vma(vma);
1205
1206 xe_userptr_remove(uvma);
1207 xe_vm_put(vm);
1208 } else if (xe_vma_is_null(vma) || xe_vma_is_cpu_addr_mirror(vma)) {
1209 xe_vm_put(vm);
1210 } else {
1211 xe_bo_put(bo);
1212 }
1213
1214 xe_vma_free(vma);
1215 }
1216
vma_destroy_work_func(struct work_struct * w)1217 static void vma_destroy_work_func(struct work_struct *w)
1218 {
1219 struct xe_vma *vma =
1220 container_of(w, struct xe_vma, destroy_work);
1221
1222 xe_vma_destroy_late(vma);
1223 }
1224
vma_destroy_cb(struct dma_fence * fence,struct dma_fence_cb * cb)1225 static void vma_destroy_cb(struct dma_fence *fence,
1226 struct dma_fence_cb *cb)
1227 {
1228 struct xe_vma *vma = container_of(cb, struct xe_vma, destroy_cb);
1229
1230 INIT_WORK(&vma->destroy_work, vma_destroy_work_func);
1231 queue_work(system_dfl_wq, &vma->destroy_work);
1232 }
1233
xe_vma_destroy(struct xe_vma * vma,struct dma_fence * fence)1234 static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence)
1235 {
1236 struct xe_vm *vm = xe_vma_vm(vma);
1237 struct xe_bo *bo = xe_vma_bo(vma);
1238
1239 lockdep_assert_held_write(&vm->lock);
1240 xe_assert(vm->xe, list_empty(&vma->combined_links.destroy));
1241
1242 if (xe_vma_is_userptr(vma)) {
1243 xe_assert(vm->xe, vma->gpuva.flags & XE_VMA_DESTROYED);
1244 xe_userptr_destroy(to_userptr_vma(vma));
1245 } else if (!xe_vma_is_null(vma) && !xe_vma_is_cpu_addr_mirror(vma)) {
1246 xe_bo_assert_held(bo);
1247
1248 drm_gpuva_unlink(&vma->gpuva);
1249
1250 xe_bo_vma_count_dec_locked(bo);
1251 if (vma->attr.purgeable_state == XE_MADV_PURGEABLE_WILLNEED)
1252 xe_bo_willneed_put_locked(bo);
1253 }
1254
1255 xe_vm_assert_held(vm);
1256 if (fence) {
1257 int ret = dma_fence_add_callback(fence, &vma->destroy_cb,
1258 vma_destroy_cb);
1259
1260 if (ret) {
1261 XE_WARN_ON(ret != -ENOENT);
1262 xe_vma_destroy_late(vma);
1263 }
1264 } else {
1265 xe_vma_destroy_late(vma);
1266 }
1267 }
1268
1269 /**
1270 * xe_vm_lock_vma() - drm_exec utility to lock a vma
1271 * @exec: The drm_exec object we're currently locking for.
1272 * @vma: The vma for witch we want to lock the vm resv and any attached
1273 * object's resv.
1274 *
1275 * Return: 0 on success, negative error code on error. In particular
1276 * may return -EDEADLK on WW transaction contention and -EINTR if
1277 * an interruptible wait is terminated by a signal.
1278 */
xe_vm_lock_vma(struct drm_exec * exec,struct xe_vma * vma)1279 int xe_vm_lock_vma(struct drm_exec *exec, struct xe_vma *vma)
1280 {
1281 struct xe_vm *vm = xe_vma_vm(vma);
1282 struct xe_bo *bo = xe_vma_bo(vma);
1283 int err;
1284
1285 XE_WARN_ON(!vm);
1286
1287 err = drm_exec_lock_obj(exec, xe_vm_obj(vm));
1288 if (!err && bo && !bo->vm)
1289 err = drm_exec_lock_obj(exec, &bo->ttm.base);
1290
1291 return err;
1292 }
1293
xe_vma_destroy_unlocked(struct xe_vma * vma)1294 static void xe_vma_destroy_unlocked(struct xe_vma *vma)
1295 {
1296 struct xe_device *xe = xe_vma_vm(vma)->xe;
1297 struct xe_validation_ctx ctx;
1298 struct drm_exec exec;
1299 int err = 0;
1300
1301 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {}, err) {
1302 err = xe_vm_lock_vma(&exec, vma);
1303 drm_exec_retry_on_contention(&exec);
1304 if (XE_WARN_ON(err))
1305 break;
1306 xe_vma_destroy(vma, NULL);
1307 }
1308 xe_assert(xe, !err);
1309 }
1310
1311 struct xe_vma *
xe_vm_find_overlapping_vma(struct xe_vm * vm,u64 start,u64 range)1312 xe_vm_find_overlapping_vma(struct xe_vm *vm, u64 start, u64 range)
1313 {
1314 struct drm_gpuva *gpuva;
1315
1316 lockdep_assert_held(&vm->lock);
1317
1318 if (xe_vm_is_closed_or_banned(vm))
1319 return NULL;
1320
1321 xe_assert(vm->xe, start + range <= vm->size);
1322
1323 gpuva = drm_gpuva_find_first(&vm->gpuvm, start, range);
1324
1325 return gpuva ? gpuva_to_vma(gpuva) : NULL;
1326 }
1327
xe_vm_insert_vma(struct xe_vm * vm,struct xe_vma * vma)1328 static int xe_vm_insert_vma(struct xe_vm *vm, struct xe_vma *vma)
1329 {
1330 int err;
1331
1332 xe_assert(vm->xe, xe_vma_vm(vma) == vm);
1333 lockdep_assert_held(&vm->lock);
1334
1335 mutex_lock(&vm->snap_mutex);
1336 err = drm_gpuva_insert(&vm->gpuvm, &vma->gpuva);
1337 mutex_unlock(&vm->snap_mutex);
1338 XE_WARN_ON(err); /* Shouldn't be possible */
1339
1340 return err;
1341 }
1342
xe_vm_remove_vma(struct xe_vm * vm,struct xe_vma * vma)1343 static void xe_vm_remove_vma(struct xe_vm *vm, struct xe_vma *vma)
1344 {
1345 xe_assert(vm->xe, xe_vma_vm(vma) == vm);
1346 lockdep_assert_held(&vm->lock);
1347
1348 mutex_lock(&vm->snap_mutex);
1349 drm_gpuva_remove(&vma->gpuva);
1350 mutex_unlock(&vm->snap_mutex);
1351 if (vm->usm.last_fault_vma == vma)
1352 vm->usm.last_fault_vma = NULL;
1353 }
1354
xe_vm_op_alloc(void)1355 static struct drm_gpuva_op *xe_vm_op_alloc(void)
1356 {
1357 struct xe_vma_op *op;
1358
1359 op = kzalloc_obj(*op);
1360
1361 if (unlikely(!op))
1362 return NULL;
1363
1364 return &op->base;
1365 }
1366
1367 static void xe_vm_free(struct drm_gpuvm *gpuvm);
1368
1369 static const struct drm_gpuvm_ops gpuvm_ops = {
1370 .op_alloc = xe_vm_op_alloc,
1371 .vm_bo_validate = xe_gpuvm_validate,
1372 .vm_free = xe_vm_free,
1373 };
1374
pde_encode_pat_index(u16 pat_index)1375 static u64 pde_encode_pat_index(u16 pat_index)
1376 {
1377 u64 pte = 0;
1378
1379 if (pat_index & BIT(0))
1380 pte |= XE_PPGTT_PTE_PAT0;
1381
1382 if (pat_index & BIT(1))
1383 pte |= XE_PPGTT_PTE_PAT1;
1384
1385 return pte;
1386 }
1387
pte_encode_pat_index(u16 pat_index,u32 pt_level)1388 static u64 pte_encode_pat_index(u16 pat_index, u32 pt_level)
1389 {
1390 u64 pte = 0;
1391
1392 if (pat_index & BIT(0))
1393 pte |= XE_PPGTT_PTE_PAT0;
1394
1395 if (pat_index & BIT(1))
1396 pte |= XE_PPGTT_PTE_PAT1;
1397
1398 if (pat_index & BIT(2)) {
1399 if (pt_level)
1400 pte |= XE_PPGTT_PDE_PDPE_PAT2;
1401 else
1402 pte |= XE_PPGTT_PTE_PAT2;
1403 }
1404
1405 if (pat_index & BIT(3))
1406 pte |= XELPG_PPGTT_PTE_PAT3;
1407
1408 if (pat_index & (BIT(4)))
1409 pte |= XE2_PPGTT_PTE_PAT4;
1410
1411 return pte;
1412 }
1413
pte_encode_ps(u32 pt_level)1414 static u64 pte_encode_ps(u32 pt_level)
1415 {
1416 XE_WARN_ON(pt_level > MAX_HUGEPTE_LEVEL);
1417
1418 if (pt_level == 1)
1419 return XE_PDE_PS_2M;
1420 else if (pt_level == 2)
1421 return XE_PDPE_PS_1G;
1422
1423 return 0;
1424 }
1425
pde_pat_index(struct xe_bo * bo)1426 static u16 pde_pat_index(struct xe_bo *bo)
1427 {
1428 struct xe_device *xe = xe_bo_device(bo);
1429 u16 pat_index;
1430
1431 /*
1432 * We only have two bits to encode the PAT index in non-leaf nodes, but
1433 * these only point to other paging structures so we only need a minimal
1434 * selection of options. The user PAT index is only for encoding leaf
1435 * nodes, where we have use of more bits to do the encoding. The
1436 * non-leaf nodes are instead under driver control so the chosen index
1437 * here should be distinct from the user PAT index. Also the
1438 * corresponding coherency of the PAT index should be tied to the
1439 * allocation type of the page table (or at least we should pick
1440 * something which is always safe).
1441 */
1442 if (!xe_bo_is_vram(bo) && bo->ttm.ttm->caching == ttm_cached)
1443 pat_index = xe_cache_pat_idx(xe, XE_CACHE_WB);
1444 else
1445 pat_index = xe_cache_pat_idx(xe, XE_CACHE_NONE);
1446
1447 xe_assert(xe, pat_index <= 3);
1448
1449 return pat_index;
1450 }
1451
xelp_pde_encode_bo(struct xe_bo * bo,u64 bo_offset)1452 static u64 xelp_pde_encode_bo(struct xe_bo *bo, u64 bo_offset)
1453 {
1454 u64 pde;
1455
1456 pde = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
1457 pde |= XE_PAGE_PRESENT | XE_PAGE_RW;
1458 pde |= pde_encode_pat_index(pde_pat_index(bo));
1459
1460 return pde;
1461 }
1462
xelp_pte_encode_bo(struct xe_bo * bo,u64 bo_offset,u16 pat_index,u32 pt_level)1463 static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
1464 u16 pat_index, u32 pt_level)
1465 {
1466 u64 pte;
1467
1468 pte = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
1469 pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
1470 pte |= pte_encode_pat_index(pat_index, pt_level);
1471 pte |= pte_encode_ps(pt_level);
1472
1473 if (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo))
1474 pte |= XE_PPGTT_PTE_DM;
1475
1476 return pte;
1477 }
1478
xelp_pte_encode_vma(u64 pte,struct xe_vma * vma,u16 pat_index,u32 pt_level)1479 static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
1480 u16 pat_index, u32 pt_level)
1481 {
1482 struct xe_bo *bo = xe_vma_bo(vma);
1483 struct xe_vm *vm = xe_vma_vm(vma);
1484
1485 pte |= XE_PAGE_PRESENT;
1486
1487 if (likely(!xe_vma_read_only(vma)))
1488 pte |= XE_PAGE_RW;
1489
1490 pte |= pte_encode_pat_index(pat_index, pt_level);
1491 pte |= pte_encode_ps(pt_level);
1492
1493 /*
1494 * NULL PTEs redirect to scratch page (return zeros on read).
1495 * Set for: 1) explicit null VMAs, 2) purged BOs on scratch VMs.
1496 * Never set NULL flag without scratch page - causes undefined behavior.
1497 */
1498 if (unlikely(xe_vma_is_null(vma) ||
1499 (bo && xe_bo_is_purged(bo) && xe_vm_has_scratch(vm))))
1500 pte |= XE_PTE_NULL;
1501
1502 return pte;
1503 }
1504
xelp_pte_encode_addr(struct xe_device * xe,u64 addr,u16 pat_index,u32 pt_level,bool devmem,u64 flags)1505 static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
1506 u16 pat_index,
1507 u32 pt_level, bool devmem, u64 flags)
1508 {
1509 u64 pte;
1510
1511 /* Avoid passing random bits directly as flags */
1512 xe_assert(xe, !(flags & ~XE_PTE_PS64));
1513
1514 pte = addr;
1515 pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
1516 pte |= pte_encode_pat_index(pat_index, pt_level);
1517 pte |= pte_encode_ps(pt_level);
1518
1519 if (devmem)
1520 pte |= XE_PPGTT_PTE_DM;
1521
1522 pte |= flags;
1523
1524 return pte;
1525 }
1526
1527 static const struct xe_pt_ops xelp_pt_ops = {
1528 .pte_encode_bo = xelp_pte_encode_bo,
1529 .pte_encode_vma = xelp_pte_encode_vma,
1530 .pte_encode_addr = xelp_pte_encode_addr,
1531 .pde_encode_bo = xelp_pde_encode_bo,
1532 };
1533
1534 static void vm_destroy_work_func(struct work_struct *w);
1535
1536 /**
1537 * xe_vm_create_scratch() - Setup a scratch memory pagetable tree for the
1538 * given tile and vm.
1539 * @xe: xe device.
1540 * @tile: tile to set up for.
1541 * @vm: vm to set up for.
1542 * @exec: The struct drm_exec object used to lock the vm resv.
1543 *
1544 * Sets up a pagetable tree with one page-table per level and a single
1545 * leaf PTE. All pagetable entries point to the single page-table or,
1546 * for MAX_HUGEPTE_LEVEL, a NULL huge PTE returning 0 on read and
1547 * writes become NOPs.
1548 *
1549 * Return: 0 on success, negative error code on error.
1550 */
xe_vm_create_scratch(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm,struct drm_exec * exec)1551 static int xe_vm_create_scratch(struct xe_device *xe, struct xe_tile *tile,
1552 struct xe_vm *vm, struct drm_exec *exec)
1553 {
1554 u8 id = tile->id;
1555 int i;
1556
1557 for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; i++) {
1558 vm->scratch_pt[id][i] = xe_pt_create(vm, tile, i, exec);
1559 if (IS_ERR(vm->scratch_pt[id][i])) {
1560 int err = PTR_ERR(vm->scratch_pt[id][i]);
1561
1562 vm->scratch_pt[id][i] = NULL;
1563 return err;
1564 }
1565 xe_pt_populate_empty(tile, vm, vm->scratch_pt[id][i]);
1566 }
1567
1568 return 0;
1569 }
1570 ALLOW_ERROR_INJECTION(xe_vm_create_scratch, ERRNO);
1571
xe_vm_free_scratch(struct xe_vm * vm)1572 static void xe_vm_free_scratch(struct xe_vm *vm)
1573 {
1574 struct xe_tile *tile;
1575 u8 id;
1576
1577 if (!xe_vm_has_scratch(vm))
1578 return;
1579
1580 for_each_tile(tile, vm->xe, id) {
1581 u32 i;
1582
1583 if (!vm->pt_root[id])
1584 continue;
1585
1586 for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; ++i)
1587 if (vm->scratch_pt[id][i])
1588 xe_pt_destroy(vm->scratch_pt[id][i], vm->flags, NULL);
1589 }
1590 }
1591
xe_vm_pt_destroy(struct xe_vm * vm)1592 static void xe_vm_pt_destroy(struct xe_vm *vm)
1593 {
1594 struct xe_tile *tile;
1595 u8 id;
1596
1597 xe_vm_assert_held(vm);
1598
1599 for_each_tile(tile, vm->xe, id) {
1600 if (vm->pt_root[id]) {
1601 xe_pt_destroy(vm->pt_root[id], vm->flags, NULL);
1602 vm->pt_root[id] = NULL;
1603 }
1604 }
1605 }
1606
xe_vm_init_prove_locking(struct xe_device * xe,struct xe_vm * vm)1607 static void xe_vm_init_prove_locking(struct xe_device *xe, struct xe_vm *vm)
1608 {
1609 if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
1610 return;
1611
1612 fs_reclaim_acquire(GFP_KERNEL);
1613 might_lock(&vm->exec_queues.lock);
1614 fs_reclaim_release(GFP_KERNEL);
1615
1616 down_read(&vm->exec_queues.lock);
1617 might_lock(&xe_root_mmio_gt(xe)->uc.guc.ct.lock);
1618 up_read(&vm->exec_queues.lock);
1619 }
1620
xe_vm_create(struct xe_device * xe,u32 flags,struct xe_file * xef)1621 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags, struct xe_file *xef)
1622 {
1623 struct drm_gem_object *vm_resv_obj;
1624 struct xe_validation_ctx ctx;
1625 struct drm_exec exec;
1626 struct xe_vm *vm;
1627 int err;
1628 struct xe_tile *tile;
1629 u8 id;
1630
1631 /*
1632 * Since the GSCCS is not user-accessible, we don't expect a GSC VM to
1633 * ever be in faulting mode.
1634 */
1635 xe_assert(xe, !((flags & XE_VM_FLAG_GSC) && (flags & XE_VM_FLAG_FAULT_MODE)));
1636
1637 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1638 if (!vm)
1639 return ERR_PTR(-ENOMEM);
1640
1641 vm->xe = xe;
1642
1643 vm->size = 1ull << xe->info.va_bits;
1644 vm->flags = flags;
1645
1646 if (xef)
1647 vm->xef = xe_file_get(xef);
1648 /*
1649 * GSC VMs are kernel-owned, only used for PXP ops and can sometimes be
1650 * manipulated under the PXP mutex. However, the PXP mutex can be taken
1651 * under a user-VM lock when the PXP session is started at exec_queue
1652 * creation time. Those are different VMs and therefore there is no risk
1653 * of deadlock, but we need to tell lockdep that this is the case or it
1654 * will print a warning.
1655 */
1656 if (flags & XE_VM_FLAG_GSC) {
1657 static struct lock_class_key gsc_vm_key;
1658
1659 __init_rwsem(&vm->lock, "gsc_vm", &gsc_vm_key);
1660 } else {
1661 init_rwsem(&vm->lock);
1662 }
1663 mutex_init(&vm->snap_mutex);
1664
1665 INIT_LIST_HEAD(&vm->rebind_list);
1666
1667 INIT_LIST_HEAD(&vm->userptr.repin_list);
1668 INIT_LIST_HEAD(&vm->userptr.invalidated);
1669 spin_lock_init(&vm->userptr.invalidated_lock);
1670
1671 INIT_LIST_HEAD(&vm->faults.list);
1672 spin_lock_init(&vm->faults.lock);
1673
1674 ttm_lru_bulk_move_init(&vm->lru_bulk_move);
1675
1676 INIT_WORK(&vm->destroy_work, vm_destroy_work_func);
1677
1678 INIT_LIST_HEAD(&vm->preempt.exec_queues);
1679 for (id = 0; id < XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE; ++id)
1680 INIT_LIST_HEAD(&vm->exec_queues.list[id]);
1681 if (flags & XE_VM_FLAG_FAULT_MODE)
1682 vm->preempt.min_run_period_ms = xe->min_run_period_pf_ms;
1683 else
1684 vm->preempt.min_run_period_ms = xe->min_run_period_lr_ms;
1685
1686 init_rwsem(&vm->exec_queues.lock);
1687 xe_vm_init_prove_locking(xe, vm);
1688
1689 for_each_tile(tile, xe, id)
1690 xe_range_fence_tree_init(&vm->rftree[id]);
1691
1692 vm->pt_ops = &xelp_pt_ops;
1693
1694 /*
1695 * Long-running workloads are not protected by the scheduler references.
1696 * By design, run_job for long-running workloads returns NULL and the
1697 * scheduler drops all the references of it, hence protecting the VM
1698 * for this case is necessary.
1699 */
1700 if (flags & XE_VM_FLAG_LR_MODE) {
1701 INIT_WORK(&vm->preempt.rebind_work, preempt_rebind_work_func);
1702 xe_pm_runtime_get_noresume(xe);
1703 INIT_LIST_HEAD(&vm->preempt.pm_activate_link);
1704 }
1705
1706 err = xe_svm_init(vm);
1707 if (err)
1708 goto err_no_resv;
1709
1710 vm_resv_obj = drm_gpuvm_resv_object_alloc(&xe->drm);
1711 if (!vm_resv_obj) {
1712 err = -ENOMEM;
1713 goto err_svm_fini;
1714 }
1715
1716 drm_gpuvm_init(&vm->gpuvm, "Xe VM", DRM_GPUVM_RESV_PROTECTED, &xe->drm,
1717 vm_resv_obj, 0, vm->size, 0, 0, &gpuvm_ops);
1718
1719 drm_gem_object_put(vm_resv_obj);
1720
1721 err = 0;
1722 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = true},
1723 err) {
1724 err = xe_vm_drm_exec_lock(vm, &exec);
1725 drm_exec_retry_on_contention(&exec);
1726
1727 if (IS_DGFX(xe) && xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)
1728 vm->flags |= XE_VM_FLAG_64K;
1729
1730 for_each_tile(tile, xe, id) {
1731 if (flags & XE_VM_FLAG_MIGRATION &&
1732 tile->id != XE_VM_FLAG_TILE_ID(flags))
1733 continue;
1734
1735 vm->pt_root[id] = xe_pt_create(vm, tile, xe->info.vm_max_level,
1736 &exec);
1737 if (IS_ERR(vm->pt_root[id])) {
1738 err = PTR_ERR(vm->pt_root[id]);
1739 vm->pt_root[id] = NULL;
1740 xe_vm_pt_destroy(vm);
1741 drm_exec_retry_on_contention(&exec);
1742 xe_validation_retry_on_oom(&ctx, &err);
1743 break;
1744 }
1745 }
1746 if (err)
1747 break;
1748
1749 if (xe_vm_has_scratch(vm)) {
1750 for_each_tile(tile, xe, id) {
1751 if (!vm->pt_root[id])
1752 continue;
1753
1754 err = xe_vm_create_scratch(xe, tile, vm, &exec);
1755 if (err) {
1756 xe_vm_free_scratch(vm);
1757 xe_vm_pt_destroy(vm);
1758 drm_exec_retry_on_contention(&exec);
1759 xe_validation_retry_on_oom(&ctx, &err);
1760 break;
1761 }
1762 }
1763 if (err)
1764 break;
1765 vm->batch_invalidate_tlb = true;
1766 }
1767
1768 if (vm->flags & XE_VM_FLAG_LR_MODE)
1769 vm->batch_invalidate_tlb = false;
1770
1771 /* Fill pt_root after allocating scratch tables */
1772 for_each_tile(tile, xe, id) {
1773 if (!vm->pt_root[id])
1774 continue;
1775
1776 xe_pt_populate_empty(tile, vm, vm->pt_root[id]);
1777 }
1778 }
1779 if (err)
1780 goto err_close;
1781
1782 /* Kernel migration VM shouldn't have a circular loop.. */
1783 if (!(flags & XE_VM_FLAG_MIGRATION)) {
1784 for_each_tile(tile, xe, id) {
1785 struct xe_exec_queue *q;
1786 u32 create_flags = EXEC_QUEUE_FLAG_VM;
1787
1788 if (!vm->pt_root[id])
1789 continue;
1790
1791 if (!xef) /* Not from userspace */
1792 create_flags |= EXEC_QUEUE_FLAG_KERNEL;
1793
1794 q = xe_exec_queue_create_bind(xe, tile, vm, create_flags, 0);
1795 if (IS_ERR(q)) {
1796 err = PTR_ERR(q);
1797 goto err_close;
1798 }
1799 vm->q[id] = q;
1800 }
1801 }
1802
1803 if (xef && xe->info.has_asid) {
1804 u32 asid;
1805
1806 down_write(&xe->usm.lock);
1807 err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, vm,
1808 XA_LIMIT(1, XE_MAX_ASID - 1),
1809 &xe->usm.next_asid, GFP_NOWAIT);
1810 up_write(&xe->usm.lock);
1811 if (err < 0)
1812 goto err_close;
1813
1814 vm->usm.asid = asid;
1815 }
1816
1817 trace_xe_vm_create(vm);
1818
1819 return vm;
1820
1821 err_close:
1822 xe_vm_close_and_put(vm);
1823 return ERR_PTR(err);
1824
1825 err_svm_fini:
1826 vm->size = 0; /* close the vm */
1827 if (flags & XE_VM_FLAG_FAULT_MODE)
1828 xe_svm_close(vm);
1829 xe_svm_fini(vm);
1830 err_no_resv:
1831 mutex_destroy(&vm->snap_mutex);
1832 for_each_tile(tile, xe, id)
1833 xe_range_fence_tree_fini(&vm->rftree[id]);
1834 ttm_lru_bulk_move_fini(&xe->ttm, &vm->lru_bulk_move);
1835 if (vm->xef)
1836 xe_file_put(vm->xef);
1837 kfree(vm);
1838 if (flags & XE_VM_FLAG_LR_MODE)
1839 xe_pm_runtime_put(xe);
1840 return ERR_PTR(err);
1841 }
1842
xe_vm_close(struct xe_vm * vm)1843 static void xe_vm_close(struct xe_vm *vm)
1844 {
1845 struct xe_device *xe = vm->xe;
1846 bool bound;
1847 int idx;
1848
1849 bound = drm_dev_enter(&xe->drm, &idx);
1850
1851 down_write(&vm->lock);
1852 if (xe_vm_in_fault_mode(vm))
1853 xe_svm_notifier_lock(vm);
1854
1855 vm->size = 0;
1856
1857 if (!((vm->flags & XE_VM_FLAG_MIGRATION))) {
1858 struct xe_tile *tile;
1859 struct xe_gt *gt;
1860 u8 id;
1861
1862 /* Wait for pending binds */
1863 dma_resv_wait_timeout(xe_vm_resv(vm),
1864 DMA_RESV_USAGE_BOOKKEEP,
1865 false, MAX_SCHEDULE_TIMEOUT);
1866
1867 if (bound) {
1868 for_each_tile(tile, xe, id)
1869 if (vm->pt_root[id])
1870 xe_pt_clear(xe, vm->pt_root[id]);
1871
1872 for_each_gt(gt, xe, id)
1873 xe_tlb_inval_vm(>->tlb_inval, vm);
1874 }
1875 }
1876
1877 if (xe_vm_in_fault_mode(vm))
1878 xe_svm_notifier_unlock(vm);
1879 up_write(&vm->lock);
1880
1881 if (bound)
1882 drm_dev_exit(idx);
1883 }
1884
xe_vm_close_and_put(struct xe_vm * vm)1885 void xe_vm_close_and_put(struct xe_vm *vm)
1886 {
1887 LIST_HEAD(contested);
1888 struct xe_device *xe = vm->xe;
1889 struct xe_tile *tile;
1890 struct xe_vma *vma, *next_vma;
1891 struct drm_gpuva *gpuva, *next;
1892 u8 id;
1893
1894 xe_assert(xe, !vm->preempt.num_exec_queues);
1895
1896 xe_vm_close(vm);
1897 if (xe_vm_in_preempt_fence_mode(vm)) {
1898 mutex_lock(&xe->rebind_resume_lock);
1899 list_del_init(&vm->preempt.pm_activate_link);
1900 mutex_unlock(&xe->rebind_resume_lock);
1901 flush_work(&vm->preempt.rebind_work);
1902 }
1903 if (xe_vm_in_fault_mode(vm))
1904 xe_svm_close(vm);
1905
1906 down_write(&vm->lock);
1907 for_each_tile(tile, xe, id) {
1908 if (vm->q[id]) {
1909 int i;
1910
1911 xe_exec_queue_last_fence_put(vm->q[id], vm);
1912 for_each_tlb_inval(i)
1913 xe_exec_queue_tlb_inval_last_fence_put(vm->q[id], vm, i);
1914 }
1915 }
1916 up_write(&vm->lock);
1917
1918 for_each_tile(tile, xe, id) {
1919 if (vm->q[id]) {
1920 xe_exec_queue_kill(vm->q[id]);
1921 xe_exec_queue_put(vm->q[id]);
1922 vm->q[id] = NULL;
1923 }
1924 }
1925
1926 down_write(&vm->lock);
1927 xe_vm_lock(vm, false);
1928 drm_gpuvm_for_each_va_safe(gpuva, next, &vm->gpuvm) {
1929 vma = gpuva_to_vma(gpuva);
1930
1931 if (xe_vma_has_no_bo(vma)) {
1932 xe_svm_notifier_lock(vm);
1933 vma->gpuva.flags |= XE_VMA_DESTROYED;
1934 xe_svm_notifier_unlock(vm);
1935 }
1936
1937 xe_vm_remove_vma(vm, vma);
1938
1939 /* easy case, remove from VMA? */
1940 if (xe_vma_has_no_bo(vma) || xe_vma_bo(vma)->vm) {
1941 list_del_init(&vma->combined_links.rebind);
1942 xe_vma_destroy(vma, NULL);
1943 continue;
1944 }
1945
1946 list_move_tail(&vma->combined_links.destroy, &contested);
1947 vma->gpuva.flags |= XE_VMA_DESTROYED;
1948 }
1949
1950 xe_vm_unlock(vm);
1951
1952 /*
1953 * Unlink and destroy all contested external-BO VMAs before destroying
1954 * the page tables. Otherwise, concurrent eviction holding only bo->resv
1955 * can walk the BO's VMAs and attempt to invalidate/zap page tables that
1956 * have already been freed.
1957 */
1958 list_for_each_entry_safe(vma, next_vma, &contested,
1959 combined_links.destroy) {
1960 list_del_init(&vma->combined_links.destroy);
1961 xe_vma_destroy_unlocked(vma);
1962 }
1963
1964 xe_vm_lock(vm, false);
1965 xe_vm_free_scratch(vm);
1966 xe_vm_pt_destroy(vm);
1967 xe_vm_unlock(vm);
1968
1969 xe_svm_fini(vm);
1970
1971 up_write(&vm->lock);
1972
1973 down_write(&xe->usm.lock);
1974 if (vm->usm.asid) {
1975 void *lookup;
1976
1977 xe_assert(xe, xe->info.has_asid);
1978 xe_assert(xe, !(vm->flags & XE_VM_FLAG_MIGRATION));
1979
1980 lookup = xa_erase(&xe->usm.asid_to_vm, vm->usm.asid);
1981 xe_assert(xe, lookup == vm);
1982 }
1983 up_write(&xe->usm.lock);
1984
1985 xe_vm_clear_fault_entries(vm);
1986
1987 for_each_tile(tile, xe, id)
1988 xe_range_fence_tree_fini(&vm->rftree[id]);
1989
1990 xe_vm_put(vm);
1991 }
1992
vm_destroy_work_func(struct work_struct * w)1993 static void vm_destroy_work_func(struct work_struct *w)
1994 {
1995 struct xe_vm *vm =
1996 container_of(w, struct xe_vm, destroy_work);
1997 struct xe_device *xe = vm->xe;
1998 struct xe_tile *tile;
1999 u8 id;
2000
2001 /* xe_vm_close_and_put was not called? */
2002 xe_assert(xe, !vm->size);
2003
2004 if (xe_vm_in_preempt_fence_mode(vm))
2005 flush_work(&vm->preempt.rebind_work);
2006
2007 mutex_destroy(&vm->snap_mutex);
2008
2009 if (vm->flags & XE_VM_FLAG_LR_MODE)
2010 xe_pm_runtime_put(xe);
2011
2012 for_each_tile(tile, xe, id)
2013 XE_WARN_ON(vm->pt_root[id]);
2014
2015 trace_xe_vm_free(vm);
2016
2017 ttm_lru_bulk_move_fini(&xe->ttm, &vm->lru_bulk_move);
2018
2019 if (vm->xef)
2020 xe_file_put(vm->xef);
2021
2022 kfree(vm);
2023 }
2024
xe_vm_free(struct drm_gpuvm * gpuvm)2025 static void xe_vm_free(struct drm_gpuvm *gpuvm)
2026 {
2027 struct xe_vm *vm = container_of(gpuvm, struct xe_vm, gpuvm);
2028
2029 /* To destroy the VM we need to be able to sleep */
2030 queue_work(system_dfl_wq, &vm->destroy_work);
2031 }
2032
xe_vm_lookup(struct xe_file * xef,u32 id)2033 struct xe_vm *xe_vm_lookup(struct xe_file *xef, u32 id)
2034 {
2035 struct xe_vm *vm;
2036
2037 mutex_lock(&xef->vm.lock);
2038 vm = xa_load(&xef->vm.xa, id);
2039 if (vm)
2040 xe_vm_get(vm);
2041 mutex_unlock(&xef->vm.lock);
2042
2043 return vm;
2044 }
2045
xe_vm_pdp4_descriptor(struct xe_vm * vm,struct xe_tile * tile)2046 u64 xe_vm_pdp4_descriptor(struct xe_vm *vm, struct xe_tile *tile)
2047 {
2048 return vm->pt_ops->pde_encode_bo(vm->pt_root[tile->id]->bo, 0);
2049 }
2050
2051 static struct xe_exec_queue *
to_wait_exec_queue(struct xe_vm * vm,struct xe_exec_queue * q)2052 to_wait_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
2053 {
2054 return q ? q : vm->q[0];
2055 }
2056
2057 static struct xe_user_fence *
find_ufence_get(struct xe_sync_entry * syncs,u32 num_syncs)2058 find_ufence_get(struct xe_sync_entry *syncs, u32 num_syncs)
2059 {
2060 unsigned int i;
2061
2062 for (i = 0; i < num_syncs; i++) {
2063 struct xe_sync_entry *e = &syncs[i];
2064
2065 if (xe_sync_is_ufence(e))
2066 return xe_sync_ufence_get(e);
2067 }
2068
2069 return NULL;
2070 }
2071
2072 #define ALL_DRM_XE_VM_CREATE_FLAGS (DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \
2073 DRM_XE_VM_CREATE_FLAG_LR_MODE | \
2074 DRM_XE_VM_CREATE_FLAG_FAULT_MODE | \
2075 DRM_XE_VM_CREATE_FLAG_NO_VM_OVERCOMMIT)
2076
xe_vm_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file)2077 int xe_vm_create_ioctl(struct drm_device *dev, void *data,
2078 struct drm_file *file)
2079 {
2080 struct xe_device *xe = to_xe_device(dev);
2081 struct xe_file *xef = to_xe_file(file);
2082 struct drm_xe_vm_create *args = data;
2083 struct xe_gt *wa_gt = xe_root_mmio_gt(xe);
2084 struct xe_vm *vm;
2085 u32 id;
2086 int err;
2087 u32 flags = 0;
2088
2089 if (XE_IOCTL_DBG(xe, args->extensions))
2090 return -EINVAL;
2091
2092 if (wa_gt && XE_GT_WA(wa_gt, 22014953428))
2093 args->flags |= DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE;
2094
2095 if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE &&
2096 !xe->info.has_usm))
2097 return -EINVAL;
2098
2099 if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2100 return -EINVAL;
2101
2102 if (XE_IOCTL_DBG(xe, args->flags & ~ALL_DRM_XE_VM_CREATE_FLAGS))
2103 return -EINVAL;
2104
2105 if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE &&
2106 args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE &&
2107 !xe->info.needs_scratch))
2108 return -EINVAL;
2109
2110 if (XE_IOCTL_DBG(xe, !(args->flags & DRM_XE_VM_CREATE_FLAG_LR_MODE) &&
2111 args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE))
2112 return -EINVAL;
2113
2114 if (XE_IOCTL_DBG(xe, !(args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE) &&
2115 args->flags & DRM_XE_VM_CREATE_FLAG_NO_VM_OVERCOMMIT))
2116 return -EINVAL;
2117
2118 if (args->flags & DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE)
2119 flags |= XE_VM_FLAG_SCRATCH_PAGE;
2120 if (args->flags & DRM_XE_VM_CREATE_FLAG_LR_MODE)
2121 flags |= XE_VM_FLAG_LR_MODE;
2122 if (args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE)
2123 flags |= XE_VM_FLAG_FAULT_MODE;
2124 if (args->flags & DRM_XE_VM_CREATE_FLAG_NO_VM_OVERCOMMIT)
2125 flags |= XE_VM_FLAG_NO_VM_OVERCOMMIT;
2126
2127 vm = xe_vm_create(xe, flags, xef);
2128 if (IS_ERR(vm))
2129 return PTR_ERR(vm);
2130
2131 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_MEM)
2132 /* Warning: Security issue - never enable by default */
2133 args->reserved[0] = xe_bo_main_addr(vm->pt_root[0]->bo, XE_PAGE_SIZE);
2134 #endif
2135
2136 /* user id alloc must always be last in ioctl to prevent UAF */
2137 err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
2138 if (err)
2139 goto err_close_and_put;
2140
2141 args->vm_id = id;
2142
2143 return 0;
2144
2145 err_close_and_put:
2146 xe_vm_close_and_put(vm);
2147
2148 return err;
2149 }
2150
xe_vm_destroy_ioctl(struct drm_device * dev,void * data,struct drm_file * file)2151 int xe_vm_destroy_ioctl(struct drm_device *dev, void *data,
2152 struct drm_file *file)
2153 {
2154 struct xe_device *xe = to_xe_device(dev);
2155 struct xe_file *xef = to_xe_file(file);
2156 struct drm_xe_vm_destroy *args = data;
2157 struct xe_vm *vm;
2158 int err = 0;
2159
2160 if (XE_IOCTL_DBG(xe, args->pad) ||
2161 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2162 return -EINVAL;
2163
2164 mutex_lock(&xef->vm.lock);
2165 vm = xa_load(&xef->vm.xa, args->vm_id);
2166 if (XE_IOCTL_DBG(xe, !vm))
2167 err = -ENOENT;
2168 else if (XE_IOCTL_DBG(xe, vm->preempt.num_exec_queues))
2169 err = -EBUSY;
2170 else
2171 xa_erase(&xef->vm.xa, args->vm_id);
2172 mutex_unlock(&xef->vm.lock);
2173
2174 if (!err)
2175 xe_vm_close_and_put(vm);
2176
2177 return err;
2178 }
2179
xe_vm_query_vmas(struct xe_vm * vm,u64 start,u64 end)2180 static int xe_vm_query_vmas(struct xe_vm *vm, u64 start, u64 end)
2181 {
2182 struct drm_gpuva *gpuva;
2183 u32 num_vmas = 0;
2184
2185 lockdep_assert_held(&vm->lock);
2186 drm_gpuvm_for_each_va_range(gpuva, &vm->gpuvm, start, end)
2187 num_vmas++;
2188
2189 return num_vmas;
2190 }
2191
get_mem_attrs(struct xe_vm * vm,u32 * num_vmas,u64 start,u64 end,struct drm_xe_mem_range_attr * attrs)2192 static int get_mem_attrs(struct xe_vm *vm, u32 *num_vmas, u64 start,
2193 u64 end, struct drm_xe_mem_range_attr *attrs)
2194 {
2195 struct drm_gpuva *gpuva;
2196 int i = 0;
2197
2198 lockdep_assert_held(&vm->lock);
2199
2200 drm_gpuvm_for_each_va_range(gpuva, &vm->gpuvm, start, end) {
2201 struct xe_vma *vma = gpuva_to_vma(gpuva);
2202
2203 if (i == *num_vmas)
2204 return -ENOSPC;
2205
2206 attrs[i].start = xe_vma_start(vma);
2207 attrs[i].end = xe_vma_end(vma);
2208 attrs[i].atomic.val = vma->attr.atomic_access;
2209 attrs[i].pat_index.val = vma->attr.pat_index;
2210 attrs[i].preferred_mem_loc.devmem_fd = vma->attr.preferred_loc.devmem_fd;
2211 attrs[i].preferred_mem_loc.migration_policy =
2212 vma->attr.preferred_loc.migration_policy;
2213
2214 i++;
2215 }
2216
2217 *num_vmas = i;
2218 return 0;
2219 }
2220
xe_vm_query_vmas_attrs_ioctl(struct drm_device * dev,void * data,struct drm_file * file)2221 int xe_vm_query_vmas_attrs_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2222 {
2223 struct xe_device *xe = to_xe_device(dev);
2224 struct xe_file *xef = to_xe_file(file);
2225 struct drm_xe_mem_range_attr *mem_attrs;
2226 struct drm_xe_vm_query_mem_range_attr *args = data;
2227 u64 __user *attrs_user = u64_to_user_ptr(args->vector_of_mem_attr);
2228 struct xe_vm *vm;
2229 int err = 0;
2230
2231 if (XE_IOCTL_DBG(xe,
2232 ((args->num_mem_ranges == 0 &&
2233 (attrs_user || args->sizeof_mem_range_attr != 0)) ||
2234 (args->num_mem_ranges > 0 &&
2235 (!attrs_user ||
2236 args->sizeof_mem_range_attr !=
2237 sizeof(struct drm_xe_mem_range_attr))))))
2238 return -EINVAL;
2239
2240 vm = xe_vm_lookup(xef, args->vm_id);
2241 if (XE_IOCTL_DBG(xe, !vm))
2242 return -EINVAL;
2243
2244 err = down_read_interruptible(&vm->lock);
2245 if (err)
2246 goto put_vm;
2247
2248 attrs_user = u64_to_user_ptr(args->vector_of_mem_attr);
2249
2250 if (args->num_mem_ranges == 0 && !attrs_user) {
2251 args->num_mem_ranges = xe_vm_query_vmas(vm, args->start, args->start + args->range);
2252 args->sizeof_mem_range_attr = sizeof(struct drm_xe_mem_range_attr);
2253 goto unlock_vm;
2254 }
2255
2256 mem_attrs = kvmalloc_array(args->num_mem_ranges, args->sizeof_mem_range_attr,
2257 GFP_KERNEL | __GFP_ACCOUNT |
2258 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2259 if (!mem_attrs) {
2260 err = args->num_mem_ranges > 1 ? -ENOBUFS : -ENOMEM;
2261 goto unlock_vm;
2262 }
2263
2264 memset(mem_attrs, 0, args->num_mem_ranges * args->sizeof_mem_range_attr);
2265 err = get_mem_attrs(vm, &args->num_mem_ranges, args->start,
2266 args->start + args->range, mem_attrs);
2267 if (err)
2268 goto free_mem_attrs;
2269
2270 err = copy_to_user(attrs_user, mem_attrs,
2271 args->sizeof_mem_range_attr * args->num_mem_ranges);
2272 if (err)
2273 err = -EFAULT;
2274
2275 free_mem_attrs:
2276 kvfree(mem_attrs);
2277 unlock_vm:
2278 up_read(&vm->lock);
2279 put_vm:
2280 xe_vm_put(vm);
2281 return err;
2282 }
2283
vma_matches(struct xe_vma * vma,u64 page_addr)2284 static bool vma_matches(struct xe_vma *vma, u64 page_addr)
2285 {
2286 if (page_addr > xe_vma_end(vma) - 1 ||
2287 page_addr + SZ_4K - 1 < xe_vma_start(vma))
2288 return false;
2289
2290 return true;
2291 }
2292
2293 /**
2294 * xe_vm_find_vma_by_addr() - Find a VMA by its address
2295 *
2296 * @vm: the xe_vm the vma belongs to
2297 * @page_addr: address to look up
2298 */
xe_vm_find_vma_by_addr(struct xe_vm * vm,u64 page_addr)2299 struct xe_vma *xe_vm_find_vma_by_addr(struct xe_vm *vm, u64 page_addr)
2300 {
2301 struct xe_vma *vma = NULL;
2302
2303 if (vm->usm.last_fault_vma) { /* Fast lookup */
2304 if (vma_matches(vm->usm.last_fault_vma, page_addr))
2305 vma = vm->usm.last_fault_vma;
2306 }
2307 if (!vma)
2308 vma = xe_vm_find_overlapping_vma(vm, page_addr, SZ_4K);
2309
2310 return vma;
2311 }
2312
2313 static const u32 region_to_mem_type[] = {
2314 XE_PL_TT,
2315 XE_PL_VRAM0,
2316 XE_PL_VRAM1,
2317 };
2318
prep_vma_destroy(struct xe_vm * vm,struct xe_vma * vma,bool post_commit)2319 static void prep_vma_destroy(struct xe_vm *vm, struct xe_vma *vma,
2320 bool post_commit)
2321 {
2322 xe_svm_notifier_lock(vm);
2323 vma->gpuva.flags |= XE_VMA_DESTROYED;
2324 xe_svm_notifier_unlock(vm);
2325 if (post_commit)
2326 xe_vm_remove_vma(vm, vma);
2327 }
2328
2329 #undef ULL
2330 #define ULL unsigned long long
2331
2332 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)
print_op(struct xe_device * xe,struct drm_gpuva_op * op)2333 static void print_op(struct xe_device *xe, struct drm_gpuva_op *op)
2334 {
2335 struct xe_vma *vma;
2336
2337 switch (op->op) {
2338 case DRM_GPUVA_OP_MAP:
2339 vm_dbg(&xe->drm, "MAP: addr=0x%016llx, range=0x%016llx",
2340 (ULL)op->map.va.addr, (ULL)op->map.va.range);
2341 break;
2342 case DRM_GPUVA_OP_REMAP:
2343 vma = gpuva_to_vma(op->remap.unmap->va);
2344 vm_dbg(&xe->drm, "REMAP:UNMAP: addr=0x%016llx, range=0x%016llx, keep=%d",
2345 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma),
2346 op->remap.unmap->keep ? 1 : 0);
2347 if (op->remap.prev)
2348 vm_dbg(&xe->drm,
2349 "REMAP:PREV: addr=0x%016llx, range=0x%016llx",
2350 (ULL)op->remap.prev->va.addr,
2351 (ULL)op->remap.prev->va.range);
2352 if (op->remap.next)
2353 vm_dbg(&xe->drm,
2354 "REMAP:NEXT: addr=0x%016llx, range=0x%016llx",
2355 (ULL)op->remap.next->va.addr,
2356 (ULL)op->remap.next->va.range);
2357 break;
2358 case DRM_GPUVA_OP_UNMAP:
2359 vma = gpuva_to_vma(op->unmap.va);
2360 vm_dbg(&xe->drm, "UNMAP: addr=0x%016llx, range=0x%016llx, keep=%d",
2361 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma),
2362 op->unmap.keep ? 1 : 0);
2363 break;
2364 case DRM_GPUVA_OP_PREFETCH:
2365 vma = gpuva_to_vma(op->prefetch.va);
2366 vm_dbg(&xe->drm, "PREFETCH: addr=0x%016llx, range=0x%016llx",
2367 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma));
2368 break;
2369 default:
2370 drm_warn(&xe->drm, "NOT POSSIBLE\n");
2371 }
2372 }
2373 #else
print_op(struct xe_device * xe,struct drm_gpuva_op * op)2374 static void print_op(struct xe_device *xe, struct drm_gpuva_op *op)
2375 {
2376 }
2377 #endif
2378
__xe_vm_needs_clear_scratch_pages(struct xe_vm * vm,u32 bind_flags)2379 static bool __xe_vm_needs_clear_scratch_pages(struct xe_vm *vm, u32 bind_flags)
2380 {
2381 if (!xe_vm_in_fault_mode(vm))
2382 return false;
2383
2384 if (!xe_vm_has_scratch(vm))
2385 return false;
2386
2387 if (bind_flags & DRM_XE_VM_BIND_FLAG_IMMEDIATE)
2388 return false;
2389
2390 return true;
2391 }
2392
xe_svm_prefetch_gpuva_ops_fini(struct drm_gpuva_ops * ops)2393 static void xe_svm_prefetch_gpuva_ops_fini(struct drm_gpuva_ops *ops)
2394 {
2395 struct drm_gpuva_op *__op;
2396
2397 drm_gpuva_for_each_op(__op, ops) {
2398 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2399
2400 xe_vma_svm_prefetch_op_fini(op);
2401 }
2402 }
2403
2404 /*
2405 * Create operations list from IOCTL arguments, setup operations fields so parse
2406 * and commit steps are decoupled from IOCTL arguments. This step can fail.
2407 */
2408 static struct drm_gpuva_ops *
vm_bind_ioctl_ops_create(struct xe_vm * vm,struct xe_vma_ops * vops,struct xe_bo * bo,u64 bo_offset_or_userptr,u64 addr,u64 range,u32 operation,u32 flags,u32 prefetch_region,u16 pat_index)2409 vm_bind_ioctl_ops_create(struct xe_vm *vm, struct xe_vma_ops *vops,
2410 struct xe_bo *bo, u64 bo_offset_or_userptr,
2411 u64 addr, u64 range,
2412 u32 operation, u32 flags,
2413 u32 prefetch_region, u16 pat_index)
2414 {
2415 struct drm_gem_object *obj = bo ? &bo->ttm.base : NULL;
2416 struct drm_gpuva_ops *ops;
2417 struct drm_gpuva_op *__op;
2418 struct drm_gpuvm_bo *vm_bo;
2419 u64 range_start = addr;
2420 u64 range_end = addr + range;
2421 int err;
2422
2423 lockdep_assert_held_write(&vm->lock);
2424
2425 vm_dbg(&vm->xe->drm,
2426 "op=%d, addr=0x%016llx, range=0x%016llx, bo_offset_or_userptr=0x%016llx",
2427 operation, (ULL)addr, (ULL)range,
2428 (ULL)bo_offset_or_userptr);
2429
2430 switch (operation) {
2431 case DRM_XE_VM_BIND_OP_MAP:
2432 if (flags & DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR) {
2433 xe_vm_find_cpu_addr_mirror_vma_range(vm, &range_start, &range_end);
2434 vops->flags |= XE_VMA_OPS_FLAG_ALLOW_SVM_UNMAP;
2435 }
2436
2437 fallthrough;
2438 case DRM_XE_VM_BIND_OP_MAP_USERPTR: {
2439 struct drm_gpuvm_map_req map_req = {
2440 .map.va.addr = range_start,
2441 .map.va.range = range_end - range_start,
2442 .map.gem.obj = obj,
2443 .map.gem.offset = bo_offset_or_userptr,
2444 };
2445
2446 ops = drm_gpuvm_sm_map_ops_create(&vm->gpuvm, &map_req);
2447 break;
2448 }
2449 case DRM_XE_VM_BIND_OP_UNMAP:
2450 ops = drm_gpuvm_sm_unmap_ops_create(&vm->gpuvm, addr, range);
2451 break;
2452 case DRM_XE_VM_BIND_OP_PREFETCH:
2453 ops = drm_gpuvm_prefetch_ops_create(&vm->gpuvm, addr, range);
2454 break;
2455 case DRM_XE_VM_BIND_OP_UNMAP_ALL:
2456 xe_assert(vm->xe, bo);
2457
2458 err = xe_bo_lock(bo, true);
2459 if (err)
2460 return ERR_PTR(err);
2461
2462 vm_bo = drm_gpuvm_bo_obtain_locked(&vm->gpuvm, obj);
2463 if (IS_ERR(vm_bo)) {
2464 xe_bo_unlock(bo);
2465 return ERR_CAST(vm_bo);
2466 }
2467
2468 ops = drm_gpuvm_bo_unmap_ops_create(vm_bo);
2469 drm_gpuvm_bo_put(vm_bo);
2470 xe_bo_unlock(bo);
2471 break;
2472 default:
2473 drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
2474 ops = ERR_PTR(-EINVAL);
2475 }
2476 if (IS_ERR(ops))
2477 return ops;
2478
2479 drm_gpuva_for_each_op(__op, ops) {
2480 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2481
2482 if (__op->op == DRM_GPUVA_OP_MAP) {
2483 op->map.immediate =
2484 flags & DRM_XE_VM_BIND_FLAG_IMMEDIATE;
2485 if (flags & DRM_XE_VM_BIND_FLAG_READONLY)
2486 op->map.vma_flags |= XE_VMA_READ_ONLY;
2487 if (flags & DRM_XE_VM_BIND_FLAG_NULL)
2488 op->map.vma_flags |= DRM_GPUVA_SPARSE;
2489 if (flags & DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR)
2490 op->map.vma_flags |= XE_VMA_SYSTEM_ALLOCATOR;
2491 if (flags & DRM_XE_VM_BIND_FLAG_DUMPABLE)
2492 op->map.vma_flags |= XE_VMA_DUMPABLE;
2493 if (flags & DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET)
2494 op->map.vma_flags |= XE_VMA_MADV_AUTORESET;
2495 op->map.request_decompress = flags & DRM_XE_VM_BIND_FLAG_DECOMPRESS;
2496 op->map.pat_index = pat_index;
2497 op->map.invalidate_on_bind =
2498 __xe_vm_needs_clear_scratch_pages(vm, flags);
2499 } else if (__op->op == DRM_GPUVA_OP_PREFETCH) {
2500 struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
2501 struct xe_tile *tile;
2502 struct xe_svm_range *svm_range;
2503 struct drm_gpusvm_ctx ctx = {};
2504 struct drm_pagemap *dpagemap = NULL;
2505 u8 id, tile_mask = 0;
2506 u32 i;
2507
2508 if (!xe_vma_is_cpu_addr_mirror(vma)) {
2509 op->prefetch.region = prefetch_region;
2510 break;
2511 }
2512
2513 ctx.read_only = xe_vma_read_only(vma);
2514 ctx.devmem_possible = IS_DGFX(vm->xe) &&
2515 IS_ENABLED(CONFIG_DRM_XE_PAGEMAP);
2516
2517 for_each_tile(tile, vm->xe, id)
2518 tile_mask |= 0x1 << id;
2519
2520 xa_init_flags(&op->prefetch_range.range, XA_FLAGS_ALLOC);
2521 op->prefetch_range.ranges_count = 0;
2522
2523 if (prefetch_region == DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC) {
2524 dpagemap = xe_vma_resolve_pagemap(vma,
2525 xe_device_get_root_tile(vm->xe));
2526 } else if (prefetch_region) {
2527 tile = &vm->xe->tiles[region_to_mem_type[prefetch_region] -
2528 XE_PL_VRAM0];
2529 dpagemap = xe_tile_local_pagemap(tile);
2530 }
2531
2532 op->prefetch_range.dpagemap = dpagemap;
2533 alloc_next_range:
2534 svm_range = xe_svm_range_find_or_insert(vm, addr, vma, &ctx);
2535
2536 if (PTR_ERR(svm_range) == -ENOENT) {
2537 u64 ret = xe_svm_find_vma_start(vm, addr, range_end, vma);
2538
2539 addr = ret == ULONG_MAX ? 0 : ret;
2540 if (addr)
2541 goto alloc_next_range;
2542 else
2543 goto print_op_label;
2544 }
2545
2546 if (IS_ERR(svm_range)) {
2547 err = PTR_ERR(svm_range);
2548 goto unwind_prefetch_ops;
2549 }
2550
2551 if (xe_svm_range_validate(vm, svm_range, tile_mask, dpagemap)) {
2552 xe_svm_range_debug(svm_range, "PREFETCH - RANGE IS VALID");
2553 goto check_next_range;
2554 }
2555
2556 err = xa_alloc(&op->prefetch_range.range,
2557 &i, svm_range, xa_limit_32b,
2558 GFP_KERNEL);
2559
2560 if (err)
2561 goto unwind_prefetch_ops;
2562
2563 op->prefetch_range.ranges_count++;
2564 vops->flags |= XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH;
2565 xe_svm_range_debug(svm_range, "PREFETCH - RANGE CREATED");
2566 check_next_range:
2567 if (range_end > xe_svm_range_end(svm_range) &&
2568 xe_svm_range_end(svm_range) < xe_vma_end(vma)) {
2569 addr = xe_svm_range_end(svm_range);
2570 goto alloc_next_range;
2571 }
2572 }
2573 print_op_label:
2574 print_op(vm->xe, __op);
2575 }
2576
2577 return ops;
2578
2579 unwind_prefetch_ops:
2580 xe_svm_prefetch_gpuva_ops_fini(ops);
2581 drm_gpuva_ops_free(&vm->gpuvm, ops);
2582 return ERR_PTR(err);
2583 }
2584
2585 ALLOW_ERROR_INJECTION(vm_bind_ioctl_ops_create, ERRNO);
2586
new_vma(struct xe_vm * vm,struct drm_gpuva_op_map * op,struct xe_vma_mem_attr * attr,unsigned int flags)2587 static struct xe_vma *new_vma(struct xe_vm *vm, struct drm_gpuva_op_map *op,
2588 struct xe_vma_mem_attr *attr, unsigned int flags)
2589 {
2590 struct xe_bo *bo = op->gem.obj ? gem_to_xe_bo(op->gem.obj) : NULL;
2591 struct xe_validation_ctx ctx;
2592 struct drm_exec exec;
2593 struct xe_vma *vma;
2594 int err = 0;
2595
2596 lockdep_assert_held_write(&vm->lock);
2597
2598 if (bo) {
2599 err = 0;
2600 xe_validation_guard(&ctx, &vm->xe->val, &exec,
2601 (struct xe_val_flags) {.interruptible = true}, err) {
2602 if (!bo->vm) {
2603 err = drm_exec_lock_obj(&exec, xe_vm_obj(vm));
2604 drm_exec_retry_on_contention(&exec);
2605 }
2606 if (!err) {
2607 err = drm_exec_lock_obj(&exec, &bo->ttm.base);
2608 drm_exec_retry_on_contention(&exec);
2609 }
2610 if (err)
2611 return ERR_PTR(err);
2612
2613 vma = xe_vma_create(vm, bo, op->gem.offset,
2614 op->va.addr, op->va.addr +
2615 op->va.range - 1, attr, flags);
2616 if (IS_ERR(vma))
2617 return vma;
2618
2619 if (!bo->vm) {
2620 err = add_preempt_fences(vm, bo);
2621 if (err) {
2622 prep_vma_destroy(vm, vma, false);
2623 xe_vma_destroy(vma, NULL);
2624 }
2625 }
2626 }
2627 if (err)
2628 return ERR_PTR(err);
2629 } else {
2630 vma = xe_vma_create(vm, NULL, op->gem.offset,
2631 op->va.addr, op->va.addr +
2632 op->va.range - 1, attr, flags);
2633 if (IS_ERR(vma))
2634 return vma;
2635
2636 if (xe_vma_is_userptr(vma)) {
2637 err = xe_vma_userptr_pin_pages(to_userptr_vma(vma));
2638 /*
2639 * -EBUSY has dedicated meaning that a user fence
2640 * attached to the VMA is busy, in practice
2641 * xe_vma_userptr_pin_pages can only fail with -EBUSY if
2642 * we are low on memory so convert this to -ENOMEM.
2643 */
2644 if (err == -EBUSY)
2645 err = -ENOMEM;
2646 }
2647 }
2648 if (err) {
2649 prep_vma_destroy(vm, vma, false);
2650 xe_vma_destroy_unlocked(vma);
2651 vma = ERR_PTR(err);
2652 }
2653
2654 return vma;
2655 }
2656
xe_vma_max_pte_size(struct xe_vma * vma)2657 static u64 xe_vma_max_pte_size(struct xe_vma *vma)
2658 {
2659 if (vma->gpuva.flags & XE_VMA_PTE_1G)
2660 return SZ_1G;
2661 else if (vma->gpuva.flags & (XE_VMA_PTE_2M | XE_VMA_PTE_COMPACT))
2662 return SZ_2M;
2663 else if (vma->gpuva.flags & XE_VMA_PTE_64K)
2664 return SZ_64K;
2665 else if (vma->gpuva.flags & XE_VMA_PTE_4K)
2666 return SZ_4K;
2667
2668 return SZ_1G; /* Uninitialized, used max size */
2669 }
2670
xe_vma_set_pte_size(struct xe_vma * vma,u64 size)2671 static void xe_vma_set_pte_size(struct xe_vma *vma, u64 size)
2672 {
2673 switch (size) {
2674 case SZ_1G:
2675 vma->gpuva.flags |= XE_VMA_PTE_1G;
2676 break;
2677 case SZ_2M:
2678 vma->gpuva.flags |= XE_VMA_PTE_2M;
2679 break;
2680 case SZ_64K:
2681 vma->gpuva.flags |= XE_VMA_PTE_64K;
2682 break;
2683 case SZ_4K:
2684 vma->gpuva.flags |= XE_VMA_PTE_4K;
2685 break;
2686 }
2687 }
2688
xe_vma_op_commit(struct xe_vm * vm,struct xe_vma_op * op)2689 static int xe_vma_op_commit(struct xe_vm *vm, struct xe_vma_op *op)
2690 {
2691 int err = 0;
2692
2693 lockdep_assert_held_write(&vm->lock);
2694
2695 switch (op->base.op) {
2696 case DRM_GPUVA_OP_MAP:
2697 err |= xe_vm_insert_vma(vm, op->map.vma);
2698 if (!err)
2699 op->flags |= XE_VMA_OP_COMMITTED;
2700 break;
2701 case DRM_GPUVA_OP_REMAP:
2702 {
2703 u8 tile_present =
2704 gpuva_to_vma(op->base.remap.unmap->va)->tile_present;
2705
2706 prep_vma_destroy(vm, gpuva_to_vma(op->base.remap.unmap->va),
2707 true);
2708 op->flags |= XE_VMA_OP_COMMITTED;
2709
2710 if (op->remap.prev) {
2711 err |= xe_vm_insert_vma(vm, op->remap.prev);
2712 if (!err)
2713 op->flags |= XE_VMA_OP_PREV_COMMITTED;
2714 if (!err && op->remap.skip_prev) {
2715 op->remap.prev->tile_present =
2716 tile_present;
2717 }
2718 }
2719 if (op->remap.next) {
2720 err |= xe_vm_insert_vma(vm, op->remap.next);
2721 if (!err)
2722 op->flags |= XE_VMA_OP_NEXT_COMMITTED;
2723 if (!err && op->remap.skip_next) {
2724 op->remap.next->tile_present =
2725 tile_present;
2726 }
2727 }
2728
2729 /*
2730 * Adjust for partial unbind after removing VMA from VM. In case
2731 * of unwind we might need to undo this later.
2732 */
2733 if (!err) {
2734 op->base.remap.unmap->va->va.addr = op->remap.start;
2735 op->base.remap.unmap->va->va.range = op->remap.range;
2736 }
2737 break;
2738 }
2739 case DRM_GPUVA_OP_UNMAP:
2740 prep_vma_destroy(vm, gpuva_to_vma(op->base.unmap.va), true);
2741 op->flags |= XE_VMA_OP_COMMITTED;
2742 break;
2743 case DRM_GPUVA_OP_PREFETCH:
2744 op->flags |= XE_VMA_OP_COMMITTED;
2745 break;
2746 default:
2747 drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
2748 }
2749
2750 return err;
2751 }
2752
2753 /**
2754 * xe_vma_has_default_mem_attrs - Check if a VMA has default memory attributes
2755 * @vma: Pointer to the xe_vma structure to check
2756 *
2757 * This function determines whether the given VMA (Virtual Memory Area)
2758 * has its memory attributes set to their default values. Specifically,
2759 * it checks the following conditions:
2760 *
2761 * - `atomic_access` is `DRM_XE_VMA_ATOMIC_UNDEFINED`
2762 * - `pat_index` is equal to `default_pat_index`
2763 * - `preferred_loc.devmem_fd` is `DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE`
2764 * - `preferred_loc.migration_policy` is `DRM_XE_MIGRATE_ALL_PAGES`
2765 *
2766 * Return: true if all attributes are at their default values, false otherwise.
2767 */
xe_vma_has_default_mem_attrs(struct xe_vma * vma)2768 bool xe_vma_has_default_mem_attrs(struct xe_vma *vma)
2769 {
2770 return (vma->attr.atomic_access == DRM_XE_ATOMIC_UNDEFINED &&
2771 vma->attr.pat_index == vma->attr.default_pat_index &&
2772 vma->attr.preferred_loc.devmem_fd == DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE &&
2773 vma->attr.preferred_loc.migration_policy == DRM_XE_MIGRATE_ALL_PAGES);
2774 }
2775
vm_bind_ioctl_ops_parse(struct xe_vm * vm,struct drm_gpuva_ops * ops,struct xe_vma_ops * vops)2776 static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct drm_gpuva_ops *ops,
2777 struct xe_vma_ops *vops)
2778 {
2779 struct xe_device *xe = vm->xe;
2780 struct drm_gpuva_op *__op;
2781 struct xe_tile *tile;
2782 u8 id, tile_mask = 0;
2783 int err = 0;
2784
2785 lockdep_assert_held_write(&vm->lock);
2786
2787 for_each_tile(tile, vm->xe, id)
2788 tile_mask |= 0x1 << id;
2789
2790 drm_gpuva_for_each_op(__op, ops) {
2791 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2792 struct xe_vma *vma;
2793 unsigned int flags = 0;
2794
2795 INIT_LIST_HEAD(&op->link);
2796 list_add_tail(&op->link, &vops->list);
2797 op->tile_mask = tile_mask;
2798
2799 switch (op->base.op) {
2800 case DRM_GPUVA_OP_MAP:
2801 {
2802 struct xe_vma_mem_attr default_attr = {
2803 .preferred_loc = {
2804 .devmem_fd = DRM_XE_PREFERRED_LOC_DEFAULT_DEVICE,
2805 .migration_policy = DRM_XE_MIGRATE_ALL_PAGES,
2806 },
2807 .atomic_access = DRM_XE_ATOMIC_UNDEFINED,
2808 .default_pat_index = op->map.pat_index,
2809 .pat_index = op->map.pat_index,
2810 .purgeable_state = XE_MADV_PURGEABLE_WILLNEED,
2811 };
2812
2813 flags |= op->map.vma_flags & XE_VMA_CREATE_MASK;
2814
2815 vma = new_vma(vm, &op->base.map, &default_attr,
2816 flags);
2817 if (IS_ERR(vma))
2818 return PTR_ERR(vma);
2819
2820 op->map.vma = vma;
2821 if (((op->map.immediate || !xe_vm_in_fault_mode(vm)) &&
2822 !(op->map.vma_flags & XE_VMA_SYSTEM_ALLOCATOR)) ||
2823 op->map.invalidate_on_bind)
2824 xe_vma_ops_incr_pt_update_ops(vops,
2825 op->tile_mask, 1);
2826 break;
2827 }
2828 case DRM_GPUVA_OP_REMAP:
2829 {
2830 struct xe_vma *old =
2831 gpuva_to_vma(op->base.remap.unmap->va);
2832 bool skip = xe_vma_is_cpu_addr_mirror(old);
2833 u64 start = xe_vma_start(old), end = xe_vma_end(old);
2834 int num_remap_ops = 0;
2835
2836 if (op->base.remap.prev)
2837 start = op->base.remap.prev->va.addr +
2838 op->base.remap.prev->va.range;
2839 if (op->base.remap.next)
2840 end = op->base.remap.next->va.addr;
2841
2842 if (xe_vma_is_cpu_addr_mirror(old) &&
2843 xe_svm_has_mapping(vm, start, end)) {
2844 if (vops->flags & XE_VMA_OPS_FLAG_MADVISE)
2845 xe_svm_unmap_address_range(vm, start, end);
2846 else
2847 return -EBUSY;
2848 }
2849
2850 op->remap.start = xe_vma_start(old);
2851 op->remap.range = xe_vma_size(old);
2852 op->remap.old_start = op->remap.start;
2853 op->remap.old_range = op->remap.range;
2854
2855 flags |= op->base.remap.unmap->va->flags & XE_VMA_CREATE_MASK;
2856 if (op->base.remap.prev) {
2857 vma = new_vma(vm, op->base.remap.prev,
2858 &old->attr, flags);
2859 if (IS_ERR(vma))
2860 return PTR_ERR(vma);
2861
2862 op->remap.prev = vma;
2863
2864 /*
2865 * Userptr creates a new SG mapping so
2866 * we must also rebind.
2867 */
2868 op->remap.skip_prev = skip ||
2869 (!xe_vma_is_userptr(old) &&
2870 IS_ALIGNED(xe_vma_end(vma),
2871 xe_vma_max_pte_size(old)));
2872 if (op->remap.skip_prev) {
2873 xe_vma_set_pte_size(vma, xe_vma_max_pte_size(old));
2874 op->remap.range -=
2875 xe_vma_end(vma) -
2876 xe_vma_start(old);
2877 op->remap.start = xe_vma_end(vma);
2878 vm_dbg(&xe->drm, "REMAP:SKIP_PREV: addr=0x%016llx, range=0x%016llx",
2879 (ULL)op->remap.start,
2880 (ULL)op->remap.range);
2881 } else {
2882 num_remap_ops++;
2883 }
2884 }
2885
2886 if (op->base.remap.next) {
2887 vma = new_vma(vm, op->base.remap.next,
2888 &old->attr, flags);
2889 if (IS_ERR(vma))
2890 return PTR_ERR(vma);
2891
2892 op->remap.next = vma;
2893
2894 /*
2895 * Userptr creates a new SG mapping so
2896 * we must also rebind.
2897 */
2898 op->remap.skip_next = skip ||
2899 (!xe_vma_is_userptr(old) &&
2900 IS_ALIGNED(xe_vma_start(vma),
2901 xe_vma_max_pte_size(old)));
2902 if (op->remap.skip_next) {
2903 xe_vma_set_pte_size(vma, xe_vma_max_pte_size(old));
2904 op->remap.range -=
2905 xe_vma_end(old) -
2906 xe_vma_start(vma);
2907 vm_dbg(&xe->drm, "REMAP:SKIP_NEXT: addr=0x%016llx, range=0x%016llx",
2908 (ULL)op->remap.start,
2909 (ULL)op->remap.range);
2910 } else {
2911 num_remap_ops++;
2912 }
2913 }
2914 if (!skip)
2915 num_remap_ops++;
2916
2917 xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, num_remap_ops);
2918 break;
2919 }
2920 case DRM_GPUVA_OP_UNMAP:
2921 vma = gpuva_to_vma(op->base.unmap.va);
2922
2923 if (xe_vma_is_cpu_addr_mirror(vma) &&
2924 xe_svm_has_mapping(vm, xe_vma_start(vma),
2925 xe_vma_end(vma)) &&
2926 !(vops->flags & XE_VMA_OPS_FLAG_ALLOW_SVM_UNMAP))
2927 return -EBUSY;
2928
2929 if (!xe_vma_is_cpu_addr_mirror(vma))
2930 xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, 1);
2931 break;
2932 case DRM_GPUVA_OP_PREFETCH:
2933 vma = gpuva_to_vma(op->base.prefetch.va);
2934
2935 if (xe_vma_is_userptr(vma)) {
2936 err = xe_vma_userptr_pin_pages(to_userptr_vma(vma));
2937 if (err)
2938 return err;
2939 }
2940
2941 if (xe_vma_is_cpu_addr_mirror(vma))
2942 xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask,
2943 op->prefetch_range.ranges_count);
2944 else
2945 xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, 1);
2946
2947 break;
2948 default:
2949 drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
2950 }
2951
2952 err = xe_vma_op_commit(vm, op);
2953 if (err)
2954 return err;
2955 }
2956
2957 return 0;
2958 }
2959
xe_vma_op_unwind(struct xe_vm * vm,struct xe_vma_op * op,bool post_commit,bool prev_post_commit,bool next_post_commit)2960 static void xe_vma_op_unwind(struct xe_vm *vm, struct xe_vma_op *op,
2961 bool post_commit, bool prev_post_commit,
2962 bool next_post_commit)
2963 {
2964 lockdep_assert_held_write(&vm->lock);
2965
2966 switch (op->base.op) {
2967 case DRM_GPUVA_OP_MAP:
2968 if (op->map.vma) {
2969 prep_vma_destroy(vm, op->map.vma, post_commit);
2970 xe_vma_destroy_unlocked(op->map.vma);
2971 }
2972 break;
2973 case DRM_GPUVA_OP_UNMAP:
2974 {
2975 struct xe_vma *vma = gpuva_to_vma(op->base.unmap.va);
2976
2977 if (vma) {
2978 xe_svm_notifier_lock(vm);
2979 vma->gpuva.flags &= ~XE_VMA_DESTROYED;
2980 xe_svm_notifier_unlock(vm);
2981 if (post_commit)
2982 xe_vm_insert_vma(vm, vma);
2983 }
2984 break;
2985 }
2986 case DRM_GPUVA_OP_REMAP:
2987 {
2988 struct xe_vma *vma = gpuva_to_vma(op->base.remap.unmap->va);
2989
2990 if (op->remap.prev) {
2991 prep_vma_destroy(vm, op->remap.prev, prev_post_commit);
2992 xe_vma_destroy_unlocked(op->remap.prev);
2993 }
2994 if (op->remap.next) {
2995 prep_vma_destroy(vm, op->remap.next, next_post_commit);
2996 xe_vma_destroy_unlocked(op->remap.next);
2997 }
2998 if (vma) {
2999 xe_svm_notifier_lock(vm);
3000 vma->gpuva.flags &= ~XE_VMA_DESTROYED;
3001 xe_svm_notifier_unlock(vm);
3002 if (post_commit) {
3003 /*
3004 * Restore the old va range, in case of the
3005 * prev/next skip optimisation. Otherwise what
3006 * we re-insert here could be smaller than the
3007 * original range.
3008 */
3009 op->base.remap.unmap->va->va.addr =
3010 op->remap.old_start;
3011 op->base.remap.unmap->va->va.range =
3012 op->remap.old_range;
3013 xe_vm_insert_vma(vm, vma);
3014 }
3015 }
3016 break;
3017 }
3018 case DRM_GPUVA_OP_PREFETCH:
3019 /* Nothing to do */
3020 break;
3021 default:
3022 drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
3023 }
3024 }
3025
vm_bind_ioctl_ops_unwind(struct xe_vm * vm,struct drm_gpuva_ops ** ops,int num_ops_list)3026 static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,
3027 struct drm_gpuva_ops **ops,
3028 int num_ops_list)
3029 {
3030 int i;
3031
3032 for (i = num_ops_list - 1; i >= 0; --i) {
3033 struct drm_gpuva_ops *__ops = ops[i];
3034 struct drm_gpuva_op *__op;
3035
3036 if (!__ops)
3037 continue;
3038
3039 drm_gpuva_for_each_op_reverse(__op, __ops) {
3040 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
3041
3042 xe_vma_op_unwind(vm, op,
3043 op->flags & XE_VMA_OP_COMMITTED,
3044 op->flags & XE_VMA_OP_PREV_COMMITTED,
3045 op->flags & XE_VMA_OP_NEXT_COMMITTED);
3046 }
3047 }
3048 }
3049
3050 /**
3051 * struct xe_vma_lock_and_validate_flags - Flags for vma_lock_and_validate()
3052 * @res_evict: Allow evicting resources during validation
3053 * @validate: Perform BO validation
3054 * @request_decompress: Request BO decompression
3055 * @check_purged: Reject operation if BO is DONTNEED or PURGED
3056 */
3057 struct xe_vma_lock_and_validate_flags {
3058 u32 res_evict : 1;
3059 u32 validate : 1;
3060 u32 request_decompress : 1;
3061 u32 check_purged : 1;
3062 };
3063
vma_lock_and_validate(struct drm_exec * exec,struct xe_vma * vma,struct xe_vma_lock_and_validate_flags flags)3064 static int vma_lock_and_validate(struct drm_exec *exec, struct xe_vma *vma,
3065 struct xe_vma_lock_and_validate_flags flags)
3066 {
3067 struct xe_bo *bo = xe_vma_bo(vma);
3068 struct xe_vm *vm = xe_vma_vm(vma);
3069 bool validate_bo = flags.validate;
3070 int err = 0;
3071
3072 if (bo) {
3073 if (!bo->vm)
3074 err = drm_exec_lock_obj(exec, &bo->ttm.base);
3075
3076 /* Reject new mappings to DONTNEED/purged BOs; allow cleanup operations */
3077 if (!err && flags.check_purged) {
3078 if (xe_bo_madv_is_dontneed(bo))
3079 err = -EBUSY; /* BO marked purgeable */
3080 else if (xe_bo_is_purged(bo))
3081 err = -EINVAL; /* BO already purged */
3082 }
3083
3084 /* Don't validate the BO for DONTNEED/PURGED remap remnants. */
3085 if (vma->attr.purgeable_state != XE_MADV_PURGEABLE_WILLNEED)
3086 validate_bo = false;
3087
3088 if (!err && validate_bo)
3089 err = xe_bo_validate(bo, vm,
3090 xe_vm_allow_vm_eviction(vm) &&
3091 flags.res_evict, exec);
3092
3093 if (err)
3094 return err;
3095
3096 if (flags.request_decompress)
3097 err = xe_bo_decompress(bo);
3098 }
3099
3100 return err;
3101 }
3102
check_ufence(struct xe_vma * vma)3103 static int check_ufence(struct xe_vma *vma)
3104 {
3105 if (vma->ufence) {
3106 struct xe_user_fence * const f = vma->ufence;
3107
3108 if (!xe_sync_ufence_get_status(f))
3109 return -EBUSY;
3110
3111 vma->ufence = NULL;
3112 xe_sync_ufence_put(f);
3113 }
3114
3115 return 0;
3116 }
3117
prefetch_ranges(struct xe_vm * vm,struct xe_vma_op * op)3118 static int prefetch_ranges(struct xe_vm *vm, struct xe_vma_op *op)
3119 {
3120 bool devmem_possible = IS_DGFX(vm->xe) && IS_ENABLED(CONFIG_DRM_XE_PAGEMAP);
3121 struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
3122 struct drm_pagemap *dpagemap = op->prefetch_range.dpagemap;
3123 int err = 0;
3124
3125 struct xe_svm_range *svm_range;
3126 struct drm_gpusvm_ctx ctx = {};
3127 unsigned long i;
3128
3129 if (!xe_vma_is_cpu_addr_mirror(vma))
3130 return 0;
3131
3132 ctx.read_only = xe_vma_read_only(vma);
3133 ctx.devmem_possible = devmem_possible;
3134 ctx.check_pages_threshold = devmem_possible ? SZ_64K : 0;
3135 ctx.device_private_page_owner = xe_svm_private_page_owner(vm, !dpagemap);
3136
3137 /* TODO: Threading the migration */
3138 xa_for_each(&op->prefetch_range.range, i, svm_range) {
3139 if (!dpagemap)
3140 xe_svm_range_migrate_to_smem(vm, svm_range);
3141
3142 if (IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)) {
3143 drm_dbg(&vm->xe->drm,
3144 "Prefetch pagemap is %s start 0x%016lx end 0x%016lx\n",
3145 dpagemap ? dpagemap->drm->unique : "system",
3146 xe_svm_range_start(svm_range), xe_svm_range_end(svm_range));
3147 }
3148
3149 if (xe_svm_range_needs_migrate_to_vram(svm_range, vma, dpagemap)) {
3150 err = xe_svm_alloc_vram(svm_range, &ctx, dpagemap);
3151 if (err) {
3152 drm_dbg(&vm->xe->drm, "VRAM allocation failed, retry from userspace, asid=%u, gpusvm=%p, errno=%pe\n",
3153 vm->usm.asid, &vm->svm.gpusvm, ERR_PTR(err));
3154 return -ENODATA;
3155 }
3156 xe_svm_range_debug(svm_range, "PREFETCH - RANGE MIGRATED TO VRAM");
3157 }
3158
3159 err = xe_svm_range_get_pages(vm, svm_range, &ctx);
3160 if (err) {
3161 drm_dbg(&vm->xe->drm, "Get pages failed, asid=%u, gpusvm=%p, errno=%pe\n",
3162 vm->usm.asid, &vm->svm.gpusvm, ERR_PTR(err));
3163 if (err == -EOPNOTSUPP || err == -EFAULT || err == -EPERM)
3164 err = -ENODATA;
3165 return err;
3166 }
3167 xe_svm_range_debug(svm_range, "PREFETCH - RANGE GET PAGES DONE");
3168 }
3169
3170 return err;
3171 }
3172
op_lock_and_prep(struct drm_exec * exec,struct xe_vm * vm,struct xe_vma_ops * vops,struct xe_vma_op * op)3173 static int op_lock_and_prep(struct drm_exec *exec, struct xe_vm *vm,
3174 struct xe_vma_ops *vops, struct xe_vma_op *op)
3175 {
3176 int err = 0;
3177 bool res_evict;
3178
3179 /*
3180 * We only allow evicting a BO within the VM if it is not part of an
3181 * array of binds, as an array of binds can evict another BO within the
3182 * bind.
3183 */
3184 res_evict = !(vops->flags & XE_VMA_OPS_ARRAY_OF_BINDS);
3185
3186 switch (op->base.op) {
3187 case DRM_GPUVA_OP_MAP:
3188 if (!op->map.invalidate_on_bind)
3189 err = vma_lock_and_validate(exec, op->map.vma,
3190 (struct xe_vma_lock_and_validate_flags) {
3191 .res_evict = res_evict,
3192 .validate = !xe_vm_in_fault_mode(vm) ||
3193 op->map.immediate,
3194 .request_decompress =
3195 op->map.request_decompress,
3196 .check_purged = false,
3197 });
3198 break;
3199 case DRM_GPUVA_OP_REMAP:
3200 err = check_ufence(gpuva_to_vma(op->base.remap.unmap->va));
3201 if (err)
3202 break;
3203
3204 err = vma_lock_and_validate(exec,
3205 gpuva_to_vma(op->base.remap.unmap->va),
3206 (struct xe_vma_lock_and_validate_flags) {
3207 .res_evict = res_evict,
3208 .validate = false,
3209 .request_decompress = false,
3210 .check_purged = false,
3211 });
3212 if (!err && op->remap.prev)
3213 err = vma_lock_and_validate(exec, op->remap.prev,
3214 (struct xe_vma_lock_and_validate_flags) {
3215 .res_evict = res_evict,
3216 .validate = true,
3217 .request_decompress = false,
3218 .check_purged = false,
3219 });
3220 if (!err && op->remap.next)
3221 err = vma_lock_and_validate(exec, op->remap.next,
3222 (struct xe_vma_lock_and_validate_flags) {
3223 .res_evict = res_evict,
3224 .validate = true,
3225 .request_decompress = false,
3226 .check_purged = false,
3227 });
3228 break;
3229 case DRM_GPUVA_OP_UNMAP:
3230 err = check_ufence(gpuva_to_vma(op->base.unmap.va));
3231 if (err)
3232 break;
3233
3234 err = vma_lock_and_validate(exec,
3235 gpuva_to_vma(op->base.unmap.va),
3236 (struct xe_vma_lock_and_validate_flags) {
3237 .res_evict = res_evict,
3238 .validate = false,
3239 .request_decompress = false,
3240 .check_purged = false,
3241 });
3242 break;
3243 case DRM_GPUVA_OP_PREFETCH:
3244 {
3245 struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
3246 u32 region;
3247
3248 if (!xe_vma_is_cpu_addr_mirror(vma)) {
3249 region = op->prefetch.region;
3250 xe_assert(vm->xe, region == DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC ||
3251 region <= ARRAY_SIZE(region_to_mem_type));
3252 }
3253
3254 /*
3255 * PREFETCH is the only op that still gates on BO purge state.
3256 * MAP/REMAP handle this inside xe_vma_create() so partial
3257 * unbind on a DONTNEED BO still works. PREFETCH skips
3258 * xe_vma_create() and would migrate a BO with no backing
3259 * store, so reject DONTNEED/PURGED here.
3260 */
3261 err = vma_lock_and_validate(exec,
3262 gpuva_to_vma(op->base.prefetch.va),
3263 (struct xe_vma_lock_and_validate_flags) {
3264 .res_evict = res_evict,
3265 .validate = false,
3266 .request_decompress = false,
3267 .check_purged = true,
3268 });
3269 if (!err && !xe_vma_has_no_bo(vma)) {
3270 struct xe_bo *bo = xe_vma_bo(vma);
3271 u32 mem_type;
3272
3273 if (region == DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC) {
3274 unsigned int i;
3275
3276 mem_type = XE_PL_TT;
3277 for (i = 0; i < bo->placement.num_placement; i++) {
3278 if (mem_type_is_vram(bo->placements[i].mem_type)) {
3279 mem_type = bo->placements[i].mem_type;
3280 break;
3281 }
3282 }
3283 } else {
3284 mem_type = region_to_mem_type[region];
3285 }
3286
3287 err = xe_bo_migrate(bo, mem_type, NULL, exec);
3288 }
3289 break;
3290 }
3291 default:
3292 drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
3293 }
3294
3295 return err;
3296 }
3297
vm_bind_ioctl_ops_prefetch_ranges(struct xe_vm * vm,struct xe_vma_ops * vops)3298 static int vm_bind_ioctl_ops_prefetch_ranges(struct xe_vm *vm, struct xe_vma_ops *vops)
3299 {
3300 struct xe_vma_op *op;
3301 int err;
3302
3303 if (!(vops->flags & XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH))
3304 return 0;
3305
3306 list_for_each_entry(op, &vops->list, link) {
3307 if (op->base.op == DRM_GPUVA_OP_PREFETCH) {
3308 err = prefetch_ranges(vm, op);
3309 if (err)
3310 return err;
3311 }
3312 }
3313
3314 return 0;
3315 }
3316
vm_bind_ioctl_ops_lock_and_prep(struct drm_exec * exec,struct xe_vm * vm,struct xe_vma_ops * vops)3317 static int vm_bind_ioctl_ops_lock_and_prep(struct drm_exec *exec,
3318 struct xe_vm *vm,
3319 struct xe_vma_ops *vops)
3320 {
3321 struct xe_vma_op *op;
3322 int err;
3323
3324 err = drm_exec_lock_obj(exec, xe_vm_obj(vm));
3325 if (err)
3326 return err;
3327
3328 list_for_each_entry(op, &vops->list, link) {
3329 err = op_lock_and_prep(exec, vm, vops, op);
3330 if (err)
3331 return err;
3332 }
3333
3334 #ifdef TEST_VM_OPS_ERROR
3335 if (vops->inject_error &&
3336 vm->xe->vm_inject_error_position == FORCE_OP_ERROR_LOCK)
3337 return -ENOSPC;
3338 #endif
3339
3340 return 0;
3341 }
3342
op_trace(struct xe_vma_op * op)3343 static void op_trace(struct xe_vma_op *op)
3344 {
3345 switch (op->base.op) {
3346 case DRM_GPUVA_OP_MAP:
3347 trace_xe_vma_bind(op->map.vma);
3348 break;
3349 case DRM_GPUVA_OP_REMAP:
3350 trace_xe_vma_unbind(gpuva_to_vma(op->base.remap.unmap->va));
3351 if (op->remap.prev)
3352 trace_xe_vma_bind(op->remap.prev);
3353 if (op->remap.next)
3354 trace_xe_vma_bind(op->remap.next);
3355 break;
3356 case DRM_GPUVA_OP_UNMAP:
3357 trace_xe_vma_unbind(gpuva_to_vma(op->base.unmap.va));
3358 break;
3359 case DRM_GPUVA_OP_PREFETCH:
3360 trace_xe_vma_bind(gpuva_to_vma(op->base.prefetch.va));
3361 break;
3362 case DRM_GPUVA_OP_DRIVER:
3363 break;
3364 default:
3365 XE_WARN_ON("NOT POSSIBLE");
3366 }
3367 }
3368
trace_xe_vm_ops_execute(struct xe_vma_ops * vops)3369 static void trace_xe_vm_ops_execute(struct xe_vma_ops *vops)
3370 {
3371 struct xe_vma_op *op;
3372
3373 list_for_each_entry(op, &vops->list, link)
3374 op_trace(op);
3375 }
3376
vm_ops_setup_tile_args(struct xe_vm * vm,struct xe_vma_ops * vops)3377 static int vm_ops_setup_tile_args(struct xe_vm *vm, struct xe_vma_ops *vops)
3378 {
3379 struct xe_exec_queue *q = vops->q;
3380 struct xe_tile *tile;
3381 int number_tiles = 0;
3382 u8 id;
3383
3384 for_each_tile(tile, vm->xe, id) {
3385 if (vops->pt_update_ops[id].num_ops)
3386 ++number_tiles;
3387
3388 if (vops->pt_update_ops[id].q)
3389 continue;
3390
3391 if (q) {
3392 vops->pt_update_ops[id].q = q;
3393 if (vm->pt_root[id] && !list_empty(&q->multi_gt_list))
3394 q = list_next_entry(q, multi_gt_list);
3395 } else {
3396 vops->pt_update_ops[id].q = vm->q[id];
3397 }
3398 }
3399
3400 return number_tiles;
3401 }
3402
ops_execute(struct xe_vm * vm,struct xe_vma_ops * vops)3403 static struct dma_fence *ops_execute(struct xe_vm *vm,
3404 struct xe_vma_ops *vops)
3405 {
3406 struct xe_tile *tile;
3407 struct dma_fence *fence = NULL;
3408 struct dma_fence **fences = NULL;
3409 struct dma_fence_array *cf = NULL;
3410 int number_tiles = 0, current_fence = 0, n_fence = 0, err, i;
3411 u8 id;
3412
3413 number_tiles = vm_ops_setup_tile_args(vm, vops);
3414 if (number_tiles == 0)
3415 return ERR_PTR(-ENODATA);
3416
3417 for_each_tile(tile, vm->xe, id) {
3418 ++n_fence;
3419
3420 if (!(vops->flags & XE_VMA_OPS_FLAG_SKIP_TLB_WAIT))
3421 for_each_tlb_inval(i)
3422 ++n_fence;
3423 }
3424
3425 fences = kmalloc_objs(*fences, n_fence);
3426 if (!fences) {
3427 fence = ERR_PTR(-ENOMEM);
3428 goto err_trace;
3429 }
3430
3431 cf = dma_fence_array_alloc(n_fence);
3432 if (!cf) {
3433 fence = ERR_PTR(-ENOMEM);
3434 goto err_out;
3435 }
3436
3437 for_each_tile(tile, vm->xe, id) {
3438 if (!vops->pt_update_ops[id].num_ops)
3439 continue;
3440
3441 err = xe_pt_update_ops_prepare(tile, vops);
3442 if (err) {
3443 fence = ERR_PTR(err);
3444 goto err_out;
3445 }
3446 }
3447
3448 trace_xe_vm_ops_execute(vops);
3449
3450 for_each_tile(tile, vm->xe, id) {
3451 struct xe_exec_queue *q = vops->pt_update_ops[tile->id].q;
3452
3453 fence = NULL;
3454 if (!vops->pt_update_ops[id].num_ops)
3455 goto collect_fences;
3456
3457 fence = xe_pt_update_ops_run(tile, vops);
3458 if (IS_ERR(fence))
3459 goto err_out;
3460
3461 collect_fences:
3462 fences[current_fence++] = fence ?: dma_fence_get_stub();
3463 if (vops->flags & XE_VMA_OPS_FLAG_SKIP_TLB_WAIT)
3464 continue;
3465
3466 xe_migrate_job_lock(tile->migrate, q);
3467 for_each_tlb_inval(i)
3468 fences[current_fence++] =
3469 xe_exec_queue_tlb_inval_last_fence_get(q, vm, i);
3470 xe_migrate_job_unlock(tile->migrate, q);
3471 }
3472
3473 xe_assert(vm->xe, current_fence == n_fence);
3474 dma_fence_array_init(cf, n_fence, fences, dma_fence_context_alloc(1),
3475 1);
3476 fence = &cf->base;
3477
3478 for_each_tile(tile, vm->xe, id) {
3479 if (!vops->pt_update_ops[id].num_ops)
3480 continue;
3481
3482 xe_pt_update_ops_fini(tile, vops);
3483 }
3484
3485 return fence;
3486
3487 err_out:
3488 for_each_tile(tile, vm->xe, id) {
3489 if (!vops->pt_update_ops[id].num_ops)
3490 continue;
3491
3492 xe_pt_update_ops_abort(tile, vops);
3493 }
3494 while (current_fence)
3495 dma_fence_put(fences[--current_fence]);
3496 kfree(fences);
3497 kfree(cf);
3498
3499 err_trace:
3500 trace_xe_vm_ops_fail(vm);
3501 return fence;
3502 }
3503
vma_add_ufence(struct xe_vma * vma,struct xe_user_fence * ufence)3504 static void vma_add_ufence(struct xe_vma *vma, struct xe_user_fence *ufence)
3505 {
3506 if (vma->ufence)
3507 xe_sync_ufence_put(vma->ufence);
3508 vma->ufence = __xe_sync_ufence_get(ufence);
3509 }
3510
op_add_ufence(struct xe_vm * vm,struct xe_vma_op * op,struct xe_user_fence * ufence)3511 static void op_add_ufence(struct xe_vm *vm, struct xe_vma_op *op,
3512 struct xe_user_fence *ufence)
3513 {
3514 switch (op->base.op) {
3515 case DRM_GPUVA_OP_MAP:
3516 if (!xe_vma_is_cpu_addr_mirror(op->map.vma))
3517 vma_add_ufence(op->map.vma, ufence);
3518 break;
3519 case DRM_GPUVA_OP_REMAP:
3520 if (op->remap.prev)
3521 vma_add_ufence(op->remap.prev, ufence);
3522 if (op->remap.next)
3523 vma_add_ufence(op->remap.next, ufence);
3524 break;
3525 case DRM_GPUVA_OP_UNMAP:
3526 break;
3527 case DRM_GPUVA_OP_PREFETCH:
3528 vma_add_ufence(gpuva_to_vma(op->base.prefetch.va), ufence);
3529 break;
3530 default:
3531 drm_warn(&vm->xe->drm, "NOT POSSIBLE\n");
3532 }
3533 }
3534
vm_bind_ioctl_ops_fini(struct xe_vm * vm,struct xe_vma_ops * vops,struct dma_fence * fence)3535 static void vm_bind_ioctl_ops_fini(struct xe_vm *vm, struct xe_vma_ops *vops,
3536 struct dma_fence *fence)
3537 {
3538 struct xe_user_fence *ufence;
3539 struct xe_vma_op *op;
3540 int i;
3541
3542 ufence = find_ufence_get(vops->syncs, vops->num_syncs);
3543 list_for_each_entry(op, &vops->list, link) {
3544 if (ufence)
3545 op_add_ufence(vm, op, ufence);
3546
3547 if (op->base.op == DRM_GPUVA_OP_UNMAP)
3548 xe_vma_destroy(gpuva_to_vma(op->base.unmap.va), fence);
3549 else if (op->base.op == DRM_GPUVA_OP_REMAP)
3550 xe_vma_destroy(gpuva_to_vma(op->base.remap.unmap->va),
3551 fence);
3552 }
3553 if (ufence)
3554 xe_sync_ufence_put(ufence);
3555 if (fence) {
3556 for (i = 0; i < vops->num_syncs; i++)
3557 xe_sync_entry_signal(vops->syncs + i, fence);
3558 }
3559 }
3560
vm_bind_ioctl_ops_execute(struct xe_vm * vm,struct xe_vma_ops * vops)3561 static struct dma_fence *vm_bind_ioctl_ops_execute(struct xe_vm *vm,
3562 struct xe_vma_ops *vops)
3563 {
3564 struct xe_validation_ctx ctx;
3565 struct drm_exec exec;
3566 struct dma_fence *fence;
3567 int err = 0;
3568
3569 lockdep_assert_held_write(&vm->lock);
3570
3571 xe_validation_guard(&ctx, &vm->xe->val, &exec,
3572 ((struct xe_val_flags) {
3573 .interruptible = true,
3574 .exec_ignore_duplicates = true,
3575 }), err) {
3576 err = vm_bind_ioctl_ops_lock_and_prep(&exec, vm, vops);
3577 drm_exec_retry_on_contention(&exec);
3578 xe_validation_retry_on_oom(&ctx, &err);
3579 if (err)
3580 return ERR_PTR(err);
3581
3582 xe_vm_set_validation_exec(vm, &exec);
3583 fence = ops_execute(vm, vops);
3584 xe_vm_set_validation_exec(vm, NULL);
3585 if (IS_ERR(fence)) {
3586 if (PTR_ERR(fence) == -ENODATA)
3587 vm_bind_ioctl_ops_fini(vm, vops, NULL);
3588 return fence;
3589 }
3590
3591 vm_bind_ioctl_ops_fini(vm, vops, fence);
3592 }
3593
3594 return err ? ERR_PTR(err) : fence;
3595 }
3596 ALLOW_ERROR_INJECTION(vm_bind_ioctl_ops_execute, ERRNO);
3597
3598 #define SUPPORTED_FLAGS_STUB \
3599 (DRM_XE_VM_BIND_FLAG_READONLY | \
3600 DRM_XE_VM_BIND_FLAG_IMMEDIATE | \
3601 DRM_XE_VM_BIND_FLAG_NULL | \
3602 DRM_XE_VM_BIND_FLAG_DUMPABLE | \
3603 DRM_XE_VM_BIND_FLAG_CHECK_PXP | \
3604 DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR | \
3605 DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET | \
3606 DRM_XE_VM_BIND_FLAG_DECOMPRESS)
3607
3608 #ifdef TEST_VM_OPS_ERROR
3609 #define SUPPORTED_FLAGS (SUPPORTED_FLAGS_STUB | FORCE_OP_ERROR)
3610 #else
3611 #define SUPPORTED_FLAGS SUPPORTED_FLAGS_STUB
3612 #endif
3613
3614 #define XE_64K_PAGE_MASK 0xffffull
3615 #define ALL_DRM_XE_SYNCS_FLAGS (DRM_XE_SYNCS_FLAG_WAIT_FOR_OP)
3616
vm_bind_ioctl_check_args(struct xe_device * xe,struct xe_vm * vm,struct drm_xe_vm_bind * args,struct drm_xe_vm_bind_op ** bind_ops)3617 static int vm_bind_ioctl_check_args(struct xe_device *xe, struct xe_vm *vm,
3618 struct drm_xe_vm_bind *args,
3619 struct drm_xe_vm_bind_op **bind_ops)
3620 {
3621 int err;
3622 int i;
3623
3624 if (XE_IOCTL_DBG(xe, args->pad || args->pad2) ||
3625 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
3626 return -EINVAL;
3627
3628 if (XE_IOCTL_DBG(xe, args->extensions))
3629 return -EINVAL;
3630
3631 if (XE_IOCTL_DBG(xe, args->num_syncs > DRM_XE_MAX_SYNCS))
3632 return -EINVAL;
3633
3634 if (args->num_binds > 1) {
3635 u64 __user *bind_user =
3636 u64_to_user_ptr(args->vector_of_binds);
3637
3638 *bind_ops = kvmalloc_objs(struct drm_xe_vm_bind_op,
3639 args->num_binds,
3640 GFP_KERNEL | __GFP_ACCOUNT | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
3641 if (!*bind_ops)
3642 return args->num_binds > 1 ? -ENOBUFS : -ENOMEM;
3643
3644 err = copy_from_user(*bind_ops, bind_user,
3645 sizeof(struct drm_xe_vm_bind_op) *
3646 args->num_binds);
3647 if (XE_IOCTL_DBG(xe, err)) {
3648 err = -EFAULT;
3649 goto free_bind_ops;
3650 }
3651 } else {
3652 *bind_ops = &args->bind;
3653 }
3654
3655 for (i = 0; i < args->num_binds; ++i) {
3656 u64 range = (*bind_ops)[i].range;
3657 u64 addr = (*bind_ops)[i].addr;
3658 u32 op = (*bind_ops)[i].op;
3659 u32 flags = (*bind_ops)[i].flags;
3660 u32 obj = (*bind_ops)[i].obj;
3661 u64 obj_offset = (*bind_ops)[i].obj_offset;
3662 u32 prefetch_region = (*bind_ops)[i].prefetch_mem_region_instance;
3663 bool is_null = flags & DRM_XE_VM_BIND_FLAG_NULL;
3664 bool is_cpu_addr_mirror = flags &
3665 DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR;
3666 bool is_decompress = flags & DRM_XE_VM_BIND_FLAG_DECOMPRESS;
3667 u16 pat_index = (*bind_ops)[i].pat_index;
3668 u16 coh_mode;
3669 bool comp_en;
3670
3671 if (XE_IOCTL_DBG(xe, is_cpu_addr_mirror &&
3672 (!xe_vm_in_fault_mode(vm) ||
3673 !IS_ENABLED(CONFIG_DRM_XE_GPUSVM)))) {
3674 err = -EINVAL;
3675 goto free_bind_ops;
3676 }
3677
3678 if (XE_IOCTL_DBG(xe, pat_index >= xe->pat.n_entries)) {
3679 err = -EINVAL;
3680 goto free_bind_ops;
3681 }
3682
3683 pat_index = array_index_nospec(pat_index, xe->pat.n_entries);
3684 (*bind_ops)[i].pat_index = pat_index;
3685 coh_mode = xe_pat_index_get_coh_mode(xe, pat_index);
3686 comp_en = xe_pat_index_get_comp_en(xe, pat_index);
3687 if (XE_IOCTL_DBG(xe, !coh_mode)) { /* hw reserved */
3688 err = -EINVAL;
3689 goto free_bind_ops;
3690 }
3691
3692 if (XE_WARN_ON(coh_mode > XE_COH_2WAY)) {
3693 err = -EINVAL;
3694 goto free_bind_ops;
3695 }
3696
3697 if (XE_IOCTL_DBG(xe, op > DRM_XE_VM_BIND_OP_PREFETCH) ||
3698 XE_IOCTL_DBG(xe, flags & ~SUPPORTED_FLAGS) ||
3699 XE_IOCTL_DBG(xe, obj && (is_null || is_cpu_addr_mirror)) ||
3700 XE_IOCTL_DBG(xe, obj_offset && (is_null ||
3701 is_cpu_addr_mirror)) ||
3702 XE_IOCTL_DBG(xe, op != DRM_XE_VM_BIND_OP_MAP &&
3703 (is_decompress || is_null || is_cpu_addr_mirror)) ||
3704 XE_IOCTL_DBG(xe, is_decompress &&
3705 xe_pat_index_get_comp_en(xe, pat_index)) ||
3706 XE_IOCTL_DBG(xe, !obj &&
3707 op == DRM_XE_VM_BIND_OP_MAP &&
3708 !is_null && !is_cpu_addr_mirror) ||
3709 XE_IOCTL_DBG(xe, !obj &&
3710 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
3711 XE_IOCTL_DBG(xe, addr &&
3712 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
3713 XE_IOCTL_DBG(xe, range &&
3714 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
3715 XE_IOCTL_DBG(xe, obj &&
3716 op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
3717 XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE &&
3718 op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
3719 XE_IOCTL_DBG(xe, !IS_DGFX(xe) && coh_mode == XE_COH_NONE &&
3720 is_cpu_addr_mirror) ||
3721 XE_IOCTL_DBG(xe, xe_device_is_l2_flush_optimized(xe) &&
3722 (op == DRM_XE_VM_BIND_OP_MAP_USERPTR ||
3723 is_cpu_addr_mirror) &&
3724 (pat_index != 19 && coh_mode != XE_COH_2WAY)) ||
3725 XE_IOCTL_DBG(xe, comp_en &&
3726 op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
3727 XE_IOCTL_DBG(xe, op == DRM_XE_VM_BIND_OP_MAP_USERPTR &&
3728 !IS_ENABLED(CONFIG_DRM_GPUSVM)) ||
3729 XE_IOCTL_DBG(xe, obj &&
3730 op == DRM_XE_VM_BIND_OP_PREFETCH) ||
3731 XE_IOCTL_DBG(xe, prefetch_region &&
3732 op != DRM_XE_VM_BIND_OP_PREFETCH) ||
3733 XE_IOCTL_DBG(xe, (prefetch_region != DRM_XE_CONSULT_MEM_ADVISE_PREF_LOC &&
3734 /* Guard against undefined shift in BIT(prefetch_region) */
3735 (prefetch_region >= (sizeof(xe->info.mem_region_mask) * 8) ||
3736 !(BIT(prefetch_region) & xe->info.mem_region_mask)))) ||
3737 XE_IOCTL_DBG(xe, obj &&
3738 op == DRM_XE_VM_BIND_OP_UNMAP) ||
3739 XE_IOCTL_DBG(xe, (flags & DRM_XE_VM_BIND_FLAG_MADVISE_AUTORESET) &&
3740 (!is_cpu_addr_mirror || op != DRM_XE_VM_BIND_OP_MAP))) {
3741 err = -EINVAL;
3742 goto free_bind_ops;
3743 }
3744
3745 if (XE_IOCTL_DBG(xe, obj_offset & ~PAGE_MASK) ||
3746 XE_IOCTL_DBG(xe, addr & ~PAGE_MASK) ||
3747 XE_IOCTL_DBG(xe, range & ~PAGE_MASK) ||
3748 XE_IOCTL_DBG(xe, !range &&
3749 op != DRM_XE_VM_BIND_OP_UNMAP_ALL)) {
3750 err = -EINVAL;
3751 goto free_bind_ops;
3752 }
3753
3754 if (is_decompress && (XE_IOCTL_DBG(xe, !xe_device_has_flat_ccs(xe)) ||
3755 XE_IOCTL_DBG(xe, GRAPHICS_VER(xe) < 20) ||
3756 XE_IOCTL_DBG(xe, !IS_DGFX(xe)))) {
3757 err = -EOPNOTSUPP;
3758 goto free_bind_ops;
3759 }
3760 }
3761
3762 return 0;
3763
3764 free_bind_ops:
3765 if (args->num_binds > 1)
3766 kvfree(*bind_ops);
3767 *bind_ops = NULL;
3768 return err;
3769 }
3770
vm_bind_ioctl_signal_fences(struct xe_vm * vm,struct xe_exec_queue * q,struct xe_sync_entry * syncs,int num_syncs)3771 static int vm_bind_ioctl_signal_fences(struct xe_vm *vm,
3772 struct xe_exec_queue *q,
3773 struct xe_sync_entry *syncs,
3774 int num_syncs)
3775 {
3776 struct dma_fence *fence = NULL;
3777 int i, err = 0;
3778
3779 if (num_syncs) {
3780 fence = xe_sync_in_fence_get(syncs, num_syncs,
3781 to_wait_exec_queue(vm, q), vm);
3782 if (IS_ERR(fence))
3783 return PTR_ERR(fence);
3784
3785 for (i = 0; i < num_syncs; i++)
3786 xe_sync_entry_signal(&syncs[i], fence);
3787 }
3788
3789 dma_fence_put(fence);
3790
3791 return err;
3792 }
3793
xe_vma_ops_init(struct xe_vma_ops * vops,struct xe_vm * vm,struct xe_exec_queue * q,struct xe_sync_entry * syncs,u32 num_syncs)3794 static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
3795 struct xe_exec_queue *q,
3796 struct xe_sync_entry *syncs, u32 num_syncs)
3797 {
3798 memset(vops, 0, sizeof(*vops));
3799 INIT_LIST_HEAD(&vops->list);
3800 vops->vm = vm;
3801 vops->q = q;
3802 vops->syncs = syncs;
3803 vops->num_syncs = num_syncs;
3804 vops->flags = 0;
3805 }
3806
xe_vm_bind_ioctl_validate_bo(struct xe_device * xe,struct xe_bo * bo,u64 addr,u64 range,u64 obj_offset,u16 pat_index,u32 op,u32 bind_flags)3807 static int xe_vm_bind_ioctl_validate_bo(struct xe_device *xe, struct xe_bo *bo,
3808 u64 addr, u64 range, u64 obj_offset,
3809 u16 pat_index, u32 op, u32 bind_flags)
3810 {
3811 u16 coh_mode;
3812 bool comp_en;
3813
3814 if (XE_IOCTL_DBG(xe, (bo->flags & XE_BO_FLAG_NO_COMPRESSION) &&
3815 xe_pat_index_get_comp_en(xe, pat_index)))
3816 return -EINVAL;
3817
3818 if (XE_IOCTL_DBG(xe, range > xe_bo_size(bo)) ||
3819 XE_IOCTL_DBG(xe, obj_offset >
3820 xe_bo_size(bo) - range)) {
3821 return -EINVAL;
3822 }
3823
3824 /*
3825 * Some platforms require 64k VM_BIND alignment,
3826 * specifically those with XE_VRAM_FLAGS_NEED64K.
3827 *
3828 * Other platforms may have BO's set to 64k physical placement,
3829 * but can be mapped at 4k offsets anyway. This check is only
3830 * there for the former case.
3831 */
3832 if ((bo->flags & XE_BO_FLAG_INTERNAL_64K) &&
3833 (xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)) {
3834 if (XE_IOCTL_DBG(xe, obj_offset &
3835 XE_64K_PAGE_MASK) ||
3836 XE_IOCTL_DBG(xe, addr & XE_64K_PAGE_MASK) ||
3837 XE_IOCTL_DBG(xe, range & XE_64K_PAGE_MASK)) {
3838 return -EINVAL;
3839 }
3840 }
3841
3842 coh_mode = xe_pat_index_get_coh_mode(xe, pat_index);
3843 if (bo->cpu_caching) {
3844 if (XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE &&
3845 bo->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB)) {
3846 return -EINVAL;
3847 }
3848 } else if (XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE)) {
3849 /*
3850 * Imported dma-buf from a different device should
3851 * require 1way or 2way coherency since we don't know
3852 * how it was mapped on the CPU. Just assume is it
3853 * potentially cached on CPU side.
3854 */
3855 return -EINVAL;
3856 }
3857
3858 /*
3859 * Ensures that imported buffer objects (dma-bufs) are not mapped
3860 * with a PAT index that enables compression.
3861 */
3862 comp_en = xe_pat_index_get_comp_en(xe, pat_index);
3863 if (XE_IOCTL_DBG(xe, bo->ttm.base.import_attach && comp_en))
3864 return -EINVAL;
3865
3866 if (XE_IOCTL_DBG(xe, bo->ttm.base.import_attach && xe_device_is_l2_flush_optimized(xe) &&
3867 (pat_index != 19 && coh_mode != XE_COH_2WAY)))
3868 return -EINVAL;
3869
3870 /* If a BO is protected it can only be mapped if the key is still valid */
3871 if ((bind_flags & DRM_XE_VM_BIND_FLAG_CHECK_PXP) && xe_bo_is_protected(bo) &&
3872 op != DRM_XE_VM_BIND_OP_UNMAP && op != DRM_XE_VM_BIND_OP_UNMAP_ALL)
3873 if (XE_IOCTL_DBG(xe, xe_pxp_bo_key_check(xe->pxp, bo) != 0))
3874 return -ENOEXEC;
3875
3876 return 0;
3877 }
3878
xe_vm_bind_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3879 int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3880 {
3881 struct xe_device *xe = to_xe_device(dev);
3882 struct xe_file *xef = to_xe_file(file);
3883 struct drm_xe_vm_bind *args = data;
3884 struct drm_xe_sync __user *syncs_user;
3885 struct xe_bo **bos = NULL;
3886 struct drm_gpuva_ops **ops = NULL;
3887 struct xe_vm *vm;
3888 struct xe_exec_queue *q = NULL;
3889 u32 num_syncs, num_ufence = 0;
3890 struct xe_sync_entry *syncs = NULL;
3891 struct drm_xe_vm_bind_op *bind_ops = NULL;
3892 struct xe_vma_ops vops;
3893 struct dma_fence *fence;
3894 int err;
3895 int i;
3896
3897 vm = xe_vm_lookup(xef, args->vm_id);
3898 if (XE_IOCTL_DBG(xe, !vm))
3899 return -EINVAL;
3900
3901 err = vm_bind_ioctl_check_args(xe, vm, args, &bind_ops);
3902 if (err)
3903 goto put_vm;
3904
3905 if (args->exec_queue_id) {
3906 q = xe_exec_queue_lookup(xef, args->exec_queue_id);
3907 if (XE_IOCTL_DBG(xe, !q)) {
3908 err = -ENOENT;
3909 goto free_bind_ops;
3910 }
3911
3912 if (XE_IOCTL_DBG(xe, !(q->flags & EXEC_QUEUE_FLAG_VM))) {
3913 err = -EINVAL;
3914 goto put_exec_queue;
3915 }
3916 }
3917
3918 if (XE_IOCTL_DBG(xe, q && vm != q->user_vm)) {
3919 err = -EINVAL;
3920 goto put_exec_queue;
3921 }
3922
3923 /* Ensure all UNMAPs visible */
3924 xe_svm_flush(vm);
3925
3926 err = down_write_killable(&vm->lock);
3927 if (err)
3928 goto put_exec_queue;
3929
3930 if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
3931 err = -ENOENT;
3932 goto release_vm_lock;
3933 }
3934
3935 for (i = 0; i < args->num_binds; ++i) {
3936 u64 range = bind_ops[i].range;
3937 u64 addr = bind_ops[i].addr;
3938
3939 if (XE_IOCTL_DBG(xe, range > vm->size) ||
3940 XE_IOCTL_DBG(xe, addr > vm->size - range)) {
3941 err = -EINVAL;
3942 goto release_vm_lock;
3943 }
3944 }
3945
3946 if (args->num_binds) {
3947 bos = kvzalloc_objs(*bos, args->num_binds,
3948 GFP_KERNEL | __GFP_ACCOUNT | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
3949 if (!bos) {
3950 err = -ENOMEM;
3951 goto release_vm_lock;
3952 }
3953
3954 ops = kvzalloc_objs(*ops, args->num_binds,
3955 GFP_KERNEL | __GFP_ACCOUNT | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
3956 if (!ops) {
3957 err = -ENOMEM;
3958 goto free_bos;
3959 }
3960 }
3961
3962 for (i = 0; i < args->num_binds; ++i) {
3963 struct drm_gem_object *gem_obj;
3964 u64 range = bind_ops[i].range;
3965 u64 addr = bind_ops[i].addr;
3966 u32 obj = bind_ops[i].obj;
3967 u64 obj_offset = bind_ops[i].obj_offset;
3968 u16 pat_index = bind_ops[i].pat_index;
3969 u32 op = bind_ops[i].op;
3970 u32 bind_flags = bind_ops[i].flags;
3971
3972 if (!obj)
3973 continue;
3974
3975 gem_obj = drm_gem_object_lookup(file, obj);
3976 if (XE_IOCTL_DBG(xe, !gem_obj)) {
3977 err = -ENOENT;
3978 goto put_obj;
3979 }
3980 bos[i] = gem_to_xe_bo(gem_obj);
3981
3982 err = xe_vm_bind_ioctl_validate_bo(xe, bos[i], addr, range,
3983 obj_offset, pat_index, op,
3984 bind_flags);
3985 if (err)
3986 goto put_obj;
3987 }
3988
3989 if (args->num_syncs) {
3990 syncs = kzalloc_objs(*syncs, args->num_syncs);
3991 if (!syncs) {
3992 err = -ENOMEM;
3993 goto put_obj;
3994 }
3995 }
3996
3997 syncs_user = u64_to_user_ptr(args->syncs);
3998 for (num_syncs = 0; num_syncs < args->num_syncs; num_syncs++) {
3999 struct xe_exec_queue *__q = q ?: vm->q[0];
4000
4001 err = xe_sync_entry_parse(xe, xef, &syncs[num_syncs],
4002 &syncs_user[num_syncs],
4003 __q->ufence_syncobj,
4004 ++__q->ufence_timeline_value,
4005 (xe_vm_in_lr_mode(vm) ?
4006 SYNC_PARSE_FLAG_LR_MODE : 0) |
4007 (!args->num_binds ?
4008 SYNC_PARSE_FLAG_DISALLOW_USER_FENCE : 0));
4009 if (err)
4010 goto free_syncs;
4011
4012 if (xe_sync_is_ufence(&syncs[num_syncs]))
4013 num_ufence++;
4014 }
4015
4016 if (XE_IOCTL_DBG(xe, num_ufence > 1)) {
4017 err = -EINVAL;
4018 goto free_syncs;
4019 }
4020
4021 if (!args->num_binds) {
4022 err = -ENODATA;
4023 goto free_syncs;
4024 }
4025
4026 xe_vma_ops_init(&vops, vm, q, syncs, num_syncs);
4027 if (args->num_binds > 1)
4028 vops.flags |= XE_VMA_OPS_ARRAY_OF_BINDS;
4029 for (i = 0; i < args->num_binds; ++i) {
4030 u64 range = bind_ops[i].range;
4031 u64 addr = bind_ops[i].addr;
4032 u32 op = bind_ops[i].op;
4033 u32 flags = bind_ops[i].flags;
4034 u64 obj_offset = bind_ops[i].obj_offset;
4035 u32 prefetch_region = bind_ops[i].prefetch_mem_region_instance;
4036 u16 pat_index = bind_ops[i].pat_index;
4037
4038 ops[i] = vm_bind_ioctl_ops_create(vm, &vops, bos[i], obj_offset,
4039 addr, range, op, flags,
4040 prefetch_region, pat_index);
4041 if (IS_ERR(ops[i])) {
4042 err = PTR_ERR(ops[i]);
4043 ops[i] = NULL;
4044 goto unwind_ops;
4045 }
4046
4047 err = vm_bind_ioctl_ops_parse(vm, ops[i], &vops);
4048 if (err)
4049 goto unwind_ops;
4050
4051 #ifdef TEST_VM_OPS_ERROR
4052 if (flags & FORCE_OP_ERROR) {
4053 vops.inject_error = true;
4054 vm->xe->vm_inject_error_position =
4055 (vm->xe->vm_inject_error_position + 1) %
4056 FORCE_OP_ERROR_COUNT;
4057 }
4058 #endif
4059 }
4060
4061 /* Nothing to do */
4062 if (list_empty(&vops.list)) {
4063 err = -ENODATA;
4064 goto unwind_ops;
4065 }
4066
4067 err = xe_vma_ops_alloc(&vops, args->num_binds > 1);
4068 if (err)
4069 goto unwind_ops;
4070
4071 err = vm_bind_ioctl_ops_prefetch_ranges(vm, &vops);
4072 if (err)
4073 goto unwind_ops;
4074
4075 fence = vm_bind_ioctl_ops_execute(vm, &vops);
4076 if (IS_ERR(fence))
4077 err = PTR_ERR(fence);
4078 else
4079 dma_fence_put(fence);
4080
4081 unwind_ops:
4082 if (err && err != -ENODATA)
4083 vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
4084 xe_vma_ops_fini(&vops);
4085 for (i = args->num_binds - 1; i >= 0; --i)
4086 if (ops[i])
4087 drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
4088 free_syncs:
4089 if (err == -ENODATA)
4090 err = vm_bind_ioctl_signal_fences(vm, q, syncs, num_syncs);
4091 while (num_syncs--)
4092 xe_sync_entry_cleanup(&syncs[num_syncs]);
4093
4094 kfree(syncs);
4095 put_obj:
4096 for (i = 0; i < args->num_binds; ++i)
4097 xe_bo_put(bos[i]);
4098
4099 kvfree(ops);
4100 free_bos:
4101 kvfree(bos);
4102 release_vm_lock:
4103 up_write(&vm->lock);
4104 put_exec_queue:
4105 if (q)
4106 xe_exec_queue_put(q);
4107 free_bind_ops:
4108 if (args->num_binds > 1)
4109 kvfree(bind_ops);
4110 put_vm:
4111 xe_vm_put(vm);
4112 return err;
4113 }
4114
4115 /*
4116 * Map access type, fault type, and fault level from current bspec
4117 * specification to user spec abstraction. The current mapping is
4118 * approximately 1-to-1, with access type being the only notable
4119 * exception as it carries additional data with respect to prefetch
4120 * status that needs to be masked out.
4121 */
xe_to_user_access_type(u8 access_type)4122 static u8 xe_to_user_access_type(u8 access_type)
4123 {
4124 return access_type & XE_PAGEFAULT_ACCESS_TYPE_MASK;
4125 }
4126
xe_to_user_fault_type(u8 fault_type)4127 static u8 xe_to_user_fault_type(u8 fault_type)
4128 {
4129 return fault_type;
4130 }
4131
xe_to_user_fault_level(u8 fault_level)4132 static u8 xe_to_user_fault_level(u8 fault_level)
4133 {
4134 return fault_level;
4135 }
4136
fill_faults(struct xe_vm * vm,struct drm_xe_vm_get_property * args)4137 static int fill_faults(struct xe_vm *vm,
4138 struct drm_xe_vm_get_property *args)
4139 {
4140 struct xe_vm_fault __user *usr_ptr = u64_to_user_ptr(args->data);
4141 struct xe_vm_fault *fault_list, fault_entry = { 0 };
4142 struct xe_vm_fault_entry *entry;
4143 int ret = 0, i = 0, count, entry_size;
4144
4145 entry_size = sizeof(struct xe_vm_fault);
4146 count = args->size / entry_size;
4147
4148 fault_list = kzalloc_objs(struct xe_vm_fault, count);
4149 if (!fault_list)
4150 return -ENOMEM;
4151
4152 spin_lock(&vm->faults.lock);
4153 list_for_each_entry(entry, &vm->faults.list, list) {
4154 if (i == count)
4155 break;
4156
4157 fault_entry.address = xe_device_canonicalize_addr(vm->xe, entry->address);
4158 fault_entry.address_precision = entry->address_precision;
4159
4160 fault_entry.access_type = xe_to_user_access_type(entry->access_type);
4161 fault_entry.fault_type = xe_to_user_fault_type(entry->fault_type);
4162 fault_entry.fault_level = xe_to_user_fault_level(entry->fault_level);
4163
4164 memcpy(&fault_list[i], &fault_entry, entry_size);
4165
4166 i++;
4167 }
4168 spin_unlock(&vm->faults.lock);
4169
4170 ret = copy_to_user(usr_ptr, fault_list, args->size);
4171
4172 kfree(fault_list);
4173 return ret ? -EFAULT : 0;
4174 }
4175
xe_vm_get_property_helper(struct xe_vm * vm,struct drm_xe_vm_get_property * args)4176 static int xe_vm_get_property_helper(struct xe_vm *vm,
4177 struct drm_xe_vm_get_property *args)
4178 {
4179 size_t size;
4180
4181 switch (args->property) {
4182 case DRM_XE_VM_GET_PROPERTY_FAULTS:
4183 spin_lock(&vm->faults.lock);
4184 size = size_mul(sizeof(struct xe_vm_fault), vm->faults.len);
4185 spin_unlock(&vm->faults.lock);
4186
4187 if (!args->size) {
4188 args->size = size;
4189 return 0;
4190 }
4191
4192 /*
4193 * Number of faults may increase between calls to
4194 * xe_vm_get_property_ioctl, so just report the number of
4195 * faults the user requests if it's less than or equal to
4196 * the number of faults in the VM fault array.
4197 *
4198 * We should also at least assert that the args->size value
4199 * is a multiple of the xe_vm_fault struct size.
4200 */
4201 if (args->size > size || args->size % sizeof(struct xe_vm_fault))
4202 return -EINVAL;
4203
4204 return fill_faults(vm, args);
4205 }
4206 return -EINVAL;
4207 }
4208
xe_vm_get_property_ioctl(struct drm_device * drm,void * data,struct drm_file * file)4209 int xe_vm_get_property_ioctl(struct drm_device *drm, void *data,
4210 struct drm_file *file)
4211 {
4212 struct xe_device *xe = to_xe_device(drm);
4213 struct xe_file *xef = to_xe_file(file);
4214 struct drm_xe_vm_get_property *args = data;
4215 struct xe_vm *vm;
4216 int ret = 0;
4217
4218 if (XE_IOCTL_DBG(xe, (args->reserved[0] || args->reserved[1] ||
4219 args->reserved[2] || args->extensions ||
4220 args->pad)))
4221 return -EINVAL;
4222
4223 vm = xe_vm_lookup(xef, args->vm_id);
4224 if (XE_IOCTL_DBG(xe, !vm))
4225 return -ENOENT;
4226
4227 ret = xe_vm_get_property_helper(vm, args);
4228
4229 xe_vm_put(vm);
4230 return ret;
4231 }
4232
4233 /**
4234 * xe_vm_bind_kernel_bo - bind a kernel BO to a VM
4235 * @vm: VM to bind the BO to
4236 * @bo: BO to bind
4237 * @q: exec queue to use for the bind (optional)
4238 * @addr: address at which to bind the BO
4239 * @cache_lvl: PAT cache level to use
4240 *
4241 * Execute a VM bind map operation on a kernel-owned BO to bind it into a
4242 * kernel-owned VM.
4243 *
4244 * Returns a dma_fence to track the binding completion if the job to do so was
4245 * successfully submitted, an error pointer otherwise.
4246 */
xe_vm_bind_kernel_bo(struct xe_vm * vm,struct xe_bo * bo,struct xe_exec_queue * q,u64 addr,enum xe_cache_level cache_lvl)4247 struct dma_fence *xe_vm_bind_kernel_bo(struct xe_vm *vm, struct xe_bo *bo,
4248 struct xe_exec_queue *q, u64 addr,
4249 enum xe_cache_level cache_lvl)
4250 {
4251 struct xe_vma_ops vops;
4252 struct drm_gpuva_ops *ops = NULL;
4253 struct dma_fence *fence;
4254 int err;
4255
4256 xe_bo_get(bo);
4257 xe_vm_get(vm);
4258 if (q)
4259 xe_exec_queue_get(q);
4260
4261 down_write(&vm->lock);
4262
4263 xe_vma_ops_init(&vops, vm, q, NULL, 0);
4264
4265 ops = vm_bind_ioctl_ops_create(vm, &vops, bo, 0, addr, xe_bo_size(bo),
4266 DRM_XE_VM_BIND_OP_MAP, 0, 0,
4267 xe_cache_pat_idx(vm->xe, cache_lvl));
4268 if (IS_ERR(ops)) {
4269 err = PTR_ERR(ops);
4270 goto release_vm_lock;
4271 }
4272
4273 err = vm_bind_ioctl_ops_parse(vm, ops, &vops);
4274 if (err)
4275 goto release_vm_lock;
4276
4277 xe_assert(vm->xe, !list_empty(&vops.list));
4278
4279 err = xe_vma_ops_alloc(&vops, false);
4280 if (err)
4281 goto unwind_ops;
4282
4283 fence = vm_bind_ioctl_ops_execute(vm, &vops);
4284 if (IS_ERR(fence))
4285 err = PTR_ERR(fence);
4286
4287 unwind_ops:
4288 if (err && err != -ENODATA)
4289 vm_bind_ioctl_ops_unwind(vm, &ops, 1);
4290
4291 xe_vma_ops_fini(&vops);
4292 drm_gpuva_ops_free(&vm->gpuvm, ops);
4293
4294 release_vm_lock:
4295 up_write(&vm->lock);
4296
4297 if (q)
4298 xe_exec_queue_put(q);
4299 xe_vm_put(vm);
4300 xe_bo_put(bo);
4301
4302 if (err)
4303 fence = ERR_PTR(err);
4304
4305 return fence;
4306 }
4307
4308 /**
4309 * xe_vm_lock() - Lock the vm's dma_resv object
4310 * @vm: The struct xe_vm whose lock is to be locked
4311 * @intr: Whether to perform any wait interruptible
4312 *
4313 * Return: 0 on success, -EINTR if @intr is true and the wait for a
4314 * contended lock was interrupted. If @intr is false, the function
4315 * always returns 0.
4316 */
xe_vm_lock(struct xe_vm * vm,bool intr)4317 int xe_vm_lock(struct xe_vm *vm, bool intr)
4318 {
4319 int ret;
4320
4321 if (intr)
4322 ret = dma_resv_lock_interruptible(xe_vm_resv(vm), NULL);
4323 else
4324 ret = dma_resv_lock(xe_vm_resv(vm), NULL);
4325
4326 return ret;
4327 }
4328
4329 /**
4330 * xe_vm_unlock() - Unlock the vm's dma_resv object
4331 * @vm: The struct xe_vm whose lock is to be released.
4332 *
4333 * Unlock a buffer object lock that was locked by xe_vm_lock().
4334 */
xe_vm_unlock(struct xe_vm * vm)4335 void xe_vm_unlock(struct xe_vm *vm)
4336 {
4337 dma_resv_unlock(xe_vm_resv(vm));
4338 }
4339
4340 /**
4341 * xe_vm_invalidate_vma_submit - Submit a job to invalidate GPU mappings for
4342 * VMA.
4343 * @vma: VMA to invalidate
4344 * @batch: TLB invalidation batch to populate; caller must later call
4345 * xe_tlb_inval_batch_wait() on it to wait for completion
4346 *
4347 * Walks a list of page tables leaves which it memset the entries owned by this
4348 * VMA to zero, invalidates the TLBs, but doesn't block waiting for TLB flush
4349 * to complete, but instead populates @batch which can be waited on using
4350 * xe_tlb_inval_batch_wait().
4351 *
4352 * Returns 0 for success, negative error code otherwise.
4353 */
xe_vm_invalidate_vma_submit(struct xe_vma * vma,struct xe_tlb_inval_batch * batch)4354 int xe_vm_invalidate_vma_submit(struct xe_vma *vma, struct xe_tlb_inval_batch *batch)
4355 {
4356 struct xe_device *xe = xe_vma_vm(vma)->xe;
4357 struct xe_vm *vm = xe_vma_vm(vma);
4358 struct xe_tile *tile;
4359 u8 tile_mask = 0;
4360 int ret = 0;
4361 u8 id;
4362
4363 xe_assert(xe, !xe_vma_is_null(vma));
4364 xe_assert(xe, !xe_vma_is_cpu_addr_mirror(vma));
4365 trace_xe_vma_invalidate(vma);
4366
4367 vm_dbg(&vm->xe->drm,
4368 "INVALIDATE: addr=0x%016llx, range=0x%016llx",
4369 xe_vma_start(vma), xe_vma_size(vma));
4370
4371 /*
4372 * Check that we don't race with page-table updates, tile_invalidated
4373 * update is safe
4374 */
4375 if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
4376 if (xe_vma_is_userptr(vma)) {
4377 lockdep_assert(lockdep_is_held_type(&vm->svm.gpusvm.notifier_lock, 0) ||
4378 (lockdep_is_held_type(&vm->svm.gpusvm.notifier_lock, 1) &&
4379 lockdep_is_held(&xe_vm_resv(vm)->lock.base)));
4380
4381 WARN_ON_ONCE(!mmu_interval_check_retry
4382 (&to_userptr_vma(vma)->userptr.notifier,
4383 to_userptr_vma(vma)->userptr.pages.notifier_seq));
4384 WARN_ON_ONCE(!dma_resv_test_signaled(xe_vm_resv(vm),
4385 DMA_RESV_USAGE_BOOKKEEP));
4386
4387 } else {
4388 xe_bo_assert_held(xe_vma_bo(vma));
4389 }
4390 }
4391
4392 for_each_tile(tile, xe, id)
4393 if (xe_pt_zap_ptes(tile, vma))
4394 tile_mask |= BIT(id);
4395
4396 xe_device_wmb(xe);
4397
4398 ret = xe_tlb_inval_range_tilemask_submit(xe, xe_vma_vm(vma)->usm.asid,
4399 xe_vma_start(vma), xe_vma_end(vma),
4400 tile_mask, batch);
4401
4402 /* WRITE_ONCE pairs with READ_ONCE in xe_vm_has_valid_gpu_mapping() */
4403 WRITE_ONCE(vma->tile_invalidated, vma->tile_mask);
4404 return ret;
4405 }
4406
4407 /**
4408 * xe_vm_invalidate_vma - invalidate GPU mappings for VMA without a lock
4409 * @vma: VMA to invalidate
4410 *
4411 * Walks a list of page tables leaves which it memset the entries owned by this
4412 * VMA to zero, invalidates the TLBs, and block until TLBs invalidation is
4413 * complete.
4414 *
4415 * Returns 0 for success, negative error code otherwise.
4416 */
xe_vm_invalidate_vma(struct xe_vma * vma)4417 int xe_vm_invalidate_vma(struct xe_vma *vma)
4418 {
4419 struct xe_tlb_inval_batch batch;
4420 int ret;
4421
4422 ret = xe_vm_invalidate_vma_submit(vma, &batch);
4423 if (ret)
4424 return ret;
4425
4426 xe_tlb_inval_batch_wait(&batch);
4427 return ret;
4428 }
4429
xe_vm_validate_protected(struct xe_vm * vm)4430 int xe_vm_validate_protected(struct xe_vm *vm)
4431 {
4432 struct drm_gpuva *gpuva;
4433 int err = 0;
4434
4435 if (!vm)
4436 return -ENODEV;
4437
4438 mutex_lock(&vm->snap_mutex);
4439
4440 drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
4441 struct xe_vma *vma = gpuva_to_vma(gpuva);
4442 struct xe_bo *bo = vma->gpuva.gem.obj ?
4443 gem_to_xe_bo(vma->gpuva.gem.obj) : NULL;
4444
4445 if (!bo)
4446 continue;
4447
4448 if (xe_bo_is_protected(bo)) {
4449 err = xe_pxp_bo_key_check(vm->xe->pxp, bo);
4450 if (err)
4451 break;
4452 }
4453 }
4454
4455 mutex_unlock(&vm->snap_mutex);
4456 return err;
4457 }
4458
4459 struct xe_vm_snapshot {
4460 int uapi_flags;
4461 unsigned long num_snaps;
4462 struct {
4463 u64 ofs, bo_ofs;
4464 unsigned long len;
4465 #define XE_VM_SNAP_FLAG_USERPTR BIT(0)
4466 #define XE_VM_SNAP_FLAG_READ_ONLY BIT(1)
4467 #define XE_VM_SNAP_FLAG_IS_NULL BIT(2)
4468 unsigned long flags;
4469 int uapi_mem_region;
4470 u16 pat_index;
4471 int cpu_caching;
4472 struct xe_bo *bo;
4473 void *data;
4474 struct mm_struct *mm;
4475 } snap[];
4476 };
4477
xe_vm_snapshot_capture(struct xe_vm * vm)4478 struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm)
4479 {
4480 unsigned long num_snaps = 0, i;
4481 struct xe_vm_snapshot *snap = NULL;
4482 struct drm_gpuva *gpuva;
4483
4484 if (!vm)
4485 return NULL;
4486
4487 mutex_lock(&vm->snap_mutex);
4488 drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
4489 if (gpuva->flags & XE_VMA_DUMPABLE)
4490 num_snaps++;
4491 }
4492
4493 if (num_snaps)
4494 snap = kvzalloc(offsetof(struct xe_vm_snapshot, snap[num_snaps]), GFP_NOWAIT);
4495 if (!snap) {
4496 snap = num_snaps ? ERR_PTR(-ENOMEM) : ERR_PTR(-ENODEV);
4497 goto out_unlock;
4498 }
4499
4500 if (vm->flags & XE_VM_FLAG_FAULT_MODE)
4501 snap->uapi_flags |= DRM_XE_VM_CREATE_FLAG_FAULT_MODE;
4502 if (vm->flags & XE_VM_FLAG_LR_MODE)
4503 snap->uapi_flags |= DRM_XE_VM_CREATE_FLAG_LR_MODE;
4504 if (vm->flags & XE_VM_FLAG_SCRATCH_PAGE)
4505 snap->uapi_flags |= DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE;
4506
4507 snap->num_snaps = num_snaps;
4508 i = 0;
4509 drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
4510 struct xe_vma *vma = gpuva_to_vma(gpuva);
4511 struct xe_bo *bo = vma->gpuva.gem.obj ?
4512 gem_to_xe_bo(vma->gpuva.gem.obj) : NULL;
4513
4514 if (!(gpuva->flags & XE_VMA_DUMPABLE))
4515 continue;
4516
4517 snap->snap[i].ofs = xe_vma_start(vma);
4518 snap->snap[i].len = xe_vma_size(vma);
4519 snap->snap[i].flags = xe_vma_read_only(vma) ?
4520 XE_VM_SNAP_FLAG_READ_ONLY : 0;
4521 snap->snap[i].pat_index = vma->attr.pat_index;
4522 if (bo) {
4523 snap->snap[i].cpu_caching = bo->cpu_caching;
4524 snap->snap[i].bo = xe_bo_get(bo);
4525 snap->snap[i].bo_ofs = xe_vma_bo_offset(vma);
4526 switch (bo->ttm.resource->mem_type) {
4527 case XE_PL_SYSTEM:
4528 case XE_PL_TT:
4529 snap->snap[i].uapi_mem_region = 0;
4530 break;
4531 case XE_PL_VRAM0:
4532 snap->snap[i].uapi_mem_region = 1;
4533 break;
4534 case XE_PL_VRAM1:
4535 snap->snap[i].uapi_mem_region = 2;
4536 break;
4537 }
4538 } else if (xe_vma_is_userptr(vma)) {
4539 struct mm_struct *mm =
4540 to_userptr_vma(vma)->userptr.notifier.mm;
4541
4542 if (mmget_not_zero(mm))
4543 snap->snap[i].mm = mm;
4544 else
4545 snap->snap[i].data = ERR_PTR(-EFAULT);
4546
4547 snap->snap[i].bo_ofs = xe_vma_userptr(vma);
4548 snap->snap[i].flags |= XE_VM_SNAP_FLAG_USERPTR;
4549 snap->snap[i].uapi_mem_region = 0;
4550 } else if (xe_vma_is_null(vma)) {
4551 snap->snap[i].flags |= XE_VM_SNAP_FLAG_IS_NULL;
4552 snap->snap[i].uapi_mem_region = -1;
4553 } else {
4554 snap->snap[i].data = ERR_PTR(-ENOENT);
4555 snap->snap[i].uapi_mem_region = -1;
4556 }
4557 i++;
4558 }
4559
4560 out_unlock:
4561 mutex_unlock(&vm->snap_mutex);
4562 return snap;
4563 }
4564
xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot * snap)4565 void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap)
4566 {
4567 if (IS_ERR_OR_NULL(snap))
4568 return;
4569
4570 for (int i = 0; i < snap->num_snaps; i++) {
4571 struct xe_bo *bo = snap->snap[i].bo;
4572 int err;
4573
4574 if (IS_ERR(snap->snap[i].data) ||
4575 snap->snap[i].flags & XE_VM_SNAP_FLAG_IS_NULL)
4576 continue;
4577
4578 snap->snap[i].data = kvmalloc(snap->snap[i].len, GFP_USER);
4579 if (!snap->snap[i].data) {
4580 snap->snap[i].data = ERR_PTR(-ENOMEM);
4581 goto cleanup_bo;
4582 }
4583
4584 if (bo) {
4585 err = xe_bo_read(bo, snap->snap[i].bo_ofs,
4586 snap->snap[i].data, snap->snap[i].len);
4587 } else {
4588 void __user *userptr = (void __user *)(size_t)snap->snap[i].bo_ofs;
4589
4590 kthread_use_mm(snap->snap[i].mm);
4591 if (!copy_from_user(snap->snap[i].data, userptr, snap->snap[i].len))
4592 err = 0;
4593 else
4594 err = -EFAULT;
4595 kthread_unuse_mm(snap->snap[i].mm);
4596
4597 mmput(snap->snap[i].mm);
4598 snap->snap[i].mm = NULL;
4599 }
4600
4601 if (err) {
4602 kvfree(snap->snap[i].data);
4603 snap->snap[i].data = ERR_PTR(err);
4604 }
4605
4606 cleanup_bo:
4607 xe_bo_put(bo);
4608 snap->snap[i].bo = NULL;
4609 }
4610 }
4611
xe_vm_snapshot_print(struct xe_vm_snapshot * snap,struct drm_printer * p)4612 void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p)
4613 {
4614 unsigned long i, j;
4615
4616 if (IS_ERR_OR_NULL(snap)) {
4617 drm_printf(p, "[0].error: %li\n", PTR_ERR(snap));
4618 return;
4619 }
4620
4621 drm_printf(p, "VM.uapi_flags: 0x%x\n", snap->uapi_flags);
4622 for (i = 0; i < snap->num_snaps; i++) {
4623 drm_printf(p, "[%llx].length: 0x%lx\n", snap->snap[i].ofs, snap->snap[i].len);
4624
4625 drm_printf(p, "[%llx].properties: %s|%s|mem_region=0x%lx|pat_index=%d|cpu_caching=%d\n",
4626 snap->snap[i].ofs,
4627 snap->snap[i].flags & XE_VM_SNAP_FLAG_READ_ONLY ?
4628 "read_only" : "read_write",
4629 snap->snap[i].flags & XE_VM_SNAP_FLAG_IS_NULL ?
4630 "null_sparse" :
4631 snap->snap[i].flags & XE_VM_SNAP_FLAG_USERPTR ?
4632 "userptr" : "bo",
4633 snap->snap[i].uapi_mem_region == -1 ? 0 :
4634 BIT(snap->snap[i].uapi_mem_region),
4635 snap->snap[i].pat_index,
4636 snap->snap[i].cpu_caching);
4637
4638 if (IS_ERR(snap->snap[i].data)) {
4639 drm_printf(p, "[%llx].error: %li\n", snap->snap[i].ofs,
4640 PTR_ERR(snap->snap[i].data));
4641 continue;
4642 }
4643
4644 if (snap->snap[i].flags & XE_VM_SNAP_FLAG_IS_NULL)
4645 continue;
4646
4647 drm_printf(p, "[%llx].data: ", snap->snap[i].ofs);
4648
4649 for (j = 0; j < snap->snap[i].len; j += sizeof(u32)) {
4650 u32 *val = snap->snap[i].data + j;
4651 char dumped[ASCII85_BUFSZ];
4652
4653 drm_puts(p, ascii85_encode(*val, dumped));
4654 }
4655
4656 drm_puts(p, "\n");
4657
4658 if (drm_coredump_printer_is_full(p))
4659 return;
4660 }
4661 }
4662
xe_vm_snapshot_free(struct xe_vm_snapshot * snap)4663 void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
4664 {
4665 unsigned long i;
4666
4667 if (IS_ERR_OR_NULL(snap))
4668 return;
4669
4670 for (i = 0; i < snap->num_snaps; i++) {
4671 if (!IS_ERR(snap->snap[i].data))
4672 kvfree(snap->snap[i].data);
4673 xe_bo_put(snap->snap[i].bo);
4674 if (snap->snap[i].mm)
4675 mmput(snap->snap[i].mm);
4676 }
4677 kvfree(snap);
4678 }
4679
4680 /**
4681 * xe_vma_need_vram_for_atomic - Check if VMA needs VRAM migration for atomic operations
4682 * @xe: Pointer to the Xe device structure
4683 * @vma: Pointer to the virtual memory area (VMA) structure
4684 * @is_atomic: In pagefault path and atomic operation
4685 *
4686 * This function determines whether the given VMA needs to be migrated to
4687 * VRAM in order to do atomic GPU operation.
4688 *
4689 * Return:
4690 * 1 - Migration to VRAM is required
4691 * 0 - Migration is not required
4692 * -EACCES - Invalid access for atomic memory attr
4693 *
4694 */
xe_vma_need_vram_for_atomic(struct xe_device * xe,struct xe_vma * vma,bool is_atomic)4695 int xe_vma_need_vram_for_atomic(struct xe_device *xe, struct xe_vma *vma, bool is_atomic)
4696 {
4697 u32 atomic_access = xe_vma_bo(vma) ? xe_vma_bo(vma)->attr.atomic_access :
4698 vma->attr.atomic_access;
4699
4700 if (!IS_DGFX(xe) || !is_atomic)
4701 return false;
4702
4703 /*
4704 * NOTE: The checks implemented here are platform-specific. For
4705 * instance, on a device supporting CXL atomics, these would ideally
4706 * work universally without additional handling.
4707 */
4708 switch (atomic_access) {
4709 case DRM_XE_ATOMIC_DEVICE:
4710 return !xe->info.has_device_atomics_on_smem;
4711
4712 case DRM_XE_ATOMIC_CPU:
4713 return -EACCES;
4714
4715 case DRM_XE_ATOMIC_UNDEFINED:
4716 case DRM_XE_ATOMIC_GLOBAL:
4717 default:
4718 return 1;
4719 }
4720 }
4721
xe_vm_alloc_vma(struct xe_vm * vm,struct drm_gpuvm_map_req * map_req,bool is_madvise)4722 static int xe_vm_alloc_vma(struct xe_vm *vm,
4723 struct drm_gpuvm_map_req *map_req,
4724 bool is_madvise)
4725 {
4726 struct xe_vma_ops vops;
4727 struct drm_gpuva_ops *ops = NULL;
4728 struct drm_gpuva_op *__op;
4729 unsigned int vma_flags = 0;
4730 bool remap_op = false;
4731 struct xe_vma_mem_attr tmp_attr = {};
4732 u16 default_pat;
4733 int err;
4734
4735 lockdep_assert_held_write(&vm->lock);
4736
4737 if (is_madvise)
4738 ops = drm_gpuvm_madvise_ops_create(&vm->gpuvm, map_req);
4739 else
4740 ops = drm_gpuvm_sm_map_ops_create(&vm->gpuvm, map_req);
4741
4742 if (IS_ERR(ops))
4743 return PTR_ERR(ops);
4744
4745 if (list_empty(&ops->list)) {
4746 err = 0;
4747 goto free_ops;
4748 }
4749
4750 drm_gpuva_for_each_op(__op, ops) {
4751 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
4752 struct xe_vma *vma = NULL;
4753
4754 if (!is_madvise) {
4755 if (__op->op == DRM_GPUVA_OP_UNMAP) {
4756 vma = gpuva_to_vma(op->base.unmap.va);
4757 XE_WARN_ON(!xe_vma_has_default_mem_attrs(vma));
4758 default_pat = vma->attr.default_pat_index;
4759 vma_flags = vma->gpuva.flags;
4760 }
4761
4762 if (__op->op == DRM_GPUVA_OP_REMAP) {
4763 vma = gpuva_to_vma(op->base.remap.unmap->va);
4764 default_pat = vma->attr.default_pat_index;
4765 vma_flags = vma->gpuva.flags;
4766 }
4767
4768 if (__op->op == DRM_GPUVA_OP_MAP) {
4769 op->map.vma_flags |= vma_flags & XE_VMA_CREATE_MASK;
4770 op->map.pat_index = default_pat;
4771 }
4772 } else {
4773 if (__op->op == DRM_GPUVA_OP_REMAP) {
4774 vma = gpuva_to_vma(op->base.remap.unmap->va);
4775 xe_assert(vm->xe, !remap_op);
4776 xe_assert(vm->xe, xe_vma_has_no_bo(vma));
4777 remap_op = true;
4778 vma_flags = vma->gpuva.flags;
4779 }
4780
4781 if (__op->op == DRM_GPUVA_OP_MAP) {
4782 xe_assert(vm->xe, remap_op);
4783 remap_op = false;
4784 /*
4785 * In case of madvise ops DRM_GPUVA_OP_MAP is
4786 * always after DRM_GPUVA_OP_REMAP, so ensure
4787 * to propagate the flags from the vma we're
4788 * unmapping.
4789 */
4790 op->map.vma_flags |= vma_flags & XE_VMA_CREATE_MASK;
4791 }
4792 }
4793 print_op(vm->xe, __op);
4794 }
4795
4796 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
4797
4798 if (is_madvise)
4799 vops.flags |= XE_VMA_OPS_FLAG_MADVISE;
4800 else
4801 vops.flags |= XE_VMA_OPS_FLAG_ALLOW_SVM_UNMAP;
4802
4803 err = vm_bind_ioctl_ops_parse(vm, ops, &vops);
4804 if (err)
4805 goto unwind_ops;
4806
4807 xe_vm_lock(vm, false);
4808
4809 drm_gpuva_for_each_op(__op, ops) {
4810 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
4811 struct xe_vma *vma;
4812
4813 if (__op->op == DRM_GPUVA_OP_UNMAP) {
4814 vma = gpuva_to_vma(op->base.unmap.va);
4815 /* There should be no unmap for madvise */
4816 if (is_madvise)
4817 XE_WARN_ON("UNEXPECTED UNMAP");
4818
4819 xe_vma_destroy(vma, NULL);
4820 } else if (__op->op == DRM_GPUVA_OP_REMAP) {
4821 vma = gpuva_to_vma(op->base.remap.unmap->va);
4822 /* In case of madvise ops Store attributes for REMAP UNMAPPED
4823 * VMA, so they can be assigned to newly MAP created vma.
4824 */
4825 if (is_madvise)
4826 xe_vma_mem_attr_copy(&tmp_attr, &vma->attr);
4827
4828 xe_vma_destroy(gpuva_to_vma(op->base.remap.unmap->va), NULL);
4829 } else if (__op->op == DRM_GPUVA_OP_MAP) {
4830 vma = op->map.vma;
4831 /* In case of madvise call, MAP will always be followed by REMAP.
4832 * Therefore temp_attr will always have sane values, making it safe to
4833 * copy them to new vma.
4834 */
4835 if (is_madvise)
4836 xe_vma_mem_attr_copy(&vma->attr, &tmp_attr);
4837 }
4838 }
4839
4840 xe_vm_unlock(vm);
4841 drm_gpuva_ops_free(&vm->gpuvm, ops);
4842 xe_vma_mem_attr_fini(&tmp_attr);
4843 return 0;
4844
4845 unwind_ops:
4846 vm_bind_ioctl_ops_unwind(vm, &ops, 1);
4847 free_ops:
4848 drm_gpuva_ops_free(&vm->gpuvm, ops);
4849 return err;
4850 }
4851
4852 /**
4853 * xe_vm_alloc_madvise_vma - Allocate VMA's with madvise ops
4854 * @vm: Pointer to the xe_vm structure
4855 * @start: Starting input address
4856 * @range: Size of the input range
4857 *
4858 * This function splits existing vma to create new vma for user provided input range
4859 *
4860 * Return: 0 if success
4861 */
xe_vm_alloc_madvise_vma(struct xe_vm * vm,uint64_t start,uint64_t range)4862 int xe_vm_alloc_madvise_vma(struct xe_vm *vm, uint64_t start, uint64_t range)
4863 {
4864 struct drm_gpuvm_map_req map_req = {
4865 .map.va.addr = start,
4866 .map.va.range = range,
4867 };
4868
4869 lockdep_assert_held_write(&vm->lock);
4870
4871 vm_dbg(&vm->xe->drm, "MADVISE_OPS_CREATE: addr=0x%016llx, size=0x%016llx", start, range);
4872
4873 return xe_vm_alloc_vma(vm, &map_req, true);
4874 }
4875
is_cpu_addr_vma_with_default_attr(struct xe_vma * vma)4876 static bool is_cpu_addr_vma_with_default_attr(struct xe_vma *vma)
4877 {
4878 return vma && xe_vma_is_cpu_addr_mirror(vma) &&
4879 xe_vma_has_default_mem_attrs(vma);
4880 }
4881
4882 /**
4883 * xe_vm_find_cpu_addr_mirror_vma_range - Extend a VMA range to include adjacent CPU-mirrored VMAs
4884 * @vm: VM to search within
4885 * @start: Input/output pointer to the starting address of the range
4886 * @end: Input/output pointer to the end address of the range
4887 *
4888 * Given a range defined by @start and @range, this function checks the VMAs
4889 * immediately before and after the range. If those neighboring VMAs are
4890 * CPU-address-mirrored and have default memory attributes, the function
4891 * updates @start and @range to include them. This extended range can then
4892 * be used for merging or other operations that require a unified VMA.
4893 *
4894 * The function does not perform the merge itself; it only computes the
4895 * mergeable boundaries.
4896 */
xe_vm_find_cpu_addr_mirror_vma_range(struct xe_vm * vm,u64 * start,u64 * end)4897 void xe_vm_find_cpu_addr_mirror_vma_range(struct xe_vm *vm, u64 *start, u64 *end)
4898 {
4899 struct xe_vma *prev, *next;
4900
4901 lockdep_assert_held(&vm->lock);
4902
4903 if (*start >= SZ_4K) {
4904 prev = xe_vm_find_vma_by_addr(vm, *start - SZ_4K);
4905 if (is_cpu_addr_vma_with_default_attr(prev))
4906 *start = xe_vma_start(prev);
4907 }
4908
4909 if (*end < vm->size) {
4910 next = xe_vm_find_vma_by_addr(vm, *end + 1);
4911 if (is_cpu_addr_vma_with_default_attr(next))
4912 *end = xe_vma_end(next);
4913 }
4914 }
4915
4916 /**
4917 * xe_vm_alloc_cpu_addr_mirror_vma - Allocate CPU addr mirror vma
4918 * @vm: Pointer to the xe_vm structure
4919 * @start: Starting input address
4920 * @range: Size of the input range
4921 *
4922 * This function splits/merges existing vma to create new vma for user provided input range
4923 *
4924 * Return: 0 if success
4925 */
xe_vm_alloc_cpu_addr_mirror_vma(struct xe_vm * vm,uint64_t start,uint64_t range)4926 int xe_vm_alloc_cpu_addr_mirror_vma(struct xe_vm *vm, uint64_t start, uint64_t range)
4927 {
4928 struct drm_gpuvm_map_req map_req = {
4929 .map.va.addr = start,
4930 .map.va.range = range,
4931 };
4932
4933 lockdep_assert_held_write(&vm->lock);
4934
4935 vm_dbg(&vm->xe->drm, "CPU_ADDR_MIRROR_VMA_OPS_CREATE: addr=0x%016llx, size=0x%016llx",
4936 start, range);
4937
4938 return xe_vm_alloc_vma(vm, &map_req, false);
4939 }
4940
4941 /**
4942 * xe_vm_add_exec_queue() - Add exec queue to VM
4943 * @vm: The VM.
4944 * @q: The exec_queue
4945 *
4946 * Add exec queue to VM, skipped if the device does not have context based TLB
4947 * invalidations.
4948 */
xe_vm_add_exec_queue(struct xe_vm * vm,struct xe_exec_queue * q)4949 void xe_vm_add_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
4950 {
4951 struct xe_device *xe = vm->xe;
4952
4953 /* User VMs and queues only */
4954 xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_KERNEL));
4955 xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_PERMANENT));
4956 xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_VM));
4957 xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_MIGRATE));
4958 xe_assert(xe, vm->xef);
4959 xe_assert(xe, vm == q->vm);
4960
4961 if (!xe->info.has_ctx_tlb_inval)
4962 return;
4963
4964 down_write(&vm->exec_queues.lock);
4965 list_add(&q->vm_exec_queue_link, &vm->exec_queues.list[q->gt->info.id]);
4966 ++vm->exec_queues.count[q->gt->info.id];
4967 up_write(&vm->exec_queues.lock);
4968 }
4969
4970 /**
4971 * xe_vm_remove_exec_queue() - Remove exec queue from VM
4972 * @vm: The VM.
4973 * @q: The exec_queue
4974 *
4975 * Remove exec queue from VM, skipped if the device does not have context based
4976 * TLB invalidations.
4977 */
xe_vm_remove_exec_queue(struct xe_vm * vm,struct xe_exec_queue * q)4978 void xe_vm_remove_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
4979 {
4980 if (!vm->xe->info.has_ctx_tlb_inval)
4981 return;
4982
4983 down_write(&vm->exec_queues.lock);
4984 if (!list_empty(&q->vm_exec_queue_link)) {
4985 list_del(&q->vm_exec_queue_link);
4986 --vm->exec_queues.count[q->gt->info.id];
4987 }
4988 up_write(&vm->exec_queues.lock);
4989 }
4990