1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
6 #include "xe_gt_pagefault.h"
8 #include <linux/bitfield.h>
9 #include <linux/circ_buf.h>
11 #include <drm/drm_exec.h>
12 #include <drm/drm_managed.h>
13 #include <drm/ttm/ttm_execbuf_util.h>
15 #include "abi/guc_actions_abi.h"
18 #include "xe_gt_tlb_invalidation.h"
20 #include "xe_guc_ct.h"
21 #include "xe_migrate.h"
36 u8 fault_unsuccessful;
42 ACCESS_TYPE_WRITE = 1,
43 ACCESS_TYPE_ATOMIC = 2,
44 ACCESS_TYPE_RESERVED = 3,
49 WRITE_ACCESS_VIOLATION = 1,
50 ATOMIC_ACCESS_VIOLATION = 2,
64 static bool access_is_atomic(enum access_type access_type)
66 return access_type == ACCESS_TYPE_ATOMIC;
69 static bool vma_is_valid(struct xe_tile *tile, struct xe_vma *vma)
71 return BIT(tile->id) & vma->tile_present &&
72 !(BIT(tile->id) & vma->tile_invalidated);
75 static bool vma_matches(struct xe_vma *vma, u64 page_addr)
77 if (page_addr > xe_vma_end(vma) - 1 ||
78 page_addr + SZ_4K - 1 < xe_vma_start(vma))
84 static struct xe_vma *lookup_vma(struct xe_vm *vm, u64 page_addr)
86 struct xe_vma *vma = NULL;
88 if (vm->usm.last_fault_vma) { /* Fast lookup */
89 if (vma_matches(vm->usm.last_fault_vma, page_addr))
90 vma = vm->usm.last_fault_vma;
93 vma = xe_vm_find_overlapping_vma(vm, page_addr, SZ_4K);
98 static int xe_pf_begin(struct drm_exec *exec, struct xe_vma *vma,
99 bool atomic, unsigned int id)
101 struct xe_bo *bo = xe_vma_bo(vma);
102 struct xe_vm *vm = xe_vma_vm(vma);
105 err = xe_vm_lock_vma(exec, vma);
109 if (atomic && IS_DGFX(vm->xe)) {
110 if (xe_vma_is_userptr(vma)) {
115 /* Migrate to VRAM, move should invalidate the VMA first */
116 err = xe_bo_migrate(bo, XE_PL_VRAM0 + id);
120 /* Create backing store if needed */
121 err = xe_bo_validate(bo, vm, true);
129 static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf)
131 struct xe_device *xe = gt_to_xe(gt);
132 struct xe_tile *tile = gt_to_tile(gt);
133 struct drm_exec exec;
135 struct xe_vma *vma = NULL;
136 struct dma_fence *fence;
141 /* SW isn't expected to handle TRTT faults */
146 mutex_lock(&xe->usm.lock);
147 vm = xa_load(&xe->usm.asid_to_vm, pf->asid);
148 if (vm && xe_vm_in_fault_mode(vm))
152 mutex_unlock(&xe->usm.lock);
158 * TODO: Avoid exclusive lock if VM doesn't have userptrs, or
159 * start out read-locked?
161 down_write(&vm->lock);
163 vma = lookup_vma(vm, pf->page_addr);
169 if (!xe_vma_is_userptr(vma) ||
170 !xe_vma_userptr_check_repin(to_userptr_vma(vma))) {
171 downgrade_write(&vm->lock);
172 write_locked = false;
175 trace_xe_vma_pagefault(vma);
177 atomic = access_is_atomic(pf->access_type);
179 /* Check if VMA is valid */
180 if (vma_is_valid(tile, vma) && !atomic)
183 /* TODO: Validate fault */
185 if (xe_vma_is_userptr(vma) && write_locked) {
186 struct xe_userptr_vma *uvma = to_userptr_vma(vma);
188 spin_lock(&vm->userptr.invalidated_lock);
189 list_del_init(&uvma->userptr.invalidate_link);
190 spin_unlock(&vm->userptr.invalidated_lock);
192 ret = xe_vma_userptr_pin_pages(uvma);
196 downgrade_write(&vm->lock);
197 write_locked = false;
200 /* Lock VM and BOs dma-resv */
201 drm_exec_init(&exec, 0, 0);
202 drm_exec_until_all_locked(&exec) {
203 ret = xe_pf_begin(&exec, vma, atomic, tile->id);
204 drm_exec_retry_on_contention(&exec);
206 goto unlock_dma_resv;
209 /* Bind VMA only to the GT that has faulted */
210 trace_xe_vma_pf_bind(vma);
211 fence = __xe_pt_bind_vma(tile, vma, xe_tile_migrate_engine(tile), NULL, 0,
212 vma->tile_present & BIT(tile->id));
214 ret = PTR_ERR(fence);
215 goto unlock_dma_resv;
219 * XXX: Should we drop the lock before waiting? This only helps if doing
220 * GPU binds which is currently only done if we have to wait for more
221 * than 10ms on a move.
223 dma_fence_wait(fence, false);
224 dma_fence_put(fence);
226 if (xe_vma_is_userptr(vma))
227 ret = xe_vma_userptr_check_repin(to_userptr_vma(vma));
228 vma->tile_invalidated &= ~BIT(tile->id);
231 drm_exec_fini(&exec);
234 vm->usm.last_fault_vma = vma;
243 ret = xe_gt_tlb_invalidation_vma(gt, NULL, vma);
252 static int send_pagefault_reply(struct xe_guc *guc,
253 struct xe_guc_pagefault_reply *reply)
256 XE_GUC_ACTION_PAGE_FAULT_RES_DESC,
261 return xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
264 static void print_pagefault(struct xe_device *xe, struct pagefault *pf)
266 drm_dbg(&xe->drm, "\n\tASID: %d\n"
269 "\tFaulted Address: 0x%08x%08x\n"
273 "\tEngineClass: %d\n"
274 "\tEngineInstance: %d\n",
275 pf->asid, pf->vfid, pf->pdata, upper_32_bits(pf->page_addr),
276 lower_32_bits(pf->page_addr),
277 pf->fault_type, pf->access_type, pf->fault_level,
278 pf->engine_class, pf->engine_instance);
281 #define PF_MSG_LEN_DW 4
283 static bool get_pagefault(struct pf_queue *pf_queue, struct pagefault *pf)
285 const struct xe_guc_pagefault_desc *desc;
288 spin_lock_irq(&pf_queue->lock);
289 if (pf_queue->tail != pf_queue->head) {
290 desc = (const struct xe_guc_pagefault_desc *)
291 (pf_queue->data + pf_queue->tail);
293 pf->fault_level = FIELD_GET(PFD_FAULT_LEVEL, desc->dw0);
294 pf->trva_fault = FIELD_GET(XE2_PFD_TRVA_FAULT, desc->dw0);
295 pf->engine_class = FIELD_GET(PFD_ENG_CLASS, desc->dw0);
296 pf->engine_instance = FIELD_GET(PFD_ENG_INSTANCE, desc->dw0);
297 pf->pdata = FIELD_GET(PFD_PDATA_HI, desc->dw1) <<
299 pf->pdata |= FIELD_GET(PFD_PDATA_LO, desc->dw0);
300 pf->asid = FIELD_GET(PFD_ASID, desc->dw1);
301 pf->vfid = FIELD_GET(PFD_VFID, desc->dw2);
302 pf->access_type = FIELD_GET(PFD_ACCESS_TYPE, desc->dw2);
303 pf->fault_type = FIELD_GET(PFD_FAULT_TYPE, desc->dw2);
304 pf->page_addr = (u64)(FIELD_GET(PFD_VIRTUAL_ADDR_HI, desc->dw3)) <<
305 PFD_VIRTUAL_ADDR_HI_SHIFT;
306 pf->page_addr |= FIELD_GET(PFD_VIRTUAL_ADDR_LO, desc->dw2) <<
307 PFD_VIRTUAL_ADDR_LO_SHIFT;
309 pf_queue->tail = (pf_queue->tail + PF_MSG_LEN_DW) %
313 spin_unlock_irq(&pf_queue->lock);
318 static bool pf_queue_full(struct pf_queue *pf_queue)
320 lockdep_assert_held(&pf_queue->lock);
322 return CIRC_SPACE(pf_queue->head, pf_queue->tail, PF_QUEUE_NUM_DW) <=
326 int xe_guc_pagefault_handler(struct xe_guc *guc, u32 *msg, u32 len)
328 struct xe_gt *gt = guc_to_gt(guc);
329 struct xe_device *xe = gt_to_xe(gt);
330 struct pf_queue *pf_queue;
336 * The below logic doesn't work unless PF_QUEUE_NUM_DW % PF_MSG_LEN_DW == 0
338 BUILD_BUG_ON(PF_QUEUE_NUM_DW % PF_MSG_LEN_DW);
340 if (unlikely(len != PF_MSG_LEN_DW))
343 asid = FIELD_GET(PFD_ASID, msg[1]);
344 pf_queue = gt->usm.pf_queue + (asid % NUM_PF_QUEUE);
346 spin_lock_irqsave(&pf_queue->lock, flags);
347 full = pf_queue_full(pf_queue);
349 memcpy(pf_queue->data + pf_queue->head, msg, len * sizeof(u32));
350 pf_queue->head = (pf_queue->head + len) % PF_QUEUE_NUM_DW;
351 queue_work(gt->usm.pf_wq, &pf_queue->worker);
353 drm_warn(&xe->drm, "PF Queue full, shouldn't be possible");
355 spin_unlock_irqrestore(&pf_queue->lock, flags);
357 return full ? -ENOSPC : 0;
360 #define USM_QUEUE_MAX_RUNTIME_MS 20
362 static void pf_queue_work_func(struct work_struct *w)
364 struct pf_queue *pf_queue = container_of(w, struct pf_queue, worker);
365 struct xe_gt *gt = pf_queue->gt;
366 struct xe_device *xe = gt_to_xe(gt);
367 struct xe_guc_pagefault_reply reply = {};
368 struct pagefault pf = {};
369 unsigned long threshold;
372 threshold = jiffies + msecs_to_jiffies(USM_QUEUE_MAX_RUNTIME_MS);
374 while (get_pagefault(pf_queue, &pf)) {
375 ret = handle_pagefault(gt, &pf);
377 print_pagefault(xe, &pf);
378 pf.fault_unsuccessful = 1;
379 drm_dbg(&xe->drm, "Fault response: Unsuccessful %d\n", ret);
382 reply.dw0 = FIELD_PREP(PFR_VALID, 1) |
383 FIELD_PREP(PFR_SUCCESS, pf.fault_unsuccessful) |
384 FIELD_PREP(PFR_REPLY, PFR_ACCESS) |
385 FIELD_PREP(PFR_DESC_TYPE, FAULT_RESPONSE_DESC) |
386 FIELD_PREP(PFR_ASID, pf.asid);
388 reply.dw1 = FIELD_PREP(PFR_VFID, pf.vfid) |
389 FIELD_PREP(PFR_ENG_INSTANCE, pf.engine_instance) |
390 FIELD_PREP(PFR_ENG_CLASS, pf.engine_class) |
391 FIELD_PREP(PFR_PDATA, pf.pdata);
393 send_pagefault_reply(>->uc.guc, &reply);
395 if (time_after(jiffies, threshold) &&
396 pf_queue->tail != pf_queue->head) {
397 queue_work(gt->usm.pf_wq, w);
403 static void acc_queue_work_func(struct work_struct *w);
405 int xe_gt_pagefault_init(struct xe_gt *gt)
407 struct xe_device *xe = gt_to_xe(gt);
410 if (!xe->info.has_usm)
413 for (i = 0; i < NUM_PF_QUEUE; ++i) {
414 gt->usm.pf_queue[i].gt = gt;
415 spin_lock_init(>->usm.pf_queue[i].lock);
416 INIT_WORK(>->usm.pf_queue[i].worker, pf_queue_work_func);
418 for (i = 0; i < NUM_ACC_QUEUE; ++i) {
419 gt->usm.acc_queue[i].gt = gt;
420 spin_lock_init(>->usm.acc_queue[i].lock);
421 INIT_WORK(>->usm.acc_queue[i].worker, acc_queue_work_func);
424 gt->usm.pf_wq = alloc_workqueue("xe_gt_page_fault_work_queue",
425 WQ_UNBOUND | WQ_HIGHPRI, NUM_PF_QUEUE);
429 gt->usm.acc_wq = alloc_workqueue("xe_gt_access_counter_work_queue",
430 WQ_UNBOUND | WQ_HIGHPRI,
438 void xe_gt_pagefault_reset(struct xe_gt *gt)
440 struct xe_device *xe = gt_to_xe(gt);
443 if (!xe->info.has_usm)
446 for (i = 0; i < NUM_PF_QUEUE; ++i) {
447 spin_lock_irq(>->usm.pf_queue[i].lock);
448 gt->usm.pf_queue[i].head = 0;
449 gt->usm.pf_queue[i].tail = 0;
450 spin_unlock_irq(>->usm.pf_queue[i].lock);
453 for (i = 0; i < NUM_ACC_QUEUE; ++i) {
454 spin_lock(>->usm.acc_queue[i].lock);
455 gt->usm.acc_queue[i].head = 0;
456 gt->usm.acc_queue[i].tail = 0;
457 spin_unlock(>->usm.acc_queue[i].lock);
461 static int granularity_in_byte(int val)
477 static int sub_granularity_in_byte(int val)
479 return (granularity_in_byte(val) / 32);
482 static void print_acc(struct xe_device *xe, struct acc *acc)
484 drm_warn(&xe->drm, "Access counter request:\n"
489 "\tGranularity: 0x%x KB Region/ %d KB sub-granularity\n"
490 "\tSub_Granularity Vector: 0x%08x\n"
491 "\tVA Range base: 0x%016llx\n",
492 acc->access_type ? "AC_NTFY_VAL" : "AC_TRIG_VAL",
493 acc->asid, acc->vfid, acc->engine_class, acc->engine_instance,
494 granularity_in_byte(acc->granularity) / SZ_1K,
495 sub_granularity_in_byte(acc->granularity) / SZ_1K,
496 acc->sub_granularity, acc->va_range_base);
499 static struct xe_vma *get_acc_vma(struct xe_vm *vm, struct acc *acc)
501 u64 page_va = acc->va_range_base + (ffs(acc->sub_granularity) - 1) *
502 sub_granularity_in_byte(acc->granularity);
504 return xe_vm_find_overlapping_vma(vm, page_va, SZ_4K);
507 static int handle_acc(struct xe_gt *gt, struct acc *acc)
509 struct xe_device *xe = gt_to_xe(gt);
510 struct xe_tile *tile = gt_to_tile(gt);
511 struct drm_exec exec;
516 /* We only support ACC_TRIGGER at the moment */
517 if (acc->access_type != ACC_TRIGGER)
521 mutex_lock(&xe->usm.lock);
522 vm = xa_load(&xe->usm.asid_to_vm, acc->asid);
525 mutex_unlock(&xe->usm.lock);
526 if (!vm || !xe_vm_in_fault_mode(vm))
529 down_read(&vm->lock);
532 vma = get_acc_vma(vm, acc);
538 trace_xe_vma_acc(vma);
540 /* Userptr or null can't be migrated, nothing to do */
541 if (xe_vma_has_no_bo(vma))
544 /* Lock VM and BOs dma-resv */
545 drm_exec_init(&exec, 0, 0);
546 drm_exec_until_all_locked(&exec) {
547 ret = xe_pf_begin(&exec, vma, true, tile->id);
548 drm_exec_retry_on_contention(&exec);
553 drm_exec_fini(&exec);
561 #define make_u64(hi__, low__) ((u64)(hi__) << 32 | (u64)(low__))
563 #define ACC_MSG_LEN_DW 4
565 static bool get_acc(struct acc_queue *acc_queue, struct acc *acc)
567 const struct xe_guc_acc_desc *desc;
570 spin_lock(&acc_queue->lock);
571 if (acc_queue->tail != acc_queue->head) {
572 desc = (const struct xe_guc_acc_desc *)
573 (acc_queue->data + acc_queue->tail);
575 acc->granularity = FIELD_GET(ACC_GRANULARITY, desc->dw2);
576 acc->sub_granularity = FIELD_GET(ACC_SUBG_HI, desc->dw1) << 31 |
577 FIELD_GET(ACC_SUBG_LO, desc->dw0);
578 acc->engine_class = FIELD_GET(ACC_ENG_CLASS, desc->dw1);
579 acc->engine_instance = FIELD_GET(ACC_ENG_INSTANCE, desc->dw1);
580 acc->asid = FIELD_GET(ACC_ASID, desc->dw1);
581 acc->vfid = FIELD_GET(ACC_VFID, desc->dw2);
582 acc->access_type = FIELD_GET(ACC_TYPE, desc->dw0);
583 acc->va_range_base = make_u64(desc->dw3 & ACC_VIRTUAL_ADDR_RANGE_HI,
584 desc->dw2 & ACC_VIRTUAL_ADDR_RANGE_LO);
586 acc_queue->tail = (acc_queue->tail + ACC_MSG_LEN_DW) %
590 spin_unlock(&acc_queue->lock);
595 static void acc_queue_work_func(struct work_struct *w)
597 struct acc_queue *acc_queue = container_of(w, struct acc_queue, worker);
598 struct xe_gt *gt = acc_queue->gt;
599 struct xe_device *xe = gt_to_xe(gt);
601 unsigned long threshold;
604 threshold = jiffies + msecs_to_jiffies(USM_QUEUE_MAX_RUNTIME_MS);
606 while (get_acc(acc_queue, &acc)) {
607 ret = handle_acc(gt, &acc);
610 drm_warn(&xe->drm, "ACC: Unsuccessful %d\n", ret);
613 if (time_after(jiffies, threshold) &&
614 acc_queue->tail != acc_queue->head) {
615 queue_work(gt->usm.acc_wq, w);
621 static bool acc_queue_full(struct acc_queue *acc_queue)
623 lockdep_assert_held(&acc_queue->lock);
625 return CIRC_SPACE(acc_queue->head, acc_queue->tail, ACC_QUEUE_NUM_DW) <=
629 int xe_guc_access_counter_notify_handler(struct xe_guc *guc, u32 *msg, u32 len)
631 struct xe_gt *gt = guc_to_gt(guc);
632 struct acc_queue *acc_queue;
637 * The below logic doesn't work unless ACC_QUEUE_NUM_DW % ACC_MSG_LEN_DW == 0
639 BUILD_BUG_ON(ACC_QUEUE_NUM_DW % ACC_MSG_LEN_DW);
641 if (unlikely(len != ACC_MSG_LEN_DW))
644 asid = FIELD_GET(ACC_ASID, msg[1]);
645 acc_queue = >->usm.acc_queue[asid % NUM_ACC_QUEUE];
647 spin_lock(&acc_queue->lock);
648 full = acc_queue_full(acc_queue);
650 memcpy(acc_queue->data + acc_queue->head, msg,
652 acc_queue->head = (acc_queue->head + len) % ACC_QUEUE_NUM_DW;
653 queue_work(gt->usm.acc_wq, &acc_queue->worker);
655 drm_warn(>_to_xe(gt)->drm, "ACC Queue full, dropping ACC");
657 spin_unlock(&acc_queue->lock);
659 return full ? -ENOSPC : 0;