xref: /linux/drivers/gpu/drm/xe/xe_pagefault.c (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2025 Intel Corporation
4  */
5 
6 #include <linux/circ_buf.h>
7 
8 #include <drm/drm_exec.h>
9 #include <drm/drm_managed.h>
10 
11 #include "xe_bo.h"
12 #include "xe_device.h"
13 #include "xe_gt_printk.h"
14 #include "xe_gt_types.h"
15 #include "xe_gt_stats.h"
16 #include "xe_hw_engine.h"
17 #include "xe_pagefault.h"
18 #include "xe_pagefault_types.h"
19 #include "xe_pm.h"
20 #include "xe_svm.h"
21 #include "xe_trace_bo.h"
22 #include "xe_vm.h"
23 
24 /**
25  * DOC: Xe page faults
26  *
27  * Xe page faults are handled in two layers. The producer layer interacts with
28  * hardware or firmware to receive and parse faults into struct xe_pagefault,
29  * then forwards them to the consumer. The consumer layer services the faults
30  * (e.g., memory migration, page table updates) and acknowledges the result back
31  * to the producer, which then forwards the results to the hardware or firmware.
32  * The consumer uses a page fault queue sized to absorb all potential faults and
33  * a multi-threaded worker to process them. Multiple producers are supported,
34  * with a single shared consumer.
35  *
36  * xe_pagefault.c implements the consumer layer.
37  */
38 
xe_pagefault_entry_size(void)39 static int xe_pagefault_entry_size(void)
40 {
41 	/*
42 	 * Power of two alignment is not a hardware requirement, rather a
43 	 * software restriction which makes the math for page fault queue
44 	 * management simplier.
45 	 */
46 	return roundup_pow_of_two(sizeof(struct xe_pagefault));
47 }
48 
xe_pagefault_begin(struct drm_exec * exec,struct xe_vma * vma,struct xe_vram_region * vram,bool need_vram_move)49 static int xe_pagefault_begin(struct drm_exec *exec, struct xe_vma *vma,
50 			      struct xe_vram_region *vram, bool need_vram_move)
51 {
52 	struct xe_bo *bo = xe_vma_bo(vma);
53 	struct xe_vm *vm = xe_vma_vm(vma);
54 	int err;
55 
56 	err = xe_vm_lock_vma(exec, vma);
57 	if (err)
58 		return err;
59 
60 	if (!bo)
61 		return 0;
62 
63 	/*
64 	 * Skip validate/migrate for DONTNEED/purged BOs - repopulating
65 	 * their pages would prevent the shrinker from reclaiming them.
66 	 * For non-scratch VMs there is no safe fallback so fail the fault.
67 	 * For scratch VMs let xe_vma_rebind() run normally; it will install
68 	 * scratch PTEs so the GPU gets safe zero reads instead of faulting.
69 	 */
70 	if (unlikely(xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo))) {
71 		if (!xe_vm_has_scratch(vm))
72 			return -EACCES;
73 		return 0;
74 	}
75 
76 	return need_vram_move ? xe_bo_migrate(bo, vram->placement, NULL, exec) :
77 		xe_bo_validate(bo, vm, true, exec);
78 }
79 
xe_pagefault_handle_vma(struct xe_gt * gt,struct xe_vma * vma,bool atomic)80 static int xe_pagefault_handle_vma(struct xe_gt *gt, struct xe_vma *vma,
81 				   bool atomic)
82 {
83 	struct xe_vm *vm = xe_vma_vm(vma);
84 	struct xe_tile *tile = gt_to_tile(gt);
85 	struct xe_validation_ctx ctx;
86 	struct drm_exec exec;
87 	struct dma_fence *fence;
88 	int err, needs_vram;
89 
90 	lockdep_assert_held_write(&vm->lock);
91 
92 	needs_vram = xe_vma_need_vram_for_atomic(vm->xe, vma, atomic);
93 	if (needs_vram < 0 || (needs_vram && xe_vma_is_userptr(vma)))
94 		return needs_vram < 0 ? needs_vram : -EACCES;
95 
96 	xe_gt_stats_incr(gt, XE_GT_STATS_ID_VMA_PAGEFAULT_COUNT, 1);
97 	xe_gt_stats_incr(gt, XE_GT_STATS_ID_VMA_PAGEFAULT_KB,
98 			 xe_vma_size(vma) / SZ_1K);
99 
100 	trace_xe_vma_pagefault(vma);
101 
102 	/* Check if VMA is valid, opportunistic check only */
103 	if (xe_vm_has_valid_gpu_mapping(tile, vma->tile_present,
104 					vma->tile_invalidated) && !atomic)
105 		return 0;
106 
107 retry_userptr:
108 	if (xe_vma_is_userptr(vma) &&
109 	    xe_vma_userptr_check_repin(to_userptr_vma(vma))) {
110 		struct xe_userptr_vma *uvma = to_userptr_vma(vma);
111 
112 		err = xe_vma_userptr_pin_pages(uvma);
113 		if (err)
114 			return err;
115 	}
116 
117 	/* Lock VM and BOs dma-resv */
118 	xe_validation_ctx_init(&ctx, &vm->xe->val, &exec, (struct xe_val_flags) {});
119 	drm_exec_until_all_locked(&exec) {
120 		err = xe_pagefault_begin(&exec, vma, tile->mem.vram,
121 					 needs_vram == 1);
122 		drm_exec_retry_on_contention(&exec);
123 		xe_validation_retry_on_oom(&ctx, &err);
124 		if (err)
125 			goto unlock_dma_resv;
126 
127 		/* Bind VMA only to the GT that has faulted */
128 		trace_xe_vma_pf_bind(vma);
129 		xe_vm_set_validation_exec(vm, &exec);
130 		fence = xe_vma_rebind(vm, vma, BIT(tile->id));
131 		xe_vm_set_validation_exec(vm, NULL);
132 		if (IS_ERR(fence)) {
133 			err = PTR_ERR(fence);
134 			xe_validation_retry_on_oom(&ctx, &err);
135 			goto unlock_dma_resv;
136 		}
137 	}
138 
139 	dma_fence_wait(fence, false);
140 	dma_fence_put(fence);
141 
142 unlock_dma_resv:
143 	xe_validation_ctx_fini(&ctx);
144 	if (err == -EAGAIN)
145 		goto retry_userptr;
146 
147 	return err;
148 }
149 
150 static bool
xe_pagefault_access_is_atomic(enum xe_pagefault_access_type access_type)151 xe_pagefault_access_is_atomic(enum xe_pagefault_access_type access_type)
152 {
153 	return (access_type & XE_PAGEFAULT_ACCESS_TYPE_MASK) == XE_PAGEFAULT_ACCESS_TYPE_ATOMIC;
154 }
155 
xe_pagefault_asid_to_vm(struct xe_device * xe,u32 asid)156 static struct xe_vm *xe_pagefault_asid_to_vm(struct xe_device *xe, u32 asid)
157 {
158 	struct xe_vm *vm;
159 
160 	down_read(&xe->usm.lock);
161 	vm = xa_load(&xe->usm.asid_to_vm, asid);
162 	if (vm && xe_vm_in_fault_mode(vm))
163 		xe_vm_get(vm);
164 	else
165 		vm = ERR_PTR(-EINVAL);
166 	up_read(&xe->usm.lock);
167 
168 	return vm;
169 }
170 
xe_pagefault_service(struct xe_pagefault * pf)171 static int xe_pagefault_service(struct xe_pagefault *pf)
172 {
173 	struct xe_gt *gt = pf->gt;
174 	struct xe_device *xe = gt_to_xe(gt);
175 	struct xe_vm *vm;
176 	struct xe_vma *vma = NULL;
177 	int err;
178 	bool atomic;
179 
180 	/* Producer flagged this fault to be nacked */
181 	if (pf->consumer.fault_type_level == XE_PAGEFAULT_TYPE_LEVEL_NACK)
182 		return -EFAULT;
183 
184 	vm = xe_pagefault_asid_to_vm(xe, pf->consumer.asid);
185 	if (IS_ERR(vm))
186 		return PTR_ERR(vm);
187 
188 	/*
189 	 * TODO: Change to read lock? Using write lock for simplicity.
190 	 */
191 	down_write(&vm->lock);
192 
193 	if (xe_vm_is_closed(vm)) {
194 		err = -ENOENT;
195 		goto unlock_vm;
196 	}
197 
198 	vma = xe_vm_find_vma_by_addr(vm, pf->consumer.page_addr);
199 	if (!vma) {
200 		err = -EINVAL;
201 		goto unlock_vm;
202 	}
203 
204 	if (xe_vma_read_only(vma) &&
205 	    pf->consumer.access_type != XE_PAGEFAULT_ACCESS_TYPE_READ) {
206 		err = -EPERM;
207 		goto unlock_vm;
208 	}
209 
210 	atomic = xe_pagefault_access_is_atomic(pf->consumer.access_type);
211 
212 	if (xe_vma_is_cpu_addr_mirror(vma))
213 		err = xe_svm_handle_pagefault(vm, vma, gt,
214 					      pf->consumer.page_addr, atomic);
215 	else
216 		err = xe_pagefault_handle_vma(gt, vma, atomic);
217 
218 unlock_vm:
219 	if (!err)
220 		vm->usm.last_fault_vma = vma;
221 	up_write(&vm->lock);
222 	xe_vm_put(vm);
223 
224 	return err;
225 }
226 
xe_pagefault_queue_pop(struct xe_pagefault_queue * pf_queue,struct xe_pagefault * pf)227 static bool xe_pagefault_queue_pop(struct xe_pagefault_queue *pf_queue,
228 				   struct xe_pagefault *pf)
229 {
230 	bool found_fault = false;
231 
232 	spin_lock_irq(&pf_queue->lock);
233 	if (pf_queue->tail != pf_queue->head) {
234 		memcpy(pf, pf_queue->data + pf_queue->tail, sizeof(*pf));
235 		pf_queue->tail = (pf_queue->tail + xe_pagefault_entry_size()) %
236 			pf_queue->size;
237 		found_fault = true;
238 	}
239 	spin_unlock_irq(&pf_queue->lock);
240 
241 	return found_fault;
242 }
243 
xe_pagefault_print(struct xe_pagefault * pf)244 static void xe_pagefault_print(struct xe_pagefault *pf)
245 {
246 	xe_gt_info(pf->gt, "\n\tASID: %d\n"
247 		   "\tFaulted Address: 0x%08x%08x\n"
248 		   "\tFaultType: %lu\n"
249 		   "\tAccessType: %lu\n"
250 		   "\tFaultLevel: %lu\n"
251 		   "\tEngineClass: %d %s\n"
252 		   "\tEngineInstance: %d\n",
253 		   pf->consumer.asid,
254 		   upper_32_bits(pf->consumer.page_addr),
255 		   lower_32_bits(pf->consumer.page_addr),
256 		   FIELD_GET(XE_PAGEFAULT_TYPE_MASK,
257 			     pf->consumer.fault_type_level),
258 		   FIELD_GET(XE_PAGEFAULT_ACCESS_TYPE_MASK,
259 			     pf->consumer.access_type),
260 		   FIELD_GET(XE_PAGEFAULT_LEVEL_MASK,
261 			     pf->consumer.fault_type_level),
262 		   pf->consumer.engine_class,
263 		   xe_hw_engine_class_to_str(pf->consumer.engine_class),
264 		   pf->consumer.engine_instance);
265 }
266 
xe_pagefault_save_to_vm(struct xe_device * xe,struct xe_pagefault * pf)267 static void xe_pagefault_save_to_vm(struct xe_device *xe, struct xe_pagefault *pf)
268 {
269 	struct xe_vm *vm;
270 
271 	/*
272 	 * Pagefault may be asociated to VM that is not in fault mode.
273 	 * Perform asid_to_vm behavior, except if VM is not in fault
274 	 * mode, return VM anyways.
275 	 */
276 	down_read(&xe->usm.lock);
277 	vm = xa_load(&xe->usm.asid_to_vm, pf->consumer.asid);
278 	if (vm)
279 		xe_vm_get(vm);
280 	else
281 		vm = ERR_PTR(-EINVAL);
282 	up_read(&xe->usm.lock);
283 
284 	if (IS_ERR(vm))
285 		return;
286 
287 	xe_vm_add_fault_entry_pf(vm, pf);
288 
289 	xe_vm_put(vm);
290 }
291 
xe_pagefault_queue_work(struct work_struct * w)292 static void xe_pagefault_queue_work(struct work_struct *w)
293 {
294 	struct xe_pagefault_queue *pf_queue =
295 		container_of(w, typeof(*pf_queue), worker);
296 	struct xe_device *xe = pf_queue->xe;
297 	struct xe_pagefault pf;
298 	unsigned long threshold;
299 
300 	/*
301 	 * A live VM holds a PM reference, but a torn-down VM does not.
302 	 * Guard the entire worker loop to safely drain stale faults and
303 	 * prevent autosuspends from desyncing batched CT flushes.
304 	 */
305 	guard(xe_pm_runtime)(xe);
306 
307 #define USM_QUEUE_MAX_RUNTIME_MS      20
308 	threshold = jiffies + msecs_to_jiffies(USM_QUEUE_MAX_RUNTIME_MS);
309 
310 	while (xe_pagefault_queue_pop(pf_queue, &pf)) {
311 		int err;
312 
313 		if (!pf.gt)	/* Fault squashed during reset */
314 			continue;
315 
316 		err = xe_pagefault_service(&pf);
317 		if (err) {
318 			xe_pagefault_save_to_vm(gt_to_xe(pf.gt), &pf);
319 			if (!(pf.consumer.access_type & XE_PAGEFAULT_ACCESS_PREFETCH)) {
320 				xe_pagefault_print(&pf);
321 				xe_gt_info(pf.gt, "Fault response: Unsuccessful %pe\n",
322 					   ERR_PTR(err));
323 			} else {
324 				xe_gt_stats_incr(pf.gt, XE_GT_STATS_ID_INVALID_PREFETCH_PAGEFAULT_COUNT, 1);
325 				xe_gt_dbg(pf.gt, "Prefetch Fault response: Unsuccessful %pe\n",
326 					  ERR_PTR(err));
327 			}
328 		}
329 
330 		pf.producer.ops->ack_fault(&pf, err);
331 
332 		if (time_after(jiffies, threshold)) {
333 			queue_work(gt_to_xe(pf.gt)->usm.pf_wq, w);
334 			break;
335 		}
336 	}
337 #undef USM_QUEUE_MAX_RUNTIME_MS
338 }
339 
xe_pagefault_queue_init(struct xe_device * xe,struct xe_pagefault_queue * pf_queue)340 static int xe_pagefault_queue_init(struct xe_device *xe,
341 				   struct xe_pagefault_queue *pf_queue)
342 {
343 	struct xe_gt *gt;
344 	int total_num_eus = 0;
345 	u8 id;
346 
347 	for_each_gt(gt, xe, id) {
348 		xe_dss_mask_t all_dss;
349 		int num_dss, num_eus;
350 
351 		num_dss = bitmap_weighted_or(all_dss, gt->fuse_topo.g_dss_mask,
352 			  gt->fuse_topo.c_dss_mask, XE_MAX_DSS_FUSE_BITS);
353 
354 		num_eus = bitmap_weight(gt->fuse_topo.eu_mask_per_dss,
355 					XE_MAX_EU_FUSE_BITS) * num_dss;
356 
357 		total_num_eus += num_eus;
358 	}
359 
360 	xe_assert(xe, total_num_eus);
361 
362 	/*
363 	 * user can issue separate page faults per EU and per CS
364 	 *
365 	 * XXX: Multiplier required as compute UMD are getting PF queue errors
366 	 * without it. Follow on why this multiplier is required.
367 	 */
368 #define PF_MULTIPLIER	8
369 	pf_queue->size = (total_num_eus + XE_NUM_HW_ENGINES) *
370 		xe_pagefault_entry_size() * PF_MULTIPLIER;
371 	pf_queue->size = roundup_pow_of_two(pf_queue->size);
372 #undef PF_MULTIPLIER
373 
374 	drm_dbg(&xe->drm, "xe_pagefault_entry_size=%d, total_num_eus=%d, pf_queue->size=%u",
375 		xe_pagefault_entry_size(), total_num_eus, pf_queue->size);
376 
377 	pf_queue->xe = xe;
378 	spin_lock_init(&pf_queue->lock);
379 	INIT_WORK(&pf_queue->worker, xe_pagefault_queue_work);
380 
381 	pf_queue->data = drmm_kzalloc(&xe->drm, pf_queue->size, GFP_KERNEL);
382 	if (!pf_queue->data)
383 		return -ENOMEM;
384 
385 	return 0;
386 }
387 
xe_pagefault_fini(void * arg)388 static void xe_pagefault_fini(void *arg)
389 {
390 	struct xe_device *xe = arg;
391 
392 	destroy_workqueue(xe->usm.pf_wq);
393 }
394 
395 /**
396  * xe_pagefault_init() - Page fault init
397  * @xe: xe device instance
398  *
399  * Initialize Xe page fault state. Must be done after reading fuses.
400  *
401  * Return: 0 on Success, errno on failure
402  */
xe_pagefault_init(struct xe_device * xe)403 int xe_pagefault_init(struct xe_device *xe)
404 {
405 	int err, i;
406 
407 	if (!xe->info.has_usm)
408 		return 0;
409 
410 	xe->usm.pf_wq = alloc_workqueue("xe_page_fault_work_queue",
411 					WQ_UNBOUND | WQ_HIGHPRI,
412 					XE_PAGEFAULT_QUEUE_COUNT);
413 	if (!xe->usm.pf_wq)
414 		return -ENOMEM;
415 
416 	for (i = 0; i < XE_PAGEFAULT_QUEUE_COUNT; ++i) {
417 		err = xe_pagefault_queue_init(xe, xe->usm.pf_queue + i);
418 		if (err)
419 			goto err_out;
420 	}
421 
422 	return devm_add_action_or_reset(xe->drm.dev, xe_pagefault_fini, xe);
423 
424 err_out:
425 	destroy_workqueue(xe->usm.pf_wq);
426 	return err;
427 }
428 
xe_pagefault_queue_reset(struct xe_device * xe,struct xe_gt * gt,struct xe_pagefault_queue * pf_queue)429 static void xe_pagefault_queue_reset(struct xe_device *xe, struct xe_gt *gt,
430 				     struct xe_pagefault_queue *pf_queue)
431 {
432 	u32 i;
433 
434 	/* Driver load failure guard / USM not enabled guard */
435 	if (!pf_queue->data)
436 		return;
437 
438 	/* Squash all pending faults on the GT */
439 
440 	spin_lock_irq(&pf_queue->lock);
441 	for (i = pf_queue->tail; i != pf_queue->head;
442 	     i = (i + xe_pagefault_entry_size()) % pf_queue->size) {
443 		struct xe_pagefault *pf = pf_queue->data + i;
444 
445 		if (pf->gt == gt)
446 			pf->gt = NULL;
447 	}
448 	spin_unlock_irq(&pf_queue->lock);
449 }
450 
451 /**
452  * xe_pagefault_reset() - Page fault reset for a GT
453  * @xe: xe device instance
454  * @gt: GT being reset
455  *
456  * Reset the Xe page fault state for a GT; that is, squash any pending faults on
457  * the GT.
458  */
xe_pagefault_reset(struct xe_device * xe,struct xe_gt * gt)459 void xe_pagefault_reset(struct xe_device *xe, struct xe_gt *gt)
460 {
461 	int i;
462 
463 	for (i = 0; i < XE_PAGEFAULT_QUEUE_COUNT; ++i)
464 		xe_pagefault_queue_reset(xe, gt, xe->usm.pf_queue + i);
465 }
466 
xe_pagefault_queue_full(struct xe_pagefault_queue * pf_queue)467 static bool xe_pagefault_queue_full(struct xe_pagefault_queue *pf_queue)
468 {
469 	lockdep_assert_held(&pf_queue->lock);
470 
471 	return CIRC_SPACE(pf_queue->head, pf_queue->tail, pf_queue->size) <=
472 		xe_pagefault_entry_size();
473 }
474 
475 /**
476  * xe_pagefault_handler() - Page fault handler
477  * @xe: xe device instance
478  * @pf: Page fault
479  *
480  * Sink the page fault to a queue (i.e., a memory buffer) and queue a worker to
481  * service it. Safe to be called from IRQ or process context. Reclaim safe.
482  *
483  * Return: 0 on success, errno on failure
484  */
xe_pagefault_handler(struct xe_device * xe,struct xe_pagefault * pf)485 int xe_pagefault_handler(struct xe_device *xe, struct xe_pagefault *pf)
486 {
487 	struct xe_pagefault_queue *pf_queue = xe->usm.pf_queue +
488 		(pf->consumer.asid % XE_PAGEFAULT_QUEUE_COUNT);
489 	unsigned long flags;
490 	bool full;
491 
492 	spin_lock_irqsave(&pf_queue->lock, flags);
493 	full = xe_pagefault_queue_full(pf_queue);
494 	if (!full) {
495 		memcpy(pf_queue->data + pf_queue->head, pf, sizeof(*pf));
496 		pf_queue->head = (pf_queue->head + xe_pagefault_entry_size()) %
497 			pf_queue->size;
498 		queue_work(xe->usm.pf_wq, &pf_queue->worker);
499 	} else {
500 		drm_warn(&xe->drm,
501 			 "PageFault Queue (%d) full, shouldn't be possible\n",
502 			 pf->consumer.asid % XE_PAGEFAULT_QUEUE_COUNT);
503 	}
504 	spin_unlock_irqrestore(&pf_queue->lock, flags);
505 
506 	return full ? -ENOSPC : 0;
507 }
508