xref: /linux/drivers/gpu/drm/xe/xe_guc_ct.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_guc_ct.h"
7 
8 #include <linux/bitfield.h>
9 #include <linux/circ_buf.h>
10 #include <linux/delay.h>
11 #include <linux/fault-inject.h>
12 
13 #include <kunit/static_stub.h>
14 
15 #include <drm/drm_managed.h>
16 
17 #include "abi/guc_actions_abi.h"
18 #include "abi/guc_actions_sriov_abi.h"
19 #include "abi/guc_klvs_abi.h"
20 #include "xe_bo.h"
21 #include "xe_devcoredump.h"
22 #include "xe_device.h"
23 #include "xe_gt.h"
24 #include "xe_gt_printk.h"
25 #include "xe_gt_sriov_pf_control.h"
26 #include "xe_gt_sriov_pf_monitor.h"
27 #include "xe_guc.h"
28 #include "xe_guc_log.h"
29 #include "xe_guc_pagefault.h"
30 #include "xe_guc_relay.h"
31 #include "xe_guc_submit.h"
32 #include "xe_guc_tlb_inval.h"
33 #include "xe_map.h"
34 #include "xe_page_reclaim.h"
35 #include "xe_pm.h"
36 #include "xe_sleep.h"
37 #include "xe_sriov_vf.h"
38 #include "xe_trace_guc.h"
39 
40 static void receive_g2h(struct xe_guc_ct *ct);
41 static void g2h_worker_func(struct work_struct *w);
42 static void safe_mode_worker_func(struct work_struct *w);
43 static void ct_exit_safe_mode(struct xe_guc_ct *ct);
44 static void guc_ct_change_state(struct xe_guc_ct *ct,
45 				enum xe_guc_ct_state state);
46 
47 static struct xe_guc *ct_to_guc(struct xe_guc_ct *ct)
48 {
49 	return container_of(ct, struct xe_guc, ct);
50 }
51 
52 static struct xe_gt *ct_to_gt(struct xe_guc_ct *ct)
53 {
54 	return container_of(ct, struct xe_gt, uc.guc.ct);
55 }
56 
57 static struct xe_device *ct_to_xe(struct xe_guc_ct *ct)
58 {
59 	return gt_to_xe(ct_to_gt(ct));
60 }
61 
62 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG)
63 enum {
64 	/* Internal states, not error conditions */
65 	CT_DEAD_STATE_REARM,			/* 0x0001 */
66 	CT_DEAD_STATE_CAPTURE,			/* 0x0002 */
67 
68 	/* Error conditions */
69 	CT_DEAD_SETUP,				/* 0x0004 */
70 	CT_DEAD_H2G_WRITE,			/* 0x0008 */
71 	CT_DEAD_H2G_HAS_ROOM,			/* 0x0010 */
72 	CT_DEAD_G2H_READ,			/* 0x0020 */
73 	CT_DEAD_G2H_RECV,			/* 0x0040 */
74 	CT_DEAD_G2H_RELEASE,			/* 0x0080 */
75 	CT_DEAD_DEADLOCK,			/* 0x0100 */
76 	CT_DEAD_PROCESS_FAILED,			/* 0x0200 */
77 	CT_DEAD_FAST_G2H,			/* 0x0400 */
78 	CT_DEAD_PARSE_G2H_RESPONSE,		/* 0x0800 */
79 	CT_DEAD_PARSE_G2H_UNKNOWN,		/* 0x1000 */
80 	CT_DEAD_PARSE_G2H_ORIGIN,		/* 0x2000 */
81 	CT_DEAD_PARSE_G2H_TYPE,			/* 0x4000 */
82 	CT_DEAD_CRASH,				/* 0x8000 */
83 };
84 
85 static void ct_dead_worker_func(struct work_struct *w);
86 static void ct_dead_capture(struct xe_guc_ct *ct, struct guc_ctb *ctb, u32 reason_code);
87 
88 static void ct_dead_fini(struct xe_guc_ct *ct)
89 {
90 	cancel_work_sync(&ct->dead.worker);
91 }
92 
93 static void ct_dead_init(struct xe_guc_ct *ct)
94 {
95 	spin_lock_init(&ct->dead.lock);
96 	INIT_WORK(&ct->dead.worker, ct_dead_worker_func);
97 
98 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_GUC)
99 	stack_depot_init();
100 #endif
101 }
102 
103 static void fast_req_stack_save(struct xe_guc_ct *ct, unsigned int slot)
104 {
105 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_GUC)
106 	unsigned long entries[SZ_32];
107 	unsigned int n;
108 
109 	n = stack_trace_save(entries, ARRAY_SIZE(entries), 1);
110 	/* May be called under spinlock, so avoid sleeping */
111 	ct->fast_req[slot].stack = stack_depot_save(entries, n, GFP_NOWAIT);
112 #endif
113 }
114 
115 static void fast_req_dump(struct xe_guc_ct *ct, u16 fence, unsigned int slot)
116 {
117 	struct xe_gt *gt = ct_to_gt(ct);
118 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_GUC)
119 	char *buf __cleanup(kfree) = kmalloc(SZ_4K, GFP_NOWAIT);
120 
121 	if (buf && stack_depot_snprint(ct->fast_req[slot].stack, buf, SZ_4K, 0))
122 		xe_gt_err(gt, "Fence 0x%x was used by action %#04x sent at:\n%s\n",
123 			  fence, ct->fast_req[slot].action, buf);
124 	else
125 		xe_gt_err(gt, "Fence 0x%x was used by action %#04x [failed to retrieve stack]\n",
126 			  fence, ct->fast_req[slot].action);
127 #else
128 	xe_gt_err(gt, "Fence 0x%x was used by action %#04x\n",
129 		  fence, ct->fast_req[slot].action);
130 #endif
131 }
132 
133 static void fast_req_report(struct xe_guc_ct *ct, u16 fence)
134 {
135 	u16 fence_min = U16_MAX, fence_max = 0;
136 	struct xe_gt *gt = ct_to_gt(ct);
137 	unsigned int n;
138 
139 	lockdep_assert_held(&ct->lock);
140 
141 	for (n = 0; n < ARRAY_SIZE(ct->fast_req); n++) {
142 		if (ct->fast_req[n].fence < fence_min)
143 			fence_min = ct->fast_req[n].fence;
144 		if (ct->fast_req[n].fence > fence_max)
145 			fence_max = ct->fast_req[n].fence;
146 
147 		if (ct->fast_req[n].fence != fence)
148 			continue;
149 
150 		return fast_req_dump(ct, fence, n);
151 	}
152 
153 	xe_gt_warn(gt, "Fence 0x%x not found - tracking buffer wrapped? [range = 0x%x -> 0x%x, next = 0x%X]\n",
154 		   fence, fence_min, fence_max, ct->fence_seqno);
155 }
156 
157 static void fast_req_track(struct xe_guc_ct *ct, u16 fence, u16 action)
158 {
159 	unsigned int slot = fence % ARRAY_SIZE(ct->fast_req);
160 
161 	fast_req_stack_save(ct, slot);
162 	ct->fast_req[slot].fence = fence;
163 	ct->fast_req[slot].action = action;
164 }
165 
166 #define CT_DEAD(ct, ctb, reason_code)	ct_dead_capture((ct), (ctb), CT_DEAD_##reason_code)
167 
168 #else
169 
170 static void ct_dead_fini(struct xe_guc_ct *ct) { }
171 static void ct_dead_init(struct xe_guc_ct *ct) { }
172 
173 static void fast_req_report(struct xe_guc_ct *ct, u16 fence) { }
174 static void fast_req_track(struct xe_guc_ct *ct, u16 fence, u16 action) { }
175 
176 #define CT_DEAD(ct, ctb, reason)			\
177 	do {						\
178 		struct guc_ctb *_ctb = (ctb);		\
179 		if (_ctb)				\
180 			_ctb->info.broken = true;	\
181 	} while (0)
182 
183 #endif
184 
185 /* Used when a CT send wants to block and / or receive data */
186 struct g2h_fence {
187 	u32 *response_buffer;
188 	u32 seqno;
189 	/* fields below this point are setup based on the response */
190 	u32 response_data;
191 	u16 response_len;
192 	u16 error;
193 	u16 hint;
194 	u16 reason;
195 	u32 counter;
196 	bool cancel;
197 	bool retry;
198 	bool wait;
199 	bool fail;
200 	bool done;
201 };
202 
203 static void g2h_fence_init(struct g2h_fence *g2h_fence, u32 *response_buffer)
204 {
205 	memset(g2h_fence, 0, sizeof(*g2h_fence));
206 	g2h_fence->response_buffer = response_buffer;
207 	g2h_fence->seqno = ~0x0;
208 }
209 
210 static void g2h_fence_reinit(struct g2h_fence *g2h_fence)
211 {
212 	memset_after(g2h_fence, 0, seqno);
213 }
214 
215 static void g2h_fence_cancel(struct g2h_fence *g2h_fence)
216 {
217 	g2h_fence->cancel = true;
218 	g2h_fence->fail = true;
219 
220 	/* WRITE_ONCE pairs with READ_ONCEs in guc_ct_send_recv. */
221 	WRITE_ONCE(g2h_fence->done, true);
222 }
223 
224 static bool g2h_fence_needs_alloc(struct g2h_fence *g2h_fence)
225 {
226 	return g2h_fence->seqno == ~0x0;
227 }
228 
229 /**
230  * DOC: GuC CTB Blob
231  *
232  * We allocate single blob to hold both CTB descriptors and buffers:
233  *
234  *      +--------+-----------------------------------------------+------+
235  *      | offset | contents                                      | size |
236  *      +========+===============================================+======+
237  *      | 0x0000 | H2G CTB Descriptor (send)                     |      |
238  *      +--------+-----------------------------------------------+  4K  |
239  *      | 0x0800 | G2H CTB Descriptor (g2h)                      |      |
240  *      +--------+-----------------------------------------------+------+
241  *      | 0x1000 | H2G CT Buffer (send)                          | n*4K |
242  *      |        |                                               |      |
243  *      +--------+-----------------------------------------------+------+
244  *      | 0x1000 | G2H CT Buffer (g2h)                           | m*4K |
245  *      | + n*4K |                                               |      |
246  *      +--------+-----------------------------------------------+------+
247  *
248  * Size of each ``CT Buffer`` must be multiple of 4K.
249  * We don't expect too many messages in flight at any time, unless we are
250  * using the GuC submission. In that case each request requires a minimum
251  * 2 dwords which gives us a maximum 256 queue'd requests. Hopefully this
252  * enough space to avoid backpressure on the driver. We increase the size
253  * of the receive buffer (relative to the send) to ensure a G2H response
254  * CTB has a landing spot.
255  *
256  * In addition to submissions, the G2H buffer needs to be able to hold
257  * enough space for recoverable page fault notifications. The number of
258  * page faults is interrupt driven and can be as much as the number of
259  * compute resources available. However, most of the actual work for these
260  * is in a separate page fault worker thread. Therefore we only need to
261  * make sure the queue has enough space to handle all of the submissions
262  * and responses and an extra buffer for incoming page faults.
263  */
264 
265 #define CTB_DESC_SIZE		ALIGN(sizeof(struct guc_ct_buffer_desc), SZ_2K)
266 #define CTB_H2G_BUFFER_OFFSET	(CTB_DESC_SIZE * 2)
267 #define CTB_G2H_BUFFER_OFFSET	(CTB_DESC_SIZE * 2)
268 #define CTB_H2G_BUFFER_SIZE	(SZ_4K)
269 #define CTB_H2G_BUFFER_DWORDS	(CTB_H2G_BUFFER_SIZE / sizeof(u32))
270 #define CTB_G2H_BUFFER_SIZE	(SZ_128K)
271 #define CTB_G2H_BUFFER_DWORDS	(CTB_G2H_BUFFER_SIZE / sizeof(u32))
272 #define G2H_ROOM_BUFFER_SIZE	(CTB_G2H_BUFFER_SIZE / 2)
273 #define G2H_ROOM_BUFFER_DWORDS	(CTB_G2H_BUFFER_DWORDS / 2)
274 
275 /**
276  * xe_guc_ct_queue_proc_time_jiffies - Return maximum time to process a full
277  * CT command queue
278  * @ct: the &xe_guc_ct. Unused at this moment but will be used in the future.
279  *
280  * Observation is that a 4KiB buffer full of commands takes a little over a
281  * second to process. Use that to calculate maximum time to process a full CT
282  * command queue.
283  *
284  * Return: Maximum time to process a full CT queue in jiffies.
285  */
286 long xe_guc_ct_queue_proc_time_jiffies(struct xe_guc_ct *ct)
287 {
288 	BUILD_BUG_ON(!IS_ALIGNED(CTB_H2G_BUFFER_SIZE, SZ_4K));
289 	return (CTB_H2G_BUFFER_SIZE / SZ_4K) * HZ;
290 }
291 
292 static size_t guc_h2g_size(void)
293 {
294 	return CTB_H2G_BUFFER_OFFSET + CTB_H2G_BUFFER_SIZE;
295 }
296 
297 static size_t guc_g2h_size(void)
298 {
299 	return CTB_G2H_BUFFER_OFFSET + CTB_G2H_BUFFER_SIZE;
300 }
301 
302 static void guc_ct_fini(struct drm_device *drm, void *arg)
303 {
304 	struct xe_guc_ct *ct = arg;
305 
306 	ct_dead_fini(ct);
307 	ct_exit_safe_mode(ct);
308 	destroy_workqueue(ct->g2h_wq);
309 	xa_destroy(&ct->fence_lookup);
310 }
311 
312 static void primelockdep(struct xe_guc_ct *ct)
313 {
314 	if (!IS_ENABLED(CONFIG_LOCKDEP))
315 		return;
316 
317 	fs_reclaim_acquire(GFP_KERNEL);
318 	might_lock(&ct->lock);
319 	fs_reclaim_release(GFP_KERNEL);
320 }
321 
322 int xe_guc_ct_init_noalloc(struct xe_guc_ct *ct)
323 {
324 	struct xe_device *xe = ct_to_xe(ct);
325 	struct xe_gt *gt = ct_to_gt(ct);
326 	int err;
327 
328 	xe_gt_assert(gt, !(guc_h2g_size() % PAGE_SIZE));
329 	xe_gt_assert(gt, !(guc_g2h_size() % PAGE_SIZE));
330 
331 	err = drmm_mutex_init(&xe->drm, &ct->lock);
332 	if (err)
333 		return err;
334 
335 	primelockdep(ct);
336 
337 	ct->g2h_wq = alloc_ordered_workqueue("xe-g2h-wq", WQ_MEM_RECLAIM);
338 	if (!ct->g2h_wq)
339 		return -ENOMEM;
340 
341 	spin_lock_init(&ct->fast_lock);
342 	xa_init(&ct->fence_lookup);
343 	INIT_WORK(&ct->g2h_worker, g2h_worker_func);
344 	INIT_DELAYED_WORK(&ct->safe_mode_worker, safe_mode_worker_func);
345 
346 	ct_dead_init(ct);
347 	init_waitqueue_head(&ct->wq);
348 	init_waitqueue_head(&ct->g2h_fence_wq);
349 
350 	err = drmm_add_action_or_reset(&xe->drm, guc_ct_fini, ct);
351 	if (err)
352 		return err;
353 
354 	xe_gt_assert(gt, ct->state == XE_GUC_CT_STATE_NOT_INITIALIZED);
355 	ct->state = XE_GUC_CT_STATE_DISABLED;
356 	return 0;
357 }
358 ALLOW_ERROR_INJECTION(xe_guc_ct_init_noalloc, ERRNO); /* See xe_pci_probe() */
359 
360 static void guc_action_disable_ct(void *arg)
361 {
362 	struct xe_guc_ct *ct = arg;
363 
364 	xe_guc_ct_stop(ct);
365 	guc_ct_change_state(ct, XE_GUC_CT_STATE_DISABLED);
366 }
367 
368 int xe_guc_ct_init(struct xe_guc_ct *ct)
369 {
370 	struct xe_device *xe = ct_to_xe(ct);
371 	struct xe_gt *gt = ct_to_gt(ct);
372 	struct xe_tile *tile = gt_to_tile(gt);
373 	struct xe_bo *bo;
374 
375 	bo = xe_managed_bo_create_pin_map(xe, tile, guc_h2g_size(),
376 					  XE_BO_FLAG_SYSTEM |
377 					  XE_BO_FLAG_GGTT |
378 					  XE_BO_FLAG_GGTT_INVALIDATE |
379 					  XE_BO_FLAG_PINNED_NORESTORE);
380 	if (IS_ERR(bo))
381 		return PTR_ERR(bo);
382 
383 	ct->ctbs.h2g.bo = bo;
384 
385 	bo = xe_managed_bo_create_pin_map(xe, tile, guc_g2h_size(),
386 					  XE_BO_FLAG_SYSTEM |
387 					  XE_BO_FLAG_GGTT |
388 					  XE_BO_FLAG_GGTT_INVALIDATE |
389 					  XE_BO_FLAG_PINNED_NORESTORE);
390 	if (IS_ERR(bo))
391 		return PTR_ERR(bo);
392 
393 	ct->ctbs.g2h.bo = bo;
394 
395 	return devm_add_action_or_reset(xe->drm.dev, guc_action_disable_ct, ct);
396 }
397 ALLOW_ERROR_INJECTION(xe_guc_ct_init, ERRNO); /* See xe_pci_probe() */
398 
399 /**
400  * xe_guc_ct_init_post_hwconfig - Reinitialize the GuC CTB in VRAM
401  * @ct: the &xe_guc_ct
402  *
403  * Allocate a new BO in VRAM and free the previous BO that was allocated
404  * in system memory (SMEM). Applicable only for DGFX products.
405  *
406  * Return: 0 on success, or a negative errno on failure.
407  */
408 int xe_guc_ct_init_post_hwconfig(struct xe_guc_ct *ct)
409 {
410 	struct xe_device *xe = ct_to_xe(ct);
411 	struct xe_gt *gt = ct_to_gt(ct);
412 	struct xe_tile *tile = gt_to_tile(gt);
413 	int ret;
414 
415 	xe_assert(xe, !xe_guc_ct_enabled(ct));
416 
417 	if (IS_DGFX(xe)) {
418 		ret = xe_managed_bo_reinit_in_vram(xe, tile, &ct->ctbs.h2g.bo);
419 		if (ret)
420 			return ret;
421 	}
422 
423 	devm_remove_action(xe->drm.dev, guc_action_disable_ct, ct);
424 	return devm_add_action_or_reset(xe->drm.dev, guc_action_disable_ct, ct);
425 }
426 
427 #define desc_read(xe_, guc_ctb__, field_)			\
428 	xe_map_rd_field(xe_, &guc_ctb__->desc, 0,		\
429 			struct guc_ct_buffer_desc, field_)
430 
431 #define desc_write(xe_, guc_ctb__, field_, val_)		\
432 	xe_map_wr_field(xe_, &guc_ctb__->desc, 0,		\
433 			struct guc_ct_buffer_desc, field_, val_)
434 
435 static void guc_ct_ctb_h2g_init(struct xe_device *xe, struct guc_ctb *h2g,
436 				struct iosys_map *map)
437 {
438 	h2g->info.size = CTB_H2G_BUFFER_DWORDS;
439 	h2g->info.resv_space = 0;
440 	h2g->info.tail = 0;
441 	h2g->info.head = 0;
442 	h2g->info.space = CIRC_SPACE(h2g->info.tail, h2g->info.head,
443 				     h2g->info.size) -
444 			  h2g->info.resv_space;
445 	h2g->info.broken = false;
446 
447 	h2g->desc = *map;
448 	xe_map_memset(xe, &h2g->desc, 0, 0, sizeof(struct guc_ct_buffer_desc));
449 
450 	h2g->cmds = IOSYS_MAP_INIT_OFFSET(map, CTB_H2G_BUFFER_OFFSET);
451 }
452 
453 static void guc_ct_ctb_g2h_init(struct xe_device *xe, struct guc_ctb *g2h,
454 				struct iosys_map *map)
455 {
456 	g2h->info.size = CTB_G2H_BUFFER_DWORDS;
457 	g2h->info.resv_space = G2H_ROOM_BUFFER_DWORDS;
458 	g2h->info.head = 0;
459 	g2h->info.tail = 0;
460 	g2h->info.space = CIRC_SPACE(g2h->info.tail, g2h->info.head,
461 				     g2h->info.size) -
462 			  g2h->info.resv_space;
463 	g2h->info.broken = false;
464 
465 	g2h->desc = IOSYS_MAP_INIT_OFFSET(map, CTB_DESC_SIZE);
466 	xe_map_memset(xe, &g2h->desc, 0, 0, sizeof(struct guc_ct_buffer_desc));
467 
468 	g2h->cmds = IOSYS_MAP_INIT_OFFSET(map, CTB_G2H_BUFFER_OFFSET);
469 }
470 
471 static int guc_ct_ctb_h2g_register(struct xe_guc_ct *ct)
472 {
473 	struct xe_guc *guc = ct_to_guc(ct);
474 	u32 desc_addr, ctb_addr, size;
475 	int err;
476 
477 	desc_addr = xe_bo_ggtt_addr(ct->ctbs.h2g.bo);
478 	ctb_addr = xe_bo_ggtt_addr(ct->ctbs.h2g.bo) + CTB_H2G_BUFFER_OFFSET;
479 	size = ct->ctbs.h2g.info.size * sizeof(u32);
480 
481 	err = xe_guc_self_cfg64(guc,
482 				GUC_KLV_SELF_CFG_H2G_CTB_DESCRIPTOR_ADDR_KEY,
483 				desc_addr);
484 	if (err)
485 		return err;
486 
487 	err = xe_guc_self_cfg64(guc,
488 				GUC_KLV_SELF_CFG_H2G_CTB_ADDR_KEY,
489 				ctb_addr);
490 	if (err)
491 		return err;
492 
493 	return xe_guc_self_cfg32(guc,
494 				 GUC_KLV_SELF_CFG_H2G_CTB_SIZE_KEY,
495 				 size);
496 }
497 
498 static int guc_ct_ctb_g2h_register(struct xe_guc_ct *ct)
499 {
500 	struct xe_guc *guc = ct_to_guc(ct);
501 	u32 desc_addr, ctb_addr, size;
502 	int err;
503 
504 	desc_addr = xe_bo_ggtt_addr(ct->ctbs.g2h.bo) + CTB_DESC_SIZE;
505 	ctb_addr = xe_bo_ggtt_addr(ct->ctbs.g2h.bo) + CTB_G2H_BUFFER_OFFSET;
506 	size = ct->ctbs.g2h.info.size * sizeof(u32);
507 
508 	err = xe_guc_self_cfg64(guc,
509 				GUC_KLV_SELF_CFG_G2H_CTB_DESCRIPTOR_ADDR_KEY,
510 				desc_addr);
511 	if (err)
512 		return err;
513 
514 	err = xe_guc_self_cfg64(guc,
515 				GUC_KLV_SELF_CFG_G2H_CTB_ADDR_KEY,
516 				ctb_addr);
517 	if (err)
518 		return err;
519 
520 	return xe_guc_self_cfg32(guc,
521 				 GUC_KLV_SELF_CFG_G2H_CTB_SIZE_KEY,
522 				 size);
523 }
524 
525 static int guc_ct_control_toggle(struct xe_guc_ct *ct, bool enable)
526 {
527 	u32 request[HOST2GUC_CONTROL_CTB_REQUEST_MSG_LEN] = {
528 		FIELD_PREP(GUC_HXG_MSG_0_ORIGIN, GUC_HXG_ORIGIN_HOST) |
529 		FIELD_PREP(GUC_HXG_MSG_0_TYPE, GUC_HXG_TYPE_REQUEST) |
530 		FIELD_PREP(GUC_HXG_REQUEST_MSG_0_ACTION,
531 			   GUC_ACTION_HOST2GUC_CONTROL_CTB),
532 		FIELD_PREP(HOST2GUC_CONTROL_CTB_REQUEST_MSG_1_CONTROL,
533 			   enable ? GUC_CTB_CONTROL_ENABLE :
534 			   GUC_CTB_CONTROL_DISABLE),
535 	};
536 	int ret = xe_guc_mmio_send(ct_to_guc(ct), request, ARRAY_SIZE(request));
537 
538 	return ret > 0 ? -EPROTO : ret;
539 }
540 
541 static void guc_ct_change_state(struct xe_guc_ct *ct,
542 				enum xe_guc_ct_state state)
543 {
544 	struct xe_gt *gt = ct_to_gt(ct);
545 	struct g2h_fence *g2h_fence;
546 	unsigned long idx;
547 
548 	mutex_lock(&ct->lock);		/* Serialise dequeue_one_g2h() */
549 	spin_lock_irq(&ct->fast_lock);	/* Serialise CT fast-path */
550 
551 	xe_gt_assert(ct_to_gt(ct), ct->g2h_outstanding == 0 ||
552 		     state == XE_GUC_CT_STATE_STOPPED);
553 
554 	if (ct->g2h_outstanding)
555 		xe_pm_runtime_put(ct_to_xe(ct));
556 	ct->g2h_outstanding = 0;
557 
558 	/*
559 	 * WRITE_ONCE pairs with READ_ONCEs in xe_guc_ct_initialized and
560 	 * xe_guc_ct_enabled.
561 	 */
562 	WRITE_ONCE(ct->state, state);
563 
564 	xe_gt_dbg(gt, "GuC CT communication channel %s\n",
565 		  state == XE_GUC_CT_STATE_STOPPED ? "stopped" :
566 		  str_enabled_disabled(state == XE_GUC_CT_STATE_ENABLED));
567 
568 	spin_unlock_irq(&ct->fast_lock);
569 
570 	/* cancel all in-flight send-recv requests */
571 	xa_for_each(&ct->fence_lookup, idx, g2h_fence)
572 		g2h_fence_cancel(g2h_fence);
573 
574 	/* make sure guc_ct_send_recv() will see g2h_fence changes */
575 	smp_mb();
576 	wake_up_all(&ct->g2h_fence_wq);
577 
578 	/*
579 	 * Lockdep doesn't like this under the fast lock and he destroy only
580 	 * needs to be serialized with the send path which ct lock provides.
581 	 */
582 	xa_destroy(&ct->fence_lookup);
583 
584 	mutex_unlock(&ct->lock);
585 }
586 
587 static bool ct_needs_safe_mode(struct xe_guc_ct *ct)
588 {
589 	return !pci_dev_msi_enabled(to_pci_dev(ct_to_xe(ct)->drm.dev));
590 }
591 
592 static bool ct_restart_safe_mode_worker(struct xe_guc_ct *ct)
593 {
594 	if (!ct_needs_safe_mode(ct))
595 		return false;
596 
597 	queue_delayed_work(ct->g2h_wq, &ct->safe_mode_worker, HZ / 10);
598 	return true;
599 }
600 
601 static void safe_mode_worker_func(struct work_struct *w)
602 {
603 	struct xe_guc_ct *ct = container_of(w, struct xe_guc_ct, safe_mode_worker.work);
604 
605 	receive_g2h(ct);
606 
607 	if (!ct_restart_safe_mode_worker(ct))
608 		xe_gt_dbg(ct_to_gt(ct), "GuC CT safe-mode canceled\n");
609 }
610 
611 static void ct_enter_safe_mode(struct xe_guc_ct *ct)
612 {
613 	if (ct_restart_safe_mode_worker(ct))
614 		xe_gt_dbg(ct_to_gt(ct), "GuC CT safe-mode enabled\n");
615 }
616 
617 static void ct_exit_safe_mode(struct xe_guc_ct *ct)
618 {
619 	if (cancel_delayed_work_sync(&ct->safe_mode_worker))
620 		xe_gt_dbg(ct_to_gt(ct), "GuC CT safe-mode disabled\n");
621 }
622 
623 static int __xe_guc_ct_start(struct xe_guc_ct *ct, bool needs_register)
624 {
625 	struct xe_device *xe = ct_to_xe(ct);
626 	struct xe_gt *gt = ct_to_gt(ct);
627 	int err;
628 
629 	xe_gt_assert(gt, !xe_guc_ct_enabled(ct));
630 
631 	if (needs_register) {
632 		xe_map_memset(xe, &ct->ctbs.h2g.bo->vmap, 0, 0,
633 			      xe_bo_size(ct->ctbs.h2g.bo));
634 		xe_map_memset(xe, &ct->ctbs.g2h.bo->vmap, 0, 0,
635 			      xe_bo_size(ct->ctbs.g2h.bo));
636 		guc_ct_ctb_h2g_init(xe, &ct->ctbs.h2g, &ct->ctbs.h2g.bo->vmap);
637 		guc_ct_ctb_g2h_init(xe, &ct->ctbs.g2h, &ct->ctbs.g2h.bo->vmap);
638 
639 		err = guc_ct_ctb_h2g_register(ct);
640 		if (err)
641 			goto err_out;
642 
643 		err = guc_ct_ctb_g2h_register(ct);
644 		if (err)
645 			goto err_out;
646 
647 		err = guc_ct_control_toggle(ct, true);
648 		if (err)
649 			goto err_out;
650 	} else {
651 		ct->ctbs.h2g.info.broken = false;
652 		ct->ctbs.g2h.info.broken = false;
653 		/* Skip everything in H2G buffer */
654 		xe_map_memset(xe, &ct->ctbs.h2g.bo->vmap, CTB_H2G_BUFFER_OFFSET, 0,
655 			      CTB_H2G_BUFFER_SIZE);
656 	}
657 
658 	guc_ct_change_state(ct, XE_GUC_CT_STATE_ENABLED);
659 
660 	smp_mb();
661 	wake_up_all(&ct->wq);
662 
663 	if (ct_needs_safe_mode(ct))
664 		ct_enter_safe_mode(ct);
665 
666 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG)
667 	/*
668 	 * The CT has now been reset so the dumper can be re-armed
669 	 * after any existing dead state has been dumped.
670 	 */
671 	spin_lock_irq(&ct->dead.lock);
672 	if (ct->dead.reason) {
673 		ct->dead.reason |= (1 << CT_DEAD_STATE_REARM);
674 		queue_work(system_dfl_wq, &ct->dead.worker);
675 	}
676 	spin_unlock_irq(&ct->dead.lock);
677 #endif
678 
679 	return 0;
680 
681 err_out:
682 	xe_gt_err(gt, "Failed to enable GuC CT (%pe)\n", ERR_PTR(err));
683 	CT_DEAD(ct, NULL, SETUP);
684 
685 	return err;
686 }
687 
688 /**
689  * xe_guc_ct_restart() - Restart GuC CT
690  * @ct: the &xe_guc_ct
691  *
692  * Restart GuC CT to an empty state without issuing a CT register MMIO command.
693  *
694  * Return: 0 on success, or a negative errno on failure.
695  */
696 int xe_guc_ct_restart(struct xe_guc_ct *ct)
697 {
698 	return __xe_guc_ct_start(ct, false);
699 }
700 
701 /**
702  * xe_guc_ct_enable() - Enable GuC CT
703  * @ct: the &xe_guc_ct
704  *
705  * Enable GuC CT to an empty state and issue a CT register MMIO command.
706  *
707  * Return: 0 on success, or a negative errno on failure.
708  */
709 int xe_guc_ct_enable(struct xe_guc_ct *ct)
710 {
711 	return __xe_guc_ct_start(ct, true);
712 }
713 
714 static void stop_g2h_handler(struct xe_guc_ct *ct)
715 {
716 	cancel_work_sync(&ct->g2h_worker);
717 }
718 
719 /**
720  * xe_guc_ct_disable - Set GuC to disabled state
721  * @ct: the &xe_guc_ct
722  *
723  * Set GuC CT to disabled state and stop g2h handler. No outstanding g2h expected
724  * in this transition.
725  */
726 void xe_guc_ct_disable(struct xe_guc_ct *ct)
727 {
728 	guc_ct_change_state(ct, XE_GUC_CT_STATE_DISABLED);
729 	ct_exit_safe_mode(ct);
730 	stop_g2h_handler(ct);
731 }
732 
733 /**
734  * xe_guc_ct_flush_and_stop - Flush and stop all processing of G2H / H2G
735  * @ct: the &xe_guc_ct
736  */
737 void xe_guc_ct_flush_and_stop(struct xe_guc_ct *ct)
738 {
739 	receive_g2h(ct);
740 	xe_guc_ct_stop(ct);
741 }
742 
743 /**
744  * xe_guc_ct_stop - Set GuC to stopped state
745  * @ct: the &xe_guc_ct
746  *
747  * Set GuC CT to stopped state, stop g2h handler, and clear any outstanding g2h
748  */
749 void xe_guc_ct_stop(struct xe_guc_ct *ct)
750 {
751 	if (!xe_guc_ct_initialized(ct))
752 		return;
753 
754 	guc_ct_change_state(ct, XE_GUC_CT_STATE_STOPPED);
755 	stop_g2h_handler(ct);
756 }
757 
758 /**
759  * xe_guc_ct_runtime_suspend() - GuC CT runtime suspend
760  * @ct: the &xe_guc_ct
761  *
762  * Set GuC CT to disabled state.
763  */
764 void xe_guc_ct_runtime_suspend(struct xe_guc_ct *ct)
765 {
766 	struct guc_ctb *g2h = &ct->ctbs.g2h;
767 	u32 credits = CIRC_SPACE(0, 0, CTB_G2H_BUFFER_DWORDS) - G2H_ROOM_BUFFER_DWORDS;
768 
769 	/* We should be back to guc_ct_ctb_g2h_init() values */
770 	xe_gt_assert(ct_to_gt(ct), g2h->info.space == credits);
771 
772 	/*
773 	 * Since we're already in runtime suspend path, we shouldn't have pending
774 	 * messages. But if there happen to be any, we'd probably want them to be
775 	 * thrown as errors for further investigation.
776 	 */
777 	xe_guc_ct_disable(ct);
778 }
779 
780 /**
781  * xe_guc_ct_runtime_resume() - GuC CT runtime resume
782  * @ct: the &xe_guc_ct
783  *
784  * Restart GuC CT and set it to enabled state.
785  */
786 void xe_guc_ct_runtime_resume(struct xe_guc_ct *ct)
787 {
788 	xe_guc_ct_restart(ct);
789 }
790 
791 static bool h2g_has_room(struct xe_guc_ct *ct, u32 cmd_len)
792 {
793 	struct guc_ctb *h2g = &ct->ctbs.h2g;
794 
795 	lockdep_assert_held(&ct->lock);
796 
797 	if (cmd_len > h2g->info.space) {
798 		h2g->info.head = desc_read(ct_to_xe(ct), h2g, head);
799 
800 		if (h2g->info.head > h2g->info.size) {
801 			struct xe_device *xe = ct_to_xe(ct);
802 			u32 desc_status = desc_read(xe, h2g, status);
803 
804 			desc_write(xe, h2g, status, desc_status | GUC_CTB_STATUS_OVERFLOW);
805 
806 			xe_gt_err(ct_to_gt(ct), "CT: invalid head offset %u >= %u)\n",
807 				  h2g->info.head, h2g->info.size);
808 			CT_DEAD(ct, h2g, H2G_HAS_ROOM);
809 			return false;
810 		}
811 
812 		h2g->info.space = CIRC_SPACE(h2g->info.tail, h2g->info.head,
813 					     h2g->info.size) -
814 				  h2g->info.resv_space;
815 		if (cmd_len > h2g->info.space)
816 			return false;
817 	}
818 
819 	return true;
820 }
821 
822 static bool g2h_has_room(struct xe_guc_ct *ct, u32 g2h_len)
823 {
824 	if (!g2h_len)
825 		return true;
826 
827 	lockdep_assert_held(&ct->fast_lock);
828 
829 	return ct->ctbs.g2h.info.space > g2h_len;
830 }
831 
832 static int has_room(struct xe_guc_ct *ct, u32 cmd_len, u32 g2h_len)
833 {
834 	lockdep_assert_held(&ct->lock);
835 
836 	if (!g2h_has_room(ct, g2h_len) || !h2g_has_room(ct, cmd_len))
837 		return -EBUSY;
838 
839 	return 0;
840 }
841 
842 static void h2g_reserve_space(struct xe_guc_ct *ct, u32 cmd_len)
843 {
844 	lockdep_assert_held(&ct->lock);
845 	ct->ctbs.h2g.info.space -= cmd_len;
846 }
847 
848 static void __g2h_reserve_space(struct xe_guc_ct *ct, u32 g2h_len, u32 num_g2h)
849 {
850 	xe_gt_assert(ct_to_gt(ct), g2h_len <= ct->ctbs.g2h.info.space);
851 	xe_gt_assert(ct_to_gt(ct), (!g2h_len && !num_g2h) ||
852 		     (g2h_len && num_g2h));
853 
854 	if (g2h_len) {
855 		lockdep_assert_held(&ct->fast_lock);
856 
857 		if (!ct->g2h_outstanding)
858 			xe_pm_runtime_get_noresume(ct_to_xe(ct));
859 
860 		ct->ctbs.g2h.info.space -= g2h_len;
861 		ct->g2h_outstanding += num_g2h;
862 	}
863 }
864 
865 static void __g2h_release_space(struct xe_guc_ct *ct, u32 g2h_len)
866 {
867 	bool bad = false;
868 
869 	lockdep_assert_held(&ct->fast_lock);
870 
871 	bad = ct->ctbs.g2h.info.space + g2h_len >
872 		     ct->ctbs.g2h.info.size - ct->ctbs.g2h.info.resv_space;
873 	bad |= !ct->g2h_outstanding;
874 
875 	if (bad) {
876 		xe_gt_err(ct_to_gt(ct), "Invalid G2H release: %d + %d vs %d - %d -> %d vs %d, outstanding = %d!\n",
877 			  ct->ctbs.g2h.info.space, g2h_len,
878 			  ct->ctbs.g2h.info.size, ct->ctbs.g2h.info.resv_space,
879 			  ct->ctbs.g2h.info.space + g2h_len,
880 			  ct->ctbs.g2h.info.size - ct->ctbs.g2h.info.resv_space,
881 			  ct->g2h_outstanding);
882 		CT_DEAD(ct, &ct->ctbs.g2h, G2H_RELEASE);
883 		return;
884 	}
885 
886 	ct->ctbs.g2h.info.space += g2h_len;
887 	if (!--ct->g2h_outstanding)
888 		xe_pm_runtime_put(ct_to_xe(ct));
889 }
890 
891 static void g2h_release_space(struct xe_guc_ct *ct, u32 g2h_len)
892 {
893 	spin_lock_irq(&ct->fast_lock);
894 	__g2h_release_space(ct, g2h_len);
895 	spin_unlock_irq(&ct->fast_lock);
896 }
897 
898 /*
899  * The CT protocol accepts a 16 bits fence. This field is fully owned by the
900  * driver, the GuC will just copy it to the reply message. Since we need to
901  * be able to distinguish between replies to REQUEST and FAST_REQUEST messages,
902  * we use one bit of the seqno as an indicator for that and a rolling counter
903  * for the remaining 15 bits.
904  */
905 #define CT_SEQNO_MASK GENMASK(14, 0)
906 #define CT_SEQNO_UNTRACKED BIT(15)
907 static u16 next_ct_seqno(struct xe_guc_ct *ct, bool is_g2h_fence)
908 {
909 	u32 seqno = ct->fence_seqno++ & CT_SEQNO_MASK;
910 
911 	if (!is_g2h_fence)
912 		seqno |= CT_SEQNO_UNTRACKED;
913 
914 	return seqno;
915 }
916 
917 #define MAKE_ACTION(type, __action)				\
918 ({								\
919 	FIELD_PREP(GUC_HXG_MSG_0_TYPE, type) |			\
920 	FIELD_PREP(GUC_HXG_EVENT_MSG_0_ACTION |			\
921 		   GUC_HXG_EVENT_MSG_0_DATA0, __action);	\
922 })
923 
924 static bool vf_action_can_safely_fail(struct xe_device *xe, u32 action)
925 {
926 	/*
927 	 * When resuming a VF, we can't reliably track whether context
928 	 * registration has completed in the GuC state machine. It is harmless
929 	 * to resend the request, as it will fail silently if GUC_HXG_TYPE_EVENT
930 	 * is used. Additionally, if there is an H2G protocol issue on a VF,
931 	 * subsequent H2G messages sent as GUC_HXG_TYPE_FAST_REQUEST will likely
932 	 * fail.
933 	 */
934 	return IS_SRIOV_VF(xe) && xe_sriov_vf_migration_supported(xe) &&
935 		(action == XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC ||
936 		 action == XE_GUC_ACTION_REGISTER_CONTEXT);
937 }
938 
939 #define H2G_CT_HEADERS (GUC_CTB_HDR_LEN + 1) /* one DW CTB header and one DW HxG header */
940 
941 static int h2g_write(struct xe_guc_ct *ct, const u32 *action, u32 len,
942 		     u32 ct_fence_value, bool want_response)
943 {
944 	struct xe_device *xe = ct_to_xe(ct);
945 	struct xe_gt *gt = ct_to_gt(ct);
946 	struct guc_ctb *h2g = &ct->ctbs.h2g;
947 	u32 cmd[H2G_CT_HEADERS];
948 	u32 tail = h2g->info.tail;
949 	u32 full_len;
950 	struct iosys_map map = IOSYS_MAP_INIT_OFFSET(&h2g->cmds,
951 							 tail * sizeof(u32));
952 
953 	full_len = len + GUC_CTB_HDR_LEN;
954 
955 	lockdep_assert_held(&ct->lock);
956 	xe_gt_assert(gt, full_len <= GUC_CTB_MSG_MAX_LEN);
957 
958 	if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) {
959 		u32 desc_tail = desc_read(xe, h2g, tail);
960 		u32 desc_head = desc_read(xe, h2g, head);
961 		u32 desc_status;
962 
963 		desc_status = desc_read(xe, h2g, status);
964 		if (desc_status) {
965 			xe_gt_err(gt, "CT write: non-zero status: %u\n", desc_status);
966 			goto corrupted;
967 		}
968 
969 		if (tail != desc_tail) {
970 			desc_write(xe, h2g, status, desc_status | GUC_CTB_STATUS_MISMATCH);
971 			xe_gt_err(gt, "CT write: tail was modified %u != %u\n", desc_tail, tail);
972 			goto corrupted;
973 		}
974 
975 		if (tail > h2g->info.size) {
976 			desc_write(xe, h2g, status, desc_status | GUC_CTB_STATUS_OVERFLOW);
977 			xe_gt_err(gt, "CT write: tail out of range: %u vs %u\n",
978 				  tail, h2g->info.size);
979 			goto corrupted;
980 		}
981 
982 		if (desc_head >= h2g->info.size) {
983 			desc_write(xe, h2g, status, desc_status | GUC_CTB_STATUS_OVERFLOW);
984 			xe_gt_err(gt, "CT write: invalid head offset %u >= %u)\n",
985 				  desc_head, h2g->info.size);
986 			goto corrupted;
987 		}
988 	}
989 
990 	/* Command will wrap, zero fill (NOPs), return and check credits again */
991 	if (tail + full_len > h2g->info.size) {
992 		xe_map_memset(xe, &map, 0, 0,
993 			      (h2g->info.size - tail) * sizeof(u32));
994 		h2g_reserve_space(ct, (h2g->info.size - tail));
995 		h2g->info.tail = 0;
996 		desc_write(xe, h2g, tail, h2g->info.tail);
997 
998 		return -EAGAIN;
999 	}
1000 
1001 	/*
1002 	 * dw0: CT header (including fence)
1003 	 * dw1: HXG header (including action code)
1004 	 * dw2+: action data
1005 	 */
1006 	cmd[0] = FIELD_PREP(GUC_CTB_MSG_0_FORMAT, GUC_CTB_FORMAT_HXG) |
1007 		FIELD_PREP(GUC_CTB_MSG_0_NUM_DWORDS, len) |
1008 		FIELD_PREP(GUC_CTB_MSG_0_FENCE, ct_fence_value);
1009 	if (want_response) {
1010 		cmd[1] = MAKE_ACTION(GUC_HXG_TYPE_REQUEST, action[0]);
1011 	} else if (vf_action_can_safely_fail(xe, action[0])) {
1012 		cmd[1] = MAKE_ACTION(GUC_HXG_TYPE_EVENT, action[0]);
1013 	} else {
1014 		fast_req_track(ct, ct_fence_value,
1015 			       FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, action[0]));
1016 
1017 		cmd[1] = MAKE_ACTION(GUC_HXG_TYPE_FAST_REQUEST, action[0]);
1018 	}
1019 
1020 	/* H2G header in cmd[1] replaces action[0] so: */
1021 	--len;
1022 	++action;
1023 
1024 	/* Write H2G ensuring visible before descriptor update */
1025 	xe_map_memcpy_to(xe, &map, 0, cmd, H2G_CT_HEADERS * sizeof(u32));
1026 	xe_map_memcpy_to(xe, &map, H2G_CT_HEADERS * sizeof(u32), action, len * sizeof(u32));
1027 	xe_device_wmb(xe);
1028 
1029 	/* Update local copies */
1030 	h2g->info.tail = (tail + full_len) % h2g->info.size;
1031 	h2g_reserve_space(ct, full_len);
1032 
1033 	/* Update descriptor */
1034 	desc_write(xe, h2g, tail, h2g->info.tail);
1035 
1036 	/*
1037 	 * desc_read() performs an VRAM read which serializes the CPU and drains
1038 	 * posted writes on dGPU platforms. Tracepoints evaluate arguments even
1039 	 * when disabled, so guard the event to avoid adding µs-scale latency to
1040 	 * the fast H2G submission path when tracing is not active.
1041 	 */
1042 	if (trace_xe_guc_ctb_h2g_enabled())
1043 		trace_xe_guc_ctb_h2g(xe, gt->info.id, *(action - 1), full_len,
1044 				     desc_read(xe, h2g, head), h2g->info.tail);
1045 
1046 	return 0;
1047 
1048 corrupted:
1049 	CT_DEAD(ct, &ct->ctbs.h2g, H2G_WRITE);
1050 	return -EPIPE;
1051 }
1052 
1053 static int __guc_ct_send_locked(struct xe_guc_ct *ct, const u32 *action,
1054 				u32 len, u32 g2h_len, u32 num_g2h,
1055 				struct g2h_fence *g2h_fence)
1056 {
1057 	struct xe_gt *gt = ct_to_gt(ct);
1058 	u16 seqno;
1059 	int ret;
1060 
1061 	xe_gt_assert(gt, xe_guc_ct_initialized(ct));
1062 	xe_gt_assert(gt, !g2h_len || !g2h_fence);
1063 	xe_gt_assert(gt, !num_g2h || !g2h_fence);
1064 	xe_gt_assert(gt, !g2h_len || num_g2h);
1065 	xe_gt_assert(gt, g2h_len || !num_g2h);
1066 	lockdep_assert_held(&ct->lock);
1067 
1068 	if (unlikely(ct->ctbs.h2g.info.broken)) {
1069 		ret = -EPIPE;
1070 		goto out;
1071 	}
1072 
1073 	if (ct->state == XE_GUC_CT_STATE_DISABLED) {
1074 		ret = -ENODEV;
1075 		goto out;
1076 	}
1077 
1078 	if (ct->state == XE_GUC_CT_STATE_STOPPED || xe_gt_recovery_pending(gt)) {
1079 		ret = -ECANCELED;
1080 		goto out;
1081 	}
1082 
1083 	xe_gt_assert(gt, xe_guc_ct_enabled(ct));
1084 
1085 	if (g2h_fence) {
1086 		g2h_len = GUC_CTB_HXG_MSG_MAX_LEN;
1087 		num_g2h = 1;
1088 
1089 		if (g2h_fence_needs_alloc(g2h_fence)) {
1090 			g2h_fence->seqno = next_ct_seqno(ct, true);
1091 			ret = xa_err(xa_store(&ct->fence_lookup,
1092 					      g2h_fence->seqno, g2h_fence,
1093 					      GFP_ATOMIC));
1094 			if (ret)
1095 				goto out;
1096 		}
1097 
1098 		seqno = g2h_fence->seqno;
1099 	} else {
1100 		seqno = next_ct_seqno(ct, false);
1101 	}
1102 
1103 	if (g2h_len)
1104 		spin_lock_irq(&ct->fast_lock);
1105 retry:
1106 	ret = has_room(ct, len + GUC_CTB_HDR_LEN, g2h_len);
1107 	if (unlikely(ret))
1108 		goto out_unlock;
1109 
1110 	ret = h2g_write(ct, action, len, seqno, !!g2h_fence);
1111 	if (unlikely(ret)) {
1112 		if (ret == -EAGAIN)
1113 			goto retry;
1114 		goto out_unlock;
1115 	}
1116 
1117 	__g2h_reserve_space(ct, g2h_len, num_g2h);
1118 	xe_guc_notify(ct_to_guc(ct));
1119 out_unlock:
1120 	if (g2h_len)
1121 		spin_unlock_irq(&ct->fast_lock);
1122 out:
1123 	return ret;
1124 }
1125 
1126 static void kick_reset(struct xe_guc_ct *ct)
1127 {
1128 	xe_gt_reset_async(ct_to_gt(ct));
1129 }
1130 
1131 static int dequeue_one_g2h(struct xe_guc_ct *ct);
1132 
1133 /*
1134  * wait before retry of sending h2g message
1135  * Return: true if ready for retry, false if the wait timeouted
1136  */
1137 static bool guc_ct_send_wait_for_retry(struct xe_guc_ct *ct, u32 len,
1138 				       u32 g2h_len, struct g2h_fence *g2h_fence,
1139 				       unsigned int *sleep_period_ms,
1140 				       unsigned int *sleep_total_ms)
1141 {
1142 	struct xe_device *xe = ct_to_xe(ct);
1143 
1144 	/*
1145 	 * We wait to try to restore credits for about 1 second before bailing.
1146 	 * In the case of H2G credits we have no choice but just to wait for the
1147 	 * GuC to consume H2Gs in the channel so we use a wait / sleep loop. In
1148 	 * the case of G2H we process any G2H in the channel, hopefully freeing
1149 	 * credits as we consume the G2H messages.
1150 	 */
1151 	if (!h2g_has_room(ct, len + GUC_CTB_HDR_LEN)) {
1152 		struct guc_ctb *h2g = &ct->ctbs.h2g;
1153 
1154 		if (*sleep_total_ms > 1000)
1155 			return false;
1156 
1157 		trace_xe_guc_ct_h2g_flow_control(xe, h2g->info.head, h2g->info.tail,
1158 						 h2g->info.size,
1159 						 h2g->info.space,
1160 						 len + GUC_CTB_HDR_LEN);
1161 		*sleep_total_ms += xe_sleep_exponential_ms(sleep_period_ms, 64);
1162 	} else {
1163 		struct guc_ctb *g2h = &ct->ctbs.g2h;
1164 		int ret;
1165 
1166 		trace_xe_guc_ct_g2h_flow_control(xe, g2h->info.head,
1167 						 desc_read(xe, g2h, tail),
1168 						 g2h->info.size,
1169 						 g2h->info.space,
1170 						 g2h_fence ?
1171 						 GUC_CTB_HXG_MSG_MAX_LEN :
1172 						 g2h_len);
1173 
1174 #define g2h_avail(ct)	\
1175 	(desc_read(ct_to_xe(ct), (&ct->ctbs.g2h), tail) != ct->ctbs.g2h.info.head)
1176 		if (!wait_event_timeout(ct->wq, !ct->g2h_outstanding ||
1177 					g2h_avail(ct), HZ))
1178 			return false;
1179 #undef g2h_avail
1180 
1181 		ret = dequeue_one_g2h(ct);
1182 		if (ret < 0) {
1183 			if (ret != -ECANCELED)
1184 				xe_gt_err(ct_to_gt(ct), "CTB receive failed (%pe)\n",
1185 					  ERR_PTR(ret));
1186 			return false;
1187 		}
1188 	}
1189 	return true;
1190 }
1191 
1192 static int guc_ct_send_locked(struct xe_guc_ct *ct, const u32 *action, u32 len,
1193 			      u32 g2h_len, u32 num_g2h,
1194 			      struct g2h_fence *g2h_fence)
1195 {
1196 	struct xe_gt *gt = ct_to_gt(ct);
1197 	unsigned int sleep_period_ms = 1;
1198 	unsigned int sleep_total_ms = 0;
1199 	int ret;
1200 
1201 	xe_gt_assert(gt, !g2h_len || !g2h_fence);
1202 	lockdep_assert_held(&ct->lock);
1203 	xe_device_assert_mem_access(ct_to_xe(ct));
1204 
1205 try_again:
1206 	ret = __guc_ct_send_locked(ct, action, len, g2h_len, num_g2h,
1207 				   g2h_fence);
1208 
1209 	if (unlikely(ret == -EBUSY)) {
1210 		if (!guc_ct_send_wait_for_retry(ct, len, g2h_len, g2h_fence,
1211 						&sleep_period_ms, &sleep_total_ms))
1212 			goto broken;
1213 		goto try_again;
1214 	}
1215 
1216 	return ret;
1217 
1218 broken:
1219 	xe_gt_err(gt, "No forward process on H2G, reset required\n");
1220 	CT_DEAD(ct, &ct->ctbs.h2g, DEADLOCK);
1221 
1222 	return -EDEADLK;
1223 }
1224 
1225 static int guc_ct_send(struct xe_guc_ct *ct, const u32 *action, u32 len,
1226 		       u32 g2h_len, u32 num_g2h, struct g2h_fence *g2h_fence)
1227 {
1228 	int ret;
1229 
1230 	xe_gt_assert(ct_to_gt(ct), !g2h_len || !g2h_fence);
1231 
1232 	mutex_lock(&ct->lock);
1233 	ret = guc_ct_send_locked(ct, action, len, g2h_len, num_g2h, g2h_fence);
1234 	mutex_unlock(&ct->lock);
1235 
1236 	return ret;
1237 }
1238 
1239 int xe_guc_ct_send(struct xe_guc_ct *ct, const u32 *action, u32 len,
1240 		   u32 g2h_len, u32 num_g2h)
1241 {
1242 	int ret;
1243 
1244 	ret = guc_ct_send(ct, action, len, g2h_len, num_g2h, NULL);
1245 	if (ret == -EDEADLK)
1246 		kick_reset(ct);
1247 
1248 	return ret;
1249 }
1250 
1251 int xe_guc_ct_send_locked(struct xe_guc_ct *ct, const u32 *action, u32 len,
1252 			  u32 g2h_len, u32 num_g2h)
1253 {
1254 	int ret;
1255 
1256 	ret = guc_ct_send_locked(ct, action, len, g2h_len, num_g2h, NULL);
1257 	if (ret == -EDEADLK)
1258 		kick_reset(ct);
1259 
1260 	return ret;
1261 }
1262 
1263 int xe_guc_ct_send_g2h_handler(struct xe_guc_ct *ct, const u32 *action, u32 len)
1264 {
1265 	int ret;
1266 
1267 	lockdep_assert_held(&ct->lock);
1268 
1269 	ret = guc_ct_send_locked(ct, action, len, 0, 0, NULL);
1270 	if (ret == -EDEADLK)
1271 		kick_reset(ct);
1272 
1273 	return ret;
1274 }
1275 
1276 /*
1277  * Check if a GT reset is in progress or will occur and if GT reset brought the
1278  * CT back up. Randomly picking 5 seconds for an upper limit to do a GT a reset.
1279  */
1280 static bool retry_failure(struct xe_guc_ct *ct, int ret)
1281 {
1282 	if (!(ret == -EDEADLK || ret == -EPIPE || ret == -ENODEV))
1283 		return false;
1284 
1285 #define ct_alive(ct)	\
1286 	(xe_guc_ct_enabled(ct) && !ct->ctbs.h2g.info.broken && \
1287 	 !ct->ctbs.g2h.info.broken)
1288 	if (!wait_event_interruptible_timeout(ct->wq, ct_alive(ct), HZ * 5))
1289 		return false;
1290 #undef ct_alive
1291 
1292 	return true;
1293 }
1294 
1295 #define GUC_SEND_RETRY_LIMIT	50
1296 #define GUC_SEND_RETRY_MSLEEP	5
1297 
1298 static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
1299 			    u32 *response_buffer, bool no_fail)
1300 {
1301 	struct xe_gt *gt = ct_to_gt(ct);
1302 	struct g2h_fence g2h_fence;
1303 	unsigned int retries = 0;
1304 	int ret = 0;
1305 
1306 	/*
1307 	 * We use a fence to implement blocking sends / receiving response data.
1308 	 * The seqno of the fence is sent in the H2G, returned in the G2H, and
1309 	 * an xarray is used as storage media with the seqno being to key.
1310 	 * Fields in the fence hold success, failure, retry status and the
1311 	 * response data. Safe to allocate on the stack as the xarray is the
1312 	 * only reference and it cannot be present after this function exits.
1313 	 */
1314 retry:
1315 	g2h_fence_init(&g2h_fence, response_buffer);
1316 retry_same_fence:
1317 	ret = guc_ct_send(ct, action, len, 0, 0, &g2h_fence);
1318 	if (unlikely(ret == -ENOMEM)) {
1319 		/* Retry allocation /w GFP_KERNEL */
1320 		ret = xa_err(xa_store(&ct->fence_lookup, g2h_fence.seqno,
1321 				      &g2h_fence, GFP_KERNEL));
1322 		if (ret)
1323 			return ret;
1324 
1325 		goto retry_same_fence;
1326 	} else if (unlikely(ret)) {
1327 		if (ret == -EDEADLK)
1328 			kick_reset(ct);
1329 
1330 		if (no_fail && retry_failure(ct, ret))
1331 			goto retry_same_fence;
1332 
1333 		if (!g2h_fence_needs_alloc(&g2h_fence))
1334 			xa_erase(&ct->fence_lookup, g2h_fence.seqno);
1335 
1336 		return ret;
1337 	}
1338 
1339 	/* READ_ONCEs pairs with WRITE_ONCEs in parse_g2h_response
1340 	 * and g2h_fence_cancel.
1341 	 */
1342 wait_again:
1343 	ret = wait_event_timeout(ct->g2h_fence_wq, READ_ONCE(g2h_fence.done), HZ);
1344 	if (!ret) {
1345 		LNL_FLUSH_WORK(&ct->g2h_worker);
1346 		if (READ_ONCE(g2h_fence.done)) {
1347 			xe_gt_warn(gt, "G2H fence %u, action %04x, done\n",
1348 				   g2h_fence.seqno, action[0]);
1349 			ret = 1;
1350 		}
1351 	}
1352 
1353 	/*
1354 	 * Ensure we serialize with completion side to prevent UAF with fence going out of scope on
1355 	 * the stack, since we have no clue if it will fire after the timeout before we can erase
1356 	 * from the xa. Also we have some dependent loads and stores below for which we need the
1357 	 * correct ordering, and we lack the needed barriers.
1358 	 */
1359 	mutex_lock(&ct->lock);
1360 	if (!ret) {
1361 		xe_gt_err(gt, "Timed out wait for G2H, fence %u, action %04x, done %s\n",
1362 			  g2h_fence.seqno, action[0], str_yes_no(g2h_fence.done));
1363 		xa_erase(&ct->fence_lookup, g2h_fence.seqno);
1364 		mutex_unlock(&ct->lock);
1365 		return -ETIME;
1366 	}
1367 
1368 	if (g2h_fence.wait) {
1369 		xe_gt_dbg(gt, "H2G action %#x busy: counter %u\n",
1370 			  action[0], g2h_fence.counter);
1371 		/* we can't leave any response data if we want to wait again */
1372 		g2h_fence_reinit(&g2h_fence);
1373 		mutex_unlock(&ct->lock);
1374 		goto wait_again;
1375 	}
1376 	if (g2h_fence.retry) {
1377 		xe_gt_dbg(gt, "H2G action %#x retrying: reason %#x\n",
1378 			  action[0], g2h_fence.reason);
1379 		mutex_unlock(&ct->lock);
1380 		if (++retries > GUC_SEND_RETRY_LIMIT) {
1381 			xe_gt_err(gt, "H2G action %#x reached retry limit=%u, aborting\n",
1382 				  action[0], GUC_SEND_RETRY_LIMIT);
1383 			return -ELOOP;
1384 		}
1385 		msleep(GUC_SEND_RETRY_MSLEEP * retries);
1386 		goto retry;
1387 	}
1388 	if (g2h_fence.fail) {
1389 		if (g2h_fence.cancel) {
1390 			xe_gt_dbg(gt, "H2G request %#x canceled!\n", action[0]);
1391 			ret = -ECANCELED;
1392 			goto unlock;
1393 		}
1394 		xe_gt_err(gt, "H2G request %#x failed: error %#x hint %#x\n",
1395 			  action[0], g2h_fence.error, g2h_fence.hint);
1396 		ret = -EIO;
1397 	}
1398 
1399 	if (ret > 0)
1400 		ret = response_buffer ? g2h_fence.response_len : g2h_fence.response_data;
1401 
1402 unlock:
1403 	mutex_unlock(&ct->lock);
1404 
1405 	return ret;
1406 }
1407 
1408 /**
1409  * xe_guc_ct_send_recv - Send and receive HXG to the GuC
1410  * @ct: the &xe_guc_ct
1411  * @action: the dword array with `HXG Request`_ message (can't be NULL)
1412  * @len: length of the `HXG Request`_ message (in dwords, can't be 0)
1413  * @response_buffer: placeholder for the `HXG Response`_ message (can be NULL)
1414  *
1415  * Send a `HXG Request`_ message to the GuC over CT communication channel and
1416  * blocks until GuC replies with a `HXG Response`_ message.
1417  *
1418  * For non-blocking communication with GuC use xe_guc_ct_send().
1419  *
1420  * Note: The size of &response_buffer must be at least GUC_CTB_MAX_DWORDS_.
1421  *
1422  * Return: response length (in dwords) if &response_buffer was not NULL, or
1423  *         DATA0 from `HXG Response`_ if &response_buffer was NULL, or
1424  *         a negative error code on failure.
1425  */
1426 int xe_guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
1427 			u32 *response_buffer)
1428 {
1429 	KUNIT_STATIC_STUB_REDIRECT(xe_guc_ct_send_recv, ct, action, len, response_buffer);
1430 	return guc_ct_send_recv(ct, action, len, response_buffer, false);
1431 }
1432 ALLOW_ERROR_INJECTION(xe_guc_ct_send_recv, ERRNO);
1433 
1434 int xe_guc_ct_send_recv_no_fail(struct xe_guc_ct *ct, const u32 *action,
1435 				u32 len, u32 *response_buffer)
1436 {
1437 	return guc_ct_send_recv(ct, action, len, response_buffer, true);
1438 }
1439 
1440 static u32 *msg_to_hxg(u32 *msg)
1441 {
1442 	return msg + GUC_CTB_MSG_MIN_LEN;
1443 }
1444 
1445 static u32 msg_len_to_hxg_len(u32 len)
1446 {
1447 	return len - GUC_CTB_MSG_MIN_LEN;
1448 }
1449 
1450 static int parse_g2h_event(struct xe_guc_ct *ct, u32 *msg, u32 len)
1451 {
1452 	u32 *hxg = msg_to_hxg(msg);
1453 	u32 action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]);
1454 
1455 	lockdep_assert_held(&ct->lock);
1456 
1457 	switch (action) {
1458 	case XE_GUC_ACTION_NOTIFY_MULTI_QUEUE_CONTEXT_CGP_SYNC_DONE:
1459 	case XE_GUC_ACTION_SCHED_CONTEXT_MODE_DONE:
1460 	case XE_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
1461 	case XE_GUC_ACTION_SCHED_ENGINE_MODE_DONE:
1462 	case XE_GUC_ACTION_TLB_INVALIDATION_DONE:
1463 	case XE_GUC_ACTION_PAGE_RECLAMATION_DONE:
1464 		g2h_release_space(ct, len);
1465 	}
1466 
1467 	return 0;
1468 }
1469 
1470 static int guc_crash_process_msg(struct xe_guc_ct *ct, u32 action)
1471 {
1472 	struct xe_gt *gt = ct_to_gt(ct);
1473 
1474 	if (action == XE_GUC_ACTION_NOTIFY_CRASH_DUMP_POSTED)
1475 		xe_gt_err(gt, "GuC Crash dump notification\n");
1476 	else if (action == XE_GUC_ACTION_NOTIFY_EXCEPTION)
1477 		xe_gt_err(gt, "GuC Exception notification\n");
1478 	else
1479 		xe_gt_err(gt, "Unknown GuC crash notification: 0x%04X\n", action);
1480 
1481 	CT_DEAD(ct, NULL, CRASH);
1482 
1483 	kick_reset(ct);
1484 
1485 	return 0;
1486 }
1487 
1488 static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len)
1489 {
1490 	struct xe_gt *gt =  ct_to_gt(ct);
1491 	u32 *hxg = msg_to_hxg(msg);
1492 	u32 hxg_len = msg_len_to_hxg_len(len);
1493 	u32 fence = FIELD_GET(GUC_CTB_MSG_0_FENCE, msg[0]);
1494 	u32 type = FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]);
1495 	struct g2h_fence *g2h_fence;
1496 
1497 	lockdep_assert_held(&ct->lock);
1498 
1499 	/*
1500 	 * Fences for FAST_REQUEST messages are not tracked in ct->fence_lookup.
1501 	 * Those messages should never fail, so if we do get an error back it
1502 	 * means we're likely doing an illegal operation and the GuC is
1503 	 * rejecting it. We have no way to inform the code that submitted the
1504 	 * H2G that the message was rejected, so we need to escalate the
1505 	 * failure to trigger a reset.
1506 	 */
1507 	if (fence & CT_SEQNO_UNTRACKED) {
1508 		if (type == GUC_HXG_TYPE_RESPONSE_FAILURE)
1509 			xe_gt_err(gt, "FAST_REQ H2G fence 0x%x failed! e=0x%x, h=%u\n",
1510 				  fence,
1511 				  FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, hxg[0]),
1512 				  FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, hxg[0]));
1513 		else
1514 			xe_gt_err(gt, "unexpected response %u for FAST_REQ H2G fence 0x%x!\n",
1515 				  type, fence);
1516 
1517 		fast_req_report(ct, fence);
1518 
1519 		/* FIXME: W/A race in the GuC, will get in firmware soon */
1520 		if (xe_gt_recovery_pending(gt))
1521 			return 0;
1522 
1523 		CT_DEAD(ct, NULL, PARSE_G2H_RESPONSE);
1524 
1525 		return -EPROTO;
1526 	}
1527 
1528 	/* don't erase as we still expect a final response with the same fence */
1529 	if (type == GUC_HXG_TYPE_NO_RESPONSE_BUSY)
1530 		g2h_fence = xa_load(&ct->fence_lookup, fence);
1531 	else
1532 		g2h_fence = xa_erase(&ct->fence_lookup, fence);
1533 
1534 	if (unlikely(!g2h_fence)) {
1535 		/* Don't tear down channel, as send could've timed out */
1536 		/* CT_DEAD(ct, NULL, PARSE_G2H_UNKNOWN); */
1537 		xe_gt_warn(gt, "G2H fence (%u) not found!\n", fence);
1538 		g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN);
1539 		return 0;
1540 	}
1541 
1542 	xe_gt_assert(gt, fence == g2h_fence->seqno);
1543 
1544 	/*
1545 	 * reinit as we might have already process this g2h_fence before
1546 	 * if we received a NO_RESPONSE_BUSY reply
1547 	 */
1548 	g2h_fence_reinit(g2h_fence);
1549 
1550 	if (type == GUC_HXG_TYPE_RESPONSE_FAILURE) {
1551 		g2h_fence->fail = true;
1552 		g2h_fence->error = FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, hxg[0]);
1553 		g2h_fence->hint = FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, hxg[0]);
1554 	} else if (type == GUC_HXG_TYPE_NO_RESPONSE_RETRY) {
1555 		g2h_fence->retry = true;
1556 		g2h_fence->reason = FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, hxg[0]);
1557 	} else if (type == GUC_HXG_TYPE_NO_RESPONSE_BUSY) {
1558 		g2h_fence->wait = true;
1559 		g2h_fence->counter = FIELD_GET(GUC_HXG_BUSY_MSG_0_COUNTER, hxg[0]);
1560 	} else if (g2h_fence->response_buffer) {
1561 		g2h_fence->response_len = hxg_len;
1562 		memcpy(g2h_fence->response_buffer, hxg, hxg_len * sizeof(u32));
1563 	} else {
1564 		g2h_fence->response_data = FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, hxg[0]);
1565 	}
1566 
1567 	/* don't release any space if it was an intermediate message */
1568 	if (!g2h_fence->wait)
1569 		g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN);
1570 
1571 	/* WRITE_ONCE pairs with READ_ONCEs in guc_ct_send_recv. */
1572 	WRITE_ONCE(g2h_fence->done, true);
1573 	smp_mb();
1574 
1575 	wake_up_all(&ct->g2h_fence_wq);
1576 
1577 	return 0;
1578 }
1579 
1580 static int parse_g2h_msg(struct xe_guc_ct *ct, u32 *msg, u32 len)
1581 {
1582 	struct xe_gt *gt = ct_to_gt(ct);
1583 	u32 *hxg = msg_to_hxg(msg);
1584 	u32 origin, type;
1585 	int ret;
1586 
1587 	lockdep_assert_held(&ct->lock);
1588 
1589 	origin = FIELD_GET(GUC_HXG_MSG_0_ORIGIN, hxg[0]);
1590 	if (unlikely(origin != GUC_HXG_ORIGIN_GUC)) {
1591 		xe_gt_err(gt, "G2H channel broken on read, origin=%u, reset required\n",
1592 			  origin);
1593 		CT_DEAD(ct, &ct->ctbs.g2h, PARSE_G2H_ORIGIN);
1594 
1595 		return -EPROTO;
1596 	}
1597 
1598 	type = FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]);
1599 	switch (type) {
1600 	case GUC_HXG_TYPE_EVENT:
1601 		ret = parse_g2h_event(ct, msg, len);
1602 		break;
1603 	case GUC_HXG_TYPE_RESPONSE_SUCCESS:
1604 	case GUC_HXG_TYPE_RESPONSE_FAILURE:
1605 	case GUC_HXG_TYPE_NO_RESPONSE_RETRY:
1606 	case GUC_HXG_TYPE_NO_RESPONSE_BUSY:
1607 		ret = parse_g2h_response(ct, msg, len);
1608 		break;
1609 	default:
1610 		xe_gt_err(gt, "G2H channel broken on read, type=%u, reset required\n",
1611 			  type);
1612 		CT_DEAD(ct, &ct->ctbs.g2h, PARSE_G2H_TYPE);
1613 
1614 		ret = -EOPNOTSUPP;
1615 	}
1616 
1617 	return ret;
1618 }
1619 
1620 static int process_g2h_msg(struct xe_guc_ct *ct, u32 *msg, u32 len)
1621 {
1622 	struct xe_guc *guc = ct_to_guc(ct);
1623 	struct xe_gt *gt = ct_to_gt(ct);
1624 	u32 hxg_len = msg_len_to_hxg_len(len);
1625 	u32 *hxg = msg_to_hxg(msg);
1626 	u32 action, adj_len;
1627 	u32 *payload;
1628 	int ret = 0;
1629 
1630 	if (FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]) != GUC_HXG_TYPE_EVENT)
1631 		return 0;
1632 
1633 	action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]);
1634 	payload = hxg + GUC_HXG_EVENT_MSG_MIN_LEN;
1635 	adj_len = hxg_len - GUC_HXG_EVENT_MSG_MIN_LEN;
1636 
1637 	switch (action) {
1638 	case XE_GUC_ACTION_SCHED_CONTEXT_MODE_DONE:
1639 		ret = xe_guc_sched_done_handler(guc, payload, adj_len);
1640 		break;
1641 	case XE_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
1642 		ret = xe_guc_deregister_done_handler(guc, payload, adj_len);
1643 		break;
1644 	case XE_GUC_ACTION_CONTEXT_RESET_NOTIFICATION:
1645 		ret = xe_guc_exec_queue_reset_handler(guc, payload, adj_len);
1646 		break;
1647 	case XE_GUC_ACTION_ENGINE_FAILURE_NOTIFICATION:
1648 		ret = xe_guc_exec_queue_reset_failure_handler(guc, payload,
1649 							      adj_len);
1650 		break;
1651 	case XE_GUC_ACTION_SCHED_ENGINE_MODE_DONE:
1652 		/* Selftest only at the moment */
1653 		break;
1654 	case XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION:
1655 		ret = xe_guc_error_capture_handler(guc, payload, adj_len);
1656 		break;
1657 	case XE_GUC_ACTION_NOTIFY_FLUSH_LOG_BUFFER_TO_FILE:
1658 		/* FIXME: Handle this */
1659 		break;
1660 	case XE_GUC_ACTION_NOTIFY_MEMORY_CAT_ERROR:
1661 		ret = xe_guc_exec_queue_memory_cat_error_handler(guc, payload,
1662 								 adj_len);
1663 		break;
1664 	case XE_GUC_ACTION_REPORT_PAGE_FAULT_REQ_DESC:
1665 		ret = xe_guc_pagefault_handler(guc, payload, adj_len);
1666 		break;
1667 	case XE_GUC_ACTION_TLB_INVALIDATION_DONE:
1668 		ret = xe_guc_tlb_inval_done_handler(guc, payload, adj_len);
1669 		break;
1670 	case XE_GUC_ACTION_PAGE_RECLAMATION_DONE:
1671 		ret = xe_guc_page_reclaim_done_handler(guc, payload, adj_len);
1672 		break;
1673 	case XE_GUC_ACTION_GUC2PF_RELAY_FROM_VF:
1674 		ret = xe_guc_relay_process_guc2pf(&guc->relay, hxg, hxg_len);
1675 		break;
1676 	case XE_GUC_ACTION_GUC2VF_RELAY_FROM_PF:
1677 		ret = xe_guc_relay_process_guc2vf(&guc->relay, hxg, hxg_len);
1678 		break;
1679 	case GUC_ACTION_GUC2PF_VF_STATE_NOTIFY:
1680 		ret = xe_gt_sriov_pf_control_process_guc2pf(gt, hxg, hxg_len);
1681 		break;
1682 	case GUC_ACTION_GUC2PF_ADVERSE_EVENT:
1683 		ret = xe_gt_sriov_pf_monitor_process_guc2pf(gt, hxg, hxg_len);
1684 		break;
1685 	case XE_GUC_ACTION_NOTIFY_CRASH_DUMP_POSTED:
1686 	case XE_GUC_ACTION_NOTIFY_EXCEPTION:
1687 		ret = guc_crash_process_msg(ct, action);
1688 		break;
1689 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
1690 	case XE_GUC_ACTION_TEST_G2G_RECV:
1691 		ret = xe_guc_g2g_test_notification(guc, payload, adj_len);
1692 		break;
1693 #endif
1694 	case XE_GUC_ACTION_NOTIFY_MULTI_QUEUE_CONTEXT_CGP_SYNC_DONE:
1695 		ret = xe_guc_exec_queue_cgp_sync_done_handler(guc, payload, adj_len);
1696 		break;
1697 	case XE_GUC_ACTION_NOTIFY_MULTI_QUEUE_CGP_CONTEXT_ERROR:
1698 		ret = xe_guc_exec_queue_cgp_context_error_handler(guc, payload,
1699 								  adj_len);
1700 		break;
1701 	default:
1702 		xe_gt_err(gt, "unexpected G2H action 0x%04x\n", action);
1703 	}
1704 
1705 	if (ret) {
1706 		xe_gt_err(gt, "G2H action %#04x failed (%pe) len %u msg %*ph\n",
1707 			  action, ERR_PTR(ret), hxg_len, (int)sizeof(u32) * hxg_len, hxg);
1708 		CT_DEAD(ct, NULL, PROCESS_FAILED);
1709 	}
1710 
1711 	return 0;
1712 }
1713 
1714 static int g2h_read(struct xe_guc_ct *ct, u32 *msg, bool fast_path)
1715 {
1716 	struct xe_device *xe = ct_to_xe(ct);
1717 	struct xe_gt *gt = ct_to_gt(ct);
1718 	struct guc_ctb *g2h = &ct->ctbs.g2h;
1719 	u32 tail, head, len, desc_status;
1720 	s32 avail;
1721 	u32 action;
1722 	u32 *hxg;
1723 
1724 	xe_gt_assert(gt, xe_guc_ct_initialized(ct));
1725 	lockdep_assert_held(&ct->fast_lock);
1726 
1727 	if (ct->state == XE_GUC_CT_STATE_DISABLED)
1728 		return -ENODEV;
1729 
1730 	if (ct->state == XE_GUC_CT_STATE_STOPPED)
1731 		return -ECANCELED;
1732 
1733 	if (g2h->info.broken)
1734 		return -EPIPE;
1735 
1736 	xe_gt_assert(gt, xe_guc_ct_enabled(ct));
1737 
1738 	desc_status = desc_read(xe, g2h, status);
1739 	if (desc_status) {
1740 		if (desc_status & GUC_CTB_STATUS_DISABLED) {
1741 			/*
1742 			 * Potentially valid if a CLIENT_RESET request resulted in
1743 			 * contexts/engines being reset. But should never happen as
1744 			 * no contexts should be active when CLIENT_RESET is sent.
1745 			 */
1746 			xe_gt_err(gt, "CT read: unexpected G2H after GuC has stopped!\n");
1747 			desc_status &= ~GUC_CTB_STATUS_DISABLED;
1748 		}
1749 
1750 		if (desc_status) {
1751 			xe_gt_err(gt, "CT read: non-zero status: %u\n", desc_status);
1752 			goto corrupted;
1753 		}
1754 	}
1755 
1756 	if (IS_ENABLED(CONFIG_DRM_XE_DEBUG)) {
1757 		u32 desc_tail = desc_read(xe, g2h, tail);
1758 		/*
1759 		u32 desc_head = desc_read(xe, g2h, head);
1760 
1761 		 * info.head and desc_head are updated back-to-back at the end of
1762 		 * this function and nowhere else. Hence, they cannot be different
1763 		 * unless two g2h_read calls are running concurrently. Which is not
1764 		 * possible because it is guarded by ct->fast_lock. And yet, some
1765 		 * discrete platforms are regularly hitting this error :(.
1766 		 *
1767 		 * desc_head rolling backwards shouldn't cause any noticeable
1768 		 * problems - just a delay in GuC being allowed to proceed past that
1769 		 * point in the queue. So for now, just disable the error until it
1770 		 * can be root caused.
1771 		 *
1772 		if (g2h->info.head != desc_head) {
1773 			desc_write(xe, g2h, status, desc_status | GUC_CTB_STATUS_MISMATCH);
1774 			xe_gt_err(gt, "CT read: head was modified %u != %u\n",
1775 				  desc_head, g2h->info.head);
1776 			goto corrupted;
1777 		}
1778 		 */
1779 
1780 		if (g2h->info.head > g2h->info.size) {
1781 			desc_write(xe, g2h, status, desc_status | GUC_CTB_STATUS_OVERFLOW);
1782 			xe_gt_err(gt, "CT read: head out of range: %u vs %u\n",
1783 				  g2h->info.head, g2h->info.size);
1784 			goto corrupted;
1785 		}
1786 
1787 		if (desc_tail >= g2h->info.size) {
1788 			desc_write(xe, g2h, status, desc_status | GUC_CTB_STATUS_OVERFLOW);
1789 			xe_gt_err(gt, "CT read: invalid tail offset %u >= %u)\n",
1790 				  desc_tail, g2h->info.size);
1791 			goto corrupted;
1792 		}
1793 	}
1794 
1795 	/* Calculate DW available to read */
1796 	tail = desc_read(xe, g2h, tail);
1797 	avail = tail - g2h->info.head;
1798 	if (unlikely(avail == 0))
1799 		return 0;
1800 
1801 	if (avail < 0)
1802 		avail += g2h->info.size;
1803 
1804 	/* Read header */
1805 	xe_map_memcpy_from(xe, msg, &g2h->cmds, sizeof(u32) * g2h->info.head,
1806 			   sizeof(u32));
1807 	len = FIELD_GET(GUC_CTB_MSG_0_NUM_DWORDS, msg[0]) + GUC_CTB_MSG_MIN_LEN;
1808 	if (len > avail) {
1809 		xe_gt_err(gt, "G2H channel broken on read, avail=%d, len=%d, reset required\n",
1810 			  avail, len);
1811 		goto corrupted;
1812 	}
1813 
1814 	head = (g2h->info.head + 1) % g2h->info.size;
1815 	avail = len - 1;
1816 
1817 	/* Read G2H message */
1818 	if (avail + head > g2h->info.size) {
1819 		u32 avail_til_wrap = g2h->info.size - head;
1820 
1821 		xe_map_memcpy_from(xe, msg + 1,
1822 				   &g2h->cmds, sizeof(u32) * head,
1823 				   avail_til_wrap * sizeof(u32));
1824 		xe_map_memcpy_from(xe, msg + 1 + avail_til_wrap,
1825 				   &g2h->cmds, 0,
1826 				   (avail - avail_til_wrap) * sizeof(u32));
1827 	} else {
1828 		xe_map_memcpy_from(xe, msg + 1,
1829 				   &g2h->cmds, sizeof(u32) * head,
1830 				   avail * sizeof(u32));
1831 	}
1832 
1833 	hxg = msg_to_hxg(msg);
1834 	action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]);
1835 
1836 	if (fast_path) {
1837 		if (FIELD_GET(GUC_HXG_MSG_0_TYPE, hxg[0]) != GUC_HXG_TYPE_EVENT)
1838 			return 0;
1839 
1840 		switch (action) {
1841 		case XE_GUC_ACTION_REPORT_PAGE_FAULT_REQ_DESC:
1842 		case XE_GUC_ACTION_TLB_INVALIDATION_DONE:
1843 		case XE_GUC_ACTION_PAGE_RECLAMATION_DONE:
1844 			break;	/* Process these in fast-path */
1845 		default:
1846 			return 0;
1847 		}
1848 	}
1849 
1850 	/* Update local / descriptor header */
1851 	g2h->info.head = (head + avail) % g2h->info.size;
1852 	desc_write(xe, g2h, head, g2h->info.head);
1853 
1854 	trace_xe_guc_ctb_g2h(xe, ct_to_gt(ct)->info.id,
1855 			     action, len, g2h->info.head, tail);
1856 
1857 	return len;
1858 
1859 corrupted:
1860 	CT_DEAD(ct, &ct->ctbs.g2h, G2H_READ);
1861 	return -EPROTO;
1862 }
1863 
1864 static void g2h_fast_path(struct xe_guc_ct *ct, u32 *msg, u32 len)
1865 {
1866 	struct xe_gt *gt = ct_to_gt(ct);
1867 	struct xe_guc *guc = ct_to_guc(ct);
1868 	u32 hxg_len = msg_len_to_hxg_len(len);
1869 	u32 *hxg = msg_to_hxg(msg);
1870 	u32 action = FIELD_GET(GUC_HXG_EVENT_MSG_0_ACTION, hxg[0]);
1871 	u32 *payload = hxg + GUC_HXG_MSG_MIN_LEN;
1872 	u32 adj_len = hxg_len - GUC_HXG_MSG_MIN_LEN;
1873 	int ret = 0;
1874 
1875 	switch (action) {
1876 	case XE_GUC_ACTION_REPORT_PAGE_FAULT_REQ_DESC:
1877 		ret = xe_guc_pagefault_handler(guc, payload, adj_len);
1878 		break;
1879 	case XE_GUC_ACTION_TLB_INVALIDATION_DONE:
1880 		__g2h_release_space(ct, len);
1881 		ret = xe_guc_tlb_inval_done_handler(guc, payload, adj_len);
1882 		break;
1883 	case XE_GUC_ACTION_PAGE_RECLAMATION_DONE:
1884 		__g2h_release_space(ct, len);
1885 		ret = xe_guc_page_reclaim_done_handler(guc, payload, adj_len);
1886 		break;
1887 	default:
1888 		xe_gt_warn(gt, "NOT_POSSIBLE\n");
1889 	}
1890 
1891 	if (ret) {
1892 		xe_gt_err(gt, "G2H action 0x%04x failed (%pe)\n",
1893 			  action, ERR_PTR(ret));
1894 		CT_DEAD(ct, NULL, FAST_G2H);
1895 	}
1896 }
1897 
1898 /**
1899  * xe_guc_ct_fast_path - process critical G2H in the IRQ handler
1900  * @ct: GuC CT object
1901  *
1902  * Anything related to page faults is critical for performance, process these
1903  * critical G2H in the IRQ. This is safe as these handlers either just wake up
1904  * waiters or queue another worker.
1905  */
1906 void xe_guc_ct_fast_path(struct xe_guc_ct *ct)
1907 {
1908 	struct xe_device *xe = ct_to_xe(ct);
1909 	bool ongoing;
1910 	int len;
1911 
1912 	ongoing = xe_pm_runtime_get_if_active(ct_to_xe(ct));
1913 	if (!ongoing && xe_pm_read_callback_task(ct_to_xe(ct)) == NULL)
1914 		return;
1915 
1916 	spin_lock(&ct->fast_lock);
1917 	do {
1918 		len = g2h_read(ct, ct->fast_msg, true);
1919 		if (len > 0)
1920 			g2h_fast_path(ct, ct->fast_msg, len);
1921 	} while (len > 0);
1922 	spin_unlock(&ct->fast_lock);
1923 
1924 	if (ongoing)
1925 		xe_pm_runtime_put(xe);
1926 }
1927 
1928 /* Returns less than zero on error, 0 on done, 1 on more available */
1929 static int dequeue_one_g2h(struct xe_guc_ct *ct)
1930 {
1931 	int len;
1932 	int ret;
1933 
1934 	lockdep_assert_held(&ct->lock);
1935 
1936 	spin_lock_irq(&ct->fast_lock);
1937 	len = g2h_read(ct, ct->msg, false);
1938 	spin_unlock_irq(&ct->fast_lock);
1939 	if (len <= 0)
1940 		return len;
1941 
1942 	ret = parse_g2h_msg(ct, ct->msg, len);
1943 	if (unlikely(ret < 0))
1944 		return ret;
1945 
1946 	ret = process_g2h_msg(ct, ct->msg, len);
1947 	if (unlikely(ret < 0))
1948 		return ret;
1949 
1950 	return 1;
1951 }
1952 
1953 static void receive_g2h(struct xe_guc_ct *ct)
1954 {
1955 	bool ongoing;
1956 	int ret;
1957 
1958 	/*
1959 	 * Normal users must always hold mem_access.ref around CT calls. However
1960 	 * during the runtime pm callbacks we rely on CT to talk to the GuC, but
1961 	 * at this stage we can't rely on mem_access.ref and even the
1962 	 * callback_task will be different than current.  For such cases we just
1963 	 * need to ensure we always process the responses from any blocking
1964 	 * ct_send requests or where we otherwise expect some response when
1965 	 * initiated from those callbacks (which will need to wait for the below
1966 	 * dequeue_one_g2h()).  The dequeue_one_g2h() will gracefully fail if
1967 	 * the device has suspended to the point that the CT communication has
1968 	 * been disabled.
1969 	 *
1970 	 * If we are inside the runtime pm callback, we can be the only task
1971 	 * still issuing CT requests (since that requires having the
1972 	 * mem_access.ref).  It seems like it might in theory be possible to
1973 	 * receive unsolicited events from the GuC just as we are
1974 	 * suspending-resuming, but those will currently anyway be lost when
1975 	 * eventually exiting from suspend, hence no need to wake up the device
1976 	 * here. If we ever need something stronger than get_if_ongoing() then
1977 	 * we need to be careful with blocking the pm callbacks from getting CT
1978 	 * responses, if the worker here is blocked on those callbacks
1979 	 * completing, creating a deadlock.
1980 	 */
1981 	ongoing = xe_pm_runtime_get_if_active(ct_to_xe(ct));
1982 	if (!ongoing && xe_pm_read_callback_task(ct_to_xe(ct)) == NULL)
1983 		return;
1984 
1985 	do {
1986 		mutex_lock(&ct->lock);
1987 		ret = dequeue_one_g2h(ct);
1988 		mutex_unlock(&ct->lock);
1989 
1990 		if (unlikely(ret == -EPROTO || ret == -EOPNOTSUPP)) {
1991 			xe_gt_err(ct_to_gt(ct), "CT dequeue failed: %d\n", ret);
1992 			CT_DEAD(ct, NULL, G2H_RECV);
1993 			kick_reset(ct);
1994 		}
1995 	} while (ret == 1);
1996 
1997 	if (ongoing)
1998 		xe_pm_runtime_put(ct_to_xe(ct));
1999 }
2000 
2001 static void g2h_worker_func(struct work_struct *w)
2002 {
2003 	struct xe_guc_ct *ct = container_of(w, struct xe_guc_ct, g2h_worker);
2004 
2005 	receive_g2h(ct);
2006 }
2007 
2008 static struct xe_guc_ct_snapshot *guc_ct_snapshot_alloc(struct xe_guc_ct *ct, bool atomic,
2009 							bool want_ctb)
2010 {
2011 	struct xe_guc_ct_snapshot *snapshot;
2012 
2013 	snapshot = kzalloc_obj(*snapshot, atomic ? GFP_ATOMIC : GFP_KERNEL);
2014 	if (!snapshot)
2015 		return NULL;
2016 
2017 	if (ct->ctbs.h2g.bo && ct->ctbs.g2h.bo && want_ctb) {
2018 		snapshot->ctb_size = xe_bo_size(ct->ctbs.h2g.bo) +
2019 			xe_bo_size(ct->ctbs.g2h.bo);
2020 		snapshot->ctb = kmalloc(snapshot->ctb_size, atomic ? GFP_ATOMIC : GFP_KERNEL);
2021 	}
2022 
2023 	return snapshot;
2024 }
2025 
2026 static void guc_ctb_snapshot_capture(struct xe_device *xe, struct guc_ctb *ctb,
2027 				     struct guc_ctb_snapshot *snapshot)
2028 {
2029 	xe_map_memcpy_from(xe, &snapshot->desc, &ctb->desc, 0,
2030 			   sizeof(struct guc_ct_buffer_desc));
2031 	memcpy(&snapshot->info, &ctb->info, sizeof(struct guc_ctb_info));
2032 }
2033 
2034 static void guc_ctb_snapshot_print(struct guc_ctb_snapshot *snapshot,
2035 				   struct drm_printer *p)
2036 {
2037 	drm_printf(p, "\tsize: %d\n", snapshot->info.size);
2038 	drm_printf(p, "\tresv_space: %d\n", snapshot->info.resv_space);
2039 	drm_printf(p, "\thead: %d\n", snapshot->info.head);
2040 	drm_printf(p, "\ttail: %d\n", snapshot->info.tail);
2041 	drm_printf(p, "\tspace: %d\n", snapshot->info.space);
2042 	drm_printf(p, "\tbroken: %d\n", snapshot->info.broken);
2043 	drm_printf(p, "\thead (memory): %d\n", snapshot->desc.head);
2044 	drm_printf(p, "\ttail (memory): %d\n", snapshot->desc.tail);
2045 	drm_printf(p, "\tstatus (memory): 0x%x\n", snapshot->desc.status);
2046 }
2047 
2048 static struct xe_guc_ct_snapshot *guc_ct_snapshot_capture(struct xe_guc_ct *ct, bool atomic,
2049 							  bool want_ctb)
2050 {
2051 	struct xe_device *xe = ct_to_xe(ct);
2052 	struct xe_guc_ct_snapshot *snapshot;
2053 
2054 	snapshot = guc_ct_snapshot_alloc(ct, atomic, want_ctb);
2055 	if (!snapshot) {
2056 		xe_gt_err(ct_to_gt(ct), "Skipping CTB snapshot entirely.\n");
2057 		return NULL;
2058 	}
2059 
2060 	if (xe_guc_ct_enabled(ct) || ct->state == XE_GUC_CT_STATE_STOPPED) {
2061 		snapshot->ct_enabled = true;
2062 		snapshot->g2h_outstanding = READ_ONCE(ct->g2h_outstanding);
2063 		guc_ctb_snapshot_capture(xe, &ct->ctbs.h2g, &snapshot->h2g);
2064 		guc_ctb_snapshot_capture(xe, &ct->ctbs.g2h, &snapshot->g2h);
2065 	}
2066 
2067 	if (ct->ctbs.h2g.bo && ct->ctbs.g2h.bo && snapshot->ctb) {
2068 		xe_map_memcpy_from(xe, snapshot->ctb, &ct->ctbs.h2g.bo->vmap, 0,
2069 				   xe_bo_size(ct->ctbs.h2g.bo));
2070 		xe_map_memcpy_from(xe, snapshot->ctb + xe_bo_size(ct->ctbs.h2g.bo),
2071 				   &ct->ctbs.g2h.bo->vmap, 0,
2072 				   xe_bo_size(ct->ctbs.g2h.bo));
2073 	}
2074 
2075 	return snapshot;
2076 }
2077 
2078 /**
2079  * xe_guc_ct_snapshot_capture - Take a quick snapshot of the CT state.
2080  * @ct: GuC CT object.
2081  *
2082  * This can be printed out in a later stage like during dev_coredump
2083  * analysis. This is safe to be called during atomic context.
2084  *
2085  * Returns: a GuC CT snapshot object that must be freed by the caller
2086  * by using `xe_guc_ct_snapshot_free`.
2087  */
2088 struct xe_guc_ct_snapshot *xe_guc_ct_snapshot_capture(struct xe_guc_ct *ct)
2089 {
2090 	return guc_ct_snapshot_capture(ct, true, true);
2091 }
2092 
2093 /**
2094  * xe_guc_ct_snapshot_print - Print out a given GuC CT snapshot.
2095  * @snapshot: GuC CT snapshot object.
2096  * @p: drm_printer where it will be printed out.
2097  *
2098  * This function prints out a given GuC CT snapshot object.
2099  */
2100 void xe_guc_ct_snapshot_print(struct xe_guc_ct_snapshot *snapshot,
2101 			      struct drm_printer *p)
2102 {
2103 	if (!snapshot)
2104 		return;
2105 
2106 	if (snapshot->ct_enabled) {
2107 		drm_puts(p, "H2G CTB (all sizes in DW):\n");
2108 		guc_ctb_snapshot_print(&snapshot->h2g, p);
2109 
2110 		drm_puts(p, "G2H CTB (all sizes in DW):\n");
2111 		guc_ctb_snapshot_print(&snapshot->g2h, p);
2112 		drm_printf(p, "\tg2h outstanding: %d\n",
2113 			   snapshot->g2h_outstanding);
2114 
2115 		if (snapshot->ctb) {
2116 			drm_printf(p, "[CTB].length: 0x%zx\n", snapshot->ctb_size);
2117 			xe_print_blob_ascii85(p, "[CTB].data", '\n',
2118 					      snapshot->ctb, 0, snapshot->ctb_size);
2119 		}
2120 	} else {
2121 		drm_puts(p, "CT disabled\n");
2122 	}
2123 }
2124 
2125 /**
2126  * xe_guc_ct_snapshot_free - Free all allocated objects for a given snapshot.
2127  * @snapshot: GuC CT snapshot object.
2128  *
2129  * This function free all the memory that needed to be allocated at capture
2130  * time.
2131  */
2132 void xe_guc_ct_snapshot_free(struct xe_guc_ct_snapshot *snapshot)
2133 {
2134 	if (!snapshot)
2135 		return;
2136 
2137 	kfree(snapshot->ctb);
2138 	kfree(snapshot);
2139 }
2140 
2141 /**
2142  * xe_guc_ct_print - GuC CT Print.
2143  * @ct: GuC CT.
2144  * @p: drm_printer where it will be printed out.
2145  * @want_ctb: Should the full CTB content be dumped (vs just the headers)
2146  *
2147  * This function will quickly capture a snapshot of the CT state
2148  * and immediately print it out.
2149  */
2150 void xe_guc_ct_print(struct xe_guc_ct *ct, struct drm_printer *p, bool want_ctb)
2151 {
2152 	struct xe_guc_ct_snapshot *snapshot;
2153 
2154 	snapshot = guc_ct_snapshot_capture(ct, false, want_ctb);
2155 	xe_guc_ct_snapshot_print(snapshot, p);
2156 	xe_guc_ct_snapshot_free(snapshot);
2157 }
2158 
2159 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG)
2160 
2161 #ifdef CONFIG_FUNCTION_ERROR_INJECTION
2162 /*
2163  * This is a helper function which assists the driver in identifying if a fault
2164  * injection test is currently active, allowing it to reduce unnecessary debug
2165  * output. Typically, the function returns zero, but the fault injection
2166  * framework can alter this to return an error. Since faults are injected
2167  * through this function, it's important to ensure the compiler doesn't optimize
2168  * it into an inline function. To avoid such optimization, the 'noinline'
2169  * attribute is applied. Compiler optimizes the static function defined in the
2170  * header file as an inline function.
2171  */
2172 noinline int xe_is_injection_active(void) { return 0; }
2173 ALLOW_ERROR_INJECTION(xe_is_injection_active, ERRNO);
2174 #else
2175 int xe_is_injection_active(void) { return 0; }
2176 #endif
2177 
2178 static void ct_dead_capture(struct xe_guc_ct *ct, struct guc_ctb *ctb, u32 reason_code)
2179 {
2180 	struct xe_guc_log_snapshot *snapshot_log;
2181 	struct xe_guc_ct_snapshot *snapshot_ct;
2182 	struct xe_guc *guc = ct_to_guc(ct);
2183 	unsigned long flags;
2184 	bool have_capture;
2185 
2186 	if (ctb)
2187 		ctb->info.broken = true;
2188 	/*
2189 	 * Huge dump is getting generated when injecting error for guc CT/MMIO
2190 	 * functions. So, let us suppress the dump when fault is injected.
2191 	 */
2192 	if (xe_is_injection_active())
2193 		return;
2194 
2195 	/* Ignore further errors after the first dump until a reset */
2196 	if (ct->dead.reported)
2197 		return;
2198 
2199 	spin_lock_irqsave(&ct->dead.lock, flags);
2200 
2201 	/* And only capture one dump at a time */
2202 	have_capture = ct->dead.reason & (1 << CT_DEAD_STATE_CAPTURE);
2203 	ct->dead.reason |= (1 << reason_code) |
2204 			   (1 << CT_DEAD_STATE_CAPTURE);
2205 
2206 	spin_unlock_irqrestore(&ct->dead.lock, flags);
2207 
2208 	if (have_capture)
2209 		return;
2210 
2211 	snapshot_log = xe_guc_log_snapshot_capture(&guc->log, true);
2212 	snapshot_ct = xe_guc_ct_snapshot_capture((ct));
2213 
2214 	spin_lock_irqsave(&ct->dead.lock, flags);
2215 
2216 	if (ct->dead.snapshot_log || ct->dead.snapshot_ct) {
2217 		xe_gt_err(ct_to_gt(ct), "Got unexpected dead CT capture!\n");
2218 		xe_guc_log_snapshot_free(snapshot_log);
2219 		xe_guc_ct_snapshot_free(snapshot_ct);
2220 	} else {
2221 		ct->dead.snapshot_log = snapshot_log;
2222 		ct->dead.snapshot_ct = snapshot_ct;
2223 	}
2224 
2225 	spin_unlock_irqrestore(&ct->dead.lock, flags);
2226 
2227 	queue_work(system_dfl_wq, &(ct)->dead.worker);
2228 }
2229 
2230 static void ct_dead_print(struct xe_dead_ct *dead)
2231 {
2232 	struct xe_guc_ct *ct = container_of(dead, struct xe_guc_ct, dead);
2233 	struct xe_device *xe = ct_to_xe(ct);
2234 	struct xe_gt *gt = ct_to_gt(ct);
2235 	static int g_count;
2236 	struct drm_printer ip = xe_gt_info_printer(gt);
2237 	struct drm_printer lp = drm_line_printer(&ip, "Capture", ++g_count);
2238 
2239 	if (!dead->reason) {
2240 		xe_gt_err(gt, "CTB is dead for no reason!?\n");
2241 		return;
2242 	}
2243 
2244 	/* Can't generate a genuine core dump at this point, so just do the good bits */
2245 	drm_puts(&lp, "**** Xe Device Coredump ****\n");
2246 	drm_printf(&lp, "Reason: CTB is dead - 0x%X\n", dead->reason);
2247 	xe_device_snapshot_print(xe, &lp);
2248 
2249 	drm_printf(&lp, "**** GT #%d ****\n", gt->info.id);
2250 	drm_printf(&lp, "\tTile: %d\n", gt->tile->id);
2251 
2252 	drm_puts(&lp, "**** GuC Log ****\n");
2253 	xe_guc_log_snapshot_print(dead->snapshot_log, &lp);
2254 
2255 	drm_puts(&lp, "**** GuC CT ****\n");
2256 	xe_guc_ct_snapshot_print(dead->snapshot_ct, &lp);
2257 
2258 	drm_puts(&lp, "Done.\n");
2259 }
2260 
2261 static void ct_dead_worker_func(struct work_struct *w)
2262 {
2263 	struct xe_guc_ct *ct = container_of(w, struct xe_guc_ct, dead.worker);
2264 
2265 	if (!ct->dead.reported) {
2266 		ct->dead.reported = true;
2267 		ct_dead_print(&ct->dead);
2268 	}
2269 
2270 	spin_lock_irq(&ct->dead.lock);
2271 
2272 	xe_guc_log_snapshot_free(ct->dead.snapshot_log);
2273 	ct->dead.snapshot_log = NULL;
2274 	xe_guc_ct_snapshot_free(ct->dead.snapshot_ct);
2275 	ct->dead.snapshot_ct = NULL;
2276 
2277 	if (ct->dead.reason & (1 << CT_DEAD_STATE_REARM)) {
2278 		/* A reset has occurred so re-arm the error reporting */
2279 		ct->dead.reason = 0;
2280 		ct->dead.reported = false;
2281 	}
2282 
2283 	spin_unlock_irq(&ct->dead.lock);
2284 }
2285 #endif
2286