1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2023 Intel Corporation
4 */
5
6 #include "xe_gt_tlb_invalidation.h"
7
8 #include "abi/guc_actions_abi.h"
9 #include "xe_device.h"
10 #include "xe_force_wake.h"
11 #include "xe_gt.h"
12 #include "xe_gt_printk.h"
13 #include "xe_guc.h"
14 #include "xe_guc_ct.h"
15 #include "xe_gt_stats.h"
16 #include "xe_mmio.h"
17 #include "xe_pm.h"
18 #include "xe_sriov.h"
19 #include "xe_trace.h"
20 #include "regs/xe_guc_regs.h"
21
22 #define FENCE_STACK_BIT DMA_FENCE_FLAG_USER_BITS
23
24 /*
25 * TLB inval depends on pending commands in the CT queue and then the real
26 * invalidation time. Double up the time to process full CT queue
27 * just to be on the safe side.
28 */
tlb_timeout_jiffies(struct xe_gt * gt)29 static long tlb_timeout_jiffies(struct xe_gt *gt)
30 {
31 /* this reflects what HW/GuC needs to process TLB inv request */
32 const long hw_tlb_timeout = HZ / 4;
33
34 /* this estimates actual delay caused by the CTB transport */
35 long delay = xe_guc_ct_queue_proc_time_jiffies(>->uc.guc.ct);
36
37 return hw_tlb_timeout + 2 * delay;
38 }
39
xe_gt_tlb_invalidation_fence_fini(struct xe_gt_tlb_invalidation_fence * fence)40 static void xe_gt_tlb_invalidation_fence_fini(struct xe_gt_tlb_invalidation_fence *fence)
41 {
42 if (WARN_ON_ONCE(!fence->gt))
43 return;
44
45 xe_pm_runtime_put(gt_to_xe(fence->gt));
46 fence->gt = NULL; /* fini() should be called once */
47 }
48
49 static void
__invalidation_fence_signal(struct xe_device * xe,struct xe_gt_tlb_invalidation_fence * fence)50 __invalidation_fence_signal(struct xe_device *xe, struct xe_gt_tlb_invalidation_fence *fence)
51 {
52 bool stack = test_bit(FENCE_STACK_BIT, &fence->base.flags);
53
54 trace_xe_gt_tlb_invalidation_fence_signal(xe, fence);
55 xe_gt_tlb_invalidation_fence_fini(fence);
56 dma_fence_signal(&fence->base);
57 if (!stack)
58 dma_fence_put(&fence->base);
59 }
60
61 static void
invalidation_fence_signal(struct xe_device * xe,struct xe_gt_tlb_invalidation_fence * fence)62 invalidation_fence_signal(struct xe_device *xe, struct xe_gt_tlb_invalidation_fence *fence)
63 {
64 list_del(&fence->link);
65 __invalidation_fence_signal(xe, fence);
66 }
67
xe_gt_tlb_invalidation_fence_signal(struct xe_gt_tlb_invalidation_fence * fence)68 void xe_gt_tlb_invalidation_fence_signal(struct xe_gt_tlb_invalidation_fence *fence)
69 {
70 if (WARN_ON_ONCE(!fence->gt))
71 return;
72
73 __invalidation_fence_signal(gt_to_xe(fence->gt), fence);
74 }
75
xe_gt_tlb_fence_timeout(struct work_struct * work)76 static void xe_gt_tlb_fence_timeout(struct work_struct *work)
77 {
78 struct xe_gt *gt = container_of(work, struct xe_gt,
79 tlb_invalidation.fence_tdr.work);
80 struct xe_device *xe = gt_to_xe(gt);
81 struct xe_gt_tlb_invalidation_fence *fence, *next;
82
83 LNL_FLUSH_WORK(>->uc.guc.ct.g2h_worker);
84
85 spin_lock_irq(>->tlb_invalidation.pending_lock);
86 list_for_each_entry_safe(fence, next,
87 >->tlb_invalidation.pending_fences, link) {
88 s64 since_inval_ms = ktime_ms_delta(ktime_get(),
89 fence->invalidation_time);
90
91 if (msecs_to_jiffies(since_inval_ms) < tlb_timeout_jiffies(gt))
92 break;
93
94 trace_xe_gt_tlb_invalidation_fence_timeout(xe, fence);
95 xe_gt_err(gt, "TLB invalidation fence timeout, seqno=%d recv=%d",
96 fence->seqno, gt->tlb_invalidation.seqno_recv);
97
98 fence->base.error = -ETIME;
99 invalidation_fence_signal(xe, fence);
100 }
101 if (!list_empty(>->tlb_invalidation.pending_fences))
102 queue_delayed_work(system_wq,
103 >->tlb_invalidation.fence_tdr,
104 tlb_timeout_jiffies(gt));
105 spin_unlock_irq(>->tlb_invalidation.pending_lock);
106 }
107
108 /**
109 * xe_gt_tlb_invalidation_init_early - Initialize GT TLB invalidation state
110 * @gt: graphics tile
111 *
112 * Initialize GT TLB invalidation state, purely software initialization, should
113 * be called once during driver load.
114 *
115 * Return: 0 on success, negative error code on error.
116 */
xe_gt_tlb_invalidation_init_early(struct xe_gt * gt)117 int xe_gt_tlb_invalidation_init_early(struct xe_gt *gt)
118 {
119 gt->tlb_invalidation.seqno = 1;
120 INIT_LIST_HEAD(>->tlb_invalidation.pending_fences);
121 spin_lock_init(>->tlb_invalidation.pending_lock);
122 spin_lock_init(>->tlb_invalidation.lock);
123 INIT_DELAYED_WORK(>->tlb_invalidation.fence_tdr,
124 xe_gt_tlb_fence_timeout);
125
126 return 0;
127 }
128
129 /**
130 * xe_gt_tlb_invalidation_reset - Initialize GT TLB invalidation reset
131 * @gt: graphics tile
132 *
133 * Signal any pending invalidation fences, should be called during a GT reset
134 */
xe_gt_tlb_invalidation_reset(struct xe_gt * gt)135 void xe_gt_tlb_invalidation_reset(struct xe_gt *gt)
136 {
137 struct xe_gt_tlb_invalidation_fence *fence, *next;
138 int pending_seqno;
139
140 /*
141 * we can get here before the CTs are even initialized if we're wedging
142 * very early, in which case there are not going to be any pending
143 * fences so we can bail immediately.
144 */
145 if (!xe_guc_ct_initialized(>->uc.guc.ct))
146 return;
147
148 /*
149 * CT channel is already disabled at this point. No new TLB requests can
150 * appear.
151 */
152
153 mutex_lock(>->uc.guc.ct.lock);
154 spin_lock_irq(>->tlb_invalidation.pending_lock);
155 cancel_delayed_work(>->tlb_invalidation.fence_tdr);
156 /*
157 * We might have various kworkers waiting for TLB flushes to complete
158 * which are not tracked with an explicit TLB fence, however at this
159 * stage that will never happen since the CT is already disabled, so
160 * make sure we signal them here under the assumption that we have
161 * completed a full GT reset.
162 */
163 if (gt->tlb_invalidation.seqno == 1)
164 pending_seqno = TLB_INVALIDATION_SEQNO_MAX - 1;
165 else
166 pending_seqno = gt->tlb_invalidation.seqno - 1;
167 WRITE_ONCE(gt->tlb_invalidation.seqno_recv, pending_seqno);
168
169 list_for_each_entry_safe(fence, next,
170 >->tlb_invalidation.pending_fences, link)
171 invalidation_fence_signal(gt_to_xe(gt), fence);
172 spin_unlock_irq(>->tlb_invalidation.pending_lock);
173 mutex_unlock(>->uc.guc.ct.lock);
174 }
175
tlb_invalidation_seqno_past(struct xe_gt * gt,int seqno)176 static bool tlb_invalidation_seqno_past(struct xe_gt *gt, int seqno)
177 {
178 int seqno_recv = READ_ONCE(gt->tlb_invalidation.seqno_recv);
179
180 if (seqno - seqno_recv < -(TLB_INVALIDATION_SEQNO_MAX / 2))
181 return false;
182
183 if (seqno - seqno_recv > (TLB_INVALIDATION_SEQNO_MAX / 2))
184 return true;
185
186 return seqno_recv >= seqno;
187 }
188
send_tlb_invalidation(struct xe_guc * guc,struct xe_gt_tlb_invalidation_fence * fence,u32 * action,int len)189 static int send_tlb_invalidation(struct xe_guc *guc,
190 struct xe_gt_tlb_invalidation_fence *fence,
191 u32 *action, int len)
192 {
193 struct xe_gt *gt = guc_to_gt(guc);
194 struct xe_device *xe = gt_to_xe(gt);
195 int seqno;
196 int ret;
197
198 xe_gt_assert(gt, fence);
199
200 /*
201 * XXX: The seqno algorithm relies on TLB invalidation being processed
202 * in order which they currently are, if that changes the algorithm will
203 * need to be updated.
204 */
205
206 mutex_lock(&guc->ct.lock);
207 seqno = gt->tlb_invalidation.seqno;
208 fence->seqno = seqno;
209 trace_xe_gt_tlb_invalidation_fence_send(xe, fence);
210 action[1] = seqno;
211 ret = xe_guc_ct_send_locked(&guc->ct, action, len,
212 G2H_LEN_DW_TLB_INVALIDATE, 1);
213 if (!ret) {
214 spin_lock_irq(>->tlb_invalidation.pending_lock);
215 /*
216 * We haven't actually published the TLB fence as per
217 * pending_fences, but in theory our seqno could have already
218 * been written as we acquired the pending_lock. In such a case
219 * we can just go ahead and signal the fence here.
220 */
221 if (tlb_invalidation_seqno_past(gt, seqno)) {
222 __invalidation_fence_signal(xe, fence);
223 } else {
224 fence->invalidation_time = ktime_get();
225 list_add_tail(&fence->link,
226 >->tlb_invalidation.pending_fences);
227
228 if (list_is_singular(>->tlb_invalidation.pending_fences))
229 queue_delayed_work(system_wq,
230 >->tlb_invalidation.fence_tdr,
231 tlb_timeout_jiffies(gt));
232 }
233 spin_unlock_irq(>->tlb_invalidation.pending_lock);
234 } else {
235 __invalidation_fence_signal(xe, fence);
236 }
237 if (!ret) {
238 gt->tlb_invalidation.seqno = (gt->tlb_invalidation.seqno + 1) %
239 TLB_INVALIDATION_SEQNO_MAX;
240 if (!gt->tlb_invalidation.seqno)
241 gt->tlb_invalidation.seqno = 1;
242 }
243 mutex_unlock(&guc->ct.lock);
244 xe_gt_stats_incr(gt, XE_GT_STATS_ID_TLB_INVAL, 1);
245
246 return ret;
247 }
248
249 #define MAKE_INVAL_OP(type) ((type << XE_GUC_TLB_INVAL_TYPE_SHIFT) | \
250 XE_GUC_TLB_INVAL_MODE_HEAVY << XE_GUC_TLB_INVAL_MODE_SHIFT | \
251 XE_GUC_TLB_INVAL_FLUSH_CACHE)
252
253 /**
254 * xe_gt_tlb_invalidation_guc - Issue a TLB invalidation on this GT for the GuC
255 * @gt: graphics tile
256 * @fence: invalidation fence which will be signal on TLB invalidation
257 * completion
258 *
259 * Issue a TLB invalidation for the GuC. Completion of TLB is asynchronous and
260 * caller can use the invalidation fence to wait for completion.
261 *
262 * Return: 0 on success, negative error code on error
263 */
xe_gt_tlb_invalidation_guc(struct xe_gt * gt,struct xe_gt_tlb_invalidation_fence * fence)264 static int xe_gt_tlb_invalidation_guc(struct xe_gt *gt,
265 struct xe_gt_tlb_invalidation_fence *fence)
266 {
267 u32 action[] = {
268 XE_GUC_ACTION_TLB_INVALIDATION,
269 0, /* seqno, replaced in send_tlb_invalidation */
270 MAKE_INVAL_OP(XE_GUC_TLB_INVAL_GUC),
271 };
272
273 return send_tlb_invalidation(>->uc.guc, fence, action,
274 ARRAY_SIZE(action));
275 }
276
277 /**
278 * xe_gt_tlb_invalidation_ggtt - Issue a TLB invalidation on this GT for the GGTT
279 * @gt: graphics tile
280 *
281 * Issue a TLB invalidation for the GGTT. Completion of TLB invalidation is
282 * synchronous.
283 *
284 * Return: 0 on success, negative error code on error
285 */
xe_gt_tlb_invalidation_ggtt(struct xe_gt * gt)286 int xe_gt_tlb_invalidation_ggtt(struct xe_gt *gt)
287 {
288 struct xe_device *xe = gt_to_xe(gt);
289
290 if (xe_guc_ct_enabled(>->uc.guc.ct) &&
291 gt->uc.guc.submission_state.enabled) {
292 struct xe_gt_tlb_invalidation_fence fence;
293 int ret;
294
295 xe_gt_tlb_invalidation_fence_init(gt, &fence, true);
296 ret = xe_gt_tlb_invalidation_guc(gt, &fence);
297 if (ret)
298 return ret;
299
300 xe_gt_tlb_invalidation_fence_wait(&fence);
301 } else if (xe_device_uc_enabled(xe) && !xe_device_wedged(xe)) {
302 if (IS_SRIOV_VF(xe))
303 return 0;
304
305 xe_gt_WARN_ON(gt, xe_force_wake_get(gt_to_fw(gt), XE_FW_GT));
306 if (xe->info.platform == XE_PVC || GRAPHICS_VER(xe) >= 20) {
307 xe_mmio_write32(gt, PVC_GUC_TLB_INV_DESC1,
308 PVC_GUC_TLB_INV_DESC1_INVALIDATE);
309 xe_mmio_write32(gt, PVC_GUC_TLB_INV_DESC0,
310 PVC_GUC_TLB_INV_DESC0_VALID);
311 } else {
312 xe_mmio_write32(gt, GUC_TLB_INV_CR,
313 GUC_TLB_INV_CR_INVALIDATE);
314 }
315 xe_force_wake_put(gt_to_fw(gt), XE_FW_GT);
316 }
317
318 return 0;
319 }
320
321 /*
322 * Ensure that roundup_pow_of_two(length) doesn't overflow.
323 * Note that roundup_pow_of_two() operates on unsigned long,
324 * not on u64.
325 */
326 #define MAX_RANGE_TLB_INVALIDATION_LENGTH (rounddown_pow_of_two(ULONG_MAX))
327
328 /**
329 * xe_gt_tlb_invalidation_range - Issue a TLB invalidation on this GT for an
330 * address range
331 *
332 * @gt: graphics tile
333 * @fence: invalidation fence which will be signal on TLB invalidation
334 * completion
335 * @start: start address
336 * @end: end address
337 * @asid: address space id
338 *
339 * Issue a range based TLB invalidation if supported, if not fallback to a full
340 * TLB invalidation. Completion of TLB is asynchronous and caller can use
341 * the invalidation fence to wait for completion.
342 *
343 * Return: Negative error code on error, 0 on success
344 */
xe_gt_tlb_invalidation_range(struct xe_gt * gt,struct xe_gt_tlb_invalidation_fence * fence,u64 start,u64 end,u32 asid)345 int xe_gt_tlb_invalidation_range(struct xe_gt *gt,
346 struct xe_gt_tlb_invalidation_fence *fence,
347 u64 start, u64 end, u32 asid)
348 {
349 struct xe_device *xe = gt_to_xe(gt);
350 #define MAX_TLB_INVALIDATION_LEN 7
351 u32 action[MAX_TLB_INVALIDATION_LEN];
352 u64 length = end - start;
353 int len = 0;
354
355 xe_gt_assert(gt, fence);
356
357 /* Execlists not supported */
358 if (gt_to_xe(gt)->info.force_execlist) {
359 __invalidation_fence_signal(xe, fence);
360 return 0;
361 }
362
363 action[len++] = XE_GUC_ACTION_TLB_INVALIDATION;
364 action[len++] = 0; /* seqno, replaced in send_tlb_invalidation */
365 if (!xe->info.has_range_tlb_invalidation ||
366 length > MAX_RANGE_TLB_INVALIDATION_LENGTH) {
367 action[len++] = MAKE_INVAL_OP(XE_GUC_TLB_INVAL_FULL);
368 } else {
369 u64 orig_start = start;
370 u64 align;
371
372 if (length < SZ_4K)
373 length = SZ_4K;
374
375 /*
376 * We need to invalidate a higher granularity if start address
377 * is not aligned to length. When start is not aligned with
378 * length we need to find the length large enough to create an
379 * address mask covering the required range.
380 */
381 align = roundup_pow_of_two(length);
382 start = ALIGN_DOWN(start, align);
383 end = ALIGN(end, align);
384 length = align;
385 while (start + length < end) {
386 length <<= 1;
387 start = ALIGN_DOWN(orig_start, length);
388 }
389
390 /*
391 * Minimum invalidation size for a 2MB page that the hardware
392 * expects is 16MB
393 */
394 if (length >= SZ_2M) {
395 length = max_t(u64, SZ_16M, length);
396 start = ALIGN_DOWN(orig_start, length);
397 }
398
399 xe_gt_assert(gt, length >= SZ_4K);
400 xe_gt_assert(gt, is_power_of_2(length));
401 xe_gt_assert(gt, !(length & GENMASK(ilog2(SZ_16M) - 1,
402 ilog2(SZ_2M) + 1)));
403 xe_gt_assert(gt, IS_ALIGNED(start, length));
404
405 action[len++] = MAKE_INVAL_OP(XE_GUC_TLB_INVAL_PAGE_SELECTIVE);
406 action[len++] = asid;
407 action[len++] = lower_32_bits(start);
408 action[len++] = upper_32_bits(start);
409 action[len++] = ilog2(length) - ilog2(SZ_4K);
410 }
411
412 xe_gt_assert(gt, len <= MAX_TLB_INVALIDATION_LEN);
413
414 return send_tlb_invalidation(>->uc.guc, fence, action, len);
415 }
416
417 /**
418 * xe_gt_tlb_invalidation_vm - Issue a TLB invalidation on this GT for a VM
419 * @gt: graphics tile
420 * @vm: VM to invalidate
421 *
422 * Invalidate entire VM's address space
423 */
xe_gt_tlb_invalidation_vm(struct xe_gt * gt,struct xe_vm * vm)424 void xe_gt_tlb_invalidation_vm(struct xe_gt *gt, struct xe_vm *vm)
425 {
426 struct xe_gt_tlb_invalidation_fence fence;
427 u64 range = 1ull << vm->xe->info.va_bits;
428 int ret;
429
430 xe_gt_tlb_invalidation_fence_init(gt, &fence, true);
431
432 ret = xe_gt_tlb_invalidation_range(gt, &fence, 0, range, vm->usm.asid);
433 if (ret < 0)
434 return;
435
436 xe_gt_tlb_invalidation_fence_wait(&fence);
437 }
438
439 /**
440 * xe_gt_tlb_invalidation_vma - Issue a TLB invalidation on this GT for a VMA
441 * @gt: graphics tile
442 * @fence: invalidation fence which will be signal on TLB invalidation
443 * completion, can be NULL
444 * @vma: VMA to invalidate
445 *
446 * Issue a range based TLB invalidation if supported, if not fallback to a full
447 * TLB invalidation. Completion of TLB is asynchronous and caller can use
448 * the invalidation fence to wait for completion.
449 *
450 * Return: Negative error code on error, 0 on success
451 */
xe_gt_tlb_invalidation_vma(struct xe_gt * gt,struct xe_gt_tlb_invalidation_fence * fence,struct xe_vma * vma)452 int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
453 struct xe_gt_tlb_invalidation_fence *fence,
454 struct xe_vma *vma)
455 {
456 xe_gt_assert(gt, vma);
457
458 return xe_gt_tlb_invalidation_range(gt, fence, xe_vma_start(vma),
459 xe_vma_end(vma),
460 xe_vma_vm(vma)->usm.asid);
461 }
462
463 /**
464 * xe_guc_tlb_invalidation_done_handler - TLB invalidation done handler
465 * @guc: guc
466 * @msg: message indicating TLB invalidation done
467 * @len: length of message
468 *
469 * Parse seqno of TLB invalidation, wake any waiters for seqno, and signal any
470 * invalidation fences for seqno. Algorithm for this depends on seqno being
471 * received in-order and asserts this assumption.
472 *
473 * Return: 0 on success, -EPROTO for malformed messages.
474 */
xe_guc_tlb_invalidation_done_handler(struct xe_guc * guc,u32 * msg,u32 len)475 int xe_guc_tlb_invalidation_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
476 {
477 struct xe_gt *gt = guc_to_gt(guc);
478 struct xe_device *xe = gt_to_xe(gt);
479 struct xe_gt_tlb_invalidation_fence *fence, *next;
480 unsigned long flags;
481
482 if (unlikely(len != 1))
483 return -EPROTO;
484
485 /*
486 * This can also be run both directly from the IRQ handler and also in
487 * process_g2h_msg(). Only one may process any individual CT message,
488 * however the order they are processed here could result in skipping a
489 * seqno. To handle that we just process all the seqnos from the last
490 * seqno_recv up to and including the one in msg[0]. The delta should be
491 * very small so there shouldn't be much of pending_fences we actually
492 * need to iterate over here.
493 *
494 * From GuC POV we expect the seqnos to always appear in-order, so if we
495 * see something later in the timeline we can be sure that anything
496 * appearing earlier has already signalled, just that we have yet to
497 * officially process the CT message like if racing against
498 * process_g2h_msg().
499 */
500 spin_lock_irqsave(>->tlb_invalidation.pending_lock, flags);
501 if (tlb_invalidation_seqno_past(gt, msg[0])) {
502 spin_unlock_irqrestore(>->tlb_invalidation.pending_lock, flags);
503 return 0;
504 }
505
506 WRITE_ONCE(gt->tlb_invalidation.seqno_recv, msg[0]);
507
508 list_for_each_entry_safe(fence, next,
509 >->tlb_invalidation.pending_fences, link) {
510 trace_xe_gt_tlb_invalidation_fence_recv(xe, fence);
511
512 if (!tlb_invalidation_seqno_past(gt, fence->seqno))
513 break;
514
515 invalidation_fence_signal(xe, fence);
516 }
517
518 if (!list_empty(>->tlb_invalidation.pending_fences))
519 mod_delayed_work(system_wq,
520 >->tlb_invalidation.fence_tdr,
521 tlb_timeout_jiffies(gt));
522 else
523 cancel_delayed_work(>->tlb_invalidation.fence_tdr);
524
525 spin_unlock_irqrestore(>->tlb_invalidation.pending_lock, flags);
526
527 return 0;
528 }
529
530 static const char *
invalidation_fence_get_driver_name(struct dma_fence * dma_fence)531 invalidation_fence_get_driver_name(struct dma_fence *dma_fence)
532 {
533 return "xe";
534 }
535
536 static const char *
invalidation_fence_get_timeline_name(struct dma_fence * dma_fence)537 invalidation_fence_get_timeline_name(struct dma_fence *dma_fence)
538 {
539 return "invalidation_fence";
540 }
541
542 static const struct dma_fence_ops invalidation_fence_ops = {
543 .get_driver_name = invalidation_fence_get_driver_name,
544 .get_timeline_name = invalidation_fence_get_timeline_name,
545 };
546
547 /**
548 * xe_gt_tlb_invalidation_fence_init - Initialize TLB invalidation fence
549 * @gt: GT
550 * @fence: TLB invalidation fence to initialize
551 * @stack: fence is stack variable
552 *
553 * Initialize TLB invalidation fence for use. xe_gt_tlb_invalidation_fence_fini
554 * will be automatically called when fence is signalled (all fences must signal),
555 * even on error.
556 */
xe_gt_tlb_invalidation_fence_init(struct xe_gt * gt,struct xe_gt_tlb_invalidation_fence * fence,bool stack)557 void xe_gt_tlb_invalidation_fence_init(struct xe_gt *gt,
558 struct xe_gt_tlb_invalidation_fence *fence,
559 bool stack)
560 {
561 xe_pm_runtime_get_noresume(gt_to_xe(gt));
562
563 spin_lock_irq(>->tlb_invalidation.lock);
564 dma_fence_init(&fence->base, &invalidation_fence_ops,
565 >->tlb_invalidation.lock,
566 dma_fence_context_alloc(1), 1);
567 spin_unlock_irq(>->tlb_invalidation.lock);
568 INIT_LIST_HEAD(&fence->link);
569 if (stack)
570 set_bit(FENCE_STACK_BIT, &fence->base.flags);
571 else
572 dma_fence_get(&fence->base);
573 fence->gt = gt;
574 }
575