xref: /linux/drivers/gpu/drm/xe/xe_tlb_inval.c (revision 6812ce4e4379ffc99c52401ec28f0d7ffbc36206)
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 
xe_tlb_inval_fence_fini(struct xe_tlb_inval_fence * fence)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
xe_tlb_inval_fence_signal(struct xe_tlb_inval_fence * fence)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
xe_tlb_inval_fence_signal_unlocked(struct xe_tlb_inval_fence * fence)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 
xe_tlb_inval_fence_timeout(struct work_struct * work)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  */
tlb_inval_fini(struct drm_device * drm,void * arg)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 
primelockdep(struct xe_tlb_inval * tlb_inval)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  */
xe_gt_tlb_inval_init_early(struct xe_gt * gt)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  */
xe_tlb_inval_reset(struct xe_tlb_inval * tlb_inval)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  */
xe_tlb_inval_reset_timeout(struct xe_tlb_inval * tlb_inval)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 
xe_tlb_inval_seqno_past(struct xe_tlb_inval * tlb_inval,int seqno)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 
xe_tlb_inval_fence_prep(struct xe_tlb_inval_fence * fence)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 	/* Undelivered: fence already signalled, report done */	\
284 	(__ret == -ECANCELED || __ret == -ENOTRECOVERABLE) ? 0 : __ret;	\
285 })
286 
287 /**
288  * xe_tlb_inval_all() - Issue a TLB invalidation for all TLBs
289  * @tlb_inval: TLB invalidation client
290  * @fence: invalidation fence which will be signal on TLB invalidation
291  * completion
292  *
293  * Issue a TLB invalidation for all TLBs. Completion of TLB is asynchronous and
294  * caller can use the invalidation fence to wait for completion.
295  *
296  * Return: 0 on success, negative error code on error
297  */
xe_tlb_inval_all(struct xe_tlb_inval * tlb_inval,struct xe_tlb_inval_fence * fence)298 int xe_tlb_inval_all(struct xe_tlb_inval *tlb_inval,
299 		     struct xe_tlb_inval_fence *fence)
300 {
301 	return xe_tlb_inval_issue(tlb_inval, fence, tlb_inval->ops->all);
302 }
303 
304 /**
305  * xe_tlb_inval_ggtt() - Issue a TLB invalidation for the GGTT
306  * @tlb_inval: TLB invalidation client
307  *
308  * Issue a TLB invalidation for the GGTT. Completion of TLB is asynchronous and
309  * caller can use the invalidation fence to wait for completion.
310  *
311  * Return: 0 on success, negative error code on error
312  */
xe_tlb_inval_ggtt(struct xe_tlb_inval * tlb_inval)313 int xe_tlb_inval_ggtt(struct xe_tlb_inval *tlb_inval)
314 {
315 	struct xe_tlb_inval_fence fence, *fence_ptr = &fence;
316 	int ret;
317 
318 	xe_tlb_inval_fence_init(tlb_inval, fence_ptr, true);
319 	ret = xe_tlb_inval_issue(tlb_inval, fence_ptr, tlb_inval->ops->ggtt);
320 	xe_tlb_inval_fence_wait(fence_ptr);
321 
322 	return ret;
323 }
324 
325 /**
326  * xe_tlb_inval_range() - Issue a TLB invalidation for an address range
327  * @tlb_inval: TLB invalidation client
328  * @fence: invalidation fence which will be signal on TLB invalidation
329  * completion
330  * @start: start address
331  * @end: end address
332  * @asid: address space id
333  * @prl_sa: suballocation of page reclaim list if used, NULL indicates PPC flush
334  *
335  * Issue a range based TLB invalidation if supported, if not fallback to a full
336  * TLB invalidation. Completion of TLB is asynchronous and caller can use
337  * the invalidation fence to wait for completion.
338  *
339  * Return: Negative error code on error, 0 on success
340  */
xe_tlb_inval_range(struct xe_tlb_inval * tlb_inval,struct xe_tlb_inval_fence * fence,u64 start,u64 end,u32 asid,struct drm_suballoc * prl_sa)341 int xe_tlb_inval_range(struct xe_tlb_inval *tlb_inval,
342 		       struct xe_tlb_inval_fence *fence, u64 start, u64 end,
343 		       u32 asid, struct drm_suballoc *prl_sa)
344 {
345 	return xe_tlb_inval_issue(tlb_inval, fence, tlb_inval->ops->ppgtt,
346 				  start, end, asid, prl_sa);
347 }
348 
349 /**
350  * xe_tlb_inval_vm() - Issue a TLB invalidation for a VM
351  * @tlb_inval: TLB invalidation client
352  * @vm: VM to invalidate
353  *
354  * Invalidate entire VM's address space
355  */
xe_tlb_inval_vm(struct xe_tlb_inval * tlb_inval,struct xe_vm * vm)356 void xe_tlb_inval_vm(struct xe_tlb_inval *tlb_inval, struct xe_vm *vm)
357 {
358 	struct xe_tlb_inval_fence fence;
359 	u64 range = 1ull << vm->xe->info.va_bits;
360 
361 	xe_tlb_inval_fence_init(tlb_inval, &fence, true);
362 	xe_tlb_inval_range(tlb_inval, &fence, 0, range, vm->usm.asid, NULL);
363 	xe_tlb_inval_fence_wait(&fence);
364 }
365 
366 /**
367  * xe_tlb_inval_done_handler() - TLB invalidation done handler
368  * @tlb_inval: TLB invalidation client
369  * @seqno: seqno of invalidation that is done
370  *
371  * Update recv seqno, signal any TLB invalidation fences, and restart TDR
372  */
xe_tlb_inval_done_handler(struct xe_tlb_inval * tlb_inval,int seqno)373 void xe_tlb_inval_done_handler(struct xe_tlb_inval *tlb_inval, int seqno)
374 {
375 	struct xe_device *xe = tlb_inval->xe;
376 	struct xe_tlb_inval_fence *fence, *next;
377 	unsigned long flags;
378 
379 	/*
380 	 * This can also be run both directly from the IRQ handler and also in
381 	 * process_g2h_msg(). Only one may process any individual CT message,
382 	 * however the order they are processed here could result in skipping a
383 	 * seqno. To handle that we just process all the seqnos from the last
384 	 * seqno_recv up to and including the one in msg[0]. The delta should be
385 	 * very small so there shouldn't be much of pending_fences we actually
386 	 * need to iterate over here.
387 	 *
388 	 * From GuC POV we expect the seqnos to always appear in-order, so if we
389 	 * see something later in the timeline we can be sure that anything
390 	 * appearing earlier has already signalled, just that we have yet to
391 	 * officially process the CT message like if racing against
392 	 * process_g2h_msg().
393 	 */
394 	spin_lock_irqsave(&tlb_inval->pending_lock, flags);
395 	if (seqno == TLB_INVALIDATION_SEQNO_INVALID) {
396 		xe_tlb_inval_reset_timeout(tlb_inval);
397 		spin_unlock_irqrestore(&tlb_inval->pending_lock, flags);
398 		return;
399 	}
400 
401 	if (xe_tlb_inval_seqno_past(tlb_inval, seqno)) {
402 		spin_unlock_irqrestore(&tlb_inval->pending_lock, flags);
403 		return;
404 	}
405 
406 	WRITE_ONCE(tlb_inval->seqno_recv, seqno);
407 
408 	list_for_each_entry_safe(fence, next,
409 				 &tlb_inval->pending_fences, link) {
410 		trace_xe_tlb_inval_fence_recv(xe, fence);
411 
412 		if (!xe_tlb_inval_seqno_past(tlb_inval, fence->seqno))
413 			break;
414 
415 		xe_tlb_inval_fence_signal(fence);
416 	}
417 
418 	if (!list_empty(&tlb_inval->pending_fences))
419 		mod_delayed_work(tlb_inval->timeout_wq,
420 				 &tlb_inval->fence_tdr,
421 				 tlb_inval->ops->timeout_delay(tlb_inval));
422 	else
423 		cancel_delayed_work(&tlb_inval->fence_tdr);
424 
425 	spin_unlock_irqrestore(&tlb_inval->pending_lock, flags);
426 }
427 
428 static const char *
xe_inval_fence_get_driver_name(struct dma_fence * dma_fence)429 xe_inval_fence_get_driver_name(struct dma_fence *dma_fence)
430 {
431 	return "xe";
432 }
433 
434 static const char *
xe_inval_fence_get_timeline_name(struct dma_fence * dma_fence)435 xe_inval_fence_get_timeline_name(struct dma_fence *dma_fence)
436 {
437 	return "tlb_inval_fence";
438 }
439 
440 static const struct dma_fence_ops inval_fence_ops = {
441 	.get_driver_name = xe_inval_fence_get_driver_name,
442 	.get_timeline_name = xe_inval_fence_get_timeline_name,
443 };
444 
445 /**
446  * xe_tlb_inval_fence_init() - Initialize TLB invalidation fence
447  * @tlb_inval: TLB invalidation client
448  * @fence: TLB invalidation fence to initialize
449  * @stack: fence is stack variable
450  *
451  * Initialize TLB invalidation fence for use. xe_tlb_inval_fence_fini
452  * will be automatically called when fence is signalled (all fences must signal),
453  * even on error.
454  */
xe_tlb_inval_fence_init(struct xe_tlb_inval * tlb_inval,struct xe_tlb_inval_fence * fence,bool stack)455 void xe_tlb_inval_fence_init(struct xe_tlb_inval *tlb_inval,
456 			     struct xe_tlb_inval_fence *fence,
457 			     bool stack)
458 {
459 	xe_pm_runtime_get_noresume(tlb_inval->xe);
460 
461 	spin_lock_irq(&tlb_inval->lock);
462 	dma_fence_init(&fence->base, &inval_fence_ops, &tlb_inval->lock,
463 		       dma_fence_context_alloc(1), 1);
464 	spin_unlock_irq(&tlb_inval->lock);
465 	INIT_LIST_HEAD(&fence->link);
466 	if (stack)
467 		set_bit(FENCE_STACK_BIT, &fence->base.flags);
468 	else
469 		dma_fence_get(&fence->base);
470 	fence->tlb_inval = tlb_inval;
471 }
472 
473 /**
474  * xe_tlb_inval_idle() - Initialize TLB invalidation is idle
475  * @tlb_inval: TLB invalidation client
476  *
477  * Check the TLB invalidation seqno to determine if it is idle (i.e., no TLB
478  * invalidations are in flight). Expected to be called in the backend after the
479  * fence has been added to the pending list, and takes this into account.
480  *
481  * Return: True if TLB invalidation client is idle, False otherwise
482  */
xe_tlb_inval_idle(struct xe_tlb_inval * tlb_inval)483 bool xe_tlb_inval_idle(struct xe_tlb_inval *tlb_inval)
484 {
485 	lockdep_assert_held(&tlb_inval->seqno_lock);
486 
487 	guard(spinlock_irq)(&tlb_inval->pending_lock);
488 	return list_is_singular(&tlb_inval->pending_fences);
489 }
490 
491 /**
492  * xe_tlb_inval_batch_wait() - Wait for all fences in a TLB invalidation batch
493  * @batch: Batch of TLB invalidation fences to wait on
494  *
495  * Waits for every fence in @batch to signal, then resets @batch so it can be
496  * reused for a subsequent invalidation.
497  */
xe_tlb_inval_batch_wait(struct xe_tlb_inval_batch * batch)498 void xe_tlb_inval_batch_wait(struct xe_tlb_inval_batch *batch)
499 {
500 	struct xe_tlb_inval_fence *fence = &batch->fence[0];
501 	unsigned int i;
502 
503 	for (i = 0; i < batch->num_fences; ++i)
504 		xe_tlb_inval_fence_wait(fence++);
505 
506 	batch->num_fences = 0;
507 }
508 
509 /**
510  * xe_tlb_inval_range_tilemask_submit() - Submit TLB invalidations for an
511  * address range on a tile mask
512  * @xe: The xe device
513  * @asid: Address space ID
514  * @start: start address
515  * @end: end address
516  * @tile_mask: mask for which gt's issue tlb invalidation
517  * @batch: Batch of tlb invalidate fences
518  *
519  * Issue a range based TLB invalidation for gt's in tilemask
520  * If the function returns an error, there is no need to call
521  * xe_tlb_inval_batch_wait() on @batch.
522  *
523  * Returns 0 for success, negative error code otherwise.
524  */
xe_tlb_inval_range_tilemask_submit(struct xe_device * xe,u32 asid,u64 start,u64 end,u8 tile_mask,struct xe_tlb_inval_batch * batch)525 int xe_tlb_inval_range_tilemask_submit(struct xe_device *xe, u32 asid,
526 				       u64 start, u64 end, u8 tile_mask,
527 				       struct xe_tlb_inval_batch *batch)
528 {
529 	struct xe_tlb_inval_fence *fence = &batch->fence[0];
530 	struct xe_tile *tile;
531 	u32 fence_id = 0;
532 	u8 id;
533 	int err = 0;
534 
535 	batch->num_fences = 0;
536 	if (!tile_mask)
537 		return 0;
538 
539 	for_each_tile(tile, xe, id) {
540 		if (!(tile_mask & BIT(id)))
541 			continue;
542 
543 		xe_tlb_inval_fence_init(&tile->primary_gt->tlb_inval,
544 					&fence[fence_id], true);
545 
546 		err = xe_tlb_inval_range(&tile->primary_gt->tlb_inval,
547 					 &fence[fence_id], start, end,
548 					 asid, NULL);
549 		if (err)
550 			goto wait;
551 		++fence_id;
552 
553 		if (!tile->media_gt)
554 			continue;
555 
556 		xe_tlb_inval_fence_init(&tile->media_gt->tlb_inval,
557 					&fence[fence_id], true);
558 
559 		err = xe_tlb_inval_range(&tile->media_gt->tlb_inval,
560 					 &fence[fence_id], start, end,
561 					 asid, NULL);
562 		if (err)
563 			goto wait;
564 		++fence_id;
565 	}
566 
567 wait:
568 	batch->num_fences = fence_id;
569 	if (err)
570 		xe_tlb_inval_batch_wait(batch);
571 
572 	return err;
573 }
574