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_pagefault.h"
31 #include "xe_gt_tlb_invalidation.h"
32 #include "xe_migrate.h"
33 #include "xe_pat.h"
34 #include "xe_pm.h"
35 #include "xe_preempt_fence.h"
36 #include "xe_pt.h"
37 #include "xe_pxp.h"
38 #include "xe_res_cursor.h"
39 #include "xe_svm.h"
40 #include "xe_sync.h"
41 #include "xe_trace_bo.h"
42 #include "xe_wa.h"
43 #include "xe_hmm.h"
44
xe_vm_obj(struct xe_vm * vm)45 static struct drm_gem_object *xe_vm_obj(struct xe_vm *vm)
46 {
47 return vm->gpuvm.r_obj;
48 }
49
50 /**
51 * xe_vma_userptr_check_repin() - Advisory check for repin needed
52 * @uvma: The userptr vma
53 *
54 * Check if the userptr vma has been invalidated since last successful
55 * repin. The check is advisory only and can the function can be called
56 * without the vm->userptr.notifier_lock held. There is no guarantee that the
57 * vma userptr will remain valid after a lockless check, so typically
58 * the call needs to be followed by a proper check under the notifier_lock.
59 *
60 * Return: 0 if userptr vma is valid, -EAGAIN otherwise; repin recommended.
61 */
xe_vma_userptr_check_repin(struct xe_userptr_vma * uvma)62 int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma)
63 {
64 return mmu_interval_check_retry(&uvma->userptr.notifier,
65 uvma->userptr.notifier_seq) ?
66 -EAGAIN : 0;
67 }
68
xe_vma_userptr_pin_pages(struct xe_userptr_vma * uvma)69 int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma)
70 {
71 struct xe_vma *vma = &uvma->vma;
72 struct xe_vm *vm = xe_vma_vm(vma);
73 struct xe_device *xe = vm->xe;
74
75 lockdep_assert_held(&vm->lock);
76 xe_assert(xe, xe_vma_is_userptr(vma));
77
78 return xe_hmm_userptr_populate_range(uvma, false);
79 }
80
preempt_fences_waiting(struct xe_vm * vm)81 static bool preempt_fences_waiting(struct xe_vm *vm)
82 {
83 struct xe_exec_queue *q;
84
85 lockdep_assert_held(&vm->lock);
86 xe_vm_assert_held(vm);
87
88 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
89 if (!q->lr.pfence ||
90 test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
91 &q->lr.pfence->flags)) {
92 return true;
93 }
94 }
95
96 return false;
97 }
98
free_preempt_fences(struct list_head * list)99 static void free_preempt_fences(struct list_head *list)
100 {
101 struct list_head *link, *next;
102
103 list_for_each_safe(link, next, list)
104 xe_preempt_fence_free(to_preempt_fence_from_link(link));
105 }
106
alloc_preempt_fences(struct xe_vm * vm,struct list_head * list,unsigned int * count)107 static int alloc_preempt_fences(struct xe_vm *vm, struct list_head *list,
108 unsigned int *count)
109 {
110 lockdep_assert_held(&vm->lock);
111 xe_vm_assert_held(vm);
112
113 if (*count >= vm->preempt.num_exec_queues)
114 return 0;
115
116 for (; *count < vm->preempt.num_exec_queues; ++(*count)) {
117 struct xe_preempt_fence *pfence = xe_preempt_fence_alloc();
118
119 if (IS_ERR(pfence))
120 return PTR_ERR(pfence);
121
122 list_move_tail(xe_preempt_fence_link(pfence), list);
123 }
124
125 return 0;
126 }
127
wait_for_existing_preempt_fences(struct xe_vm * vm)128 static int wait_for_existing_preempt_fences(struct xe_vm *vm)
129 {
130 struct xe_exec_queue *q;
131
132 xe_vm_assert_held(vm);
133
134 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
135 if (q->lr.pfence) {
136 long timeout = dma_fence_wait(q->lr.pfence, false);
137
138 /* Only -ETIME on fence indicates VM needs to be killed */
139 if (timeout < 0 || q->lr.pfence->error == -ETIME)
140 return -ETIME;
141
142 dma_fence_put(q->lr.pfence);
143 q->lr.pfence = NULL;
144 }
145 }
146
147 return 0;
148 }
149
xe_vm_is_idle(struct xe_vm * vm)150 static bool xe_vm_is_idle(struct xe_vm *vm)
151 {
152 struct xe_exec_queue *q;
153
154 xe_vm_assert_held(vm);
155 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
156 if (!xe_exec_queue_is_idle(q))
157 return false;
158 }
159
160 return true;
161 }
162
arm_preempt_fences(struct xe_vm * vm,struct list_head * list)163 static void arm_preempt_fences(struct xe_vm *vm, struct list_head *list)
164 {
165 struct list_head *link;
166 struct xe_exec_queue *q;
167
168 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
169 struct dma_fence *fence;
170
171 link = list->next;
172 xe_assert(vm->xe, link != list);
173
174 fence = xe_preempt_fence_arm(to_preempt_fence_from_link(link),
175 q, q->lr.context,
176 ++q->lr.seqno);
177 dma_fence_put(q->lr.pfence);
178 q->lr.pfence = fence;
179 }
180 }
181
add_preempt_fences(struct xe_vm * vm,struct xe_bo * bo)182 static int add_preempt_fences(struct xe_vm *vm, struct xe_bo *bo)
183 {
184 struct xe_exec_queue *q;
185 int err;
186
187 xe_bo_assert_held(bo);
188
189 if (!vm->preempt.num_exec_queues)
190 return 0;
191
192 err = dma_resv_reserve_fences(bo->ttm.base.resv, vm->preempt.num_exec_queues);
193 if (err)
194 return err;
195
196 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link)
197 if (q->lr.pfence) {
198 dma_resv_add_fence(bo->ttm.base.resv,
199 q->lr.pfence,
200 DMA_RESV_USAGE_BOOKKEEP);
201 }
202
203 return 0;
204 }
205
resume_and_reinstall_preempt_fences(struct xe_vm * vm,struct drm_exec * exec)206 static void resume_and_reinstall_preempt_fences(struct xe_vm *vm,
207 struct drm_exec *exec)
208 {
209 struct xe_exec_queue *q;
210
211 lockdep_assert_held(&vm->lock);
212 xe_vm_assert_held(vm);
213
214 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link) {
215 q->ops->resume(q);
216
217 drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, q->lr.pfence,
218 DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
219 }
220 }
221
xe_vm_add_compute_exec_queue(struct xe_vm * vm,struct xe_exec_queue * q)222 int xe_vm_add_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
223 {
224 struct drm_gpuvm_exec vm_exec = {
225 .vm = &vm->gpuvm,
226 .flags = DRM_EXEC_INTERRUPTIBLE_WAIT,
227 .num_fences = 1,
228 };
229 struct drm_exec *exec = &vm_exec.exec;
230 struct dma_fence *pfence;
231 int err;
232 bool wait;
233
234 xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm));
235
236 down_write(&vm->lock);
237 err = drm_gpuvm_exec_lock(&vm_exec);
238 if (err)
239 goto out_up_write;
240
241 pfence = xe_preempt_fence_create(q, q->lr.context,
242 ++q->lr.seqno);
243 if (!pfence) {
244 err = -ENOMEM;
245 goto out_fini;
246 }
247
248 list_add(&q->lr.link, &vm->preempt.exec_queues);
249 ++vm->preempt.num_exec_queues;
250 q->lr.pfence = pfence;
251
252 down_read(&vm->userptr.notifier_lock);
253
254 drm_gpuvm_resv_add_fence(&vm->gpuvm, exec, pfence,
255 DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
256
257 /*
258 * Check to see if a preemption on VM is in flight or userptr
259 * invalidation, if so trigger this preempt fence to sync state with
260 * other preempt fences on the VM.
261 */
262 wait = __xe_vm_userptr_needs_repin(vm) || preempt_fences_waiting(vm);
263 if (wait)
264 dma_fence_enable_sw_signaling(pfence);
265
266 up_read(&vm->userptr.notifier_lock);
267
268 out_fini:
269 drm_exec_fini(exec);
270 out_up_write:
271 up_write(&vm->lock);
272
273 return err;
274 }
275 ALLOW_ERROR_INJECTION(xe_vm_add_compute_exec_queue, ERRNO);
276
277 /**
278 * xe_vm_remove_compute_exec_queue() - Remove compute exec queue from VM
279 * @vm: The VM.
280 * @q: The exec_queue
281 *
282 * Note that this function might be called multiple times on the same queue.
283 */
xe_vm_remove_compute_exec_queue(struct xe_vm * vm,struct xe_exec_queue * q)284 void xe_vm_remove_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
285 {
286 if (!xe_vm_in_preempt_fence_mode(vm))
287 return;
288
289 down_write(&vm->lock);
290 if (!list_empty(&q->lr.link)) {
291 list_del_init(&q->lr.link);
292 --vm->preempt.num_exec_queues;
293 }
294 if (q->lr.pfence) {
295 dma_fence_enable_sw_signaling(q->lr.pfence);
296 dma_fence_put(q->lr.pfence);
297 q->lr.pfence = NULL;
298 }
299 up_write(&vm->lock);
300 }
301
302 /**
303 * __xe_vm_userptr_needs_repin() - Check whether the VM does have userptrs
304 * that need repinning.
305 * @vm: The VM.
306 *
307 * This function checks for whether the VM has userptrs that need repinning,
308 * and provides a release-type barrier on the userptr.notifier_lock after
309 * checking.
310 *
311 * Return: 0 if there are no userptrs needing repinning, -EAGAIN if there are.
312 */
__xe_vm_userptr_needs_repin(struct xe_vm * vm)313 int __xe_vm_userptr_needs_repin(struct xe_vm *vm)
314 {
315 lockdep_assert_held_read(&vm->userptr.notifier_lock);
316
317 return (list_empty(&vm->userptr.repin_list) &&
318 list_empty(&vm->userptr.invalidated)) ? 0 : -EAGAIN;
319 }
320
321 #define XE_VM_REBIND_RETRY_TIMEOUT_MS 1000
322
323 /**
324 * xe_vm_kill() - VM Kill
325 * @vm: The VM.
326 * @unlocked: Flag indicates the VM's dma-resv is not held
327 *
328 * Kill the VM by setting banned flag indicated VM is no longer available for
329 * use. If in preempt fence mode, also kill all exec queue attached to the VM.
330 */
xe_vm_kill(struct xe_vm * vm,bool unlocked)331 void xe_vm_kill(struct xe_vm *vm, bool unlocked)
332 {
333 struct xe_exec_queue *q;
334
335 lockdep_assert_held(&vm->lock);
336
337 if (unlocked)
338 xe_vm_lock(vm, false);
339
340 vm->flags |= XE_VM_FLAG_BANNED;
341 trace_xe_vm_kill(vm);
342
343 list_for_each_entry(q, &vm->preempt.exec_queues, lr.link)
344 q->ops->kill(q);
345
346 if (unlocked)
347 xe_vm_unlock(vm);
348
349 /* TODO: Inform user the VM is banned */
350 }
351
352 /**
353 * xe_vm_validate_should_retry() - Whether to retry after a validate error.
354 * @exec: The drm_exec object used for locking before validation.
355 * @err: The error returned from ttm_bo_validate().
356 * @end: A ktime_t cookie that should be set to 0 before first use and
357 * that should be reused on subsequent calls.
358 *
359 * With multiple active VMs, under memory pressure, it is possible that
360 * ttm_bo_validate() run into -EDEADLK and in such case returns -ENOMEM.
361 * Until ttm properly handles locking in such scenarios, best thing the
362 * driver can do is retry with a timeout. Check if that is necessary, and
363 * if so unlock the drm_exec's objects while keeping the ticket to prepare
364 * for a rerun.
365 *
366 * Return: true if a retry after drm_exec_init() is recommended;
367 * false otherwise.
368 */
xe_vm_validate_should_retry(struct drm_exec * exec,int err,ktime_t * end)369 bool xe_vm_validate_should_retry(struct drm_exec *exec, int err, ktime_t *end)
370 {
371 ktime_t cur;
372
373 if (err != -ENOMEM)
374 return false;
375
376 cur = ktime_get();
377 *end = *end ? : ktime_add_ms(cur, XE_VM_REBIND_RETRY_TIMEOUT_MS);
378 if (!ktime_before(cur, *end))
379 return false;
380
381 msleep(20);
382 return true;
383 }
384
xe_gpuvm_validate(struct drm_gpuvm_bo * vm_bo,struct drm_exec * exec)385 static int xe_gpuvm_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec)
386 {
387 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
388 struct drm_gpuva *gpuva;
389 int ret;
390
391 lockdep_assert_held(&vm->lock);
392 drm_gpuvm_bo_for_each_va(gpuva, vm_bo)
393 list_move_tail(&gpuva_to_vma(gpuva)->combined_links.rebind,
394 &vm->rebind_list);
395
396 ret = xe_bo_validate(gem_to_xe_bo(vm_bo->obj), vm, false);
397 if (ret)
398 return ret;
399
400 vm_bo->evicted = false;
401 return 0;
402 }
403
404 /**
405 * xe_vm_validate_rebind() - Validate buffer objects and rebind vmas
406 * @vm: The vm for which we are rebinding.
407 * @exec: The struct drm_exec with the locked GEM objects.
408 * @num_fences: The number of fences to reserve for the operation, not
409 * including rebinds and validations.
410 *
411 * Validates all evicted gem objects and rebinds their vmas. Note that
412 * rebindings may cause evictions and hence the validation-rebind
413 * sequence is rerun until there are no more objects to validate.
414 *
415 * Return: 0 on success, negative error code on error. In particular,
416 * may return -EINTR or -ERESTARTSYS if interrupted, and -EDEADLK if
417 * the drm_exec transaction needs to be restarted.
418 */
xe_vm_validate_rebind(struct xe_vm * vm,struct drm_exec * exec,unsigned int num_fences)419 int xe_vm_validate_rebind(struct xe_vm *vm, struct drm_exec *exec,
420 unsigned int num_fences)
421 {
422 struct drm_gem_object *obj;
423 unsigned long index;
424 int ret;
425
426 do {
427 ret = drm_gpuvm_validate(&vm->gpuvm, exec);
428 if (ret)
429 return ret;
430
431 ret = xe_vm_rebind(vm, false);
432 if (ret)
433 return ret;
434 } while (!list_empty(&vm->gpuvm.evict.list));
435
436 drm_exec_for_each_locked_object(exec, index, obj) {
437 ret = dma_resv_reserve_fences(obj->resv, num_fences);
438 if (ret)
439 return ret;
440 }
441
442 return 0;
443 }
444
xe_preempt_work_begin(struct drm_exec * exec,struct xe_vm * vm,bool * done)445 static int xe_preempt_work_begin(struct drm_exec *exec, struct xe_vm *vm,
446 bool *done)
447 {
448 int err;
449
450 err = drm_gpuvm_prepare_vm(&vm->gpuvm, exec, 0);
451 if (err)
452 return err;
453
454 if (xe_vm_is_idle(vm)) {
455 vm->preempt.rebind_deactivated = true;
456 *done = true;
457 return 0;
458 }
459
460 if (!preempt_fences_waiting(vm)) {
461 *done = true;
462 return 0;
463 }
464
465 err = drm_gpuvm_prepare_objects(&vm->gpuvm, exec, 0);
466 if (err)
467 return err;
468
469 err = wait_for_existing_preempt_fences(vm);
470 if (err)
471 return err;
472
473 /*
474 * Add validation and rebinding to the locking loop since both can
475 * cause evictions which may require blocing dma_resv locks.
476 * The fence reservation here is intended for the new preempt fences
477 * we attach at the end of the rebind work.
478 */
479 return xe_vm_validate_rebind(vm, exec, vm->preempt.num_exec_queues);
480 }
481
preempt_rebind_work_func(struct work_struct * w)482 static void preempt_rebind_work_func(struct work_struct *w)
483 {
484 struct xe_vm *vm = container_of(w, struct xe_vm, preempt.rebind_work);
485 struct drm_exec exec;
486 unsigned int fence_count = 0;
487 LIST_HEAD(preempt_fences);
488 ktime_t end = 0;
489 int err = 0;
490 long wait;
491 int __maybe_unused tries = 0;
492
493 xe_assert(vm->xe, xe_vm_in_preempt_fence_mode(vm));
494 trace_xe_vm_rebind_worker_enter(vm);
495
496 down_write(&vm->lock);
497
498 if (xe_vm_is_closed_or_banned(vm)) {
499 up_write(&vm->lock);
500 trace_xe_vm_rebind_worker_exit(vm);
501 return;
502 }
503
504 retry:
505 if (xe_vm_userptr_check_repin(vm)) {
506 err = xe_vm_userptr_pin(vm);
507 if (err)
508 goto out_unlock_outer;
509 }
510
511 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
512
513 drm_exec_until_all_locked(&exec) {
514 bool done = false;
515
516 err = xe_preempt_work_begin(&exec, vm, &done);
517 drm_exec_retry_on_contention(&exec);
518 if (err || done) {
519 drm_exec_fini(&exec);
520 if (err && xe_vm_validate_should_retry(&exec, err, &end))
521 err = -EAGAIN;
522
523 goto out_unlock_outer;
524 }
525 }
526
527 err = alloc_preempt_fences(vm, &preempt_fences, &fence_count);
528 if (err)
529 goto out_unlock;
530
531 err = xe_vm_rebind(vm, true);
532 if (err)
533 goto out_unlock;
534
535 /* Wait on rebinds and munmap style VM unbinds */
536 wait = dma_resv_wait_timeout(xe_vm_resv(vm),
537 DMA_RESV_USAGE_KERNEL,
538 false, MAX_SCHEDULE_TIMEOUT);
539 if (wait <= 0) {
540 err = -ETIME;
541 goto out_unlock;
542 }
543
544 #define retry_required(__tries, __vm) \
545 (IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT) ? \
546 (!(__tries)++ || __xe_vm_userptr_needs_repin(__vm)) : \
547 __xe_vm_userptr_needs_repin(__vm))
548
549 down_read(&vm->userptr.notifier_lock);
550 if (retry_required(tries, vm)) {
551 up_read(&vm->userptr.notifier_lock);
552 err = -EAGAIN;
553 goto out_unlock;
554 }
555
556 #undef retry_required
557
558 spin_lock(&vm->xe->ttm.lru_lock);
559 ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
560 spin_unlock(&vm->xe->ttm.lru_lock);
561
562 /* Point of no return. */
563 arm_preempt_fences(vm, &preempt_fences);
564 resume_and_reinstall_preempt_fences(vm, &exec);
565 up_read(&vm->userptr.notifier_lock);
566
567 out_unlock:
568 drm_exec_fini(&exec);
569 out_unlock_outer:
570 if (err == -EAGAIN) {
571 trace_xe_vm_rebind_worker_retry(vm);
572 goto retry;
573 }
574
575 if (err) {
576 drm_warn(&vm->xe->drm, "VM worker error: %d\n", err);
577 xe_vm_kill(vm, true);
578 }
579 up_write(&vm->lock);
580
581 free_preempt_fences(&preempt_fences);
582
583 trace_xe_vm_rebind_worker_exit(vm);
584 }
585
__vma_userptr_invalidate(struct xe_vm * vm,struct xe_userptr_vma * uvma)586 static void __vma_userptr_invalidate(struct xe_vm *vm, struct xe_userptr_vma *uvma)
587 {
588 struct xe_userptr *userptr = &uvma->userptr;
589 struct xe_vma *vma = &uvma->vma;
590 struct dma_resv_iter cursor;
591 struct dma_fence *fence;
592 long err;
593
594 /*
595 * Tell exec and rebind worker they need to repin and rebind this
596 * userptr.
597 */
598 if (!xe_vm_in_fault_mode(vm) &&
599 !(vma->gpuva.flags & XE_VMA_DESTROYED)) {
600 spin_lock(&vm->userptr.invalidated_lock);
601 list_move_tail(&userptr->invalidate_link,
602 &vm->userptr.invalidated);
603 spin_unlock(&vm->userptr.invalidated_lock);
604 }
605
606 /*
607 * Preempt fences turn into schedule disables, pipeline these.
608 * Note that even in fault mode, we need to wait for binds and
609 * unbinds to complete, and those are attached as BOOKMARK fences
610 * to the vm.
611 */
612 dma_resv_iter_begin(&cursor, xe_vm_resv(vm),
613 DMA_RESV_USAGE_BOOKKEEP);
614 dma_resv_for_each_fence_unlocked(&cursor, fence)
615 dma_fence_enable_sw_signaling(fence);
616 dma_resv_iter_end(&cursor);
617
618 err = dma_resv_wait_timeout(xe_vm_resv(vm),
619 DMA_RESV_USAGE_BOOKKEEP,
620 false, MAX_SCHEDULE_TIMEOUT);
621 XE_WARN_ON(err <= 0);
622
623 if (xe_vm_in_fault_mode(vm) && userptr->initial_bind) {
624 err = xe_vm_invalidate_vma(vma);
625 XE_WARN_ON(err);
626 }
627
628 xe_hmm_userptr_unmap(uvma);
629 }
630
vma_userptr_invalidate(struct mmu_interval_notifier * mni,const struct mmu_notifier_range * range,unsigned long cur_seq)631 static bool vma_userptr_invalidate(struct mmu_interval_notifier *mni,
632 const struct mmu_notifier_range *range,
633 unsigned long cur_seq)
634 {
635 struct xe_userptr_vma *uvma = container_of(mni, typeof(*uvma), userptr.notifier);
636 struct xe_vma *vma = &uvma->vma;
637 struct xe_vm *vm = xe_vma_vm(vma);
638
639 xe_assert(vm->xe, xe_vma_is_userptr(vma));
640 trace_xe_vma_userptr_invalidate(vma);
641
642 if (!mmu_notifier_range_blockable(range))
643 return false;
644
645 vm_dbg(&xe_vma_vm(vma)->xe->drm,
646 "NOTIFIER: addr=0x%016llx, range=0x%016llx",
647 xe_vma_start(vma), xe_vma_size(vma));
648
649 down_write(&vm->userptr.notifier_lock);
650 mmu_interval_set_seq(mni, cur_seq);
651
652 __vma_userptr_invalidate(vm, uvma);
653 up_write(&vm->userptr.notifier_lock);
654 trace_xe_vma_userptr_invalidate_complete(vma);
655
656 return true;
657 }
658
659 static const struct mmu_interval_notifier_ops vma_userptr_notifier_ops = {
660 .invalidate = vma_userptr_invalidate,
661 };
662
663 #if IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT)
664 /**
665 * xe_vma_userptr_force_invalidate() - force invalidate a userptr
666 * @uvma: The userptr vma to invalidate
667 *
668 * Perform a forced userptr invalidation for testing purposes.
669 */
xe_vma_userptr_force_invalidate(struct xe_userptr_vma * uvma)670 void xe_vma_userptr_force_invalidate(struct xe_userptr_vma *uvma)
671 {
672 struct xe_vm *vm = xe_vma_vm(&uvma->vma);
673
674 /* Protect against concurrent userptr pinning */
675 lockdep_assert_held(&vm->lock);
676 /* Protect against concurrent notifiers */
677 lockdep_assert_held(&vm->userptr.notifier_lock);
678 /*
679 * Protect against concurrent instances of this function and
680 * the critical exec sections
681 */
682 xe_vm_assert_held(vm);
683
684 if (!mmu_interval_read_retry(&uvma->userptr.notifier,
685 uvma->userptr.notifier_seq))
686 uvma->userptr.notifier_seq -= 2;
687 __vma_userptr_invalidate(vm, uvma);
688 }
689 #endif
690
xe_vm_userptr_pin(struct xe_vm * vm)691 int xe_vm_userptr_pin(struct xe_vm *vm)
692 {
693 struct xe_userptr_vma *uvma, *next;
694 int err = 0;
695
696 xe_assert(vm->xe, !xe_vm_in_fault_mode(vm));
697 lockdep_assert_held_write(&vm->lock);
698
699 /* Collect invalidated userptrs */
700 spin_lock(&vm->userptr.invalidated_lock);
701 xe_assert(vm->xe, list_empty(&vm->userptr.repin_list));
702 list_for_each_entry_safe(uvma, next, &vm->userptr.invalidated,
703 userptr.invalidate_link) {
704 list_del_init(&uvma->userptr.invalidate_link);
705 list_add_tail(&uvma->userptr.repin_link,
706 &vm->userptr.repin_list);
707 }
708 spin_unlock(&vm->userptr.invalidated_lock);
709
710 /* Pin and move to bind list */
711 list_for_each_entry_safe(uvma, next, &vm->userptr.repin_list,
712 userptr.repin_link) {
713 err = xe_vma_userptr_pin_pages(uvma);
714 if (err == -EFAULT) {
715 list_del_init(&uvma->userptr.repin_link);
716 /*
717 * We might have already done the pin once already, but
718 * then had to retry before the re-bind happened, due
719 * some other condition in the caller, but in the
720 * meantime the userptr got dinged by the notifier such
721 * that we need to revalidate here, but this time we hit
722 * the EFAULT. In such a case make sure we remove
723 * ourselves from the rebind list to avoid going down in
724 * flames.
725 */
726 if (!list_empty(&uvma->vma.combined_links.rebind))
727 list_del_init(&uvma->vma.combined_links.rebind);
728
729 /* Wait for pending binds */
730 xe_vm_lock(vm, false);
731 dma_resv_wait_timeout(xe_vm_resv(vm),
732 DMA_RESV_USAGE_BOOKKEEP,
733 false, MAX_SCHEDULE_TIMEOUT);
734
735 down_read(&vm->userptr.notifier_lock);
736 err = xe_vm_invalidate_vma(&uvma->vma);
737 up_read(&vm->userptr.notifier_lock);
738 xe_vm_unlock(vm);
739 if (err)
740 break;
741 } else {
742 if (err)
743 break;
744
745 list_del_init(&uvma->userptr.repin_link);
746 list_move_tail(&uvma->vma.combined_links.rebind,
747 &vm->rebind_list);
748 }
749 }
750
751 if (err) {
752 down_write(&vm->userptr.notifier_lock);
753 spin_lock(&vm->userptr.invalidated_lock);
754 list_for_each_entry_safe(uvma, next, &vm->userptr.repin_list,
755 userptr.repin_link) {
756 list_del_init(&uvma->userptr.repin_link);
757 list_move_tail(&uvma->userptr.invalidate_link,
758 &vm->userptr.invalidated);
759 }
760 spin_unlock(&vm->userptr.invalidated_lock);
761 up_write(&vm->userptr.notifier_lock);
762 }
763 return err;
764 }
765
766 /**
767 * xe_vm_userptr_check_repin() - Check whether the VM might have userptrs
768 * that need repinning.
769 * @vm: The VM.
770 *
771 * This function does an advisory check for whether the VM has userptrs that
772 * need repinning.
773 *
774 * Return: 0 if there are no indications of userptrs needing repinning,
775 * -EAGAIN if there are.
776 */
xe_vm_userptr_check_repin(struct xe_vm * vm)777 int xe_vm_userptr_check_repin(struct xe_vm *vm)
778 {
779 return (list_empty_careful(&vm->userptr.repin_list) &&
780 list_empty_careful(&vm->userptr.invalidated)) ? 0 : -EAGAIN;
781 }
782
xe_vma_ops_alloc(struct xe_vma_ops * vops,bool array_of_binds)783 static int xe_vma_ops_alloc(struct xe_vma_ops *vops, bool array_of_binds)
784 {
785 int i;
786
787 for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i) {
788 if (!vops->pt_update_ops[i].num_ops)
789 continue;
790
791 vops->pt_update_ops[i].ops =
792 kmalloc_array(vops->pt_update_ops[i].num_ops,
793 sizeof(*vops->pt_update_ops[i].ops),
794 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
795 if (!vops->pt_update_ops[i].ops)
796 return array_of_binds ? -ENOBUFS : -ENOMEM;
797 }
798
799 return 0;
800 }
801 ALLOW_ERROR_INJECTION(xe_vma_ops_alloc, ERRNO);
802
xe_vma_svm_prefetch_op_fini(struct xe_vma_op * op)803 static void xe_vma_svm_prefetch_op_fini(struct xe_vma_op *op)
804 {
805 struct xe_vma *vma;
806
807 vma = gpuva_to_vma(op->base.prefetch.va);
808
809 if (op->base.op == DRM_GPUVA_OP_PREFETCH && xe_vma_is_cpu_addr_mirror(vma))
810 xa_destroy(&op->prefetch_range.range);
811 }
812
xe_vma_svm_prefetch_ops_fini(struct xe_vma_ops * vops)813 static void xe_vma_svm_prefetch_ops_fini(struct xe_vma_ops *vops)
814 {
815 struct xe_vma_op *op;
816
817 if (!(vops->flags & XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH))
818 return;
819
820 list_for_each_entry(op, &vops->list, link)
821 xe_vma_svm_prefetch_op_fini(op);
822 }
823
xe_vma_ops_fini(struct xe_vma_ops * vops)824 static void xe_vma_ops_fini(struct xe_vma_ops *vops)
825 {
826 int i;
827
828 xe_vma_svm_prefetch_ops_fini(vops);
829
830 for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
831 kfree(vops->pt_update_ops[i].ops);
832 }
833
xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops * vops,u8 tile_mask,int inc_val)834 static void xe_vma_ops_incr_pt_update_ops(struct xe_vma_ops *vops, u8 tile_mask, int inc_val)
835 {
836 int i;
837
838 if (!inc_val)
839 return;
840
841 for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
842 if (BIT(i) & tile_mask)
843 vops->pt_update_ops[i].num_ops += inc_val;
844 }
845
xe_vm_populate_rebind(struct xe_vma_op * op,struct xe_vma * vma,u8 tile_mask)846 static void xe_vm_populate_rebind(struct xe_vma_op *op, struct xe_vma *vma,
847 u8 tile_mask)
848 {
849 INIT_LIST_HEAD(&op->link);
850 op->tile_mask = tile_mask;
851 op->base.op = DRM_GPUVA_OP_MAP;
852 op->base.map.va.addr = vma->gpuva.va.addr;
853 op->base.map.va.range = vma->gpuva.va.range;
854 op->base.map.gem.obj = vma->gpuva.gem.obj;
855 op->base.map.gem.offset = vma->gpuva.gem.offset;
856 op->map.vma = vma;
857 op->map.immediate = true;
858 op->map.dumpable = vma->gpuva.flags & XE_VMA_DUMPABLE;
859 op->map.is_null = xe_vma_is_null(vma);
860 }
861
xe_vm_ops_add_rebind(struct xe_vma_ops * vops,struct xe_vma * vma,u8 tile_mask)862 static int xe_vm_ops_add_rebind(struct xe_vma_ops *vops, struct xe_vma *vma,
863 u8 tile_mask)
864 {
865 struct xe_vma_op *op;
866
867 op = kzalloc(sizeof(*op), GFP_KERNEL);
868 if (!op)
869 return -ENOMEM;
870
871 xe_vm_populate_rebind(op, vma, tile_mask);
872 list_add_tail(&op->link, &vops->list);
873 xe_vma_ops_incr_pt_update_ops(vops, tile_mask, 1);
874
875 return 0;
876 }
877
878 static struct dma_fence *ops_execute(struct xe_vm *vm,
879 struct xe_vma_ops *vops);
880 static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
881 struct xe_exec_queue *q,
882 struct xe_sync_entry *syncs, u32 num_syncs);
883
xe_vm_rebind(struct xe_vm * vm,bool rebind_worker)884 int xe_vm_rebind(struct xe_vm *vm, bool rebind_worker)
885 {
886 struct dma_fence *fence;
887 struct xe_vma *vma, *next;
888 struct xe_vma_ops vops;
889 struct xe_vma_op *op, *next_op;
890 int err, i;
891
892 lockdep_assert_held(&vm->lock);
893 if ((xe_vm_in_lr_mode(vm) && !rebind_worker) ||
894 list_empty(&vm->rebind_list))
895 return 0;
896
897 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
898 for (i = 0; i < XE_MAX_TILES_PER_DEVICE; ++i)
899 vops.pt_update_ops[i].wait_vm_bookkeep = true;
900
901 xe_vm_assert_held(vm);
902 list_for_each_entry(vma, &vm->rebind_list, combined_links.rebind) {
903 xe_assert(vm->xe, vma->tile_present);
904
905 if (rebind_worker)
906 trace_xe_vma_rebind_worker(vma);
907 else
908 trace_xe_vma_rebind_exec(vma);
909
910 err = xe_vm_ops_add_rebind(&vops, vma,
911 vma->tile_present);
912 if (err)
913 goto free_ops;
914 }
915
916 err = xe_vma_ops_alloc(&vops, false);
917 if (err)
918 goto free_ops;
919
920 fence = ops_execute(vm, &vops);
921 if (IS_ERR(fence)) {
922 err = PTR_ERR(fence);
923 } else {
924 dma_fence_put(fence);
925 list_for_each_entry_safe(vma, next, &vm->rebind_list,
926 combined_links.rebind)
927 list_del_init(&vma->combined_links.rebind);
928 }
929 free_ops:
930 list_for_each_entry_safe(op, next_op, &vops.list, link) {
931 list_del(&op->link);
932 kfree(op);
933 }
934 xe_vma_ops_fini(&vops);
935
936 return err;
937 }
938
xe_vma_rebind(struct xe_vm * vm,struct xe_vma * vma,u8 tile_mask)939 struct dma_fence *xe_vma_rebind(struct xe_vm *vm, struct xe_vma *vma, u8 tile_mask)
940 {
941 struct dma_fence *fence = NULL;
942 struct xe_vma_ops vops;
943 struct xe_vma_op *op, *next_op;
944 struct xe_tile *tile;
945 u8 id;
946 int err;
947
948 lockdep_assert_held(&vm->lock);
949 xe_vm_assert_held(vm);
950 xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
951
952 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
953 for_each_tile(tile, vm->xe, id) {
954 vops.pt_update_ops[id].wait_vm_bookkeep = true;
955 vops.pt_update_ops[tile->id].q =
956 xe_tile_migrate_exec_queue(tile);
957 }
958
959 err = xe_vm_ops_add_rebind(&vops, vma, tile_mask);
960 if (err)
961 return ERR_PTR(err);
962
963 err = xe_vma_ops_alloc(&vops, false);
964 if (err) {
965 fence = ERR_PTR(err);
966 goto free_ops;
967 }
968
969 fence = ops_execute(vm, &vops);
970
971 free_ops:
972 list_for_each_entry_safe(op, next_op, &vops.list, link) {
973 list_del(&op->link);
974 kfree(op);
975 }
976 xe_vma_ops_fini(&vops);
977
978 return fence;
979 }
980
xe_vm_populate_range_rebind(struct xe_vma_op * op,struct xe_vma * vma,struct xe_svm_range * range,u8 tile_mask)981 static void xe_vm_populate_range_rebind(struct xe_vma_op *op,
982 struct xe_vma *vma,
983 struct xe_svm_range *range,
984 u8 tile_mask)
985 {
986 INIT_LIST_HEAD(&op->link);
987 op->tile_mask = tile_mask;
988 op->base.op = DRM_GPUVA_OP_DRIVER;
989 op->subop = XE_VMA_SUBOP_MAP_RANGE;
990 op->map_range.vma = vma;
991 op->map_range.range = range;
992 }
993
994 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)995 xe_vm_ops_add_range_rebind(struct xe_vma_ops *vops,
996 struct xe_vma *vma,
997 struct xe_svm_range *range,
998 u8 tile_mask)
999 {
1000 struct xe_vma_op *op;
1001
1002 op = kzalloc(sizeof(*op), GFP_KERNEL);
1003 if (!op)
1004 return -ENOMEM;
1005
1006 xe_vm_populate_range_rebind(op, vma, range, tile_mask);
1007 list_add_tail(&op->link, &vops->list);
1008 xe_vma_ops_incr_pt_update_ops(vops, tile_mask, 1);
1009
1010 return 0;
1011 }
1012
1013 /**
1014 * xe_vm_range_rebind() - VM range (re)bind
1015 * @vm: The VM which the range belongs to.
1016 * @vma: The VMA which the range belongs to.
1017 * @range: SVM range to rebind.
1018 * @tile_mask: Tile mask to bind the range to.
1019 *
1020 * (re)bind SVM range setting up GPU page tables for the range.
1021 *
1022 * Return: dma fence for rebind to signal completion on succees, ERR_PTR on
1023 * failure
1024 */
xe_vm_range_rebind(struct xe_vm * vm,struct xe_vma * vma,struct xe_svm_range * range,u8 tile_mask)1025 struct dma_fence *xe_vm_range_rebind(struct xe_vm *vm,
1026 struct xe_vma *vma,
1027 struct xe_svm_range *range,
1028 u8 tile_mask)
1029 {
1030 struct dma_fence *fence = NULL;
1031 struct xe_vma_ops vops;
1032 struct xe_vma_op *op, *next_op;
1033 struct xe_tile *tile;
1034 u8 id;
1035 int err;
1036
1037 lockdep_assert_held(&vm->lock);
1038 xe_vm_assert_held(vm);
1039 xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
1040 xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(vma));
1041
1042 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
1043 for_each_tile(tile, vm->xe, id) {
1044 vops.pt_update_ops[id].wait_vm_bookkeep = true;
1045 vops.pt_update_ops[tile->id].q =
1046 xe_tile_migrate_exec_queue(tile);
1047 }
1048
1049 err = xe_vm_ops_add_range_rebind(&vops, vma, range, tile_mask);
1050 if (err)
1051 return ERR_PTR(err);
1052
1053 err = xe_vma_ops_alloc(&vops, false);
1054 if (err) {
1055 fence = ERR_PTR(err);
1056 goto free_ops;
1057 }
1058
1059 fence = ops_execute(vm, &vops);
1060
1061 free_ops:
1062 list_for_each_entry_safe(op, next_op, &vops.list, link) {
1063 list_del(&op->link);
1064 kfree(op);
1065 }
1066 xe_vma_ops_fini(&vops);
1067
1068 return fence;
1069 }
1070
xe_vm_populate_range_unbind(struct xe_vma_op * op,struct xe_svm_range * range)1071 static void xe_vm_populate_range_unbind(struct xe_vma_op *op,
1072 struct xe_svm_range *range)
1073 {
1074 INIT_LIST_HEAD(&op->link);
1075 op->tile_mask = range->tile_present;
1076 op->base.op = DRM_GPUVA_OP_DRIVER;
1077 op->subop = XE_VMA_SUBOP_UNMAP_RANGE;
1078 op->unmap_range.range = range;
1079 }
1080
1081 static int
xe_vm_ops_add_range_unbind(struct xe_vma_ops * vops,struct xe_svm_range * range)1082 xe_vm_ops_add_range_unbind(struct xe_vma_ops *vops,
1083 struct xe_svm_range *range)
1084 {
1085 struct xe_vma_op *op;
1086
1087 op = kzalloc(sizeof(*op), GFP_KERNEL);
1088 if (!op)
1089 return -ENOMEM;
1090
1091 xe_vm_populate_range_unbind(op, range);
1092 list_add_tail(&op->link, &vops->list);
1093 xe_vma_ops_incr_pt_update_ops(vops, range->tile_present, 1);
1094
1095 return 0;
1096 }
1097
1098 /**
1099 * xe_vm_range_unbind() - VM range unbind
1100 * @vm: The VM which the range belongs to.
1101 * @range: SVM range to rebind.
1102 *
1103 * Unbind SVM range removing the GPU page tables for the range.
1104 *
1105 * Return: dma fence for unbind to signal completion on succees, ERR_PTR on
1106 * failure
1107 */
xe_vm_range_unbind(struct xe_vm * vm,struct xe_svm_range * range)1108 struct dma_fence *xe_vm_range_unbind(struct xe_vm *vm,
1109 struct xe_svm_range *range)
1110 {
1111 struct dma_fence *fence = NULL;
1112 struct xe_vma_ops vops;
1113 struct xe_vma_op *op, *next_op;
1114 struct xe_tile *tile;
1115 u8 id;
1116 int err;
1117
1118 lockdep_assert_held(&vm->lock);
1119 xe_vm_assert_held(vm);
1120 xe_assert(vm->xe, xe_vm_in_fault_mode(vm));
1121
1122 if (!range->tile_present)
1123 return dma_fence_get_stub();
1124
1125 xe_vma_ops_init(&vops, vm, NULL, NULL, 0);
1126 for_each_tile(tile, vm->xe, id) {
1127 vops.pt_update_ops[id].wait_vm_bookkeep = true;
1128 vops.pt_update_ops[tile->id].q =
1129 xe_tile_migrate_exec_queue(tile);
1130 }
1131
1132 err = xe_vm_ops_add_range_unbind(&vops, range);
1133 if (err)
1134 return ERR_PTR(err);
1135
1136 err = xe_vma_ops_alloc(&vops, false);
1137 if (err) {
1138 fence = ERR_PTR(err);
1139 goto free_ops;
1140 }
1141
1142 fence = ops_execute(vm, &vops);
1143
1144 free_ops:
1145 list_for_each_entry_safe(op, next_op, &vops.list, link) {
1146 list_del(&op->link);
1147 kfree(op);
1148 }
1149 xe_vma_ops_fini(&vops);
1150
1151 return fence;
1152 }
1153
xe_vma_free(struct xe_vma * vma)1154 static void xe_vma_free(struct xe_vma *vma)
1155 {
1156 if (xe_vma_is_userptr(vma))
1157 kfree(to_userptr_vma(vma));
1158 else
1159 kfree(vma);
1160 }
1161
1162 #define VMA_CREATE_FLAG_READ_ONLY BIT(0)
1163 #define VMA_CREATE_FLAG_IS_NULL BIT(1)
1164 #define VMA_CREATE_FLAG_DUMPABLE BIT(2)
1165 #define VMA_CREATE_FLAG_IS_SYSTEM_ALLOCATOR BIT(3)
1166
xe_vma_create(struct xe_vm * vm,struct xe_bo * bo,u64 bo_offset_or_userptr,u64 start,u64 end,u16 pat_index,unsigned int flags)1167 static struct xe_vma *xe_vma_create(struct xe_vm *vm,
1168 struct xe_bo *bo,
1169 u64 bo_offset_or_userptr,
1170 u64 start, u64 end,
1171 u16 pat_index, unsigned int flags)
1172 {
1173 struct xe_vma *vma;
1174 struct xe_tile *tile;
1175 u8 id;
1176 bool read_only = (flags & VMA_CREATE_FLAG_READ_ONLY);
1177 bool is_null = (flags & VMA_CREATE_FLAG_IS_NULL);
1178 bool dumpable = (flags & VMA_CREATE_FLAG_DUMPABLE);
1179 bool is_cpu_addr_mirror =
1180 (flags & VMA_CREATE_FLAG_IS_SYSTEM_ALLOCATOR);
1181
1182 xe_assert(vm->xe, start < end);
1183 xe_assert(vm->xe, end < vm->size);
1184
1185 /*
1186 * Allocate and ensure that the xe_vma_is_userptr() return
1187 * matches what was allocated.
1188 */
1189 if (!bo && !is_null && !is_cpu_addr_mirror) {
1190 struct xe_userptr_vma *uvma = kzalloc(sizeof(*uvma), GFP_KERNEL);
1191
1192 if (!uvma)
1193 return ERR_PTR(-ENOMEM);
1194
1195 vma = &uvma->vma;
1196 } else {
1197 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
1198 if (!vma)
1199 return ERR_PTR(-ENOMEM);
1200
1201 if (is_cpu_addr_mirror)
1202 vma->gpuva.flags |= XE_VMA_SYSTEM_ALLOCATOR;
1203 if (is_null)
1204 vma->gpuva.flags |= DRM_GPUVA_SPARSE;
1205 if (bo)
1206 vma->gpuva.gem.obj = &bo->ttm.base;
1207 }
1208
1209 INIT_LIST_HEAD(&vma->combined_links.rebind);
1210
1211 INIT_LIST_HEAD(&vma->gpuva.gem.entry);
1212 vma->gpuva.vm = &vm->gpuvm;
1213 vma->gpuva.va.addr = start;
1214 vma->gpuva.va.range = end - start + 1;
1215 if (read_only)
1216 vma->gpuva.flags |= XE_VMA_READ_ONLY;
1217 if (dumpable)
1218 vma->gpuva.flags |= XE_VMA_DUMPABLE;
1219
1220 for_each_tile(tile, vm->xe, id)
1221 vma->tile_mask |= 0x1 << id;
1222
1223 if (vm->xe->info.has_atomic_enable_pte_bit)
1224 vma->gpuva.flags |= XE_VMA_ATOMIC_PTE_BIT;
1225
1226 vma->pat_index = pat_index;
1227
1228 if (bo) {
1229 struct drm_gpuvm_bo *vm_bo;
1230
1231 xe_bo_assert_held(bo);
1232
1233 vm_bo = drm_gpuvm_bo_obtain(vma->gpuva.vm, &bo->ttm.base);
1234 if (IS_ERR(vm_bo)) {
1235 xe_vma_free(vma);
1236 return ERR_CAST(vm_bo);
1237 }
1238
1239 drm_gpuvm_bo_extobj_add(vm_bo);
1240 drm_gem_object_get(&bo->ttm.base);
1241 vma->gpuva.gem.offset = bo_offset_or_userptr;
1242 drm_gpuva_link(&vma->gpuva, vm_bo);
1243 drm_gpuvm_bo_put(vm_bo);
1244 } else /* userptr or null */ {
1245 if (!is_null && !is_cpu_addr_mirror) {
1246 struct xe_userptr *userptr = &to_userptr_vma(vma)->userptr;
1247 u64 size = end - start + 1;
1248 int err;
1249
1250 INIT_LIST_HEAD(&userptr->invalidate_link);
1251 INIT_LIST_HEAD(&userptr->repin_link);
1252 vma->gpuva.gem.offset = bo_offset_or_userptr;
1253 mutex_init(&userptr->unmap_mutex);
1254
1255 err = mmu_interval_notifier_insert(&userptr->notifier,
1256 current->mm,
1257 xe_vma_userptr(vma), size,
1258 &vma_userptr_notifier_ops);
1259 if (err) {
1260 xe_vma_free(vma);
1261 return ERR_PTR(err);
1262 }
1263
1264 userptr->notifier_seq = LONG_MAX;
1265 }
1266
1267 xe_vm_get(vm);
1268 }
1269
1270 return vma;
1271 }
1272
xe_vma_destroy_late(struct xe_vma * vma)1273 static void xe_vma_destroy_late(struct xe_vma *vma)
1274 {
1275 struct xe_vm *vm = xe_vma_vm(vma);
1276
1277 if (vma->ufence) {
1278 xe_sync_ufence_put(vma->ufence);
1279 vma->ufence = NULL;
1280 }
1281
1282 if (xe_vma_is_userptr(vma)) {
1283 struct xe_userptr_vma *uvma = to_userptr_vma(vma);
1284 struct xe_userptr *userptr = &uvma->userptr;
1285
1286 if (userptr->sg)
1287 xe_hmm_userptr_free_sg(uvma);
1288
1289 /*
1290 * Since userptr pages are not pinned, we can't remove
1291 * the notifier until we're sure the GPU is not accessing
1292 * them anymore
1293 */
1294 mmu_interval_notifier_remove(&userptr->notifier);
1295 mutex_destroy(&userptr->unmap_mutex);
1296 xe_vm_put(vm);
1297 } else if (xe_vma_is_null(vma) || xe_vma_is_cpu_addr_mirror(vma)) {
1298 xe_vm_put(vm);
1299 } else {
1300 xe_bo_put(xe_vma_bo(vma));
1301 }
1302
1303 xe_vma_free(vma);
1304 }
1305
vma_destroy_work_func(struct work_struct * w)1306 static void vma_destroy_work_func(struct work_struct *w)
1307 {
1308 struct xe_vma *vma =
1309 container_of(w, struct xe_vma, destroy_work);
1310
1311 xe_vma_destroy_late(vma);
1312 }
1313
vma_destroy_cb(struct dma_fence * fence,struct dma_fence_cb * cb)1314 static void vma_destroy_cb(struct dma_fence *fence,
1315 struct dma_fence_cb *cb)
1316 {
1317 struct xe_vma *vma = container_of(cb, struct xe_vma, destroy_cb);
1318
1319 INIT_WORK(&vma->destroy_work, vma_destroy_work_func);
1320 queue_work(system_unbound_wq, &vma->destroy_work);
1321 }
1322
xe_vma_destroy(struct xe_vma * vma,struct dma_fence * fence)1323 static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence)
1324 {
1325 struct xe_vm *vm = xe_vma_vm(vma);
1326
1327 lockdep_assert_held_write(&vm->lock);
1328 xe_assert(vm->xe, list_empty(&vma->combined_links.destroy));
1329
1330 if (xe_vma_is_userptr(vma)) {
1331 xe_assert(vm->xe, vma->gpuva.flags & XE_VMA_DESTROYED);
1332
1333 spin_lock(&vm->userptr.invalidated_lock);
1334 xe_assert(vm->xe, list_empty(&to_userptr_vma(vma)->userptr.repin_link));
1335 list_del(&to_userptr_vma(vma)->userptr.invalidate_link);
1336 spin_unlock(&vm->userptr.invalidated_lock);
1337 } else if (!xe_vma_is_null(vma) && !xe_vma_is_cpu_addr_mirror(vma)) {
1338 xe_bo_assert_held(xe_vma_bo(vma));
1339
1340 drm_gpuva_unlink(&vma->gpuva);
1341 }
1342
1343 xe_vm_assert_held(vm);
1344 if (fence) {
1345 int ret = dma_fence_add_callback(fence, &vma->destroy_cb,
1346 vma_destroy_cb);
1347
1348 if (ret) {
1349 XE_WARN_ON(ret != -ENOENT);
1350 xe_vma_destroy_late(vma);
1351 }
1352 } else {
1353 xe_vma_destroy_late(vma);
1354 }
1355 }
1356
1357 /**
1358 * xe_vm_lock_vma() - drm_exec utility to lock a vma
1359 * @exec: The drm_exec object we're currently locking for.
1360 * @vma: The vma for witch we want to lock the vm resv and any attached
1361 * object's resv.
1362 *
1363 * Return: 0 on success, negative error code on error. In particular
1364 * may return -EDEADLK on WW transaction contention and -EINTR if
1365 * an interruptible wait is terminated by a signal.
1366 */
xe_vm_lock_vma(struct drm_exec * exec,struct xe_vma * vma)1367 int xe_vm_lock_vma(struct drm_exec *exec, struct xe_vma *vma)
1368 {
1369 struct xe_vm *vm = xe_vma_vm(vma);
1370 struct xe_bo *bo = xe_vma_bo(vma);
1371 int err;
1372
1373 XE_WARN_ON(!vm);
1374
1375 err = drm_exec_lock_obj(exec, xe_vm_obj(vm));
1376 if (!err && bo && !bo->vm)
1377 err = drm_exec_lock_obj(exec, &bo->ttm.base);
1378
1379 return err;
1380 }
1381
xe_vma_destroy_unlocked(struct xe_vma * vma)1382 static void xe_vma_destroy_unlocked(struct xe_vma *vma)
1383 {
1384 struct drm_exec exec;
1385 int err;
1386
1387 drm_exec_init(&exec, 0, 0);
1388 drm_exec_until_all_locked(&exec) {
1389 err = xe_vm_lock_vma(&exec, vma);
1390 drm_exec_retry_on_contention(&exec);
1391 if (XE_WARN_ON(err))
1392 break;
1393 }
1394
1395 xe_vma_destroy(vma, NULL);
1396
1397 drm_exec_fini(&exec);
1398 }
1399
1400 struct xe_vma *
xe_vm_find_overlapping_vma(struct xe_vm * vm,u64 start,u64 range)1401 xe_vm_find_overlapping_vma(struct xe_vm *vm, u64 start, u64 range)
1402 {
1403 struct drm_gpuva *gpuva;
1404
1405 lockdep_assert_held(&vm->lock);
1406
1407 if (xe_vm_is_closed_or_banned(vm))
1408 return NULL;
1409
1410 xe_assert(vm->xe, start + range <= vm->size);
1411
1412 gpuva = drm_gpuva_find_first(&vm->gpuvm, start, range);
1413
1414 return gpuva ? gpuva_to_vma(gpuva) : NULL;
1415 }
1416
xe_vm_insert_vma(struct xe_vm * vm,struct xe_vma * vma)1417 static int xe_vm_insert_vma(struct xe_vm *vm, struct xe_vma *vma)
1418 {
1419 int err;
1420
1421 xe_assert(vm->xe, xe_vma_vm(vma) == vm);
1422 lockdep_assert_held(&vm->lock);
1423
1424 mutex_lock(&vm->snap_mutex);
1425 err = drm_gpuva_insert(&vm->gpuvm, &vma->gpuva);
1426 mutex_unlock(&vm->snap_mutex);
1427 XE_WARN_ON(err); /* Shouldn't be possible */
1428
1429 return err;
1430 }
1431
xe_vm_remove_vma(struct xe_vm * vm,struct xe_vma * vma)1432 static void xe_vm_remove_vma(struct xe_vm *vm, struct xe_vma *vma)
1433 {
1434 xe_assert(vm->xe, xe_vma_vm(vma) == vm);
1435 lockdep_assert_held(&vm->lock);
1436
1437 mutex_lock(&vm->snap_mutex);
1438 drm_gpuva_remove(&vma->gpuva);
1439 mutex_unlock(&vm->snap_mutex);
1440 if (vm->usm.last_fault_vma == vma)
1441 vm->usm.last_fault_vma = NULL;
1442 }
1443
xe_vm_op_alloc(void)1444 static struct drm_gpuva_op *xe_vm_op_alloc(void)
1445 {
1446 struct xe_vma_op *op;
1447
1448 op = kzalloc(sizeof(*op), GFP_KERNEL);
1449
1450 if (unlikely(!op))
1451 return NULL;
1452
1453 return &op->base;
1454 }
1455
1456 static void xe_vm_free(struct drm_gpuvm *gpuvm);
1457
1458 static const struct drm_gpuvm_ops gpuvm_ops = {
1459 .op_alloc = xe_vm_op_alloc,
1460 .vm_bo_validate = xe_gpuvm_validate,
1461 .vm_free = xe_vm_free,
1462 };
1463
pde_encode_pat_index(u16 pat_index)1464 static u64 pde_encode_pat_index(u16 pat_index)
1465 {
1466 u64 pte = 0;
1467
1468 if (pat_index & BIT(0))
1469 pte |= XE_PPGTT_PTE_PAT0;
1470
1471 if (pat_index & BIT(1))
1472 pte |= XE_PPGTT_PTE_PAT1;
1473
1474 return pte;
1475 }
1476
pte_encode_pat_index(u16 pat_index,u32 pt_level)1477 static u64 pte_encode_pat_index(u16 pat_index, u32 pt_level)
1478 {
1479 u64 pte = 0;
1480
1481 if (pat_index & BIT(0))
1482 pte |= XE_PPGTT_PTE_PAT0;
1483
1484 if (pat_index & BIT(1))
1485 pte |= XE_PPGTT_PTE_PAT1;
1486
1487 if (pat_index & BIT(2)) {
1488 if (pt_level)
1489 pte |= XE_PPGTT_PDE_PDPE_PAT2;
1490 else
1491 pte |= XE_PPGTT_PTE_PAT2;
1492 }
1493
1494 if (pat_index & BIT(3))
1495 pte |= XELPG_PPGTT_PTE_PAT3;
1496
1497 if (pat_index & (BIT(4)))
1498 pte |= XE2_PPGTT_PTE_PAT4;
1499
1500 return pte;
1501 }
1502
pte_encode_ps(u32 pt_level)1503 static u64 pte_encode_ps(u32 pt_level)
1504 {
1505 XE_WARN_ON(pt_level > MAX_HUGEPTE_LEVEL);
1506
1507 if (pt_level == 1)
1508 return XE_PDE_PS_2M;
1509 else if (pt_level == 2)
1510 return XE_PDPE_PS_1G;
1511
1512 return 0;
1513 }
1514
xelp_pde_encode_bo(struct xe_bo * bo,u64 bo_offset,const u16 pat_index)1515 static u64 xelp_pde_encode_bo(struct xe_bo *bo, u64 bo_offset,
1516 const u16 pat_index)
1517 {
1518 u64 pde;
1519
1520 pde = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
1521 pde |= XE_PAGE_PRESENT | XE_PAGE_RW;
1522 pde |= pde_encode_pat_index(pat_index);
1523
1524 return pde;
1525 }
1526
xelp_pte_encode_bo(struct xe_bo * bo,u64 bo_offset,u16 pat_index,u32 pt_level)1527 static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
1528 u16 pat_index, u32 pt_level)
1529 {
1530 u64 pte;
1531
1532 pte = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
1533 pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
1534 pte |= pte_encode_pat_index(pat_index, pt_level);
1535 pte |= pte_encode_ps(pt_level);
1536
1537 if (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo))
1538 pte |= XE_PPGTT_PTE_DM;
1539
1540 return pte;
1541 }
1542
xelp_pte_encode_vma(u64 pte,struct xe_vma * vma,u16 pat_index,u32 pt_level)1543 static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
1544 u16 pat_index, u32 pt_level)
1545 {
1546 pte |= XE_PAGE_PRESENT;
1547
1548 if (likely(!xe_vma_read_only(vma)))
1549 pte |= XE_PAGE_RW;
1550
1551 pte |= pte_encode_pat_index(pat_index, pt_level);
1552 pte |= pte_encode_ps(pt_level);
1553
1554 if (unlikely(xe_vma_is_null(vma)))
1555 pte |= XE_PTE_NULL;
1556
1557 return pte;
1558 }
1559
xelp_pte_encode_addr(struct xe_device * xe,u64 addr,u16 pat_index,u32 pt_level,bool devmem,u64 flags)1560 static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
1561 u16 pat_index,
1562 u32 pt_level, bool devmem, u64 flags)
1563 {
1564 u64 pte;
1565
1566 /* Avoid passing random bits directly as flags */
1567 xe_assert(xe, !(flags & ~XE_PTE_PS64));
1568
1569 pte = addr;
1570 pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
1571 pte |= pte_encode_pat_index(pat_index, pt_level);
1572 pte |= pte_encode_ps(pt_level);
1573
1574 if (devmem)
1575 pte |= XE_PPGTT_PTE_DM;
1576
1577 pte |= flags;
1578
1579 return pte;
1580 }
1581
1582 static const struct xe_pt_ops xelp_pt_ops = {
1583 .pte_encode_bo = xelp_pte_encode_bo,
1584 .pte_encode_vma = xelp_pte_encode_vma,
1585 .pte_encode_addr = xelp_pte_encode_addr,
1586 .pde_encode_bo = xelp_pde_encode_bo,
1587 };
1588
1589 static void vm_destroy_work_func(struct work_struct *w);
1590
1591 /**
1592 * xe_vm_create_scratch() - Setup a scratch memory pagetable tree for the
1593 * given tile and vm.
1594 * @xe: xe device.
1595 * @tile: tile to set up for.
1596 * @vm: vm to set up for.
1597 *
1598 * Sets up a pagetable tree with one page-table per level and a single
1599 * leaf PTE. All pagetable entries point to the single page-table or,
1600 * for MAX_HUGEPTE_LEVEL, a NULL huge PTE returning 0 on read and
1601 * writes become NOPs.
1602 *
1603 * Return: 0 on success, negative error code on error.
1604 */
xe_vm_create_scratch(struct xe_device * xe,struct xe_tile * tile,struct xe_vm * vm)1605 static int xe_vm_create_scratch(struct xe_device *xe, struct xe_tile *tile,
1606 struct xe_vm *vm)
1607 {
1608 u8 id = tile->id;
1609 int i;
1610
1611 for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; i++) {
1612 vm->scratch_pt[id][i] = xe_pt_create(vm, tile, i);
1613 if (IS_ERR(vm->scratch_pt[id][i]))
1614 return PTR_ERR(vm->scratch_pt[id][i]);
1615
1616 xe_pt_populate_empty(tile, vm, vm->scratch_pt[id][i]);
1617 }
1618
1619 return 0;
1620 }
1621 ALLOW_ERROR_INJECTION(xe_vm_create_scratch, ERRNO);
1622
xe_vm_free_scratch(struct xe_vm * vm)1623 static void xe_vm_free_scratch(struct xe_vm *vm)
1624 {
1625 struct xe_tile *tile;
1626 u8 id;
1627
1628 if (!xe_vm_has_scratch(vm))
1629 return;
1630
1631 for_each_tile(tile, vm->xe, id) {
1632 u32 i;
1633
1634 if (!vm->pt_root[id])
1635 continue;
1636
1637 for (i = MAX_HUGEPTE_LEVEL; i < vm->pt_root[id]->level; ++i)
1638 if (vm->scratch_pt[id][i])
1639 xe_pt_destroy(vm->scratch_pt[id][i], vm->flags, NULL);
1640 }
1641 }
1642
xe_vm_create(struct xe_device * xe,u32 flags)1643 struct xe_vm *xe_vm_create(struct xe_device *xe, u32 flags)
1644 {
1645 struct drm_gem_object *vm_resv_obj;
1646 struct xe_vm *vm;
1647 int err, number_tiles = 0;
1648 struct xe_tile *tile;
1649 u8 id;
1650
1651 /*
1652 * Since the GSCCS is not user-accessible, we don't expect a GSC VM to
1653 * ever be in faulting mode.
1654 */
1655 xe_assert(xe, !((flags & XE_VM_FLAG_GSC) && (flags & XE_VM_FLAG_FAULT_MODE)));
1656
1657 vm = kzalloc(sizeof(*vm), GFP_KERNEL);
1658 if (!vm)
1659 return ERR_PTR(-ENOMEM);
1660
1661 vm->xe = xe;
1662
1663 vm->size = 1ull << xe->info.va_bits;
1664
1665 vm->flags = flags;
1666
1667 /**
1668 * GSC VMs are kernel-owned, only used for PXP ops and can sometimes be
1669 * manipulated under the PXP mutex. However, the PXP mutex can be taken
1670 * under a user-VM lock when the PXP session is started at exec_queue
1671 * creation time. Those are different VMs and therefore there is no risk
1672 * of deadlock, but we need to tell lockdep that this is the case or it
1673 * will print a warning.
1674 */
1675 if (flags & XE_VM_FLAG_GSC) {
1676 static struct lock_class_key gsc_vm_key;
1677
1678 __init_rwsem(&vm->lock, "gsc_vm", &gsc_vm_key);
1679 } else {
1680 init_rwsem(&vm->lock);
1681 }
1682 mutex_init(&vm->snap_mutex);
1683
1684 INIT_LIST_HEAD(&vm->rebind_list);
1685
1686 INIT_LIST_HEAD(&vm->userptr.repin_list);
1687 INIT_LIST_HEAD(&vm->userptr.invalidated);
1688 init_rwsem(&vm->userptr.notifier_lock);
1689 spin_lock_init(&vm->userptr.invalidated_lock);
1690
1691 ttm_lru_bulk_move_init(&vm->lru_bulk_move);
1692
1693 INIT_WORK(&vm->destroy_work, vm_destroy_work_func);
1694
1695 INIT_LIST_HEAD(&vm->preempt.exec_queues);
1696 vm->preempt.min_run_period_ms = 10; /* FIXME: Wire up to uAPI */
1697
1698 for_each_tile(tile, xe, id)
1699 xe_range_fence_tree_init(&vm->rftree[id]);
1700
1701 vm->pt_ops = &xelp_pt_ops;
1702
1703 /*
1704 * Long-running workloads are not protected by the scheduler references.
1705 * By design, run_job for long-running workloads returns NULL and the
1706 * scheduler drops all the references of it, hence protecting the VM
1707 * for this case is necessary.
1708 */
1709 if (flags & XE_VM_FLAG_LR_MODE) {
1710 INIT_WORK(&vm->preempt.rebind_work, preempt_rebind_work_func);
1711 xe_pm_runtime_get_noresume(xe);
1712 }
1713
1714 if (flags & XE_VM_FLAG_FAULT_MODE) {
1715 err = xe_svm_init(vm);
1716 if (err)
1717 goto err_no_resv;
1718 }
1719
1720 vm_resv_obj = drm_gpuvm_resv_object_alloc(&xe->drm);
1721 if (!vm_resv_obj) {
1722 err = -ENOMEM;
1723 goto err_svm_fini;
1724 }
1725
1726 drm_gpuvm_init(&vm->gpuvm, "Xe VM", DRM_GPUVM_RESV_PROTECTED, &xe->drm,
1727 vm_resv_obj, 0, vm->size, 0, 0, &gpuvm_ops);
1728
1729 drm_gem_object_put(vm_resv_obj);
1730
1731 err = xe_vm_lock(vm, true);
1732 if (err)
1733 goto err_close;
1734
1735 if (IS_DGFX(xe) && xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)
1736 vm->flags |= XE_VM_FLAG_64K;
1737
1738 for_each_tile(tile, xe, id) {
1739 if (flags & XE_VM_FLAG_MIGRATION &&
1740 tile->id != XE_VM_FLAG_TILE_ID(flags))
1741 continue;
1742
1743 vm->pt_root[id] = xe_pt_create(vm, tile, xe->info.vm_max_level);
1744 if (IS_ERR(vm->pt_root[id])) {
1745 err = PTR_ERR(vm->pt_root[id]);
1746 vm->pt_root[id] = NULL;
1747 goto err_unlock_close;
1748 }
1749 }
1750
1751 if (xe_vm_has_scratch(vm)) {
1752 for_each_tile(tile, xe, id) {
1753 if (!vm->pt_root[id])
1754 continue;
1755
1756 err = xe_vm_create_scratch(xe, tile, vm);
1757 if (err)
1758 goto err_unlock_close;
1759 }
1760 vm->batch_invalidate_tlb = true;
1761 }
1762
1763 if (vm->flags & XE_VM_FLAG_LR_MODE)
1764 vm->batch_invalidate_tlb = false;
1765
1766 /* Fill pt_root after allocating scratch tables */
1767 for_each_tile(tile, xe, id) {
1768 if (!vm->pt_root[id])
1769 continue;
1770
1771 xe_pt_populate_empty(tile, vm, vm->pt_root[id]);
1772 }
1773 xe_vm_unlock(vm);
1774
1775 /* Kernel migration VM shouldn't have a circular loop.. */
1776 if (!(flags & XE_VM_FLAG_MIGRATION)) {
1777 for_each_tile(tile, xe, id) {
1778 struct xe_exec_queue *q;
1779 u32 create_flags = EXEC_QUEUE_FLAG_VM;
1780
1781 if (!vm->pt_root[id])
1782 continue;
1783
1784 q = xe_exec_queue_create_bind(xe, tile, create_flags, 0);
1785 if (IS_ERR(q)) {
1786 err = PTR_ERR(q);
1787 goto err_close;
1788 }
1789 vm->q[id] = q;
1790 number_tiles++;
1791 }
1792 }
1793
1794 if (number_tiles > 1)
1795 vm->composite_fence_ctx = dma_fence_context_alloc(1);
1796
1797 trace_xe_vm_create(vm);
1798
1799 return vm;
1800
1801 err_unlock_close:
1802 xe_vm_unlock(vm);
1803 err_close:
1804 xe_vm_close_and_put(vm);
1805 return ERR_PTR(err);
1806
1807 err_svm_fini:
1808 if (flags & XE_VM_FLAG_FAULT_MODE) {
1809 vm->size = 0; /* close the vm */
1810 xe_svm_fini(vm);
1811 }
1812 err_no_resv:
1813 mutex_destroy(&vm->snap_mutex);
1814 for_each_tile(tile, xe, id)
1815 xe_range_fence_tree_fini(&vm->rftree[id]);
1816 ttm_lru_bulk_move_fini(&xe->ttm, &vm->lru_bulk_move);
1817 kfree(vm);
1818 if (flags & XE_VM_FLAG_LR_MODE)
1819 xe_pm_runtime_put(xe);
1820 return ERR_PTR(err);
1821 }
1822
xe_vm_close(struct xe_vm * vm)1823 static void xe_vm_close(struct xe_vm *vm)
1824 {
1825 struct xe_device *xe = vm->xe;
1826 bool bound;
1827 int idx;
1828
1829 bound = drm_dev_enter(&xe->drm, &idx);
1830
1831 down_write(&vm->lock);
1832 if (xe_vm_in_fault_mode(vm))
1833 xe_svm_notifier_lock(vm);
1834
1835 vm->size = 0;
1836
1837 if (!((vm->flags & XE_VM_FLAG_MIGRATION))) {
1838 struct xe_tile *tile;
1839 struct xe_gt *gt;
1840 u8 id;
1841
1842 /* Wait for pending binds */
1843 dma_resv_wait_timeout(xe_vm_resv(vm),
1844 DMA_RESV_USAGE_BOOKKEEP,
1845 false, MAX_SCHEDULE_TIMEOUT);
1846
1847 if (bound) {
1848 for_each_tile(tile, xe, id)
1849 if (vm->pt_root[id])
1850 xe_pt_clear(xe, vm->pt_root[id]);
1851
1852 for_each_gt(gt, xe, id)
1853 xe_gt_tlb_invalidation_vm(gt, vm);
1854 }
1855 }
1856
1857 if (xe_vm_in_fault_mode(vm))
1858 xe_svm_notifier_unlock(vm);
1859 up_write(&vm->lock);
1860
1861 if (bound)
1862 drm_dev_exit(idx);
1863 }
1864
xe_vm_close_and_put(struct xe_vm * vm)1865 void xe_vm_close_and_put(struct xe_vm *vm)
1866 {
1867 LIST_HEAD(contested);
1868 struct xe_device *xe = vm->xe;
1869 struct xe_tile *tile;
1870 struct xe_vma *vma, *next_vma;
1871 struct drm_gpuva *gpuva, *next;
1872 u8 id;
1873
1874 xe_assert(xe, !vm->preempt.num_exec_queues);
1875
1876 xe_vm_close(vm);
1877 if (xe_vm_in_preempt_fence_mode(vm))
1878 flush_work(&vm->preempt.rebind_work);
1879 if (xe_vm_in_fault_mode(vm))
1880 xe_svm_close(vm);
1881
1882 down_write(&vm->lock);
1883 for_each_tile(tile, xe, id) {
1884 if (vm->q[id])
1885 xe_exec_queue_last_fence_put(vm->q[id], vm);
1886 }
1887 up_write(&vm->lock);
1888
1889 for_each_tile(tile, xe, id) {
1890 if (vm->q[id]) {
1891 xe_exec_queue_kill(vm->q[id]);
1892 xe_exec_queue_put(vm->q[id]);
1893 vm->q[id] = NULL;
1894 }
1895 }
1896
1897 down_write(&vm->lock);
1898 xe_vm_lock(vm, false);
1899 drm_gpuvm_for_each_va_safe(gpuva, next, &vm->gpuvm) {
1900 vma = gpuva_to_vma(gpuva);
1901
1902 if (xe_vma_has_no_bo(vma)) {
1903 down_read(&vm->userptr.notifier_lock);
1904 vma->gpuva.flags |= XE_VMA_DESTROYED;
1905 up_read(&vm->userptr.notifier_lock);
1906 }
1907
1908 xe_vm_remove_vma(vm, vma);
1909
1910 /* easy case, remove from VMA? */
1911 if (xe_vma_has_no_bo(vma) || xe_vma_bo(vma)->vm) {
1912 list_del_init(&vma->combined_links.rebind);
1913 xe_vma_destroy(vma, NULL);
1914 continue;
1915 }
1916
1917 list_move_tail(&vma->combined_links.destroy, &contested);
1918 vma->gpuva.flags |= XE_VMA_DESTROYED;
1919 }
1920
1921 /*
1922 * All vm operations will add shared fences to resv.
1923 * The only exception is eviction for a shared object,
1924 * but even so, the unbind when evicted would still
1925 * install a fence to resv. Hence it's safe to
1926 * destroy the pagetables immediately.
1927 */
1928 xe_vm_free_scratch(vm);
1929
1930 for_each_tile(tile, xe, id) {
1931 if (vm->pt_root[id]) {
1932 xe_pt_destroy(vm->pt_root[id], vm->flags, NULL);
1933 vm->pt_root[id] = NULL;
1934 }
1935 }
1936 xe_vm_unlock(vm);
1937
1938 /*
1939 * VM is now dead, cannot re-add nodes to vm->vmas if it's NULL
1940 * Since we hold a refcount to the bo, we can remove and free
1941 * the members safely without locking.
1942 */
1943 list_for_each_entry_safe(vma, next_vma, &contested,
1944 combined_links.destroy) {
1945 list_del_init(&vma->combined_links.destroy);
1946 xe_vma_destroy_unlocked(vma);
1947 }
1948
1949 if (xe_vm_in_fault_mode(vm))
1950 xe_svm_fini(vm);
1951
1952 up_write(&vm->lock);
1953
1954 down_write(&xe->usm.lock);
1955 if (vm->usm.asid) {
1956 void *lookup;
1957
1958 xe_assert(xe, xe->info.has_asid);
1959 xe_assert(xe, !(vm->flags & XE_VM_FLAG_MIGRATION));
1960
1961 lookup = xa_erase(&xe->usm.asid_to_vm, vm->usm.asid);
1962 xe_assert(xe, lookup == vm);
1963 }
1964 up_write(&xe->usm.lock);
1965
1966 for_each_tile(tile, xe, id)
1967 xe_range_fence_tree_fini(&vm->rftree[id]);
1968
1969 xe_vm_put(vm);
1970 }
1971
vm_destroy_work_func(struct work_struct * w)1972 static void vm_destroy_work_func(struct work_struct *w)
1973 {
1974 struct xe_vm *vm =
1975 container_of(w, struct xe_vm, destroy_work);
1976 struct xe_device *xe = vm->xe;
1977 struct xe_tile *tile;
1978 u8 id;
1979
1980 /* xe_vm_close_and_put was not called? */
1981 xe_assert(xe, !vm->size);
1982
1983 if (xe_vm_in_preempt_fence_mode(vm))
1984 flush_work(&vm->preempt.rebind_work);
1985
1986 mutex_destroy(&vm->snap_mutex);
1987
1988 if (vm->flags & XE_VM_FLAG_LR_MODE)
1989 xe_pm_runtime_put(xe);
1990
1991 for_each_tile(tile, xe, id)
1992 XE_WARN_ON(vm->pt_root[id]);
1993
1994 trace_xe_vm_free(vm);
1995
1996 ttm_lru_bulk_move_fini(&xe->ttm, &vm->lru_bulk_move);
1997
1998 if (vm->xef)
1999 xe_file_put(vm->xef);
2000
2001 kfree(vm);
2002 }
2003
xe_vm_free(struct drm_gpuvm * gpuvm)2004 static void xe_vm_free(struct drm_gpuvm *gpuvm)
2005 {
2006 struct xe_vm *vm = container_of(gpuvm, struct xe_vm, gpuvm);
2007
2008 /* To destroy the VM we need to be able to sleep */
2009 queue_work(system_unbound_wq, &vm->destroy_work);
2010 }
2011
xe_vm_lookup(struct xe_file * xef,u32 id)2012 struct xe_vm *xe_vm_lookup(struct xe_file *xef, u32 id)
2013 {
2014 struct xe_vm *vm;
2015
2016 mutex_lock(&xef->vm.lock);
2017 vm = xa_load(&xef->vm.xa, id);
2018 if (vm)
2019 xe_vm_get(vm);
2020 mutex_unlock(&xef->vm.lock);
2021
2022 return vm;
2023 }
2024
xe_vm_pdp4_descriptor(struct xe_vm * vm,struct xe_tile * tile)2025 u64 xe_vm_pdp4_descriptor(struct xe_vm *vm, struct xe_tile *tile)
2026 {
2027 return vm->pt_ops->pde_encode_bo(vm->pt_root[tile->id]->bo, 0,
2028 tile_to_xe(tile)->pat.idx[XE_CACHE_WB]);
2029 }
2030
2031 static struct xe_exec_queue *
to_wait_exec_queue(struct xe_vm * vm,struct xe_exec_queue * q)2032 to_wait_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
2033 {
2034 return q ? q : vm->q[0];
2035 }
2036
2037 static struct xe_user_fence *
find_ufence_get(struct xe_sync_entry * syncs,u32 num_syncs)2038 find_ufence_get(struct xe_sync_entry *syncs, u32 num_syncs)
2039 {
2040 unsigned int i;
2041
2042 for (i = 0; i < num_syncs; i++) {
2043 struct xe_sync_entry *e = &syncs[i];
2044
2045 if (xe_sync_is_ufence(e))
2046 return xe_sync_ufence_get(e);
2047 }
2048
2049 return NULL;
2050 }
2051
2052 #define ALL_DRM_XE_VM_CREATE_FLAGS (DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE | \
2053 DRM_XE_VM_CREATE_FLAG_LR_MODE | \
2054 DRM_XE_VM_CREATE_FLAG_FAULT_MODE)
2055
xe_vm_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file)2056 int xe_vm_create_ioctl(struct drm_device *dev, void *data,
2057 struct drm_file *file)
2058 {
2059 struct xe_device *xe = to_xe_device(dev);
2060 struct xe_file *xef = to_xe_file(file);
2061 struct drm_xe_vm_create *args = data;
2062 struct xe_tile *tile;
2063 struct xe_vm *vm;
2064 u32 id, asid;
2065 int err;
2066 u32 flags = 0;
2067
2068 if (XE_IOCTL_DBG(xe, args->extensions))
2069 return -EINVAL;
2070
2071 if (XE_WA(xe_root_mmio_gt(xe), 14016763929))
2072 args->flags |= DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE;
2073
2074 if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE &&
2075 !xe->info.has_usm))
2076 return -EINVAL;
2077
2078 if (XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2079 return -EINVAL;
2080
2081 if (XE_IOCTL_DBG(xe, args->flags & ~ALL_DRM_XE_VM_CREATE_FLAGS))
2082 return -EINVAL;
2083
2084 if (XE_IOCTL_DBG(xe, args->flags & DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE &&
2085 args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE &&
2086 !xe->info.needs_scratch))
2087 return -EINVAL;
2088
2089 if (XE_IOCTL_DBG(xe, !(args->flags & DRM_XE_VM_CREATE_FLAG_LR_MODE) &&
2090 args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE))
2091 return -EINVAL;
2092
2093 if (args->flags & DRM_XE_VM_CREATE_FLAG_SCRATCH_PAGE)
2094 flags |= XE_VM_FLAG_SCRATCH_PAGE;
2095 if (args->flags & DRM_XE_VM_CREATE_FLAG_LR_MODE)
2096 flags |= XE_VM_FLAG_LR_MODE;
2097 if (args->flags & DRM_XE_VM_CREATE_FLAG_FAULT_MODE)
2098 flags |= XE_VM_FLAG_FAULT_MODE;
2099
2100 vm = xe_vm_create(xe, flags);
2101 if (IS_ERR(vm))
2102 return PTR_ERR(vm);
2103
2104 if (xe->info.has_asid) {
2105 down_write(&xe->usm.lock);
2106 err = xa_alloc_cyclic(&xe->usm.asid_to_vm, &asid, vm,
2107 XA_LIMIT(1, XE_MAX_ASID - 1),
2108 &xe->usm.next_asid, GFP_KERNEL);
2109 up_write(&xe->usm.lock);
2110 if (err < 0)
2111 goto err_close_and_put;
2112
2113 vm->usm.asid = asid;
2114 }
2115
2116 vm->xef = xe_file_get(xef);
2117
2118 /* Record BO memory for VM pagetable created against client */
2119 for_each_tile(tile, xe, id)
2120 if (vm->pt_root[id])
2121 xe_drm_client_add_bo(vm->xef->client, vm->pt_root[id]->bo);
2122
2123 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_MEM)
2124 /* Warning: Security issue - never enable by default */
2125 args->reserved[0] = xe_bo_main_addr(vm->pt_root[0]->bo, XE_PAGE_SIZE);
2126 #endif
2127
2128 /* user id alloc must always be last in ioctl to prevent UAF */
2129 err = xa_alloc(&xef->vm.xa, &id, vm, xa_limit_32b, GFP_KERNEL);
2130 if (err)
2131 goto err_close_and_put;
2132
2133 args->vm_id = id;
2134
2135 return 0;
2136
2137 err_close_and_put:
2138 xe_vm_close_and_put(vm);
2139
2140 return err;
2141 }
2142
xe_vm_destroy_ioctl(struct drm_device * dev,void * data,struct drm_file * file)2143 int xe_vm_destroy_ioctl(struct drm_device *dev, void *data,
2144 struct drm_file *file)
2145 {
2146 struct xe_device *xe = to_xe_device(dev);
2147 struct xe_file *xef = to_xe_file(file);
2148 struct drm_xe_vm_destroy *args = data;
2149 struct xe_vm *vm;
2150 int err = 0;
2151
2152 if (XE_IOCTL_DBG(xe, args->pad) ||
2153 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
2154 return -EINVAL;
2155
2156 mutex_lock(&xef->vm.lock);
2157 vm = xa_load(&xef->vm.xa, args->vm_id);
2158 if (XE_IOCTL_DBG(xe, !vm))
2159 err = -ENOENT;
2160 else if (XE_IOCTL_DBG(xe, vm->preempt.num_exec_queues))
2161 err = -EBUSY;
2162 else
2163 xa_erase(&xef->vm.xa, args->vm_id);
2164 mutex_unlock(&xef->vm.lock);
2165
2166 if (!err)
2167 xe_vm_close_and_put(vm);
2168
2169 return err;
2170 }
2171
vma_matches(struct xe_vma * vma,u64 page_addr)2172 static bool vma_matches(struct xe_vma *vma, u64 page_addr)
2173 {
2174 if (page_addr > xe_vma_end(vma) - 1 ||
2175 page_addr + SZ_4K - 1 < xe_vma_start(vma))
2176 return false;
2177
2178 return true;
2179 }
2180
2181 /**
2182 * xe_vm_find_vma_by_addr() - Find a VMA by its address
2183 *
2184 * @vm: the xe_vm the vma belongs to
2185 * @page_addr: address to look up
2186 */
xe_vm_find_vma_by_addr(struct xe_vm * vm,u64 page_addr)2187 struct xe_vma *xe_vm_find_vma_by_addr(struct xe_vm *vm, u64 page_addr)
2188 {
2189 struct xe_vma *vma = NULL;
2190
2191 if (vm->usm.last_fault_vma) { /* Fast lookup */
2192 if (vma_matches(vm->usm.last_fault_vma, page_addr))
2193 vma = vm->usm.last_fault_vma;
2194 }
2195 if (!vma)
2196 vma = xe_vm_find_overlapping_vma(vm, page_addr, SZ_4K);
2197
2198 return vma;
2199 }
2200
2201 static const u32 region_to_mem_type[] = {
2202 XE_PL_TT,
2203 XE_PL_VRAM0,
2204 XE_PL_VRAM1,
2205 };
2206
prep_vma_destroy(struct xe_vm * vm,struct xe_vma * vma,bool post_commit)2207 static void prep_vma_destroy(struct xe_vm *vm, struct xe_vma *vma,
2208 bool post_commit)
2209 {
2210 down_read(&vm->userptr.notifier_lock);
2211 vma->gpuva.flags |= XE_VMA_DESTROYED;
2212 up_read(&vm->userptr.notifier_lock);
2213 if (post_commit)
2214 xe_vm_remove_vma(vm, vma);
2215 }
2216
2217 #undef ULL
2218 #define ULL unsigned long long
2219
2220 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)
print_op(struct xe_device * xe,struct drm_gpuva_op * op)2221 static void print_op(struct xe_device *xe, struct drm_gpuva_op *op)
2222 {
2223 struct xe_vma *vma;
2224
2225 switch (op->op) {
2226 case DRM_GPUVA_OP_MAP:
2227 vm_dbg(&xe->drm, "MAP: addr=0x%016llx, range=0x%016llx",
2228 (ULL)op->map.va.addr, (ULL)op->map.va.range);
2229 break;
2230 case DRM_GPUVA_OP_REMAP:
2231 vma = gpuva_to_vma(op->remap.unmap->va);
2232 vm_dbg(&xe->drm, "REMAP:UNMAP: addr=0x%016llx, range=0x%016llx, keep=%d",
2233 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma),
2234 op->remap.unmap->keep ? 1 : 0);
2235 if (op->remap.prev)
2236 vm_dbg(&xe->drm,
2237 "REMAP:PREV: addr=0x%016llx, range=0x%016llx",
2238 (ULL)op->remap.prev->va.addr,
2239 (ULL)op->remap.prev->va.range);
2240 if (op->remap.next)
2241 vm_dbg(&xe->drm,
2242 "REMAP:NEXT: addr=0x%016llx, range=0x%016llx",
2243 (ULL)op->remap.next->va.addr,
2244 (ULL)op->remap.next->va.range);
2245 break;
2246 case DRM_GPUVA_OP_UNMAP:
2247 vma = gpuva_to_vma(op->unmap.va);
2248 vm_dbg(&xe->drm, "UNMAP: addr=0x%016llx, range=0x%016llx, keep=%d",
2249 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma),
2250 op->unmap.keep ? 1 : 0);
2251 break;
2252 case DRM_GPUVA_OP_PREFETCH:
2253 vma = gpuva_to_vma(op->prefetch.va);
2254 vm_dbg(&xe->drm, "PREFETCH: addr=0x%016llx, range=0x%016llx",
2255 (ULL)xe_vma_start(vma), (ULL)xe_vma_size(vma));
2256 break;
2257 default:
2258 drm_warn(&xe->drm, "NOT POSSIBLE");
2259 }
2260 }
2261 #else
print_op(struct xe_device * xe,struct drm_gpuva_op * op)2262 static void print_op(struct xe_device *xe, struct drm_gpuva_op *op)
2263 {
2264 }
2265 #endif
2266
__xe_vm_needs_clear_scratch_pages(struct xe_vm * vm,u32 bind_flags)2267 static bool __xe_vm_needs_clear_scratch_pages(struct xe_vm *vm, u32 bind_flags)
2268 {
2269 if (!xe_vm_in_fault_mode(vm))
2270 return false;
2271
2272 if (!xe_vm_has_scratch(vm))
2273 return false;
2274
2275 if (bind_flags & DRM_XE_VM_BIND_FLAG_IMMEDIATE)
2276 return false;
2277
2278 return true;
2279 }
2280
xe_svm_prefetch_gpuva_ops_fini(struct drm_gpuva_ops * ops)2281 static void xe_svm_prefetch_gpuva_ops_fini(struct drm_gpuva_ops *ops)
2282 {
2283 struct drm_gpuva_op *__op;
2284
2285 drm_gpuva_for_each_op(__op, ops) {
2286 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2287
2288 xe_vma_svm_prefetch_op_fini(op);
2289 }
2290 }
2291
2292 /*
2293 * Create operations list from IOCTL arguments, setup operations fields so parse
2294 * and commit steps are decoupled from IOCTL arguments. This step can fail.
2295 */
2296 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)2297 vm_bind_ioctl_ops_create(struct xe_vm *vm, struct xe_vma_ops *vops,
2298 struct xe_bo *bo, u64 bo_offset_or_userptr,
2299 u64 addr, u64 range,
2300 u32 operation, u32 flags,
2301 u32 prefetch_region, u16 pat_index)
2302 {
2303 struct drm_gem_object *obj = bo ? &bo->ttm.base : NULL;
2304 struct drm_gpuva_ops *ops;
2305 struct drm_gpuva_op *__op;
2306 struct drm_gpuvm_bo *vm_bo;
2307 u64 range_end = addr + range;
2308 int err;
2309
2310 lockdep_assert_held_write(&vm->lock);
2311
2312 vm_dbg(&vm->xe->drm,
2313 "op=%d, addr=0x%016llx, range=0x%016llx, bo_offset_or_userptr=0x%016llx",
2314 operation, (ULL)addr, (ULL)range,
2315 (ULL)bo_offset_or_userptr);
2316
2317 switch (operation) {
2318 case DRM_XE_VM_BIND_OP_MAP:
2319 case DRM_XE_VM_BIND_OP_MAP_USERPTR:
2320 ops = drm_gpuvm_sm_map_ops_create(&vm->gpuvm, addr, range,
2321 obj, bo_offset_or_userptr);
2322 break;
2323 case DRM_XE_VM_BIND_OP_UNMAP:
2324 ops = drm_gpuvm_sm_unmap_ops_create(&vm->gpuvm, addr, range);
2325 break;
2326 case DRM_XE_VM_BIND_OP_PREFETCH:
2327 ops = drm_gpuvm_prefetch_ops_create(&vm->gpuvm, addr, range);
2328 break;
2329 case DRM_XE_VM_BIND_OP_UNMAP_ALL:
2330 xe_assert(vm->xe, bo);
2331
2332 err = xe_bo_lock(bo, true);
2333 if (err)
2334 return ERR_PTR(err);
2335
2336 vm_bo = drm_gpuvm_bo_obtain(&vm->gpuvm, obj);
2337 if (IS_ERR(vm_bo)) {
2338 xe_bo_unlock(bo);
2339 return ERR_CAST(vm_bo);
2340 }
2341
2342 ops = drm_gpuvm_bo_unmap_ops_create(vm_bo);
2343 drm_gpuvm_bo_put(vm_bo);
2344 xe_bo_unlock(bo);
2345 break;
2346 default:
2347 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2348 ops = ERR_PTR(-EINVAL);
2349 }
2350 if (IS_ERR(ops))
2351 return ops;
2352
2353 drm_gpuva_for_each_op(__op, ops) {
2354 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2355
2356 if (__op->op == DRM_GPUVA_OP_MAP) {
2357 op->map.immediate =
2358 flags & DRM_XE_VM_BIND_FLAG_IMMEDIATE;
2359 op->map.read_only =
2360 flags & DRM_XE_VM_BIND_FLAG_READONLY;
2361 op->map.is_null = flags & DRM_XE_VM_BIND_FLAG_NULL;
2362 op->map.is_cpu_addr_mirror = flags &
2363 DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR;
2364 op->map.dumpable = flags & DRM_XE_VM_BIND_FLAG_DUMPABLE;
2365 op->map.pat_index = pat_index;
2366 op->map.invalidate_on_bind =
2367 __xe_vm_needs_clear_scratch_pages(vm, flags);
2368 } else if (__op->op == DRM_GPUVA_OP_PREFETCH) {
2369 struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
2370 struct xe_svm_range *svm_range;
2371 struct drm_gpusvm_ctx ctx = {};
2372 struct xe_tile *tile;
2373 u8 id, tile_mask = 0;
2374 u32 i;
2375
2376 if (!xe_vma_is_cpu_addr_mirror(vma)) {
2377 op->prefetch.region = prefetch_region;
2378 break;
2379 }
2380
2381 ctx.read_only = xe_vma_read_only(vma);
2382 ctx.devmem_possible = IS_DGFX(vm->xe) &&
2383 IS_ENABLED(CONFIG_DRM_XE_PAGEMAP);
2384
2385 for_each_tile(tile, vm->xe, id)
2386 tile_mask |= 0x1 << id;
2387
2388 xa_init_flags(&op->prefetch_range.range, XA_FLAGS_ALLOC);
2389 op->prefetch_range.region = prefetch_region;
2390 op->prefetch_range.ranges_count = 0;
2391 alloc_next_range:
2392 svm_range = xe_svm_range_find_or_insert(vm, addr, vma, &ctx);
2393
2394 if (PTR_ERR(svm_range) == -ENOENT) {
2395 u64 ret = xe_svm_find_vma_start(vm, addr, range_end, vma);
2396
2397 addr = ret == ULONG_MAX ? 0 : ret;
2398 if (addr)
2399 goto alloc_next_range;
2400 else
2401 goto print_op_label;
2402 }
2403
2404 if (IS_ERR(svm_range)) {
2405 err = PTR_ERR(svm_range);
2406 goto unwind_prefetch_ops;
2407 }
2408
2409 if (xe_svm_range_validate(vm, svm_range, tile_mask, !!prefetch_region)) {
2410 xe_svm_range_debug(svm_range, "PREFETCH - RANGE IS VALID");
2411 goto check_next_range;
2412 }
2413
2414 err = xa_alloc(&op->prefetch_range.range,
2415 &i, svm_range, xa_limit_32b,
2416 GFP_KERNEL);
2417
2418 if (err)
2419 goto unwind_prefetch_ops;
2420
2421 op->prefetch_range.ranges_count++;
2422 vops->flags |= XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH;
2423 xe_svm_range_debug(svm_range, "PREFETCH - RANGE CREATED");
2424 check_next_range:
2425 if (range_end > xe_svm_range_end(svm_range) &&
2426 xe_svm_range_end(svm_range) < xe_vma_end(vma)) {
2427 addr = xe_svm_range_end(svm_range);
2428 goto alloc_next_range;
2429 }
2430 }
2431 print_op_label:
2432 print_op(vm->xe, __op);
2433 }
2434
2435 return ops;
2436
2437 unwind_prefetch_ops:
2438 xe_svm_prefetch_gpuva_ops_fini(ops);
2439 drm_gpuva_ops_free(&vm->gpuvm, ops);
2440 return ERR_PTR(err);
2441 }
2442
2443 ALLOW_ERROR_INJECTION(vm_bind_ioctl_ops_create, ERRNO);
2444
new_vma(struct xe_vm * vm,struct drm_gpuva_op_map * op,u16 pat_index,unsigned int flags)2445 static struct xe_vma *new_vma(struct xe_vm *vm, struct drm_gpuva_op_map *op,
2446 u16 pat_index, unsigned int flags)
2447 {
2448 struct xe_bo *bo = op->gem.obj ? gem_to_xe_bo(op->gem.obj) : NULL;
2449 struct drm_exec exec;
2450 struct xe_vma *vma;
2451 int err = 0;
2452
2453 lockdep_assert_held_write(&vm->lock);
2454
2455 if (bo) {
2456 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
2457 drm_exec_until_all_locked(&exec) {
2458 err = 0;
2459 if (!bo->vm) {
2460 err = drm_exec_lock_obj(&exec, xe_vm_obj(vm));
2461 drm_exec_retry_on_contention(&exec);
2462 }
2463 if (!err) {
2464 err = drm_exec_lock_obj(&exec, &bo->ttm.base);
2465 drm_exec_retry_on_contention(&exec);
2466 }
2467 if (err) {
2468 drm_exec_fini(&exec);
2469 return ERR_PTR(err);
2470 }
2471 }
2472 }
2473 vma = xe_vma_create(vm, bo, op->gem.offset,
2474 op->va.addr, op->va.addr +
2475 op->va.range - 1, pat_index, flags);
2476 if (IS_ERR(vma))
2477 goto err_unlock;
2478
2479 if (xe_vma_is_userptr(vma))
2480 err = xe_vma_userptr_pin_pages(to_userptr_vma(vma));
2481 else if (!xe_vma_has_no_bo(vma) && !bo->vm)
2482 err = add_preempt_fences(vm, bo);
2483
2484 err_unlock:
2485 if (bo)
2486 drm_exec_fini(&exec);
2487
2488 if (err) {
2489 prep_vma_destroy(vm, vma, false);
2490 xe_vma_destroy_unlocked(vma);
2491 vma = ERR_PTR(err);
2492 }
2493
2494 return vma;
2495 }
2496
xe_vma_max_pte_size(struct xe_vma * vma)2497 static u64 xe_vma_max_pte_size(struct xe_vma *vma)
2498 {
2499 if (vma->gpuva.flags & XE_VMA_PTE_1G)
2500 return SZ_1G;
2501 else if (vma->gpuva.flags & (XE_VMA_PTE_2M | XE_VMA_PTE_COMPACT))
2502 return SZ_2M;
2503 else if (vma->gpuva.flags & XE_VMA_PTE_64K)
2504 return SZ_64K;
2505 else if (vma->gpuva.flags & XE_VMA_PTE_4K)
2506 return SZ_4K;
2507
2508 return SZ_1G; /* Uninitialized, used max size */
2509 }
2510
xe_vma_set_pte_size(struct xe_vma * vma,u64 size)2511 static void xe_vma_set_pte_size(struct xe_vma *vma, u64 size)
2512 {
2513 switch (size) {
2514 case SZ_1G:
2515 vma->gpuva.flags |= XE_VMA_PTE_1G;
2516 break;
2517 case SZ_2M:
2518 vma->gpuva.flags |= XE_VMA_PTE_2M;
2519 break;
2520 case SZ_64K:
2521 vma->gpuva.flags |= XE_VMA_PTE_64K;
2522 break;
2523 case SZ_4K:
2524 vma->gpuva.flags |= XE_VMA_PTE_4K;
2525 break;
2526 }
2527 }
2528
xe_vma_op_commit(struct xe_vm * vm,struct xe_vma_op * op)2529 static int xe_vma_op_commit(struct xe_vm *vm, struct xe_vma_op *op)
2530 {
2531 int err = 0;
2532
2533 lockdep_assert_held_write(&vm->lock);
2534
2535 switch (op->base.op) {
2536 case DRM_GPUVA_OP_MAP:
2537 err |= xe_vm_insert_vma(vm, op->map.vma);
2538 if (!err)
2539 op->flags |= XE_VMA_OP_COMMITTED;
2540 break;
2541 case DRM_GPUVA_OP_REMAP:
2542 {
2543 u8 tile_present =
2544 gpuva_to_vma(op->base.remap.unmap->va)->tile_present;
2545
2546 prep_vma_destroy(vm, gpuva_to_vma(op->base.remap.unmap->va),
2547 true);
2548 op->flags |= XE_VMA_OP_COMMITTED;
2549
2550 if (op->remap.prev) {
2551 err |= xe_vm_insert_vma(vm, op->remap.prev);
2552 if (!err)
2553 op->flags |= XE_VMA_OP_PREV_COMMITTED;
2554 if (!err && op->remap.skip_prev) {
2555 op->remap.prev->tile_present =
2556 tile_present;
2557 op->remap.prev = NULL;
2558 }
2559 }
2560 if (op->remap.next) {
2561 err |= xe_vm_insert_vma(vm, op->remap.next);
2562 if (!err)
2563 op->flags |= XE_VMA_OP_NEXT_COMMITTED;
2564 if (!err && op->remap.skip_next) {
2565 op->remap.next->tile_present =
2566 tile_present;
2567 op->remap.next = NULL;
2568 }
2569 }
2570
2571 /* Adjust for partial unbind after removing VMA from VM */
2572 if (!err) {
2573 op->base.remap.unmap->va->va.addr = op->remap.start;
2574 op->base.remap.unmap->va->va.range = op->remap.range;
2575 }
2576 break;
2577 }
2578 case DRM_GPUVA_OP_UNMAP:
2579 prep_vma_destroy(vm, gpuva_to_vma(op->base.unmap.va), true);
2580 op->flags |= XE_VMA_OP_COMMITTED;
2581 break;
2582 case DRM_GPUVA_OP_PREFETCH:
2583 op->flags |= XE_VMA_OP_COMMITTED;
2584 break;
2585 default:
2586 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2587 }
2588
2589 return err;
2590 }
2591
vm_bind_ioctl_ops_parse(struct xe_vm * vm,struct drm_gpuva_ops * ops,struct xe_vma_ops * vops)2592 static int vm_bind_ioctl_ops_parse(struct xe_vm *vm, struct drm_gpuva_ops *ops,
2593 struct xe_vma_ops *vops)
2594 {
2595 struct xe_device *xe = vm->xe;
2596 struct drm_gpuva_op *__op;
2597 struct xe_tile *tile;
2598 u8 id, tile_mask = 0;
2599 int err = 0;
2600
2601 lockdep_assert_held_write(&vm->lock);
2602
2603 for_each_tile(tile, vm->xe, id)
2604 tile_mask |= 0x1 << id;
2605
2606 drm_gpuva_for_each_op(__op, ops) {
2607 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2608 struct xe_vma *vma;
2609 unsigned int flags = 0;
2610
2611 INIT_LIST_HEAD(&op->link);
2612 list_add_tail(&op->link, &vops->list);
2613 op->tile_mask = tile_mask;
2614
2615 switch (op->base.op) {
2616 case DRM_GPUVA_OP_MAP:
2617 {
2618 flags |= op->map.read_only ?
2619 VMA_CREATE_FLAG_READ_ONLY : 0;
2620 flags |= op->map.is_null ?
2621 VMA_CREATE_FLAG_IS_NULL : 0;
2622 flags |= op->map.dumpable ?
2623 VMA_CREATE_FLAG_DUMPABLE : 0;
2624 flags |= op->map.is_cpu_addr_mirror ?
2625 VMA_CREATE_FLAG_IS_SYSTEM_ALLOCATOR : 0;
2626
2627 vma = new_vma(vm, &op->base.map, op->map.pat_index,
2628 flags);
2629 if (IS_ERR(vma))
2630 return PTR_ERR(vma);
2631
2632 op->map.vma = vma;
2633 if (((op->map.immediate || !xe_vm_in_fault_mode(vm)) &&
2634 !op->map.is_cpu_addr_mirror) ||
2635 op->map.invalidate_on_bind)
2636 xe_vma_ops_incr_pt_update_ops(vops,
2637 op->tile_mask, 1);
2638 break;
2639 }
2640 case DRM_GPUVA_OP_REMAP:
2641 {
2642 struct xe_vma *old =
2643 gpuva_to_vma(op->base.remap.unmap->va);
2644 bool skip = xe_vma_is_cpu_addr_mirror(old);
2645 u64 start = xe_vma_start(old), end = xe_vma_end(old);
2646 int num_remap_ops = 0;
2647
2648 if (op->base.remap.prev)
2649 start = op->base.remap.prev->va.addr +
2650 op->base.remap.prev->va.range;
2651 if (op->base.remap.next)
2652 end = op->base.remap.next->va.addr;
2653
2654 if (xe_vma_is_cpu_addr_mirror(old) &&
2655 xe_svm_has_mapping(vm, start, end))
2656 return -EBUSY;
2657
2658 op->remap.start = xe_vma_start(old);
2659 op->remap.range = xe_vma_size(old);
2660
2661 flags |= op->base.remap.unmap->va->flags &
2662 XE_VMA_READ_ONLY ?
2663 VMA_CREATE_FLAG_READ_ONLY : 0;
2664 flags |= op->base.remap.unmap->va->flags &
2665 DRM_GPUVA_SPARSE ?
2666 VMA_CREATE_FLAG_IS_NULL : 0;
2667 flags |= op->base.remap.unmap->va->flags &
2668 XE_VMA_DUMPABLE ?
2669 VMA_CREATE_FLAG_DUMPABLE : 0;
2670 flags |= xe_vma_is_cpu_addr_mirror(old) ?
2671 VMA_CREATE_FLAG_IS_SYSTEM_ALLOCATOR : 0;
2672
2673 if (op->base.remap.prev) {
2674 vma = new_vma(vm, op->base.remap.prev,
2675 old->pat_index, flags);
2676 if (IS_ERR(vma))
2677 return PTR_ERR(vma);
2678
2679 op->remap.prev = vma;
2680
2681 /*
2682 * Userptr creates a new SG mapping so
2683 * we must also rebind.
2684 */
2685 op->remap.skip_prev = skip ||
2686 (!xe_vma_is_userptr(old) &&
2687 IS_ALIGNED(xe_vma_end(vma),
2688 xe_vma_max_pte_size(old)));
2689 if (op->remap.skip_prev) {
2690 xe_vma_set_pte_size(vma, xe_vma_max_pte_size(old));
2691 op->remap.range -=
2692 xe_vma_end(vma) -
2693 xe_vma_start(old);
2694 op->remap.start = xe_vma_end(vma);
2695 vm_dbg(&xe->drm, "REMAP:SKIP_PREV: addr=0x%016llx, range=0x%016llx",
2696 (ULL)op->remap.start,
2697 (ULL)op->remap.range);
2698 } else {
2699 num_remap_ops++;
2700 }
2701 }
2702
2703 if (op->base.remap.next) {
2704 vma = new_vma(vm, op->base.remap.next,
2705 old->pat_index, flags);
2706 if (IS_ERR(vma))
2707 return PTR_ERR(vma);
2708
2709 op->remap.next = vma;
2710
2711 /*
2712 * Userptr creates a new SG mapping so
2713 * we must also rebind.
2714 */
2715 op->remap.skip_next = skip ||
2716 (!xe_vma_is_userptr(old) &&
2717 IS_ALIGNED(xe_vma_start(vma),
2718 xe_vma_max_pte_size(old)));
2719 if (op->remap.skip_next) {
2720 xe_vma_set_pte_size(vma, xe_vma_max_pte_size(old));
2721 op->remap.range -=
2722 xe_vma_end(old) -
2723 xe_vma_start(vma);
2724 vm_dbg(&xe->drm, "REMAP:SKIP_NEXT: addr=0x%016llx, range=0x%016llx",
2725 (ULL)op->remap.start,
2726 (ULL)op->remap.range);
2727 } else {
2728 num_remap_ops++;
2729 }
2730 }
2731 if (!skip)
2732 num_remap_ops++;
2733
2734 xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, num_remap_ops);
2735 break;
2736 }
2737 case DRM_GPUVA_OP_UNMAP:
2738 vma = gpuva_to_vma(op->base.unmap.va);
2739
2740 if (xe_vma_is_cpu_addr_mirror(vma) &&
2741 xe_svm_has_mapping(vm, xe_vma_start(vma),
2742 xe_vma_end(vma)))
2743 return -EBUSY;
2744
2745 if (!xe_vma_is_cpu_addr_mirror(vma))
2746 xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, 1);
2747 break;
2748 case DRM_GPUVA_OP_PREFETCH:
2749 vma = gpuva_to_vma(op->base.prefetch.va);
2750
2751 if (xe_vma_is_userptr(vma)) {
2752 err = xe_vma_userptr_pin_pages(to_userptr_vma(vma));
2753 if (err)
2754 return err;
2755 }
2756
2757 if (xe_vma_is_cpu_addr_mirror(vma))
2758 xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask,
2759 op->prefetch_range.ranges_count);
2760 else
2761 xe_vma_ops_incr_pt_update_ops(vops, op->tile_mask, 1);
2762
2763 break;
2764 default:
2765 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2766 }
2767
2768 err = xe_vma_op_commit(vm, op);
2769 if (err)
2770 return err;
2771 }
2772
2773 return 0;
2774 }
2775
xe_vma_op_unwind(struct xe_vm * vm,struct xe_vma_op * op,bool post_commit,bool prev_post_commit,bool next_post_commit)2776 static void xe_vma_op_unwind(struct xe_vm *vm, struct xe_vma_op *op,
2777 bool post_commit, bool prev_post_commit,
2778 bool next_post_commit)
2779 {
2780 lockdep_assert_held_write(&vm->lock);
2781
2782 switch (op->base.op) {
2783 case DRM_GPUVA_OP_MAP:
2784 if (op->map.vma) {
2785 prep_vma_destroy(vm, op->map.vma, post_commit);
2786 xe_vma_destroy_unlocked(op->map.vma);
2787 }
2788 break;
2789 case DRM_GPUVA_OP_UNMAP:
2790 {
2791 struct xe_vma *vma = gpuva_to_vma(op->base.unmap.va);
2792
2793 if (vma) {
2794 down_read(&vm->userptr.notifier_lock);
2795 vma->gpuva.flags &= ~XE_VMA_DESTROYED;
2796 up_read(&vm->userptr.notifier_lock);
2797 if (post_commit)
2798 xe_vm_insert_vma(vm, vma);
2799 }
2800 break;
2801 }
2802 case DRM_GPUVA_OP_REMAP:
2803 {
2804 struct xe_vma *vma = gpuva_to_vma(op->base.remap.unmap->va);
2805
2806 if (op->remap.prev) {
2807 prep_vma_destroy(vm, op->remap.prev, prev_post_commit);
2808 xe_vma_destroy_unlocked(op->remap.prev);
2809 }
2810 if (op->remap.next) {
2811 prep_vma_destroy(vm, op->remap.next, next_post_commit);
2812 xe_vma_destroy_unlocked(op->remap.next);
2813 }
2814 if (vma) {
2815 down_read(&vm->userptr.notifier_lock);
2816 vma->gpuva.flags &= ~XE_VMA_DESTROYED;
2817 up_read(&vm->userptr.notifier_lock);
2818 if (post_commit)
2819 xe_vm_insert_vma(vm, vma);
2820 }
2821 break;
2822 }
2823 case DRM_GPUVA_OP_PREFETCH:
2824 /* Nothing to do */
2825 break;
2826 default:
2827 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2828 }
2829 }
2830
vm_bind_ioctl_ops_unwind(struct xe_vm * vm,struct drm_gpuva_ops ** ops,int num_ops_list)2831 static void vm_bind_ioctl_ops_unwind(struct xe_vm *vm,
2832 struct drm_gpuva_ops **ops,
2833 int num_ops_list)
2834 {
2835 int i;
2836
2837 for (i = num_ops_list - 1; i >= 0; --i) {
2838 struct drm_gpuva_ops *__ops = ops[i];
2839 struct drm_gpuva_op *__op;
2840
2841 if (!__ops)
2842 continue;
2843
2844 drm_gpuva_for_each_op_reverse(__op, __ops) {
2845 struct xe_vma_op *op = gpuva_op_to_vma_op(__op);
2846
2847 xe_vma_op_unwind(vm, op,
2848 op->flags & XE_VMA_OP_COMMITTED,
2849 op->flags & XE_VMA_OP_PREV_COMMITTED,
2850 op->flags & XE_VMA_OP_NEXT_COMMITTED);
2851 }
2852 }
2853 }
2854
vma_lock_and_validate(struct drm_exec * exec,struct xe_vma * vma,bool validate)2855 static int vma_lock_and_validate(struct drm_exec *exec, struct xe_vma *vma,
2856 bool validate)
2857 {
2858 struct xe_bo *bo = xe_vma_bo(vma);
2859 struct xe_vm *vm = xe_vma_vm(vma);
2860 int err = 0;
2861
2862 if (bo) {
2863 if (!bo->vm)
2864 err = drm_exec_lock_obj(exec, &bo->ttm.base);
2865 if (!err && validate)
2866 err = xe_bo_validate(bo, vm,
2867 !xe_vm_in_preempt_fence_mode(vm));
2868 }
2869
2870 return err;
2871 }
2872
check_ufence(struct xe_vma * vma)2873 static int check_ufence(struct xe_vma *vma)
2874 {
2875 if (vma->ufence) {
2876 struct xe_user_fence * const f = vma->ufence;
2877
2878 if (!xe_sync_ufence_get_status(f))
2879 return -EBUSY;
2880
2881 vma->ufence = NULL;
2882 xe_sync_ufence_put(f);
2883 }
2884
2885 return 0;
2886 }
2887
prefetch_ranges(struct xe_vm * vm,struct xe_vma_op * op)2888 static int prefetch_ranges(struct xe_vm *vm, struct xe_vma_op *op)
2889 {
2890 bool devmem_possible = IS_DGFX(vm->xe) && IS_ENABLED(CONFIG_DRM_XE_PAGEMAP);
2891 struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
2892 int err = 0;
2893
2894 struct xe_svm_range *svm_range;
2895 struct drm_gpusvm_ctx ctx = {};
2896 struct xe_tile *tile;
2897 unsigned long i;
2898 u32 region;
2899
2900 if (!xe_vma_is_cpu_addr_mirror(vma))
2901 return 0;
2902
2903 region = op->prefetch_range.region;
2904
2905 ctx.read_only = xe_vma_read_only(vma);
2906 ctx.devmem_possible = devmem_possible;
2907 ctx.check_pages_threshold = devmem_possible ? SZ_64K : 0;
2908
2909 /* TODO: Threading the migration */
2910 xa_for_each(&op->prefetch_range.range, i, svm_range) {
2911 if (!region)
2912 xe_svm_range_migrate_to_smem(vm, svm_range);
2913
2914 if (xe_svm_range_needs_migrate_to_vram(svm_range, vma, region)) {
2915 tile = &vm->xe->tiles[region_to_mem_type[region] - XE_PL_VRAM0];
2916 err = xe_svm_alloc_vram(tile, svm_range, &ctx);
2917 if (err) {
2918 drm_dbg(&vm->xe->drm, "VRAM allocation failed, retry from userspace, asid=%u, gpusvm=%p, errno=%pe\n",
2919 vm->usm.asid, &vm->svm.gpusvm, ERR_PTR(err));
2920 return -ENODATA;
2921 }
2922 xe_svm_range_debug(svm_range, "PREFETCH - RANGE MIGRATED TO VRAM");
2923 }
2924
2925 err = xe_svm_range_get_pages(vm, svm_range, &ctx);
2926 if (err) {
2927 drm_dbg(&vm->xe->drm, "Get pages failed, asid=%u, gpusvm=%p, errno=%pe\n",
2928 vm->usm.asid, &vm->svm.gpusvm, ERR_PTR(err));
2929 if (err == -EOPNOTSUPP || err == -EFAULT || err == -EPERM)
2930 err = -ENODATA;
2931 return err;
2932 }
2933 xe_svm_range_debug(svm_range, "PREFETCH - RANGE GET PAGES DONE");
2934 }
2935
2936 return err;
2937 }
2938
op_lock_and_prep(struct drm_exec * exec,struct xe_vm * vm,struct xe_vma_op * op)2939 static int op_lock_and_prep(struct drm_exec *exec, struct xe_vm *vm,
2940 struct xe_vma_op *op)
2941 {
2942 int err = 0;
2943
2944 switch (op->base.op) {
2945 case DRM_GPUVA_OP_MAP:
2946 if (!op->map.invalidate_on_bind)
2947 err = vma_lock_and_validate(exec, op->map.vma,
2948 !xe_vm_in_fault_mode(vm) ||
2949 op->map.immediate);
2950 break;
2951 case DRM_GPUVA_OP_REMAP:
2952 err = check_ufence(gpuva_to_vma(op->base.remap.unmap->va));
2953 if (err)
2954 break;
2955
2956 err = vma_lock_and_validate(exec,
2957 gpuva_to_vma(op->base.remap.unmap->va),
2958 false);
2959 if (!err && op->remap.prev)
2960 err = vma_lock_and_validate(exec, op->remap.prev, true);
2961 if (!err && op->remap.next)
2962 err = vma_lock_and_validate(exec, op->remap.next, true);
2963 break;
2964 case DRM_GPUVA_OP_UNMAP:
2965 err = check_ufence(gpuva_to_vma(op->base.unmap.va));
2966 if (err)
2967 break;
2968
2969 err = vma_lock_and_validate(exec,
2970 gpuva_to_vma(op->base.unmap.va),
2971 false);
2972 break;
2973 case DRM_GPUVA_OP_PREFETCH:
2974 {
2975 struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va);
2976 u32 region;
2977
2978 if (xe_vma_is_cpu_addr_mirror(vma))
2979 region = op->prefetch_range.region;
2980 else
2981 region = op->prefetch.region;
2982
2983 xe_assert(vm->xe, region <= ARRAY_SIZE(region_to_mem_type));
2984
2985 err = vma_lock_and_validate(exec,
2986 gpuva_to_vma(op->base.prefetch.va),
2987 false);
2988 if (!err && !xe_vma_has_no_bo(vma))
2989 err = xe_bo_migrate(xe_vma_bo(vma),
2990 region_to_mem_type[region]);
2991 break;
2992 }
2993 default:
2994 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
2995 }
2996
2997 return err;
2998 }
2999
vm_bind_ioctl_ops_prefetch_ranges(struct xe_vm * vm,struct xe_vma_ops * vops)3000 static int vm_bind_ioctl_ops_prefetch_ranges(struct xe_vm *vm, struct xe_vma_ops *vops)
3001 {
3002 struct xe_vma_op *op;
3003 int err;
3004
3005 if (!(vops->flags & XE_VMA_OPS_FLAG_HAS_SVM_PREFETCH))
3006 return 0;
3007
3008 list_for_each_entry(op, &vops->list, link) {
3009 if (op->base.op == DRM_GPUVA_OP_PREFETCH) {
3010 err = prefetch_ranges(vm, op);
3011 if (err)
3012 return err;
3013 }
3014 }
3015
3016 return 0;
3017 }
3018
vm_bind_ioctl_ops_lock_and_prep(struct drm_exec * exec,struct xe_vm * vm,struct xe_vma_ops * vops)3019 static int vm_bind_ioctl_ops_lock_and_prep(struct drm_exec *exec,
3020 struct xe_vm *vm,
3021 struct xe_vma_ops *vops)
3022 {
3023 struct xe_vma_op *op;
3024 int err;
3025
3026 err = drm_exec_lock_obj(exec, xe_vm_obj(vm));
3027 if (err)
3028 return err;
3029
3030 list_for_each_entry(op, &vops->list, link) {
3031 err = op_lock_and_prep(exec, vm, op);
3032 if (err)
3033 return err;
3034 }
3035
3036 #ifdef TEST_VM_OPS_ERROR
3037 if (vops->inject_error &&
3038 vm->xe->vm_inject_error_position == FORCE_OP_ERROR_LOCK)
3039 return -ENOSPC;
3040 #endif
3041
3042 return 0;
3043 }
3044
op_trace(struct xe_vma_op * op)3045 static void op_trace(struct xe_vma_op *op)
3046 {
3047 switch (op->base.op) {
3048 case DRM_GPUVA_OP_MAP:
3049 trace_xe_vma_bind(op->map.vma);
3050 break;
3051 case DRM_GPUVA_OP_REMAP:
3052 trace_xe_vma_unbind(gpuva_to_vma(op->base.remap.unmap->va));
3053 if (op->remap.prev)
3054 trace_xe_vma_bind(op->remap.prev);
3055 if (op->remap.next)
3056 trace_xe_vma_bind(op->remap.next);
3057 break;
3058 case DRM_GPUVA_OP_UNMAP:
3059 trace_xe_vma_unbind(gpuva_to_vma(op->base.unmap.va));
3060 break;
3061 case DRM_GPUVA_OP_PREFETCH:
3062 trace_xe_vma_bind(gpuva_to_vma(op->base.prefetch.va));
3063 break;
3064 case DRM_GPUVA_OP_DRIVER:
3065 break;
3066 default:
3067 XE_WARN_ON("NOT POSSIBLE");
3068 }
3069 }
3070
trace_xe_vm_ops_execute(struct xe_vma_ops * vops)3071 static void trace_xe_vm_ops_execute(struct xe_vma_ops *vops)
3072 {
3073 struct xe_vma_op *op;
3074
3075 list_for_each_entry(op, &vops->list, link)
3076 op_trace(op);
3077 }
3078
vm_ops_setup_tile_args(struct xe_vm * vm,struct xe_vma_ops * vops)3079 static int vm_ops_setup_tile_args(struct xe_vm *vm, struct xe_vma_ops *vops)
3080 {
3081 struct xe_exec_queue *q = vops->q;
3082 struct xe_tile *tile;
3083 int number_tiles = 0;
3084 u8 id;
3085
3086 for_each_tile(tile, vm->xe, id) {
3087 if (vops->pt_update_ops[id].num_ops)
3088 ++number_tiles;
3089
3090 if (vops->pt_update_ops[id].q)
3091 continue;
3092
3093 if (q) {
3094 vops->pt_update_ops[id].q = q;
3095 if (vm->pt_root[id] && !list_empty(&q->multi_gt_list))
3096 q = list_next_entry(q, multi_gt_list);
3097 } else {
3098 vops->pt_update_ops[id].q = vm->q[id];
3099 }
3100 }
3101
3102 return number_tiles;
3103 }
3104
ops_execute(struct xe_vm * vm,struct xe_vma_ops * vops)3105 static struct dma_fence *ops_execute(struct xe_vm *vm,
3106 struct xe_vma_ops *vops)
3107 {
3108 struct xe_tile *tile;
3109 struct dma_fence *fence = NULL;
3110 struct dma_fence **fences = NULL;
3111 struct dma_fence_array *cf = NULL;
3112 int number_tiles = 0, current_fence = 0, err;
3113 u8 id;
3114
3115 number_tiles = vm_ops_setup_tile_args(vm, vops);
3116 if (number_tiles == 0)
3117 return ERR_PTR(-ENODATA);
3118
3119 if (number_tiles > 1) {
3120 fences = kmalloc_array(number_tiles, sizeof(*fences),
3121 GFP_KERNEL);
3122 if (!fences) {
3123 fence = ERR_PTR(-ENOMEM);
3124 goto err_trace;
3125 }
3126 }
3127
3128 for_each_tile(tile, vm->xe, id) {
3129 if (!vops->pt_update_ops[id].num_ops)
3130 continue;
3131
3132 err = xe_pt_update_ops_prepare(tile, vops);
3133 if (err) {
3134 fence = ERR_PTR(err);
3135 goto err_out;
3136 }
3137 }
3138
3139 trace_xe_vm_ops_execute(vops);
3140
3141 for_each_tile(tile, vm->xe, id) {
3142 if (!vops->pt_update_ops[id].num_ops)
3143 continue;
3144
3145 fence = xe_pt_update_ops_run(tile, vops);
3146 if (IS_ERR(fence))
3147 goto err_out;
3148
3149 if (fences)
3150 fences[current_fence++] = fence;
3151 }
3152
3153 if (fences) {
3154 cf = dma_fence_array_create(number_tiles, fences,
3155 vm->composite_fence_ctx,
3156 vm->composite_fence_seqno++,
3157 false);
3158 if (!cf) {
3159 --vm->composite_fence_seqno;
3160 fence = ERR_PTR(-ENOMEM);
3161 goto err_out;
3162 }
3163 fence = &cf->base;
3164 }
3165
3166 for_each_tile(tile, vm->xe, id) {
3167 if (!vops->pt_update_ops[id].num_ops)
3168 continue;
3169
3170 xe_pt_update_ops_fini(tile, vops);
3171 }
3172
3173 return fence;
3174
3175 err_out:
3176 for_each_tile(tile, vm->xe, id) {
3177 if (!vops->pt_update_ops[id].num_ops)
3178 continue;
3179
3180 xe_pt_update_ops_abort(tile, vops);
3181 }
3182 while (current_fence)
3183 dma_fence_put(fences[--current_fence]);
3184 kfree(fences);
3185 kfree(cf);
3186
3187 err_trace:
3188 trace_xe_vm_ops_fail(vm);
3189 return fence;
3190 }
3191
vma_add_ufence(struct xe_vma * vma,struct xe_user_fence * ufence)3192 static void vma_add_ufence(struct xe_vma *vma, struct xe_user_fence *ufence)
3193 {
3194 if (vma->ufence)
3195 xe_sync_ufence_put(vma->ufence);
3196 vma->ufence = __xe_sync_ufence_get(ufence);
3197 }
3198
op_add_ufence(struct xe_vm * vm,struct xe_vma_op * op,struct xe_user_fence * ufence)3199 static void op_add_ufence(struct xe_vm *vm, struct xe_vma_op *op,
3200 struct xe_user_fence *ufence)
3201 {
3202 switch (op->base.op) {
3203 case DRM_GPUVA_OP_MAP:
3204 vma_add_ufence(op->map.vma, ufence);
3205 break;
3206 case DRM_GPUVA_OP_REMAP:
3207 if (op->remap.prev)
3208 vma_add_ufence(op->remap.prev, ufence);
3209 if (op->remap.next)
3210 vma_add_ufence(op->remap.next, ufence);
3211 break;
3212 case DRM_GPUVA_OP_UNMAP:
3213 break;
3214 case DRM_GPUVA_OP_PREFETCH:
3215 vma_add_ufence(gpuva_to_vma(op->base.prefetch.va), ufence);
3216 break;
3217 default:
3218 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
3219 }
3220 }
3221
vm_bind_ioctl_ops_fini(struct xe_vm * vm,struct xe_vma_ops * vops,struct dma_fence * fence)3222 static void vm_bind_ioctl_ops_fini(struct xe_vm *vm, struct xe_vma_ops *vops,
3223 struct dma_fence *fence)
3224 {
3225 struct xe_exec_queue *wait_exec_queue = to_wait_exec_queue(vm, vops->q);
3226 struct xe_user_fence *ufence;
3227 struct xe_vma_op *op;
3228 int i;
3229
3230 ufence = find_ufence_get(vops->syncs, vops->num_syncs);
3231 list_for_each_entry(op, &vops->list, link) {
3232 if (ufence)
3233 op_add_ufence(vm, op, ufence);
3234
3235 if (op->base.op == DRM_GPUVA_OP_UNMAP)
3236 xe_vma_destroy(gpuva_to_vma(op->base.unmap.va), fence);
3237 else if (op->base.op == DRM_GPUVA_OP_REMAP)
3238 xe_vma_destroy(gpuva_to_vma(op->base.remap.unmap->va),
3239 fence);
3240 }
3241 if (ufence)
3242 xe_sync_ufence_put(ufence);
3243 if (fence) {
3244 for (i = 0; i < vops->num_syncs; i++)
3245 xe_sync_entry_signal(vops->syncs + i, fence);
3246 xe_exec_queue_last_fence_set(wait_exec_queue, vm, fence);
3247 }
3248 }
3249
vm_bind_ioctl_ops_execute(struct xe_vm * vm,struct xe_vma_ops * vops)3250 static struct dma_fence *vm_bind_ioctl_ops_execute(struct xe_vm *vm,
3251 struct xe_vma_ops *vops)
3252 {
3253 struct drm_exec exec;
3254 struct dma_fence *fence;
3255 int err;
3256
3257 lockdep_assert_held_write(&vm->lock);
3258
3259 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
3260 DRM_EXEC_IGNORE_DUPLICATES, 0);
3261 drm_exec_until_all_locked(&exec) {
3262 err = vm_bind_ioctl_ops_lock_and_prep(&exec, vm, vops);
3263 drm_exec_retry_on_contention(&exec);
3264 if (err) {
3265 fence = ERR_PTR(err);
3266 goto unlock;
3267 }
3268
3269 fence = ops_execute(vm, vops);
3270 if (IS_ERR(fence)) {
3271 if (PTR_ERR(fence) == -ENODATA)
3272 vm_bind_ioctl_ops_fini(vm, vops, NULL);
3273 goto unlock;
3274 }
3275
3276 vm_bind_ioctl_ops_fini(vm, vops, fence);
3277 }
3278
3279 unlock:
3280 drm_exec_fini(&exec);
3281 return fence;
3282 }
3283 ALLOW_ERROR_INJECTION(vm_bind_ioctl_ops_execute, ERRNO);
3284
3285 #define SUPPORTED_FLAGS_STUB \
3286 (DRM_XE_VM_BIND_FLAG_READONLY | \
3287 DRM_XE_VM_BIND_FLAG_IMMEDIATE | \
3288 DRM_XE_VM_BIND_FLAG_NULL | \
3289 DRM_XE_VM_BIND_FLAG_DUMPABLE | \
3290 DRM_XE_VM_BIND_FLAG_CHECK_PXP | \
3291 DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR)
3292
3293 #ifdef TEST_VM_OPS_ERROR
3294 #define SUPPORTED_FLAGS (SUPPORTED_FLAGS_STUB | FORCE_OP_ERROR)
3295 #else
3296 #define SUPPORTED_FLAGS SUPPORTED_FLAGS_STUB
3297 #endif
3298
3299 #define XE_64K_PAGE_MASK 0xffffull
3300 #define ALL_DRM_XE_SYNCS_FLAGS (DRM_XE_SYNCS_FLAG_WAIT_FOR_OP)
3301
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)3302 static int vm_bind_ioctl_check_args(struct xe_device *xe, struct xe_vm *vm,
3303 struct drm_xe_vm_bind *args,
3304 struct drm_xe_vm_bind_op **bind_ops)
3305 {
3306 int err;
3307 int i;
3308
3309 if (XE_IOCTL_DBG(xe, args->pad || args->pad2) ||
3310 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
3311 return -EINVAL;
3312
3313 if (XE_IOCTL_DBG(xe, args->extensions))
3314 return -EINVAL;
3315
3316 if (args->num_binds > 1) {
3317 u64 __user *bind_user =
3318 u64_to_user_ptr(args->vector_of_binds);
3319
3320 *bind_ops = kvmalloc_array(args->num_binds,
3321 sizeof(struct drm_xe_vm_bind_op),
3322 GFP_KERNEL | __GFP_ACCOUNT |
3323 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
3324 if (!*bind_ops)
3325 return args->num_binds > 1 ? -ENOBUFS : -ENOMEM;
3326
3327 err = copy_from_user(*bind_ops, bind_user,
3328 sizeof(struct drm_xe_vm_bind_op) *
3329 args->num_binds);
3330 if (XE_IOCTL_DBG(xe, err)) {
3331 err = -EFAULT;
3332 goto free_bind_ops;
3333 }
3334 } else {
3335 *bind_ops = &args->bind;
3336 }
3337
3338 for (i = 0; i < args->num_binds; ++i) {
3339 u64 range = (*bind_ops)[i].range;
3340 u64 addr = (*bind_ops)[i].addr;
3341 u32 op = (*bind_ops)[i].op;
3342 u32 flags = (*bind_ops)[i].flags;
3343 u32 obj = (*bind_ops)[i].obj;
3344 u64 obj_offset = (*bind_ops)[i].obj_offset;
3345 u32 prefetch_region = (*bind_ops)[i].prefetch_mem_region_instance;
3346 bool is_null = flags & DRM_XE_VM_BIND_FLAG_NULL;
3347 bool is_cpu_addr_mirror = flags &
3348 DRM_XE_VM_BIND_FLAG_CPU_ADDR_MIRROR;
3349 u16 pat_index = (*bind_ops)[i].pat_index;
3350 u16 coh_mode;
3351
3352 if (XE_IOCTL_DBG(xe, is_cpu_addr_mirror &&
3353 (!xe_vm_in_fault_mode(vm) ||
3354 !IS_ENABLED(CONFIG_DRM_XE_GPUSVM)))) {
3355 err = -EINVAL;
3356 goto free_bind_ops;
3357 }
3358
3359 if (XE_IOCTL_DBG(xe, pat_index >= xe->pat.n_entries)) {
3360 err = -EINVAL;
3361 goto free_bind_ops;
3362 }
3363
3364 pat_index = array_index_nospec(pat_index, xe->pat.n_entries);
3365 (*bind_ops)[i].pat_index = pat_index;
3366 coh_mode = xe_pat_index_get_coh_mode(xe, pat_index);
3367 if (XE_IOCTL_DBG(xe, !coh_mode)) { /* hw reserved */
3368 err = -EINVAL;
3369 goto free_bind_ops;
3370 }
3371
3372 if (XE_WARN_ON(coh_mode > XE_COH_AT_LEAST_1WAY)) {
3373 err = -EINVAL;
3374 goto free_bind_ops;
3375 }
3376
3377 if (XE_IOCTL_DBG(xe, op > DRM_XE_VM_BIND_OP_PREFETCH) ||
3378 XE_IOCTL_DBG(xe, flags & ~SUPPORTED_FLAGS) ||
3379 XE_IOCTL_DBG(xe, obj && (is_null || is_cpu_addr_mirror)) ||
3380 XE_IOCTL_DBG(xe, obj_offset && (is_null ||
3381 is_cpu_addr_mirror)) ||
3382 XE_IOCTL_DBG(xe, op != DRM_XE_VM_BIND_OP_MAP &&
3383 (is_null || is_cpu_addr_mirror)) ||
3384 XE_IOCTL_DBG(xe, !obj &&
3385 op == DRM_XE_VM_BIND_OP_MAP &&
3386 !is_null && !is_cpu_addr_mirror) ||
3387 XE_IOCTL_DBG(xe, !obj &&
3388 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
3389 XE_IOCTL_DBG(xe, addr &&
3390 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
3391 XE_IOCTL_DBG(xe, range &&
3392 op == DRM_XE_VM_BIND_OP_UNMAP_ALL) ||
3393 XE_IOCTL_DBG(xe, obj &&
3394 op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
3395 XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE &&
3396 op == DRM_XE_VM_BIND_OP_MAP_USERPTR) ||
3397 XE_IOCTL_DBG(xe, obj &&
3398 op == DRM_XE_VM_BIND_OP_PREFETCH) ||
3399 XE_IOCTL_DBG(xe, prefetch_region &&
3400 op != DRM_XE_VM_BIND_OP_PREFETCH) ||
3401 XE_IOCTL_DBG(xe, !(BIT(prefetch_region) &
3402 xe->info.mem_region_mask)) ||
3403 XE_IOCTL_DBG(xe, obj &&
3404 op == DRM_XE_VM_BIND_OP_UNMAP)) {
3405 err = -EINVAL;
3406 goto free_bind_ops;
3407 }
3408
3409 if (XE_IOCTL_DBG(xe, obj_offset & ~PAGE_MASK) ||
3410 XE_IOCTL_DBG(xe, addr & ~PAGE_MASK) ||
3411 XE_IOCTL_DBG(xe, range & ~PAGE_MASK) ||
3412 XE_IOCTL_DBG(xe, !range &&
3413 op != DRM_XE_VM_BIND_OP_UNMAP_ALL)) {
3414 err = -EINVAL;
3415 goto free_bind_ops;
3416 }
3417 }
3418
3419 return 0;
3420
3421 free_bind_ops:
3422 if (args->num_binds > 1)
3423 kvfree(*bind_ops);
3424 return err;
3425 }
3426
vm_bind_ioctl_signal_fences(struct xe_vm * vm,struct xe_exec_queue * q,struct xe_sync_entry * syncs,int num_syncs)3427 static int vm_bind_ioctl_signal_fences(struct xe_vm *vm,
3428 struct xe_exec_queue *q,
3429 struct xe_sync_entry *syncs,
3430 int num_syncs)
3431 {
3432 struct dma_fence *fence;
3433 int i, err = 0;
3434
3435 fence = xe_sync_in_fence_get(syncs, num_syncs,
3436 to_wait_exec_queue(vm, q), vm);
3437 if (IS_ERR(fence))
3438 return PTR_ERR(fence);
3439
3440 for (i = 0; i < num_syncs; i++)
3441 xe_sync_entry_signal(&syncs[i], fence);
3442
3443 xe_exec_queue_last_fence_set(to_wait_exec_queue(vm, q), vm,
3444 fence);
3445 dma_fence_put(fence);
3446
3447 return err;
3448 }
3449
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)3450 static void xe_vma_ops_init(struct xe_vma_ops *vops, struct xe_vm *vm,
3451 struct xe_exec_queue *q,
3452 struct xe_sync_entry *syncs, u32 num_syncs)
3453 {
3454 memset(vops, 0, sizeof(*vops));
3455 INIT_LIST_HEAD(&vops->list);
3456 vops->vm = vm;
3457 vops->q = q;
3458 vops->syncs = syncs;
3459 vops->num_syncs = num_syncs;
3460 vops->flags = 0;
3461 }
3462
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)3463 static int xe_vm_bind_ioctl_validate_bo(struct xe_device *xe, struct xe_bo *bo,
3464 u64 addr, u64 range, u64 obj_offset,
3465 u16 pat_index, u32 op, u32 bind_flags)
3466 {
3467 u16 coh_mode;
3468
3469 if (XE_IOCTL_DBG(xe, range > xe_bo_size(bo)) ||
3470 XE_IOCTL_DBG(xe, obj_offset >
3471 xe_bo_size(bo) - range)) {
3472 return -EINVAL;
3473 }
3474
3475 /*
3476 * Some platforms require 64k VM_BIND alignment,
3477 * specifically those with XE_VRAM_FLAGS_NEED64K.
3478 *
3479 * Other platforms may have BO's set to 64k physical placement,
3480 * but can be mapped at 4k offsets anyway. This check is only
3481 * there for the former case.
3482 */
3483 if ((bo->flags & XE_BO_FLAG_INTERNAL_64K) &&
3484 (xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K)) {
3485 if (XE_IOCTL_DBG(xe, obj_offset &
3486 XE_64K_PAGE_MASK) ||
3487 XE_IOCTL_DBG(xe, addr & XE_64K_PAGE_MASK) ||
3488 XE_IOCTL_DBG(xe, range & XE_64K_PAGE_MASK)) {
3489 return -EINVAL;
3490 }
3491 }
3492
3493 coh_mode = xe_pat_index_get_coh_mode(xe, pat_index);
3494 if (bo->cpu_caching) {
3495 if (XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE &&
3496 bo->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB)) {
3497 return -EINVAL;
3498 }
3499 } else if (XE_IOCTL_DBG(xe, coh_mode == XE_COH_NONE)) {
3500 /*
3501 * Imported dma-buf from a different device should
3502 * require 1way or 2way coherency since we don't know
3503 * how it was mapped on the CPU. Just assume is it
3504 * potentially cached on CPU side.
3505 */
3506 return -EINVAL;
3507 }
3508
3509 /* If a BO is protected it can only be mapped if the key is still valid */
3510 if ((bind_flags & DRM_XE_VM_BIND_FLAG_CHECK_PXP) && xe_bo_is_protected(bo) &&
3511 op != DRM_XE_VM_BIND_OP_UNMAP && op != DRM_XE_VM_BIND_OP_UNMAP_ALL)
3512 if (XE_IOCTL_DBG(xe, xe_pxp_bo_key_check(xe->pxp, bo) != 0))
3513 return -ENOEXEC;
3514
3515 return 0;
3516 }
3517
xe_vm_bind_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3518 int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3519 {
3520 struct xe_device *xe = to_xe_device(dev);
3521 struct xe_file *xef = to_xe_file(file);
3522 struct drm_xe_vm_bind *args = data;
3523 struct drm_xe_sync __user *syncs_user;
3524 struct xe_bo **bos = NULL;
3525 struct drm_gpuva_ops **ops = NULL;
3526 struct xe_vm *vm;
3527 struct xe_exec_queue *q = NULL;
3528 u32 num_syncs, num_ufence = 0;
3529 struct xe_sync_entry *syncs = NULL;
3530 struct drm_xe_vm_bind_op *bind_ops;
3531 struct xe_vma_ops vops;
3532 struct dma_fence *fence;
3533 int err;
3534 int i;
3535
3536 vm = xe_vm_lookup(xef, args->vm_id);
3537 if (XE_IOCTL_DBG(xe, !vm))
3538 return -EINVAL;
3539
3540 err = vm_bind_ioctl_check_args(xe, vm, args, &bind_ops);
3541 if (err)
3542 goto put_vm;
3543
3544 if (args->exec_queue_id) {
3545 q = xe_exec_queue_lookup(xef, args->exec_queue_id);
3546 if (XE_IOCTL_DBG(xe, !q)) {
3547 err = -ENOENT;
3548 goto put_vm;
3549 }
3550
3551 if (XE_IOCTL_DBG(xe, !(q->flags & EXEC_QUEUE_FLAG_VM))) {
3552 err = -EINVAL;
3553 goto put_exec_queue;
3554 }
3555 }
3556
3557 /* Ensure all UNMAPs visible */
3558 xe_svm_flush(vm);
3559
3560 err = down_write_killable(&vm->lock);
3561 if (err)
3562 goto put_exec_queue;
3563
3564 if (XE_IOCTL_DBG(xe, xe_vm_is_closed_or_banned(vm))) {
3565 err = -ENOENT;
3566 goto release_vm_lock;
3567 }
3568
3569 for (i = 0; i < args->num_binds; ++i) {
3570 u64 range = bind_ops[i].range;
3571 u64 addr = bind_ops[i].addr;
3572
3573 if (XE_IOCTL_DBG(xe, range > vm->size) ||
3574 XE_IOCTL_DBG(xe, addr > vm->size - range)) {
3575 err = -EINVAL;
3576 goto release_vm_lock;
3577 }
3578 }
3579
3580 if (args->num_binds) {
3581 bos = kvcalloc(args->num_binds, sizeof(*bos),
3582 GFP_KERNEL | __GFP_ACCOUNT |
3583 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
3584 if (!bos) {
3585 err = -ENOMEM;
3586 goto release_vm_lock;
3587 }
3588
3589 ops = kvcalloc(args->num_binds, sizeof(*ops),
3590 GFP_KERNEL | __GFP_ACCOUNT |
3591 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
3592 if (!ops) {
3593 err = -ENOMEM;
3594 goto release_vm_lock;
3595 }
3596 }
3597
3598 for (i = 0; i < args->num_binds; ++i) {
3599 struct drm_gem_object *gem_obj;
3600 u64 range = bind_ops[i].range;
3601 u64 addr = bind_ops[i].addr;
3602 u32 obj = bind_ops[i].obj;
3603 u64 obj_offset = bind_ops[i].obj_offset;
3604 u16 pat_index = bind_ops[i].pat_index;
3605 u32 op = bind_ops[i].op;
3606 u32 bind_flags = bind_ops[i].flags;
3607
3608 if (!obj)
3609 continue;
3610
3611 gem_obj = drm_gem_object_lookup(file, obj);
3612 if (XE_IOCTL_DBG(xe, !gem_obj)) {
3613 err = -ENOENT;
3614 goto put_obj;
3615 }
3616 bos[i] = gem_to_xe_bo(gem_obj);
3617
3618 err = xe_vm_bind_ioctl_validate_bo(xe, bos[i], addr, range,
3619 obj_offset, pat_index, op,
3620 bind_flags);
3621 if (err)
3622 goto put_obj;
3623 }
3624
3625 if (args->num_syncs) {
3626 syncs = kcalloc(args->num_syncs, sizeof(*syncs), GFP_KERNEL);
3627 if (!syncs) {
3628 err = -ENOMEM;
3629 goto put_obj;
3630 }
3631 }
3632
3633 syncs_user = u64_to_user_ptr(args->syncs);
3634 for (num_syncs = 0; num_syncs < args->num_syncs; num_syncs++) {
3635 err = xe_sync_entry_parse(xe, xef, &syncs[num_syncs],
3636 &syncs_user[num_syncs],
3637 (xe_vm_in_lr_mode(vm) ?
3638 SYNC_PARSE_FLAG_LR_MODE : 0) |
3639 (!args->num_binds ?
3640 SYNC_PARSE_FLAG_DISALLOW_USER_FENCE : 0));
3641 if (err)
3642 goto free_syncs;
3643
3644 if (xe_sync_is_ufence(&syncs[num_syncs]))
3645 num_ufence++;
3646 }
3647
3648 if (XE_IOCTL_DBG(xe, num_ufence > 1)) {
3649 err = -EINVAL;
3650 goto free_syncs;
3651 }
3652
3653 if (!args->num_binds) {
3654 err = -ENODATA;
3655 goto free_syncs;
3656 }
3657
3658 xe_vma_ops_init(&vops, vm, q, syncs, num_syncs);
3659 for (i = 0; i < args->num_binds; ++i) {
3660 u64 range = bind_ops[i].range;
3661 u64 addr = bind_ops[i].addr;
3662 u32 op = bind_ops[i].op;
3663 u32 flags = bind_ops[i].flags;
3664 u64 obj_offset = bind_ops[i].obj_offset;
3665 u32 prefetch_region = bind_ops[i].prefetch_mem_region_instance;
3666 u16 pat_index = bind_ops[i].pat_index;
3667
3668 ops[i] = vm_bind_ioctl_ops_create(vm, &vops, bos[i], obj_offset,
3669 addr, range, op, flags,
3670 prefetch_region, pat_index);
3671 if (IS_ERR(ops[i])) {
3672 err = PTR_ERR(ops[i]);
3673 ops[i] = NULL;
3674 goto unwind_ops;
3675 }
3676
3677 err = vm_bind_ioctl_ops_parse(vm, ops[i], &vops);
3678 if (err)
3679 goto unwind_ops;
3680
3681 #ifdef TEST_VM_OPS_ERROR
3682 if (flags & FORCE_OP_ERROR) {
3683 vops.inject_error = true;
3684 vm->xe->vm_inject_error_position =
3685 (vm->xe->vm_inject_error_position + 1) %
3686 FORCE_OP_ERROR_COUNT;
3687 }
3688 #endif
3689 }
3690
3691 /* Nothing to do */
3692 if (list_empty(&vops.list)) {
3693 err = -ENODATA;
3694 goto unwind_ops;
3695 }
3696
3697 err = xe_vma_ops_alloc(&vops, args->num_binds > 1);
3698 if (err)
3699 goto unwind_ops;
3700
3701 err = vm_bind_ioctl_ops_prefetch_ranges(vm, &vops);
3702 if (err)
3703 goto unwind_ops;
3704
3705 fence = vm_bind_ioctl_ops_execute(vm, &vops);
3706 if (IS_ERR(fence))
3707 err = PTR_ERR(fence);
3708 else
3709 dma_fence_put(fence);
3710
3711 unwind_ops:
3712 if (err && err != -ENODATA)
3713 vm_bind_ioctl_ops_unwind(vm, ops, args->num_binds);
3714 xe_vma_ops_fini(&vops);
3715 for (i = args->num_binds - 1; i >= 0; --i)
3716 if (ops[i])
3717 drm_gpuva_ops_free(&vm->gpuvm, ops[i]);
3718 free_syncs:
3719 if (err == -ENODATA)
3720 err = vm_bind_ioctl_signal_fences(vm, q, syncs, num_syncs);
3721 while (num_syncs--)
3722 xe_sync_entry_cleanup(&syncs[num_syncs]);
3723
3724 kfree(syncs);
3725 put_obj:
3726 for (i = 0; i < args->num_binds; ++i)
3727 xe_bo_put(bos[i]);
3728 release_vm_lock:
3729 up_write(&vm->lock);
3730 put_exec_queue:
3731 if (q)
3732 xe_exec_queue_put(q);
3733 put_vm:
3734 xe_vm_put(vm);
3735 kvfree(bos);
3736 kvfree(ops);
3737 if (args->num_binds > 1)
3738 kvfree(bind_ops);
3739 return err;
3740 }
3741
3742 /**
3743 * xe_vm_bind_kernel_bo - bind a kernel BO to a VM
3744 * @vm: VM to bind the BO to
3745 * @bo: BO to bind
3746 * @q: exec queue to use for the bind (optional)
3747 * @addr: address at which to bind the BO
3748 * @cache_lvl: PAT cache level to use
3749 *
3750 * Execute a VM bind map operation on a kernel-owned BO to bind it into a
3751 * kernel-owned VM.
3752 *
3753 * Returns a dma_fence to track the binding completion if the job to do so was
3754 * successfully submitted, an error pointer otherwise.
3755 */
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)3756 struct dma_fence *xe_vm_bind_kernel_bo(struct xe_vm *vm, struct xe_bo *bo,
3757 struct xe_exec_queue *q, u64 addr,
3758 enum xe_cache_level cache_lvl)
3759 {
3760 struct xe_vma_ops vops;
3761 struct drm_gpuva_ops *ops = NULL;
3762 struct dma_fence *fence;
3763 int err;
3764
3765 xe_bo_get(bo);
3766 xe_vm_get(vm);
3767 if (q)
3768 xe_exec_queue_get(q);
3769
3770 down_write(&vm->lock);
3771
3772 xe_vma_ops_init(&vops, vm, q, NULL, 0);
3773
3774 ops = vm_bind_ioctl_ops_create(vm, &vops, bo, 0, addr, xe_bo_size(bo),
3775 DRM_XE_VM_BIND_OP_MAP, 0, 0,
3776 vm->xe->pat.idx[cache_lvl]);
3777 if (IS_ERR(ops)) {
3778 err = PTR_ERR(ops);
3779 goto release_vm_lock;
3780 }
3781
3782 err = vm_bind_ioctl_ops_parse(vm, ops, &vops);
3783 if (err)
3784 goto release_vm_lock;
3785
3786 xe_assert(vm->xe, !list_empty(&vops.list));
3787
3788 err = xe_vma_ops_alloc(&vops, false);
3789 if (err)
3790 goto unwind_ops;
3791
3792 fence = vm_bind_ioctl_ops_execute(vm, &vops);
3793 if (IS_ERR(fence))
3794 err = PTR_ERR(fence);
3795
3796 unwind_ops:
3797 if (err && err != -ENODATA)
3798 vm_bind_ioctl_ops_unwind(vm, &ops, 1);
3799
3800 xe_vma_ops_fini(&vops);
3801 drm_gpuva_ops_free(&vm->gpuvm, ops);
3802
3803 release_vm_lock:
3804 up_write(&vm->lock);
3805
3806 if (q)
3807 xe_exec_queue_put(q);
3808 xe_vm_put(vm);
3809 xe_bo_put(bo);
3810
3811 if (err)
3812 fence = ERR_PTR(err);
3813
3814 return fence;
3815 }
3816
3817 /**
3818 * xe_vm_lock() - Lock the vm's dma_resv object
3819 * @vm: The struct xe_vm whose lock is to be locked
3820 * @intr: Whether to perform any wait interruptible
3821 *
3822 * Return: 0 on success, -EINTR if @intr is true and the wait for a
3823 * contended lock was interrupted. If @intr is false, the function
3824 * always returns 0.
3825 */
xe_vm_lock(struct xe_vm * vm,bool intr)3826 int xe_vm_lock(struct xe_vm *vm, bool intr)
3827 {
3828 if (intr)
3829 return dma_resv_lock_interruptible(xe_vm_resv(vm), NULL);
3830
3831 return dma_resv_lock(xe_vm_resv(vm), NULL);
3832 }
3833
3834 /**
3835 * xe_vm_unlock() - Unlock the vm's dma_resv object
3836 * @vm: The struct xe_vm whose lock is to be released.
3837 *
3838 * Unlock a buffer object lock that was locked by xe_vm_lock().
3839 */
xe_vm_unlock(struct xe_vm * vm)3840 void xe_vm_unlock(struct xe_vm *vm)
3841 {
3842 dma_resv_unlock(xe_vm_resv(vm));
3843 }
3844
3845 /**
3846 * xe_vm_range_tilemask_tlb_invalidation - Issue a TLB invalidation on this tilemask for an
3847 * address range
3848 * @vm: The VM
3849 * @start: start address
3850 * @end: end address
3851 * @tile_mask: mask for which gt's issue tlb invalidation
3852 *
3853 * Issue a range based TLB invalidation for gt's in tilemask
3854 *
3855 * Returns 0 for success, negative error code otherwise.
3856 */
xe_vm_range_tilemask_tlb_invalidation(struct xe_vm * vm,u64 start,u64 end,u8 tile_mask)3857 int xe_vm_range_tilemask_tlb_invalidation(struct xe_vm *vm, u64 start,
3858 u64 end, u8 tile_mask)
3859 {
3860 struct xe_gt_tlb_invalidation_fence fence[XE_MAX_TILES_PER_DEVICE * XE_MAX_GT_PER_TILE];
3861 struct xe_tile *tile;
3862 u32 fence_id = 0;
3863 u8 id;
3864 int err;
3865
3866 if (!tile_mask)
3867 return 0;
3868
3869 for_each_tile(tile, vm->xe, id) {
3870 if (tile_mask & BIT(id)) {
3871 xe_gt_tlb_invalidation_fence_init(tile->primary_gt,
3872 &fence[fence_id], true);
3873
3874 err = xe_gt_tlb_invalidation_range(tile->primary_gt,
3875 &fence[fence_id],
3876 start,
3877 end,
3878 vm->usm.asid);
3879 if (err)
3880 goto wait;
3881 ++fence_id;
3882
3883 if (!tile->media_gt)
3884 continue;
3885
3886 xe_gt_tlb_invalidation_fence_init(tile->media_gt,
3887 &fence[fence_id], true);
3888
3889 err = xe_gt_tlb_invalidation_range(tile->media_gt,
3890 &fence[fence_id],
3891 start,
3892 end,
3893 vm->usm.asid);
3894 if (err)
3895 goto wait;
3896 ++fence_id;
3897 }
3898 }
3899
3900 wait:
3901 for (id = 0; id < fence_id; ++id)
3902 xe_gt_tlb_invalidation_fence_wait(&fence[id]);
3903
3904 return err;
3905 }
3906
3907 /**
3908 * xe_vm_invalidate_vma - invalidate GPU mappings for VMA without a lock
3909 * @vma: VMA to invalidate
3910 *
3911 * Walks a list of page tables leaves which it memset the entries owned by this
3912 * VMA to zero, invalidates the TLBs, and block until TLBs invalidation is
3913 * complete.
3914 *
3915 * Returns 0 for success, negative error code otherwise.
3916 */
xe_vm_invalidate_vma(struct xe_vma * vma)3917 int xe_vm_invalidate_vma(struct xe_vma *vma)
3918 {
3919 struct xe_device *xe = xe_vma_vm(vma)->xe;
3920 struct xe_vm *vm = xe_vma_vm(vma);
3921 struct xe_tile *tile;
3922 u8 tile_mask = 0;
3923 int ret = 0;
3924 u8 id;
3925
3926 xe_assert(xe, !xe_vma_is_null(vma));
3927 xe_assert(xe, !xe_vma_is_cpu_addr_mirror(vma));
3928 trace_xe_vma_invalidate(vma);
3929
3930 vm_dbg(&vm->xe->drm,
3931 "INVALIDATE: addr=0x%016llx, range=0x%016llx",
3932 xe_vma_start(vma), xe_vma_size(vma));
3933
3934 /*
3935 * Check that we don't race with page-table updates, tile_invalidated
3936 * update is safe
3937 */
3938 if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
3939 if (xe_vma_is_userptr(vma)) {
3940 lockdep_assert(lockdep_is_held_type(&vm->userptr.notifier_lock, 0) ||
3941 (lockdep_is_held_type(&vm->userptr.notifier_lock, 1) &&
3942 lockdep_is_held(&xe_vm_resv(vm)->lock.base)));
3943
3944 WARN_ON_ONCE(!mmu_interval_check_retry
3945 (&to_userptr_vma(vma)->userptr.notifier,
3946 to_userptr_vma(vma)->userptr.notifier_seq));
3947 WARN_ON_ONCE(!dma_resv_test_signaled(xe_vm_resv(vm),
3948 DMA_RESV_USAGE_BOOKKEEP));
3949
3950 } else {
3951 xe_bo_assert_held(xe_vma_bo(vma));
3952 }
3953 }
3954
3955 for_each_tile(tile, xe, id)
3956 if (xe_pt_zap_ptes(tile, vma))
3957 tile_mask |= BIT(id);
3958
3959 xe_device_wmb(xe);
3960
3961 ret = xe_vm_range_tilemask_tlb_invalidation(xe_vma_vm(vma), xe_vma_start(vma),
3962 xe_vma_end(vma), tile_mask);
3963
3964 /* WRITE_ONCE pairs with READ_ONCE in xe_vm_has_valid_gpu_mapping() */
3965 WRITE_ONCE(vma->tile_invalidated, vma->tile_mask);
3966
3967 return ret;
3968 }
3969
xe_vm_validate_protected(struct xe_vm * vm)3970 int xe_vm_validate_protected(struct xe_vm *vm)
3971 {
3972 struct drm_gpuva *gpuva;
3973 int err = 0;
3974
3975 if (!vm)
3976 return -ENODEV;
3977
3978 mutex_lock(&vm->snap_mutex);
3979
3980 drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
3981 struct xe_vma *vma = gpuva_to_vma(gpuva);
3982 struct xe_bo *bo = vma->gpuva.gem.obj ?
3983 gem_to_xe_bo(vma->gpuva.gem.obj) : NULL;
3984
3985 if (!bo)
3986 continue;
3987
3988 if (xe_bo_is_protected(bo)) {
3989 err = xe_pxp_bo_key_check(vm->xe->pxp, bo);
3990 if (err)
3991 break;
3992 }
3993 }
3994
3995 mutex_unlock(&vm->snap_mutex);
3996 return err;
3997 }
3998
3999 struct xe_vm_snapshot {
4000 unsigned long num_snaps;
4001 struct {
4002 u64 ofs, bo_ofs;
4003 unsigned long len;
4004 struct xe_bo *bo;
4005 void *data;
4006 struct mm_struct *mm;
4007 } snap[];
4008 };
4009
xe_vm_snapshot_capture(struct xe_vm * vm)4010 struct xe_vm_snapshot *xe_vm_snapshot_capture(struct xe_vm *vm)
4011 {
4012 unsigned long num_snaps = 0, i;
4013 struct xe_vm_snapshot *snap = NULL;
4014 struct drm_gpuva *gpuva;
4015
4016 if (!vm)
4017 return NULL;
4018
4019 mutex_lock(&vm->snap_mutex);
4020 drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
4021 if (gpuva->flags & XE_VMA_DUMPABLE)
4022 num_snaps++;
4023 }
4024
4025 if (num_snaps)
4026 snap = kvzalloc(offsetof(struct xe_vm_snapshot, snap[num_snaps]), GFP_NOWAIT);
4027 if (!snap) {
4028 snap = num_snaps ? ERR_PTR(-ENOMEM) : ERR_PTR(-ENODEV);
4029 goto out_unlock;
4030 }
4031
4032 snap->num_snaps = num_snaps;
4033 i = 0;
4034 drm_gpuvm_for_each_va(gpuva, &vm->gpuvm) {
4035 struct xe_vma *vma = gpuva_to_vma(gpuva);
4036 struct xe_bo *bo = vma->gpuva.gem.obj ?
4037 gem_to_xe_bo(vma->gpuva.gem.obj) : NULL;
4038
4039 if (!(gpuva->flags & XE_VMA_DUMPABLE))
4040 continue;
4041
4042 snap->snap[i].ofs = xe_vma_start(vma);
4043 snap->snap[i].len = xe_vma_size(vma);
4044 if (bo) {
4045 snap->snap[i].bo = xe_bo_get(bo);
4046 snap->snap[i].bo_ofs = xe_vma_bo_offset(vma);
4047 } else if (xe_vma_is_userptr(vma)) {
4048 struct mm_struct *mm =
4049 to_userptr_vma(vma)->userptr.notifier.mm;
4050
4051 if (mmget_not_zero(mm))
4052 snap->snap[i].mm = mm;
4053 else
4054 snap->snap[i].data = ERR_PTR(-EFAULT);
4055
4056 snap->snap[i].bo_ofs = xe_vma_userptr(vma);
4057 } else {
4058 snap->snap[i].data = ERR_PTR(-ENOENT);
4059 }
4060 i++;
4061 }
4062
4063 out_unlock:
4064 mutex_unlock(&vm->snap_mutex);
4065 return snap;
4066 }
4067
xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot * snap)4068 void xe_vm_snapshot_capture_delayed(struct xe_vm_snapshot *snap)
4069 {
4070 if (IS_ERR_OR_NULL(snap))
4071 return;
4072
4073 for (int i = 0; i < snap->num_snaps; i++) {
4074 struct xe_bo *bo = snap->snap[i].bo;
4075 int err;
4076
4077 if (IS_ERR(snap->snap[i].data))
4078 continue;
4079
4080 snap->snap[i].data = kvmalloc(snap->snap[i].len, GFP_USER);
4081 if (!snap->snap[i].data) {
4082 snap->snap[i].data = ERR_PTR(-ENOMEM);
4083 goto cleanup_bo;
4084 }
4085
4086 if (bo) {
4087 err = xe_bo_read(bo, snap->snap[i].bo_ofs,
4088 snap->snap[i].data, snap->snap[i].len);
4089 } else {
4090 void __user *userptr = (void __user *)(size_t)snap->snap[i].bo_ofs;
4091
4092 kthread_use_mm(snap->snap[i].mm);
4093 if (!copy_from_user(snap->snap[i].data, userptr, snap->snap[i].len))
4094 err = 0;
4095 else
4096 err = -EFAULT;
4097 kthread_unuse_mm(snap->snap[i].mm);
4098
4099 mmput(snap->snap[i].mm);
4100 snap->snap[i].mm = NULL;
4101 }
4102
4103 if (err) {
4104 kvfree(snap->snap[i].data);
4105 snap->snap[i].data = ERR_PTR(err);
4106 }
4107
4108 cleanup_bo:
4109 xe_bo_put(bo);
4110 snap->snap[i].bo = NULL;
4111 }
4112 }
4113
xe_vm_snapshot_print(struct xe_vm_snapshot * snap,struct drm_printer * p)4114 void xe_vm_snapshot_print(struct xe_vm_snapshot *snap, struct drm_printer *p)
4115 {
4116 unsigned long i, j;
4117
4118 if (IS_ERR_OR_NULL(snap)) {
4119 drm_printf(p, "[0].error: %li\n", PTR_ERR(snap));
4120 return;
4121 }
4122
4123 for (i = 0; i < snap->num_snaps; i++) {
4124 drm_printf(p, "[%llx].length: 0x%lx\n", snap->snap[i].ofs, snap->snap[i].len);
4125
4126 if (IS_ERR(snap->snap[i].data)) {
4127 drm_printf(p, "[%llx].error: %li\n", snap->snap[i].ofs,
4128 PTR_ERR(snap->snap[i].data));
4129 continue;
4130 }
4131
4132 drm_printf(p, "[%llx].data: ", snap->snap[i].ofs);
4133
4134 for (j = 0; j < snap->snap[i].len; j += sizeof(u32)) {
4135 u32 *val = snap->snap[i].data + j;
4136 char dumped[ASCII85_BUFSZ];
4137
4138 drm_puts(p, ascii85_encode(*val, dumped));
4139 }
4140
4141 drm_puts(p, "\n");
4142
4143 if (drm_coredump_printer_is_full(p))
4144 return;
4145 }
4146 }
4147
xe_vm_snapshot_free(struct xe_vm_snapshot * snap)4148 void xe_vm_snapshot_free(struct xe_vm_snapshot *snap)
4149 {
4150 unsigned long i;
4151
4152 if (IS_ERR_OR_NULL(snap))
4153 return;
4154
4155 for (i = 0; i < snap->num_snaps; i++) {
4156 if (!IS_ERR(snap->snap[i].data))
4157 kvfree(snap->snap[i].data);
4158 xe_bo_put(snap->snap[i].bo);
4159 if (snap->snap[i].mm)
4160 mmput(snap->snap[i].mm);
4161 }
4162 kvfree(snap);
4163 }
4164