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 *
exec_queue_to_guc(struct xe_exec_queue * q)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
exec_queue_registered(struct xe_exec_queue * q)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
set_exec_queue_registered(struct xe_exec_queue * q)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
clear_exec_queue_registered(struct xe_exec_queue * q)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
exec_queue_enabled(struct xe_exec_queue * q)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
set_exec_queue_enabled(struct xe_exec_queue * q)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
clear_exec_queue_enabled(struct xe_exec_queue * q)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
exec_queue_pending_enable(struct xe_exec_queue * q)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
set_exec_queue_pending_enable(struct xe_exec_queue * q)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
clear_exec_queue_pending_enable(struct xe_exec_queue * q)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
exec_queue_pending_disable(struct xe_exec_queue * q)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
set_exec_queue_pending_disable(struct xe_exec_queue * q)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
clear_exec_queue_pending_disable(struct xe_exec_queue * q)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
exec_queue_destroyed(struct xe_exec_queue * q)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
set_exec_queue_destroyed(struct xe_exec_queue * q)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
clear_exec_queue_destroyed(struct xe_exec_queue * q)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
exec_queue_banned(struct xe_exec_queue * q)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
set_exec_queue_banned(struct xe_exec_queue * q)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
clear_exec_queue_banned(struct xe_exec_queue * q)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
exec_queue_suspended(struct xe_exec_queue * q)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
set_exec_queue_suspended(struct xe_exec_queue * q)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
clear_exec_queue_suspended(struct xe_exec_queue * q)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
exec_queue_reset(struct xe_exec_queue * q)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
set_exec_queue_reset(struct xe_exec_queue * q)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
exec_queue_killed(struct xe_exec_queue * q)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
set_exec_queue_killed(struct xe_exec_queue * q)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
exec_queue_wedged(struct xe_exec_queue * q)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
set_exec_queue_wedged(struct xe_exec_queue * q)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
exec_queue_pending_resume(struct xe_exec_queue * q)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
set_exec_queue_pending_resume(struct xe_exec_queue * q)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
clear_exec_queue_pending_resume(struct xe_exec_queue * q)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
exec_queue_killed_or_banned_or_wedged(struct xe_exec_queue * q)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
guc_submit_sw_fini(struct drm_device * drm,void * arg)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
guc_submit_fini(void * arg)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
primelockdep(struct xe_guc * guc)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 */
xe_guc_submit_init(struct xe_guc * guc,unsigned int num_ids)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 */
emit_render_compute_yield_klv(u32 * emit)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
guc_init_global_schedule_policy(struct xe_guc * guc)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
xe_guc_submit_enable(struct xe_guc * guc)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
xe_guc_submit_disable(struct xe_guc * guc)387 void xe_guc_submit_disable(struct xe_guc *guc)
388 {
389 guc->submission_state.enabled = false;
390 }
391
__release_guc_id(struct xe_guc * guc,struct xe_exec_queue * q,int count)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
alloc_guc_id(struct xe_guc * guc,struct xe_exec_queue * q)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
publish_guc_id(struct xe_guc * guc,struct xe_exec_queue * q)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
release_guc_id(struct xe_guc * guc,struct xe_exec_queue * q)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
__guc_exec_queue_policy_action_size(struct exec_queue_policy * policy)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
__guc_exec_queue_policy_start_klv(struct exec_queue_policy * policy,u16 guc_id)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
init_policies(struct xe_guc * guc,struct xe_exec_queue * q)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
set_min_preemption_timeout(struct xe_guc * guc,struct xe_exec_queue * q)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
vf_recovery(struct xe_guc * guc)545 static bool vf_recovery(struct xe_guc *guc)
546 {
547 return xe_gt_recovery_pending(guc_to_gt(guc));
548 }
549
xe_guc_exec_queue_trigger_cleanup(struct xe_exec_queue * q)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
xe_guc_exec_queue_group_stop(struct xe_exec_queue * q)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
xe_guc_exec_queue_group_start(struct xe_exec_queue * q)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
xe_guc_exec_queue_group_trigger_cleanup(struct xe_exec_queue * q)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
xe_guc_exec_queue_reset_trigger_cleanup(struct xe_exec_queue * q)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
set_exec_queue_group_banned(struct xe_exec_queue * q)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
xe_guc_exec_queue_group_cgp_update(struct xe_device * xe,struct xe_exec_queue * q)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
xe_guc_exec_queue_group_cgp_sync(struct xe_guc * guc,struct xe_exec_queue * q,const u32 * action,u32 len)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
guc_exec_queue_send_cgp_sync(struct xe_exec_queue * q)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
__register_exec_queue_group(struct xe_exec_queue * q,struct guc_ctxt_registration_info * info)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
__register_mlrc_exec_queue(struct xe_guc * guc,struct xe_exec_queue * q,struct guc_ctxt_registration_info * info)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
__register_exec_queue(struct xe_guc * guc,struct guc_ctxt_registration_info * info)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
xe_hwe_guc_logical_to_submit_mask(struct xe_hw_engine * hwe,u32 logical_mask)964 static u32 xe_hwe_guc_logical_to_submit_mask(struct xe_hw_engine *hwe, u32 logical_mask)
965 {
966 struct xe_gt *gt = hwe->gt;
967
968 if (xe_gt_is_usm_hwe(gt, hwe)) {
969 int shift = gt->usm.paging_hwe0->logical_instance;
970 u32 paging_logical_mask = gt->usm.paging_logical_mask;
971
972 xe_gt_assert(gt, (logical_mask & paging_logical_mask) == logical_mask);
973
974 /*
975 * Remap to GUC_PAGING_CLASS logical instance mask, if
976 * applicable.
977 */
978 if (xe_guc_has_paging_engine(&hwe->gt->uc.guc))
979 return logical_mask >> shift;
980 }
981
982 return logical_mask;
983 }
984
register_exec_queue(struct xe_exec_queue * q,int ctx_type)985 static void register_exec_queue(struct xe_exec_queue *q, int ctx_type)
986 {
987 struct xe_guc *guc = exec_queue_to_guc(q);
988 struct xe_device *xe = guc_to_xe(guc);
989 struct xe_lrc *lrc = q->lrc[0];
990 struct guc_ctxt_registration_info info;
991
992 xe_gt_assert(guc_to_gt(guc), !exec_queue_registered(q));
993 xe_gt_assert(guc_to_gt(guc), ctx_type < GUC_CONTEXT_COUNT);
994
995 memset(&info, 0, sizeof(info));
996 info.context_idx = q->guc->id;
997 info.engine_class = xe_hwe_to_guc_class(q->hwe);
998 info.engine_submit_mask =
999 xe_hwe_guc_logical_to_submit_mask(q->hwe, q->logical_mask);
1000 info.hwlrca_lo = lower_32_bits(xe_lrc_descriptor(lrc));
1001 info.hwlrca_hi = upper_32_bits(xe_lrc_descriptor(lrc));
1002 info.flags = CONTEXT_REGISTRATION_FLAG_KMD |
1003 FIELD_PREP(CONTEXT_REGISTRATION_FLAG_TYPE, ctx_type);
1004
1005 if (xe_exec_queue_is_multi_queue(q)) {
1006 struct xe_exec_queue_group *group = q->multi_queue.group;
1007
1008 info.cgp_lo = xe_bo_ggtt_addr(group->cgp_bo);
1009 info.cgp_hi = 0;
1010 }
1011
1012 if (xe_exec_queue_is_parallel(q)) {
1013 u64 ggtt_addr = xe_lrc_parallel_ggtt_addr(lrc);
1014 struct iosys_map map = xe_lrc_parallel_map(lrc);
1015
1016 info.wq_desc_lo = lower_32_bits(ggtt_addr +
1017 offsetof(struct guc_submit_parallel_scratch, wq_desc));
1018 info.wq_desc_hi = upper_32_bits(ggtt_addr +
1019 offsetof(struct guc_submit_parallel_scratch, wq_desc));
1020 info.wq_base_lo = lower_32_bits(ggtt_addr +
1021 offsetof(struct guc_submit_parallel_scratch, wq[0]));
1022 info.wq_base_hi = upper_32_bits(ggtt_addr +
1023 offsetof(struct guc_submit_parallel_scratch, wq[0]));
1024 info.wq_size = WQ_SIZE;
1025
1026 q->guc->wqi_head = 0;
1027 q->guc->wqi_tail = 0;
1028 xe_map_memset(xe, &map, 0, 0, PARALLEL_SCRATCH_SIZE - WQ_SIZE);
1029 parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
1030 }
1031
1032 set_exec_queue_registered(q);
1033 trace_xe_exec_queue_register(q);
1034 if (xe_exec_queue_is_multi_queue_primary(q))
1035 __register_exec_queue_group(q, &info);
1036 else if (xe_exec_queue_is_parallel(q))
1037 __register_mlrc_exec_queue(guc, q, &info);
1038 else if (!xe_exec_queue_is_multi_queue_secondary(q))
1039 __register_exec_queue(guc, &info);
1040
1041 if (!xe_exec_queue_is_multi_queue_secondary(q))
1042 init_policies(guc, q);
1043
1044 if (xe_exec_queue_is_multi_queue_secondary(q))
1045 guc_exec_queue_send_cgp_sync(q);
1046 }
1047
wq_space_until_wrap(struct xe_exec_queue * q)1048 static u32 wq_space_until_wrap(struct xe_exec_queue *q)
1049 {
1050 return (WQ_SIZE - q->guc->wqi_tail);
1051 }
1052
wq_wait_for_space(struct xe_exec_queue * q,u32 wqi_size)1053 static int wq_wait_for_space(struct xe_exec_queue *q, u32 wqi_size)
1054 {
1055 struct xe_guc *guc = exec_queue_to_guc(q);
1056 struct xe_device *xe = guc_to_xe(guc);
1057 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
1058 unsigned int sleep_period_ms = 1, sleep_total_ms = 0;
1059
1060 #define AVAILABLE_SPACE \
1061 CIRC_SPACE(q->guc->wqi_tail, q->guc->wqi_head, WQ_SIZE)
1062 if (wqi_size > AVAILABLE_SPACE && !vf_recovery(guc)) {
1063 try_again:
1064 q->guc->wqi_head = parallel_read(xe, map, wq_desc.head);
1065 if (wqi_size > AVAILABLE_SPACE && !vf_recovery(guc)) {
1066 if (sleep_total_ms > 2000) {
1067 xe_gt_reset_async(q->gt);
1068 return -ENODEV;
1069 }
1070
1071 sleep_total_ms += xe_sleep_exponential_ms(&sleep_period_ms, 64);
1072 goto try_again;
1073 }
1074 }
1075 #undef AVAILABLE_SPACE
1076
1077 return 0;
1078 }
1079
wq_noop_append(struct xe_exec_queue * q)1080 static int wq_noop_append(struct xe_exec_queue *q)
1081 {
1082 struct xe_guc *guc = exec_queue_to_guc(q);
1083 struct xe_device *xe = guc_to_xe(guc);
1084 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
1085 u32 len_dw = wq_space_until_wrap(q) / sizeof(u32) - 1;
1086
1087 if (wq_wait_for_space(q, wq_space_until_wrap(q)))
1088 return -ENODEV;
1089
1090 xe_gt_assert(guc_to_gt(guc), FIELD_FIT(WQ_LEN_MASK, len_dw));
1091
1092 parallel_write(xe, map, wq[q->guc->wqi_tail / sizeof(u32)],
1093 FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
1094 FIELD_PREP(WQ_LEN_MASK, len_dw));
1095 q->guc->wqi_tail = 0;
1096
1097 return 0;
1098 }
1099
wq_item_append(struct xe_exec_queue * q)1100 static void wq_item_append(struct xe_exec_queue *q)
1101 {
1102 struct xe_guc *guc = exec_queue_to_guc(q);
1103 struct xe_device *xe = guc_to_xe(guc);
1104 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
1105 #define WQ_HEADER_SIZE 4 /* Includes 1 LRC address too */
1106 u32 wqi[XE_HW_ENGINE_MAX_INSTANCE + (WQ_HEADER_SIZE - 1)];
1107 u32 wqi_size = (q->width + (WQ_HEADER_SIZE - 1)) * sizeof(u32);
1108 u32 len_dw = (wqi_size / sizeof(u32)) - 1;
1109 int i = 0, j;
1110
1111 if (wqi_size > wq_space_until_wrap(q)) {
1112 if (wq_noop_append(q))
1113 return;
1114 }
1115 if (wq_wait_for_space(q, wqi_size))
1116 return;
1117
1118 wqi[i++] = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) |
1119 FIELD_PREP(WQ_LEN_MASK, len_dw);
1120 wqi[i++] = xe_lrc_descriptor(q->lrc[0]);
1121 wqi[i++] = FIELD_PREP(WQ_GUC_ID_MASK, q->guc->id) |
1122 FIELD_PREP(WQ_RING_TAIL_MASK, q->lrc[0]->ring.tail / sizeof(u64));
1123 wqi[i++] = 0;
1124 for (j = 1; j < q->width; ++j) {
1125 struct xe_lrc *lrc = q->lrc[j];
1126
1127 wqi[i++] = lrc->ring.tail / sizeof(u64);
1128 }
1129
1130 xe_gt_assert(guc_to_gt(guc), i == wqi_size / sizeof(u32));
1131
1132 iosys_map_incr(&map, offsetof(struct guc_submit_parallel_scratch,
1133 wq[q->guc->wqi_tail / sizeof(u32)]));
1134 xe_map_memcpy_to(xe, &map, 0, wqi, wqi_size);
1135 q->guc->wqi_tail += wqi_size;
1136 xe_gt_assert(guc_to_gt(guc), q->guc->wqi_tail <= WQ_SIZE);
1137
1138 xe_device_wmb(xe);
1139
1140 map = xe_lrc_parallel_map(q->lrc[0]);
1141 parallel_write(xe, map, wq_desc.tail, q->guc->wqi_tail);
1142 }
1143
1144 #define RESUME_PENDING ~0x0ull
submit_exec_queue(struct xe_exec_queue * q,struct xe_sched_job * job)1145 static void submit_exec_queue(struct xe_exec_queue *q, struct xe_sched_job *job)
1146 {
1147 struct xe_guc *guc = exec_queue_to_guc(q);
1148 struct xe_lrc *lrc = q->lrc[0];
1149 u32 action[3];
1150 u32 g2h_len = 0;
1151 u32 num_g2h = 0;
1152 int len = 0;
1153 bool extra_submit = false;
1154
1155 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
1156
1157 if (!job->restore_replay || job->last_replay) {
1158 if (xe_exec_queue_is_parallel(q))
1159 wq_item_append(q);
1160 else
1161 xe_lrc_set_ring_tail(lrc, lrc->ring.tail);
1162 job->last_replay = false;
1163 }
1164
1165 if (exec_queue_suspended(q) && !xe_exec_queue_is_parallel(q))
1166 return;
1167
1168 /*
1169 * All queues in a multi-queue group will use the primary queue
1170 * of the group to interface with GuC. If primay is suspended,
1171 * just return. Jobs will get scheduled once primary is resumed.
1172 */
1173 q = xe_exec_queue_multi_queue_primary(q);
1174 if (exec_queue_suspended(q))
1175 return;
1176
1177 if (!exec_queue_enabled(q)) {
1178 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
1179 action[len++] = q->guc->id;
1180 action[len++] = GUC_CONTEXT_ENABLE;
1181 g2h_len = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
1182 num_g2h = 1;
1183 if (xe_exec_queue_is_parallel(q))
1184 extra_submit = true;
1185
1186 q->guc->resume_time = RESUME_PENDING;
1187 set_exec_queue_pending_enable(q);
1188 set_exec_queue_enabled(q);
1189 trace_xe_exec_queue_scheduling_enable(q);
1190 } else {
1191 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT;
1192 action[len++] = q->guc->id;
1193 trace_xe_exec_queue_submit(q);
1194 }
1195
1196 xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
1197
1198 if (extra_submit) {
1199 len = 0;
1200 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT;
1201 action[len++] = q->guc->id;
1202 trace_xe_exec_queue_submit(q);
1203
1204 xe_guc_ct_send(&guc->ct, action, len, 0, 0);
1205 }
1206 }
1207
1208 static struct dma_fence *
guc_exec_queue_run_job(struct drm_sched_job * drm_job)1209 guc_exec_queue_run_job(struct drm_sched_job *drm_job)
1210 {
1211 struct xe_sched_job *job = to_xe_sched_job(drm_job);
1212 struct xe_exec_queue *q = job->q;
1213 struct xe_guc *guc = exec_queue_to_guc(q);
1214 bool killed_or_banned_or_wedged =
1215 exec_queue_killed_or_banned_or_wedged(q);
1216
1217 xe_gt_assert(guc_to_gt(guc), !(exec_queue_destroyed(q) || exec_queue_pending_disable(q)) ||
1218 exec_queue_banned(q) || exec_queue_suspended(q));
1219
1220 trace_xe_sched_job_run(job);
1221
1222 if (!killed_or_banned_or_wedged && !xe_sched_job_is_error(job)) {
1223 if (xe_exec_queue_is_multi_queue_secondary(q)) {
1224 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
1225
1226 if (exec_queue_killed_or_banned_or_wedged(primary))
1227 goto run_job_out;
1228
1229 if (!exec_queue_registered(primary))
1230 register_exec_queue(primary, GUC_CONTEXT_NORMAL);
1231 }
1232
1233 if (!exec_queue_registered(q))
1234 register_exec_queue(q, GUC_CONTEXT_NORMAL);
1235 if (!job->restore_replay)
1236 q->ring_ops->emit_job(job);
1237 submit_exec_queue(q, job);
1238 job->restore_replay = false;
1239 }
1240
1241 run_job_out:
1242
1243 return job->fence;
1244 }
1245
guc_exec_queue_free_job(struct drm_sched_job * drm_job)1246 static void guc_exec_queue_free_job(struct drm_sched_job *drm_job)
1247 {
1248 struct xe_sched_job *job = to_xe_sched_job(drm_job);
1249
1250 trace_xe_sched_job_free(job);
1251 xe_sched_job_put(job);
1252 }
1253
xe_guc_read_stopped(struct xe_guc * guc)1254 int xe_guc_read_stopped(struct xe_guc *guc)
1255 {
1256 return atomic_read(&guc->submission_state.stopped);
1257 }
1258
1259 static void handle_multi_queue_secondary_sched_done(struct xe_guc *guc,
1260 struct xe_exec_queue *q,
1261 u32 runnable_state);
1262 static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q);
1263
1264 #define MAKE_SCHED_CONTEXT_ACTION(q, enable_disable) \
1265 u32 action[] = { \
1266 XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET, \
1267 q->guc->id, \
1268 GUC_CONTEXT_##enable_disable, \
1269 }
1270
disable_scheduling_deregister(struct xe_guc * guc,struct xe_exec_queue * q)1271 static void disable_scheduling_deregister(struct xe_guc *guc,
1272 struct xe_exec_queue *q)
1273 {
1274 MAKE_SCHED_CONTEXT_ACTION(q, DISABLE);
1275 int ret;
1276
1277 if (!xe_exec_queue_is_multi_queue_secondary(q))
1278 set_min_preemption_timeout(guc, q);
1279
1280 smp_rmb();
1281 ret = wait_event_timeout(guc->ct.wq,
1282 (!exec_queue_pending_enable(q) &&
1283 !exec_queue_pending_disable(q)) ||
1284 xe_guc_read_stopped(guc) ||
1285 vf_recovery(guc),
1286 HZ * 5);
1287 if (!ret && !vf_recovery(guc)) {
1288 struct xe_gpu_scheduler *sched = &q->guc->sched;
1289
1290 xe_gt_warn(q->gt, "Pending enable/disable failed to respond\n");
1291 xe_sched_submission_start(sched);
1292 xe_gt_reset_async(q->gt);
1293 xe_sched_tdr_queue_imm(sched);
1294 return;
1295 }
1296
1297 clear_exec_queue_enabled(q);
1298 set_exec_queue_pending_disable(q);
1299 set_exec_queue_destroyed(q);
1300 trace_xe_exec_queue_scheduling_disable(q);
1301
1302 /*
1303 * Reserve space for both G2H here as the 2nd G2H is sent from a G2H
1304 * handler and we are not allowed to reserved G2H space in handlers.
1305 */
1306 if (xe_exec_queue_is_multi_queue_secondary(q))
1307 handle_multi_queue_secondary_sched_done(guc, q, 0);
1308 else
1309 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
1310 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET +
1311 G2H_LEN_DW_DEREGISTER_CONTEXT, 2);
1312 }
1313
1314 /**
1315 * xe_guc_submit_wedge() - Wedge GuC submission
1316 * @guc: the GuC object
1317 *
1318 * Save exec queue's registered with GuC state by taking a ref to each queue.
1319 * Register a DRMM handler to drop refs upon driver unload.
1320 */
xe_guc_submit_wedge(struct xe_guc * guc)1321 void xe_guc_submit_wedge(struct xe_guc *guc)
1322 {
1323 struct xe_device *xe = guc_to_xe(guc);
1324 struct xe_exec_queue *q;
1325 unsigned long index;
1326
1327 xe_gt_assert(guc_to_gt(guc), guc_to_xe(guc)->wedged.mode);
1328
1329 /*
1330 * If device is being wedged even before submission_state is
1331 * initialized, there's nothing to do here.
1332 */
1333 if (!guc->submission_state.initialized)
1334 return;
1335
1336 if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) {
1337 mutex_lock(&guc->submission_state.lock);
1338 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
1339 if (xe_exec_queue_get_unless_zero(q))
1340 set_exec_queue_wedged(q);
1341 mutex_unlock(&guc->submission_state.lock);
1342 } else {
1343 /* Forcefully kill any remaining exec queues, signal fences */
1344 guc_submit_reset_prepare(guc);
1345 xe_guc_submit_stop(guc);
1346 xe_guc_softreset(guc);
1347 xe_uc_fw_sanitize(&guc->fw);
1348 xe_guc_submit_pause_abort(guc);
1349 }
1350 }
1351
guc_submit_hint_wedged(struct xe_guc * guc)1352 static bool guc_submit_hint_wedged(struct xe_guc *guc)
1353 {
1354 struct xe_device *xe = guc_to_xe(guc);
1355
1356 if (xe->wedged.mode != XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET)
1357 return false;
1358
1359 if (xe_device_wedged(xe))
1360 return true;
1361
1362 xe_device_declare_wedged(xe);
1363
1364 return true;
1365 }
1366
1367 #define ADJUST_FIVE_PERCENT(__t) mul_u64_u32_div(__t, 105, 100)
1368
check_timeout(struct xe_exec_queue * q,struct xe_sched_job * job)1369 static bool check_timeout(struct xe_exec_queue *q, struct xe_sched_job *job)
1370 {
1371 struct xe_gt *gt = guc_to_gt(exec_queue_to_guc(q));
1372 u32 ctx_timestamp, ctx_job_timestamp;
1373 u32 timeout_ms = q->sched_props.job_timeout_ms;
1374 u32 diff;
1375 u64 running_time_ms;
1376
1377 if (!xe_sched_job_started(job)) {
1378 xe_gt_warn(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, not started",
1379 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
1380 q->guc->id);
1381
1382 /* GuC never scheduled this job - let the caller trigger a GT reset. */
1383 return true;
1384 }
1385
1386 ctx_timestamp = lower_32_bits(xe_lrc_timestamp(q->lrc[0]));
1387 if (ctx_timestamp == job->sample_timestamp) {
1388 if (IS_SRIOV_VF(gt_to_xe(gt)))
1389 xe_gt_notice(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, timestamp stuck",
1390 xe_sched_job_seqno(job),
1391 xe_sched_job_lrc_seqno(job), q->guc->id);
1392 else
1393 xe_gt_warn(gt, "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, timestamp stuck",
1394 xe_sched_job_seqno(job),
1395 xe_sched_job_lrc_seqno(job), q->guc->id);
1396
1397 return xe_sched_invalidate_job(job, 0);
1398 }
1399
1400 job->sample_timestamp = ctx_timestamp;
1401 ctx_job_timestamp = xe_lrc_ctx_job_timestamp(q->lrc[0]);
1402
1403 /*
1404 * Counter wraps at ~223s at the usual 19.2MHz, be paranoid catch
1405 * possible overflows with a high timeout.
1406 */
1407 xe_gt_assert(gt, timeout_ms < 100 * MSEC_PER_SEC);
1408
1409 diff = ctx_timestamp - ctx_job_timestamp;
1410
1411 /*
1412 * Ensure timeout is within 5% to account for an GuC scheduling latency
1413 */
1414 running_time_ms =
1415 ADJUST_FIVE_PERCENT(xe_gt_clock_interval_to_ms(gt, diff));
1416
1417 xe_gt_dbg(gt,
1418 "Check job timeout: seqno=%u, lrc_seqno=%u, guc_id=%d, running_time_ms=%llu, timeout_ms=%u, diff=0x%08x",
1419 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
1420 q->guc->id, running_time_ms, timeout_ms, diff);
1421
1422 return running_time_ms >= timeout_ms;
1423 }
1424
enable_scheduling(struct xe_exec_queue * q)1425 static void enable_scheduling(struct xe_exec_queue *q)
1426 {
1427 MAKE_SCHED_CONTEXT_ACTION(q, ENABLE);
1428 struct xe_guc *guc = exec_queue_to_guc(q);
1429 int ret;
1430
1431 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
1432 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
1433 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
1434 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q));
1435
1436 set_exec_queue_pending_enable(q);
1437 set_exec_queue_enabled(q);
1438 trace_xe_exec_queue_scheduling_enable(q);
1439
1440 if (xe_exec_queue_is_multi_queue_secondary(q))
1441 handle_multi_queue_secondary_sched_done(guc, q, 1);
1442 else
1443 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
1444 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
1445
1446 ret = wait_event_timeout(guc->ct.wq,
1447 !exec_queue_pending_enable(q) ||
1448 xe_guc_read_stopped(guc) ||
1449 vf_recovery(guc), HZ * 5);
1450 if ((!ret && !vf_recovery(guc)) || xe_guc_read_stopped(guc)) {
1451 xe_gt_warn(guc_to_gt(guc), "Schedule enable failed to respond");
1452 set_exec_queue_banned(q);
1453 xe_gt_reset_async(q->gt);
1454 xe_sched_tdr_queue_imm(&q->guc->sched);
1455 }
1456 }
1457
disable_scheduling(struct xe_exec_queue * q,bool immediate)1458 static void disable_scheduling(struct xe_exec_queue *q, bool immediate)
1459 {
1460 MAKE_SCHED_CONTEXT_ACTION(q, DISABLE);
1461 struct xe_guc *guc = exec_queue_to_guc(q);
1462
1463 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
1464 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
1465 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
1466
1467 if (immediate && !xe_exec_queue_is_multi_queue_secondary(q))
1468 set_min_preemption_timeout(guc, q);
1469 clear_exec_queue_enabled(q);
1470 set_exec_queue_pending_disable(q);
1471 trace_xe_exec_queue_scheduling_disable(q);
1472
1473 if (xe_exec_queue_is_multi_queue_secondary(q))
1474 handle_multi_queue_secondary_sched_done(guc, q, 0);
1475 else
1476 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
1477 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
1478 }
1479
1480 /*
1481 * Recover via GT reset for a kernel queue, or for a GuC scheduling failure (job
1482 * never started) on a queue that was not already killed or banned. An already
1483 * banned queue must stay banned, so its unstarted jobs do not clear the ban or
1484 * trigger a reset.
1485 */
timeout_needs_gt_reset(struct xe_exec_queue * q,struct xe_sched_job * job,bool skip_timeout_check)1486 static bool timeout_needs_gt_reset(struct xe_exec_queue *q, struct xe_sched_job *job,
1487 bool skip_timeout_check)
1488 {
1489 if (q->flags & EXEC_QUEUE_FLAG_KERNEL)
1490 return true;
1491
1492 return !skip_timeout_check && !xe_sched_job_started(job);
1493 }
1494
1495 static enum drm_gpu_sched_stat
guc_exec_queue_timedout_job(struct drm_sched_job * drm_job)1496 guc_exec_queue_timedout_job(struct drm_sched_job *drm_job)
1497 {
1498 struct xe_sched_job *job = to_xe_sched_job(drm_job);
1499 struct drm_sched_job *tmp_job;
1500 struct xe_exec_queue *q = job->q, *primary;
1501 struct xe_gpu_scheduler *sched = &q->guc->sched;
1502 struct xe_guc *guc = exec_queue_to_guc(q);
1503 const char *process_name = "no process";
1504 struct xe_device *xe = guc_to_xe(guc);
1505 int err = -ETIME;
1506 pid_t pid = -1;
1507 bool wedged = false, wedge_device = false, skip_timeout_check;
1508
1509 xe_gt_assert(guc_to_gt(guc), !exec_queue_destroyed(q));
1510
1511 primary = xe_exec_queue_multi_queue_primary(q);
1512
1513 /*
1514 * TDR has fired before free job worker. Common if exec queue
1515 * immediately closed after last fence signaled. Add back to pending
1516 * list so job can be freed and kick scheduler ensuring free job is not
1517 * lost.
1518 */
1519 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &job->fence->flags) ||
1520 vf_recovery(guc))
1521 return DRM_GPU_SCHED_STAT_NO_HANG;
1522
1523 /* Kill the run_job entry point */
1524 if (xe_exec_queue_is_multi_queue(q))
1525 xe_guc_exec_queue_group_stop(q);
1526 else
1527 xe_sched_submission_stop(sched);
1528
1529 /* Must check all state after stopping scheduler */
1530 skip_timeout_check = exec_queue_reset(q) ||
1531 exec_queue_killed_or_banned_or_wedged(q);
1532
1533 /* Skip timeout check if multi-queue group is banned */
1534 if (xe_exec_queue_is_multi_queue(q) &&
1535 READ_ONCE(q->multi_queue.group->banned))
1536 skip_timeout_check = true;
1537
1538 /* LR jobs can only get here if queue has been killed or hit an error */
1539 if (xe_exec_queue_is_lr(q))
1540 xe_gt_assert(guc_to_gt(guc), skip_timeout_check);
1541
1542 /*
1543 * If devcoredump not captured and GuC capture for the job is not ready
1544 * do manual capture first and decide later if we need to use it
1545 */
1546 if (!xe_device_is_in_reset(xe) && !exec_queue_killed(q) && !xe->devcoredump.captured &&
1547 !xe_guc_capture_get_matching_and_lock(q)) {
1548 /* take force wake before engine register manual capture */
1549 CLASS(xe_force_wake, fw_ref)(gt_to_fw(q->gt), XE_FORCEWAKE_ALL);
1550 if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL))
1551 xe_gt_info(q->gt, "failed to get forcewake for coredump capture\n");
1552
1553 xe_engine_snapshot_capture_for_queue(q);
1554 }
1555
1556 /*
1557 * Check if job is actually timed out, if so restart job execution and TDR
1558 */
1559 if (!skip_timeout_check && !check_timeout(q, job))
1560 goto rearm;
1561
1562 if (!exec_queue_killed(q))
1563 wedged = guc_submit_hint_wedged(exec_queue_to_guc(q));
1564
1565 set_exec_queue_banned(q);
1566
1567 /* Kick job / queue off hardware */
1568 if (!xe_device_is_in_reset(xe) && !wedged &&
1569 (exec_queue_enabled(primary) || exec_queue_pending_disable(primary))) {
1570 int ret;
1571
1572 if (exec_queue_reset(primary))
1573 err = -EIO;
1574
1575 if (xe_uc_fw_is_running(&guc->fw)) {
1576 /*
1577 * Wait for any pending G2H to flush out before
1578 * modifying state
1579 */
1580 ret = wait_event_timeout(guc->ct.wq,
1581 (!exec_queue_pending_enable(primary) &&
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 goto trigger_reset;
1589
1590 disable_scheduling(primary, skip_timeout_check);
1591 }
1592
1593 /*
1594 * Must wait for scheduling to be disabled before signalling
1595 * any fences, if GT broken the GT reset code should signal us.
1596 *
1597 * FIXME: Tests can generate a ton of 0x6000 (IOMMU CAT fault
1598 * error) messages which can cause the schedule disable to get
1599 * lost. If this occurs, trigger a GT reset to recover.
1600 */
1601 smp_rmb();
1602 ret = wait_event_timeout(guc->ct.wq,
1603 !xe_uc_fw_is_running(&guc->fw) ||
1604 !exec_queue_pending_disable(primary) ||
1605 xe_guc_read_stopped(guc) ||
1606 vf_recovery(guc), HZ * 5);
1607 if (vf_recovery(guc))
1608 goto handle_vf_resume;
1609 if (!ret || xe_guc_read_stopped(guc)) {
1610 trigger_reset:
1611 if (!ret)
1612 xe_gt_warn(guc_to_gt(guc),
1613 "Schedule disable failed to respond, guc_id=%d",
1614 primary->guc->id);
1615 xe_devcoredump(primary, job,
1616 "Schedule disable failed to respond, guc_id=%d, ret=%d, guc_read=%d",
1617 primary->guc->id, ret, xe_guc_read_stopped(guc));
1618 xe_gt_reset_async(primary->gt);
1619 xe_sched_tdr_queue_imm(sched);
1620 goto rearm;
1621 }
1622 }
1623
1624 if (q->vm && q->vm->xef) {
1625 process_name = q->vm->xef->process_name;
1626 pid = q->vm->xef->pid;
1627 }
1628
1629 if (!exec_queue_killed(q))
1630 xe_gt_notice(guc_to_gt(guc),
1631 "Timedout job: seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx in %s [%d]",
1632 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
1633 q->guc->id, q->flags, process_name, pid);
1634
1635 trace_xe_sched_job_timedout(job);
1636
1637 /* Do not access device if in reset */
1638 if (!xe_device_is_in_reset(xe) && !exec_queue_killed(q))
1639 xe_devcoredump(q, job,
1640 "Timedout job - seqno=%u, lrc_seqno=%u, guc_id=%d, flags=0x%lx",
1641 xe_sched_job_seqno(job), xe_sched_job_lrc_seqno(job),
1642 q->guc->id, q->flags);
1643
1644 if (!wedged) {
1645 if (timeout_needs_gt_reset(q, job, skip_timeout_check)) {
1646 if (!xe_sched_invalidate_job(job, 2)) {
1647 clear_exec_queue_banned(q);
1648 xe_gt_reset_async(q->gt);
1649 goto rearm;
1650 }
1651 if (q->flags & EXEC_QUEUE_FLAG_KERNEL) {
1652 xe_gt_WARN(q->gt, true, "Kernel-submitted job timed out\n");
1653 wedge_device = true;
1654 }
1655 } else if (q->flags & EXEC_QUEUE_FLAG_VM && !exec_queue_killed(q)) {
1656 xe_gt_WARN(q->gt, true, "VM job timed out on non-killed execqueue\n");
1657 }
1658 }
1659
1660 /* Mark all outstanding jobs as bad, thus completing them */
1661 xe_sched_job_set_error(job, err);
1662 drm_sched_for_each_pending_job(tmp_job, &sched->base, NULL)
1663 xe_sched_job_set_error(to_xe_sched_job(tmp_job), -ECANCELED);
1664
1665 if (xe_exec_queue_is_multi_queue(q)) {
1666 xe_guc_exec_queue_group_start(q);
1667 xe_guc_exec_queue_group_trigger_cleanup(q);
1668 } else {
1669 xe_sched_submission_start(sched);
1670 xe_guc_exec_queue_trigger_cleanup(q);
1671 }
1672
1673 if (wedge_device)
1674 xe_device_declare_wedged(gt_to_xe(q->gt));
1675
1676 /*
1677 * We want the job added back to the pending list so it gets freed; this
1678 * is what DRM_GPU_SCHED_STAT_NO_HANG does.
1679 */
1680 return DRM_GPU_SCHED_STAT_NO_HANG;
1681
1682 rearm:
1683 /*
1684 * XXX: Ideally want to adjust timeout based on current execution time
1685 * but there is not currently an easy way to do in DRM scheduler. With
1686 * some thought, do this in a follow up.
1687 */
1688 if (xe_exec_queue_is_multi_queue(q))
1689 xe_guc_exec_queue_group_start(q);
1690 else
1691 xe_sched_submission_start(sched);
1692 handle_vf_resume:
1693 return DRM_GPU_SCHED_STAT_NO_HANG;
1694 }
1695
1696 static void guc_exec_queue_multi_queue_drop_suspend(struct xe_exec_queue *q);
1697 static int guc_exec_queue_suspend_wait_blocking(struct xe_exec_queue *q);
1698
guc_exec_queue_fini(struct xe_exec_queue * q)1699 static void guc_exec_queue_fini(struct xe_exec_queue *q)
1700 {
1701 struct xe_guc_exec_queue *ge = q->guc;
1702 struct xe_guc *guc = exec_queue_to_guc(q);
1703 struct drm_device *drm = &guc_to_xe(guc)->drm;
1704
1705 /*
1706 * A secondary can leave the group while still preempt suspended (e.g.
1707 * xe_vm_remove_compute_exec_queue() forces its preempt fence to signal,
1708 * which suspends it). It holds one forwarded suspend reference on the
1709 * primary, so drop it and resume the primary if it was the last member
1710 * that had it suspended. Primaries forward to nobody, so they don't need
1711 * this.
1712 *
1713 * First make sure the primary's forwarded suspend has completed. If the
1714 * secondary was killed/reset before its preempt fence worker ran, that
1715 * worker skips suspend_wait() (see preempt_fence_work_func()), leaving
1716 * the primary's suspend possibly in flight. drop_suspend() runs under a
1717 * spinlock and cannot wait, so drain it here with the uninterruptible
1718 * blocking wait; otherwise resuming the primary in drop_suspend() could
1719 * trip the !suspend_pending assert.
1720 */
1721 if (xe_exec_queue_is_multi_queue_secondary(q)) {
1722 if (READ_ONCE(q->guc->suspend_count))
1723 guc_exec_queue_suspend_wait_blocking(q);
1724 guc_exec_queue_multi_queue_drop_suspend(q);
1725 }
1726
1727 if (xe_exec_queue_is_multi_queue_secondary(q)) {
1728 struct xe_exec_queue_group *group = q->multi_queue.group;
1729
1730 mutex_lock(&group->list_lock);
1731 list_del(&q->multi_queue.link);
1732 mutex_unlock(&group->list_lock);
1733 }
1734
1735 release_guc_id(guc, q);
1736 xe_sched_entity_fini(&ge->entity);
1737 xe_sched_fini(&ge->sched);
1738
1739 /*
1740 * RCU free due sched being exported via DRM scheduler fences
1741 * (timeline name).
1742 */
1743 kfree_rcu(ge, rcu);
1744
1745 drm_dev_put(drm);
1746 }
1747
guc_exec_queue_do_destroy(struct xe_exec_queue * q)1748 static void guc_exec_queue_do_destroy(struct xe_exec_queue *q)
1749 {
1750 struct xe_guc_exec_queue *ge = q->guc;
1751 struct xe_guc *guc = exec_queue_to_guc(q);
1752 struct xe_device *xe = guc_to_xe(guc);
1753 struct drm_device *drm = &xe->drm;
1754
1755 /*
1756 * guc_exec_queue_fini() drops the queue's drm_device ref.
1757 * Keep the device alive until the PM-runtime guard unwinds.
1758 */
1759 drm_dev_get(drm);
1760
1761 scoped_guard(xe_pm_runtime, xe) {
1762 trace_xe_exec_queue_destroy(q);
1763
1764 /* Confirm no work left behind accessing device structures */
1765 cancel_delayed_work_sync(&ge->sched.base.work_tdr);
1766
1767 xe_exec_queue_fini(q);
1768 }
1769
1770 drm_dev_put(drm);
1771 }
1772
__guc_exec_queue_destroy_async(struct work_struct * w)1773 static void __guc_exec_queue_destroy_async(struct work_struct *w)
1774 {
1775 struct xe_guc_exec_queue *ge =
1776 container_of(w, struct xe_guc_exec_queue, destroy_async);
1777
1778 guc_exec_queue_do_destroy(ge->q);
1779 }
1780
guc_exec_queue_destroy_async(struct xe_exec_queue * q)1781 static void guc_exec_queue_destroy_async(struct xe_exec_queue *q)
1782 {
1783 INIT_WORK(&q->guc->destroy_async, __guc_exec_queue_destroy_async);
1784
1785 /* We must block on kernel engines so slabs are empty on driver unload */
1786 if (q->flags & EXEC_QUEUE_FLAG_PERMANENT || exec_queue_wedged(q))
1787 guc_exec_queue_do_destroy(q);
1788 else
1789 xe_destroy_wq_queue(&q->guc->destroy_async);
1790 }
1791
__guc_exec_queue_destroy(struct xe_guc * guc,struct xe_exec_queue * q)1792 static void __guc_exec_queue_destroy(struct xe_guc *guc, struct xe_exec_queue *q)
1793 {
1794 /*
1795 * Might be done from within the GPU scheduler, need to do async as we
1796 * fini the scheduler when the engine is fini'd, the scheduler can't
1797 * complete fini within itself (circular dependency). Async resolves
1798 * this we and don't really care when everything is fini'd, just that it
1799 * is.
1800 */
1801 guc_exec_queue_destroy_async(q);
1802 }
1803
__guc_exec_queue_process_msg_cleanup(struct xe_sched_msg * msg)1804 static void __guc_exec_queue_process_msg_cleanup(struct xe_sched_msg *msg)
1805 {
1806 struct xe_exec_queue *q = msg->private_data;
1807 struct xe_guc *guc = exec_queue_to_guc(q);
1808
1809 xe_gt_assert(guc_to_gt(guc), !(q->flags & EXEC_QUEUE_FLAG_PERMANENT));
1810 trace_xe_exec_queue_cleanup_entity(q);
1811
1812 /*
1813 * Expected state transitions for cleanup:
1814 * - If the exec queue is registered and GuC firmware is running, we must first
1815 * disable scheduling and deregister the queue to ensure proper teardown and
1816 * resource release in the GuC, then destroy the exec queue on driver side.
1817 * - If the GuC is already stopped (e.g., during driver unload or GPU reset),
1818 * we cannot expect a response for the deregister request. In this case,
1819 * it is safe to directly destroy the exec queue on driver side, as the GuC
1820 * will not process further requests and all resources must be cleaned up locally.
1821 */
1822 if (exec_queue_registered(q) && xe_uc_fw_is_running(&guc->fw))
1823 disable_scheduling_deregister(guc, q);
1824 else
1825 __guc_exec_queue_destroy(guc, q);
1826 }
1827
guc_exec_queue_allowed_to_change_state(struct xe_exec_queue * q)1828 static bool guc_exec_queue_allowed_to_change_state(struct xe_exec_queue *q)
1829 {
1830 return !exec_queue_killed_or_banned_or_wedged(q) && exec_queue_registered(q);
1831 }
1832
__guc_exec_queue_process_msg_set_sched_props(struct xe_sched_msg * msg)1833 static void __guc_exec_queue_process_msg_set_sched_props(struct xe_sched_msg *msg)
1834 {
1835 struct xe_exec_queue *q = msg->private_data;
1836 struct xe_guc *guc = exec_queue_to_guc(q);
1837
1838 if (guc_exec_queue_allowed_to_change_state(q))
1839 init_policies(guc, q);
1840 kfree(msg);
1841 }
1842
__suspend_fence_signal(struct xe_exec_queue * q)1843 static void __suspend_fence_signal(struct xe_exec_queue *q)
1844 {
1845 struct xe_guc *guc = exec_queue_to_guc(q);
1846 struct xe_device *xe = guc_to_xe(guc);
1847
1848 if (!q->guc->suspend_pending)
1849 return;
1850
1851 WRITE_ONCE(q->guc->suspend_pending, false);
1852
1853 /*
1854 * We use a GuC shared wait queue for VFs because the VF resfix start
1855 * interrupt must be able to wake all instances of suspend_wait. This
1856 * prevents the VF migration worker from being starved during
1857 * scheduling.
1858 */
1859 if (IS_SRIOV_VF(xe))
1860 wake_up_all(&guc->ct.wq);
1861 else
1862 wake_up(&q->guc->suspend_wait);
1863 }
1864
suspend_fence_signal(struct xe_exec_queue * q)1865 static void suspend_fence_signal(struct xe_exec_queue *q)
1866 {
1867 struct xe_guc *guc = exec_queue_to_guc(q);
1868
1869 xe_gt_assert(guc_to_gt(guc), exec_queue_suspended(q) || exec_queue_killed(q) ||
1870 xe_guc_read_stopped(guc));
1871 xe_gt_assert(guc_to_gt(guc), q->guc->suspend_pending);
1872
1873 __suspend_fence_signal(q);
1874 }
1875
__guc_exec_queue_process_msg_suspend(struct xe_sched_msg * msg)1876 static void __guc_exec_queue_process_msg_suspend(struct xe_sched_msg *msg)
1877 {
1878 struct xe_exec_queue *q = msg->private_data;
1879 struct xe_guc *guc = exec_queue_to_guc(q);
1880
1881 if (guc_exec_queue_allowed_to_change_state(q) && !exec_queue_suspended(q) &&
1882 exec_queue_enabled(q)) {
1883 wait_event(guc->ct.wq, vf_recovery(guc) ||
1884 ((q->guc->resume_time != RESUME_PENDING ||
1885 xe_guc_read_stopped(guc)) && !exec_queue_pending_disable(q)));
1886
1887 if (!xe_guc_read_stopped(guc)) {
1888 s64 since_resume_ms =
1889 ktime_ms_delta(ktime_get(),
1890 q->guc->resume_time);
1891 s64 wait_ms = q->vm->preempt.min_run_period_ms -
1892 since_resume_ms;
1893
1894 if (wait_ms > 0 && q->guc->resume_time)
1895 xe_sleep_relaxed_ms(wait_ms);
1896
1897 set_exec_queue_suspended(q);
1898 disable_scheduling(q, false);
1899 }
1900 } else if (q->guc->suspend_pending) {
1901 set_exec_queue_suspended(q);
1902 suspend_fence_signal(q);
1903 }
1904 }
1905
__guc_exec_queue_process_msg_resume(struct xe_sched_msg * msg)1906 static void __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
1907 {
1908 struct xe_exec_queue *q = msg->private_data;
1909
1910 if (guc_exec_queue_allowed_to_change_state(q)) {
1911 clear_exec_queue_suspended(q);
1912 if (!exec_queue_enabled(q)) {
1913 q->guc->resume_time = RESUME_PENDING;
1914 set_exec_queue_pending_resume(q);
1915 enable_scheduling(q);
1916 }
1917 } else {
1918 clear_exec_queue_suspended(q);
1919 }
1920 }
1921
__guc_exec_queue_process_msg_set_multi_queue_priority(struct xe_sched_msg * msg)1922 static void __guc_exec_queue_process_msg_set_multi_queue_priority(struct xe_sched_msg *msg)
1923 {
1924 struct xe_exec_queue *q = msg->private_data;
1925
1926 if (guc_exec_queue_allowed_to_change_state(q))
1927 guc_exec_queue_send_cgp_sync(q);
1928
1929 kfree(msg);
1930 }
1931
1932 #define CLEANUP 1 /* Non-zero values to catch uninitialized msg */
1933 #define SET_SCHED_PROPS 2
1934 #define SUSPEND 3
1935 #define RESUME 4
1936 #define SET_MULTI_QUEUE_PRIORITY 5
1937 #define OPCODE_MASK 0xf
1938 #define MSG_LOCKED BIT(8)
1939 #define MSG_HEAD BIT(9)
1940
guc_exec_queue_process_msg(struct xe_sched_msg * msg)1941 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
1942 {
1943 struct xe_device *xe = guc_to_xe(exec_queue_to_guc(msg->private_data));
1944
1945 trace_xe_sched_msg_recv(msg);
1946
1947 switch (msg->opcode) {
1948 case CLEANUP:
1949 __guc_exec_queue_process_msg_cleanup(msg);
1950 break;
1951 case SET_SCHED_PROPS:
1952 __guc_exec_queue_process_msg_set_sched_props(msg);
1953 break;
1954 case SUSPEND:
1955 __guc_exec_queue_process_msg_suspend(msg);
1956 break;
1957 case RESUME:
1958 __guc_exec_queue_process_msg_resume(msg);
1959 break;
1960 case SET_MULTI_QUEUE_PRIORITY:
1961 __guc_exec_queue_process_msg_set_multi_queue_priority(msg);
1962 break;
1963 default:
1964 XE_WARN_ON("Unknown message type");
1965 }
1966
1967 xe_pm_runtime_put(xe);
1968 }
1969
1970 static const struct drm_sched_backend_ops drm_sched_ops = {
1971 .run_job = guc_exec_queue_run_job,
1972 .free_job = guc_exec_queue_free_job,
1973 .timedout_job = guc_exec_queue_timedout_job,
1974 };
1975
1976 static const struct xe_sched_backend_ops xe_sched_ops = {
1977 .process_msg = guc_exec_queue_process_msg,
1978 };
1979
guc_exec_queue_init(struct xe_exec_queue * q)1980 static int guc_exec_queue_init(struct xe_exec_queue *q)
1981 {
1982 struct xe_gpu_scheduler *sched;
1983 struct xe_guc *guc = exec_queue_to_guc(q);
1984 struct drm_device *drm = &guc_to_xe(guc)->drm;
1985 struct workqueue_struct *submit_wq = NULL;
1986 struct xe_guc_exec_queue *ge;
1987 long timeout;
1988 int err, i;
1989
1990 xe_gt_assert(guc_to_gt(guc), xe_device_uc_enabled(guc_to_xe(guc)));
1991
1992 ge = kzalloc_obj(*ge);
1993 if (!ge)
1994 return -ENOMEM;
1995
1996 drm_dev_get(drm);
1997
1998 q->guc = ge;
1999 ge->q = q;
2000 init_rcu_head(&ge->rcu);
2001 init_waitqueue_head(&ge->suspend_wait);
2002
2003 for (i = 0; i < MAX_STATIC_MSG_TYPE; ++i)
2004 INIT_LIST_HEAD(&ge->static_msgs[i].link);
2005
2006 timeout = (q->vm && xe_vm_in_lr_mode(q->vm)) ? MAX_SCHEDULE_TIMEOUT :
2007 msecs_to_jiffies(q->sched_props.job_timeout_ms);
2008
2009 err = alloc_guc_id(guc, q);
2010 if (err)
2011 goto err_free;
2012
2013 xe_exec_queue_assign_name(q, q->guc->id);
2014
2015 strscpy(ge->name, q->name, sizeof(ge->name));
2016
2017 /*
2018 * Use primary queue's submit_wq for all secondary queues of a
2019 * multi queue group. This serialization avoids any locking around
2020 * CGP synchronization with GuC.
2021 */
2022 if (xe_exec_queue_is_multi_queue_secondary(q)) {
2023 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
2024
2025 submit_wq = primary->guc->sched.base.submit_wq;
2026 }
2027
2028 err = xe_sched_init(&ge->sched, &drm_sched_ops, &xe_sched_ops,
2029 submit_wq, xe_lrc_ring_size() / MAX_JOB_SIZE_BYTES, 64,
2030 timeout, guc_to_gt(guc)->ordered_wq, NULL,
2031 ge->name, gt_to_xe(q->gt)->drm.dev);
2032 if (err)
2033 goto err_release_id;
2034
2035 sched = &ge->sched;
2036 err = xe_sched_entity_init(&ge->entity, sched);
2037 if (err)
2038 goto err_sched;
2039
2040 q->entity = &ge->entity;
2041
2042 mutex_lock(&guc->submission_state.lock);
2043 if (xe_guc_read_stopped(guc) || vf_recovery(guc))
2044 xe_sched_stop(sched);
2045 publish_guc_id(guc, q);
2046 mutex_unlock(&guc->submission_state.lock);
2047
2048 /*
2049 * Maintain secondary queues of the multi queue group in a list
2050 * for handling dependencies across the queues in the group.
2051 */
2052 if (xe_exec_queue_is_multi_queue_secondary(q)) {
2053 struct xe_exec_queue_group *group = q->multi_queue.group;
2054
2055 INIT_LIST_HEAD(&q->multi_queue.link);
2056 mutex_lock(&group->list_lock);
2057 if (group->stopped)
2058 WRITE_ONCE(q->guc->sched.base.pause_submit, true);
2059 list_add_tail(&q->multi_queue.link, &group->list);
2060 mutex_unlock(&group->list_lock);
2061 }
2062
2063 if (xe_exec_queue_is_multi_queue(q))
2064 trace_xe_exec_queue_create_multi_queue(q);
2065 else
2066 trace_xe_exec_queue_create(q);
2067
2068 return 0;
2069
2070 err_sched:
2071 xe_sched_fini(&ge->sched);
2072 err_release_id:
2073 release_guc_id(guc, q);
2074 err_free:
2075 kfree(ge);
2076 drm_dev_put(drm);
2077
2078 return err;
2079 }
2080
guc_exec_queue_kill(struct xe_exec_queue * q)2081 static void guc_exec_queue_kill(struct xe_exec_queue *q)
2082 {
2083 trace_xe_exec_queue_kill(q);
2084 set_exec_queue_killed(q);
2085 __suspend_fence_signal(q);
2086 xe_guc_exec_queue_trigger_cleanup(q);
2087 }
2088
guc_exec_queue_add_msg(struct xe_exec_queue * q,struct xe_sched_msg * msg,u32 opcode)2089 static void guc_exec_queue_add_msg(struct xe_exec_queue *q, struct xe_sched_msg *msg,
2090 u32 opcode)
2091 {
2092 xe_pm_runtime_get_noresume(guc_to_xe(exec_queue_to_guc(q)));
2093
2094 INIT_LIST_HEAD(&msg->link);
2095 msg->opcode = opcode & OPCODE_MASK;
2096 msg->private_data = q;
2097
2098 trace_xe_sched_msg_add(msg);
2099 if (opcode & MSG_HEAD)
2100 xe_sched_add_msg_head(&q->guc->sched, msg);
2101 else if (opcode & MSG_LOCKED)
2102 xe_sched_add_msg_locked(&q->guc->sched, msg);
2103 else
2104 xe_sched_add_msg(&q->guc->sched, msg);
2105 }
2106
guc_exec_queue_try_add_msg_head(struct xe_exec_queue * q,struct xe_sched_msg * msg,u32 opcode)2107 static void guc_exec_queue_try_add_msg_head(struct xe_exec_queue *q,
2108 struct xe_sched_msg *msg,
2109 u32 opcode)
2110 {
2111 if (!list_empty(&msg->link))
2112 return;
2113
2114 guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED | MSG_HEAD);
2115 }
2116
guc_exec_queue_try_add_msg(struct xe_exec_queue * q,struct xe_sched_msg * msg,u32 opcode)2117 static bool guc_exec_queue_try_add_msg(struct xe_exec_queue *q,
2118 struct xe_sched_msg *msg,
2119 u32 opcode)
2120 {
2121 if (!list_empty(&msg->link))
2122 return false;
2123
2124 guc_exec_queue_add_msg(q, msg, opcode | MSG_LOCKED);
2125
2126 return true;
2127 }
2128
2129 #define STATIC_MSG_CLEANUP 0
2130 #define STATIC_MSG_SUSPEND 1
2131 #define STATIC_MSG_RESUME 2
guc_exec_queue_destroy(struct xe_exec_queue * q)2132 static void guc_exec_queue_destroy(struct xe_exec_queue *q)
2133 {
2134 struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_CLEANUP;
2135
2136 if (!(q->flags & EXEC_QUEUE_FLAG_PERMANENT) && !exec_queue_wedged(q))
2137 guc_exec_queue_add_msg(q, msg, CLEANUP);
2138 else
2139 __guc_exec_queue_destroy(exec_queue_to_guc(q), q);
2140 }
2141
guc_exec_queue_set_priority(struct xe_exec_queue * q,enum xe_exec_queue_priority priority)2142 static int guc_exec_queue_set_priority(struct xe_exec_queue *q,
2143 enum xe_exec_queue_priority priority)
2144 {
2145 struct xe_sched_msg *msg;
2146
2147 if (q->sched_props.priority == priority ||
2148 exec_queue_killed_or_banned_or_wedged(q))
2149 return 0;
2150
2151 msg = kmalloc_obj(*msg);
2152 if (!msg)
2153 return -ENOMEM;
2154
2155 q->sched_props.priority = priority;
2156 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
2157
2158 return 0;
2159 }
2160
guc_exec_queue_set_timeslice(struct xe_exec_queue * q,u32 timeslice_us)2161 static int guc_exec_queue_set_timeslice(struct xe_exec_queue *q, u32 timeslice_us)
2162 {
2163 struct xe_sched_msg *msg;
2164
2165 if (q->sched_props.timeslice_us == timeslice_us ||
2166 exec_queue_killed_or_banned_or_wedged(q))
2167 return 0;
2168
2169 msg = kmalloc_obj(*msg);
2170 if (!msg)
2171 return -ENOMEM;
2172
2173 q->sched_props.timeslice_us = timeslice_us;
2174 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
2175
2176 return 0;
2177 }
2178
guc_exec_queue_set_preempt_timeout(struct xe_exec_queue * q,u32 preempt_timeout_us)2179 static int guc_exec_queue_set_preempt_timeout(struct xe_exec_queue *q,
2180 u32 preempt_timeout_us)
2181 {
2182 struct xe_sched_msg *msg;
2183
2184 if (q->sched_props.preempt_timeout_us == preempt_timeout_us ||
2185 exec_queue_killed_or_banned_or_wedged(q))
2186 return 0;
2187
2188 msg = kmalloc_obj(*msg);
2189 if (!msg)
2190 return -ENOMEM;
2191
2192 q->sched_props.preempt_timeout_us = preempt_timeout_us;
2193 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
2194
2195 return 0;
2196 }
2197
guc_exec_queue_set_multi_queue_priority(struct xe_exec_queue * q,enum xe_multi_queue_priority priority)2198 static int guc_exec_queue_set_multi_queue_priority(struct xe_exec_queue *q,
2199 enum xe_multi_queue_priority priority)
2200 {
2201 struct xe_sched_msg *msg;
2202
2203 xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), xe_exec_queue_is_multi_queue(q));
2204
2205 if (exec_queue_killed_or_banned_or_wedged(q))
2206 return 0;
2207
2208 msg = kmalloc_obj(*msg);
2209 if (!msg)
2210 return -ENOMEM;
2211
2212 scoped_guard(spinlock, &q->multi_queue.lock) {
2213 if (q->multi_queue.priority == priority) {
2214 kfree(msg);
2215 return 0;
2216 }
2217
2218 q->multi_queue.priority = priority;
2219 }
2220
2221 guc_exec_queue_add_msg(q, msg, SET_MULTI_QUEUE_PRIORITY);
2222
2223 return 0;
2224 }
2225
2226 /*
2227 * Core suspend: take a suspend reference on @q and, on the first reference,
2228 * disable its GuC context so the GPU is actually preempted. Caller must have
2229 * ensured @q is not killed/banned/wedged. Returns true if this was the first
2230 * suspend reference (the 0->1 transition).
2231 */
__guc_exec_queue_suspend(struct xe_exec_queue * q)2232 static bool __guc_exec_queue_suspend(struct xe_exec_queue *q)
2233 {
2234 struct xe_guc_exec_queue *ge = q->guc;
2235 struct xe_gpu_scheduler *sched = &ge->sched;
2236 struct xe_sched_msg *msg = ge->static_msgs + STATIC_MSG_SUSPEND;
2237 bool first;
2238
2239 xe_sched_msg_lock(sched);
2240 first = (++ge->suspend_count == 1);
2241 if (first) {
2242 bool added = guc_exec_queue_try_add_msg(q, msg, SUSPEND);
2243
2244 /* slot must be free at 0->1 */
2245 xe_gt_assert(guc_to_gt(exec_queue_to_guc(q)), added);
2246 ge->suspend_pending = true;
2247 }
2248 xe_sched_msg_unlock(sched);
2249
2250 return first;
2251 }
2252
2253 /*
2254 * Core resume: drop a suspend reference on @q and, on the last reference,
2255 * re-enable its GuC context. Returns true if this dropped the last suspend
2256 * reference (the 1->0 transition).
2257 */
__guc_exec_queue_resume(struct xe_exec_queue * q)2258 static bool __guc_exec_queue_resume(struct xe_exec_queue *q)
2259 {
2260 struct xe_guc_exec_queue *ge = q->guc;
2261 struct xe_gpu_scheduler *sched = &ge->sched;
2262 struct xe_sched_msg *msg = ge->static_msgs + STATIC_MSG_RESUME;
2263 struct xe_guc *guc = exec_queue_to_guc(q);
2264 bool last;
2265
2266 xe_sched_msg_lock(sched);
2267 xe_gt_assert(guc_to_gt(guc), !ge->suspend_pending);
2268 xe_gt_assert(guc_to_gt(guc), ge->suspend_count > 0);
2269 last = (--ge->suspend_count == 0);
2270 if (last) {
2271 bool added = guc_exec_queue_try_add_msg(q, msg, RESUME);
2272
2273 /* slot must be free at 1->0 */
2274 xe_gt_assert(guc_to_gt(guc), added);
2275 }
2276 xe_sched_msg_unlock(sched);
2277
2278 return last;
2279 }
2280
guc_exec_queue_suspend(struct xe_exec_queue * q)2281 static int guc_exec_queue_suspend(struct xe_exec_queue *q)
2282 {
2283 if (exec_queue_killed_or_banned_or_wedged(q))
2284 return -EINVAL;
2285
2286 /*
2287 * Non-multi-queue queues and multi-queue primaries suspend themselves
2288 * directly: their own msg_lock makes the suspend_count 0->1 transition
2289 * and the suspend_pending update atomic, so no group level serialization
2290 * is needed.
2291 */
2292 if (!xe_exec_queue_is_multi_queue_secondary(q)) {
2293 __guc_exec_queue_suspend(q);
2294 return 0;
2295 }
2296
2297 /*
2298 * A secondary's suspend is meaningless once the primary - which owns the
2299 * group's GuC context - is gone, so fail it too. This keeps the
2300 * secondary's effective state consistent with guc_exec_queue_reset_status(),
2301 * which already reports the primary's killed/banned/wedged state for
2302 * secondaries. A primary killed *after* this check is still handled at
2303 * message-processing time, where the SUSPEND is a no-op for a killed
2304 * context; this only covers an already-dead primary.
2305 */
2306 if (exec_queue_killed_or_banned_or_wedged(xe_exec_queue_multi_queue_primary(q)))
2307 return -EINVAL;
2308
2309 /*
2310 * A secondary doesn't interface with GuC: suspend it like any other
2311 * queue (its own suspend_count drives its internally handled scheduler
2312 * state) and, only on its own 0->1 transition, forward the suspend to the
2313 * primary so the GPU is actually preempted. Hold @suspend_lock so that
2314 * observing the secondary's transition and forwarding it to the primary
2315 * happen atomically; this keeps the primary's refcount paired with member
2316 * transitions even if the same secondary is suspended and resumed
2317 * concurrently across rebind cycles.
2318 */
2319 scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) {
2320 if (__guc_exec_queue_suspend(q))
2321 __guc_exec_queue_suspend(xe_exec_queue_multi_queue_primary(q));
2322 }
2323
2324 return 0;
2325 }
2326
guc_exec_queue_suspend_timeout_ban(struct xe_exec_queue * q)2327 static void guc_exec_queue_suspend_timeout_ban(struct xe_exec_queue *q)
2328 {
2329 struct xe_guc *guc = exec_queue_to_guc(q);
2330
2331 xe_gt_warn(guc_to_gt(guc),
2332 "Suspend fence, guc_id=%d, failed to respond, banning queue",
2333 q->guc->id);
2334 /*
2335 * The GuC failed to respond to the suspend within the timeout. This is
2336 * not recoverable for this context, so ban it and tear it down via
2337 * cleanup rather than leave it suspended forever. __suspend_fence_signal
2338 * clears suspend_pending and wakes any waiter.
2339 *
2340 * @q is the primary here; it owns the group's GuC context, so a failure
2341 * to suspend it wedges the whole group. Ban and tear down the entire
2342 * group in the multi-queue case.
2343 */
2344 if (xe_exec_queue_is_multi_queue(q)) {
2345 set_exec_queue_group_banned(q);
2346 __suspend_fence_signal(q);
2347 xe_guc_exec_queue_group_trigger_cleanup(q);
2348 } else {
2349 set_exec_queue_banned(q);
2350 __suspend_fence_signal(q);
2351 xe_guc_exec_queue_trigger_cleanup(q);
2352 }
2353 }
2354
2355 /*
2356 * Wait for @q's own suspend to complete: suspend_pending cleared, or the queue
2357 * killed / GuC stopped. With @blocking, wait uninterruptibly and do not handle
2358 * VF recovery (for callers that must complete on behalf of a possibly
2359 * cross-process queue); otherwise wait interruptibly.
2360 *
2361 * Returns 0 on completion or -ETIME on timeout. Interruptible waits may also
2362 * return -EAGAIN (VF recovery in progress, retry) or -ERESTARTSYS (aborted by a
2363 * signal; suspend_pending may still be set, so callers must not resume()
2364 * without re-confirming the suspend).
2365 */
guc_exec_queue_wait_suspend_done(struct xe_exec_queue * q,bool blocking)2366 static int guc_exec_queue_wait_suspend_done(struct xe_exec_queue *q, bool blocking)
2367 {
2368 struct xe_guc *guc = exec_queue_to_guc(q);
2369 struct xe_device *xe = guc_to_xe(guc);
2370 int ret;
2371
2372 /*
2373 * Likely don't need to check exec_queue_killed() as we clear
2374 * suspend_pending upon kill but to be paranoid but races in which
2375 * suspend_pending is set after kill also check kill here.
2376 */
2377 #define WAIT_COND \
2378 (!READ_ONCE(q->guc->suspend_pending) || exec_queue_killed(q) || \
2379 xe_guc_read_stopped(guc))
2380
2381 retry:
2382 if (blocking) {
2383 if (IS_SRIOV_VF(xe))
2384 ret = wait_event_timeout(guc->ct.wq, WAIT_COND, HZ * 5);
2385 else
2386 ret = wait_event_timeout(q->guc->suspend_wait, WAIT_COND,
2387 HZ * 5);
2388 } else if (IS_SRIOV_VF(xe)) {
2389 ret = wait_event_interruptible_timeout(guc->ct.wq, WAIT_COND ||
2390 vf_recovery(guc), HZ * 5);
2391 } else {
2392 ret = wait_event_interruptible_timeout(q->guc->suspend_wait,
2393 WAIT_COND, HZ * 5);
2394 }
2395
2396 if (!blocking && vf_recovery(guc) && !xe_device_wedged(xe))
2397 return -EAGAIN;
2398
2399 if (!ret)
2400 return -ETIME;
2401 else if (!blocking && IS_SRIOV_VF(xe) && !WAIT_COND)
2402 /* Corner case on RESFIX DONE where vf_recovery() changes */
2403 goto retry;
2404
2405 #undef WAIT_COND
2406
2407 return ret < 0 ? ret : 0;
2408 }
2409
guc_exec_queue_suspend_wait_common(struct xe_exec_queue * q,bool blocking)2410 static int guc_exec_queue_suspend_wait_common(struct xe_exec_queue *q, bool blocking)
2411 {
2412 int ret;
2413
2414 /*
2415 * A secondary's suspend rides the sched-message worker (short-circuited,
2416 * no GuC round-trip) and so is not synchronous with
2417 * guc_exec_queue_suspend(): its own suspend_pending may still be set
2418 * here. Waiting on the primary alone is not sufficient - if the primary
2419 * was already suspended, the forward is a refcount-only transition that
2420 * queues no new primary SUSPEND and leaves the primary's suspend_pending
2421 * clear, so the primary wait would return immediately while the
2422 * secondary's suspend is still in flight, and a later resume() would trip
2423 * the secondary's !suspend_pending assert. So first wait for the
2424 * secondary's own suspend to complete, then wait on the primary.
2425 *
2426 * A timeout on either bans the queue (being multi-queue, that tears down
2427 * the whole group). A secondary suspend has no real GuC round-trip, so
2428 * its timeout is a software scheduler stall rather than a GuC fault, but
2429 * banning is still the safe recovery: otherwise the queue is left with
2430 * suspend_pending set and a subsequent resume() trips the !suspend_pending
2431 * assert.
2432 */
2433 if (xe_exec_queue_is_multi_queue_secondary(q)) {
2434 ret = guc_exec_queue_wait_suspend_done(q, blocking);
2435 if (ret == -ETIME)
2436 guc_exec_queue_suspend_timeout_ban(q);
2437 if (ret)
2438 return ret;
2439 }
2440
2441 q = xe_exec_queue_multi_queue_primary(q);
2442 ret = guc_exec_queue_wait_suspend_done(q, blocking);
2443 if (ret == -ETIME)
2444 guc_exec_queue_suspend_timeout_ban(q);
2445
2446 return ret;
2447 }
2448
guc_exec_queue_suspend_wait(struct xe_exec_queue * q)2449 static int guc_exec_queue_suspend_wait(struct xe_exec_queue *q)
2450 {
2451 return guc_exec_queue_suspend_wait_common(q, false);
2452 }
2453
2454 /*
2455 * Uninterruptible variant of guc_exec_queue_suspend_wait() for callers that
2456 * must complete the wait on behalf of a queue possibly owned by a different
2457 * process (e.g. cleanup/undo paths). An interruptible wait could return
2458 * -ERESTARTSYS if the calling task is signalled, leaving that queue suspended
2459 * forever (cross-process DoS). VF recovery is deliberately not handled (no
2460 * -EAGAIN) since a blocking caller cannot retry.
2461 */
guc_exec_queue_suspend_wait_blocking(struct xe_exec_queue * q)2462 static int guc_exec_queue_suspend_wait_blocking(struct xe_exec_queue *q)
2463 {
2464 return guc_exec_queue_suspend_wait_common(q, true);
2465 }
2466
guc_exec_queue_resume(struct xe_exec_queue * q)2467 static void guc_exec_queue_resume(struct xe_exec_queue *q)
2468 {
2469 /*
2470 * Non-multi-queue queues and multi-queue primaries resume themselves
2471 * directly; their own msg_lock is sufficient.
2472 */
2473 if (!xe_exec_queue_is_multi_queue_secondary(q)) {
2474 __guc_exec_queue_resume(q);
2475 return;
2476 }
2477
2478 /*
2479 * Mirror of guc_exec_queue_suspend(): resume the secondary like any
2480 * other queue and, only on its own 1->0 transition, forward the resume
2481 * to the primary so the primary's GuC context is re-enabled once the
2482 * last member that suspended it resumes. @suspend_lock keeps the
2483 * secondary transition and the primary forward atomic.
2484 */
2485 scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) {
2486 if (__guc_exec_queue_resume(q))
2487 __guc_exec_queue_resume(xe_exec_queue_multi_queue_primary(q));
2488 }
2489 }
2490
2491 /*
2492 * Drop a leaving secondary's forwarded suspend reference on the primary and
2493 * resume the primary if this was the last member that had it suspended.
2494 * See guc_exec_queue_fini().
2495 */
guc_exec_queue_multi_queue_drop_suspend(struct xe_exec_queue * q)2496 static void guc_exec_queue_multi_queue_drop_suspend(struct xe_exec_queue *q)
2497 {
2498 scoped_guard(spinlock, &q->multi_queue.group->suspend_lock) {
2499 struct xe_exec_queue *primary = xe_exec_queue_multi_queue_primary(q);
2500
2501 /*
2502 * A suspended secondary holds exactly one suspend reference on the
2503 * primary (forwarded on its 0->1 transition). If it leaves while
2504 * still suspended, release that reference so the primary is not
2505 * kept disabled forever.
2506 */
2507 if (!READ_ONCE(q->guc->suspend_count))
2508 break;
2509
2510 if (exec_queue_killed_or_banned_or_wedged(primary))
2511 break;
2512
2513 /*
2514 * No suspend_wait() here (and we can't - suspend_lock is a
2515 * spinlock). guc_exec_queue_fini() has already drained the
2516 * primary's forwarded suspend with the blocking wait, so its
2517 * suspend has completed (suspend_pending cleared) by the time we
2518 * resume it here. __guc_exec_queue_resume() asserts this.
2519 */
2520 __guc_exec_queue_resume(primary);
2521 }
2522 }
2523
guc_exec_queue_reset_status(struct xe_exec_queue * q)2524 static bool guc_exec_queue_reset_status(struct xe_exec_queue *q)
2525 {
2526 if (xe_exec_queue_is_multi_queue_secondary(q) &&
2527 guc_exec_queue_reset_status(xe_exec_queue_multi_queue_primary(q)))
2528 return true;
2529
2530 return exec_queue_reset(q) || exec_queue_killed_or_banned_or_wedged(q);
2531 }
2532
2533 /*
2534 * All of these functions are an abstraction layer which other parts of Xe can
2535 * use to trap into the GuC backend. All of these functions, aside from init,
2536 * really shouldn't do much other than trap into the DRM scheduler which
2537 * synchronizes these operations.
2538 */
2539 static const struct xe_exec_queue_ops guc_exec_queue_ops = {
2540 .init = guc_exec_queue_init,
2541 .kill = guc_exec_queue_kill,
2542 .fini = guc_exec_queue_fini,
2543 .destroy = guc_exec_queue_destroy,
2544 .set_priority = guc_exec_queue_set_priority,
2545 .set_timeslice = guc_exec_queue_set_timeslice,
2546 .set_preempt_timeout = guc_exec_queue_set_preempt_timeout,
2547 .set_multi_queue_priority = guc_exec_queue_set_multi_queue_priority,
2548 .suspend = guc_exec_queue_suspend,
2549 .suspend_wait = guc_exec_queue_suspend_wait,
2550 .suspend_wait_blocking = guc_exec_queue_suspend_wait_blocking,
2551 .resume = guc_exec_queue_resume,
2552 .reset_status = guc_exec_queue_reset_status,
2553 };
2554
guc_exec_queue_stop(struct xe_guc * guc,struct xe_exec_queue * q)2555 static void guc_exec_queue_stop(struct xe_guc *guc, struct xe_exec_queue *q)
2556 {
2557 struct xe_gpu_scheduler *sched = &q->guc->sched;
2558 bool do_destroy = false;
2559
2560 /* Stop scheduling + flush any DRM scheduler operations */
2561 xe_sched_submission_stop(sched);
2562
2563 /* Clean up lost G2H + reset engine state */
2564 if (exec_queue_registered(q)) {
2565 if (exec_queue_destroyed(q))
2566 do_destroy = true;
2567 }
2568 if (q->guc->suspend_pending) {
2569 set_exec_queue_suspended(q);
2570 suspend_fence_signal(q);
2571 }
2572 atomic_and(EXEC_QUEUE_STATE_WEDGED | EXEC_QUEUE_STATE_BANNED |
2573 EXEC_QUEUE_STATE_KILLED | EXEC_QUEUE_STATE_DESTROYED |
2574 EXEC_QUEUE_STATE_SUSPENDED,
2575 &q->guc->state);
2576 q->guc->resume_time = 0;
2577 trace_xe_exec_queue_stop(q);
2578
2579 /*
2580 * Ban any engine (aside from kernel and engines used for VM ops) with a
2581 * started but not complete job or if a job has gone through a GT reset
2582 * more than twice.
2583 */
2584 if (!(q->flags & (EXEC_QUEUE_FLAG_KERNEL | EXEC_QUEUE_FLAG_VM))) {
2585 struct xe_sched_job *job = xe_sched_first_pending_job(sched);
2586 bool ban = false;
2587
2588 if (job) {
2589 if ((xe_sched_job_started(job) &&
2590 !xe_sched_job_completed(job)) ||
2591 xe_sched_invalidate_job(job, 2)) {
2592 trace_xe_sched_job_ban(job);
2593 ban = true;
2594 }
2595 }
2596
2597 if (ban) {
2598 set_exec_queue_banned(q);
2599 xe_guc_exec_queue_trigger_cleanup(q);
2600 }
2601 }
2602
2603 if (do_destroy)
2604 __guc_exec_queue_destroy(guc, q);
2605 }
2606
guc_submit_reset_prepare(struct xe_guc * guc)2607 static int guc_submit_reset_prepare(struct xe_guc *guc)
2608 {
2609 int ret;
2610
2611 /*
2612 * Using an atomic here rather than submission_state.lock as this
2613 * function can be called while holding the CT lock (engine reset
2614 * failure). submission_state.lock needs the CT lock to resubmit jobs.
2615 * Atomic is not ideal, but it works to prevent against concurrent reset
2616 * and releasing any TDRs waiting on guc->submission_state.stopped.
2617 */
2618 ret = atomic_fetch_or(1, &guc->submission_state.stopped);
2619 smp_wmb();
2620 wake_up_all(&guc->ct.wq);
2621
2622 return ret;
2623 }
2624
xe_guc_submit_reset_prepare(struct xe_guc * guc)2625 int xe_guc_submit_reset_prepare(struct xe_guc *guc)
2626 {
2627 if (xe_gt_WARN_ON(guc_to_gt(guc), vf_recovery(guc)))
2628 return 0;
2629
2630 if (!guc->submission_state.initialized)
2631 return 0;
2632
2633 return guc_submit_reset_prepare(guc);
2634 }
2635
xe_guc_submit_reset_wait(struct xe_guc * guc)2636 void xe_guc_submit_reset_wait(struct xe_guc *guc)
2637 {
2638 wait_event(guc->ct.wq, xe_device_wedged(guc_to_xe(guc)) ||
2639 !xe_guc_read_stopped(guc));
2640 }
2641
xe_guc_submit_stop(struct xe_guc * guc)2642 void xe_guc_submit_stop(struct xe_guc *guc)
2643 {
2644 struct xe_exec_queue *q;
2645 unsigned long index;
2646
2647 xe_gt_assert(guc_to_gt(guc), xe_guc_read_stopped(guc) == 1);
2648
2649 mutex_lock(&guc->submission_state.lock);
2650
2651 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2652 /* Prevent redundant attempts to stop parallel queues */
2653 if (q->guc->id != index)
2654 continue;
2655
2656 guc_exec_queue_stop(guc, q);
2657 }
2658
2659 mutex_unlock(&guc->submission_state.lock);
2660
2661 /*
2662 * No one can enter the backend at this point, aside from new engine
2663 * creation which is protected by guc->submission_state.lock.
2664 */
2665
2666 }
2667
guc_exec_queue_revert_pending_state_change(struct xe_guc * guc,struct xe_exec_queue * q)2668 static void guc_exec_queue_revert_pending_state_change(struct xe_guc *guc,
2669 struct xe_exec_queue *q)
2670 {
2671 bool pending_enable, pending_disable, pending_resume;
2672
2673 pending_enable = exec_queue_pending_enable(q);
2674 pending_resume = exec_queue_pending_resume(q);
2675
2676 if (pending_enable && pending_resume) {
2677 q->guc->needs_resume = true;
2678 xe_gt_dbg(guc_to_gt(guc), "Replay RESUME - guc_id=%d",
2679 q->guc->id);
2680 }
2681
2682 if (pending_enable && !pending_resume) {
2683 clear_exec_queue_registered(q);
2684 xe_gt_dbg(guc_to_gt(guc), "Replay REGISTER - guc_id=%d",
2685 q->guc->id);
2686 }
2687
2688 if (pending_enable) {
2689 clear_exec_queue_enabled(q);
2690 clear_exec_queue_pending_resume(q);
2691 clear_exec_queue_pending_enable(q);
2692 xe_gt_dbg(guc_to_gt(guc), "Replay ENABLE - guc_id=%d",
2693 q->guc->id);
2694 }
2695
2696 if (exec_queue_destroyed(q) && exec_queue_registered(q)) {
2697 clear_exec_queue_destroyed(q);
2698 q->guc->needs_cleanup = true;
2699 xe_gt_dbg(guc_to_gt(guc), "Replay CLEANUP - guc_id=%d",
2700 q->guc->id);
2701 }
2702
2703 pending_disable = exec_queue_pending_disable(q);
2704
2705 if (pending_disable && exec_queue_suspended(q)) {
2706 clear_exec_queue_suspended(q);
2707 q->guc->needs_suspend = true;
2708 xe_gt_dbg(guc_to_gt(guc), "Replay SUSPEND - guc_id=%d",
2709 q->guc->id);
2710 }
2711
2712 if (pending_disable) {
2713 if (!pending_enable)
2714 set_exec_queue_enabled(q);
2715 clear_exec_queue_pending_disable(q);
2716 xe_gt_dbg(guc_to_gt(guc), "Replay DISABLE - guc_id=%d",
2717 q->guc->id);
2718 }
2719
2720 q->guc->resume_time = 0;
2721 }
2722
lrc_parallel_clear(struct xe_lrc * lrc)2723 static void lrc_parallel_clear(struct xe_lrc *lrc)
2724 {
2725 struct xe_device *xe = gt_to_xe(lrc->gt);
2726 struct iosys_map map = xe_lrc_parallel_map(lrc);
2727 int i;
2728
2729 for (i = 0; i < WQ_SIZE / sizeof(u32); ++i)
2730 parallel_write(xe, map, wq[i],
2731 FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
2732 FIELD_PREP(WQ_LEN_MASK, 0));
2733 }
2734
2735 /*
2736 * This function is quite complex but only real way to ensure no state is lost
2737 * during VF resume flows. The function scans the queue state, make adjustments
2738 * as needed, and queues jobs / messages which replayed upon unpause.
2739 */
guc_exec_queue_pause(struct xe_guc * guc,struct xe_exec_queue * q)2740 static void guc_exec_queue_pause(struct xe_guc *guc, struct xe_exec_queue *q)
2741 {
2742 struct xe_gpu_scheduler *sched = &q->guc->sched;
2743 struct xe_sched_job *job;
2744 int i;
2745
2746 lockdep_assert_held(&guc->submission_state.lock);
2747
2748 /* Stop scheduling + flush any DRM scheduler operations */
2749 xe_sched_submission_stop(sched);
2750 cancel_delayed_work_sync(&sched->base.work_tdr);
2751
2752 guc_exec_queue_revert_pending_state_change(guc, q);
2753
2754 if (xe_exec_queue_is_parallel(q)) {
2755 /* Pairs with WRITE_ONCE in __xe_exec_queue_init */
2756 struct xe_lrc *lrc = READ_ONCE(q->lrc[0]);
2757
2758 /*
2759 * NOP existing WQ commands that may contain stale GGTT
2760 * addresses. These will be replayed upon unpause. The hardware
2761 * seems to get confused if the WQ head/tail pointers are
2762 * adjusted.
2763 */
2764 if (lrc)
2765 lrc_parallel_clear(lrc);
2766 }
2767
2768 job = xe_sched_first_pending_job(sched);
2769 if (job) {
2770 job->restore_replay = true;
2771
2772 /*
2773 * Adjust software tail so jobs submitted overwrite previous
2774 * position in ring buffer with new GGTT addresses.
2775 */
2776 for (i = 0; i < q->width; ++i)
2777 q->lrc[i]->ring.tail = job->ptrs[i].head;
2778 }
2779 }
2780
2781 /**
2782 * xe_guc_submit_pause - Stop further runs of submission tasks on given GuC.
2783 * @guc: the &xe_guc struct instance whose scheduler is to be disabled
2784 */
xe_guc_submit_pause(struct xe_guc * guc)2785 void xe_guc_submit_pause(struct xe_guc *guc)
2786 {
2787 struct xe_exec_queue *q;
2788 unsigned long index;
2789
2790 mutex_lock(&guc->submission_state.lock);
2791 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
2792 xe_sched_submission_stop(&q->guc->sched);
2793 mutex_unlock(&guc->submission_state.lock);
2794 }
2795
2796 /**
2797 * xe_guc_submit_pause_vf - Stop further runs of submission tasks for VF.
2798 * @guc: the &xe_guc struct instance whose scheduler is to be disabled
2799 */
xe_guc_submit_pause_vf(struct xe_guc * guc)2800 void xe_guc_submit_pause_vf(struct xe_guc *guc)
2801 {
2802 struct xe_exec_queue *q;
2803 unsigned long index;
2804
2805 xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
2806 xe_gt_assert(guc_to_gt(guc), vf_recovery(guc));
2807
2808 mutex_lock(&guc->submission_state.lock);
2809 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2810 /* Prevent redundant attempts to stop parallel queues */
2811 if (q->guc->id != index)
2812 continue;
2813
2814 guc_exec_queue_pause(guc, q);
2815 }
2816 mutex_unlock(&guc->submission_state.lock);
2817 }
2818
guc_exec_queue_start(struct xe_exec_queue * q)2819 static void guc_exec_queue_start(struct xe_exec_queue *q)
2820 {
2821 struct xe_gpu_scheduler *sched = &q->guc->sched;
2822
2823 if (!exec_queue_killed_or_banned_or_wedged(q)) {
2824 struct xe_sched_job *job = xe_sched_first_pending_job(sched);
2825 int i;
2826
2827 trace_xe_exec_queue_resubmit(q);
2828 if (job) {
2829 for (i = 0; i < q->width; ++i) {
2830 /*
2831 * The GuC context is unregistered at this point
2832 * time, adjusting software ring tail ensures
2833 * jobs are rewritten in original placement,
2834 * adjusting LRC tail ensures the newly loaded
2835 * GuC / contexts only view the LRC tail
2836 * increasing as jobs are written out.
2837 */
2838 q->lrc[i]->ring.tail = job->ptrs[i].head;
2839 xe_lrc_set_ring_tail(q->lrc[i],
2840 xe_lrc_ring_head(q->lrc[i]));
2841 }
2842 }
2843 xe_sched_resubmit_jobs(sched);
2844 }
2845
2846 xe_sched_submission_start(sched);
2847 xe_sched_submission_resume_tdr(sched);
2848 }
2849
xe_guc_submit_start(struct xe_guc * guc)2850 int xe_guc_submit_start(struct xe_guc *guc)
2851 {
2852 struct xe_exec_queue *q;
2853 unsigned long index;
2854
2855 xe_gt_assert(guc_to_gt(guc), xe_guc_read_stopped(guc) == 1);
2856
2857 mutex_lock(&guc->submission_state.lock);
2858 atomic_dec(&guc->submission_state.stopped);
2859 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2860 /* Prevent redundant attempts to start parallel queues */
2861 if (q->guc->id != index)
2862 continue;
2863
2864 guc_exec_queue_start(q);
2865 }
2866 mutex_unlock(&guc->submission_state.lock);
2867
2868 wake_up_all(&guc->ct.wq);
2869
2870 return 0;
2871 }
2872
guc_exec_queue_unpause_prepare(struct xe_guc * guc,struct xe_exec_queue * q)2873 static void guc_exec_queue_unpause_prepare(struct xe_guc *guc,
2874 struct xe_exec_queue *q)
2875 {
2876 struct xe_gpu_scheduler *sched = &q->guc->sched;
2877 struct xe_sched_job *job = NULL;
2878 struct drm_sched_job *s_job;
2879 bool restore_replay = false;
2880
2881 drm_sched_for_each_pending_job(s_job, &sched->base, NULL) {
2882 job = to_xe_sched_job(s_job);
2883 restore_replay |= job->restore_replay;
2884 if (restore_replay) {
2885 xe_gt_dbg(guc_to_gt(guc), "Replay JOB - guc_id=%d, seqno=%d",
2886 q->guc->id, xe_sched_job_seqno(job));
2887
2888 q->ring_ops->emit_job(job);
2889 job->restore_replay = true;
2890 }
2891 }
2892
2893 if (job)
2894 job->last_replay = true;
2895 }
2896
2897 /**
2898 * xe_guc_submit_unpause_prepare_vf - Prepare unpause submission tasks for VF.
2899 * @guc: the &xe_guc struct instance whose scheduler is to be prepared for unpause
2900 */
xe_guc_submit_unpause_prepare_vf(struct xe_guc * guc)2901 void xe_guc_submit_unpause_prepare_vf(struct xe_guc *guc)
2902 {
2903 struct xe_exec_queue *q;
2904 unsigned long index;
2905
2906 xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
2907 xe_gt_assert(guc_to_gt(guc), vf_recovery(guc));
2908
2909 mutex_lock(&guc->submission_state.lock);
2910 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
2911 /* Prevent redundant attempts to stop parallel queues */
2912 if (q->guc->id != index)
2913 continue;
2914
2915 guc_exec_queue_unpause_prepare(guc, q);
2916 }
2917 mutex_unlock(&guc->submission_state.lock);
2918 }
2919
guc_exec_queue_replay_pending_state_change(struct xe_exec_queue * q)2920 static void guc_exec_queue_replay_pending_state_change(struct xe_exec_queue *q)
2921 {
2922 struct xe_gpu_scheduler *sched = &q->guc->sched;
2923 struct xe_sched_msg *msg;
2924
2925 if (q->guc->needs_cleanup) {
2926 msg = q->guc->static_msgs + STATIC_MSG_CLEANUP;
2927
2928 guc_exec_queue_add_msg(q, msg, CLEANUP);
2929 q->guc->needs_cleanup = false;
2930 }
2931
2932 if (q->guc->needs_suspend) {
2933 msg = q->guc->static_msgs + STATIC_MSG_SUSPEND;
2934
2935 xe_sched_msg_lock(sched);
2936 guc_exec_queue_try_add_msg_head(q, msg, SUSPEND);
2937 xe_sched_msg_unlock(sched);
2938
2939 q->guc->needs_suspend = false;
2940 }
2941
2942 /*
2943 * The resume must be in the message queue before the suspend as it is
2944 * not possible for a resume to be issued if a suspend pending is, but
2945 * the inverse is possible.
2946 */
2947 if (q->guc->needs_resume) {
2948 msg = q->guc->static_msgs + STATIC_MSG_RESUME;
2949
2950 xe_sched_msg_lock(sched);
2951 guc_exec_queue_try_add_msg_head(q, msg, RESUME);
2952 xe_sched_msg_unlock(sched);
2953
2954 q->guc->needs_resume = false;
2955 }
2956 }
2957
guc_exec_queue_unpause(struct xe_guc * guc,struct xe_exec_queue * q)2958 static void guc_exec_queue_unpause(struct xe_guc *guc, struct xe_exec_queue *q)
2959 {
2960 struct xe_gpu_scheduler *sched = &q->guc->sched;
2961 bool needs_tdr = exec_queue_killed_or_banned_or_wedged(q);
2962
2963 lockdep_assert_held(&guc->submission_state.lock);
2964
2965 xe_sched_resubmit_jobs(sched);
2966 guc_exec_queue_replay_pending_state_change(q);
2967 xe_sched_submission_start(sched);
2968 if (needs_tdr)
2969 xe_guc_exec_queue_trigger_cleanup(q);
2970 xe_sched_submission_resume_tdr(sched);
2971 }
2972
2973 /**
2974 * xe_guc_submit_unpause - Allow further runs of submission tasks on given GuC.
2975 * @guc: the &xe_guc struct instance whose scheduler is to be enabled
2976 */
xe_guc_submit_unpause(struct xe_guc * guc)2977 void xe_guc_submit_unpause(struct xe_guc *guc)
2978 {
2979 struct xe_exec_queue *q;
2980 unsigned long index;
2981
2982 mutex_lock(&guc->submission_state.lock);
2983 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
2984 xe_sched_submission_start(&q->guc->sched);
2985 mutex_unlock(&guc->submission_state.lock);
2986 }
2987
2988 /**
2989 * xe_guc_submit_unpause_vf - Allow further runs of submission tasks for VF.
2990 * @guc: the &xe_guc struct instance whose scheduler is to be enabled
2991 */
xe_guc_submit_unpause_vf(struct xe_guc * guc)2992 void xe_guc_submit_unpause_vf(struct xe_guc *guc)
2993 {
2994 struct xe_exec_queue *q;
2995 unsigned long index;
2996
2997 xe_gt_assert(guc_to_gt(guc), IS_SRIOV_VF(guc_to_xe(guc)));
2998
2999 mutex_lock(&guc->submission_state.lock);
3000 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
3001 /*
3002 * Prevent redundant attempts to stop parallel queues, or queues
3003 * created after resfix done.
3004 */
3005 if (q->guc->id != index ||
3006 !drm_sched_is_stopped(&q->guc->sched.base))
3007 continue;
3008
3009 guc_exec_queue_unpause(guc, q);
3010 }
3011 mutex_unlock(&guc->submission_state.lock);
3012 }
3013
3014 /**
3015 * xe_guc_submit_pause_abort - Abort all paused submission task on given GuC.
3016 * @guc: the &xe_guc struct instance whose scheduler is to be aborted
3017 */
xe_guc_submit_pause_abort(struct xe_guc * guc)3018 void xe_guc_submit_pause_abort(struct xe_guc *guc)
3019 {
3020 struct xe_exec_queue *q;
3021 unsigned long index;
3022
3023 mutex_lock(&guc->submission_state.lock);
3024 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
3025 struct xe_gpu_scheduler *sched = &q->guc->sched;
3026
3027 /* Prevent redundant attempts to stop parallel queues */
3028 if (q->guc->id != index)
3029 continue;
3030
3031 xe_sched_submission_start(sched);
3032 guc_exec_queue_kill(q);
3033 }
3034 mutex_unlock(&guc->submission_state.lock);
3035 }
3036
3037 static struct xe_exec_queue *
g2h_exec_queue_lookup(struct xe_guc * guc,u32 guc_id)3038 g2h_exec_queue_lookup(struct xe_guc *guc, u32 guc_id)
3039 {
3040 struct xe_gt *gt = guc_to_gt(guc);
3041 struct xe_exec_queue *q;
3042
3043 if (unlikely(guc_id >= GUC_ID_MAX)) {
3044 xe_gt_err(gt, "Invalid guc_id %u\n", guc_id);
3045 return NULL;
3046 }
3047
3048 q = xa_load(&guc->submission_state.exec_queue_lookup, guc_id);
3049 if (unlikely(!q)) {
3050 xe_gt_err(gt, "No exec queue found for guc_id %u\n", guc_id);
3051 return NULL;
3052 }
3053
3054 xe_gt_assert(guc_to_gt(guc), guc_id >= q->guc->id);
3055 xe_gt_assert(guc_to_gt(guc), guc_id < (q->guc->id + q->width));
3056
3057 return q;
3058 }
3059
deregister_exec_queue(struct xe_guc * guc,struct xe_exec_queue * q)3060 static void deregister_exec_queue(struct xe_guc *guc, struct xe_exec_queue *q)
3061 {
3062 u32 action[] = {
3063 XE_GUC_ACTION_DEREGISTER_CONTEXT,
3064 q->guc->id,
3065 };
3066
3067 xe_gt_assert(guc_to_gt(guc), exec_queue_destroyed(q));
3068 xe_gt_assert(guc_to_gt(guc), exec_queue_registered(q));
3069 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_disable(q));
3070 xe_gt_assert(guc_to_gt(guc), !exec_queue_pending_enable(q));
3071
3072 trace_xe_exec_queue_deregister(q);
3073
3074 if (xe_exec_queue_is_multi_queue_secondary(q))
3075 handle_deregister_done(guc, q);
3076 else
3077 xe_guc_ct_send_g2h_handler(&guc->ct, action,
3078 ARRAY_SIZE(action));
3079 }
3080
handle_sched_done(struct xe_guc * guc,struct xe_exec_queue * q,u32 runnable_state)3081 static void handle_sched_done(struct xe_guc *guc, struct xe_exec_queue *q,
3082 u32 runnable_state)
3083 {
3084 trace_xe_exec_queue_scheduling_done(q);
3085
3086 if (runnable_state == 1) {
3087 xe_gt_assert(guc_to_gt(guc), exec_queue_pending_enable(q));
3088
3089 q->guc->resume_time = ktime_get();
3090 clear_exec_queue_pending_resume(q);
3091 clear_exec_queue_pending_enable(q);
3092 smp_wmb();
3093 wake_up_all(&guc->ct.wq);
3094 } else {
3095 xe_gt_assert(guc_to_gt(guc), runnable_state == 0);
3096 xe_gt_assert(guc_to_gt(guc), exec_queue_pending_disable(q));
3097
3098 if (q->guc->suspend_pending) {
3099 clear_exec_queue_pending_disable(q);
3100 suspend_fence_signal(q);
3101 } else {
3102 if (exec_queue_banned(q)) {
3103 smp_wmb();
3104 wake_up_all(&guc->ct.wq);
3105 }
3106 if (exec_queue_destroyed(q)) {
3107 /*
3108 * Make sure to clear the pending_disable only
3109 * after sampling the destroyed state. We want
3110 * to ensure we don't trigger the unregister too
3111 * early with something intending to only
3112 * disable scheduling. The caller doing the
3113 * destroy must wait for an ongoing
3114 * pending_disable before marking as destroyed.
3115 */
3116 clear_exec_queue_pending_disable(q);
3117 deregister_exec_queue(guc, q);
3118 } else {
3119 clear_exec_queue_pending_disable(q);
3120 }
3121 }
3122 }
3123 }
3124
handle_multi_queue_secondary_sched_done(struct xe_guc * guc,struct xe_exec_queue * q,u32 runnable_state)3125 static void handle_multi_queue_secondary_sched_done(struct xe_guc *guc,
3126 struct xe_exec_queue *q,
3127 u32 runnable_state)
3128 {
3129 /* Take CT lock here as handle_sched_done() do send a h2g message */
3130 mutex_lock(&guc->ct.lock);
3131 handle_sched_done(guc, q, runnable_state);
3132 mutex_unlock(&guc->ct.lock);
3133 }
3134
xe_guc_sched_done_handler(struct xe_guc * guc,u32 * msg,u32 len)3135 int xe_guc_sched_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
3136 {
3137 struct xe_exec_queue *q;
3138 u32 guc_id, runnable_state;
3139
3140 if (unlikely(len < 2))
3141 return -EPROTO;
3142
3143 guc_id = msg[0];
3144 runnable_state = msg[1];
3145
3146 q = g2h_exec_queue_lookup(guc, guc_id);
3147 if (unlikely(!q))
3148 return -EPROTO;
3149
3150 if (unlikely(!exec_queue_pending_enable(q) &&
3151 !exec_queue_pending_disable(q))) {
3152 xe_gt_err(guc_to_gt(guc),
3153 "SCHED_DONE: Unexpected engine state 0x%04x, guc_id=%d, runnable_state=%u",
3154 atomic_read(&q->guc->state), q->guc->id,
3155 runnable_state);
3156 return -EPROTO;
3157 }
3158
3159 handle_sched_done(guc, q, runnable_state);
3160
3161 return 0;
3162 }
3163
handle_deregister_done(struct xe_guc * guc,struct xe_exec_queue * q)3164 static void handle_deregister_done(struct xe_guc *guc, struct xe_exec_queue *q)
3165 {
3166 trace_xe_exec_queue_deregister_done(q);
3167
3168 clear_exec_queue_registered(q);
3169 __guc_exec_queue_destroy(guc, q);
3170 }
3171
xe_guc_deregister_done_handler(struct xe_guc * guc,u32 * msg,u32 len)3172 int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
3173 {
3174 struct xe_exec_queue *q;
3175 u32 guc_id;
3176
3177 if (unlikely(len < 1))
3178 return -EPROTO;
3179
3180 guc_id = msg[0];
3181
3182 q = g2h_exec_queue_lookup(guc, guc_id);
3183 if (unlikely(!q))
3184 return -EPROTO;
3185
3186 if (!exec_queue_destroyed(q) || exec_queue_pending_disable(q) ||
3187 exec_queue_pending_enable(q) || exec_queue_enabled(q)) {
3188 xe_gt_err(guc_to_gt(guc),
3189 "DEREGISTER_DONE: Unexpected engine state 0x%04x, guc_id=%d",
3190 atomic_read(&q->guc->state), q->guc->id);
3191 return -EPROTO;
3192 }
3193
3194 handle_deregister_done(guc, q);
3195
3196 return 0;
3197 }
3198
xe_guc_exec_queue_reset_handler(struct xe_guc * guc,u32 * msg,u32 len)3199 int xe_guc_exec_queue_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
3200 {
3201 struct xe_gt *gt = guc_to_gt(guc);
3202 struct xe_exec_queue *q;
3203 u32 guc_id;
3204
3205 if (unlikely(len < 1))
3206 return -EPROTO;
3207
3208 guc_id = msg[0];
3209
3210 q = g2h_exec_queue_lookup(guc, guc_id);
3211 if (unlikely(!q))
3212 return -EPROTO;
3213
3214 if (!exec_queue_killed(q))
3215 xe_gt_info(gt, "Engine reset: engine_class=%s, logical_mask: 0x%x, guc_id=%d, state=0x%0x",
3216 xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id,
3217 atomic_read(&q->guc->state));
3218
3219 trace_xe_exec_queue_reset(q);
3220
3221 /*
3222 * A banned engine is a NOP at this point (came from
3223 * guc_exec_queue_timedout_job). Otherwise, kick drm scheduler to cancel
3224 * jobs by setting timeout of the job to the minimum value kicking
3225 * guc_exec_queue_timedout_job.
3226 */
3227 xe_guc_exec_queue_reset_trigger_cleanup(q);
3228
3229 return 0;
3230 }
3231
3232 /*
3233 * xe_guc_error_capture_handler - Handler of GuC captured message
3234 * @guc: The GuC object
3235 * @msg: Point to the message
3236 * @len: The message length
3237 *
3238 * When GuC captured data is ready, GuC will send message
3239 * XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION to host, this function will be
3240 * called 1st to check status before process the data comes with the message.
3241 *
3242 * Returns: error code. 0 if success
3243 */
xe_guc_error_capture_handler(struct xe_guc * guc,u32 * msg,u32 len)3244 int xe_guc_error_capture_handler(struct xe_guc *guc, u32 *msg, u32 len)
3245 {
3246 u32 status;
3247
3248 if (unlikely(len != XE_GUC_ACTION_STATE_CAPTURE_NOTIFICATION_DATA_LEN))
3249 return -EPROTO;
3250
3251 status = msg[0] & XE_GUC_STATE_CAPTURE_EVENT_STATUS_MASK;
3252 if (status == XE_GUC_STATE_CAPTURE_EVENT_STATUS_NOSPACE)
3253 xe_gt_warn(guc_to_gt(guc), "G2H-Error capture no space");
3254
3255 xe_guc_capture_process(guc);
3256
3257 return 0;
3258 }
3259
xe_guc_exec_queue_memory_cat_error_handler(struct xe_guc * guc,u32 * msg,u32 len)3260 int xe_guc_exec_queue_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
3261 u32 len)
3262 {
3263 struct xe_gt *gt = guc_to_gt(guc);
3264 struct xe_exec_queue *q;
3265 u32 guc_id;
3266 u32 type = XE_GUC_CAT_ERR_TYPE_INVALID;
3267
3268 if (unlikely(!len || len > 2))
3269 return -EPROTO;
3270
3271 guc_id = msg[0];
3272
3273 if (len == 2)
3274 type = msg[1];
3275
3276 if (guc_id == GUC_ID_UNKNOWN) {
3277 /*
3278 * GuC uses GUC_ID_UNKNOWN if it can not map the CAT fault to any PF/VF
3279 * context. In such case only PF will be notified about that fault.
3280 */
3281 xe_gt_err_ratelimited(gt, "Memory CAT error reported by GuC!\n");
3282 return 0;
3283 }
3284
3285 q = g2h_exec_queue_lookup(guc, guc_id);
3286 if (unlikely(!q))
3287 return -EPROTO;
3288
3289 /*
3290 * The type is HW-defined and changes based on platform, so we don't
3291 * decode it in the kernel and only check if it is valid.
3292 * See bspec 54047 and 72187 for details.
3293 */
3294 if (type != XE_GUC_CAT_ERR_TYPE_INVALID)
3295 xe_gt_info(gt,
3296 "Engine memory CAT error [%u]: class=%s, logical_mask: 0x%x, guc_id=%d",
3297 type, xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id);
3298 else
3299 xe_gt_info(gt,
3300 "Engine memory CAT error: class=%s, logical_mask: 0x%x, guc_id=%d",
3301 xe_hw_engine_class_to_str(q->class), q->logical_mask, guc_id);
3302
3303 trace_xe_exec_queue_memory_cat_error(q);
3304
3305 /* Treat the same as engine reset */
3306 xe_guc_exec_queue_reset_trigger_cleanup(q);
3307
3308 return 0;
3309 }
3310
xe_guc_uncorrectable_error_handler(struct xe_guc * guc,u32 * msg,u32 len)3311 int xe_guc_uncorrectable_error_handler(struct xe_guc *guc, u32 *msg, u32 len)
3312 {
3313 struct xe_gt *gt = guc_to_gt(guc);
3314 struct xe_exec_queue *q;
3315 u32 guc_id;
3316
3317 if (unlikely(!len || len > 1))
3318 return -EPROTO;
3319
3320 guc_id = msg[0];
3321
3322 if (guc_id == GUC_ID_UNKNOWN) {
3323 xe_gt_err(gt, "GuC: Uncorrectable local error with unknown GuC id\n");
3324 return 0;
3325 }
3326
3327 q = g2h_exec_queue_lookup(guc, guc_id);
3328 if (unlikely(!q))
3329 return -EPROTO;
3330
3331 xe_gt_err(gt,
3332 "GuC: Uncorrectable local error! guc_id=%d class=%s, logical_mask=0x%x",
3333 guc_id, xe_hw_engine_class_to_str(q->class), q->logical_mask);
3334
3335 trace_xe_guc_uncorrectable_error(q);
3336
3337 /* Treat the same as engine reset */
3338 xe_guc_exec_queue_reset_trigger_cleanup(q);
3339
3340 return 0;
3341 }
3342
xe_guc_exec_queue_reset_failure_handler(struct xe_guc * guc,u32 * msg,u32 len)3343 int xe_guc_exec_queue_reset_failure_handler(struct xe_guc *guc, u32 *msg, u32 len)
3344 {
3345 struct xe_gt *gt = guc_to_gt(guc);
3346 u8 guc_class, instance;
3347 u32 reason;
3348
3349 if (unlikely(len != 3))
3350 return -EPROTO;
3351
3352 guc_class = msg[0];
3353 instance = msg[1];
3354 reason = msg[2];
3355
3356 /* Unexpected failure of a hardware feature, log an actual error */
3357 xe_gt_err(gt, "GuC engine reset request failed on %d:%d because 0x%08X",
3358 guc_class, instance, reason);
3359
3360 xe_gt_reset_async(gt);
3361
3362 return 0;
3363 }
3364
xe_guc_exec_queue_cgp_context_error_handler(struct xe_guc * guc,u32 * msg,u32 len)3365 int xe_guc_exec_queue_cgp_context_error_handler(struct xe_guc *guc, u32 *msg,
3366 u32 len)
3367 {
3368 struct xe_gt *gt = guc_to_gt(guc);
3369 struct xe_device *xe = guc_to_xe(guc);
3370 struct xe_exec_queue *q;
3371 u32 guc_id = msg[2];
3372
3373 if (unlikely(len != XE_GUC_EXEC_QUEUE_CGP_CONTEXT_ERROR_LEN)) {
3374 drm_err(&xe->drm, "Invalid length %u", len);
3375 return -EPROTO;
3376 }
3377
3378 q = g2h_exec_queue_lookup(guc, guc_id);
3379 if (unlikely(!q))
3380 return -EPROTO;
3381
3382 xe_gt_dbg(gt,
3383 "CGP context error: [%s] err=0x%x, q0_id=0x%x LRCA=0x%x guc_id=0x%x",
3384 msg[0] & 1 ? "uc" : "kmd", msg[1], msg[2], msg[3], msg[4]);
3385
3386 trace_xe_exec_queue_cgp_context_error(q);
3387
3388 /* Treat the same as engine reset */
3389 xe_guc_exec_queue_reset_trigger_cleanup(q);
3390
3391 return 0;
3392 }
3393
3394 /**
3395 * xe_guc_exec_queue_cgp_sync_done_handler - CGP synchronization done handler
3396 * @guc: guc
3397 * @msg: message indicating CGP sync done
3398 * @len: length of message
3399 *
3400 * Set multi queue group's sync_pending flag to false and wakeup anyone waiting
3401 * for CGP synchronization to complete.
3402 *
3403 * Return: 0 on success, -EPROTO for malformed messages.
3404 */
xe_guc_exec_queue_cgp_sync_done_handler(struct xe_guc * guc,u32 * msg,u32 len)3405 int xe_guc_exec_queue_cgp_sync_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
3406 {
3407 struct xe_device *xe = guc_to_xe(guc);
3408 struct xe_exec_queue *q;
3409 u32 guc_id = msg[0];
3410
3411 if (unlikely(len < 1)) {
3412 drm_err(&xe->drm, "Invalid CGP_SYNC_DONE length %u", len);
3413 return -EPROTO;
3414 }
3415
3416 q = g2h_exec_queue_lookup(guc, guc_id);
3417 if (unlikely(!q))
3418 return -EPROTO;
3419
3420 if (!xe_exec_queue_is_multi_queue_primary(q)) {
3421 drm_err(&xe->drm, "Unexpected CGP_SYNC_DONE response");
3422 return -EPROTO;
3423 }
3424
3425 /* Wakeup the serialized cgp update wait */
3426 WRITE_ONCE(q->multi_queue.group->sync_pending, false);
3427 xe_guc_ct_wake_waiters(&guc->ct);
3428
3429 return 0;
3430 }
3431
3432 static void
guc_exec_queue_wq_snapshot_capture(struct xe_exec_queue * q,struct xe_guc_submit_exec_queue_snapshot * snapshot)3433 guc_exec_queue_wq_snapshot_capture(struct xe_exec_queue *q,
3434 struct xe_guc_submit_exec_queue_snapshot *snapshot)
3435 {
3436 struct xe_guc *guc = exec_queue_to_guc(q);
3437 struct xe_device *xe = guc_to_xe(guc);
3438 struct iosys_map map = xe_lrc_parallel_map(q->lrc[0]);
3439 int i;
3440
3441 snapshot->guc.wqi_head = q->guc->wqi_head;
3442 snapshot->guc.wqi_tail = q->guc->wqi_tail;
3443 snapshot->parallel.wq_desc.head = parallel_read(xe, map, wq_desc.head);
3444 snapshot->parallel.wq_desc.tail = parallel_read(xe, map, wq_desc.tail);
3445 snapshot->parallel.wq_desc.status = parallel_read(xe, map,
3446 wq_desc.wq_status);
3447
3448 if (snapshot->parallel.wq_desc.head !=
3449 snapshot->parallel.wq_desc.tail) {
3450 for (i = snapshot->parallel.wq_desc.head;
3451 i != snapshot->parallel.wq_desc.tail;
3452 i = (i + sizeof(u32)) % WQ_SIZE)
3453 snapshot->parallel.wq[i / sizeof(u32)] =
3454 parallel_read(xe, map, wq[i / sizeof(u32)]);
3455 }
3456 }
3457
3458 static void
guc_exec_queue_wq_snapshot_print(struct xe_guc_submit_exec_queue_snapshot * snapshot,struct drm_printer * p)3459 guc_exec_queue_wq_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot,
3460 struct drm_printer *p)
3461 {
3462 int i;
3463
3464 drm_printf(p, "\tWQ head: %u (internal), %d (memory)\n",
3465 snapshot->guc.wqi_head, snapshot->parallel.wq_desc.head);
3466 drm_printf(p, "\tWQ tail: %u (internal), %d (memory)\n",
3467 snapshot->guc.wqi_tail, snapshot->parallel.wq_desc.tail);
3468 drm_printf(p, "\tWQ status: %u\n", snapshot->parallel.wq_desc.status);
3469
3470 if (snapshot->parallel.wq_desc.head !=
3471 snapshot->parallel.wq_desc.tail) {
3472 for (i = snapshot->parallel.wq_desc.head;
3473 i != snapshot->parallel.wq_desc.tail;
3474 i = (i + sizeof(u32)) % WQ_SIZE)
3475 drm_printf(p, "\tWQ[%zu]: 0x%08x\n", i / sizeof(u32),
3476 snapshot->parallel.wq[i / sizeof(u32)]);
3477 }
3478 }
3479
3480 /**
3481 * xe_guc_exec_queue_snapshot_capture - Take a quick snapshot of the GuC Engine.
3482 * @q: faulty exec queue
3483 *
3484 * This can be printed out in a later stage like during dev_coredump
3485 * analysis.
3486 *
3487 * Returns: a GuC Submit Engine snapshot object that must be freed by the
3488 * caller, using `xe_guc_exec_queue_snapshot_free`.
3489 */
3490 struct xe_guc_submit_exec_queue_snapshot *
xe_guc_exec_queue_snapshot_capture(struct xe_exec_queue * q)3491 xe_guc_exec_queue_snapshot_capture(struct xe_exec_queue *q)
3492 {
3493 struct xe_gpu_scheduler *sched = &q->guc->sched;
3494 struct xe_guc_submit_exec_queue_snapshot *snapshot;
3495 int i;
3496
3497 snapshot = kzalloc_obj(*snapshot, GFP_ATOMIC);
3498
3499 if (!snapshot)
3500 return NULL;
3501
3502 snapshot->guc.id = q->guc->id;
3503 memcpy(&snapshot->name, &q->name, sizeof(snapshot->name));
3504 snapshot->class = q->class;
3505 snapshot->logical_mask = q->logical_mask;
3506 snapshot->width = q->width;
3507 snapshot->refcount = kref_read(&q->refcount);
3508 snapshot->sched_timeout = sched->base.timeout;
3509 snapshot->sched_props.timeslice_us = q->sched_props.timeslice_us;
3510 snapshot->sched_props.preempt_timeout_us =
3511 q->sched_props.preempt_timeout_us;
3512
3513 snapshot->lrc = kmalloc_objs(struct xe_lrc_snapshot *, q->width,
3514 GFP_ATOMIC);
3515
3516 if (snapshot->lrc) {
3517 for (i = 0; i < q->width; ++i) {
3518 struct xe_lrc *lrc = q->lrc[i];
3519
3520 snapshot->lrc[i] = xe_lrc_snapshot_capture(lrc);
3521 }
3522 }
3523
3524 snapshot->schedule_state = atomic_read(&q->guc->state);
3525 snapshot->exec_queue_flags = q->flags;
3526
3527 snapshot->parallel_execution = xe_exec_queue_is_parallel(q);
3528 if (snapshot->parallel_execution)
3529 guc_exec_queue_wq_snapshot_capture(q, snapshot);
3530
3531 if (xe_exec_queue_is_multi_queue(q)) {
3532 snapshot->multi_queue.valid = true;
3533 snapshot->multi_queue.primary = xe_exec_queue_multi_queue_primary(q)->guc->id;
3534 snapshot->multi_queue.pos = q->multi_queue.pos;
3535 }
3536
3537 return snapshot;
3538 }
3539
3540 /**
3541 * xe_guc_exec_queue_snapshot_capture_delayed - Take delayed part of snapshot of the GuC Engine.
3542 * @snapshot: Previously captured snapshot of job.
3543 *
3544 * This captures some data that requires taking some locks, so it cannot be done in signaling path.
3545 */
3546 void
xe_guc_exec_queue_snapshot_capture_delayed(struct xe_guc_submit_exec_queue_snapshot * snapshot)3547 xe_guc_exec_queue_snapshot_capture_delayed(struct xe_guc_submit_exec_queue_snapshot *snapshot)
3548 {
3549 int i;
3550
3551 if (!snapshot || !snapshot->lrc)
3552 return;
3553
3554 for (i = 0; i < snapshot->width; ++i)
3555 xe_lrc_snapshot_capture_delayed(snapshot->lrc[i]);
3556 }
3557
3558 /**
3559 * xe_guc_exec_queue_snapshot_print - Print out a given GuC Engine snapshot.
3560 * @snapshot: GuC Submit Engine snapshot object.
3561 * @p: drm_printer where it will be printed out.
3562 *
3563 * This function prints out a given GuC Submit Engine snapshot object.
3564 */
3565 void
xe_guc_exec_queue_snapshot_print(struct xe_guc_submit_exec_queue_snapshot * snapshot,struct drm_printer * p)3566 xe_guc_exec_queue_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot,
3567 struct drm_printer *p)
3568 {
3569 int i;
3570
3571 if (!snapshot)
3572 return;
3573
3574 drm_printf(p, "GuC ID: %d\n", snapshot->guc.id);
3575 drm_printf(p, "\tName: %s\n", snapshot->name);
3576 drm_printf(p, "\tClass: %d\n", snapshot->class);
3577 drm_printf(p, "\tLogical mask: 0x%x\n", snapshot->logical_mask);
3578 drm_printf(p, "\tWidth: %d\n", snapshot->width);
3579 drm_printf(p, "\tRef: %d\n", snapshot->refcount);
3580 drm_printf(p, "\tTimeout: %ld (ms)\n", snapshot->sched_timeout);
3581 drm_printf(p, "\tTimeslice: %u (us)\n",
3582 snapshot->sched_props.timeslice_us);
3583 drm_printf(p, "\tPreempt timeout: %u (us)\n",
3584 snapshot->sched_props.preempt_timeout_us);
3585
3586 for (i = 0; snapshot->lrc && i < snapshot->width; ++i)
3587 xe_lrc_snapshot_print(snapshot->lrc[i], p);
3588
3589 drm_printf(p, "\tSchedule State: 0x%x\n", snapshot->schedule_state);
3590 drm_printf(p, "\tFlags: 0x%lx\n", snapshot->exec_queue_flags);
3591
3592 if (snapshot->parallel_execution)
3593 guc_exec_queue_wq_snapshot_print(snapshot, p);
3594
3595 if (snapshot->multi_queue.valid) {
3596 drm_printf(p, "\tMulti queue primary GuC ID: %d\n", snapshot->multi_queue.primary);
3597 drm_printf(p, "\tMulti queue position: %d\n", snapshot->multi_queue.pos);
3598 }
3599 }
3600
3601 /**
3602 * xe_guc_exec_queue_snapshot_free - Free all allocated objects for a given
3603 * snapshot.
3604 * @snapshot: GuC Submit Engine snapshot object.
3605 *
3606 * This function free all the memory that needed to be allocated at capture
3607 * time.
3608 */
xe_guc_exec_queue_snapshot_free(struct xe_guc_submit_exec_queue_snapshot * snapshot)3609 void xe_guc_exec_queue_snapshot_free(struct xe_guc_submit_exec_queue_snapshot *snapshot)
3610 {
3611 int i;
3612
3613 if (!snapshot)
3614 return;
3615
3616 if (snapshot->lrc) {
3617 for (i = 0; i < snapshot->width; i++)
3618 xe_lrc_snapshot_free(snapshot->lrc[i]);
3619 kfree(snapshot->lrc);
3620 }
3621 kfree(snapshot);
3622 }
3623
guc_exec_queue_print(struct xe_exec_queue * q,struct drm_printer * p)3624 static void guc_exec_queue_print(struct xe_exec_queue *q, struct drm_printer *p)
3625 {
3626 struct xe_guc_submit_exec_queue_snapshot *snapshot;
3627
3628 snapshot = xe_guc_exec_queue_snapshot_capture(q);
3629 xe_guc_exec_queue_snapshot_print(snapshot, p);
3630 xe_guc_exec_queue_snapshot_free(snapshot);
3631 }
3632
3633 /**
3634 * xe_guc_register_vf_exec_queue - Register exec queue for a given context type.
3635 * @q: Execution queue
3636 * @ctx_type: Type of the context
3637 *
3638 * This function registers the execution queue with the guc. Special context
3639 * types like GUC_CONTEXT_COMPRESSION_SAVE and GUC_CONTEXT_COMPRESSION_RESTORE
3640 * are only applicable for IGPU and in the VF.
3641 * Submits the execution queue to GUC after registering it.
3642 *
3643 * Returns - None.
3644 */
xe_guc_register_vf_exec_queue(struct xe_exec_queue * q,int ctx_type)3645 void xe_guc_register_vf_exec_queue(struct xe_exec_queue *q, int ctx_type)
3646 {
3647 struct xe_guc *guc = exec_queue_to_guc(q);
3648 struct xe_device *xe = guc_to_xe(guc);
3649 struct xe_gt *gt = guc_to_gt(guc);
3650
3651 xe_gt_assert(gt, IS_SRIOV_VF(xe));
3652 xe_gt_assert(gt, !IS_DGFX(xe));
3653 xe_gt_assert(gt, ctx_type == GUC_CONTEXT_COMPRESSION_SAVE ||
3654 ctx_type == GUC_CONTEXT_COMPRESSION_RESTORE);
3655 xe_gt_assert(gt, GUC_SUBMIT_VER(guc) >= MAKE_GUC_VER(1, 23, 0));
3656
3657 register_exec_queue(q, ctx_type);
3658 enable_scheduling(q);
3659 }
3660
3661 /**
3662 * xe_guc_submit_print - GuC Submit Print.
3663 * @guc: GuC.
3664 * @p: drm_printer where it will be printed out.
3665 *
3666 * This function capture and prints snapshots of **all** GuC Engines.
3667 */
xe_guc_submit_print(struct xe_guc * guc,struct drm_printer * p)3668 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p)
3669 {
3670 struct xe_exec_queue *q;
3671 unsigned long index;
3672
3673 if (!xe_device_uc_enabled(guc_to_xe(guc)))
3674 return;
3675
3676 mutex_lock(&guc->submission_state.lock);
3677 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
3678 guc_exec_queue_print(q, p);
3679 mutex_unlock(&guc->submission_state.lock);
3680 }
3681
3682 /**
3683 * xe_guc_has_registered_mlrc_queues - check whether there are any MLRC queues
3684 * registered with the GuC
3685 * @guc: GuC.
3686 *
3687 * Return: true if any MLRC queue is registered with the GuC, false otherwise.
3688 */
xe_guc_has_registered_mlrc_queues(struct xe_guc * guc)3689 bool xe_guc_has_registered_mlrc_queues(struct xe_guc *guc)
3690 {
3691 struct xe_exec_queue *q;
3692 unsigned long index;
3693
3694 guard(mutex)(&guc->submission_state.lock);
3695
3696 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
3697 if (q->width > 1)
3698 return true;
3699
3700 return false;
3701 }
3702
3703 /**
3704 * xe_guc_contexts_hwsp_rebase - Re-compute GGTT references within all
3705 * exec queues registered to given GuC.
3706 * @guc: the &xe_guc struct instance
3707 * @scratch: scratch buffer to be used as temporary storage
3708 *
3709 * Returns: zero on success, negative error code on failure.
3710 */
xe_guc_contexts_hwsp_rebase(struct xe_guc * guc,void * scratch)3711 int xe_guc_contexts_hwsp_rebase(struct xe_guc *guc, void *scratch)
3712 {
3713 struct xe_exec_queue *q;
3714 unsigned long index;
3715 int err = 0;
3716
3717 mutex_lock(&guc->submission_state.lock);
3718 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q) {
3719 /* Prevent redundant attempts to stop parallel queues */
3720 if (q->guc->id != index)
3721 continue;
3722
3723 err = xe_exec_queue_contexts_hwsp_rebase(q, scratch);
3724 if (err)
3725 break;
3726 }
3727 mutex_unlock(&guc->submission_state.lock);
3728
3729 return err;
3730 }
3731