xref: /linux/drivers/gpu/drm/xe/xe_tlb_inval.c (revision 53597deca0e38c30e6cd4ba2114fa42d2bcd85bb)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include <drm/drm_managed.h>
7 
8 #include "xe_device_types.h"
9 #include "xe_force_wake.h"
10 #include "xe_gt_stats.h"
11 #include "xe_gt_types.h"
12 #include "xe_guc_ct.h"
13 #include "xe_guc_tlb_inval.h"
14 #include "xe_mmio.h"
15 #include "xe_pm.h"
16 #include "xe_tlb_inval.h"
17 #include "xe_trace.h"
18 
19 /**
20  * DOC: Xe TLB invalidation
21  *
22  * Xe TLB invalidation is implemented in two layers. The first is the frontend
23  * API, which provides an interface for TLB invalidations to the driver code.
24  * The frontend handles seqno assignment, synchronization (fences), and the
25  * timeout mechanism. The frontend is implemented via an embedded structure
26  * xe_tlb_inval that includes a set of ops hooking into the backend. The backend
27  * interacts with the hardware (or firmware) to perform the actual invalidation.
28  */
29 
30 #define FENCE_STACK_BIT		DMA_FENCE_FLAG_USER_BITS
31 
32 static void xe_tlb_inval_fence_fini(struct xe_tlb_inval_fence *fence)
33 {
34 	if (WARN_ON_ONCE(!fence->tlb_inval))
35 		return;
36 
37 	xe_pm_runtime_put(fence->tlb_inval->xe);
38 	fence->tlb_inval = NULL; /* fini() should be called once */
39 }
40 
41 static void
42 xe_tlb_inval_fence_signal(struct xe_tlb_inval_fence *fence)
43 {
44 	struct xe_tlb_inval *tlb_inval = fence->tlb_inval;
45 	bool stack = test_bit(FENCE_STACK_BIT, &fence->base.flags);
46 
47 	lockdep_assert_held(&fence->tlb_inval->pending_lock);
48 
49 	list_del(&fence->link);
50 	if (list_empty(&tlb_inval->pending_fences))
51 		cancel_delayed_work(&tlb_inval->fence_tdr);
52 	trace_xe_tlb_inval_fence_signal(fence->tlb_inval->xe, fence);
53 	xe_tlb_inval_fence_fini(fence);
54 	dma_fence_signal(&fence->base);
55 	if (!stack)
56 		dma_fence_put(&fence->base);
57 }
58 
59 static void
60 xe_tlb_inval_fence_signal_unlocked(struct xe_tlb_inval_fence *fence)
61 {
62 	struct xe_tlb_inval *tlb_inval = fence->tlb_inval;
63 
64 	spin_lock_irq(&tlb_inval->pending_lock);
65 	xe_tlb_inval_fence_signal(fence);
66 	spin_unlock_irq(&tlb_inval->pending_lock);
67 }
68 
69 static void xe_tlb_inval_fence_timeout(struct work_struct *work)
70 {
71 	struct xe_tlb_inval *tlb_inval = container_of(work, struct xe_tlb_inval,
72 						      fence_tdr.work);
73 	struct xe_device *xe = tlb_inval->xe;
74 	struct xe_tlb_inval_fence *fence, *next;
75 	long timeout_delay = tlb_inval->ops->timeout_delay(tlb_inval);
76 
77 	tlb_inval->ops->flush(tlb_inval);
78 
79 	spin_lock_irq(&tlb_inval->pending_lock);
80 	list_for_each_entry_safe(fence, next,
81 				 &tlb_inval->pending_fences, link) {
82 		s64 since_inval_ms = ktime_ms_delta(ktime_get(),
83 						    fence->inval_time);
84 
85 		if (msecs_to_jiffies(since_inval_ms) < timeout_delay)
86 			break;
87 
88 		trace_xe_tlb_inval_fence_timeout(xe, fence);
89 		drm_err(&xe->drm,
90 			"TLB invalidation fence timeout, seqno=%d recv=%d",
91 			fence->seqno, tlb_inval->seqno_recv);
92 
93 		fence->base.error = -ETIME;
94 		xe_tlb_inval_fence_signal(fence);
95 	}
96 	if (!list_empty(&tlb_inval->pending_fences))
97 		queue_delayed_work(tlb_inval->timeout_wq, &tlb_inval->fence_tdr,
98 				   timeout_delay);
99 	spin_unlock_irq(&tlb_inval->pending_lock);
100 }
101 
102 /**
103  * tlb_inval_fini - Clean up TLB invalidation state
104  * @drm: @drm_device
105  * @arg: pointer to struct @xe_tlb_inval
106  *
107  * Cancel pending fence workers and clean up any additional
108  * TLB invalidation state.
109  */
110 static void tlb_inval_fini(struct drm_device *drm, void *arg)
111 {
112 	struct xe_tlb_inval *tlb_inval = arg;
113 
114 	xe_tlb_inval_reset(tlb_inval);
115 }
116 
117 static void primelockdep(struct xe_tlb_inval *tlb_inval)
118 {
119 	if (!IS_ENABLED(CONFIG_LOCKDEP))
120 		return;
121 
122 	fs_reclaim_acquire(GFP_KERNEL);
123 	might_lock(&tlb_inval->seqno_lock);
124 	fs_reclaim_release(GFP_KERNEL);
125 }
126 
127 /**
128  * xe_gt_tlb_inval_init_early() - Initialize TLB invalidation state
129  * @gt: GT structure
130  *
131  * Initialize TLB invalidation state, purely software initialization, should
132  * be called once during driver load.
133  *
134  * Return: 0 on success, negative error code on error.
135  */
136 int xe_gt_tlb_inval_init_early(struct xe_gt *gt)
137 {
138 	struct xe_device *xe = gt_to_xe(gt);
139 	struct xe_tlb_inval *tlb_inval = &gt->tlb_inval;
140 	int err;
141 
142 	tlb_inval->xe = xe;
143 	tlb_inval->seqno = 1;
144 	INIT_LIST_HEAD(&tlb_inval->pending_fences);
145 	spin_lock_init(&tlb_inval->pending_lock);
146 	spin_lock_init(&tlb_inval->lock);
147 	INIT_DELAYED_WORK(&tlb_inval->fence_tdr, xe_tlb_inval_fence_timeout);
148 
149 	err = drmm_mutex_init(&xe->drm, &tlb_inval->seqno_lock);
150 	if (err)
151 		return err;
152 
153 	primelockdep(tlb_inval);
154 
155 	tlb_inval->job_wq = drmm_alloc_ordered_workqueue(&xe->drm,
156 							 "gt-tbl-inval-job-wq",
157 							 WQ_MEM_RECLAIM);
158 	if (IS_ERR(tlb_inval->job_wq))
159 		return PTR_ERR(tlb_inval->job_wq);
160 
161 	tlb_inval->timeout_wq = gt->ordered_wq;
162 	if (IS_ERR(tlb_inval->timeout_wq))
163 		return PTR_ERR(tlb_inval->timeout_wq);
164 
165 	/* XXX: Blindly setting up backend to GuC */
166 	xe_guc_tlb_inval_init_early(&gt->uc.guc, tlb_inval);
167 
168 	return drmm_add_action_or_reset(&xe->drm, tlb_inval_fini, tlb_inval);
169 }
170 
171 /**
172  * xe_tlb_inval_reset() - TLB invalidation reset
173  * @tlb_inval: TLB invalidation client
174  *
175  * Signal any pending invalidation fences, should be called during a GT reset
176  */
177 void xe_tlb_inval_reset(struct xe_tlb_inval *tlb_inval)
178 {
179 	struct xe_tlb_inval_fence *fence, *next;
180 	int pending_seqno;
181 
182 	/*
183 	 * we can get here before the backends are even initialized if we're
184 	 * wedging very early, in which case there are not going to be any
185 	 * pendind fences so we can bail immediately.
186 	 */
187 	if (!tlb_inval->ops->initialized(tlb_inval))
188 		return;
189 
190 	/*
191 	 * Backend is already disabled at this point. No new TLB requests can
192 	 * appear.
193 	 */
194 
195 	mutex_lock(&tlb_inval->seqno_lock);
196 	spin_lock_irq(&tlb_inval->pending_lock);
197 	cancel_delayed_work(&tlb_inval->fence_tdr);
198 	/*
199 	 * We might have various kworkers waiting for TLB flushes to complete
200 	 * which are not tracked with an explicit TLB fence, however at this
201 	 * stage that will never happen since the backend is already disabled,
202 	 * so make sure we signal them here under the assumption that we have
203 	 * completed a full GT reset.
204 	 */
205 	if (tlb_inval->seqno == 1)
206 		pending_seqno = TLB_INVALIDATION_SEQNO_MAX - 1;
207 	else
208 		pending_seqno = tlb_inval->seqno - 1;
209 	WRITE_ONCE(tlb_inval->seqno_recv, pending_seqno);
210 
211 	list_for_each_entry_safe(fence, next,
212 				 &tlb_inval->pending_fences, link)
213 		xe_tlb_inval_fence_signal(fence);
214 	spin_unlock_irq(&tlb_inval->pending_lock);
215 	mutex_unlock(&tlb_inval->seqno_lock);
216 }
217 
218 /**
219  * xe_tlb_inval_reset_timeout() - Reset TLB inval fence timeout
220  * @tlb_inval: TLB invalidation client
221  *
222  * Reset the TLB invalidation timeout timer.
223  */
224 static void xe_tlb_inval_reset_timeout(struct xe_tlb_inval *tlb_inval)
225 {
226 	lockdep_assert_held(&tlb_inval->pending_lock);
227 
228 	mod_delayed_work(system_wq, &tlb_inval->fence_tdr,
229 			 tlb_inval->ops->timeout_delay(tlb_inval));
230 }
231 
232 static bool xe_tlb_inval_seqno_past(struct xe_tlb_inval *tlb_inval, int seqno)
233 {
234 	int seqno_recv = READ_ONCE(tlb_inval->seqno_recv);
235 
236 	lockdep_assert_held(&tlb_inval->pending_lock);
237 
238 	if (seqno - seqno_recv < -(TLB_INVALIDATION_SEQNO_MAX / 2))
239 		return false;
240 
241 	if (seqno - seqno_recv > (TLB_INVALIDATION_SEQNO_MAX / 2))
242 		return true;
243 
244 	return seqno_recv >= seqno;
245 }
246 
247 static void xe_tlb_inval_fence_prep(struct xe_tlb_inval_fence *fence)
248 {
249 	struct xe_tlb_inval *tlb_inval = fence->tlb_inval;
250 
251 	fence->seqno = tlb_inval->seqno;
252 	trace_xe_tlb_inval_fence_send(tlb_inval->xe, fence);
253 
254 	spin_lock_irq(&tlb_inval->pending_lock);
255 	fence->inval_time = ktime_get();
256 	list_add_tail(&fence->link, &tlb_inval->pending_fences);
257 
258 	if (list_is_singular(&tlb_inval->pending_fences))
259 		queue_delayed_work(tlb_inval->timeout_wq, &tlb_inval->fence_tdr,
260 				   tlb_inval->ops->timeout_delay(tlb_inval));
261 	spin_unlock_irq(&tlb_inval->pending_lock);
262 
263 	tlb_inval->seqno = (tlb_inval->seqno + 1) %
264 		TLB_INVALIDATION_SEQNO_MAX;
265 	if (!tlb_inval->seqno)
266 		tlb_inval->seqno = 1;
267 }
268 
269 #define xe_tlb_inval_issue(__tlb_inval, __fence, op, args...)	\
270 ({								\
271 	int __ret;						\
272 								\
273 	xe_assert((__tlb_inval)->xe, (__tlb_inval)->ops);	\
274 	xe_assert((__tlb_inval)->xe, (__fence));		\
275 								\
276 	mutex_lock(&(__tlb_inval)->seqno_lock); 		\
277 	xe_tlb_inval_fence_prep((__fence));			\
278 	__ret = op((__tlb_inval), (__fence)->seqno, ##args);	\
279 	if (__ret < 0)						\
280 		xe_tlb_inval_fence_signal_unlocked((__fence));	\
281 	mutex_unlock(&(__tlb_inval)->seqno_lock);		\
282 								\
283 	__ret == -ECANCELED ? 0 : __ret;			\
284 })
285 
286 /**
287  * xe_tlb_inval_all() - Issue a TLB invalidation for all TLBs
288  * @tlb_inval: TLB invalidation client
289  * @fence: invalidation fence which will be signal on TLB invalidation
290  * completion
291  *
292  * Issue a TLB invalidation for all TLBs. Completion of TLB is asynchronous and
293  * caller can use the invalidation fence to wait for completion.
294  *
295  * Return: 0 on success, negative error code on error
296  */
297 int xe_tlb_inval_all(struct xe_tlb_inval *tlb_inval,
298 		     struct xe_tlb_inval_fence *fence)
299 {
300 	return xe_tlb_inval_issue(tlb_inval, fence, tlb_inval->ops->all);
301 }
302 
303 /**
304  * xe_tlb_inval_ggtt() - Issue a TLB invalidation for the GGTT
305  * @tlb_inval: TLB invalidation client
306  *
307  * Issue a TLB invalidation for the GGTT. Completion of TLB is asynchronous and
308  * caller can use the invalidation fence to wait for completion.
309  *
310  * Return: 0 on success, negative error code on error
311  */
312 int xe_tlb_inval_ggtt(struct xe_tlb_inval *tlb_inval)
313 {
314 	struct xe_tlb_inval_fence fence, *fence_ptr = &fence;
315 	int ret;
316 
317 	xe_tlb_inval_fence_init(tlb_inval, fence_ptr, true);
318 	ret = xe_tlb_inval_issue(tlb_inval, fence_ptr, tlb_inval->ops->ggtt);
319 	xe_tlb_inval_fence_wait(fence_ptr);
320 
321 	return ret;
322 }
323 
324 /**
325  * xe_tlb_inval_range() - Issue a TLB invalidation for an address range
326  * @tlb_inval: TLB invalidation client
327  * @fence: invalidation fence which will be signal on TLB invalidation
328  * completion
329  * @start: start address
330  * @end: end address
331  * @asid: address space id
332  * @prl_sa: suballocation of page reclaim list if used, NULL indicates PPC flush
333  *
334  * Issue a range based TLB invalidation if supported, if not fallback to a full
335  * TLB invalidation. Completion of TLB is asynchronous and caller can use
336  * the invalidation fence to wait for completion.
337  *
338  * Return: Negative error code on error, 0 on success
339  */
340 int xe_tlb_inval_range(struct xe_tlb_inval *tlb_inval,
341 		       struct xe_tlb_inval_fence *fence, u64 start, u64 end,
342 		       u32 asid, struct drm_suballoc *prl_sa)
343 {
344 	return xe_tlb_inval_issue(tlb_inval, fence, tlb_inval->ops->ppgtt,
345 				  start, end, asid, prl_sa);
346 }
347 
348 /**
349  * xe_tlb_inval_vm() - Issue a TLB invalidation for a VM
350  * @tlb_inval: TLB invalidation client
351  * @vm: VM to invalidate
352  *
353  * Invalidate entire VM's address space
354  */
355 void xe_tlb_inval_vm(struct xe_tlb_inval *tlb_inval, struct xe_vm *vm)
356 {
357 	struct xe_tlb_inval_fence fence;
358 	u64 range = 1ull << vm->xe->info.va_bits;
359 
360 	xe_tlb_inval_fence_init(tlb_inval, &fence, true);
361 	xe_tlb_inval_range(tlb_inval, &fence, 0, range, vm->usm.asid, NULL);
362 	xe_tlb_inval_fence_wait(&fence);
363 }
364 
365 /**
366  * xe_tlb_inval_done_handler() - TLB invalidation done handler
367  * @tlb_inval: TLB invalidation client
368  * @seqno: seqno of invalidation that is done
369  *
370  * Update recv seqno, signal any TLB invalidation fences, and restart TDR
371  */
372 void xe_tlb_inval_done_handler(struct xe_tlb_inval *tlb_inval, int seqno)
373 {
374 	struct xe_device *xe = tlb_inval->xe;
375 	struct xe_tlb_inval_fence *fence, *next;
376 	unsigned long flags;
377 
378 	/*
379 	 * This can also be run both directly from the IRQ handler and also in
380 	 * process_g2h_msg(). Only one may process any individual CT message,
381 	 * however the order they are processed here could result in skipping a
382 	 * seqno. To handle that we just process all the seqnos from the last
383 	 * seqno_recv up to and including the one in msg[0]. The delta should be
384 	 * very small so there shouldn't be much of pending_fences we actually
385 	 * need to iterate over here.
386 	 *
387 	 * From GuC POV we expect the seqnos to always appear in-order, so if we
388 	 * see something later in the timeline we can be sure that anything
389 	 * appearing earlier has already signalled, just that we have yet to
390 	 * officially process the CT message like if racing against
391 	 * process_g2h_msg().
392 	 */
393 	spin_lock_irqsave(&tlb_inval->pending_lock, flags);
394 	if (seqno == TLB_INVALIDATION_SEQNO_INVALID) {
395 		xe_tlb_inval_reset_timeout(tlb_inval);
396 		spin_unlock_irqrestore(&tlb_inval->pending_lock, flags);
397 		return;
398 	}
399 
400 	if (xe_tlb_inval_seqno_past(tlb_inval, seqno)) {
401 		spin_unlock_irqrestore(&tlb_inval->pending_lock, flags);
402 		return;
403 	}
404 
405 	WRITE_ONCE(tlb_inval->seqno_recv, seqno);
406 
407 	list_for_each_entry_safe(fence, next,
408 				 &tlb_inval->pending_fences, link) {
409 		trace_xe_tlb_inval_fence_recv(xe, fence);
410 
411 		if (!xe_tlb_inval_seqno_past(tlb_inval, fence->seqno))
412 			break;
413 
414 		xe_tlb_inval_fence_signal(fence);
415 	}
416 
417 	if (!list_empty(&tlb_inval->pending_fences))
418 		mod_delayed_work(tlb_inval->timeout_wq,
419 				 &tlb_inval->fence_tdr,
420 				 tlb_inval->ops->timeout_delay(tlb_inval));
421 	else
422 		cancel_delayed_work(&tlb_inval->fence_tdr);
423 
424 	spin_unlock_irqrestore(&tlb_inval->pending_lock, flags);
425 }
426 
427 static const char *
428 xe_inval_fence_get_driver_name(struct dma_fence *dma_fence)
429 {
430 	return "xe";
431 }
432 
433 static const char *
434 xe_inval_fence_get_timeline_name(struct dma_fence *dma_fence)
435 {
436 	return "tlb_inval_fence";
437 }
438 
439 static const struct dma_fence_ops inval_fence_ops = {
440 	.get_driver_name = xe_inval_fence_get_driver_name,
441 	.get_timeline_name = xe_inval_fence_get_timeline_name,
442 };
443 
444 /**
445  * xe_tlb_inval_fence_init() - Initialize TLB invalidation fence
446  * @tlb_inval: TLB invalidation client
447  * @fence: TLB invalidation fence to initialize
448  * @stack: fence is stack variable
449  *
450  * Initialize TLB invalidation fence for use. xe_tlb_inval_fence_fini
451  * will be automatically called when fence is signalled (all fences must signal),
452  * even on error.
453  */
454 void xe_tlb_inval_fence_init(struct xe_tlb_inval *tlb_inval,
455 			     struct xe_tlb_inval_fence *fence,
456 			     bool stack)
457 {
458 	xe_pm_runtime_get_noresume(tlb_inval->xe);
459 
460 	spin_lock_irq(&tlb_inval->lock);
461 	dma_fence_init(&fence->base, &inval_fence_ops, &tlb_inval->lock,
462 		       dma_fence_context_alloc(1), 1);
463 	spin_unlock_irq(&tlb_inval->lock);
464 	INIT_LIST_HEAD(&fence->link);
465 	if (stack)
466 		set_bit(FENCE_STACK_BIT, &fence->base.flags);
467 	else
468 		dma_fence_get(&fence->base);
469 	fence->tlb_inval = tlb_inval;
470 }
471 
472 /**
473  * xe_tlb_inval_idle() - Initialize TLB invalidation is idle
474  * @tlb_inval: TLB invalidation client
475  *
476  * Check the TLB invalidation seqno to determine if it is idle (i.e., no TLB
477  * invalidations are in flight). Expected to be called in the backend after the
478  * fence has been added to the pending list, and takes this into account.
479  *
480  * Return: True if TLB invalidation client is idle, False otherwise
481  */
482 bool xe_tlb_inval_idle(struct xe_tlb_inval *tlb_inval)
483 {
484 	lockdep_assert_held(&tlb_inval->seqno_lock);
485 
486 	guard(spinlock_irq)(&tlb_inval->pending_lock);
487 	return list_is_singular(&tlb_inval->pending_fences);
488 }
489 
490 /**
491  * xe_tlb_inval_batch_wait() - Wait for all fences in a TLB invalidation batch
492  * @batch: Batch of TLB invalidation fences to wait on
493  *
494  * Waits for every fence in @batch to signal, then resets @batch so it can be
495  * reused for a subsequent invalidation.
496  */
497 void xe_tlb_inval_batch_wait(struct xe_tlb_inval_batch *batch)
498 {
499 	struct xe_tlb_inval_fence *fence = &batch->fence[0];
500 	unsigned int i;
501 
502 	for (i = 0; i < batch->num_fences; ++i)
503 		xe_tlb_inval_fence_wait(fence++);
504 
505 	batch->num_fences = 0;
506 }
507 
508 /**
509  * xe_tlb_inval_range_tilemask_submit() - Submit TLB invalidations for an
510  * address range on a tile mask
511  * @xe: The xe device
512  * @asid: Address space ID
513  * @start: start address
514  * @end: end address
515  * @tile_mask: mask for which gt's issue tlb invalidation
516  * @batch: Batch of tlb invalidate fences
517  *
518  * Issue a range based TLB invalidation for gt's in tilemask
519  * If the function returns an error, there is no need to call
520  * xe_tlb_inval_batch_wait() on @batch.
521  *
522  * Returns 0 for success, negative error code otherwise.
523  */
524 int xe_tlb_inval_range_tilemask_submit(struct xe_device *xe, u32 asid,
525 				       u64 start, u64 end, u8 tile_mask,
526 				       struct xe_tlb_inval_batch *batch)
527 {
528 	struct xe_tlb_inval_fence *fence = &batch->fence[0];
529 	struct xe_tile *tile;
530 	u32 fence_id = 0;
531 	u8 id;
532 	int err;
533 
534 	batch->num_fences = 0;
535 	if (!tile_mask)
536 		return 0;
537 
538 	for_each_tile(tile, xe, id) {
539 		if (!(tile_mask & BIT(id)))
540 			continue;
541 
542 		xe_tlb_inval_fence_init(&tile->primary_gt->tlb_inval,
543 					&fence[fence_id], true);
544 
545 		err = xe_tlb_inval_range(&tile->primary_gt->tlb_inval,
546 					 &fence[fence_id], start, end,
547 					 asid, NULL);
548 		if (err)
549 			goto wait;
550 		++fence_id;
551 
552 		if (!tile->media_gt)
553 			continue;
554 
555 		xe_tlb_inval_fence_init(&tile->media_gt->tlb_inval,
556 					&fence[fence_id], true);
557 
558 		err = xe_tlb_inval_range(&tile->media_gt->tlb_inval,
559 					 &fence[fence_id], start, end,
560 					 asid, NULL);
561 		if (err)
562 			goto wait;
563 		++fence_id;
564 	}
565 
566 wait:
567 	batch->num_fences = fence_id;
568 	if (err)
569 		xe_tlb_inval_batch_wait(batch);
570 
571 	return err;
572 }
573