1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
8 #include <linux/dma-fence-array.h>
9 #include <linux/nospec.h>
11 #include <drm/drm_exec.h>
12 #include <drm/drm_print.h>
13 #include <drm/ttm/ttm_execbuf_util.h>
14 #include <drm/ttm/ttm_tt.h>
15 #include <drm/xe_drm.h>
16 #include <linux/delay.h>
17 #include <linux/kthread.h>
19 #include <linux/swap.h>
21 #include "xe_assert.h"
23 #include "xe_device.h"
24 #include "xe_drm_client.h"
25 #include "xe_exec_queue.h"
27 #include "xe_gt_pagefault.h"
28 #include "xe_gt_tlb_invalidation.h"
29 #include "xe_migrate.h"
32 #include "xe_preempt_fence.h"
34 #include "xe_res_cursor.h"
37 #include "generated/xe_wa_oob.h"
40 #define TEST_VM_ASYNC_OPS_ERROR
42 static struct drm_gem_object *xe_vm_obj(struct xe_vm *vm)
44 return vm->gpuvm.r_obj;
48 * xe_vma_userptr_check_repin() - Advisory check for repin needed
49 * @vma: The userptr vma
51 * Check if the userptr vma has been invalidated since last successful
52 * repin. The check is advisory only and can the function can be called
53 * without the vm->userptr.notifier_lock held. There is no guarantee that the
54 * vma userptr will remain valid after a lockless check, so typically
55 * the call needs to be followed by a proper check under the notifier_lock.
57 * Return: 0 if userptr vma is valid, -EAGAIN otherwise; repin recommended.
59 int xe_vma_userptr_check_repin(struct xe_vma *vma)
61 return mmu_interval_check_retry(&vma->userptr.notifier,
62 vma->userptr.notifier_seq) ?
66 int xe_vma_userptr_pin_pages(struct xe_vma *vma)
68 struct xe_vm *vm = xe_vma_vm(vma);
69 struct xe_device *xe = vm->xe;
70 const unsigned long num_pages = xe_vma_size(vma) >> PAGE_SHIFT;
72 bool in_kthread = !current->mm;
73 unsigned long notifier_seq;
75 bool read_only = xe_vma_read_only(vma);
77 lockdep_assert_held(&vm->lock);
78 xe_assert(xe, xe_vma_is_userptr(vma));
80 if (vma->gpuva.flags & XE_VMA_DESTROYED)
83 notifier_seq = mmu_interval_read_begin(&vma->userptr.notifier);
84 if (notifier_seq == vma->userptr.notifier_seq)
87 pages = kvmalloc_array(num_pages, sizeof(*pages), GFP_KERNEL);
91 if (vma->userptr.sg) {
92 dma_unmap_sgtable(xe->drm.dev,
94 read_only ? DMA_TO_DEVICE :
95 DMA_BIDIRECTIONAL, 0);
96 sg_free_table(vma->userptr.sg);
97 vma->userptr.sg = NULL;
102 if (!mmget_not_zero(vma->userptr.notifier.mm)) {
106 kthread_use_mm(vma->userptr.notifier.mm);
109 while (pinned < num_pages) {
110 ret = get_user_pages_fast(xe_vma_userptr(vma) +
113 read_only ? 0 : FOLL_WRITE,
126 kthread_unuse_mm(vma->userptr.notifier.mm);
127 mmput(vma->userptr.notifier.mm);
133 ret = sg_alloc_table_from_pages_segment(&vma->userptr.sgt, pages,
135 (u64)pinned << PAGE_SHIFT,
136 xe_sg_segment_size(xe->drm.dev),
139 vma->userptr.sg = NULL;
142 vma->userptr.sg = &vma->userptr.sgt;
144 ret = dma_map_sgtable(xe->drm.dev, vma->userptr.sg,
145 read_only ? DMA_TO_DEVICE :
147 DMA_ATTR_SKIP_CPU_SYNC |
148 DMA_ATTR_NO_KERNEL_MAPPING);
150 sg_free_table(vma->userptr.sg);
151 vma->userptr.sg = NULL;
155 for (i = 0; i < pinned; ++i) {
158 set_page_dirty(pages[i]);
159 unlock_page(pages[i]);
162 mark_page_accessed(pages[i]);
166 release_pages(pages, pinned);
170 vma->userptr.notifier_seq = notifier_seq;
171 if (xe_vma_userptr_check_repin(vma) == -EAGAIN)
175 return ret < 0 ? ret : 0;
178 static bool preempt_fences_waiting(struct xe_vm *vm)
180 struct xe_exec_queue *q;
182 lockdep_assert_held(&vm->lock);
183 xe_vm_assert_held(vm);
185 list_for_each_entry(q, &vm->preempt.exec_queues, compute.link) {
186 if (!q->compute.pfence ||
187 (q->compute.pfence && test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
188 &q->compute.pfence->flags))) {
196 static void free_preempt_fences(struct list_head *list)
198 struct list_head *link, *next;
200 list_for_each_safe(link, next, list)
201 xe_preempt_fence_free(to_preempt_fence_from_link(link));
204 static int alloc_preempt_fences(struct xe_vm *vm, struct list_head *list,
207 lockdep_assert_held(&vm->lock);
208 xe_vm_assert_held(vm);
210 if (*count >= vm->preempt.num_exec_queues)
213 for (; *count < vm->preempt.num_exec_queues; ++(*count)) {
214 struct xe_preempt_fence *pfence = xe_preempt_fence_alloc();
217 return PTR_ERR(pfence);
219 list_move_tail(xe_preempt_fence_link(pfence), list);
225 static int wait_for_existing_preempt_fences(struct xe_vm *vm)
227 struct xe_exec_queue *q;
229 xe_vm_assert_held(vm);
231 list_for_each_entry(q, &vm->preempt.exec_queues, compute.link) {
232 if (q->compute.pfence) {
233 long timeout = dma_fence_wait(q->compute.pfence, false);
237 dma_fence_put(q->compute.pfence);
238 q->compute.pfence = NULL;
245 static bool xe_vm_is_idle(struct xe_vm *vm)
247 struct xe_exec_queue *q;
249 xe_vm_assert_held(vm);
250 list_for_each_entry(q, &vm->preempt.exec_queues, compute.link) {
251 if (!xe_exec_queue_is_idle(q))
258 static void arm_preempt_fences(struct xe_vm *vm, struct list_head *list)
260 struct list_head *link;
261 struct xe_exec_queue *q;
263 list_for_each_entry(q, &vm->preempt.exec_queues, compute.link) {
264 struct dma_fence *fence;
267 xe_assert(vm->xe, link != list);
269 fence = xe_preempt_fence_arm(to_preempt_fence_from_link(link),
270 q, q->compute.context,
272 dma_fence_put(q->compute.pfence);
273 q->compute.pfence = fence;
277 static int add_preempt_fences(struct xe_vm *vm, struct xe_bo *bo)
279 struct xe_exec_queue *q;
282 if (!vm->preempt.num_exec_queues)
285 err = xe_bo_lock(bo, true);
289 err = dma_resv_reserve_fences(bo->ttm.base.resv, vm->preempt.num_exec_queues);
293 list_for_each_entry(q, &vm->preempt.exec_queues, compute.link)
294 if (q->compute.pfence) {
295 dma_resv_add_fence(bo->ttm.base.resv,
297 DMA_RESV_USAGE_BOOKKEEP);
305 static void resume_and_reinstall_preempt_fences(struct xe_vm *vm,
306 struct drm_exec *exec)
308 struct xe_exec_queue *q;
310 lockdep_assert_held(&vm->lock);
311 xe_vm_assert_held(vm);
313 list_for_each_entry(q, &vm->preempt.exec_queues, compute.link) {
316 drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, q->compute.pfence,
317 DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
321 int xe_vm_add_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
323 struct drm_gpuvm_exec vm_exec = {
325 .flags = DRM_EXEC_INTERRUPTIBLE_WAIT,
328 struct drm_exec *exec = &vm_exec.exec;
329 struct dma_fence *pfence;
333 xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm));
335 down_write(&vm->lock);
336 err = drm_gpuvm_exec_lock(&vm_exec);
340 pfence = xe_preempt_fence_create(q, q->compute.context,
347 list_add(&q->compute.link, &vm->preempt.exec_queues);
348 ++vm->preempt.num_exec_queues;
349 q->compute.pfence = pfence;
351 down_read(&vm->userptr.notifier_lock);
353 drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, pfence,
354 DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
357 * Check to see if a preemption on VM is in flight or userptr
358 * invalidation, if so trigger this preempt fence to sync state with
359 * other preempt fences on the VM.
361 wait = __xe_vm_userptr_needs_repin(vm) || preempt_fences_waiting(vm);
363 dma_fence_enable_sw_signaling(pfence);
365 up_read(&vm->userptr.notifier_lock);
375 * xe_vm_remove_compute_exec_queue() - Remove compute exec queue from VM
379 void xe_vm_remove_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
381 if (!xe_vm_in_preempt_fence_mode(vm))
384 down_write(&vm->lock);
385 list_del(&q->compute.link);
386 --vm->preempt.num_exec_queues;
387 if (q->compute.pfence) {
388 dma_fence_enable_sw_signaling(q->compute.pfence);
389 dma_fence_put(q->compute.pfence);
390 q->compute.pfence = NULL;
396 * __xe_vm_userptr_needs_repin() - Check whether the VM does have userptrs
397 * that need repinning.
400 * This function checks for whether the VM has userptrs that need repinning,
401 * and provides a release-type barrier on the userptr.notifier_lock after
404 * Return: 0 if there are no userptrs needing repinning, -EAGAIN if there are.
406 int __xe_vm_userptr_needs_repin(struct xe_vm *vm)
408 lockdep_assert_held_read(&vm->userptr.notifier_lock);
410 return (list_empty(&vm->userptr.repin_list) &&
411 list_empty(&vm->userptr.invalidated)) ? 0 : -EAGAIN;
414 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
416 static void xe_vm_kill(struct xe_vm *vm)
418 struct xe_exec_queue *q;
420 lockdep_assert_held(&vm->lock);
422 xe_vm_lock(vm, false);
423 vm->flags |= XE_VM_FLAG_BANNED;
424 trace_xe_vm_kill(vm);
426 list_for_each_entry(q, &vm->preempt.exec_queues, compute.link)
430 /* TODO: Inform user the VM is banned */
434 * xe_vm_validate_should_retry() - Whether to retry after a validate error.
435 * @exec: The drm_exec object used for locking before validation.
436 * @err: The error returned from ttm_bo_validate().
437 * @end: A ktime_t cookie that should be set to 0 before first use and
438 * that should be reused on subsequent calls.
440 * With multiple active VMs, under memory pressure, it is possible that
441 * ttm_bo_validate() run into -EDEADLK and in such case returns -ENOMEM.
442 * Until ttm properly handles locking in such scenarios, best thing the
443 * driver can do is retry with a timeout. Check if that is necessary, and
444 * if so unlock the drm_exec's objects while keeping the ticket to prepare
447 * Return: true if a retry after drm_exec_init() is recommended;
450 bool xe_vm_validate_should_retry(struct drm_exec *exec, int err, ktime_t *end)
458 *end = *end ? : ktime_add_ms(cur, XE_VM_REBIND_RETRY_TIMEOUT_MS);
459 if (!ktime_before(cur, *end))
466 static int xe_gpuvm_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec)
468 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
469 struct drm_gpuva *gpuva;
472 lockdep_assert_held(&vm->lock);
473 drm_gpuvm_bo_for_each_va(gpuva, vm_bo)
474 list_move_tail(&gpuva_to_vma(gpuva)->combined_links.rebind,
477 ret = xe_bo_validate(gem_to_xe_bo(vm_bo->obj), vm, false);
481 vm_bo->evicted = false;
485 static int xe_preempt_work_begin(struct drm_exec *exec, struct xe_vm *vm,
491 * 1 fence for each preempt fence plus a fence for each tile from a
494 err = drm_gpuvm_prepare_vm(&vm->gpuvm, exec, vm->preempt.num_exec_queues +
495 vm->xe->info.tile_count);
499 if (xe_vm_is_idle(vm)) {
500 vm->preempt.rebind_deactivated = true;
505 if (!preempt_fences_waiting(vm)) {
510 err = drm_gpuvm_prepare_objects(&vm->gpuvm, exec, vm->preempt.num_exec_queues);
514 err = wait_for_existing_preempt_fences(vm);
518 return drm_gpuvm_validate(&vm->gpuvm, exec);
521 static void preempt_rebind_work_func(struct work_struct *w)
523 struct xe_vm *vm = container_of(w, struct xe_vm, preempt.rebind_work);
524 struct drm_exec exec;
525 struct dma_fence *rebind_fence;
526 unsigned int fence_count = 0;
527 LIST_HEAD(preempt_fences);
531 int __maybe_unused tries = 0;
533 xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm));
534 trace_xe_vm_rebind_worker_enter(vm);
536 down_write(&vm->lock);
538 if (xe_vm_is_closed_or_banned(vm)) {
540 trace_xe_vm_rebind_worker_exit(vm);
545 if (xe_vm_userptr_check_repin(vm)) {
546 err = xe_vm_userptr_pin(vm);
548 goto out_unlock_outer;
551 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
553 drm_exec_until_all_locked(&exec) {
556 err = xe_preempt_work_begin(&exec, vm, &done);
557 drm_exec_retry_on_contention(&exec);
559 drm_exec_fini(&exec);
560 if (err && xe_vm_validate_should_retry(&exec, err, &end))
563 goto out_unlock_outer;
567 err = alloc_preempt_fences(vm, &preempt_fences, &fence_count);
571 rebind_fence = xe_vm_rebind(vm, true);
572 if (IS_ERR(rebind_fence)) {
573 err = PTR_ERR(rebind_fence);
578 dma_fence_wait(rebind_fence, false);
579 dma_fence_put(rebind_fence);
582 /* Wait on munmap style VM unbinds */
583 wait = dma_resv_wait_timeout(xe_vm_resv(vm),
584 DMA_RESV_USAGE_KERNEL,
585 false, MAX_SCHEDULE_TIMEOUT);
591 #define retry_required(__tries, __vm) \
592 (IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT) ? \
593 (!(__tries)++ || __xe_vm_userptr_needs_repin(__vm)) : \
594 __xe_vm_userptr_needs_repin(__vm))
596 down_read(&vm->userptr.notifier_lock);
597 if (retry_required(tries, vm)) {
598 up_read(&vm->userptr.notifier_lock);
603 #undef retry_required
605 spin_lock(&vm->xe->ttm.lru_lock);
606 ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
607 spin_unlock(&vm->xe->ttm.lru_lock);
609 /* Point of no return. */
610 arm_preempt_fences(vm, &preempt_fences);
611 resume_and_reinstall_preempt_fences(vm, &exec);
612 up_read(&vm->userptr.notifier_lock);
615 drm_exec_fini(&exec);
617 if (err == -EAGAIN) {
618 trace_xe_vm_rebind_worker_retry(vm);
623 drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
628 free_preempt_fences(&preempt_fences);
630 trace_xe_vm_rebind_worker_exit(vm);
633 static bool vma_userptr_invalidate(struct mmu_interval_notifier *mni,
634 const struct mmu_notifier_range *range,
635 unsigned long cur_seq)
637 struct xe_vma *vma = container_of(mni, struct xe_vma, userptr.notifier);
638 struct xe_vm *vm = xe_vma_vm(vma);
639 struct dma_resv_iter cursor;
640 struct dma_fence *fence;
643 xe_assert(vm->xe, xe_vma_is_userptr(vma));
644 trace_xe_vma_userptr_invalidate(vma);
646 if (!mmu_notifier_range_blockable(range))
649 down_write(&vm->userptr.notifier_lock);
650 mmu_interval_set_seq(mni, cur_seq);
652 /* No need to stop gpu access if the userptr is not yet bound. */
653 if (!vma->userptr.initial_bind) {
654 up_write(&vm->userptr.notifier_lock);
659 * Tell exec and rebind worker they need to repin and rebind this
662 if (!xe_vm_in_fault_mode(vm) &&
663 !(vma->gpuva.flags & XE_VMA_DESTROYED) && vma->tile_present) {
664 spin_lock(&vm->userptr.invalidated_lock);
665 list_move_tail(&vma->userptr.invalidate_link,
666 &vm->userptr.invalidated);
667 spin_unlock(&vm->userptr.invalidated_lock);
670 up_write(&vm->userptr.notifier_lock);
673 * Preempt fences turn into schedule disables, pipeline these.
674 * Note that even in fault mode, we need to wait for binds and
675 * unbinds to complete, and those are attached as BOOKMARK fences
678 dma_resv_iter_begin(&cursor, xe_vm_resv(vm),
679 DMA_RESV_USAGE_BOOKKEEP);
680 dma_resv_for_each_fence_unlocked(&cursor, fence)
681 dma_fence_enable_sw_signaling(fence);
682 dma_resv_iter_end(&cursor);
684 err = dma_resv_wait_timeout(xe_vm_resv(vm),
685 DMA_RESV_USAGE_BOOKKEEP,
686 false, MAX_SCHEDULE_TIMEOUT);
687 XE_WARN_ON(err <= 0);
689 if (xe_vm_in_fault_mode(vm)) {
690 err = xe_vm_invalidate_vma(vma);
694 trace_xe_vma_userptr_invalidate_complete(vma);
699 static const struct mmu_interval_notifier_ops vma_userptr_notifier_ops = {
700 .invalidate = vma_userptr_invalidate,
703 int xe_vm_userptr_pin(struct xe_vm *vm)
705 struct xe_vma *vma, *next;
707 LIST_HEAD(tmp_evict);
709 lockdep_assert_held_write(&vm->lock);
711 /* Collect invalidated userptrs */
712 spin_lock(&vm->userptr.invalidated_lock);
713 list_for_each_entry_safe(vma, next, &vm->userptr.invalidated,
714 userptr.invalidate_link) {
715 list_del_init(&vma->userptr.invalidate_link);
716 list_move_tail(&vma->combined_links.userptr,
717 &vm->userptr.repin_list);
719 spin_unlock(&vm->userptr.invalidated_lock);
721 /* Pin and move to temporary list */
722 list_for_each_entry_safe(vma, next, &vm->userptr.repin_list,
723 combined_links.userptr) {
724 err = xe_vma_userptr_pin_pages(vma);
728 list_move_tail(&vma->combined_links.userptr, &vm->rebind_list);
735 * xe_vm_userptr_check_repin() - Check whether the VM might have userptrs
736 * that need repinning.
739 * This function does an advisory check for whether the VM has userptrs that
742 * Return: 0 if there are no indications of userptrs needing repinning,
743 * -EAGAIN if there are.
745 int xe_vm_userptr_check_repin(struct xe_vm *vm)
747 return (list_empty_careful(&vm->userptr.repin_list) &&
748 list_empty_careful(&vm->userptr.invalidated)) ? 0 : -EAGAIN;
751 static struct dma_fence *
752 xe_vm_bind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
753 struct xe_sync_entry *syncs, u32 num_syncs,
754 bool first_op, bool last_op);
756 struct dma_fence *xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
758 struct dma_fence *fence = NULL;
759 struct xe_vma *vma, *next;
761 lockdep_assert_held(&vm->lock);
762 if (xe_vm_in_lr_mode(vm) && !rebind_worker)
765 xe_vm_assert_held(vm);
766 list_for_each_entry_safe(vma, next, &vm->rebind_list,
767 combined_links.rebind) {
768 xe_assert(vm->xe, vma->tile_present);
770 list_del_init(&vma->combined_links.rebind);
771 dma_fence_put(fence);
773 trace_xe_vma_rebind_worker(vma);
775 trace_xe_vma_rebind_exec(vma);
776 fence = xe_vm_bind_vma(vma, NULL, NULL, 0, false, false);
784 #define VMA_CREATE_FLAG_READ_ONLY BIT(0)
785 #define VMA_CREATE_FLAG_IS_NULL BIT(1)
787 static struct xe_vma *xe_vma_create(struct xe_vm *vm,
789 u64 bo_offset_or_userptr,
791 u16 pat_index, unsigned int flags)
794 struct xe_tile *tile;
796 bool read_only = (flags & VMA_CREATE_FLAG_READ_ONLY);
797 bool is_null = (flags & VMA_CREATE_FLAG_IS_NULL);
799 xe_assert(vm->xe, start < end);
800 xe_assert(vm->xe, end < vm->size);
802 if (!bo && !is_null) /* userptr */
803 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
805 vma = kzalloc(sizeof(*vma) - sizeof(struct xe_userptr),
808 vma = ERR_PTR(-ENOMEM);
812 INIT_LIST_HEAD(&vma->combined_links.rebind);
814 INIT_LIST_HEAD(&vma->gpuva.gem.entry);
815 vma->gpuva.vm = &vm->gpuvm;
816 vma->gpuva.va.addr = start;
817 vma->gpuva.va.range = end - start + 1;
819 vma->gpuva.flags |= XE_VMA_READ_ONLY;
821 vma->gpuva.flags |= DRM_GPUVA_SPARSE;
823 for_each_tile(tile, vm->xe, id)
824 vma->tile_mask |= 0x1 << id;
826 if (GRAPHICS_VER(vm->xe) >= 20 || vm->xe->info.platform == XE_PVC)
827 vma->gpuva.flags |= XE_VMA_ATOMIC_PTE_BIT;
829 vma->pat_index = pat_index;
832 struct drm_gpuvm_bo *vm_bo;
834 xe_bo_assert_held(bo);
836 vm_bo = drm_gpuvm_bo_obtain(vma->gpuva.vm, &bo->ttm.base);
839 return ERR_CAST(vm_bo);
842 drm_gpuvm_bo_extobj_add(vm_bo);
843 drm_gem_object_get(&bo->ttm.base);
844 vma->gpuva.gem.obj = &bo->ttm.base;
845 vma->gpuva.gem.offset = bo_offset_or_userptr;
846 drm_gpuva_link(&vma->gpuva, vm_bo);
847 drm_gpuvm_bo_put(vm_bo);
848 } else /* userptr or null */ {
850 u64 size = end - start + 1;
853 INIT_LIST_HEAD(&vma->userptr.invalidate_link);
854 vma->gpuva.gem.offset = bo_offset_or_userptr;
856 err = mmu_interval_notifier_insert(&vma->userptr.notifier,
858 xe_vma_userptr(vma), size,
859 &vma_userptr_notifier_ops);
866 vma->userptr.notifier_seq = LONG_MAX;
875 static void xe_vma_destroy_late(struct xe_vma *vma)
877 struct xe_vm *vm = xe_vma_vm(vma);
878 struct xe_device *xe = vm->xe;
879 bool read_only = xe_vma_read_only(vma);
881 if (xe_vma_is_userptr(vma)) {
882 if (vma->userptr.sg) {
883 dma_unmap_sgtable(xe->drm.dev,
885 read_only ? DMA_TO_DEVICE :
886 DMA_BIDIRECTIONAL, 0);
887 sg_free_table(vma->userptr.sg);
888 vma->userptr.sg = NULL;
892 * Since userptr pages are not pinned, we can't remove
893 * the notifer until we're sure the GPU is not accessing
896 mmu_interval_notifier_remove(&vma->userptr.notifier);
898 } else if (xe_vma_is_null(vma)) {
901 xe_bo_put(xe_vma_bo(vma));
907 static void vma_destroy_work_func(struct work_struct *w)
910 container_of(w, struct xe_vma, destroy_work);
912 xe_vma_destroy_late(vma);
915 static void vma_destroy_cb(struct dma_fence *fence,
916 struct dma_fence_cb *cb)
918 struct xe_vma *vma = container_of(cb, struct xe_vma, destroy_cb);
920 INIT_WORK(&vma->destroy_work, vma_destroy_work_func);
921 queue_work(system_unbound_wq, &vma->destroy_work);
924 static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence)
926 struct xe_vm *vm = xe_vma_vm(vma);
928 lockdep_assert_held_write(&vm->lock);
929 xe_assert(vm->xe, list_empty(&vma->combined_links.destroy));
931 if (xe_vma_is_userptr(vma)) {
932 xe_assert(vm->xe, vma->gpuva.flags & XE_VMA_DESTROYED);
934 spin_lock(&vm->userptr.invalidated_lock);
935 list_del(&vma->userptr.invalidate_link);
936 spin_unlock(&vm->userptr.invalidated_lock);
937 } else if (!xe_vma_is_null(vma)) {
938 xe_bo_assert_held(xe_vma_bo(vma));
940 drm_gpuva_unlink(&vma->gpuva);
943 xe_vm_assert_held(vm);
945 int ret = dma_fence_add_callback(fence, &vma->destroy_cb,
949 XE_WARN_ON(ret != -ENOENT);
950 xe_vma_destroy_late(vma);
953 xe_vma_destroy_late(vma);
958 * xe_vm_prepare_vma() - drm_exec utility to lock a vma
959 * @exec: The drm_exec object we're currently locking for.
960 * @vma: The vma for witch we want to lock the vm resv and any attached
962 * @num_shared: The number of dma-fence slots to pre-allocate in the
963 * objects' reservation objects.
965 * Return: 0 on success, negative error code on error. In particular
966 * may return -EDEADLK on WW transaction contention and -EINTR if
967 * an interruptible wait is terminated by a signal.
969 int xe_vm_prepare_vma(struct drm_exec *exec, struct xe_vma *vma,
970 unsigned int num_shared)
972 struct xe_vm *vm = xe_vma_vm(vma);
973 struct xe_bo *bo = xe_vma_bo(vma);
977 err = drm_exec_prepare_obj(exec, xe_vm_obj(vm), num_shared);
978 if (!err && bo && !bo->vm)
979 err = drm_exec_prepare_obj(exec, &bo->ttm.base, num_shared);
984 static void xe_vma_destroy_unlocked(struct xe_vma *vma)
986 struct drm_exec exec;
989 drm_exec_init(&exec, 0, 0);
990 drm_exec_until_all_locked(&exec) {
991 err = xe_vm_prepare_vma(&exec, vma, 0);
992 drm_exec_retry_on_contention(&exec);
997 xe_vma_destroy(vma, NULL);
999 drm_exec_fini(&exec);
1003 xe_vm_find_overlapping_vma(struct xe_vm *vm, u64 start, u64 range)
1005 struct drm_gpuva *gpuva;
1007 lockdep_assert_held(&vm->lock);
1009 if (xe_vm_is_closed_or_banned(vm))
1012 xe_assert(vm->xe, start + range <= vm->size);
1014 gpuva = drm_gpuva_find_first(&vm->gpuvm, start, range);
1016 return gpuva ? gpuva_to_vma(gpuva) : NULL;
1019 static int xe_vm_insert_vma(struct xe_vm *vm, struct xe_vma *vma)
1023 xe_assert(vm->xe, xe_vma_vm(vma) == vm);
1024 lockdep_assert_held(&vm->lock);
1026 err = drm_gpuva_insert(&vm->gpuvm, &vma->gpuva);
1027 XE_WARN_ON(err); /* Shouldn't be possible */
1032 static void xe_vm_remove_vma(struct xe_vm *vm, struct xe_vma *vma)
1034 xe_assert(vm->xe, xe_vma_vm(vma) == vm);
1035 lockdep_assert_held(&vm->lock);
1037 drm_gpuva_remove(&vma->gpuva);
1038 if (vm->usm.last_fault_vma == vma)
1039 vm->usm.last_fault_vma = NULL;
1042 static struct drm_gpuva_op *xe_vm_op_alloc(void)
1044 struct xe_vma_op *op;
1046 op = kzalloc(sizeof(*op), GFP_KERNEL);
1054 static void xe_vm_free(struct drm_gpuvm *gpuvm);
1056 static struct drm_gpuvm_ops gpuvm_ops = {
1057 .op_alloc = xe_vm_op_alloc,
1058 .vm_bo_validate = xe_gpuvm_validate,
1059 .vm_free = xe_vm_free,
1062 static u64 pde_encode_pat_index(struct xe_device *xe, u16 pat_index)
1066 if (pat_index & BIT(0))
1067 pte |= XE_PPGTT_PTE_PAT0;
1069 if (pat_index & BIT(1))
1070 pte |= XE_PPGTT_PTE_PAT1;
1075 static u64 pte_encode_pat_index(struct xe_device *xe, u16 pat_index,
1080 if (pat_index & BIT(0))
1081 pte |= XE_PPGTT_PTE_PAT0;
1083 if (pat_index & BIT(1))
1084 pte |= XE_PPGTT_PTE_PAT1;
1086 if (pat_index & BIT(2)) {
1088 pte |= XE_PPGTT_PDE_PDPE_PAT2;
1090 pte |= XE_PPGTT_PTE_PAT2;
1093 if (pat_index & BIT(3))
1094 pte |= XELPG_PPGTT_PTE_PAT3;
1096 if (pat_index & (BIT(4)))
1097 pte |= XE2_PPGTT_PTE_PAT4;
1102 static u64 pte_encode_ps(u32 pt_level)
1104 XE_WARN_ON(pt_level > MAX_HUGEPTE_LEVEL);
1107 return XE_PDE_PS_2M;
1108 else if (pt_level == 2)
1109 return XE_PDPE_PS_1G;
1114 static u64 xelp_pde_encode_bo(struct xe_bo *bo, u64 bo_offset,
1115 const u16 pat_index)
1117 struct xe_device *xe = xe_bo_device(bo);
1120 pde = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
1121 pde |= XE_PAGE_PRESENT | XE_PAGE_RW;
1122 pde |= pde_encode_pat_index(xe, pat_index);
1127 static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
1128 u16 pat_index, u32 pt_level)
1130 struct xe_device *xe = xe_bo_device(bo);
1133 pte = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
1134 pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
1135 pte |= pte_encode_pat_index(xe, pat_index, pt_level);
1136 pte |= pte_encode_ps(pt_level);
1138 if (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo))
1139 pte |= XE_PPGTT_PTE_DM;
1144 static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
1145 u16 pat_index, u32 pt_level)
1147 struct xe_device *xe = xe_vma_vm(vma)->xe;
1149 pte |= XE_PAGE_PRESENT;
1151 if (likely(!xe_vma_read_only(vma)))
1154 pte |= pte_encode_pat_index(xe, pat_index, pt_level);
1155 pte |= pte_encode_ps(pt_level);
1157 if (unlikely(xe_vma_is_null(vma)))
1163 static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
1165 u32 pt_level, bool devmem, u64 flags)
1169 /* Avoid passing random bits directly as flags */
1170 xe_assert(xe, !(flags & ~XE_PTE_PS64));
1173 pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
1174 pte |= pte_encode_pat_index(xe, pat_index, pt_level);
1175 pte |= pte_encode_ps(pt_level);
1178 pte |= XE_PPGTT_PTE_DM;
1185 static const struct xe_pt_ops xelp_pt_ops = {
1186 .pte_encode_bo = xelp_pte_encode_bo,
1187 .pte_encode_vma = xelp_pte_encode_vma,
1188 .pte_encode_addr = xelp_pte_encode_addr,
1189 .pde_encode_bo = xelp_pde_encode_bo,
1192 static void vm_destroy_work_func(struct work_struct *w);
1195 * xe_vm_create_scratch() - Setup a scratch memory pagetable tree for the
1196 * given tile and vm.
1198 * @tile: tile to set up for.
1199 * @vm: vm to set up for.
1201 * Sets up a pagetable tree with one page-table per level and a single
1202 * leaf PTE. All pagetable entries point to the single page-table or,
1203 * for MAX_HUGEPTE_LEVEL, a NULL huge PTE returning 0 on read and
1204 * writes become NOPs.
1206 * Return: 0 on success, negative error code on error.
1208 static int xe_vm_create_scratch(struct xe_device *xe, struct xe_tile *tile,
1214 for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; i++) {
1215 vm->scratch_pt[id][i] = xe_pt_create(vm, tile, i);
1216 if (IS_ERR(vm->scratch_pt[id][i]))
1217 return PTR_ERR(vm->scratch_pt[id][i]);
1219 xe_pt_populate_empty(tile, vm, vm->scratch_pt[id][i]);
1225 static void xe_vm_free_scratch(struct xe_vm *vm)
1227 struct xe_tile *tile;
1230 if (!xe_vm_has_scratch(vm))
1233 for_each_tile(tile, vm->xe, id) {
1236 if (!vm->pt_root[id])
1239 for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; ++i)
1240 if (vm->scratch_pt[id][i])
1241 xe_pt_destroy(vm->scratch_pt[id][i], vm->flags, NULL);
1245 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
1247 struct drm_gem_object *vm_resv_obj;
1249 int err, number_tiles = 0;
1250 struct xe_tile *tile;
1253 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1255 return ERR_PTR(-ENOMEM);
1259 vm->size = 1ull << xe->info.va_bits;
1263 init_rwsem(&vm->lock);
1265 INIT_LIST_HEAD(&vm->rebind_list);
1267 INIT_LIST_HEAD(&vm->userptr.repin_list);
1268 INIT_LIST_HEAD(&vm->userptr.invalidated);
1269 init_rwsem(&vm->userptr.notifier_lock);
1270 spin_lock_init(&vm->userptr.invalidated_lock);
1272 INIT_WORK(&vm->destroy_work, vm_destroy_work_func);
1274 INIT_LIST_HEAD(&vm->preempt.exec_queues);
1275 vm->preempt.min_run_period_ms = 10; /* FIXME: Wire up to uAPI */
1277 for_each_tile(tile, xe, id)
1278 xe_range_fence_tree_init(&vm->rftree[id]);
1280 vm->pt_ops = &xelp_pt_ops;
1282 if (!(flags & XE_VM_FLAG_MIGRATION))
1283 xe_device_mem_access_get(xe);
1285 vm_resv_obj = drm_gpuvm_resv_object_alloc(&xe->drm);
1291 drm_gpuvm_init(&vm->gpuvm, "Xe VM", DRM_GPUVM_RESV_PROTECTED, &xe->drm,
1292 vm_resv_obj, 0, vm->size, 0, 0, &gpuvm_ops);
1294 drm_gem_object_put(vm_resv_obj);
1296 err = dma_resv_lock_interruptible(xe_vm_resv(vm), NULL);
1300 if (IS_DGFX(xe) && xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)
1301 vm->flags |= XE_VM_FLAG_64K;
1303 for_each_tile(tile, xe, id) {
1304 if (flags & XE_VM_FLAG_MIGRATION &&
1305 tile->id != XE_VM_FLAG_TILE_ID(flags))
1308 vm->pt_root[id] = xe_pt_create(vm, tile, xe->info.vm_max_level);
1309 if (IS_ERR(vm->pt_root[id])) {
1310 err = PTR_ERR(vm->pt_root[id]);
1311 vm->pt_root[id] = NULL;
1312 goto err_unlock_close;
1316 if (xe_vm_has_scratch(vm)) {
1317 for_each_tile(tile, xe, id) {
1318 if (!vm->pt_root[id])
1321 err = xe_vm_create_scratch(xe, tile, vm);
1323 goto err_unlock_close;
1325 vm->batch_invalidate_tlb = true;
1328 if (flags & XE_VM_FLAG_LR_MODE) {
1329 INIT_WORK(&vm->preempt.rebind_work, preempt_rebind_work_func);
1330 vm->flags |= XE_VM_FLAG_LR_MODE;
1331 vm->batch_invalidate_tlb = false;
1334 /* Fill pt_root after allocating scratch tables */
1335 for_each_tile(tile, xe, id) {
1336 if (!vm->pt_root[id])
1339 xe_pt_populate_empty(tile, vm, vm->pt_root[id]);
1341 dma_resv_unlock(xe_vm_resv(vm));
1343 /* Kernel migration VM shouldn't have a circular loop.. */
1344 if (!(flags & XE_VM_FLAG_MIGRATION)) {
1345 for_each_tile(tile, xe, id) {
1346 struct xe_gt *gt = tile->primary_gt;
1347 struct xe_vm *migrate_vm;
1348 struct xe_exec_queue *q;
1349 u32 create_flags = EXEC_QUEUE_FLAG_VM;
1351 if (!vm->pt_root[id])
1354 migrate_vm = xe_migrate_get_vm(tile->migrate);
1355 q = xe_exec_queue_create_class(xe, gt, migrate_vm,
1356 XE_ENGINE_CLASS_COPY,
1358 xe_vm_put(migrate_vm);
1368 if (number_tiles > 1)
1369 vm->composite_fence_ctx = dma_fence_context_alloc(1);
1371 mutex_lock(&xe->usm.lock);
1372 if (flags & XE_VM_FLAG_FAULT_MODE)
1373 xe->usm.num_vm_in_fault_mode++;
1374 else if (!(flags & XE_VM_FLAG_MIGRATION))
1375 xe->usm.num_vm_in_non_fault_mode++;
1376 mutex_unlock(&xe->usm.lock);
1378 trace_xe_vm_create(vm);
1383 dma_resv_unlock(xe_vm_resv(vm));
1385 xe_vm_close_and_put(vm);
1386 return ERR_PTR(err);
1389 for_each_tile(tile, xe, id)
1390 xe_range_fence_tree_fini(&vm->rftree[id]);
1392 if (!(flags & XE_VM_FLAG_MIGRATION))
1393 xe_device_mem_access_put(xe);
1394 return ERR_PTR(err);
1397 static void xe_vm_close(struct xe_vm *vm)
1399 down_write(&vm->lock);
1401 up_write(&vm->lock);
1404 void xe_vm_close_and_put(struct xe_vm *vm)
1406 LIST_HEAD(contested);
1407 struct xe_device *xe = vm->xe;
1408 struct xe_tile *tile;
1409 struct xe_vma *vma, *next_vma;
1410 struct drm_gpuva *gpuva, *next;
1413 xe_assert(xe, !vm->preempt.num_exec_queues);
1416 if (xe_vm_in_preempt_fence_mode(vm))
1417 flush_work(&vm->preempt.rebind_work);
1419 down_write(&vm->lock);
1420 for_each_tile(tile, xe, id) {
1422 xe_exec_queue_last_fence_put(vm->q[id], vm);
1424 up_write(&vm->lock);
1426 for_each_tile(tile, xe, id) {
1428 xe_exec_queue_kill(vm->q[id]);
1429 xe_exec_queue_put(vm->q[id]);
1434 down_write(&vm->lock);
1435 xe_vm_lock(vm, false);
1436 drm_gpuvm_for_each_va_safe(gpuva, next, &vm->gpuvm) {
1437 vma = gpuva_to_vma(gpuva);
1439 if (xe_vma_has_no_bo(vma)) {
1440 down_read(&vm->userptr.notifier_lock);
1441 vma->gpuva.flags |= XE_VMA_DESTROYED;
1442 up_read(&vm->userptr.notifier_lock);
1445 xe_vm_remove_vma(vm, vma);
1447 /* easy case, remove from VMA? */
1448 if (xe_vma_has_no_bo(vma) || xe_vma_bo(vma)->vm) {
1449 list_del_init(&vma->combined_links.rebind);
1450 xe_vma_destroy(vma, NULL);
1454 list_move_tail(&vma->combined_links.destroy, &contested);
1455 vma->gpuva.flags |= XE_VMA_DESTROYED;
1459 * All vm operations will add shared fences to resv.
1460 * The only exception is eviction for a shared object,
1461 * but even so, the unbind when evicted would still
1462 * install a fence to resv. Hence it's safe to
1463 * destroy the pagetables immediately.
1465 xe_vm_free_scratch(vm);
1467 for_each_tile(tile, xe, id) {
1468 if (vm->pt_root[id]) {
1469 xe_pt_destroy(vm->pt_root[id], vm->flags, NULL);
1470 vm->pt_root[id] = NULL;
1476 * VM is now dead, cannot re-add nodes to vm->vmas if it's NULL
1477 * Since we hold a refcount to the bo, we can remove and free
1478 * the members safely without locking.
1480 list_for_each_entry_safe(vma, next_vma, &contested,
1481 combined_links.destroy) {
1482 list_del_init(&vma->combined_links.destroy);
1483 xe_vma_destroy_unlocked(vma);
1486 up_write(&vm->lock);
1488 mutex_lock(&xe->usm.lock);
1489 if (vm->flags & XE_VM_FLAG_FAULT_MODE)
1490 xe->usm.num_vm_in_fault_mode--;
1491 else if (!(vm->flags & XE_VM_FLAG_MIGRATION))
1492 xe->usm.num_vm_in_non_fault_mode--;
1493 mutex_unlock(&xe->usm.lock);
1495 for_each_tile(tile, xe, id)
1496 xe_range_fence_tree_fini(&vm->rftree[id]);
1501 static void vm_destroy_work_func(struct work_struct *w)
1504 container_of(w, struct xe_vm, destroy_work);
1505 struct xe_device *xe = vm->xe;
1506 struct xe_tile *tile;
1510 /* xe_vm_close_and_put was not called? */
1511 xe_assert(xe, !vm->size);
1513 if (!(vm->flags & XE_VM_FLAG_MIGRATION)) {
1514 xe_device_mem_access_put(xe);
1516 if (xe->info.has_asid && vm->usm.asid) {
1517 mutex_lock(&xe->usm.lock);
1518 lookup = xa_erase(&xe->usm.asid_to_vm, vm->usm.asid);
1519 xe_assert(xe, lookup == vm);
1520 mutex_unlock(&xe->usm.lock);
1524 for_each_tile(tile, xe, id)
1525 XE_WARN_ON(vm->pt_root[id]);
1527 trace_xe_vm_free(vm);
1528 dma_fence_put(vm->rebind_fence);
1532 static void xe_vm_free(struct drm_gpuvm *gpuvm)
1534 struct xe_vm *vm = container_of(gpuvm, struct xe_vm, gpuvm);
1536 /* To destroy the VM we need to be able to sleep */
1537 queue_work(system_unbound_wq, &vm->destroy_work);
1540 struct xe_vm *xe_vm_lookup(struct xe_file *xef, u32 id)
1544 mutex_lock(&xef->vm.lock);
1545 vm = xa_load(&xef->vm.xa, id);
1548 mutex_unlock(&xef->vm.lock);
1553 u64 xe_vm_pdp4_descriptor(struct xe_vm *vm, struct xe_tile *tile)
1555 return vm->pt_ops->pde_encode_bo(vm->pt_root[tile->id]->bo, 0,
1556 tile_to_xe(tile)->pat.idx[XE_CACHE_WB]);
1559 static struct xe_exec_queue *
1560 to_wait_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
1562 return q ? q : vm->q[0];
1565 static struct dma_fence *
1566 xe_vm_unbind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
1567 struct xe_sync_entry *syncs, u32 num_syncs,
1568 bool first_op, bool last_op)
1570 struct xe_vm *vm = xe_vma_vm(vma);
1571 struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
1572 struct xe_tile *tile;
1573 struct dma_fence *fence = NULL;
1574 struct dma_fence **fences = NULL;
1575 struct dma_fence_array *cf = NULL;
1576 int cur_fence = 0, i;
1577 int number_tiles = hweight8(vma->tile_present);
1581 trace_xe_vma_unbind(vma);
1583 if (number_tiles > 1) {
1584 fences = kmalloc_array(number_tiles, sizeof(*fences),
1587 return ERR_PTR(-ENOMEM);
1590 for_each_tile(tile, vm->xe, id) {
1591 if (!(vma->tile_present & BIT(id)))
1594 fence = __xe_pt_unbind_vma(tile, vma, q ? q : vm->q[id],
1595 first_op ? syncs : NULL,
1596 first_op ? num_syncs : 0);
1597 if (IS_ERR(fence)) {
1598 err = PTR_ERR(fence);
1603 fences[cur_fence++] = fence;
1606 if (q && vm->pt_root[id] && !list_empty(&q->multi_gt_list))
1607 q = list_next_entry(q, multi_gt_list);
1611 cf = dma_fence_array_create(number_tiles, fences,
1612 vm->composite_fence_ctx,
1613 vm->composite_fence_seqno++,
1616 --vm->composite_fence_seqno;
1622 fence = cf ? &cf->base : !fence ?
1623 xe_exec_queue_last_fence_get(wait_exec_queue, vm) : fence;
1625 for (i = 0; i < num_syncs; i++)
1626 xe_sync_entry_signal(&syncs[i], NULL, fence);
1634 dma_fence_put(fences[--cur_fence]);
1638 return ERR_PTR(err);
1641 static struct dma_fence *
1642 xe_vm_bind_vma(struct xe_vma *vma, struct xe_exec_queue *q,
1643 struct xe_sync_entry *syncs, u32 num_syncs,
1644 bool first_op, bool last_op)
1646 struct xe_tile *tile;
1647 struct dma_fence *fence;
1648 struct dma_fence **fences = NULL;
1649 struct dma_fence_array *cf = NULL;
1650 struct xe_vm *vm = xe_vma_vm(vma);
1651 int cur_fence = 0, i;
1652 int number_tiles = hweight8(vma->tile_mask);
1656 trace_xe_vma_bind(vma);
1658 if (number_tiles > 1) {
1659 fences = kmalloc_array(number_tiles, sizeof(*fences),
1662 return ERR_PTR(-ENOMEM);
1665 for_each_tile(tile, vm->xe, id) {
1666 if (!(vma->tile_mask & BIT(id)))
1669 fence = __xe_pt_bind_vma(tile, vma, q ? q : vm->q[id],
1670 first_op ? syncs : NULL,
1671 first_op ? num_syncs : 0,
1672 vma->tile_present & BIT(id));
1673 if (IS_ERR(fence)) {
1674 err = PTR_ERR(fence);
1679 fences[cur_fence++] = fence;
1682 if (q && vm->pt_root[id] && !list_empty(&q->multi_gt_list))
1683 q = list_next_entry(q, multi_gt_list);
1687 cf = dma_fence_array_create(number_tiles, fences,
1688 vm->composite_fence_ctx,
1689 vm->composite_fence_seqno++,
1692 --vm->composite_fence_seqno;
1699 for (i = 0; i < num_syncs; i++)
1700 xe_sync_entry_signal(&syncs[i], NULL,
1701 cf ? &cf->base : fence);
1704 return cf ? &cf->base : fence;
1709 dma_fence_put(fences[--cur_fence]);
1713 return ERR_PTR(err);
1716 static int __xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma,
1717 struct xe_exec_queue *q, struct xe_sync_entry *syncs,
1718 u32 num_syncs, bool immediate, bool first_op,
1721 struct dma_fence *fence;
1722 struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
1724 xe_vm_assert_held(vm);
1727 fence = xe_vm_bind_vma(vma, q, syncs, num_syncs, first_op,
1730 return PTR_ERR(fence);
1734 xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
1736 fence = xe_exec_queue_last_fence_get(wait_exec_queue, vm);
1738 for (i = 0; i < num_syncs; i++)
1739 xe_sync_entry_signal(&syncs[i], NULL, fence);
1744 xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
1745 dma_fence_put(fence);
1750 static int xe_vm_bind(struct xe_vm *vm, struct xe_vma *vma, struct xe_exec_queue *q,
1751 struct xe_bo *bo, struct xe_sync_entry *syncs,
1752 u32 num_syncs, bool immediate, bool first_op,
1757 xe_vm_assert_held(vm);
1758 xe_bo_assert_held(bo);
1760 if (bo && immediate) {
1761 err = xe_bo_validate(bo, vm, true);
1766 return __xe_vm_bind(vm, vma, q, syncs, num_syncs, immediate, first_op,
1770 static int xe_vm_unbind(struct xe_vm *vm, struct xe_vma *vma,
1771 struct xe_exec_queue *q, struct xe_sync_entry *syncs,
1772 u32 num_syncs, bool first_op, bool last_op)
1774 struct dma_fence *fence;
1775 struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
1777 xe_vm_assert_held(vm);
1778 xe_bo_assert_held(xe_vma_bo(vma));
1780 fence = xe_vm_unbind_vma(vma, q, syncs, num_syncs, first_op, last_op);
1782 return PTR_ERR(fence);
1784 xe_vma_destroy(vma, fence);
1786 xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
1787 dma_fence_put(fence);
1792 #define ALL_DRM_XE_VM_CREATE_FLAGS (DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \
1793 DRM_XE_VM_CREATE_FLAG_LR_MODE | \
1794 DRM_XE_VM_CREATE_FLAG_FAULT_MODE)
1796 int xe_vm_create_ioctl(struct drm_device *dev, void *data,
1797 struct drm_file *file)
1799 struct xe_device *xe = to_xe_device(dev);
1800 struct xe_file *xef = to_xe_file(file);
1801 struct drm_xe_vm_create *args = data;
1802 struct xe_tile *tile;
1808 if (XE_IOCTL_DBG(xe, args->extensions))
1811 if (XE_WA(xe_root_mmio_gt(xe), 14016763929))
1812 args->flags |= DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE;
1814 if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE &&
1818 if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1821 if (XE_IOCTL_DBG(xe, args->flags & ~ALL_DRM_XE_VM_CREATE_FLAGS))
1824 if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE &&
1825 args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE))
1828 if (XE_IOCTL_DBG(xe, !(args->flags & DRM_XE_VM_CREATE_FLAG_LR_MODE) &&
1829 args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE))
1832 if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE &&
1833 xe_device_in_non_fault_mode(xe)))
1836 if (XE_IOCTL_DBG(xe, !(args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE) &&
1837 xe_device_in_fault_mode(xe)))
1840 if (XE_IOCTL_DBG(xe, args->extensions))
1843 if (args->flags & DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE)
1844 flags |= XE_VM_FLAG_SCRATCH_PAGE;
1845 if (args->flags & DRM_XE_VM_CREATE_FLAG_LR_MODE)
1846 flags |= XE_VM_FLAG_LR_MODE;
1847 if (args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE)
1848 flags |= XE_VM_FLAG_FAULT_MODE;
1850 vm = xe_vm_create(xe, flags);
1854 mutex_lock(&xef->vm.lock);
1855 err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
1856 mutex_unlock(&xef->vm.lock);
1858 xe_vm_close_and_put(vm);
1862 if (xe->info.has_asid) {
1863 mutex_lock(&xe->usm.lock);
1864 err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, vm,
1865 XA_LIMIT(1, XE_MAX_ASID - 1),
1866 &xe->usm.next_asid, GFP_KERNEL);
1867 mutex_unlock(&xe->usm.lock);
1869 xe_vm_close_and_put(vm);
1873 vm->usm.asid = asid;
1879 /* Record BO memory for VM pagetable created against client */
1880 for_each_tile(tile, xe, id)
1881 if (vm->pt_root[id])
1882 xe_drm_client_add_bo(vm->xef->client, vm->pt_root[id]->bo);
1884 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_MEM)
1885 /* Warning: Security issue - never enable by default */
1886 args->reserved[0] = xe_bo_main_addr(vm->pt_root[0]->bo, XE_PAGE_SIZE);
1892 int xe_vm_destroy_ioctl(struct drm_device *dev, void *data,
1893 struct drm_file *file)
1895 struct xe_device *xe = to_xe_device(dev);
1896 struct xe_file *xef = to_xe_file(file);
1897 struct drm_xe_vm_destroy *args = data;
1901 if (XE_IOCTL_DBG(xe, args->pad) ||
1902 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
1905 mutex_lock(&xef->vm.lock);
1906 vm = xa_load(&xef->vm.xa, args->vm_id);
1907 if (XE_IOCTL_DBG(xe, !vm))
1909 else if (XE_IOCTL_DBG(xe, vm->preempt.num_exec_queues))
1912 xa_erase(&xef->vm.xa, args->vm_id);
1913 mutex_unlock(&xef->vm.lock);
1916 xe_vm_close_and_put(vm);
1921 static const u32 region_to_mem_type[] = {
1927 static int xe_vm_prefetch(struct xe_vm *vm, struct xe_vma *vma,
1928 struct xe_exec_queue *q, u32 region,
1929 struct xe_sync_entry *syncs, u32 num_syncs,
1930 bool first_op, bool last_op)
1932 struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, q);
1935 xe_assert(vm->xe, region <= ARRAY_SIZE(region_to_mem_type));
1937 if (!xe_vma_has_no_bo(vma)) {
1938 err = xe_bo_migrate(xe_vma_bo(vma), region_to_mem_type[region]);
1943 if (vma->tile_mask != (vma->tile_present & ~vma->usm.tile_invalidated)) {
1944 return xe_vm_bind(vm, vma, q, xe_vma_bo(vma), syncs, num_syncs,
1945 true, first_op, last_op);
1949 /* Nothing to do, signal fences now */
1951 for (i = 0; i < num_syncs; i++) {
1952 struct dma_fence *fence =
1953 xe_exec_queue_last_fence_get(wait_exec_queue, vm);
1955 xe_sync_entry_signal(&syncs[i], NULL, fence);
1963 static void prep_vma_destroy(struct xe_vm *vm, struct xe_vma *vma,
1966 down_read(&vm->userptr.notifier_lock);
1967 vma->gpuva.flags |= XE_VMA_DESTROYED;
1968 up_read(&vm->userptr.notifier_lock);
1970 xe_vm_remove_vma(vm, vma);
1974 #define ULL unsigned long long
1976 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)
1977 static void print_op(struct xe_device *xe, struct drm_gpuva_op *op)
1982 case DRM_GPUVA_OP_MAP:
1983 vm_dbg(&xe->drm, "MAP: addr=0x%016llx, range=0x%016llx",
1984 (ULL)op->map.va.addr, (ULL)op->map.va.range);
1986 case DRM_GPUVA_OP_REMAP:
1987 vma = gpuva_to_vma(op->remap.unmap->va);
1988 vm_dbg(&xe->drm, "REMAP:UNMAP: addr=0x%016llx, range=0x%016llx, keep=%d",
1989 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma),
1990 op->remap.unmap->keep ? 1 : 0);
1993 "REMAP:PREV: addr=0x%016llx, range=0x%016llx",
1994 (ULL)op->remap.prev->va.addr,
1995 (ULL)op->remap.prev->va.range);
1998 "REMAP:NEXT: addr=0x%016llx, range=0x%016llx",
1999 (ULL)op->remap.next->va.addr,
2000 (ULL)op->remap.next->va.range);
2002 case DRM_GPUVA_OP_UNMAP:
2003 vma = gpuva_to_vma(op->unmap.va);
2004 vm_dbg(&xe->drm, "UNMAP: addr=0x%016llx, range=0x%016llx, keep=%d",
2005 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma),
2006 op->unmap.keep ? 1 : 0);
2008 case DRM_GPUVA_OP_PREFETCH:
2009 vma = gpuva_to_vma(op->prefetch.va);
2010 vm_dbg(&xe->drm, "PREFETCH: addr=0x%016llx, range=0x%016llx",
2011 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma));
2014 drm_warn(&xe->drm, "NOT POSSIBLE");
2018 static void print_op(struct xe_device *xe, struct drm_gpuva_op *op)
2024 * Create operations list from IOCTL arguments, setup operations fields so parse
2025 * and commit steps are decoupled from IOCTL arguments. This step can fail.
2027 static struct drm_gpuva_ops *
2028 vm_bind_ioctl_ops_create(struct xe_vm *vm, struct xe_bo *bo,
2029 u64 bo_offset_or_userptr, u64 addr, u64 range,
2030 u32 operation, u32 flags,
2031 u32 prefetch_region, u16 pat_index)
2033 struct drm_gem_object *obj = bo ? &bo->ttm.base : NULL;
2034 struct drm_gpuva_ops *ops;
2035 struct drm_gpuva_op *__op;
2036 struct xe_vma_op *op;
2037 struct drm_gpuvm_bo *vm_bo;
2040 lockdep_assert_held_write(&vm->lock);
2042 vm_dbg(&vm->xe->drm,
2043 "op=%d, addr=0x%016llx, range=0x%016llx, bo_offset_or_userptr=0x%016llx",
2044 operation, (ULL)addr, (ULL)range,
2045 (ULL)bo_offset_or_userptr);
2047 switch (operation) {
2048 case DRM_XE_VM_BIND_OP_MAP:
2049 case DRM_XE_VM_BIND_OP_MAP_USERPTR:
2050 ops = drm_gpuvm_sm_map_ops_create(&vm->gpuvm, addr, range,
2051 obj, bo_offset_or_userptr);
2053 case DRM_XE_VM_BIND_OP_UNMAP:
2054 ops = drm_gpuvm_sm_unmap_ops_create(&vm->gpuvm, addr, range);
2056 case DRM_XE_VM_BIND_OP_PREFETCH:
2057 ops = drm_gpuvm_prefetch_ops_create(&vm->gpuvm, addr, range);
2059 case DRM_XE_VM_BIND_OP_UNMAP_ALL:
2060 xe_assert(vm->xe, bo);
2062 err = xe_bo_lock(bo, true);
2064 return ERR_PTR(err);
2066 vm_bo = drm_gpuvm_bo_obtain(&vm->gpuvm, obj);
2067 if (IS_ERR(vm_bo)) {
2069 return ERR_CAST(vm_bo);
2072 ops = drm_gpuvm_bo_unmap_ops_create(vm_bo);
2073 drm_gpuvm_bo_put(vm_bo);
2077 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2078 ops = ERR_PTR(-EINVAL);
2083 #ifdef TEST_VM_ASYNC_OPS_ERROR
2084 if (operation & FORCE_ASYNC_OP_ERROR) {
2085 op = list_first_entry_or_null(&ops->list, struct xe_vma_op,
2088 op->inject_error = true;
2092 drm_gpuva_for_each_op(__op, ops) {
2093 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2095 if (__op->op == DRM_GPUVA_OP_MAP) {
2097 flags & DRM_XE_VM_BIND_FLAG_IMMEDIATE;
2099 flags & DRM_XE_VM_BIND_FLAG_READONLY;
2100 op->map.is_null = flags & DRM_XE_VM_BIND_FLAG_NULL;
2101 op->map.pat_index = pat_index;
2102 } else if (__op->op == DRM_GPUVA_OP_PREFETCH) {
2103 op->prefetch.region = prefetch_region;
2106 print_op(vm->xe, __op);
2112 static struct xe_vma *new_vma(struct xe_vm *vm, struct drm_gpuva_op_map *op,
2113 u16 pat_index, unsigned int flags)
2115 struct xe_bo *bo = op->gem.obj ? gem_to_xe_bo(op->gem.obj) : NULL;
2116 struct drm_exec exec;
2120 lockdep_assert_held_write(&vm->lock);
2123 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
2124 drm_exec_until_all_locked(&exec) {
2127 err = drm_exec_lock_obj(&exec, xe_vm_obj(vm));
2128 drm_exec_retry_on_contention(&exec);
2131 err = drm_exec_lock_obj(&exec, &bo->ttm.base);
2132 drm_exec_retry_on_contention(&exec);
2135 drm_exec_fini(&exec);
2136 return ERR_PTR(err);
2140 vma = xe_vma_create(vm, bo, op->gem.offset,
2141 op->va.addr, op->va.addr +
2142 op->va.range - 1, pat_index, flags);
2144 drm_exec_fini(&exec);
2146 if (xe_vma_is_userptr(vma)) {
2147 err = xe_vma_userptr_pin_pages(vma);
2149 prep_vma_destroy(vm, vma, false);
2150 xe_vma_destroy_unlocked(vma);
2151 return ERR_PTR(err);
2153 } else if (!xe_vma_has_no_bo(vma) && !bo->vm) {
2154 err = add_preempt_fences(vm, bo);
2156 prep_vma_destroy(vm, vma, false);
2157 xe_vma_destroy_unlocked(vma);
2158 return ERR_PTR(err);
2165 static u64 xe_vma_max_pte_size(struct xe_vma *vma)
2167 if (vma->gpuva.flags & XE_VMA_PTE_1G)
2169 else if (vma->gpuva.flags & XE_VMA_PTE_2M)
2175 static u64 xe_vma_set_pte_size(struct xe_vma *vma, u64 size)
2179 vma->gpuva.flags |= XE_VMA_PTE_1G;
2182 vma->gpuva.flags |= XE_VMA_PTE_2M;
2189 static int xe_vma_op_commit(struct xe_vm *vm, struct xe_vma_op *op)
2193 lockdep_assert_held_write(&vm->lock);
2195 switch (op->base.op) {
2196 case DRM_GPUVA_OP_MAP:
2197 err |= xe_vm_insert_vma(vm, op->map.vma);
2199 op->flags |= XE_VMA_OP_COMMITTED;
2201 case DRM_GPUVA_OP_REMAP:
2204 gpuva_to_vma(op->base.remap.unmap->va)->tile_present;
2206 prep_vma_destroy(vm, gpuva_to_vma(op->base.remap.unmap->va),
2208 op->flags |= XE_VMA_OP_COMMITTED;
2210 if (op->remap.prev) {
2211 err |= xe_vm_insert_vma(vm, op->remap.prev);
2213 op->flags |= XE_VMA_OP_PREV_COMMITTED;
2214 if (!err && op->remap.skip_prev) {
2215 op->remap.prev->tile_present =
2217 op->remap.prev = NULL;
2220 if (op->remap.next) {
2221 err |= xe_vm_insert_vma(vm, op->remap.next);
2223 op->flags |= XE_VMA_OP_NEXT_COMMITTED;
2224 if (!err && op->remap.skip_next) {
2225 op->remap.next->tile_present =
2227 op->remap.next = NULL;
2231 /* Adjust for partial unbind after removin VMA from VM */
2233 op->base.remap.unmap->va->va.addr = op->remap.start;
2234 op->base.remap.unmap->va->va.range = op->remap.range;
2238 case DRM_GPUVA_OP_UNMAP:
2239 prep_vma_destroy(vm, gpuva_to_vma(op->base.unmap.va), true);
2240 op->flags |= XE_VMA_OP_COMMITTED;
2242 case DRM_GPUVA_OP_PREFETCH:
2243 op->flags |= XE_VMA_OP_COMMITTED;
2246 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2253 static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct xe_exec_queue *q,
2254 struct drm_gpuva_ops *ops,
2255 struct xe_sync_entry *syncs, u32 num_syncs,
2256 struct list_head *ops_list, bool last)
2258 struct xe_vma_op *last_op = NULL;
2259 struct drm_gpuva_op *__op;
2262 lockdep_assert_held_write(&vm->lock);
2264 drm_gpuva_for_each_op(__op, ops) {
2265 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2267 bool first = list_empty(ops_list);
2268 unsigned int flags = 0;
2270 INIT_LIST_HEAD(&op->link);
2271 list_add_tail(&op->link, ops_list);
2274 op->flags |= XE_VMA_OP_FIRST;
2275 op->num_syncs = num_syncs;
2281 switch (op->base.op) {
2282 case DRM_GPUVA_OP_MAP:
2284 flags |= op->map.read_only ?
2285 VMA_CREATE_FLAG_READ_ONLY : 0;
2286 flags |= op->map.is_null ?
2287 VMA_CREATE_FLAG_IS_NULL : 0;
2289 vma = new_vma(vm, &op->base.map, op->map.pat_index,
2292 return PTR_ERR(vma);
2297 case DRM_GPUVA_OP_REMAP:
2299 struct xe_vma *old =
2300 gpuva_to_vma(op->base.remap.unmap->va);
2302 op->remap.start = xe_vma_start(old);
2303 op->remap.range = xe_vma_size(old);
2305 if (op->base.remap.prev) {
2306 flags |= op->base.remap.unmap->va->flags &
2308 VMA_CREATE_FLAG_READ_ONLY : 0;
2309 flags |= op->base.remap.unmap->va->flags &
2311 VMA_CREATE_FLAG_IS_NULL : 0;
2313 vma = new_vma(vm, op->base.remap.prev,
2314 old->pat_index, flags);
2316 return PTR_ERR(vma);
2318 op->remap.prev = vma;
2321 * Userptr creates a new SG mapping so
2322 * we must also rebind.
2324 op->remap.skip_prev = !xe_vma_is_userptr(old) &&
2325 IS_ALIGNED(xe_vma_end(vma),
2326 xe_vma_max_pte_size(old));
2327 if (op->remap.skip_prev) {
2328 xe_vma_set_pte_size(vma, xe_vma_max_pte_size(old));
2332 op->remap.start = xe_vma_end(vma);
2336 if (op->base.remap.next) {
2337 flags |= op->base.remap.unmap->va->flags &
2339 VMA_CREATE_FLAG_READ_ONLY : 0;
2340 flags |= op->base.remap.unmap->va->flags &
2342 VMA_CREATE_FLAG_IS_NULL : 0;
2344 vma = new_vma(vm, op->base.remap.next,
2345 old->pat_index, flags);
2347 return PTR_ERR(vma);
2349 op->remap.next = vma;
2352 * Userptr creates a new SG mapping so
2353 * we must also rebind.
2355 op->remap.skip_next = !xe_vma_is_userptr(old) &&
2356 IS_ALIGNED(xe_vma_start(vma),
2357 xe_vma_max_pte_size(old));
2358 if (op->remap.skip_next) {
2359 xe_vma_set_pte_size(vma, xe_vma_max_pte_size(old));
2367 case DRM_GPUVA_OP_UNMAP:
2368 case DRM_GPUVA_OP_PREFETCH:
2372 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2377 err = xe_vma_op_commit(vm, op);
2382 /* FIXME: Unhandled corner case */
2383 XE_WARN_ON(!last_op && last && !list_empty(ops_list));
2390 last_op->flags |= XE_VMA_OP_LAST;
2391 last_op->num_syncs = num_syncs;
2392 last_op->syncs = syncs;
2398 static int op_execute(struct drm_exec *exec, struct xe_vm *vm,
2399 struct xe_vma *vma, struct xe_vma_op *op)
2403 lockdep_assert_held_write(&vm->lock);
2405 err = xe_vm_prepare_vma(exec, vma, 1);
2409 xe_vm_assert_held(vm);
2410 xe_bo_assert_held(xe_vma_bo(vma));
2412 switch (op->base.op) {
2413 case DRM_GPUVA_OP_MAP:
2414 err = xe_vm_bind(vm, vma, op->q, xe_vma_bo(vma),
2415 op->syncs, op->num_syncs,
2416 op->map.immediate || !xe_vm_in_fault_mode(vm),
2417 op->flags & XE_VMA_OP_FIRST,
2418 op->flags & XE_VMA_OP_LAST);
2420 case DRM_GPUVA_OP_REMAP:
2422 bool prev = !!op->remap.prev;
2423 bool next = !!op->remap.next;
2425 if (!op->remap.unmap_done) {
2427 vma->gpuva.flags |= XE_VMA_FIRST_REBIND;
2428 err = xe_vm_unbind(vm, vma, op->q, op->syncs,
2430 op->flags & XE_VMA_OP_FIRST,
2431 op->flags & XE_VMA_OP_LAST &&
2435 op->remap.unmap_done = true;
2439 op->remap.prev->gpuva.flags |= XE_VMA_LAST_REBIND;
2440 err = xe_vm_bind(vm, op->remap.prev, op->q,
2441 xe_vma_bo(op->remap.prev), op->syncs,
2442 op->num_syncs, true, false,
2443 op->flags & XE_VMA_OP_LAST && !next);
2444 op->remap.prev->gpuva.flags &= ~XE_VMA_LAST_REBIND;
2447 op->remap.prev = NULL;
2451 op->remap.next->gpuva.flags |= XE_VMA_LAST_REBIND;
2452 err = xe_vm_bind(vm, op->remap.next, op->q,
2453 xe_vma_bo(op->remap.next),
2454 op->syncs, op->num_syncs,
2456 op->flags & XE_VMA_OP_LAST);
2457 op->remap.next->gpuva.flags &= ~XE_VMA_LAST_REBIND;
2460 op->remap.next = NULL;
2465 case DRM_GPUVA_OP_UNMAP:
2466 err = xe_vm_unbind(vm, vma, op->q, op->syncs,
2467 op->num_syncs, op->flags & XE_VMA_OP_FIRST,
2468 op->flags & XE_VMA_OP_LAST);
2470 case DRM_GPUVA_OP_PREFETCH:
2471 err = xe_vm_prefetch(vm, vma, op->q, op->prefetch.region,
2472 op->syncs, op->num_syncs,
2473 op->flags & XE_VMA_OP_FIRST,
2474 op->flags & XE_VMA_OP_LAST);
2477 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2481 trace_xe_vma_fail(vma);
2486 static int __xe_vma_op_execute(struct xe_vm *vm, struct xe_vma *vma,
2487 struct xe_vma_op *op)
2489 struct drm_exec exec;
2493 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
2494 drm_exec_until_all_locked(&exec) {
2495 err = op_execute(&exec, vm, vma, op);
2496 drm_exec_retry_on_contention(&exec);
2500 drm_exec_fini(&exec);
2502 if (err == -EAGAIN && xe_vma_is_userptr(vma)) {
2503 lockdep_assert_held_write(&vm->lock);
2504 err = xe_vma_userptr_pin_pages(vma);
2508 trace_xe_vma_fail(vma);
2514 static int xe_vma_op_execute(struct xe_vm *vm, struct xe_vma_op *op)
2518 lockdep_assert_held_write(&vm->lock);
2520 #ifdef TEST_VM_ASYNC_OPS_ERROR
2521 if (op->inject_error) {
2522 op->inject_error = false;
2527 switch (op->base.op) {
2528 case DRM_GPUVA_OP_MAP:
2529 ret = __xe_vma_op_execute(vm, op->map.vma, op);
2531 case DRM_GPUVA_OP_REMAP:
2535 if (!op->remap.unmap_done)
2536 vma = gpuva_to_vma(op->base.remap.unmap->va);
2537 else if (op->remap.prev)
2538 vma = op->remap.prev;
2540 vma = op->remap.next;
2542 ret = __xe_vma_op_execute(vm, vma, op);
2545 case DRM_GPUVA_OP_UNMAP:
2546 ret = __xe_vma_op_execute(vm, gpuva_to_vma(op->base.unmap.va),
2549 case DRM_GPUVA_OP_PREFETCH:
2550 ret = __xe_vma_op_execute(vm,
2551 gpuva_to_vma(op->base.prefetch.va),
2555 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2561 static void xe_vma_op_cleanup(struct xe_vm *vm, struct xe_vma_op *op)
2563 bool last = op->flags & XE_VMA_OP_LAST;
2566 while (op->num_syncs--)
2567 xe_sync_entry_cleanup(&op->syncs[op->num_syncs]);
2570 xe_exec_queue_put(op->q);
2572 if (!list_empty(&op->link))
2573 list_del(&op->link);
2575 drm_gpuva_ops_free(&vm->gpuvm, op->ops);
2580 static void xe_vma_op_unwind(struct xe_vm *vm, struct xe_vma_op *op,
2581 bool post_commit, bool prev_post_commit,
2582 bool next_post_commit)
2584 lockdep_assert_held_write(&vm->lock);
2586 switch (op->base.op) {
2587 case DRM_GPUVA_OP_MAP:
2589 prep_vma_destroy(vm, op->map.vma, post_commit);
2590 xe_vma_destroy_unlocked(op->map.vma);
2593 case DRM_GPUVA_OP_UNMAP:
2595 struct xe_vma *vma = gpuva_to_vma(op->base.unmap.va);
2598 down_read(&vm->userptr.notifier_lock);
2599 vma->gpuva.flags &= ~XE_VMA_DESTROYED;
2600 up_read(&vm->userptr.notifier_lock);
2602 xe_vm_insert_vma(vm, vma);
2606 case DRM_GPUVA_OP_REMAP:
2608 struct xe_vma *vma = gpuva_to_vma(op->base.remap.unmap->va);
2610 if (op->remap.prev) {
2611 prep_vma_destroy(vm, op->remap.prev, prev_post_commit);
2612 xe_vma_destroy_unlocked(op->remap.prev);
2614 if (op->remap.next) {
2615 prep_vma_destroy(vm, op->remap.next, next_post_commit);
2616 xe_vma_destroy_unlocked(op->remap.next);
2619 down_read(&vm->userptr.notifier_lock);
2620 vma->gpuva.flags &= ~XE_VMA_DESTROYED;
2621 up_read(&vm->userptr.notifier_lock);
2623 xe_vm_insert_vma(vm, vma);
2627 case DRM_GPUVA_OP_PREFETCH:
2631 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2635 static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,
2636 struct drm_gpuva_ops **ops,
2641 for (i = num_ops_list - 1; i; ++i) {
2642 struct drm_gpuva_ops *__ops = ops[i];
2643 struct drm_gpuva_op *__op;
2648 drm_gpuva_for_each_op_reverse(__op, __ops) {
2649 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2651 xe_vma_op_unwind(vm, op,
2652 op->flags & XE_VMA_OP_COMMITTED,
2653 op->flags & XE_VMA_OP_PREV_COMMITTED,
2654 op->flags & XE_VMA_OP_NEXT_COMMITTED);
2657 drm_gpuva_ops_free(&vm->gpuvm, __ops);
2661 static int vm_bind_ioctl_ops_execute(struct xe_vm *vm,
2662 struct list_head *ops_list)
2664 struct xe_vma_op *op, *next;
2667 lockdep_assert_held_write(&vm->lock);
2669 list_for_each_entry_safe(op, next, ops_list, link) {
2670 err = xe_vma_op_execute(vm, op);
2672 drm_warn(&vm->xe->drm, "VM op(%d) failed with %d",
2675 * FIXME: Killing VM rather than proper error handling
2680 xe_vma_op_cleanup(vm, op);
2686 #ifdef TEST_VM_ASYNC_OPS_ERROR
2687 #define SUPPORTED_FLAGS \
2688 (FORCE_ASYNC_OP_ERROR | DRM_XE_VM_BIND_FLAG_READONLY | \
2689 DRM_XE_VM_BIND_FLAG_IMMEDIATE | DRM_XE_VM_BIND_FLAG_NULL | 0xffff)
2691 #define SUPPORTED_FLAGS \
2692 (DRM_XE_VM_BIND_FLAG_READONLY | \
2693 DRM_XE_VM_BIND_FLAG_IMMEDIATE | DRM_XE_VM_BIND_FLAG_NULL | \
2696 #define XE_64K_PAGE_MASK 0xffffull
2697 #define ALL_DRM_XE_SYNCS_FLAGS (DRM_XE_SYNCS_FLAG_WAIT_FOR_OP)
2699 #define MAX_BINDS 512 /* FIXME: Picking random upper limit */
2701 static int vm_bind_ioctl_check_args(struct xe_device *xe,
2702 struct drm_xe_vm_bind *args,
2703 struct drm_xe_vm_bind_op **bind_ops)
2708 if (XE_IOCTL_DBG(xe, args->pad || args->pad2) ||
2709 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2712 if (XE_IOCTL_DBG(xe, args->extensions) ||
2713 XE_IOCTL_DBG(xe, args->num_binds > MAX_BINDS))
2716 if (args->num_binds > 1) {
2717 u64 __user *bind_user =
2718 u64_to_user_ptr(args->vector_of_binds);
2720 *bind_ops = kmalloc(sizeof(struct drm_xe_vm_bind_op) *
2721 args->num_binds, GFP_KERNEL);
2725 err = __copy_from_user(*bind_ops, bind_user,
2726 sizeof(struct drm_xe_vm_bind_op) *
2728 if (XE_IOCTL_DBG(xe, err)) {
2733 *bind_ops = &args->bind;
2736 for (i = 0; i < args->num_binds; ++i) {
2737 u64 range = (*bind_ops)[i].range;
2738 u64 addr = (*bind_ops)[i].addr;
2739 u32 op = (*bind_ops)[i].op;
2740 u32 flags = (*bind_ops)[i].flags;
2741 u32 obj = (*bind_ops)[i].obj;
2742 u64 obj_offset = (*bind_ops)[i].obj_offset;
2743 u32 prefetch_region = (*bind_ops)[i].prefetch_mem_region_instance;
2744 bool is_null = flags & DRM_XE_VM_BIND_FLAG_NULL;
2745 u16 pat_index = (*bind_ops)[i].pat_index;
2748 if (XE_IOCTL_DBG(xe, pat_index >= xe->pat.n_entries)) {
2753 pat_index = array_index_nospec(pat_index, xe->pat.n_entries);
2754 (*bind_ops)[i].pat_index = pat_index;
2755 coh_mode = xe_pat_index_get_coh_mode(xe, pat_index);
2756 if (XE_IOCTL_DBG(xe, !coh_mode)) { /* hw reserved */
2761 if (XE_WARN_ON(coh_mode > XE_COH_AT_LEAST_1WAY)) {
2766 if (XE_IOCTL_DBG(xe, op > DRM_XE_VM_BIND_OP_PREFETCH) ||
2767 XE_IOCTL_DBG(xe, flags & ~SUPPORTED_FLAGS) ||
2768 XE_IOCTL_DBG(xe, obj && is_null) ||
2769 XE_IOCTL_DBG(xe, obj_offset && is_null) ||
2770 XE_IOCTL_DBG(xe, op != DRM_XE_VM_BIND_OP_MAP &&
2772 XE_IOCTL_DBG(xe, !obj &&
2773 op == DRM_XE_VM_BIND_OP_MAP &&
2775 XE_IOCTL_DBG(xe, !obj &&
2776 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
2777 XE_IOCTL_DBG(xe, addr &&
2778 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
2779 XE_IOCTL_DBG(xe, range &&
2780 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
2781 XE_IOCTL_DBG(xe, obj &&
2782 op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
2783 XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE &&
2784 op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
2785 XE_IOCTL_DBG(xe, obj &&
2786 op == DRM_XE_VM_BIND_OP_PREFETCH) ||
2787 XE_IOCTL_DBG(xe, prefetch_region &&
2788 op != DRM_XE_VM_BIND_OP_PREFETCH) ||
2789 XE_IOCTL_DBG(xe, !(BIT(prefetch_region) &
2790 xe->info.mem_region_mask)) ||
2791 XE_IOCTL_DBG(xe, obj &&
2792 op == DRM_XE_VM_BIND_OP_UNMAP)) {
2797 if (XE_IOCTL_DBG(xe, obj_offset & ~PAGE_MASK) ||
2798 XE_IOCTL_DBG(xe, addr & ~PAGE_MASK) ||
2799 XE_IOCTL_DBG(xe, range & ~PAGE_MASK) ||
2800 XE_IOCTL_DBG(xe, !range &&
2801 op != DRM_XE_VM_BIND_OP_UNMAP_ALL)) {
2810 if (args->num_binds > 1)
2815 static int vm_bind_ioctl_signal_fences(struct xe_vm *vm,
2816 struct xe_exec_queue *q,
2817 struct xe_sync_entry *syncs,
2820 struct dma_fence *fence;
2823 fence = xe_sync_in_fence_get(syncs, num_syncs,
2824 to_wait_exec_queue(vm, q), vm);
2826 return PTR_ERR(fence);
2828 for (i = 0; i < num_syncs; i++)
2829 xe_sync_entry_signal(&syncs[i], NULL, fence);
2831 xe_exec_queue_last_fence_set(to_wait_exec_queue(vm, q), vm,
2833 dma_fence_put(fence);
2838 int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2840 struct xe_device *xe = to_xe_device(dev);
2841 struct xe_file *xef = to_xe_file(file);
2842 struct drm_xe_vm_bind *args = data;
2843 struct drm_xe_sync __user *syncs_user;
2844 struct xe_bo **bos = NULL;
2845 struct drm_gpuva_ops **ops = NULL;
2847 struct xe_exec_queue *q = NULL;
2849 struct xe_sync_entry *syncs = NULL;
2850 struct drm_xe_vm_bind_op *bind_ops;
2851 LIST_HEAD(ops_list);
2855 err = vm_bind_ioctl_check_args(xe, args, &bind_ops);
2859 if (args->exec_queue_id) {
2860 q = xe_exec_queue_lookup(xef, args->exec_queue_id);
2861 if (XE_IOCTL_DBG(xe, !q)) {
2866 if (XE_IOCTL_DBG(xe, !(q->flags & EXEC_QUEUE_FLAG_VM))) {
2868 goto put_exec_queue;
2872 vm = xe_vm_lookup(xef, args->vm_id);
2873 if (XE_IOCTL_DBG(xe, !vm)) {
2875 goto put_exec_queue;
2878 err = down_write_killable(&vm->lock);
2882 if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
2884 goto release_vm_lock;
2887 for (i = 0; i < args->num_binds; ++i) {
2888 u64 range = bind_ops[i].range;
2889 u64 addr = bind_ops[i].addr;
2891 if (XE_IOCTL_DBG(xe, range > vm->size) ||
2892 XE_IOCTL_DBG(xe, addr > vm->size - range)) {
2894 goto release_vm_lock;
2898 if (args->num_binds) {
2899 bos = kcalloc(args->num_binds, sizeof(*bos), GFP_KERNEL);
2902 goto release_vm_lock;
2905 ops = kcalloc(args->num_binds, sizeof(*ops), GFP_KERNEL);
2908 goto release_vm_lock;
2912 for (i = 0; i < args->num_binds; ++i) {
2913 struct drm_gem_object *gem_obj;
2914 u64 range = bind_ops[i].range;
2915 u64 addr = bind_ops[i].addr;
2916 u32 obj = bind_ops[i].obj;
2917 u64 obj_offset = bind_ops[i].obj_offset;
2918 u16 pat_index = bind_ops[i].pat_index;
2924 gem_obj = drm_gem_object_lookup(file, obj);
2925 if (XE_IOCTL_DBG(xe, !gem_obj)) {
2929 bos[i] = gem_to_xe_bo(gem_obj);
2931 if (XE_IOCTL_DBG(xe, range > bos[i]->size) ||
2932 XE_IOCTL_DBG(xe, obj_offset >
2933 bos[i]->size - range)) {
2938 if (bos[i]->flags & XE_BO_INTERNAL_64K) {
2939 if (XE_IOCTL_DBG(xe, obj_offset &
2940 XE_64K_PAGE_MASK) ||
2941 XE_IOCTL_DBG(xe, addr & XE_64K_PAGE_MASK) ||
2942 XE_IOCTL_DBG(xe, range & XE_64K_PAGE_MASK)) {
2948 coh_mode = xe_pat_index_get_coh_mode(xe, pat_index);
2949 if (bos[i]->cpu_caching) {
2950 if (XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE &&
2951 bos[i]->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB)) {
2955 } else if (XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE)) {
2957 * Imported dma-buf from a different device should
2958 * require 1way or 2way coherency since we don't know
2959 * how it was mapped on the CPU. Just assume is it
2960 * potentially cached on CPU side.
2967 if (args->num_syncs) {
2968 syncs = kcalloc(args->num_syncs, sizeof(*syncs), GFP_KERNEL);
2975 syncs_user = u64_to_user_ptr(args->syncs);
2976 for (num_syncs = 0; num_syncs < args->num_syncs; num_syncs++) {
2977 err = xe_sync_entry_parse(xe, xef, &syncs[num_syncs],
2978 &syncs_user[num_syncs],
2979 (xe_vm_in_lr_mode(vm) ?
2980 SYNC_PARSE_FLAG_LR_MODE : 0) |
2982 SYNC_PARSE_FLAG_DISALLOW_USER_FENCE : 0));
2987 if (!args->num_binds) {
2992 for (i = 0; i < args->num_binds; ++i) {
2993 u64 range = bind_ops[i].range;
2994 u64 addr = bind_ops[i].addr;
2995 u32 op = bind_ops[i].op;
2996 u32 flags = bind_ops[i].flags;
2997 u64 obj_offset = bind_ops[i].obj_offset;
2998 u32 prefetch_region = bind_ops[i].prefetch_mem_region_instance;
2999 u16 pat_index = bind_ops[i].pat_index;
3001 ops[i] = vm_bind_ioctl_ops_create(vm, bos[i], obj_offset,
3002 addr, range, op, flags,
3003 prefetch_region, pat_index);
3004 if (IS_ERR(ops[i])) {
3005 err = PTR_ERR(ops[i]);
3010 err = vm_bind_ioctl_ops_parse(vm, q, ops[i], syncs, num_syncs,
3012 i == args->num_binds - 1);
3018 if (list_empty(&ops_list)) {
3025 xe_exec_queue_get(q);
3027 err = vm_bind_ioctl_ops_execute(vm, &ops_list);
3029 up_write(&vm->lock);
3032 xe_exec_queue_put(q);
3035 for (i = 0; bos && i < args->num_binds; ++i)
3040 if (args->num_binds > 1)
3046 vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
3048 if (err == -ENODATA)
3049 err = vm_bind_ioctl_signal_fences(vm, q, syncs, num_syncs);
3051 xe_sync_entry_cleanup(&syncs[num_syncs]);
3055 for (i = 0; i < args->num_binds; ++i)
3058 up_write(&vm->lock);
3063 xe_exec_queue_put(q);
3067 if (args->num_binds > 1)
3073 * xe_vm_lock() - Lock the vm's dma_resv object
3074 * @vm: The struct xe_vm whose lock is to be locked
3075 * @intr: Whether to perform any wait interruptible
3077 * Return: 0 on success, -EINTR if @intr is true and the wait for a
3078 * contended lock was interrupted. If @intr is false, the function
3081 int xe_vm_lock(struct xe_vm *vm, bool intr)
3084 return dma_resv_lock_interruptible(xe_vm_resv(vm), NULL);
3086 return dma_resv_lock(xe_vm_resv(vm), NULL);
3090 * xe_vm_unlock() - Unlock the vm's dma_resv object
3091 * @vm: The struct xe_vm whose lock is to be released.
3093 * Unlock a buffer object lock that was locked by xe_vm_lock().
3095 void xe_vm_unlock(struct xe_vm *vm)
3097 dma_resv_unlock(xe_vm_resv(vm));
3101 * xe_vm_invalidate_vma - invalidate GPU mappings for VMA without a lock
3102 * @vma: VMA to invalidate
3104 * Walks a list of page tables leaves which it memset the entries owned by this
3105 * VMA to zero, invalidates the TLBs, and block until TLBs invalidation is
3108 * Returns 0 for success, negative error code otherwise.
3110 int xe_vm_invalidate_vma(struct xe_vma *vma)
3112 struct xe_device *xe = xe_vma_vm(vma)->xe;
3113 struct xe_tile *tile;
3114 u32 tile_needs_invalidate = 0;
3115 int seqno[XE_MAX_TILES_PER_DEVICE];
3119 xe_assert(xe, xe_vm_in_fault_mode(xe_vma_vm(vma)));
3120 xe_assert(xe, !xe_vma_is_null(vma));
3121 trace_xe_vma_usm_invalidate(vma);
3123 /* Check that we don't race with page-table updates */
3124 if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
3125 if (xe_vma_is_userptr(vma)) {
3126 WARN_ON_ONCE(!mmu_interval_check_retry
3127 (&vma->userptr.notifier,
3128 vma->userptr.notifier_seq));
3129 WARN_ON_ONCE(!dma_resv_test_signaled(xe_vm_resv(xe_vma_vm(vma)),
3130 DMA_RESV_USAGE_BOOKKEEP));
3133 xe_bo_assert_held(xe_vma_bo(vma));
3137 for_each_tile(tile, xe, id) {
3138 if (xe_pt_zap_ptes(tile, vma)) {
3139 tile_needs_invalidate |= BIT(id);
3142 * FIXME: We potentially need to invalidate multiple
3143 * GTs within the tile
3145 seqno[id] = xe_gt_tlb_invalidation_vma(tile->primary_gt, NULL, vma);
3151 for_each_tile(tile, xe, id) {
3152 if (tile_needs_invalidate & BIT(id)) {
3153 ret = xe_gt_tlb_invalidation_wait(tile->primary_gt, seqno[id]);
3159 vma->usm.tile_invalidated = vma->tile_mask;
3164 int xe_analyze_vm(struct drm_printer *p, struct xe_vm *vm, int gt_id)
3166 struct drm_gpuva *gpuva;
3170 if (!down_read_trylock(&vm->lock)) {
3171 drm_printf(p, " Failed to acquire VM lock to dump capture");
3174 if (vm->pt_root[gt_id]) {
3175 addr = xe_bo_addr(vm->pt_root[gt_id]->bo, 0, XE_PAGE_SIZE);
3176 is_vram = xe_bo_is_vram(vm->pt_root[gt_id]->bo);
3177 drm_printf(p, " VM root: A:0x%llx %s\n", addr,
3178 is_vram ? "VRAM" : "SYS");
3181 drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
3182 struct xe_vma *vma = gpuva_to_vma(gpuva);
3183 bool is_userptr = xe_vma_is_userptr(vma);
3184 bool is_null = xe_vma_is_null(vma);
3188 } else if (is_userptr) {
3189 struct xe_res_cursor cur;
3191 if (vma->userptr.sg) {
3192 xe_res_first_sg(vma->userptr.sg, 0, XE_PAGE_SIZE,
3194 addr = xe_res_dma(&cur);
3199 addr = __xe_bo_addr(xe_vma_bo(vma), 0, XE_PAGE_SIZE);
3200 is_vram = xe_bo_is_vram(xe_vma_bo(vma));
3202 drm_printf(p, " [%016llx-%016llx] S:0x%016llx A:%016llx %s\n",
3203 xe_vma_start(vma), xe_vma_end(vma) - 1,
3205 addr, is_null ? "NULL" : is_userptr ? "USR" :
3206 is_vram ? "VRAM" : "SYS");