xref: /linux/drivers/gpu/drm/xe/xe_guc_submit.c (revision fafb66e5903c2bcfc7b7e259042a8282f18a6faa)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_guc_submit.h"
7 
8 #include <linux/bitfield.h>
9 #include <linux/bitmap.h>
10 #include <linux/circ_buf.h>
11 #include <linux/dma-fence-array.h>
12 
13 #include <drm/drm_drv.h>
14 #include <drm/drm_managed.h>
15 
16 #include "abi/guc_actions_abi.h"
17 #include "abi/guc_actions_slpc_abi.h"
18 #include "abi/guc_klvs_abi.h"
19 #include "xe_assert.h"
20 #include "xe_bo.h"
21 #include "xe_devcoredump.h"
22 #include "xe_device.h"
23 #include "xe_exec_queue.h"
24 #include "xe_force_wake.h"
25 #include "xe_gpu_scheduler.h"
26 #include "xe_gt.h"
27 #include "xe_gt_clock.h"
28 #include "xe_gt_printk.h"
29 #include "xe_guc.h"
30 #include "xe_guc_capture.h"
31 #include "xe_guc_ct.h"
32 #include "xe_guc_exec_queue_types.h"
33 #include "xe_guc_id_mgr.h"
34 #include "xe_guc_klv_helpers.h"
35 #include "xe_guc_submit_types.h"
36 #include "xe_hw_engine.h"
37 #include "xe_lrc.h"
38 #include "xe_macros.h"
39 #include "xe_map.h"
40 #include "xe_mocs.h"
41 #include "xe_module.h"
42 #include "xe_pm.h"
43 #include "xe_ring_ops_types.h"
44 #include "xe_sched_job.h"
45 #include "xe_sleep.h"
46 #include "xe_trace.h"
47 #include "xe_uc_fw.h"
48 #include "xe_vm.h"
49 
50 #define XE_GUC_EXEC_QUEUE_CGP_CONTEXT_ERROR_LEN		6
51 
52 static int guc_submit_reset_prepare(struct xe_guc *guc);
53 
54 static struct xe_guc *
55 exec_queue_to_guc(struct xe_exec_queue *q)
56 {
57 	return &q->gt->uc.guc;
58 }
59 
60 /*
61  * Helpers for engine state, using an atomic as some of the bits can transition
62  * as the same time (e.g. a suspend can be happning at the same time as schedule
63  * engine done being processed).
64  */
65 #define EXEC_QUEUE_STATE_REGISTERED		(1 << 0)
66 #define EXEC_QUEUE_STATE_ENABLED		(1 << 1)
67 #define EXEC_QUEUE_STATE_PENDING_ENABLE		(1 << 2)
68 #define EXEC_QUEUE_STATE_PENDING_DISABLE	(1 << 3)
69 #define EXEC_QUEUE_STATE_DESTROYED		(1 << 4)
70 #define EXEC_QUEUE_STATE_SUSPENDED		(1 << 5)
71 #define EXEC_QUEUE_STATE_RESET			(1 << 6)
72 #define EXEC_QUEUE_STATE_KILLED			(1 << 7)
73 #define EXEC_QUEUE_STATE_WEDGED			(1 << 8)
74 #define EXEC_QUEUE_STATE_BANNED			(1 << 9)
75 #define EXEC_QUEUE_STATE_PENDING_RESUME		(1 << 10)
76 
77 static bool exec_queue_registered(struct xe_exec_queue *q)
78 {
79 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_REGISTERED;
80 }
81 
82 static void set_exec_queue_registered(struct xe_exec_queue *q)
83 {
84 	atomic_or(EXEC_QUEUE_STATE_REGISTERED, &q->guc->state);
85 }
86 
87 static void clear_exec_queue_registered(struct xe_exec_queue *q)
88 {
89 	atomic_and(~EXEC_QUEUE_STATE_REGISTERED, &q->guc->state);
90 }
91 
92 static bool exec_queue_enabled(struct xe_exec_queue *q)
93 {
94 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_ENABLED;
95 }
96 
97 static void set_exec_queue_enabled(struct xe_exec_queue *q)
98 {
99 	atomic_or(EXEC_QUEUE_STATE_ENABLED, &q->guc->state);
100 }
101 
102 static void clear_exec_queue_enabled(struct xe_exec_queue *q)
103 {
104 	atomic_and(~EXEC_QUEUE_STATE_ENABLED, &q->guc->state);
105 }
106 
107 static bool exec_queue_pending_enable(struct xe_exec_queue *q)
108 {
109 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_ENABLE;
110 }
111 
112 static void set_exec_queue_pending_enable(struct xe_exec_queue *q)
113 {
114 	atomic_or(EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state);
115 }
116 
117 static void clear_exec_queue_pending_enable(struct xe_exec_queue *q)
118 {
119 	atomic_and(~EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state);
120 }
121 
122 static bool exec_queue_pending_disable(struct xe_exec_queue *q)
123 {
124 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_DISABLE;
125 }
126 
127 static void set_exec_queue_pending_disable(struct xe_exec_queue *q)
128 {
129 	atomic_or(EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state);
130 }
131 
132 static void clear_exec_queue_pending_disable(struct xe_exec_queue *q)
133 {
134 	atomic_and(~EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state);
135 }
136 
137 static bool exec_queue_destroyed(struct xe_exec_queue *q)
138 {
139 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_DESTROYED;
140 }
141 
142 static void set_exec_queue_destroyed(struct xe_exec_queue *q)
143 {
144 	atomic_or(EXEC_QUEUE_STATE_DESTROYED, &q->guc->state);
145 }
146 
147 static void clear_exec_queue_destroyed(struct xe_exec_queue *q)
148 {
149 	atomic_and(~EXEC_QUEUE_STATE_DESTROYED, &q->guc->state);
150 }
151 
152 static bool exec_queue_banned(struct xe_exec_queue *q)
153 {
154 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_BANNED;
155 }
156 
157 static void set_exec_queue_banned(struct xe_exec_queue *q)
158 {
159 	atomic_or(EXEC_QUEUE_STATE_BANNED, &q->guc->state);
160 }
161 
162 static void clear_exec_queue_banned(struct xe_exec_queue *q)
163 {
164 	atomic_andnot(EXEC_QUEUE_STATE_BANNED, &q->guc->state);
165 }
166 
167 static bool exec_queue_suspended(struct xe_exec_queue *q)
168 {
169 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_SUSPENDED;
170 }
171 
172 static void set_exec_queue_suspended(struct xe_exec_queue *q)
173 {
174 	atomic_or(EXEC_QUEUE_STATE_SUSPENDED, &q->guc->state);
175 }
176 
177 static void clear_exec_queue_suspended(struct xe_exec_queue *q)
178 {
179 	atomic_and(~EXEC_QUEUE_STATE_SUSPENDED, &q->guc->state);
180 }
181 
182 static bool exec_queue_reset(struct xe_exec_queue *q)
183 {
184 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_RESET;
185 }
186 
187 static void set_exec_queue_reset(struct xe_exec_queue *q)
188 {
189 	atomic_or(EXEC_QUEUE_STATE_RESET, &q->guc->state);
190 }
191 
192 static bool exec_queue_killed(struct xe_exec_queue *q)
193 {
194 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_KILLED;
195 }
196 
197 static void set_exec_queue_killed(struct xe_exec_queue *q)
198 {
199 	atomic_or(EXEC_QUEUE_STATE_KILLED, &q->guc->state);
200 }
201 
202 static bool exec_queue_wedged(struct xe_exec_queue *q)
203 {
204 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_WEDGED;
205 }
206 
207 static void set_exec_queue_wedged(struct xe_exec_queue *q)
208 {
209 	atomic_or(EXEC_QUEUE_STATE_WEDGED, &q->guc->state);
210 }
211 
212 static bool exec_queue_pending_resume(struct xe_exec_queue *q)
213 {
214 	return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_RESUME;
215 }
216 
217 static void set_exec_queue_pending_resume(struct xe_exec_queue *q)
218 {
219 	atomic_or(EXEC_QUEUE_STATE_PENDING_RESUME, &q->guc->state);
220 }
221 
222 static void clear_exec_queue_pending_resume(struct xe_exec_queue *q)
223 {
224 	atomic_and(~EXEC_QUEUE_STATE_PENDING_RESUME, &q->guc->state);
225 }
226 
227 static bool exec_queue_killed_or_banned_or_wedged(struct xe_exec_queue *q)
228 {
229 	return (atomic_read(&q->guc->state) &
230 		(EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_KILLED |
231 		 EXEC_QUEUE_STATE_BANNED));
232 }
233 
234 static void guc_submit_sw_fini(struct drm_device *drm, void *arg)
235 {
236 	struct xe_guc *guc = arg;
237 	struct xe_gt *gt = guc_to_gt(guc);
238 
239 	xe_gt_assert(gt, xa_empty(&guc->submission_state.exec_queue_lookup));
240 
241 	xa_destroy(&guc->submission_state.exec_queue_lookup);
242 }
243 
244 static void guc_submit_fini(void *arg)
245 {
246 	struct xe_guc *guc = arg;
247 	struct xe_exec_queue *q;
248 	unsigned long index;
249 
250 	/* Drop any wedged queue refs */
251 	mutex_lock(&guc->submission_state.lock);
252 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
253 		if (exec_queue_wedged(q)) {
254 			mutex_unlock(&guc->submission_state.lock);
255 			xe_exec_queue_put(q);
256 			mutex_lock(&guc->submission_state.lock);
257 		}
258 	}
259 	mutex_unlock(&guc->submission_state.lock);
260 
261 	/* Forcefully kill any remaining exec queues */
262 	xe_guc_ct_stop(&guc->ct);
263 	guc_submit_reset_prepare(guc);
264 	xe_guc_softreset(guc);
265 	xe_guc_submit_stop(guc);
266 	xe_uc_fw_sanitize(&guc->fw);
267 	xe_guc_submit_pause_abort(guc);
268 }
269 
270 static const struct xe_exec_queue_ops guc_exec_queue_ops;
271 
272 static void primelockdep(struct xe_guc *guc)
273 {
274 	if (!IS_ENABLED(CONFIG_LOCKDEP))
275 		return;
276 
277 	fs_reclaim_acquire(GFP_KERNEL);
278 
279 	mutex_lock(&guc->submission_state.lock);
280 	mutex_unlock(&guc->submission_state.lock);
281 
282 	fs_reclaim_release(GFP_KERNEL);
283 }
284 
285 /**
286  * xe_guc_submit_init() - Initialize GuC submission.
287  * @guc: the &xe_guc to initialize
288  * @num_ids: number of GuC context IDs to use
289  *
290  * The bare-metal or PF driver can pass ~0 as &num_ids to indicate that all
291  * GuC context IDs supported by the GuC firmware should be used for submission.
292  *
293  * Only VF drivers will have to provide explicit number of GuC context IDs
294  * that they can use for submission.
295  *
296  * Return: 0 on success or a negative error code on failure.
297  */
298 int xe_guc_submit_init(struct xe_guc *guc, unsigned int num_ids)
299 {
300 	struct xe_device *xe = guc_to_xe(guc);
301 	struct xe_gt *gt = guc_to_gt(guc);
302 	int err;
303 
304 	err = drmm_mutex_init(&xe->drm, &guc->submission_state.lock);
305 	if (err)
306 		return err;
307 
308 	err = xe_guc_id_mgr_init(&guc->submission_state.idm, num_ids);
309 	if (err)
310 		return err;
311 
312 	gt->exec_queue_ops = &guc_exec_queue_ops;
313 
314 	xa_init(&guc->submission_state.exec_queue_lookup);
315 
316 	primelockdep(guc);
317 
318 	guc->submission_state.initialized = true;
319 
320 	err = drmm_add_action_or_reset(&xe->drm, guc_submit_sw_fini, guc);
321 	if (err)
322 		return err;
323 
324 	return devm_add_action_or_reset(xe->drm.dev, guc_submit_fini, guc);
325 }
326 
327 /*
328  * Given that we want to guarantee enough RCS throughput to avoid missing
329  * frames, we set the yield policy to 20% of each 80ms interval.
330  */
331 #define RC_YIELD_DURATION	80	/* in ms */
332 #define RC_YIELD_RATIO		20	/* in percent */
333 static u32 *emit_render_compute_yield_klv(u32 *emit)
334 {
335 	*emit++ = PREP_GUC_KLV_TAG(SCHEDULING_POLICIES_RENDER_COMPUTE_YIELD);
336 	*emit++ = RC_YIELD_DURATION;
337 	*emit++ = RC_YIELD_RATIO;
338 
339 	return emit;
340 }
341 
342 #define SCHEDULING_POLICY_MAX_DWORDS 16
343 static int guc_init_global_schedule_policy(struct xe_guc *guc)
344 {
345 	u32 data[SCHEDULING_POLICY_MAX_DWORDS];
346 	u32 *emit = data;
347 	u32 count = 0;
348 	int ret;
349 
350 	if (GUC_SUBMIT_VER(guc) < MAKE_GUC_VER(1, 1, 0))
351 		return 0;
352 
353 	*emit++ = XE_GUC_ACTION_UPDATE_SCHEDULING_POLICIES_KLV;
354 
355 	if (CCS_INSTANCES(guc_to_gt(guc)))
356 		emit = emit_render_compute_yield_klv(emit);
357 
358 	count = emit - data;
359 	if (count > 1) {
360 		xe_assert(guc_to_xe(guc), count <= SCHEDULING_POLICY_MAX_DWORDS);
361 
362 		ret = xe_guc_ct_send_block(&guc->ct, data, count);
363 		if (ret < 0) {
364 			xe_gt_err(guc_to_gt(guc),
365 				  "failed to enable GuC scheduling policies: %pe\n",
366 				  ERR_PTR(ret));
367 			return ret;
368 		}
369 	}
370 
371 	return 0;
372 }
373 
374 int xe_guc_submit_enable(struct xe_guc *guc)
375 {
376 	int ret;
377 
378 	ret = guc_init_global_schedule_policy(guc);
379 	if (ret)
380 		return ret;
381 
382 	guc->submission_state.enabled = true;
383 
384 	return 0;
385 }
386 
387 void xe_guc_submit_disable(struct xe_guc *guc)
388 {
389 	guc->submission_state.enabled = false;
390 }
391 
392 static void __release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q,
393 			     int count)
394 {
395 	int i;
396 
397 	mutex_lock(&guc->submission_state.lock);
398 
399 	for (i = 0; i < count; ++i)
400 		xa_erase(&guc->submission_state.exec_queue_lookup,
401 			 q->guc->id + i);
402 
403 	xe_guc_id_mgr_release_locked(&guc->submission_state.idm,
404 				     q->guc->id, q->width);
405 
406 	mutex_unlock(&guc->submission_state.lock);
407 }
408 
409 static int alloc_guc_id(struct xe_guc *guc, struct xe_exec_queue *q)
410 {
411 	int ret, i;
412 
413 	mutex_lock(&guc->submission_state.lock);
414 	ret = xe_guc_id_mgr_reserve_locked(&guc->submission_state.idm,
415 					   q->width);
416 	mutex_unlock(&guc->submission_state.lock);
417 	if (ret < 0)
418 		return ret;
419 
420 	q->guc->id = ret;
421 
422 	/* Reserve empty slots. */
423 	for (i = 0; i < q->width; ++i) {
424 		ret = xa_insert(&guc->submission_state.exec_queue_lookup,
425 				 q->guc->id + i, NULL, GFP_KERNEL);
426 		if (ret)
427 			goto err_release;
428 	}
429 
430 	return 0;
431 
432 err_release:
433 	__release_guc_id(guc, q, i);
434 
435 	return ret;
436 }
437 
438 static void publish_guc_id(struct xe_guc *guc, struct xe_exec_queue *q)
439 {
440 	int i;
441 
442 	lockdep_assert_held(&guc->submission_state.lock);
443 
444 	for (i = 0; i < q->width; ++i) {
445 		void *old;
446 
447 		old = xa_store(&guc->submission_state.exec_queue_lookup,
448 			       q->guc->id + i, q, GFP_NOWAIT);
449 		XE_WARN_ON(old || xa_is_err(old));
450 	}
451 }
452 
453 static void release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q)
454 {
455 	__release_guc_id(guc, q, q->width);
456 }
457 
458 struct exec_queue_policy {
459 	u32 count;
460 	struct guc_update_exec_queue_policy h2g;
461 };
462 
463 static u32 __guc_exec_queue_policy_action_size(struct exec_queue_policy *policy)
464 {
465 	size_t bytes = sizeof(policy->h2g.header) +
466 		       (sizeof(policy->h2g.klv[0]) * policy->count);
467 
468 	return bytes / sizeof(u32);
469 }
470 
471 static void __guc_exec_queue_policy_start_klv(struct exec_queue_policy *policy,
472 					      u16 guc_id)
473 {
474 	policy->h2g.header.action =
475 		XE_GUC_ACTION_HOST2GUC_UPDATE_CONTEXT_POLICIES;
476 	policy->h2g.header.guc_id = guc_id;
477 	policy->count = 0;
478 }
479 
480 #define MAKE_EXEC_QUEUE_POLICY_ADD(func, id) \
481 static void __guc_exec_queue_policy_add_##func(struct exec_queue_policy *policy, \
482 					   u32 data) \
483 { \
484 	XE_WARN_ON(policy->count >= GUC_CONTEXT_POLICIES_KLV_NUM_IDS); \
485 \
486 	policy->h2g.klv[policy->count].kl = \
487 		FIELD_PREP(GUC_KLV_0_KEY, \
488 			   GUC_CONTEXT_POLICIES_KLV_ID_##id) | \
489 		FIELD_PREP(GUC_KLV_0_LEN, 1); \
490 	policy->h2g.klv[policy->count].value = data; \
491 	policy->count++; \
492 }
493 
494 MAKE_EXEC_QUEUE_POLICY_ADD(execution_quantum, EXECUTION_QUANTUM)
495 MAKE_EXEC_QUEUE_POLICY_ADD(preemption_timeout, PREEMPTION_TIMEOUT)
496 MAKE_EXEC_QUEUE_POLICY_ADD(priority, SCHEDULING_PRIORITY)
497 MAKE_EXEC_QUEUE_POLICY_ADD(slpc_exec_queue_freq_req, SLPM_GT_FREQUENCY)
498 #undef MAKE_EXEC_QUEUE_POLICY_ADD
499 
500 static const int xe_exec_queue_prio_to_guc[] = {
501 	[XE_EXEC_QUEUE_PRIORITY_LOW] = GUC_CLIENT_PRIORITY_NORMAL,
502 	[XE_EXEC_QUEUE_PRIORITY_NORMAL] = GUC_CLIENT_PRIORITY_KMD_NORMAL,
503 	[XE_EXEC_QUEUE_PRIORITY_HIGH] = GUC_CLIENT_PRIORITY_HIGH,
504 	[XE_EXEC_QUEUE_PRIORITY_KERNEL] = GUC_CLIENT_PRIORITY_KMD_HIGH,
505 };
506 
507 static void init_policies(struct xe_guc *guc, struct xe_exec_queue *q)
508 {
509 	struct exec_queue_policy policy;
510 	enum xe_exec_queue_priority prio = q->sched_props.priority;
511 	u32 timeslice_us = q->sched_props.timeslice_us;
512 	u32 slpc_exec_queue_freq_req = 0;
513 	u32 preempt_timeout_us = q->sched_props.preempt_timeout_us;
514 
515 	xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q) &&
516 		     !xe_exec_queue_is_multi_queue_secondary(q));
517 
518 	if (q->flags & EXEC_QUEUE_FLAG_LOW_LATENCY)
519 		slpc_exec_queue_freq_req |= SLPC_CTX_FREQ_REQ_IS_COMPUTE;
520 
521 	__guc_exec_queue_policy_start_klv(&policy, q->guc->id);
522 	__guc_exec_queue_policy_add_priority(&policy, xe_exec_queue_prio_to_guc[prio]);
523 	__guc_exec_queue_policy_add_execution_quantum(&policy, timeslice_us);
524 	__guc_exec_queue_policy_add_preemption_timeout(&policy, preempt_timeout_us);
525 	__guc_exec_queue_policy_add_slpc_exec_queue_freq_req(&policy,
526 							     slpc_exec_queue_freq_req);
527 
528 	xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g,
529 		       __guc_exec_queue_policy_action_size(&policy), 0, 0);
530 }
531 
532 static void set_min_preemption_timeout(struct xe_guc *guc, struct xe_exec_queue *q)
533 {
534 	struct exec_queue_policy policy;
535 
536 	xe_assert(guc_to_xe(guc), !xe_exec_queue_is_multi_queue_secondary(q));
537 
538 	__guc_exec_queue_policy_start_klv(&policy, q->guc->id);
539 	__guc_exec_queue_policy_add_preemption_timeout(&policy, 1);
540 
541 	xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g,
542 		       __guc_exec_queue_policy_action_size(&policy), 0, 0);
543 }
544 
545 static bool vf_recovery(struct xe_guc *guc)
546 {
547 	return xe_gt_recovery_pending(guc_to_gt(guc));
548 }
549 
550 static void xe_guc_exec_queue_trigger_cleanup(struct xe_exec_queue *q)
551 {
552 	struct xe_guc *guc = exec_queue_to_guc(q);
553 	struct xe_device *xe = guc_to_xe(guc);
554 
555 	/** to wakeup xe_wait_user_fence ioctl if exec queue is reset */
556 	wake_up_all(&xe->ufence_wq);
557 
558 	xe_sched_tdr_queue_imm(&q->guc->sched);
559 }
560 
561 static void xe_guc_exec_queue_group_stop(struct xe_exec_queue *q)
562 {
563 	struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
564 	struct xe_exec_queue_group *group = q->multi_queue.group;
565 	struct xe_exec_queue *eq, *next;
566 	LIST_HEAD(tmp);
567 
568 	xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)),
569 		     xe_exec_queue_is_multi_queue(q));
570 
571 	mutex_lock(&group->list_lock);
572 
573 	/*
574 	 * Stop all future queues being from executing while group is stopped.
575 	 */
576 	group->stopped = true;
577 
578 	list_for_each_entry_safe(eq, next, &group->list, multi_queue.link)
579 		/*
580 		 * Refcount prevents an attempted removal from &group->list,
581 		 * temporary list allows safe iteration after dropping
582 		 * &group->list_lock.
583 		 */
584 		if (xe_exec_queue_get_unless_zero(eq))
585 			list_move_tail(&eq->multi_queue.link, &tmp);
586 
587 	mutex_unlock(&group->list_lock);
588 
589 	/* We cannot stop under list lock without getting inversions */
590 	xe_sched_submission_stop(&primary->guc->sched);
591 	list_for_each_entry(eq, &tmp, multi_queue.link)
592 		xe_sched_submission_stop(&eq->guc->sched);
593 
594 	mutex_lock(&group->list_lock);
595 	list_for_each_entry_safe(eq, next, &tmp, multi_queue.link) {
596 		/*
597 		 * Corner where we got banned while stopping and not on
598 		 * &group->list
599 		 */
600 		if (READ_ONCE(group->banned))
601 			xe_guc_exec_queue_trigger_cleanup(eq);
602 
603 		list_move_tail(&eq->multi_queue.link, &group->list);
604 		xe_exec_queue_put(eq);
605 	}
606 	mutex_unlock(&group->list_lock);
607 }
608 
609 static void xe_guc_exec_queue_group_start(struct xe_exec_queue *q)
610 {
611 	struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
612 	struct xe_exec_queue_group *group = q->multi_queue.group;
613 	struct xe_exec_queue *eq;
614 
615 	xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)),
616 		     xe_exec_queue_is_multi_queue(q));
617 
618 	xe_sched_submission_start(&primary->guc->sched);
619 
620 	mutex_lock(&group->list_lock);
621 	group->stopped = false;
622 	list_for_each_entry(eq, &group->list, multi_queue.link)
623 		xe_sched_submission_start(&eq->guc->sched);
624 	mutex_unlock(&group->list_lock);
625 }
626 
627 static void xe_guc_exec_queue_group_trigger_cleanup(struct xe_exec_queue *q)
628 {
629 	struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
630 	struct xe_exec_queue_group *group = q->multi_queue.group;
631 	struct xe_exec_queue *eq;
632 
633 	xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)),
634 		     xe_exec_queue_is_multi_queue(q));
635 
636 	/* Group banned, skip timeout check in TDR */
637 	WRITE_ONCE(group->banned, true);
638 	xe_guc_exec_queue_trigger_cleanup(primary);
639 
640 	mutex_lock(&group->list_lock);
641 	list_for_each_entry(eq, &group->list, multi_queue.link)
642 		xe_guc_exec_queue_trigger_cleanup(eq);
643 	mutex_unlock(&group->list_lock);
644 }
645 
646 static void xe_guc_exec_queue_reset_trigger_cleanup(struct xe_exec_queue *q)
647 {
648 	if (xe_exec_queue_is_multi_queue(q)) {
649 		struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
650 		struct xe_exec_queue_group *group = q->multi_queue.group;
651 		struct xe_exec_queue *eq;
652 
653 		/* Group banned, skip timeout check in TDR */
654 		WRITE_ONCE(group->banned, true);
655 
656 		set_exec_queue_reset(primary);
657 		if (!exec_queue_banned(primary))
658 			xe_guc_exec_queue_trigger_cleanup(primary);
659 
660 		mutex_lock(&group->list_lock);
661 		list_for_each_entry(eq, &group->list, multi_queue.link) {
662 			set_exec_queue_reset(eq);
663 			if (!exec_queue_banned(eq))
664 				xe_guc_exec_queue_trigger_cleanup(eq);
665 		}
666 		mutex_unlock(&group->list_lock);
667 	} else {
668 		set_exec_queue_reset(q);
669 		if (!exec_queue_banned(q))
670 			xe_guc_exec_queue_trigger_cleanup(q);
671 	}
672 }
673 
674 static void set_exec_queue_group_banned(struct xe_exec_queue *q)
675 {
676 	struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
677 	struct xe_exec_queue_group *group = q->multi_queue.group;
678 	struct xe_exec_queue *eq;
679 
680 	/* Ban all queues of the multi-queue group */
681 	xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)),
682 		     xe_exec_queue_is_multi_queue(q));
683 	set_exec_queue_banned(primary);
684 
685 	mutex_lock(&group->list_lock);
686 	list_for_each_entry(eq, &group->list, multi_queue.link)
687 		set_exec_queue_banned(eq);
688 	mutex_unlock(&group->list_lock);
689 }
690 
691 /* Helper for context registration H2G */
692 struct guc_ctxt_registration_info {
693 	u32 flags;
694 	u32 context_idx;
695 	u32 engine_class;
696 	u32 engine_submit_mask;
697 	u32 wq_desc_lo;
698 	u32 wq_desc_hi;
699 	u32 wq_base_lo;
700 	u32 wq_base_hi;
701 	u32 wq_size;
702 	u32 cgp_lo;
703 	u32 cgp_hi;
704 	u32 hwlrca_lo;
705 	u32 hwlrca_hi;
706 };
707 
708 #define parallel_read(xe_, map_, field_) \
709 	xe_map_rd_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \
710 			field_)
711 #define parallel_write(xe_, map_, field_, val_) \
712 	xe_map_wr_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \
713 			field_, val_)
714 
715 /**
716  * DOC: Multi Queue Group GuC interface
717  *
718  * The multi queue group coordination between KMD and GuC is through a software
719  * construct called Context Group Page (CGP). The CGP is a KMD managed 4KB page
720  * allocated in the global GTT.
721  *
722  * CGP format:
723  *
724  * +-----------+---------------------------+---------------------------------------------+
725  * | DWORD     | Name                      | Description                                 |
726  * +-----------+---------------------------+---------------------------------------------+
727  * | 0         | Version                   | Bits [15:8]=Major ver, [7:0]=Minor ver      |
728  * +-----------+---------------------------+---------------------------------------------+
729  * | 1..15     | RESERVED                  | MBZ                                         |
730  * +-----------+---------------------------+---------------------------------------------+
731  * | 16        | KMD_QUEUE_UPDATE_MASK_DW0 | KMD queue mask for queues 31..0             |
732  * +-----------+---------------------------+---------------------------------------------+
733  * | 17        | KMD_QUEUE_UPDATE_MASK_DW1 | KMD queue mask for queues 63..32            |
734  * +-----------+---------------------------+---------------------------------------------+
735  * | 18..31    | RESERVED                  | MBZ                                         |
736  * +-----------+---------------------------+---------------------------------------------+
737  * | 32        | Q0CD_DW0                  | Queue 0 context LRC descriptor lower DWORD  |
738  * +-----------+---------------------------+---------------------------------------------+
739  * | 33        | Q0ContextIndex            | Context ID for Queue 0                      |
740  * +-----------+---------------------------+---------------------------------------------+
741  * | 34        | Q1CD_DW0                  | Queue 1 context LRC descriptor lower DWORD  |
742  * +-----------+---------------------------+---------------------------------------------+
743  * | 35        | Q1ContextIndex            | Context ID for Queue 1                      |
744  * +-----------+---------------------------+---------------------------------------------+
745  * | ...       |...                        | ...                                         |
746  * +-----------+---------------------------+---------------------------------------------+
747  * | 158       | Q63CD_DW0                 | Queue 63 context LRC descriptor lower DWORD |
748  * +-----------+---------------------------+---------------------------------------------+
749  * | 159       | Q63ContextIndex           | Context ID for Queue 63                     |
750  * +-----------+---------------------------+---------------------------------------------+
751  * | 160..1024 | RESERVED                  | MBZ                                         |
752  * +-----------+---------------------------+---------------------------------------------+
753  *
754  * While registering Q0 with GuC, CGP is updated with Q0 entry and GuC is notified
755  * through XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_QUEUE H2G message which specifies
756  * the CGP address. When the secondary queues are added to the group, the CGP is
757  * updated with entry for that queue and GuC is notified through the H2G interface
758  * XE_GUC_ACTION_MULTI_QUEUE_CONTEXT_CGP_SYNC. GuC responds to these H2G messages
759  * with a XE_GUC_ACTION_NOTIFY_MULTIQ_CONTEXT_CGP_SYNC_DONE G2H message. GuC also
760  * sends a XE_GUC_ACTION_NOTIFY_MULTI_QUEUE_CGP_CONTEXT_ERROR notification for any
761  * error in the CGP. Only one of these CGP update messages can be outstanding
762  * (waiting for GuC response) at any time. The bits in KMD_QUEUE_UPDATE_MASK_DW*
763  * fields indicate which queue entry is being updated in the CGP.
764  *
765  * The primary queue (Q0) represents the multi queue group context in GuC and
766  * submission on any queue of the group must be through Q0 GuC interface only.
767  *
768  * As it is not required to register secondary queues with GuC, the secondary queue
769  * context ids in the CGP are populated with Q0 context id.
770  */
771 
772 #define CGP_VERSION_MAJOR_SHIFT	8
773 
774 static void xe_guc_exec_queue_group_cgp_update(struct xe_device *xe,
775 					       struct xe_exec_queue *q)
776 {
777 	struct xe_exec_queue_group *group = q->multi_queue.group;
778 	u32 guc_id = group->primary->guc->id;
779 
780 	/* Currently implementing CGP version 1.0 */
781 	xe_map_wr(xe, &group->cgp_bo->vmap, 0, u32,
782 		  1 << CGP_VERSION_MAJOR_SHIFT);
783 
784 	xe_map_wr(xe, &group->cgp_bo->vmap,
785 		  (32 + q->multi_queue.pos * 2) * sizeof(u32),
786 		  u32, lower_32_bits(xe_lrc_descriptor(q->lrc[0])));
787 
788 	xe_map_wr(xe, &group->cgp_bo->vmap,
789 		  (33 + q->multi_queue.pos * 2) * sizeof(u32),
790 		  u32, guc_id);
791 
792 	if (q->multi_queue.pos / 32) {
793 		xe_map_wr(xe, &group->cgp_bo->vmap, 17 * sizeof(u32),
794 			  u32, BIT(q->multi_queue.pos % 32));
795 		xe_map_wr(xe, &group->cgp_bo->vmap, 16 * sizeof(u32), u32, 0);
796 	} else {
797 		xe_map_wr(xe, &group->cgp_bo->vmap, 16 * sizeof(u32),
798 			  u32, BIT(q->multi_queue.pos));
799 		xe_map_wr(xe, &group->cgp_bo->vmap, 17 * sizeof(u32), u32, 0);
800 	}
801 }
802 
803 static void xe_guc_exec_queue_group_cgp_sync(struct xe_guc *guc,
804 					     struct xe_exec_queue *q,
805 					     const u32 *action, u32 len)
806 {
807 	struct xe_exec_queue_group *group = q->multi_queue.group;
808 	struct xe_device *xe = guc_to_xe(guc);
809 	enum xe_multi_queue_priority priority;
810 	long ret;
811 
812 	/*
813 	 * As all queues of a multi queue group use single drm scheduler
814 	 * submit workqueue, CGP synchronization with GuC are serialized.
815 	 * Hence, no locking is required here.
816 	 * Wait for any pending CGP_SYNC_DONE response before updating the
817 	 * CGP page and sending CGP_SYNC message.
818 	 *
819 	 * FIXME: Support VF migration
820 	 */
821 	ret = wait_event_timeout(guc->ct.wq,
822 				 !READ_ONCE(group->sync_pending) ||
823 				 xe_guc_read_stopped(guc), HZ);
824 	if (!ret || xe_guc_read_stopped(guc)) {
825 		/* CGP_SYNC failed. Reset gt, cleanup the group */
826 		xe_gt_warn(guc_to_gt(guc), "Wait for CGP_SYNC_DONE response failed!\n");
827 		set_exec_queue_group_banned(q);
828 		xe_gt_reset_async(q->gt);
829 		xe_guc_exec_queue_group_trigger_cleanup(q);
830 		return;
831 	}
832 
833 	scoped_guard(spinlock, &q->multi_queue.lock)
834 		priority = q->multi_queue.priority;
835 
836 	xe_lrc_set_multi_queue_priority(q->lrc[0], priority);
837 	xe_guc_exec_queue_group_cgp_update(xe, q);
838 
839 	WRITE_ONCE(group->sync_pending, true);
840 	xe_guc_ct_send(&guc->ct, action, len, G2H_LEN_DW_MULTI_QUEUE_CONTEXT, 1);
841 }
842 
843 static void guc_exec_queue_send_cgp_sync(struct xe_exec_queue *q)
844 {
845 #define MAX_MULTI_QUEUE_CGP_SYNC_SIZE	(2)
846 	struct xe_guc *guc = exec_queue_to_guc(q);
847 	struct xe_exec_queue_group *group = q->multi_queue.group;
848 	u32 action[MAX_MULTI_QUEUE_CGP_SYNC_SIZE];
849 	int len = 0;
850 
851 	action[len++] = XE_GUC_ACTION_MULTI_QUEUE_CONTEXT_CGP_SYNC;
852 	action[len++] = group->primary->guc->id;
853 
854 	xe_gt_assert(guc_to_gt(guc), len <= MAX_MULTI_QUEUE_CGP_SYNC_SIZE);
855 #undef MAX_MULTI_QUEUE_CGP_SYNC_SIZE
856 
857 	xe_guc_exec_queue_group_cgp_sync(guc, q, action, len);
858 }
859 
860 static void __register_exec_queue_group(struct xe_exec_queue *q,
861 					struct guc_ctxt_registration_info *info)
862 {
863 	struct xe_guc *guc = exec_queue_to_guc(q);
864 #define MAX_MULTI_QUEUE_REG_SIZE	(8)
865 	u32 action[MAX_MULTI_QUEUE_REG_SIZE];
866 	int len = 0;
867 
868 	action[len++] = XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_QUEUE;
869 	action[len++] = info->flags;
870 	action[len++] = info->context_idx;
871 	action[len++] = info->engine_class;
872 	action[len++] = info->engine_submit_mask;
873 	action[len++] = 0; /* Reserved */
874 	action[len++] = info->cgp_lo;
875 	action[len++] = info->cgp_hi;
876 
877 	xe_gt_assert(guc_to_gt(guc), len <= MAX_MULTI_QUEUE_REG_SIZE);
878 #undef MAX_MULTI_QUEUE_REG_SIZE
879 
880 	/*
881 	 * The above XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_QUEUE do expect a
882 	 * XE_GUC_ACTION_NOTIFY_MULTI_QUEUE_CONTEXT_CGP_SYNC_DONE response
883 	 * from guc.
884 	 */
885 	xe_guc_exec_queue_group_cgp_sync(guc, q, action, len);
886 }
887 
888 static void __register_mlrc_exec_queue(struct xe_guc *guc,
889 				       struct xe_exec_queue *q,
890 				       struct guc_ctxt_registration_info *info)
891 {
892 #define MAX_MLRC_REG_SIZE      (13 + XE_HW_ENGINE_MAX_INSTANCE * 2)
893 	u32 action[MAX_MLRC_REG_SIZE];
894 	int len = 0;
895 	int i;
896 
897 	xe_gt_assert(guc_to_gt(guc), xe_exec_queue_is_parallel(q));
898 
899 	action[len++] = XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
900 	action[len++] = info->flags;
901 	action[len++] = info->context_idx;
902 	action[len++] = info->engine_class;
903 	action[len++] = info->engine_submit_mask;
904 	action[len++] = info->wq_desc_lo;
905 	action[len++] = info->wq_desc_hi;
906 	action[len++] = info->wq_base_lo;
907 	action[len++] = info->wq_base_hi;
908 	action[len++] = info->wq_size;
909 	action[len++] = q->width;
910 	action[len++] = info->hwlrca_lo;
911 	action[len++] = info->hwlrca_hi;
912 
913 	for (i = 1; i < q->width; ++i) {
914 		struct xe_lrc *lrc = q->lrc[i];
915 
916 		action[len++] = lower_32_bits(xe_lrc_descriptor(lrc));
917 		action[len++] = upper_32_bits(xe_lrc_descriptor(lrc));
918 	}
919 
920 	/* explicitly checks some fields that we might fixup later */
921 	xe_gt_assert(guc_to_gt(guc), info->wq_desc_lo ==
922 		     action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_5_WQ_DESC_ADDR_LOWER]);
923 	xe_gt_assert(guc_to_gt(guc), info->wq_base_lo ==
924 		     action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_7_WQ_BUF_BASE_LOWER]);
925 	xe_gt_assert(guc_to_gt(guc), q->width ==
926 		     action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_10_NUM_CTXS]);
927 	xe_gt_assert(guc_to_gt(guc), info->hwlrca_lo ==
928 		     action[XE_GUC_REGISTER_CONTEXT_MULTI_LRC_DATA_11_HW_LRC_ADDR]);
929 	xe_gt_assert(guc_to_gt(guc), len <= MAX_MLRC_REG_SIZE);
930 #undef MAX_MLRC_REG_SIZE
931 
932 	xe_guc_ct_send(&guc->ct, action, len, 0, 0);
933 }
934 
935 static void __register_exec_queue(struct xe_guc *guc,
936 				  struct guc_ctxt_registration_info *info)
937 {
938 	u32 action[] = {
939 		XE_GUC_ACTION_REGISTER_CONTEXT,
940 		info->flags,
941 		info->context_idx,
942 		info->engine_class,
943 		info->engine_submit_mask,
944 		info->wq_desc_lo,
945 		info->wq_desc_hi,
946 		info->wq_base_lo,
947 		info->wq_base_hi,
948 		info->wq_size,
949 		info->hwlrca_lo,
950 		info->hwlrca_hi,
951 	};
952 
953 	/* explicitly checks some fields that we might fixup later */
954 	xe_gt_assert(guc_to_gt(guc), info->wq_desc_lo ==
955 		     action[XE_GUC_REGISTER_CONTEXT_DATA_5_WQ_DESC_ADDR_LOWER]);
956 	xe_gt_assert(guc_to_gt(guc), info->wq_base_lo ==
957 		     action[XE_GUC_REGISTER_CONTEXT_DATA_7_WQ_BUF_BASE_LOWER]);
958 	xe_gt_assert(guc_to_gt(guc), info->hwlrca_lo ==
959 		     action[XE_GUC_REGISTER_CONTEXT_DATA_10_HW_LRC_ADDR]);
960 
961 	xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
962 }
963 
964 static void register_exec_queue(struct xe_exec_queue *q, int ctx_type)
965 {
966 	struct xe_guc *guc = exec_queue_to_guc(q);
967 	struct xe_device *xe = guc_to_xe(guc);
968 	struct xe_lrc *lrc = q->lrc[0];
969 	struct guc_ctxt_registration_info info;
970 
971 	xe_gt_assert(guc_to_gt(guc), !exec_queue_registered(q));
972 	xe_gt_assert(guc_to_gt(guc), ctx_type < GUC_CONTEXT_COUNT);
973 
974 	memset(&info, 0, sizeof(info));
975 	info.context_idx = q->guc->id;
976 	info.engine_class = xe_engine_class_to_guc_class(q->class);
977 	info.engine_submit_mask = q->logical_mask;
978 	info.hwlrca_lo = lower_32_bits(xe_lrc_descriptor(lrc));
979 	info.hwlrca_hi = upper_32_bits(xe_lrc_descriptor(lrc));
980 	info.flags = CONTEXT_REGISTRATION_FLAG_KMD |
981 		FIELD_PREP(CONTEXT_REGISTRATION_FLAG_TYPE, ctx_type);
982 
983 	if (xe_exec_queue_is_multi_queue(q)) {
984 		struct xe_exec_queue_group *group = q->multi_queue.group;
985 
986 		info.cgp_lo = xe_bo_ggtt_addr(group->cgp_bo);
987 		info.cgp_hi = 0;
988 	}
989 
990 	if (xe_exec_queue_is_parallel(q)) {
991 		u64 ggtt_addr = xe_lrc_parallel_ggtt_addr(lrc);
992 		struct iosys_map map = xe_lrc_parallel_map(lrc);
993 
994 		info.wq_desc_lo = lower_32_bits(ggtt_addr +
995 			offsetof(struct guc_submit_parallel_scratch, wq_desc));
996 		info.wq_desc_hi = upper_32_bits(ggtt_addr +
997 			offsetof(struct guc_submit_parallel_scratch, wq_desc));
998 		info.wq_base_lo = lower_32_bits(ggtt_addr +
999 			offsetof(struct guc_submit_parallel_scratch, wq[0]));
1000 		info.wq_base_hi = upper_32_bits(ggtt_addr +
1001 			offsetof(struct guc_submit_parallel_scratch, wq[0]));
1002 		info.wq_size = WQ_SIZE;
1003 
1004 		q->guc->wqi_head = 0;
1005 		q->guc->wqi_tail = 0;
1006 		xe_map_memset(xe, &map, 0, 0, PARALLEL_SCRATCH_SIZE - WQ_SIZE);
1007 		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
1008 	}
1009 
1010 	set_exec_queue_registered(q);
1011 	trace_xe_exec_queue_register(q);
1012 	if (xe_exec_queue_is_multi_queue_primary(q))
1013 		__register_exec_queue_group(q, &info);
1014 	else if (xe_exec_queue_is_parallel(q))
1015 		__register_mlrc_exec_queue(guc, q, &info);
1016 	else if (!xe_exec_queue_is_multi_queue_secondary(q))
1017 		__register_exec_queue(guc, &info);
1018 
1019 	if (!xe_exec_queue_is_multi_queue_secondary(q))
1020 		init_policies(guc, q);
1021 
1022 	if (xe_exec_queue_is_multi_queue_secondary(q))
1023 		guc_exec_queue_send_cgp_sync(q);
1024 }
1025 
1026 static u32 wq_space_until_wrap(struct xe_exec_queue *q)
1027 {
1028 	return (WQ_SIZE - q->guc->wqi_tail);
1029 }
1030 
1031 static int wq_wait_for_space(struct xe_exec_queue *q, u32 wqi_size)
1032 {
1033 	struct xe_guc *guc = exec_queue_to_guc(q);
1034 	struct xe_device *xe = guc_to_xe(guc);
1035 	struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
1036 	unsigned int sleep_period_ms = 1, sleep_total_ms = 0;
1037 
1038 #define AVAILABLE_SPACE \
1039 	CIRC_SPACE(q->guc->wqi_tail, q->guc->wqi_head, WQ_SIZE)
1040 	if (wqi_size > AVAILABLE_SPACE && !vf_recovery(guc)) {
1041 try_again:
1042 		q->guc->wqi_head = parallel_read(xe, map, wq_desc.head);
1043 		if (wqi_size > AVAILABLE_SPACE && !vf_recovery(guc)) {
1044 			if (sleep_total_ms > 2000) {
1045 				xe_gt_reset_async(q->gt);
1046 				return -ENODEV;
1047 			}
1048 
1049 			sleep_total_ms += xe_sleep_exponential_ms(&sleep_period_ms, 64);
1050 			goto try_again;
1051 		}
1052 	}
1053 #undef AVAILABLE_SPACE
1054 
1055 	return 0;
1056 }
1057 
1058 static int wq_noop_append(struct xe_exec_queue *q)
1059 {
1060 	struct xe_guc *guc = exec_queue_to_guc(q);
1061 	struct xe_device *xe = guc_to_xe(guc);
1062 	struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
1063 	u32 len_dw = wq_space_until_wrap(q) / sizeof(u32) - 1;
1064 
1065 	if (wq_wait_for_space(q, wq_space_until_wrap(q)))
1066 		return -ENODEV;
1067 
1068 	xe_gt_assert(guc_to_gt(guc), FIELD_FIT(WQ_LEN_MASK, len_dw));
1069 
1070 	parallel_write(xe, map, wq[q->guc->wqi_tail / sizeof(u32)],
1071 		       FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
1072 		       FIELD_PREP(WQ_LEN_MASK, len_dw));
1073 	q->guc->wqi_tail = 0;
1074 
1075 	return 0;
1076 }
1077 
1078 static void wq_item_append(struct xe_exec_queue *q)
1079 {
1080 	struct xe_guc *guc = exec_queue_to_guc(q);
1081 	struct xe_device *xe = guc_to_xe(guc);
1082 	struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
1083 #define WQ_HEADER_SIZE	4	/* Includes 1 LRC address too */
1084 	u32 wqi[XE_HW_ENGINE_MAX_INSTANCE + (WQ_HEADER_SIZE - 1)];
1085 	u32 wqi_size = (q->width + (WQ_HEADER_SIZE - 1)) * sizeof(u32);
1086 	u32 len_dw = (wqi_size / sizeof(u32)) - 1;
1087 	int i = 0, j;
1088 
1089 	if (wqi_size > wq_space_until_wrap(q)) {
1090 		if (wq_noop_append(q))
1091 			return;
1092 	}
1093 	if (wq_wait_for_space(q, wqi_size))
1094 		return;
1095 
1096 	wqi[i++] = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) |
1097 		FIELD_PREP(WQ_LEN_MASK, len_dw);
1098 	wqi[i++] = xe_lrc_descriptor(q->lrc[0]);
1099 	wqi[i++] = FIELD_PREP(WQ_GUC_ID_MASK, q->guc->id) |
1100 		FIELD_PREP(WQ_RING_TAIL_MASK, q->lrc[0]->ring.tail / sizeof(u64));
1101 	wqi[i++] = 0;
1102 	for (j = 1; j < q->width; ++j) {
1103 		struct xe_lrc *lrc = q->lrc[j];
1104 
1105 		wqi[i++] = lrc->ring.tail / sizeof(u64);
1106 	}
1107 
1108 	xe_gt_assert(guc_to_gt(guc), i == wqi_size / sizeof(u32));
1109 
1110 	iosys_map_incr(&map, offsetof(struct guc_submit_parallel_scratch,
1111 				      wq[q->guc->wqi_tail / sizeof(u32)]));
1112 	xe_map_memcpy_to(xe, &map, 0, wqi, wqi_size);
1113 	q->guc->wqi_tail += wqi_size;
1114 	xe_gt_assert(guc_to_gt(guc), q->guc->wqi_tail <= WQ_SIZE);
1115 
1116 	xe_device_wmb(xe);
1117 
1118 	map = xe_lrc_parallel_map(q->lrc[0]);
1119 	parallel_write(xe, map, wq_desc.tail, q->guc->wqi_tail);
1120 }
1121 
1122 #define RESUME_PENDING	~0x0ull
1123 static void submit_exec_queue(struct xe_exec_queue *q, struct xe_sched_job *job)
1124 {
1125 	struct xe_guc *guc = exec_queue_to_guc(q);
1126 	struct xe_lrc *lrc = q->lrc[0];
1127 	u32 action[3];
1128 	u32 g2h_len = 0;
1129 	u32 num_g2h = 0;
1130 	int len = 0;
1131 	bool extra_submit = false;
1132 
1133 	xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
1134 
1135 	if (!job->restore_replay || job->last_replay) {
1136 		if (xe_exec_queue_is_parallel(q))
1137 			wq_item_append(q);
1138 		else
1139 			xe_lrc_set_ring_tail(lrc, lrc->ring.tail);
1140 		job->last_replay = false;
1141 	}
1142 
1143 	if (exec_queue_suspended(q) && !xe_exec_queue_is_parallel(q))
1144 		return;
1145 
1146 	/*
1147 	 * All queues in a multi-queue group will use the primary queue
1148 	 * of the group to interface with GuC. If primay is suspended,
1149 	 * just return. Jobs will get scheduled once primary is resumed.
1150 	 */
1151 	q = xe_exec_queue_multi_queue_primary(q);
1152 	if (exec_queue_suspended(q))
1153 		return;
1154 
1155 	if (!exec_queue_enabled(q)) {
1156 		action[len++] = XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
1157 		action[len++] = q->guc->id;
1158 		action[len++] = GUC_CONTEXT_ENABLE;
1159 		g2h_len = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
1160 		num_g2h = 1;
1161 		if (xe_exec_queue_is_parallel(q))
1162 			extra_submit = true;
1163 
1164 		q->guc->resume_time = RESUME_PENDING;
1165 		set_exec_queue_pending_enable(q);
1166 		set_exec_queue_enabled(q);
1167 		trace_xe_exec_queue_scheduling_enable(q);
1168 	} else {
1169 		action[len++] = XE_GUC_ACTION_SCHED_CONTEXT;
1170 		action[len++] = q->guc->id;
1171 		trace_xe_exec_queue_submit(q);
1172 	}
1173 
1174 	xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
1175 
1176 	if (extra_submit) {
1177 		len = 0;
1178 		action[len++] = XE_GUC_ACTION_SCHED_CONTEXT;
1179 		action[len++] = q->guc->id;
1180 		trace_xe_exec_queue_submit(q);
1181 
1182 		xe_guc_ct_send(&guc->ct, action, len, 0, 0);
1183 	}
1184 }
1185 
1186 static struct dma_fence *
1187 guc_exec_queue_run_job(struct drm_sched_job *drm_job)
1188 {
1189 	struct xe_sched_job *job = to_xe_sched_job(drm_job);
1190 	struct xe_exec_queue *q = job->q;
1191 	struct xe_guc *guc = exec_queue_to_guc(q);
1192 	bool killed_or_banned_or_wedged =
1193 		exec_queue_killed_or_banned_or_wedged(q);
1194 
1195 	xe_gt_assert(guc_to_gt(guc), !(exec_queue_destroyed(q) || exec_queue_pending_disable(q)) ||
1196 		     exec_queue_banned(q) || exec_queue_suspended(q));
1197 
1198 	trace_xe_sched_job_run(job);
1199 
1200 	if (!killed_or_banned_or_wedged && !xe_sched_job_is_error(job)) {
1201 		if (xe_exec_queue_is_multi_queue_secondary(q)) {
1202 			struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
1203 
1204 			if (exec_queue_killed_or_banned_or_wedged(primary))
1205 				goto run_job_out;
1206 
1207 			if (!exec_queue_registered(primary))
1208 				register_exec_queue(primary, GUC_CONTEXT_NORMAL);
1209 		}
1210 
1211 		if (!exec_queue_registered(q))
1212 			register_exec_queue(q, GUC_CONTEXT_NORMAL);
1213 		if (!job->restore_replay)
1214 			q->ring_ops->emit_job(job);
1215 		submit_exec_queue(q, job);
1216 		job->restore_replay = false;
1217 	}
1218 
1219 run_job_out:
1220 
1221 	return job->fence;
1222 }
1223 
1224 static void guc_exec_queue_free_job(struct drm_sched_job *drm_job)
1225 {
1226 	struct xe_sched_job *job = to_xe_sched_job(drm_job);
1227 
1228 	trace_xe_sched_job_free(job);
1229 	xe_sched_job_put(job);
1230 }
1231 
1232 int xe_guc_read_stopped(struct xe_guc *guc)
1233 {
1234 	return atomic_read(&guc->submission_state.stopped);
1235 }
1236 
1237 static void handle_multi_queue_secondary_sched_done(struct xe_guc *guc,
1238 						    struct xe_exec_queue *q,
1239 						    u32 runnable_state);
1240 static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q);
1241 
1242 #define MAKE_SCHED_CONTEXT_ACTION(q, enable_disable)			\
1243 	u32 action[] = {						\
1244 		XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET,			\
1245 		q->guc->id,						\
1246 		GUC_CONTEXT_##enable_disable,				\
1247 	}
1248 
1249 static void disable_scheduling_deregister(struct xe_guc *guc,
1250 					  struct xe_exec_queue *q)
1251 {
1252 	MAKE_SCHED_CONTEXT_ACTION(q, DISABLE);
1253 	int ret;
1254 
1255 	if (!xe_exec_queue_is_multi_queue_secondary(q))
1256 		set_min_preemption_timeout(guc, q);
1257 
1258 	smp_rmb();
1259 	ret = wait_event_timeout(guc->ct.wq,
1260 				 (!exec_queue_pending_enable(q) &&
1261 				  !exec_queue_pending_disable(q)) ||
1262 					 xe_guc_read_stopped(guc) ||
1263 					 vf_recovery(guc),
1264 				 HZ * 5);
1265 	if (!ret && !vf_recovery(guc)) {
1266 		struct xe_gpu_scheduler *sched = &q->guc->sched;
1267 
1268 		xe_gt_warn(q->gt, "Pending enable/disable failed to respond\n");
1269 		xe_sched_submission_start(sched);
1270 		xe_gt_reset_async(q->gt);
1271 		xe_sched_tdr_queue_imm(sched);
1272 		return;
1273 	}
1274 
1275 	clear_exec_queue_enabled(q);
1276 	set_exec_queue_pending_disable(q);
1277 	set_exec_queue_destroyed(q);
1278 	trace_xe_exec_queue_scheduling_disable(q);
1279 
1280 	/*
1281 	 * Reserve space for both G2H here as the 2nd G2H is sent from a G2H
1282 	 * handler and we are not allowed to reserved G2H space in handlers.
1283 	 */
1284 	if (xe_exec_queue_is_multi_queue_secondary(q))
1285 		handle_multi_queue_secondary_sched_done(guc, q, 0);
1286 	else
1287 		xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
1288 			       G2H_LEN_DW_SCHED_CONTEXT_MODE_SET +
1289 			       G2H_LEN_DW_DEREGISTER_CONTEXT, 2);
1290 }
1291 
1292 /**
1293  * xe_guc_submit_wedge() - Wedge GuC submission
1294  * @guc: the GuC object
1295  *
1296  * Save exec queue's registered with GuC state by taking a ref to each queue.
1297  * Register a DRMM handler to drop refs upon driver unload.
1298  */
1299 void xe_guc_submit_wedge(struct xe_guc *guc)
1300 {
1301 	struct xe_device *xe = guc_to_xe(guc);
1302 	struct xe_exec_queue *q;
1303 	unsigned long index;
1304 
1305 	xe_gt_assert(guc_to_gt(guc), guc_to_xe(guc)->wedged.mode);
1306 
1307 	/*
1308 	 * If device is being wedged even before submission_state is
1309 	 * initialized, there's nothing to do here.
1310 	 */
1311 	if (!guc->submission_state.initialized)
1312 		return;
1313 
1314 	if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) {
1315 		mutex_lock(&guc->submission_state.lock);
1316 		xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
1317 			if (xe_exec_queue_get_unless_zero(q))
1318 				set_exec_queue_wedged(q);
1319 		mutex_unlock(&guc->submission_state.lock);
1320 	} else {
1321 		/* Forcefully kill any remaining exec queues, signal fences */
1322 		guc_submit_reset_prepare(guc);
1323 		xe_guc_submit_stop(guc);
1324 		xe_guc_softreset(guc);
1325 		xe_uc_fw_sanitize(&guc->fw);
1326 		xe_guc_submit_pause_abort(guc);
1327 	}
1328 }
1329 
1330 static bool guc_submit_hint_wedged(struct xe_guc *guc)
1331 {
1332 	struct xe_device *xe = guc_to_xe(guc);
1333 
1334 	if (xe->wedged.mode != XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET)
1335 		return false;
1336 
1337 	if (xe_device_wedged(xe))
1338 		return true;
1339 
1340 	xe_device_declare_wedged(xe);
1341 
1342 	return true;
1343 }
1344 
1345 #define ADJUST_FIVE_PERCENT(__t)	mul_u64_u32_div(__t, 105, 100)
1346 
1347 static bool check_timeout(struct xe_exec_queue *q, struct xe_sched_job *job)
1348 {
1349 	struct xe_gt *gt = guc_to_gt(exec_queue_to_guc(q));
1350 	u32 ctx_timestamp, ctx_job_timestamp;
1351 	u32 timeout_ms = q->sched_props.job_timeout_ms;
1352 	u32 diff;
1353 	u64 running_time_ms;
1354 
1355 	if (!xe_sched_job_started(job)) {
1356 		xe_gt_warn(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, not started",
1357 			   xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
1358 			   q->guc->id);
1359 
1360 		/* GuC never scheduled this job - let the caller trigger a GT reset. */
1361 		return true;
1362 	}
1363 
1364 	ctx_timestamp = lower_32_bits(xe_lrc_timestamp(q->lrc[0]));
1365 	if (ctx_timestamp == job->sample_timestamp) {
1366 		if (IS_SRIOV_VF(gt_to_xe(gt)))
1367 			xe_gt_notice(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, timestamp stuck",
1368 				     xe_sched_job_seqno(job),
1369 				     xe_sched_job_lrc_seqno(job), q->guc->id);
1370 		else
1371 			xe_gt_warn(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, timestamp stuck",
1372 				   xe_sched_job_seqno(job),
1373 				   xe_sched_job_lrc_seqno(job), q->guc->id);
1374 
1375 		return xe_sched_invalidate_job(job, 0);
1376 	}
1377 
1378 	job->sample_timestamp = ctx_timestamp;
1379 	ctx_job_timestamp = xe_lrc_ctx_job_timestamp(q->lrc[0]);
1380 
1381 	/*
1382 	 * Counter wraps at ~223s at the usual 19.2MHz, be paranoid catch
1383 	 * possible overflows with a high timeout.
1384 	 */
1385 	xe_gt_assert(gt, timeout_ms < 100 * MSEC_PER_SEC);
1386 
1387 	diff = ctx_timestamp - ctx_job_timestamp;
1388 
1389 	/*
1390 	 * Ensure timeout is within 5% to account for an GuC scheduling latency
1391 	 */
1392 	running_time_ms =
1393 		ADJUST_FIVE_PERCENT(xe_gt_clock_interval_to_ms(gt, diff));
1394 
1395 	xe_gt_dbg(gt,
1396 		  "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, running_time_ms=%llu, timeout_ms=%u, diff=0x%08x",
1397 		  xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
1398 		  q->guc->id, running_time_ms, timeout_ms, diff);
1399 
1400 	return running_time_ms >= timeout_ms;
1401 }
1402 
1403 static void enable_scheduling(struct xe_exec_queue *q)
1404 {
1405 	MAKE_SCHED_CONTEXT_ACTION(q, ENABLE);
1406 	struct xe_guc *guc = exec_queue_to_guc(q);
1407 	int ret;
1408 
1409 	xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
1410 	xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
1411 	xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
1412 	xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q));
1413 
1414 	set_exec_queue_pending_enable(q);
1415 	set_exec_queue_enabled(q);
1416 	trace_xe_exec_queue_scheduling_enable(q);
1417 
1418 	if (xe_exec_queue_is_multi_queue_secondary(q))
1419 		handle_multi_queue_secondary_sched_done(guc, q, 1);
1420 	else
1421 		xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
1422 			       G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
1423 
1424 	ret = wait_event_timeout(guc->ct.wq,
1425 				 !exec_queue_pending_enable(q) ||
1426 				 xe_guc_read_stopped(guc) ||
1427 				 vf_recovery(guc), HZ * 5);
1428 	if ((!ret && !vf_recovery(guc)) || xe_guc_read_stopped(guc)) {
1429 		xe_gt_warn(guc_to_gt(guc), "Schedule enable failed to respond");
1430 		set_exec_queue_banned(q);
1431 		xe_gt_reset_async(q->gt);
1432 		xe_sched_tdr_queue_imm(&q->guc->sched);
1433 	}
1434 }
1435 
1436 static void disable_scheduling(struct xe_exec_queue *q, bool immediate)
1437 {
1438 	MAKE_SCHED_CONTEXT_ACTION(q, DISABLE);
1439 	struct xe_guc *guc = exec_queue_to_guc(q);
1440 
1441 	xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
1442 	xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
1443 	xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
1444 
1445 	if (immediate && !xe_exec_queue_is_multi_queue_secondary(q))
1446 		set_min_preemption_timeout(guc, q);
1447 	clear_exec_queue_enabled(q);
1448 	set_exec_queue_pending_disable(q);
1449 	trace_xe_exec_queue_scheduling_disable(q);
1450 
1451 	if (xe_exec_queue_is_multi_queue_secondary(q))
1452 		handle_multi_queue_secondary_sched_done(guc, q, 0);
1453 	else
1454 		xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
1455 			       G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
1456 }
1457 
1458 /*
1459  * Recover via GT reset for a kernel queue, or for a GuC scheduling failure (job
1460  * never started) on a queue that was not already killed or banned. An already
1461  * banned queue must stay banned, so its unstarted jobs do not clear the ban or
1462  * trigger a reset.
1463  */
1464 static bool timeout_needs_gt_reset(struct xe_exec_queue *q, struct xe_sched_job *job,
1465 				   bool skip_timeout_check)
1466 {
1467 	if (q->flags & EXEC_QUEUE_FLAG_KERNEL)
1468 		return true;
1469 
1470 	return !skip_timeout_check && !xe_sched_job_started(job);
1471 }
1472 
1473 static enum drm_gpu_sched_stat
1474 guc_exec_queue_timedout_job(struct drm_sched_job *drm_job)
1475 {
1476 	struct xe_sched_job *job = to_xe_sched_job(drm_job);
1477 	struct drm_sched_job *tmp_job;
1478 	struct xe_exec_queue *q = job->q, *primary;
1479 	struct xe_gpu_scheduler *sched = &q->guc->sched;
1480 	struct xe_guc *guc = exec_queue_to_guc(q);
1481 	const char *process_name = "no process";
1482 	struct xe_device *xe = guc_to_xe(guc);
1483 	int err = -ETIME;
1484 	pid_t pid = -1;
1485 	bool wedged = false, wedge_device = false, skip_timeout_check;
1486 
1487 	xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
1488 
1489 	primary = xe_exec_queue_multi_queue_primary(q);
1490 
1491 	/*
1492 	 * TDR has fired before free job worker. Common if exec queue
1493 	 * immediately closed after last fence signaled. Add back to pending
1494 	 * list so job can be freed and kick scheduler ensuring free job is not
1495 	 * lost.
1496 	 */
1497 	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &job->fence->flags) ||
1498 	    vf_recovery(guc))
1499 		return DRM_GPU_SCHED_STAT_NO_HANG;
1500 
1501 	/* Kill the run_job entry point */
1502 	if (xe_exec_queue_is_multi_queue(q))
1503 		xe_guc_exec_queue_group_stop(q);
1504 	else
1505 		xe_sched_submission_stop(sched);
1506 
1507 	/* Must check all state after stopping scheduler */
1508 	skip_timeout_check = exec_queue_reset(q) ||
1509 		exec_queue_killed_or_banned_or_wedged(q);
1510 
1511 	/* Skip timeout check if multi-queue group is banned */
1512 	if (xe_exec_queue_is_multi_queue(q) &&
1513 	    READ_ONCE(q->multi_queue.group->banned))
1514 		skip_timeout_check = true;
1515 
1516 	/* LR jobs can only get here if queue has been killed or hit an error */
1517 	if (xe_exec_queue_is_lr(q))
1518 		xe_gt_assert(guc_to_gt(guc), skip_timeout_check);
1519 
1520 	/*
1521 	 * If devcoredump not captured and GuC capture for the job is not ready
1522 	 * do manual capture first and decide later if we need to use it
1523 	 */
1524 	if (!exec_queue_killed(q) && !xe->devcoredump.captured &&
1525 	    !xe_guc_capture_get_matching_and_lock(q)) {
1526 		/* take force wake before engine register manual capture */
1527 		CLASS(xe_force_wake, fw_ref)(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
1528 		if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL))
1529 			xe_gt_info(q->gt, "failed to get forcewake for coredump capture\n");
1530 
1531 		xe_engine_snapshot_capture_for_queue(q);
1532 	}
1533 
1534 	/*
1535 	 * Check if job is actually timed out, if so restart job execution and TDR
1536 	 */
1537 	if (!skip_timeout_check && !check_timeout(q, job))
1538 		goto rearm;
1539 
1540 	if (!exec_queue_killed(q))
1541 		wedged = guc_submit_hint_wedged(exec_queue_to_guc(q));
1542 
1543 	set_exec_queue_banned(q);
1544 
1545 	/* Kick job / queue off hardware */
1546 	if (!wedged && (exec_queue_enabled(primary) ||
1547 			exec_queue_pending_disable(primary))) {
1548 		int ret;
1549 
1550 		if (exec_queue_reset(primary))
1551 			err = -EIO;
1552 
1553 		if (xe_uc_fw_is_running(&guc->fw)) {
1554 			/*
1555 			 * Wait for any pending G2H to flush out before
1556 			 * modifying state
1557 			 */
1558 			ret = wait_event_timeout(guc->ct.wq,
1559 						 (!exec_queue_pending_enable(primary) &&
1560 						  !exec_queue_pending_disable(primary)) ||
1561 						 xe_guc_read_stopped(guc) ||
1562 						 vf_recovery(guc), HZ * 5);
1563 			if (vf_recovery(guc))
1564 				goto handle_vf_resume;
1565 			if (!ret || xe_guc_read_stopped(guc))
1566 				goto trigger_reset;
1567 
1568 			disable_scheduling(primary, skip_timeout_check);
1569 		}
1570 
1571 		/*
1572 		 * Must wait for scheduling to be disabled before signalling
1573 		 * any fences, if GT broken the GT reset code should signal us.
1574 		 *
1575 		 * FIXME: Tests can generate a ton of 0x6000 (IOMMU CAT fault
1576 		 * error) messages which can cause the schedule disable to get
1577 		 * lost. If this occurs, trigger a GT reset to recover.
1578 		 */
1579 		smp_rmb();
1580 		ret = wait_event_timeout(guc->ct.wq,
1581 					 !xe_uc_fw_is_running(&guc->fw) ||
1582 					 !exec_queue_pending_disable(primary) ||
1583 					 xe_guc_read_stopped(guc) ||
1584 					 vf_recovery(guc), HZ * 5);
1585 		if (vf_recovery(guc))
1586 			goto handle_vf_resume;
1587 		if (!ret || xe_guc_read_stopped(guc)) {
1588 trigger_reset:
1589 			if (!ret)
1590 				xe_gt_warn(guc_to_gt(guc),
1591 					   "Schedule disable failed to respond, guc_id=%d",
1592 					   primary->guc->id);
1593 			xe_devcoredump(primary, job,
1594 				       "Schedule disable failed to respond, guc_id=%d, ret=%d, guc_read=%d",
1595 				       primary->guc->id, ret, xe_guc_read_stopped(guc));
1596 			xe_gt_reset_async(primary->gt);
1597 			xe_sched_tdr_queue_imm(sched);
1598 			goto rearm;
1599 		}
1600 	}
1601 
1602 	if (q->vm && q->vm->xef) {
1603 		process_name = q->vm->xef->process_name;
1604 		pid = q->vm->xef->pid;
1605 	}
1606 
1607 	if (!exec_queue_killed(q))
1608 		xe_gt_notice(guc_to_gt(guc),
1609 			     "Timedout job: seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx in %s [%d]",
1610 			     xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
1611 			     q->guc->id, q->flags, process_name, pid);
1612 
1613 	trace_xe_sched_job_timedout(job);
1614 
1615 	if (!exec_queue_killed(q))
1616 		xe_devcoredump(q, job,
1617 			       "Timedout job - seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx",
1618 			       xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
1619 			       q->guc->id, q->flags);
1620 
1621 	if (!wedged) {
1622 		if (timeout_needs_gt_reset(q, job, skip_timeout_check)) {
1623 			if (!xe_sched_invalidate_job(job, 2)) {
1624 				clear_exec_queue_banned(q);
1625 				xe_gt_reset_async(q->gt);
1626 				goto rearm;
1627 			}
1628 			if (q->flags & EXEC_QUEUE_FLAG_KERNEL) {
1629 				xe_gt_WARN(q->gt, true, "Kernel-submitted job timed out\n");
1630 				wedge_device = true;
1631 			}
1632 		} else if (q->flags & EXEC_QUEUE_FLAG_VM && !exec_queue_killed(q)) {
1633 			xe_gt_WARN(q->gt, true, "VM job timed out on non-killed execqueue\n");
1634 		}
1635 	}
1636 
1637 	/* Mark all outstanding jobs as bad, thus completing them */
1638 	xe_sched_job_set_error(job, err);
1639 	drm_sched_for_each_pending_job(tmp_job, &sched->base, NULL)
1640 		xe_sched_job_set_error(to_xe_sched_job(tmp_job), -ECANCELED);
1641 
1642 	if (xe_exec_queue_is_multi_queue(q)) {
1643 		xe_guc_exec_queue_group_start(q);
1644 		xe_guc_exec_queue_group_trigger_cleanup(q);
1645 	} else {
1646 		xe_sched_submission_start(sched);
1647 		xe_guc_exec_queue_trigger_cleanup(q);
1648 	}
1649 
1650 	if (wedge_device)
1651 		xe_device_declare_wedged(gt_to_xe(q->gt));
1652 
1653 	/*
1654 	 * We want the job added back to the pending list so it gets freed; this
1655 	 * is what DRM_GPU_SCHED_STAT_NO_HANG does.
1656 	 */
1657 	return DRM_GPU_SCHED_STAT_NO_HANG;
1658 
1659 rearm:
1660 	/*
1661 	 * XXX: Ideally want to adjust timeout based on current execution time
1662 	 * but there is not currently an easy way to do in DRM scheduler. With
1663 	 * some thought, do this in a follow up.
1664 	 */
1665 	if (xe_exec_queue_is_multi_queue(q))
1666 		xe_guc_exec_queue_group_start(q);
1667 	else
1668 		xe_sched_submission_start(sched);
1669 handle_vf_resume:
1670 	return DRM_GPU_SCHED_STAT_NO_HANG;
1671 }
1672 
1673 static void guc_exec_queue_fini(struct xe_exec_queue *q)
1674 {
1675 	struct xe_guc_exec_queue *ge = q->guc;
1676 	struct xe_guc *guc = exec_queue_to_guc(q);
1677 	struct drm_device *drm = &guc_to_xe(guc)->drm;
1678 
1679 	if (xe_exec_queue_is_multi_queue_secondary(q)) {
1680 		struct xe_exec_queue_group *group = q->multi_queue.group;
1681 
1682 		mutex_lock(&group->list_lock);
1683 		list_del(&q->multi_queue.link);
1684 		mutex_unlock(&group->list_lock);
1685 	}
1686 
1687 	release_guc_id(guc, q);
1688 	xe_sched_entity_fini(&ge->entity);
1689 	xe_sched_fini(&ge->sched);
1690 
1691 	/*
1692 	 * RCU free due sched being exported via DRM scheduler fences
1693 	 * (timeline name).
1694 	 */
1695 	kfree_rcu(ge, rcu);
1696 
1697 	drm_dev_put(drm);
1698 }
1699 
1700 static void guc_exec_queue_do_destroy(struct xe_exec_queue *q)
1701 {
1702 	struct xe_guc_exec_queue *ge = q->guc;
1703 	struct xe_guc *guc = exec_queue_to_guc(q);
1704 	struct xe_device *xe = guc_to_xe(guc);
1705 	struct drm_device *drm = &xe->drm;
1706 
1707 	/*
1708 	 * guc_exec_queue_fini() drops the queue's drm_device ref.
1709 	 * Keep the device alive until the PM-runtime guard unwinds.
1710 	 */
1711 	drm_dev_get(drm);
1712 
1713 	scoped_guard(xe_pm_runtime, xe) {
1714 		trace_xe_exec_queue_destroy(q);
1715 
1716 		/* Confirm no work left behind accessing device structures */
1717 		cancel_delayed_work_sync(&ge->sched.base.work_tdr);
1718 
1719 		xe_exec_queue_fini(q);
1720 	}
1721 
1722 	drm_dev_put(drm);
1723 }
1724 
1725 static void __guc_exec_queue_destroy_async(struct work_struct *w)
1726 {
1727 	struct xe_guc_exec_queue *ge =
1728 		container_of(w, struct xe_guc_exec_queue, destroy_async);
1729 
1730 	guc_exec_queue_do_destroy(ge->q);
1731 }
1732 
1733 static void guc_exec_queue_destroy_async(struct xe_exec_queue *q)
1734 {
1735 	INIT_WORK(&q->guc->destroy_async, __guc_exec_queue_destroy_async);
1736 
1737 	/* We must block on kernel engines so slabs are empty on driver unload */
1738 	if (q->flags & EXEC_QUEUE_FLAG_PERMANENT || exec_queue_wedged(q))
1739 		guc_exec_queue_do_destroy(q);
1740 	else
1741 		xe_destroy_wq_queue(&q->guc->destroy_async);
1742 }
1743 
1744 static void __guc_exec_queue_destroy(struct xe_guc *guc, struct xe_exec_queue *q)
1745 {
1746 	/*
1747 	 * Might be done from within the GPU scheduler, need to do async as we
1748 	 * fini the scheduler when the engine is fini'd, the scheduler can't
1749 	 * complete fini within itself (circular dependency). Async resolves
1750 	 * this we and don't really care when everything is fini'd, just that it
1751 	 * is.
1752 	 */
1753 	guc_exec_queue_destroy_async(q);
1754 }
1755 
1756 static void __guc_exec_queue_process_msg_cleanup(struct xe_sched_msg *msg)
1757 {
1758 	struct xe_exec_queue *q = msg->private_data;
1759 	struct xe_guc *guc = exec_queue_to_guc(q);
1760 
1761 	xe_gt_assert(guc_to_gt(guc), !(q->flags & EXEC_QUEUE_FLAG_PERMANENT));
1762 	trace_xe_exec_queue_cleanup_entity(q);
1763 
1764 	/*
1765 	 * Expected state transitions for cleanup:
1766 	 * - If the exec queue is registered and GuC firmware is running, we must first
1767 	 *   disable scheduling and deregister the queue to ensure proper teardown and
1768 	 *   resource release in the GuC, then destroy the exec queue on driver side.
1769 	 * - If the GuC is already stopped (e.g., during driver unload or GPU reset),
1770 	 *   we cannot expect a response for the deregister request. In this case,
1771 	 *   it is safe to directly destroy the exec queue on driver side, as the GuC
1772 	 *   will not process further requests and all resources must be cleaned up locally.
1773 	 */
1774 	if (exec_queue_registered(q) && xe_uc_fw_is_running(&guc->fw))
1775 		disable_scheduling_deregister(guc, q);
1776 	else
1777 		__guc_exec_queue_destroy(guc, q);
1778 }
1779 
1780 static bool guc_exec_queue_allowed_to_change_state(struct xe_exec_queue *q)
1781 {
1782 	return !exec_queue_killed_or_banned_or_wedged(q) && exec_queue_registered(q);
1783 }
1784 
1785 static void __guc_exec_queue_process_msg_set_sched_props(struct xe_sched_msg *msg)
1786 {
1787 	struct xe_exec_queue *q = msg->private_data;
1788 	struct xe_guc *guc = exec_queue_to_guc(q);
1789 
1790 	if (guc_exec_queue_allowed_to_change_state(q))
1791 		init_policies(guc, q);
1792 	kfree(msg);
1793 }
1794 
1795 static void __suspend_fence_signal(struct xe_exec_queue *q)
1796 {
1797 	struct xe_guc *guc = exec_queue_to_guc(q);
1798 	struct xe_device *xe = guc_to_xe(guc);
1799 
1800 	if (!q->guc->suspend_pending)
1801 		return;
1802 
1803 	WRITE_ONCE(q->guc->suspend_pending, false);
1804 
1805 	/*
1806 	 * We use a GuC shared wait queue for VFs because the VF resfix start
1807 	 * interrupt must be able to wake all instances of suspend_wait. This
1808 	 * prevents the VF migration worker from being starved during
1809 	 * scheduling.
1810 	 */
1811 	if (IS_SRIOV_VF(xe))
1812 		wake_up_all(&guc->ct.wq);
1813 	else
1814 		wake_up(&q->guc->suspend_wait);
1815 }
1816 
1817 static void suspend_fence_signal(struct xe_exec_queue *q)
1818 {
1819 	struct xe_guc *guc = exec_queue_to_guc(q);
1820 
1821 	xe_gt_assert(guc_to_gt(guc), exec_queue_suspended(q) || exec_queue_killed(q) ||
1822 		     xe_guc_read_stopped(guc));
1823 	xe_gt_assert(guc_to_gt(guc), q->guc->suspend_pending);
1824 
1825 	__suspend_fence_signal(q);
1826 }
1827 
1828 static void __guc_exec_queue_process_msg_suspend(struct xe_sched_msg *msg)
1829 {
1830 	struct xe_exec_queue *q = msg->private_data;
1831 	struct xe_guc *guc = exec_queue_to_guc(q);
1832 
1833 	if (guc_exec_queue_allowed_to_change_state(q) && !exec_queue_suspended(q) &&
1834 	    exec_queue_enabled(q)) {
1835 		wait_event(guc->ct.wq, vf_recovery(guc) ||
1836 			   ((q->guc->resume_time != RESUME_PENDING ||
1837 			   xe_guc_read_stopped(guc)) && !exec_queue_pending_disable(q)));
1838 
1839 		if (!xe_guc_read_stopped(guc)) {
1840 			s64 since_resume_ms =
1841 				ktime_ms_delta(ktime_get(),
1842 					       q->guc->resume_time);
1843 			s64 wait_ms = q->vm->preempt.min_run_period_ms -
1844 				since_resume_ms;
1845 
1846 			if (wait_ms > 0 && q->guc->resume_time)
1847 				xe_sleep_relaxed_ms(wait_ms);
1848 
1849 			set_exec_queue_suspended(q);
1850 			disable_scheduling(q, false);
1851 		}
1852 	} else if (q->guc->suspend_pending) {
1853 		set_exec_queue_suspended(q);
1854 		suspend_fence_signal(q);
1855 	}
1856 }
1857 
1858 static void __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
1859 {
1860 	struct xe_exec_queue *q = msg->private_data;
1861 
1862 	if (guc_exec_queue_allowed_to_change_state(q)) {
1863 		clear_exec_queue_suspended(q);
1864 		if (!exec_queue_enabled(q)) {
1865 			q->guc->resume_time = RESUME_PENDING;
1866 			set_exec_queue_pending_resume(q);
1867 			enable_scheduling(q);
1868 		}
1869 	} else {
1870 		clear_exec_queue_suspended(q);
1871 	}
1872 }
1873 
1874 static void __guc_exec_queue_process_msg_set_multi_queue_priority(struct xe_sched_msg *msg)
1875 {
1876 	struct xe_exec_queue *q = msg->private_data;
1877 
1878 	if (guc_exec_queue_allowed_to_change_state(q))
1879 		guc_exec_queue_send_cgp_sync(q);
1880 
1881 	kfree(msg);
1882 }
1883 
1884 #define CLEANUP				1	/* Non-zero values to catch uninitialized msg */
1885 #define SET_SCHED_PROPS			2
1886 #define SUSPEND				3
1887 #define RESUME				4
1888 #define SET_MULTI_QUEUE_PRIORITY	5
1889 #define OPCODE_MASK	0xf
1890 #define MSG_LOCKED	BIT(8)
1891 #define MSG_HEAD	BIT(9)
1892 
1893 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
1894 {
1895 	struct xe_device *xe = guc_to_xe(exec_queue_to_guc(msg->private_data));
1896 
1897 	trace_xe_sched_msg_recv(msg);
1898 
1899 	switch (msg->opcode) {
1900 	case CLEANUP:
1901 		__guc_exec_queue_process_msg_cleanup(msg);
1902 		break;
1903 	case SET_SCHED_PROPS:
1904 		__guc_exec_queue_process_msg_set_sched_props(msg);
1905 		break;
1906 	case SUSPEND:
1907 		__guc_exec_queue_process_msg_suspend(msg);
1908 		break;
1909 	case RESUME:
1910 		__guc_exec_queue_process_msg_resume(msg);
1911 		break;
1912 	case SET_MULTI_QUEUE_PRIORITY:
1913 		__guc_exec_queue_process_msg_set_multi_queue_priority(msg);
1914 		break;
1915 	default:
1916 		XE_WARN_ON("Unknown message type");
1917 	}
1918 
1919 	xe_pm_runtime_put(xe);
1920 }
1921 
1922 static const struct drm_sched_backend_ops drm_sched_ops = {
1923 	.run_job = guc_exec_queue_run_job,
1924 	.free_job = guc_exec_queue_free_job,
1925 	.timedout_job = guc_exec_queue_timedout_job,
1926 };
1927 
1928 static const struct xe_sched_backend_ops xe_sched_ops = {
1929 	.process_msg = guc_exec_queue_process_msg,
1930 };
1931 
1932 static int guc_exec_queue_init(struct xe_exec_queue *q)
1933 {
1934 	struct xe_gpu_scheduler *sched;
1935 	struct xe_guc *guc = exec_queue_to_guc(q);
1936 	struct drm_device *drm = &guc_to_xe(guc)->drm;
1937 	struct workqueue_struct *submit_wq = NULL;
1938 	struct xe_guc_exec_queue *ge;
1939 	long timeout;
1940 	int err, i;
1941 
1942 	xe_gt_assert(guc_to_gt(guc), xe_device_uc_enabled(guc_to_xe(guc)));
1943 
1944 	ge = kzalloc_obj(*ge);
1945 	if (!ge)
1946 		return -ENOMEM;
1947 
1948 	drm_dev_get(drm);
1949 
1950 	q->guc = ge;
1951 	ge->q = q;
1952 	init_rcu_head(&ge->rcu);
1953 	init_waitqueue_head(&ge->suspend_wait);
1954 
1955 	for (i = 0; i < MAX_STATIC_MSG_TYPE; ++i)
1956 		INIT_LIST_HEAD(&ge->static_msgs[i].link);
1957 
1958 	timeout = (q->vm && xe_vm_in_lr_mode(q->vm)) ? MAX_SCHEDULE_TIMEOUT :
1959 		  msecs_to_jiffies(q->sched_props.job_timeout_ms);
1960 
1961 	err = alloc_guc_id(guc, q);
1962 	if (err)
1963 		goto err_free;
1964 
1965 	xe_exec_queue_assign_name(q, q->guc->id);
1966 
1967 	strscpy(ge->name, q->name, sizeof(ge->name));
1968 
1969 	/*
1970 	 * Use primary queue's submit_wq for all secondary queues of a
1971 	 * multi queue group. This serialization avoids any locking around
1972 	 * CGP synchronization with GuC.
1973 	 */
1974 	if (xe_exec_queue_is_multi_queue_secondary(q)) {
1975 		struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
1976 
1977 		submit_wq = primary->guc->sched.base.submit_wq;
1978 	}
1979 
1980 	err = xe_sched_init(&ge->sched, &drm_sched_ops, &xe_sched_ops,
1981 			    submit_wq, xe_lrc_ring_size() / MAX_JOB_SIZE_BYTES, 64,
1982 			    timeout, guc_to_gt(guc)->ordered_wq, NULL,
1983 			    ge->name, gt_to_xe(q->gt)->drm.dev);
1984 	if (err)
1985 		goto err_release_id;
1986 
1987 	sched = &ge->sched;
1988 	err = xe_sched_entity_init(&ge->entity, sched);
1989 	if (err)
1990 		goto err_sched;
1991 
1992 	q->entity = &ge->entity;
1993 
1994 	mutex_lock(&guc->submission_state.lock);
1995 	if (xe_guc_read_stopped(guc) || vf_recovery(guc))
1996 		xe_sched_stop(sched);
1997 	publish_guc_id(guc, q);
1998 	mutex_unlock(&guc->submission_state.lock);
1999 
2000 	/*
2001 	 * Maintain secondary queues of the multi queue group in a list
2002 	 * for handling dependencies across the queues in the group.
2003 	 */
2004 	if (xe_exec_queue_is_multi_queue_secondary(q)) {
2005 		struct xe_exec_queue_group *group = q->multi_queue.group;
2006 
2007 		INIT_LIST_HEAD(&q->multi_queue.link);
2008 		mutex_lock(&group->list_lock);
2009 		if (group->stopped)
2010 			WRITE_ONCE(q->guc->sched.base.pause_submit, true);
2011 		list_add_tail(&q->multi_queue.link, &group->list);
2012 		mutex_unlock(&group->list_lock);
2013 	}
2014 
2015 	if (xe_exec_queue_is_multi_queue(q))
2016 		trace_xe_exec_queue_create_multi_queue(q);
2017 	else
2018 		trace_xe_exec_queue_create(q);
2019 
2020 	return 0;
2021 
2022 err_sched:
2023 	xe_sched_fini(&ge->sched);
2024 err_release_id:
2025 	release_guc_id(guc, q);
2026 err_free:
2027 	kfree(ge);
2028 	drm_dev_put(drm);
2029 
2030 	return err;
2031 }
2032 
2033 static void guc_exec_queue_kill(struct xe_exec_queue *q)
2034 {
2035 	trace_xe_exec_queue_kill(q);
2036 	set_exec_queue_killed(q);
2037 	__suspend_fence_signal(q);
2038 	xe_guc_exec_queue_trigger_cleanup(q);
2039 }
2040 
2041 static void guc_exec_queue_add_msg(struct xe_exec_queue *q, struct xe_sched_msg *msg,
2042 				   u32 opcode)
2043 {
2044 	xe_pm_runtime_get_noresume(guc_to_xe(exec_queue_to_guc(q)));
2045 
2046 	INIT_LIST_HEAD(&msg->link);
2047 	msg->opcode = opcode & OPCODE_MASK;
2048 	msg->private_data = q;
2049 
2050 	trace_xe_sched_msg_add(msg);
2051 	if (opcode & MSG_HEAD)
2052 		xe_sched_add_msg_head(&q->guc->sched, msg);
2053 	else if (opcode & MSG_LOCKED)
2054 		xe_sched_add_msg_locked(&q->guc->sched, msg);
2055 	else
2056 		xe_sched_add_msg(&q->guc->sched, msg);
2057 }
2058 
2059 static void guc_exec_queue_try_add_msg_head(struct xe_exec_queue *q,
2060 					    struct xe_sched_msg *msg,
2061 					    u32 opcode)
2062 {
2063 	if (!list_empty(&msg->link))
2064 		return;
2065 
2066 	guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED | MSG_HEAD);
2067 }
2068 
2069 static bool guc_exec_queue_try_add_msg(struct xe_exec_queue *q,
2070 				       struct xe_sched_msg *msg,
2071 				       u32 opcode)
2072 {
2073 	if (!list_empty(&msg->link))
2074 		return false;
2075 
2076 	guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED);
2077 
2078 	return true;
2079 }
2080 
2081 #define STATIC_MSG_CLEANUP	0
2082 #define STATIC_MSG_SUSPEND	1
2083 #define STATIC_MSG_RESUME	2
2084 static void guc_exec_queue_destroy(struct xe_exec_queue *q)
2085 {
2086 	struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_CLEANUP;
2087 
2088 	if (!(q->flags & EXEC_QUEUE_FLAG_PERMANENT) && !exec_queue_wedged(q))
2089 		guc_exec_queue_add_msg(q, msg, CLEANUP);
2090 	else
2091 		__guc_exec_queue_destroy(exec_queue_to_guc(q), q);
2092 }
2093 
2094 static int guc_exec_queue_set_priority(struct xe_exec_queue *q,
2095 				       enum xe_exec_queue_priority priority)
2096 {
2097 	struct xe_sched_msg *msg;
2098 
2099 	if (q->sched_props.priority == priority ||
2100 	    exec_queue_killed_or_banned_or_wedged(q))
2101 		return 0;
2102 
2103 	msg = kmalloc_obj(*msg);
2104 	if (!msg)
2105 		return -ENOMEM;
2106 
2107 	q->sched_props.priority = priority;
2108 	guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
2109 
2110 	return 0;
2111 }
2112 
2113 static int guc_exec_queue_set_timeslice(struct xe_exec_queue *q, u32 timeslice_us)
2114 {
2115 	struct xe_sched_msg *msg;
2116 
2117 	if (q->sched_props.timeslice_us == timeslice_us ||
2118 	    exec_queue_killed_or_banned_or_wedged(q))
2119 		return 0;
2120 
2121 	msg = kmalloc_obj(*msg);
2122 	if (!msg)
2123 		return -ENOMEM;
2124 
2125 	q->sched_props.timeslice_us = timeslice_us;
2126 	guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
2127 
2128 	return 0;
2129 }
2130 
2131 static int guc_exec_queue_set_preempt_timeout(struct xe_exec_queue *q,
2132 					      u32 preempt_timeout_us)
2133 {
2134 	struct xe_sched_msg *msg;
2135 
2136 	if (q->sched_props.preempt_timeout_us == preempt_timeout_us ||
2137 	    exec_queue_killed_or_banned_or_wedged(q))
2138 		return 0;
2139 
2140 	msg = kmalloc_obj(*msg);
2141 	if (!msg)
2142 		return -ENOMEM;
2143 
2144 	q->sched_props.preempt_timeout_us = preempt_timeout_us;
2145 	guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
2146 
2147 	return 0;
2148 }
2149 
2150 static int guc_exec_queue_set_multi_queue_priority(struct xe_exec_queue *q,
2151 						   enum xe_multi_queue_priority priority)
2152 {
2153 	struct xe_sched_msg *msg;
2154 
2155 	xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), xe_exec_queue_is_multi_queue(q));
2156 
2157 	if (exec_queue_killed_or_banned_or_wedged(q))
2158 		return 0;
2159 
2160 	msg = kmalloc_obj(*msg);
2161 	if (!msg)
2162 		return -ENOMEM;
2163 
2164 	scoped_guard(spinlock, &q->multi_queue.lock) {
2165 		if (q->multi_queue.priority == priority) {
2166 			kfree(msg);
2167 			return 0;
2168 		}
2169 
2170 		q->multi_queue.priority = priority;
2171 	}
2172 
2173 	guc_exec_queue_add_msg(q, msg, SET_MULTI_QUEUE_PRIORITY);
2174 
2175 	return 0;
2176 }
2177 
2178 static int guc_exec_queue_suspend(struct xe_exec_queue *q)
2179 {
2180 	struct xe_gpu_scheduler *sched = &q->guc->sched;
2181 	struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_SUSPEND;
2182 
2183 	if (exec_queue_killed_or_banned_or_wedged(q))
2184 		return -EINVAL;
2185 
2186 	xe_sched_msg_lock(sched);
2187 	if (guc_exec_queue_try_add_msg(q, msg, SUSPEND))
2188 		q->guc->suspend_pending = true;
2189 	xe_sched_msg_unlock(sched);
2190 
2191 	return 0;
2192 }
2193 
2194 static int guc_exec_queue_suspend_wait(struct xe_exec_queue *q)
2195 {
2196 	struct xe_guc *guc = exec_queue_to_guc(q);
2197 	struct xe_device *xe = guc_to_xe(guc);
2198 	int ret;
2199 
2200 	/*
2201 	 * Likely don't need to check exec_queue_killed() as we clear
2202 	 * suspend_pending upon kill but to be paranoid but races in which
2203 	 * suspend_pending is set after kill also check kill here.
2204 	 */
2205 #define WAIT_COND \
2206 	(!READ_ONCE(q->guc->suspend_pending) ||	exec_queue_killed(q) || \
2207 	 xe_guc_read_stopped(guc))
2208 
2209 retry:
2210 	if (IS_SRIOV_VF(xe))
2211 		ret = wait_event_interruptible_timeout(guc->ct.wq, WAIT_COND ||
2212 						       vf_recovery(guc),
2213 						       HZ * 5);
2214 	else
2215 		ret = wait_event_interruptible_timeout(q->guc->suspend_wait,
2216 						       WAIT_COND, HZ * 5);
2217 
2218 	if (vf_recovery(guc) && !xe_device_wedged((guc_to_xe(guc))))
2219 		return -EAGAIN;
2220 
2221 	if (!ret) {
2222 		xe_gt_warn(guc_to_gt(guc),
2223 			   "Suspend fence, guc_id=%d, failed to respond",
2224 			   q->guc->id);
2225 		/* XXX: Trigger GT reset? */
2226 		return -ETIME;
2227 	} else if (IS_SRIOV_VF(xe) && !WAIT_COND) {
2228 		/* Corner case on RESFIX DONE where vf_recovery() changes */
2229 		goto retry;
2230 	}
2231 
2232 #undef WAIT_COND
2233 
2234 	return ret < 0 ? ret : 0;
2235 }
2236 
2237 static void guc_exec_queue_resume(struct xe_exec_queue *q)
2238 {
2239 	struct xe_gpu_scheduler *sched = &q->guc->sched;
2240 	struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_RESUME;
2241 	struct xe_guc *guc = exec_queue_to_guc(q);
2242 
2243 	xe_gt_assert(guc_to_gt(guc), !q->guc->suspend_pending);
2244 
2245 	xe_sched_msg_lock(sched);
2246 	guc_exec_queue_try_add_msg(q, msg, RESUME);
2247 	xe_sched_msg_unlock(sched);
2248 }
2249 
2250 static bool guc_exec_queue_reset_status(struct xe_exec_queue *q)
2251 {
2252 	if (xe_exec_queue_is_multi_queue_secondary(q) &&
2253 	    guc_exec_queue_reset_status(xe_exec_queue_multi_queue_primary(q)))
2254 		return true;
2255 
2256 	return exec_queue_reset(q) || exec_queue_killed_or_banned_or_wedged(q);
2257 }
2258 
2259 static bool guc_exec_queue_active(struct xe_exec_queue *q)
2260 {
2261 	struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
2262 
2263 	return exec_queue_enabled(primary) &&
2264 		!exec_queue_pending_disable(primary);
2265 }
2266 
2267 /*
2268  * All of these functions are an abstraction layer which other parts of Xe can
2269  * use to trap into the GuC backend. All of these functions, aside from init,
2270  * really shouldn't do much other than trap into the DRM scheduler which
2271  * synchronizes these operations.
2272  */
2273 static const struct xe_exec_queue_ops guc_exec_queue_ops = {
2274 	.init = guc_exec_queue_init,
2275 	.kill = guc_exec_queue_kill,
2276 	.fini = guc_exec_queue_fini,
2277 	.destroy = guc_exec_queue_destroy,
2278 	.set_priority = guc_exec_queue_set_priority,
2279 	.set_timeslice = guc_exec_queue_set_timeslice,
2280 	.set_preempt_timeout = guc_exec_queue_set_preempt_timeout,
2281 	.set_multi_queue_priority = guc_exec_queue_set_multi_queue_priority,
2282 	.suspend = guc_exec_queue_suspend,
2283 	.suspend_wait = guc_exec_queue_suspend_wait,
2284 	.resume = guc_exec_queue_resume,
2285 	.reset_status = guc_exec_queue_reset_status,
2286 	.active = guc_exec_queue_active,
2287 };
2288 
2289 static void guc_exec_queue_stop(struct xe_guc *guc, struct xe_exec_queue *q)
2290 {
2291 	struct xe_gpu_scheduler *sched = &q->guc->sched;
2292 	bool do_destroy = false;
2293 
2294 	/* Stop scheduling + flush any DRM scheduler operations */
2295 	xe_sched_submission_stop(sched);
2296 
2297 	/* Clean up lost G2H + reset engine state */
2298 	if (exec_queue_registered(q)) {
2299 		if (exec_queue_destroyed(q))
2300 			do_destroy = true;
2301 	}
2302 	if (q->guc->suspend_pending) {
2303 		set_exec_queue_suspended(q);
2304 		suspend_fence_signal(q);
2305 	}
2306 	atomic_and(EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_BANNED |
2307 		   EXEC_QUEUE_STATE_KILLED | EXEC_QUEUE_STATE_DESTROYED |
2308 		   EXEC_QUEUE_STATE_SUSPENDED,
2309 		   &q->guc->state);
2310 	q->guc->resume_time = 0;
2311 	trace_xe_exec_queue_stop(q);
2312 
2313 	/*
2314 	 * Ban any engine (aside from kernel and engines used for VM ops) with a
2315 	 * started but not complete job or if a job has gone through a GT reset
2316 	 * more than twice.
2317 	 */
2318 	if (!(q->flags & (EXEC_QUEUE_FLAG_KERNEL | EXEC_QUEUE_FLAG_VM))) {
2319 		struct xe_sched_job *job = xe_sched_first_pending_job(sched);
2320 		bool ban = false;
2321 
2322 		if (job) {
2323 			if ((xe_sched_job_started(job) &&
2324 			    !xe_sched_job_completed(job)) ||
2325 			    xe_sched_invalidate_job(job, 2)) {
2326 				trace_xe_sched_job_ban(job);
2327 				ban = true;
2328 			}
2329 		}
2330 
2331 		if (ban) {
2332 			set_exec_queue_banned(q);
2333 			xe_guc_exec_queue_trigger_cleanup(q);
2334 		}
2335 	}
2336 
2337 	if (do_destroy)
2338 		__guc_exec_queue_destroy(guc, q);
2339 }
2340 
2341 static int guc_submit_reset_prepare(struct xe_guc *guc)
2342 {
2343 	int ret;
2344 
2345 	/*
2346 	 * Using an atomic here rather than submission_state.lock as this
2347 	 * function can be called while holding the CT lock (engine reset
2348 	 * failure). submission_state.lock needs the CT lock to resubmit jobs.
2349 	 * Atomic is not ideal, but it works to prevent against concurrent reset
2350 	 * and releasing any TDRs waiting on guc->submission_state.stopped.
2351 	 */
2352 	ret = atomic_fetch_or(1, &guc->submission_state.stopped);
2353 	smp_wmb();
2354 	wake_up_all(&guc->ct.wq);
2355 
2356 	return ret;
2357 }
2358 
2359 int xe_guc_submit_reset_prepare(struct xe_guc *guc)
2360 {
2361 	if (xe_gt_WARN_ON(guc_to_gt(guc), vf_recovery(guc)))
2362 		return 0;
2363 
2364 	if (!guc->submission_state.initialized)
2365 		return 0;
2366 
2367 	return guc_submit_reset_prepare(guc);
2368 }
2369 
2370 void xe_guc_submit_reset_wait(struct xe_guc *guc)
2371 {
2372 	wait_event(guc->ct.wq, xe_device_wedged(guc_to_xe(guc)) ||
2373 		   !xe_guc_read_stopped(guc));
2374 }
2375 
2376 void xe_guc_submit_stop(struct xe_guc *guc)
2377 {
2378 	struct xe_exec_queue *q;
2379 	unsigned long index;
2380 
2381 	xe_gt_assert(guc_to_gt(guc), xe_guc_read_stopped(guc) == 1);
2382 
2383 	mutex_lock(&guc->submission_state.lock);
2384 
2385 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2386 		/* Prevent redundant attempts to stop parallel queues */
2387 		if (q->guc->id != index)
2388 			continue;
2389 
2390 		guc_exec_queue_stop(guc, q);
2391 	}
2392 
2393 	mutex_unlock(&guc->submission_state.lock);
2394 
2395 	/*
2396 	 * No one can enter the backend at this point, aside from new engine
2397 	 * creation which is protected by guc->submission_state.lock.
2398 	 */
2399 
2400 }
2401 
2402 static void guc_exec_queue_revert_pending_state_change(struct xe_guc *guc,
2403 						       struct xe_exec_queue *q)
2404 {
2405 	bool pending_enable, pending_disable, pending_resume;
2406 
2407 	pending_enable = exec_queue_pending_enable(q);
2408 	pending_resume = exec_queue_pending_resume(q);
2409 
2410 	if (pending_enable && pending_resume) {
2411 		q->guc->needs_resume = true;
2412 		xe_gt_dbg(guc_to_gt(guc), "Replay RESUME - guc_id=%d",
2413 			  q->guc->id);
2414 	}
2415 
2416 	if (pending_enable && !pending_resume) {
2417 		clear_exec_queue_registered(q);
2418 		xe_gt_dbg(guc_to_gt(guc), "Replay REGISTER - guc_id=%d",
2419 			  q->guc->id);
2420 	}
2421 
2422 	if (pending_enable) {
2423 		clear_exec_queue_enabled(q);
2424 		clear_exec_queue_pending_resume(q);
2425 		clear_exec_queue_pending_enable(q);
2426 		xe_gt_dbg(guc_to_gt(guc), "Replay ENABLE - guc_id=%d",
2427 			  q->guc->id);
2428 	}
2429 
2430 	if (exec_queue_destroyed(q) && exec_queue_registered(q)) {
2431 		clear_exec_queue_destroyed(q);
2432 		q->guc->needs_cleanup = true;
2433 		xe_gt_dbg(guc_to_gt(guc), "Replay CLEANUP - guc_id=%d",
2434 			  q->guc->id);
2435 	}
2436 
2437 	pending_disable = exec_queue_pending_disable(q);
2438 
2439 	if (pending_disable && exec_queue_suspended(q)) {
2440 		clear_exec_queue_suspended(q);
2441 		q->guc->needs_suspend = true;
2442 		xe_gt_dbg(guc_to_gt(guc), "Replay SUSPEND - guc_id=%d",
2443 			  q->guc->id);
2444 	}
2445 
2446 	if (pending_disable) {
2447 		if (!pending_enable)
2448 			set_exec_queue_enabled(q);
2449 		clear_exec_queue_pending_disable(q);
2450 		xe_gt_dbg(guc_to_gt(guc), "Replay DISABLE - guc_id=%d",
2451 			  q->guc->id);
2452 	}
2453 
2454 	q->guc->resume_time = 0;
2455 }
2456 
2457 static void lrc_parallel_clear(struct xe_lrc *lrc)
2458 {
2459 	struct xe_device *xe = gt_to_xe(lrc->gt);
2460 	struct iosys_map map = xe_lrc_parallel_map(lrc);
2461 	int i;
2462 
2463 	for (i = 0; i < WQ_SIZE / sizeof(u32); ++i)
2464 		parallel_write(xe, map, wq[i],
2465 			       FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
2466 			       FIELD_PREP(WQ_LEN_MASK, 0));
2467 }
2468 
2469 /*
2470  * This function is quite complex but only real way to ensure no state is lost
2471  * during VF resume flows. The function scans the queue state, make adjustments
2472  * as needed, and queues jobs / messages which replayed upon unpause.
2473  */
2474 static void guc_exec_queue_pause(struct xe_guc *guc, struct xe_exec_queue *q)
2475 {
2476 	struct xe_gpu_scheduler *sched = &q->guc->sched;
2477 	struct xe_sched_job *job;
2478 	int i;
2479 
2480 	lockdep_assert_held(&guc->submission_state.lock);
2481 
2482 	/* Stop scheduling + flush any DRM scheduler operations */
2483 	xe_sched_submission_stop(sched);
2484 	cancel_delayed_work_sync(&sched->base.work_tdr);
2485 
2486 	guc_exec_queue_revert_pending_state_change(guc, q);
2487 
2488 	if (xe_exec_queue_is_parallel(q)) {
2489 		/* Pairs with WRITE_ONCE in __xe_exec_queue_init  */
2490 		struct xe_lrc *lrc = READ_ONCE(q->lrc[0]);
2491 
2492 		/*
2493 		 * NOP existing WQ commands that may contain stale GGTT
2494 		 * addresses. These will be replayed upon unpause. The hardware
2495 		 * seems to get confused if the WQ head/tail pointers are
2496 		 * adjusted.
2497 		 */
2498 		if (lrc)
2499 			lrc_parallel_clear(lrc);
2500 	}
2501 
2502 	job = xe_sched_first_pending_job(sched);
2503 	if (job) {
2504 		job->restore_replay = true;
2505 
2506 		/*
2507 		 * Adjust software tail so jobs submitted overwrite previous
2508 		 * position in ring buffer with new GGTT addresses.
2509 		 */
2510 		for (i = 0; i < q->width; ++i)
2511 			q->lrc[i]->ring.tail = job->ptrs[i].head;
2512 	}
2513 }
2514 
2515 /**
2516  * xe_guc_submit_pause - Stop further runs of submission tasks on given GuC.
2517  * @guc: the &xe_guc struct instance whose scheduler is to be disabled
2518  */
2519 void xe_guc_submit_pause(struct xe_guc *guc)
2520 {
2521 	struct xe_exec_queue *q;
2522 	unsigned long index;
2523 
2524 	mutex_lock(&guc->submission_state.lock);
2525 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
2526 		xe_sched_submission_stop(&q->guc->sched);
2527 	mutex_unlock(&guc->submission_state.lock);
2528 }
2529 
2530 /**
2531  * xe_guc_submit_pause_vf - Stop further runs of submission tasks for VF.
2532  * @guc: the &xe_guc struct instance whose scheduler is to be disabled
2533  */
2534 void xe_guc_submit_pause_vf(struct xe_guc *guc)
2535 {
2536 	struct xe_exec_queue *q;
2537 	unsigned long index;
2538 
2539 	xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
2540 	xe_gt_assert(guc_to_gt(guc), vf_recovery(guc));
2541 
2542 	mutex_lock(&guc->submission_state.lock);
2543 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2544 		/* Prevent redundant attempts to stop parallel queues */
2545 		if (q->guc->id != index)
2546 			continue;
2547 
2548 		guc_exec_queue_pause(guc, q);
2549 	}
2550 	mutex_unlock(&guc->submission_state.lock);
2551 }
2552 
2553 static void guc_exec_queue_start(struct xe_exec_queue *q)
2554 {
2555 	struct xe_gpu_scheduler *sched = &q->guc->sched;
2556 
2557 	if (!exec_queue_killed_or_banned_or_wedged(q)) {
2558 		struct xe_sched_job *job = xe_sched_first_pending_job(sched);
2559 		int i;
2560 
2561 		trace_xe_exec_queue_resubmit(q);
2562 		if (job) {
2563 			for (i = 0; i < q->width; ++i) {
2564 				/*
2565 				 * The GuC context is unregistered at this point
2566 				 * time, adjusting software ring tail ensures
2567 				 * jobs are rewritten in original placement,
2568 				 * adjusting LRC tail ensures the newly loaded
2569 				 * GuC / contexts only view the LRC tail
2570 				 * increasing as jobs are written out.
2571 				 */
2572 				q->lrc[i]->ring.tail = job->ptrs[i].head;
2573 				xe_lrc_set_ring_tail(q->lrc[i],
2574 						     xe_lrc_ring_head(q->lrc[i]));
2575 			}
2576 		}
2577 		xe_sched_resubmit_jobs(sched);
2578 	}
2579 
2580 	xe_sched_submission_start(sched);
2581 	xe_sched_submission_resume_tdr(sched);
2582 }
2583 
2584 int xe_guc_submit_start(struct xe_guc *guc)
2585 {
2586 	struct xe_exec_queue *q;
2587 	unsigned long index;
2588 
2589 	xe_gt_assert(guc_to_gt(guc), xe_guc_read_stopped(guc) == 1);
2590 
2591 	mutex_lock(&guc->submission_state.lock);
2592 	atomic_dec(&guc->submission_state.stopped);
2593 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2594 		/* Prevent redundant attempts to start parallel queues */
2595 		if (q->guc->id != index)
2596 			continue;
2597 
2598 		guc_exec_queue_start(q);
2599 	}
2600 	mutex_unlock(&guc->submission_state.lock);
2601 
2602 	wake_up_all(&guc->ct.wq);
2603 
2604 	return 0;
2605 }
2606 
2607 static void guc_exec_queue_unpause_prepare(struct xe_guc *guc,
2608 					   struct xe_exec_queue *q)
2609 {
2610 	struct xe_gpu_scheduler *sched = &q->guc->sched;
2611 	struct xe_sched_job *job = NULL;
2612 	struct drm_sched_job *s_job;
2613 	bool restore_replay = false;
2614 
2615 	drm_sched_for_each_pending_job(s_job, &sched->base, NULL) {
2616 		job = to_xe_sched_job(s_job);
2617 		restore_replay |= job->restore_replay;
2618 		if (restore_replay) {
2619 			xe_gt_dbg(guc_to_gt(guc), "Replay JOB - guc_id=%d, seqno=%d",
2620 				  q->guc->id, xe_sched_job_seqno(job));
2621 
2622 			q->ring_ops->emit_job(job);
2623 			job->restore_replay = true;
2624 		}
2625 	}
2626 
2627 	if (job)
2628 		job->last_replay = true;
2629 }
2630 
2631 /**
2632  * xe_guc_submit_unpause_prepare_vf - Prepare unpause submission tasks for VF.
2633  * @guc: the &xe_guc struct instance whose scheduler is to be prepared for unpause
2634  */
2635 void xe_guc_submit_unpause_prepare_vf(struct xe_guc *guc)
2636 {
2637 	struct xe_exec_queue *q;
2638 	unsigned long index;
2639 
2640 	xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
2641 	xe_gt_assert(guc_to_gt(guc), vf_recovery(guc));
2642 
2643 	mutex_lock(&guc->submission_state.lock);
2644 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2645 		/* Prevent redundant attempts to stop parallel queues */
2646 		if (q->guc->id != index)
2647 			continue;
2648 
2649 		guc_exec_queue_unpause_prepare(guc, q);
2650 	}
2651 	mutex_unlock(&guc->submission_state.lock);
2652 }
2653 
2654 static void guc_exec_queue_replay_pending_state_change(struct xe_exec_queue *q)
2655 {
2656 	struct xe_gpu_scheduler *sched = &q->guc->sched;
2657 	struct xe_sched_msg *msg;
2658 
2659 	if (q->guc->needs_cleanup) {
2660 		msg = q->guc->static_msgs + STATIC_MSG_CLEANUP;
2661 
2662 		guc_exec_queue_add_msg(q, msg, CLEANUP);
2663 		q->guc->needs_cleanup = false;
2664 	}
2665 
2666 	if (q->guc->needs_suspend) {
2667 		msg = q->guc->static_msgs + STATIC_MSG_SUSPEND;
2668 
2669 		xe_sched_msg_lock(sched);
2670 		guc_exec_queue_try_add_msg_head(q, msg, SUSPEND);
2671 		xe_sched_msg_unlock(sched);
2672 
2673 		q->guc->needs_suspend = false;
2674 	}
2675 
2676 	/*
2677 	 * The resume must be in the message queue before the suspend as it is
2678 	 * not possible for a resume to be issued if a suspend pending is, but
2679 	 * the inverse is possible.
2680 	 */
2681 	if (q->guc->needs_resume) {
2682 		msg = q->guc->static_msgs + STATIC_MSG_RESUME;
2683 
2684 		xe_sched_msg_lock(sched);
2685 		guc_exec_queue_try_add_msg_head(q, msg, RESUME);
2686 		xe_sched_msg_unlock(sched);
2687 
2688 		q->guc->needs_resume = false;
2689 	}
2690 }
2691 
2692 static void guc_exec_queue_unpause(struct xe_guc *guc, struct xe_exec_queue *q)
2693 {
2694 	struct xe_gpu_scheduler *sched = &q->guc->sched;
2695 	bool needs_tdr = exec_queue_killed_or_banned_or_wedged(q);
2696 
2697 	lockdep_assert_held(&guc->submission_state.lock);
2698 
2699 	xe_sched_resubmit_jobs(sched);
2700 	guc_exec_queue_replay_pending_state_change(q);
2701 	xe_sched_submission_start(sched);
2702 	if (needs_tdr)
2703 		xe_guc_exec_queue_trigger_cleanup(q);
2704 	xe_sched_submission_resume_tdr(sched);
2705 }
2706 
2707 /**
2708  * xe_guc_submit_unpause - Allow further runs of submission tasks on given GuC.
2709  * @guc: the &xe_guc struct instance whose scheduler is to be enabled
2710  */
2711 void xe_guc_submit_unpause(struct xe_guc *guc)
2712 {
2713 	struct xe_exec_queue *q;
2714 	unsigned long index;
2715 
2716 	mutex_lock(&guc->submission_state.lock);
2717 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
2718 		xe_sched_submission_start(&q->guc->sched);
2719 	mutex_unlock(&guc->submission_state.lock);
2720 }
2721 
2722 /**
2723  * xe_guc_submit_unpause_vf - Allow further runs of submission tasks for VF.
2724  * @guc: the &xe_guc struct instance whose scheduler is to be enabled
2725  */
2726 void xe_guc_submit_unpause_vf(struct xe_guc *guc)
2727 {
2728 	struct xe_exec_queue *q;
2729 	unsigned long index;
2730 
2731 	xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
2732 
2733 	mutex_lock(&guc->submission_state.lock);
2734 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2735 		/*
2736 		 * Prevent redundant attempts to stop parallel queues, or queues
2737 		 * created after resfix done.
2738 		 */
2739 		if (q->guc->id != index ||
2740 		    !drm_sched_is_stopped(&q->guc->sched.base))
2741 			continue;
2742 
2743 		guc_exec_queue_unpause(guc, q);
2744 	}
2745 	mutex_unlock(&guc->submission_state.lock);
2746 }
2747 
2748 /**
2749  * xe_guc_submit_pause_abort - Abort all paused submission task on given GuC.
2750  * @guc: the &xe_guc struct instance whose scheduler is to be aborted
2751  */
2752 void xe_guc_submit_pause_abort(struct xe_guc *guc)
2753 {
2754 	struct xe_exec_queue *q;
2755 	unsigned long index;
2756 
2757 	mutex_lock(&guc->submission_state.lock);
2758 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2759 		struct xe_gpu_scheduler *sched = &q->guc->sched;
2760 
2761 		/* Prevent redundant attempts to stop parallel queues */
2762 		if (q->guc->id != index)
2763 			continue;
2764 
2765 		xe_sched_submission_start(sched);
2766 		guc_exec_queue_kill(q);
2767 	}
2768 	mutex_unlock(&guc->submission_state.lock);
2769 }
2770 
2771 static struct xe_exec_queue *
2772 g2h_exec_queue_lookup(struct xe_guc *guc, u32 guc_id)
2773 {
2774 	struct xe_gt *gt = guc_to_gt(guc);
2775 	struct xe_exec_queue *q;
2776 
2777 	if (unlikely(guc_id >= GUC_ID_MAX)) {
2778 		xe_gt_err(gt, "Invalid guc_id %u\n", guc_id);
2779 		return NULL;
2780 	}
2781 
2782 	q = xa_load(&guc->submission_state.exec_queue_lookup, guc_id);
2783 	if (unlikely(!q)) {
2784 		xe_gt_err(gt, "No exec queue found for guc_id %u\n", guc_id);
2785 		return NULL;
2786 	}
2787 
2788 	xe_gt_assert(guc_to_gt(guc), guc_id >= q->guc->id);
2789 	xe_gt_assert(guc_to_gt(guc), guc_id < (q->guc->id + q->width));
2790 
2791 	return q;
2792 }
2793 
2794 static void deregister_exec_queue(struct xe_guc *guc, struct xe_exec_queue *q)
2795 {
2796 	u32 action[] = {
2797 		XE_GUC_ACTION_DEREGISTER_CONTEXT,
2798 		q->guc->id,
2799 	};
2800 
2801 	xe_gt_assert(guc_to_gt(guc), exec_queue_destroyed(q));
2802 	xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
2803 	xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
2804 	xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q));
2805 
2806 	trace_xe_exec_queue_deregister(q);
2807 
2808 	if (xe_exec_queue_is_multi_queue_secondary(q))
2809 		handle_deregister_done(guc, q);
2810 	else
2811 		xe_guc_ct_send_g2h_handler(&guc->ct, action,
2812 					   ARRAY_SIZE(action));
2813 }
2814 
2815 static void handle_sched_done(struct xe_guc *guc, struct xe_exec_queue *q,
2816 			      u32 runnable_state)
2817 {
2818 	trace_xe_exec_queue_scheduling_done(q);
2819 
2820 	if (runnable_state == 1) {
2821 		xe_gt_assert(guc_to_gt(guc), exec_queue_pending_enable(q));
2822 
2823 		q->guc->resume_time = ktime_get();
2824 		clear_exec_queue_pending_resume(q);
2825 		clear_exec_queue_pending_enable(q);
2826 		smp_wmb();
2827 		wake_up_all(&guc->ct.wq);
2828 	} else {
2829 		xe_gt_assert(guc_to_gt(guc), runnable_state == 0);
2830 		xe_gt_assert(guc_to_gt(guc), exec_queue_pending_disable(q));
2831 
2832 		if (q->guc->suspend_pending) {
2833 			clear_exec_queue_pending_disable(q);
2834 			suspend_fence_signal(q);
2835 		} else {
2836 			if (exec_queue_banned(q)) {
2837 				smp_wmb();
2838 				wake_up_all(&guc->ct.wq);
2839 			}
2840 			if (exec_queue_destroyed(q)) {
2841 				/*
2842 				 * Make sure to clear the pending_disable only
2843 				 * after sampling the destroyed state. We want
2844 				 * to ensure we don't trigger the unregister too
2845 				 * early with something intending to only
2846 				 * disable scheduling. The caller doing the
2847 				 * destroy must wait for an ongoing
2848 				 * pending_disable before marking as destroyed.
2849 				 */
2850 				clear_exec_queue_pending_disable(q);
2851 				deregister_exec_queue(guc, q);
2852 			} else {
2853 				clear_exec_queue_pending_disable(q);
2854 			}
2855 		}
2856 	}
2857 }
2858 
2859 static void handle_multi_queue_secondary_sched_done(struct xe_guc *guc,
2860 						    struct xe_exec_queue *q,
2861 						    u32 runnable_state)
2862 {
2863 	/* Take CT lock here as handle_sched_done() do send a h2g message */
2864 	mutex_lock(&guc->ct.lock);
2865 	handle_sched_done(guc, q, runnable_state);
2866 	mutex_unlock(&guc->ct.lock);
2867 }
2868 
2869 int xe_guc_sched_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
2870 {
2871 	struct xe_exec_queue *q;
2872 	u32 guc_id, runnable_state;
2873 
2874 	if (unlikely(len < 2))
2875 		return -EPROTO;
2876 
2877 	guc_id = msg[0];
2878 	runnable_state = msg[1];
2879 
2880 	q = g2h_exec_queue_lookup(guc, guc_id);
2881 	if (unlikely(!q))
2882 		return -EPROTO;
2883 
2884 	if (unlikely(!exec_queue_pending_enable(q) &&
2885 		     !exec_queue_pending_disable(q))) {
2886 		xe_gt_err(guc_to_gt(guc),
2887 			  "SCHED_DONE: Unexpected engine state 0x%04x, guc_id=%d, runnable_state=%u",
2888 			  atomic_read(&q->guc->state), q->guc->id,
2889 			  runnable_state);
2890 		return -EPROTO;
2891 	}
2892 
2893 	handle_sched_done(guc, q, runnable_state);
2894 
2895 	return 0;
2896 }
2897 
2898 static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q)
2899 {
2900 	trace_xe_exec_queue_deregister_done(q);
2901 
2902 	clear_exec_queue_registered(q);
2903 	__guc_exec_queue_destroy(guc, q);
2904 }
2905 
2906 int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
2907 {
2908 	struct xe_exec_queue *q;
2909 	u32 guc_id;
2910 
2911 	if (unlikely(len < 1))
2912 		return -EPROTO;
2913 
2914 	guc_id = msg[0];
2915 
2916 	q = g2h_exec_queue_lookup(guc, guc_id);
2917 	if (unlikely(!q))
2918 		return -EPROTO;
2919 
2920 	if (!exec_queue_destroyed(q) || exec_queue_pending_disable(q) ||
2921 	    exec_queue_pending_enable(q) || exec_queue_enabled(q)) {
2922 		xe_gt_err(guc_to_gt(guc),
2923 			  "DEREGISTER_DONE: Unexpected engine state 0x%04x, guc_id=%d",
2924 			  atomic_read(&q->guc->state), q->guc->id);
2925 		return -EPROTO;
2926 	}
2927 
2928 	handle_deregister_done(guc, q);
2929 
2930 	return 0;
2931 }
2932 
2933 int xe_guc_exec_queue_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
2934 {
2935 	struct xe_gt *gt = guc_to_gt(guc);
2936 	struct xe_exec_queue *q;
2937 	u32 guc_id;
2938 
2939 	if (unlikely(len < 1))
2940 		return -EPROTO;
2941 
2942 	guc_id = msg[0];
2943 
2944 	q = g2h_exec_queue_lookup(guc, guc_id);
2945 	if (unlikely(!q))
2946 		return -EPROTO;
2947 
2948 	if (!exec_queue_killed(q))
2949 		xe_gt_info(gt, "Engine reset: engine_class=%s, logical_mask: 0x%x, guc_id=%d, state=0x%0x",
2950 			   xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id,
2951 			   atomic_read(&q->guc->state));
2952 
2953 	trace_xe_exec_queue_reset(q);
2954 
2955 	/*
2956 	 * A banned engine is a NOP at this point (came from
2957 	 * guc_exec_queue_timedout_job). Otherwise, kick drm scheduler to cancel
2958 	 * jobs by setting timeout of the job to the minimum value kicking
2959 	 * guc_exec_queue_timedout_job.
2960 	 */
2961 	xe_guc_exec_queue_reset_trigger_cleanup(q);
2962 
2963 	return 0;
2964 }
2965 
2966 /*
2967  * xe_guc_error_capture_handler - Handler of GuC captured message
2968  * @guc: The GuC object
2969  * @msg: Point to the message
2970  * @len: The message length
2971  *
2972  * When GuC captured data is ready, GuC will send message
2973  * XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION to host, this function will be
2974  * called 1st to check status before process the data comes with the message.
2975  *
2976  * Returns: error code. 0 if success
2977  */
2978 int xe_guc_error_capture_handler(struct xe_guc *guc, u32 *msg, u32 len)
2979 {
2980 	u32 status;
2981 
2982 	if (unlikely(len != XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION_DATA_LEN))
2983 		return -EPROTO;
2984 
2985 	status = msg[0] & XE_GUC_STATE_CAPTURE_EVENT_STATUS_MASK;
2986 	if (status == XE_GUC_STATE_CAPTURE_EVENT_STATUS_NOSPACE)
2987 		xe_gt_warn(guc_to_gt(guc), "G2H-Error capture no space");
2988 
2989 	xe_guc_capture_process(guc);
2990 
2991 	return 0;
2992 }
2993 
2994 int xe_guc_exec_queue_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
2995 					       u32 len)
2996 {
2997 	struct xe_gt *gt = guc_to_gt(guc);
2998 	struct xe_exec_queue *q;
2999 	u32 guc_id;
3000 	u32 type = XE_GUC_CAT_ERR_TYPE_INVALID;
3001 
3002 	if (unlikely(!len || len > 2))
3003 		return -EPROTO;
3004 
3005 	guc_id = msg[0];
3006 
3007 	if (len == 2)
3008 		type = msg[1];
3009 
3010 	if (guc_id == GUC_ID_UNKNOWN) {
3011 		/*
3012 		 * GuC uses GUC_ID_UNKNOWN if it can not map the CAT fault to any PF/VF
3013 		 * context. In such case only PF will be notified about that fault.
3014 		 */
3015 		xe_gt_err_ratelimited(gt, "Memory CAT error reported by GuC!\n");
3016 		return 0;
3017 	}
3018 
3019 	q = g2h_exec_queue_lookup(guc, guc_id);
3020 	if (unlikely(!q))
3021 		return -EPROTO;
3022 
3023 	/*
3024 	 * The type is HW-defined and changes based on platform, so we don't
3025 	 * decode it in the kernel and only check if it is valid.
3026 	 * See bspec 54047 and 72187 for details.
3027 	 */
3028 	if (type != XE_GUC_CAT_ERR_TYPE_INVALID)
3029 		xe_gt_info(gt,
3030 			   "Engine memory CAT error [%u]: class=%s, logical_mask: 0x%x, guc_id=%d",
3031 			   type, xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id);
3032 	else
3033 		xe_gt_info(gt,
3034 			   "Engine memory CAT error: class=%s, logical_mask: 0x%x, guc_id=%d",
3035 			   xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id);
3036 
3037 	trace_xe_exec_queue_memory_cat_error(q);
3038 
3039 	/* Treat the same as engine reset */
3040 	xe_guc_exec_queue_reset_trigger_cleanup(q);
3041 
3042 	return 0;
3043 }
3044 
3045 int xe_guc_exec_queue_reset_failure_handler(struct xe_guc *guc, u32 *msg, u32 len)
3046 {
3047 	struct xe_gt *gt = guc_to_gt(guc);
3048 	u8 guc_class, instance;
3049 	u32 reason;
3050 
3051 	if (unlikely(len != 3))
3052 		return -EPROTO;
3053 
3054 	guc_class = msg[0];
3055 	instance = msg[1];
3056 	reason = msg[2];
3057 
3058 	/* Unexpected failure of a hardware feature, log an actual error */
3059 	xe_gt_err(gt, "GuC engine reset request failed on %d:%d because 0x%08X",
3060 		  guc_class, instance, reason);
3061 
3062 	xe_gt_reset_async(gt);
3063 
3064 	return 0;
3065 }
3066 
3067 int xe_guc_exec_queue_cgp_context_error_handler(struct xe_guc *guc, u32 *msg,
3068 						u32 len)
3069 {
3070 	struct xe_gt *gt = guc_to_gt(guc);
3071 	struct xe_device *xe = guc_to_xe(guc);
3072 	struct xe_exec_queue *q;
3073 	u32 guc_id = msg[2];
3074 
3075 	if (unlikely(len != XE_GUC_EXEC_QUEUE_CGP_CONTEXT_ERROR_LEN)) {
3076 		drm_err(&xe->drm, "Invalid length %u", len);
3077 		return -EPROTO;
3078 	}
3079 
3080 	q = g2h_exec_queue_lookup(guc, guc_id);
3081 	if (unlikely(!q))
3082 		return -EPROTO;
3083 
3084 	xe_gt_dbg(gt,
3085 		  "CGP context error: [%s] err=0x%x, q0_id=0x%x LRCA=0x%x guc_id=0x%x",
3086 		  msg[0] & 1 ? "uc" : "kmd", msg[1], msg[2], msg[3], msg[4]);
3087 
3088 	trace_xe_exec_queue_cgp_context_error(q);
3089 
3090 	/* Treat the same as engine reset */
3091 	xe_guc_exec_queue_reset_trigger_cleanup(q);
3092 
3093 	return 0;
3094 }
3095 
3096 /**
3097  * xe_guc_exec_queue_cgp_sync_done_handler - CGP synchronization done handler
3098  * @guc: guc
3099  * @msg: message indicating CGP sync done
3100  * @len: length of message
3101  *
3102  * Set multi queue group's sync_pending flag to false and wakeup anyone waiting
3103  * for CGP synchronization to complete.
3104  *
3105  * Return: 0 on success, -EPROTO for malformed messages.
3106  */
3107 int xe_guc_exec_queue_cgp_sync_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
3108 {
3109 	struct xe_device *xe = guc_to_xe(guc);
3110 	struct xe_exec_queue *q;
3111 	u32 guc_id = msg[0];
3112 
3113 	if (unlikely(len < 1)) {
3114 		drm_err(&xe->drm, "Invalid CGP_SYNC_DONE length %u", len);
3115 		return -EPROTO;
3116 	}
3117 
3118 	q = g2h_exec_queue_lookup(guc, guc_id);
3119 	if (unlikely(!q))
3120 		return -EPROTO;
3121 
3122 	if (!xe_exec_queue_is_multi_queue_primary(q)) {
3123 		drm_err(&xe->drm, "Unexpected CGP_SYNC_DONE response");
3124 		return -EPROTO;
3125 	}
3126 
3127 	/* Wakeup the serialized cgp update wait */
3128 	WRITE_ONCE(q->multi_queue.group->sync_pending, false);
3129 	xe_guc_ct_wake_waiters(&guc->ct);
3130 
3131 	return 0;
3132 }
3133 
3134 static void
3135 guc_exec_queue_wq_snapshot_capture(struct xe_exec_queue *q,
3136 				   struct xe_guc_submit_exec_queue_snapshot *snapshot)
3137 {
3138 	struct xe_guc *guc = exec_queue_to_guc(q);
3139 	struct xe_device *xe = guc_to_xe(guc);
3140 	struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
3141 	int i;
3142 
3143 	snapshot->guc.wqi_head = q->guc->wqi_head;
3144 	snapshot->guc.wqi_tail = q->guc->wqi_tail;
3145 	snapshot->parallel.wq_desc.head = parallel_read(xe, map, wq_desc.head);
3146 	snapshot->parallel.wq_desc.tail = parallel_read(xe, map, wq_desc.tail);
3147 	snapshot->parallel.wq_desc.status = parallel_read(xe, map,
3148 							  wq_desc.wq_status);
3149 
3150 	if (snapshot->parallel.wq_desc.head !=
3151 	    snapshot->parallel.wq_desc.tail) {
3152 		for (i = snapshot->parallel.wq_desc.head;
3153 		     i != snapshot->parallel.wq_desc.tail;
3154 		     i = (i + sizeof(u32)) % WQ_SIZE)
3155 			snapshot->parallel.wq[i / sizeof(u32)] =
3156 				parallel_read(xe, map, wq[i / sizeof(u32)]);
3157 	}
3158 }
3159 
3160 static void
3161 guc_exec_queue_wq_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot,
3162 				 struct drm_printer *p)
3163 {
3164 	int i;
3165 
3166 	drm_printf(p, "\tWQ head: %u (internal), %d (memory)\n",
3167 		   snapshot->guc.wqi_head, snapshot->parallel.wq_desc.head);
3168 	drm_printf(p, "\tWQ tail: %u (internal), %d (memory)\n",
3169 		   snapshot->guc.wqi_tail, snapshot->parallel.wq_desc.tail);
3170 	drm_printf(p, "\tWQ status: %u\n", snapshot->parallel.wq_desc.status);
3171 
3172 	if (snapshot->parallel.wq_desc.head !=
3173 	    snapshot->parallel.wq_desc.tail) {
3174 		for (i = snapshot->parallel.wq_desc.head;
3175 		     i != snapshot->parallel.wq_desc.tail;
3176 		     i = (i + sizeof(u32)) % WQ_SIZE)
3177 			drm_printf(p, "\tWQ[%zu]: 0x%08x\n", i / sizeof(u32),
3178 				   snapshot->parallel.wq[i / sizeof(u32)]);
3179 	}
3180 }
3181 
3182 /**
3183  * xe_guc_exec_queue_snapshot_capture - Take a quick snapshot of the GuC Engine.
3184  * @q: faulty exec queue
3185  *
3186  * This can be printed out in a later stage like during dev_coredump
3187  * analysis.
3188  *
3189  * Returns: a GuC Submit Engine snapshot object that must be freed by the
3190  * caller, using `xe_guc_exec_queue_snapshot_free`.
3191  */
3192 struct xe_guc_submit_exec_queue_snapshot *
3193 xe_guc_exec_queue_snapshot_capture(struct xe_exec_queue *q)
3194 {
3195 	struct xe_gpu_scheduler *sched = &q->guc->sched;
3196 	struct xe_guc_submit_exec_queue_snapshot *snapshot;
3197 	int i;
3198 
3199 	snapshot = kzalloc_obj(*snapshot, GFP_ATOMIC);
3200 
3201 	if (!snapshot)
3202 		return NULL;
3203 
3204 	snapshot->guc.id = q->guc->id;
3205 	memcpy(&snapshot->name, &q->name, sizeof(snapshot->name));
3206 	snapshot->class = q->class;
3207 	snapshot->logical_mask = q->logical_mask;
3208 	snapshot->width = q->width;
3209 	snapshot->refcount = kref_read(&q->refcount);
3210 	snapshot->sched_timeout = sched->base.timeout;
3211 	snapshot->sched_props.timeslice_us = q->sched_props.timeslice_us;
3212 	snapshot->sched_props.preempt_timeout_us =
3213 		q->sched_props.preempt_timeout_us;
3214 
3215 	snapshot->lrc = kmalloc_objs(struct xe_lrc_snapshot *, q->width,
3216 				     GFP_ATOMIC);
3217 
3218 	if (snapshot->lrc) {
3219 		for (i = 0; i < q->width; ++i) {
3220 			struct xe_lrc *lrc = q->lrc[i];
3221 
3222 			snapshot->lrc[i] = xe_lrc_snapshot_capture(lrc);
3223 		}
3224 	}
3225 
3226 	snapshot->schedule_state = atomic_read(&q->guc->state);
3227 	snapshot->exec_queue_flags = q->flags;
3228 
3229 	snapshot->parallel_execution = xe_exec_queue_is_parallel(q);
3230 	if (snapshot->parallel_execution)
3231 		guc_exec_queue_wq_snapshot_capture(q, snapshot);
3232 
3233 	if (xe_exec_queue_is_multi_queue(q)) {
3234 		snapshot->multi_queue.valid = true;
3235 		snapshot->multi_queue.primary = xe_exec_queue_multi_queue_primary(q)->guc->id;
3236 		snapshot->multi_queue.pos = q->multi_queue.pos;
3237 	}
3238 
3239 	return snapshot;
3240 }
3241 
3242 /**
3243  * xe_guc_exec_queue_snapshot_capture_delayed - Take delayed part of snapshot of the GuC Engine.
3244  * @snapshot: Previously captured snapshot of job.
3245  *
3246  * This captures some data that requires taking some locks, so it cannot be done in signaling path.
3247  */
3248 void
3249 xe_guc_exec_queue_snapshot_capture_delayed(struct xe_guc_submit_exec_queue_snapshot *snapshot)
3250 {
3251 	int i;
3252 
3253 	if (!snapshot || !snapshot->lrc)
3254 		return;
3255 
3256 	for (i = 0; i < snapshot->width; ++i)
3257 		xe_lrc_snapshot_capture_delayed(snapshot->lrc[i]);
3258 }
3259 
3260 /**
3261  * xe_guc_exec_queue_snapshot_print - Print out a given GuC Engine snapshot.
3262  * @snapshot: GuC Submit Engine snapshot object.
3263  * @p: drm_printer where it will be printed out.
3264  *
3265  * This function prints out a given GuC Submit Engine snapshot object.
3266  */
3267 void
3268 xe_guc_exec_queue_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot,
3269 				 struct drm_printer *p)
3270 {
3271 	int i;
3272 
3273 	if (!snapshot)
3274 		return;
3275 
3276 	drm_printf(p, "GuC ID: %d\n", snapshot->guc.id);
3277 	drm_printf(p, "\tName: %s\n", snapshot->name);
3278 	drm_printf(p, "\tClass: %d\n", snapshot->class);
3279 	drm_printf(p, "\tLogical mask: 0x%x\n", snapshot->logical_mask);
3280 	drm_printf(p, "\tWidth: %d\n", snapshot->width);
3281 	drm_printf(p, "\tRef: %d\n", snapshot->refcount);
3282 	drm_printf(p, "\tTimeout: %ld (ms)\n", snapshot->sched_timeout);
3283 	drm_printf(p, "\tTimeslice: %u (us)\n",
3284 		   snapshot->sched_props.timeslice_us);
3285 	drm_printf(p, "\tPreempt timeout: %u (us)\n",
3286 		   snapshot->sched_props.preempt_timeout_us);
3287 
3288 	for (i = 0; snapshot->lrc && i < snapshot->width; ++i)
3289 		xe_lrc_snapshot_print(snapshot->lrc[i], p);
3290 
3291 	drm_printf(p, "\tSchedule State: 0x%x\n", snapshot->schedule_state);
3292 	drm_printf(p, "\tFlags: 0x%lx\n", snapshot->exec_queue_flags);
3293 
3294 	if (snapshot->parallel_execution)
3295 		guc_exec_queue_wq_snapshot_print(snapshot, p);
3296 
3297 	if (snapshot->multi_queue.valid) {
3298 		drm_printf(p, "\tMulti queue primary GuC ID: %d\n", snapshot->multi_queue.primary);
3299 		drm_printf(p, "\tMulti queue position: %d\n", snapshot->multi_queue.pos);
3300 	}
3301 }
3302 
3303 /**
3304  * xe_guc_exec_queue_snapshot_free - Free all allocated objects for a given
3305  * snapshot.
3306  * @snapshot: GuC Submit Engine snapshot object.
3307  *
3308  * This function free all the memory that needed to be allocated at capture
3309  * time.
3310  */
3311 void xe_guc_exec_queue_snapshot_free(struct xe_guc_submit_exec_queue_snapshot *snapshot)
3312 {
3313 	int i;
3314 
3315 	if (!snapshot)
3316 		return;
3317 
3318 	if (snapshot->lrc) {
3319 		for (i = 0; i < snapshot->width; i++)
3320 			xe_lrc_snapshot_free(snapshot->lrc[i]);
3321 		kfree(snapshot->lrc);
3322 	}
3323 	kfree(snapshot);
3324 }
3325 
3326 static void guc_exec_queue_print(struct xe_exec_queue *q, struct drm_printer *p)
3327 {
3328 	struct xe_guc_submit_exec_queue_snapshot *snapshot;
3329 
3330 	snapshot = xe_guc_exec_queue_snapshot_capture(q);
3331 	xe_guc_exec_queue_snapshot_print(snapshot, p);
3332 	xe_guc_exec_queue_snapshot_free(snapshot);
3333 }
3334 
3335 /**
3336  * xe_guc_register_vf_exec_queue - Register exec queue for a given context type.
3337  * @q: Execution queue
3338  * @ctx_type: Type of the context
3339  *
3340  * This function registers the execution queue with the guc. Special context
3341  * types like GUC_CONTEXT_COMPRESSION_SAVE and GUC_CONTEXT_COMPRESSION_RESTORE
3342  * are only applicable for IGPU and in the VF.
3343  * Submits the execution queue to GUC after registering it.
3344  *
3345  * Returns - None.
3346  */
3347 void xe_guc_register_vf_exec_queue(struct xe_exec_queue *q, int ctx_type)
3348 {
3349 	struct xe_guc *guc = exec_queue_to_guc(q);
3350 	struct xe_device *xe = guc_to_xe(guc);
3351 	struct xe_gt *gt = guc_to_gt(guc);
3352 
3353 	xe_gt_assert(gt, IS_SRIOV_VF(xe));
3354 	xe_gt_assert(gt, !IS_DGFX(xe));
3355 	xe_gt_assert(gt, ctx_type == GUC_CONTEXT_COMPRESSION_SAVE ||
3356 		     ctx_type == GUC_CONTEXT_COMPRESSION_RESTORE);
3357 	xe_gt_assert(gt, GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 23, 0));
3358 
3359 	register_exec_queue(q, ctx_type);
3360 	enable_scheduling(q);
3361 }
3362 
3363 /**
3364  * xe_guc_submit_print - GuC Submit Print.
3365  * @guc: GuC.
3366  * @p: drm_printer where it will be printed out.
3367  *
3368  * This function capture and prints snapshots of **all** GuC Engines.
3369  */
3370 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p)
3371 {
3372 	struct xe_exec_queue *q;
3373 	unsigned long index;
3374 
3375 	if (!xe_device_uc_enabled(guc_to_xe(guc)))
3376 		return;
3377 
3378 	mutex_lock(&guc->submission_state.lock);
3379 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
3380 		guc_exec_queue_print(q, p);
3381 	mutex_unlock(&guc->submission_state.lock);
3382 }
3383 
3384 /**
3385  * xe_guc_has_registered_mlrc_queues - check whether there are any MLRC queues
3386  * registered with the GuC
3387  * @guc: GuC.
3388  *
3389  * Return: true if any MLRC queue is registered with the GuC, false otherwise.
3390  */
3391 bool xe_guc_has_registered_mlrc_queues(struct xe_guc *guc)
3392 {
3393 	struct xe_exec_queue *q;
3394 	unsigned long index;
3395 
3396 	guard(mutex)(&guc->submission_state.lock);
3397 
3398 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
3399 		if (q->width > 1)
3400 			return true;
3401 
3402 	return false;
3403 }
3404 
3405 /**
3406  * xe_guc_contexts_hwsp_rebase - Re-compute GGTT references within all
3407  * exec queues registered to given GuC.
3408  * @guc: the &xe_guc struct instance
3409  * @scratch: scratch buffer to be used as temporary storage
3410  *
3411  * Returns: zero on success, negative error code on failure.
3412  */
3413 int xe_guc_contexts_hwsp_rebase(struct xe_guc *guc, void *scratch)
3414 {
3415 	struct xe_exec_queue *q;
3416 	unsigned long index;
3417 	int err = 0;
3418 
3419 	mutex_lock(&guc->submission_state.lock);
3420 	xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
3421 		/* Prevent redundant attempts to stop parallel queues */
3422 		if (q->guc->id != index)
3423 			continue;
3424 
3425 		err = xe_exec_queue_contexts_hwsp_rebase(q, scratch);
3426 		if (err)
3427 			break;
3428 	}
3429 	mutex_unlock(&guc->submission_state.lock);
3430 
3431 	return err;
3432 }
3433