1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022 Intel Corporation
6 #include "xe_guc_submit.h"
8 #include <linux/bitfield.h>
9 #include <linux/bitmap.h>
10 #include <linux/circ_buf.h>
11 #include <linux/delay.h>
12 #include <linux/dma-fence-array.h>
14 #include <drm/drm_managed.h>
16 #include "abi/guc_actions_abi.h"
17 #include "abi/guc_klvs_abi.h"
18 #include "regs/xe_lrc_layout.h"
19 #include "xe_assert.h"
20 #include "xe_devcoredump.h"
21 #include "xe_device.h"
22 #include "xe_exec_queue.h"
23 #include "xe_force_wake.h"
24 #include "xe_gpu_scheduler.h"
27 #include "xe_guc_ct.h"
28 #include "xe_guc_exec_queue_types.h"
29 #include "xe_guc_submit_types.h"
30 #include "xe_hw_engine.h"
31 #include "xe_hw_fence.h"
33 #include "xe_macros.h"
36 #include "xe_ring_ops_types.h"
37 #include "xe_sched_job.h"
41 static struct xe_guc *
42 exec_queue_to_guc(struct xe_exec_queue *q)
44 return &q->gt->uc.guc;
48 * Helpers for engine state, using an atomic as some of the bits can transition
49 * as the same time (e.g. a suspend can be happning at the same time as schedule
50 * engine done being processed).
52 #define EXEC_QUEUE_STATE_REGISTERED (1 << 0)
53 #define ENGINE_STATE_ENABLED (1 << 1)
54 #define EXEC_QUEUE_STATE_PENDING_ENABLE (1 << 2)
55 #define EXEC_QUEUE_STATE_PENDING_DISABLE (1 << 3)
56 #define EXEC_QUEUE_STATE_DESTROYED (1 << 4)
57 #define ENGINE_STATE_SUSPENDED (1 << 5)
58 #define EXEC_QUEUE_STATE_RESET (1 << 6)
59 #define ENGINE_STATE_KILLED (1 << 7)
61 static bool exec_queue_registered(struct xe_exec_queue *q)
63 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_REGISTERED;
66 static void set_exec_queue_registered(struct xe_exec_queue *q)
68 atomic_or(EXEC_QUEUE_STATE_REGISTERED, &q->guc->state);
71 static void clear_exec_queue_registered(struct xe_exec_queue *q)
73 atomic_and(~EXEC_QUEUE_STATE_REGISTERED, &q->guc->state);
76 static bool exec_queue_enabled(struct xe_exec_queue *q)
78 return atomic_read(&q->guc->state) & ENGINE_STATE_ENABLED;
81 static void set_exec_queue_enabled(struct xe_exec_queue *q)
83 atomic_or(ENGINE_STATE_ENABLED, &q->guc->state);
86 static void clear_exec_queue_enabled(struct xe_exec_queue *q)
88 atomic_and(~ENGINE_STATE_ENABLED, &q->guc->state);
91 static bool exec_queue_pending_enable(struct xe_exec_queue *q)
93 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_ENABLE;
96 static void set_exec_queue_pending_enable(struct xe_exec_queue *q)
98 atomic_or(EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state);
101 static void clear_exec_queue_pending_enable(struct xe_exec_queue *q)
103 atomic_and(~EXEC_QUEUE_STATE_PENDING_ENABLE, &q->guc->state);
106 static bool exec_queue_pending_disable(struct xe_exec_queue *q)
108 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_PENDING_DISABLE;
111 static void set_exec_queue_pending_disable(struct xe_exec_queue *q)
113 atomic_or(EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state);
116 static void clear_exec_queue_pending_disable(struct xe_exec_queue *q)
118 atomic_and(~EXEC_QUEUE_STATE_PENDING_DISABLE, &q->guc->state);
121 static bool exec_queue_destroyed(struct xe_exec_queue *q)
123 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_DESTROYED;
126 static void set_exec_queue_destroyed(struct xe_exec_queue *q)
128 atomic_or(EXEC_QUEUE_STATE_DESTROYED, &q->guc->state);
131 static bool exec_queue_banned(struct xe_exec_queue *q)
133 return (q->flags & EXEC_QUEUE_FLAG_BANNED);
136 static void set_exec_queue_banned(struct xe_exec_queue *q)
138 q->flags |= EXEC_QUEUE_FLAG_BANNED;
141 static bool exec_queue_suspended(struct xe_exec_queue *q)
143 return atomic_read(&q->guc->state) & ENGINE_STATE_SUSPENDED;
146 static void set_exec_queue_suspended(struct xe_exec_queue *q)
148 atomic_or(ENGINE_STATE_SUSPENDED, &q->guc->state);
151 static void clear_exec_queue_suspended(struct xe_exec_queue *q)
153 atomic_and(~ENGINE_STATE_SUSPENDED, &q->guc->state);
156 static bool exec_queue_reset(struct xe_exec_queue *q)
158 return atomic_read(&q->guc->state) & EXEC_QUEUE_STATE_RESET;
161 static void set_exec_queue_reset(struct xe_exec_queue *q)
163 atomic_or(EXEC_QUEUE_STATE_RESET, &q->guc->state);
166 static bool exec_queue_killed(struct xe_exec_queue *q)
168 return atomic_read(&q->guc->state) & ENGINE_STATE_KILLED;
171 static void set_exec_queue_killed(struct xe_exec_queue *q)
173 atomic_or(ENGINE_STATE_KILLED, &q->guc->state);
176 static bool exec_queue_killed_or_banned(struct xe_exec_queue *q)
178 return exec_queue_killed(q) || exec_queue_banned(q);
181 #ifdef CONFIG_PROVE_LOCKING
182 static int alloc_submit_wq(struct xe_guc *guc)
186 for (i = 0; i < NUM_SUBMIT_WQ; ++i) {
187 guc->submission_state.submit_wq_pool[i] =
188 alloc_ordered_workqueue("submit_wq", 0);
189 if (!guc->submission_state.submit_wq_pool[i])
197 destroy_workqueue(guc->submission_state.submit_wq_pool[--i]);
202 static void free_submit_wq(struct xe_guc *guc)
206 for (i = 0; i < NUM_SUBMIT_WQ; ++i)
207 destroy_workqueue(guc->submission_state.submit_wq_pool[i]);
210 static struct workqueue_struct *get_submit_wq(struct xe_guc *guc)
212 int idx = guc->submission_state.submit_wq_idx++ % NUM_SUBMIT_WQ;
214 return guc->submission_state.submit_wq_pool[idx];
217 static int alloc_submit_wq(struct xe_guc *guc)
222 static void free_submit_wq(struct xe_guc *guc)
227 static struct workqueue_struct *get_submit_wq(struct xe_guc *guc)
233 static void guc_submit_fini(struct drm_device *drm, void *arg)
235 struct xe_guc *guc = arg;
237 xa_destroy(&guc->submission_state.exec_queue_lookup);
238 ida_destroy(&guc->submission_state.guc_ids);
239 bitmap_free(guc->submission_state.guc_ids_bitmap);
241 mutex_destroy(&guc->submission_state.lock);
244 #define GUC_ID_MAX 65535
245 #define GUC_ID_NUMBER_MLRC 4096
246 #define GUC_ID_NUMBER_SLRC (GUC_ID_MAX - GUC_ID_NUMBER_MLRC)
247 #define GUC_ID_START_MLRC GUC_ID_NUMBER_SLRC
249 static const struct xe_exec_queue_ops guc_exec_queue_ops;
251 static void primelockdep(struct xe_guc *guc)
253 if (!IS_ENABLED(CONFIG_LOCKDEP))
256 fs_reclaim_acquire(GFP_KERNEL);
258 mutex_lock(&guc->submission_state.lock);
259 might_lock(&guc->submission_state.suspend.lock);
260 mutex_unlock(&guc->submission_state.lock);
262 fs_reclaim_release(GFP_KERNEL);
265 int xe_guc_submit_init(struct xe_guc *guc)
267 struct xe_device *xe = guc_to_xe(guc);
268 struct xe_gt *gt = guc_to_gt(guc);
271 guc->submission_state.guc_ids_bitmap =
272 bitmap_zalloc(GUC_ID_NUMBER_MLRC, GFP_KERNEL);
273 if (!guc->submission_state.guc_ids_bitmap)
276 err = alloc_submit_wq(guc);
278 bitmap_free(guc->submission_state.guc_ids_bitmap);
282 gt->exec_queue_ops = &guc_exec_queue_ops;
284 mutex_init(&guc->submission_state.lock);
285 xa_init(&guc->submission_state.exec_queue_lookup);
286 ida_init(&guc->submission_state.guc_ids);
288 spin_lock_init(&guc->submission_state.suspend.lock);
289 guc->submission_state.suspend.context = dma_fence_context_alloc(1);
293 err = drmm_add_action_or_reset(&xe->drm, guc_submit_fini, guc);
300 static void __release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q, u32 xa_count)
304 lockdep_assert_held(&guc->submission_state.lock);
306 for (i = 0; i < xa_count; ++i)
307 xa_erase(&guc->submission_state.exec_queue_lookup, q->guc->id + i);
309 if (xe_exec_queue_is_parallel(q))
310 bitmap_release_region(guc->submission_state.guc_ids_bitmap,
311 q->guc->id - GUC_ID_START_MLRC,
312 order_base_2(q->width));
314 ida_simple_remove(&guc->submission_state.guc_ids, q->guc->id);
317 static int alloc_guc_id(struct xe_guc *guc, struct xe_exec_queue *q)
324 * Must use GFP_NOWAIT as this lock is in the dma fence signalling path,
325 * worse case user gets -ENOMEM on engine create and has to try again.
327 * FIXME: Have caller pre-alloc or post-alloc /w GFP_KERNEL to prevent
330 lockdep_assert_held(&guc->submission_state.lock);
332 if (xe_exec_queue_is_parallel(q)) {
333 void *bitmap = guc->submission_state.guc_ids_bitmap;
335 ret = bitmap_find_free_region(bitmap, GUC_ID_NUMBER_MLRC,
336 order_base_2(q->width));
338 ret = ida_simple_get(&guc->submission_state.guc_ids, 0,
339 GUC_ID_NUMBER_SLRC, GFP_NOWAIT);
345 if (xe_exec_queue_is_parallel(q))
346 q->guc->id += GUC_ID_START_MLRC;
348 for (i = 0; i < q->width; ++i) {
349 ptr = xa_store(&guc->submission_state.exec_queue_lookup,
350 q->guc->id + i, q, GFP_NOWAIT);
360 __release_guc_id(guc, q, i);
365 static void release_guc_id(struct xe_guc *guc, struct xe_exec_queue *q)
367 mutex_lock(&guc->submission_state.lock);
368 __release_guc_id(guc, q, q->width);
369 mutex_unlock(&guc->submission_state.lock);
372 struct exec_queue_policy {
374 struct guc_update_exec_queue_policy h2g;
377 static u32 __guc_exec_queue_policy_action_size(struct exec_queue_policy *policy)
379 size_t bytes = sizeof(policy->h2g.header) +
380 (sizeof(policy->h2g.klv[0]) * policy->count);
382 return bytes / sizeof(u32);
385 static void __guc_exec_queue_policy_start_klv(struct exec_queue_policy *policy,
388 policy->h2g.header.action =
389 XE_GUC_ACTION_HOST2GUC_UPDATE_CONTEXT_POLICIES;
390 policy->h2g.header.guc_id = guc_id;
394 #define MAKE_EXEC_QUEUE_POLICY_ADD(func, id) \
395 static void __guc_exec_queue_policy_add_##func(struct exec_queue_policy *policy, \
398 XE_WARN_ON(policy->count >= GUC_CONTEXT_POLICIES_KLV_NUM_IDS); \
400 policy->h2g.klv[policy->count].kl = \
401 FIELD_PREP(GUC_KLV_0_KEY, \
402 GUC_CONTEXT_POLICIES_KLV_ID_##id) | \
403 FIELD_PREP(GUC_KLV_0_LEN, 1); \
404 policy->h2g.klv[policy->count].value = data; \
408 MAKE_EXEC_QUEUE_POLICY_ADD(execution_quantum, EXECUTION_QUANTUM)
409 MAKE_EXEC_QUEUE_POLICY_ADD(preemption_timeout, PREEMPTION_TIMEOUT)
410 MAKE_EXEC_QUEUE_POLICY_ADD(priority, SCHEDULING_PRIORITY)
411 #undef MAKE_EXEC_QUEUE_POLICY_ADD
413 static const int xe_exec_queue_prio_to_guc[] = {
414 [XE_EXEC_QUEUE_PRIORITY_LOW] = GUC_CLIENT_PRIORITY_NORMAL,
415 [XE_EXEC_QUEUE_PRIORITY_NORMAL] = GUC_CLIENT_PRIORITY_KMD_NORMAL,
416 [XE_EXEC_QUEUE_PRIORITY_HIGH] = GUC_CLIENT_PRIORITY_HIGH,
417 [XE_EXEC_QUEUE_PRIORITY_KERNEL] = GUC_CLIENT_PRIORITY_KMD_HIGH,
420 static void init_policies(struct xe_guc *guc, struct xe_exec_queue *q)
422 struct exec_queue_policy policy;
423 struct xe_device *xe = guc_to_xe(guc);
424 enum xe_exec_queue_priority prio = q->sched_props.priority;
425 u32 timeslice_us = q->sched_props.timeslice_us;
426 u32 preempt_timeout_us = q->sched_props.preempt_timeout_us;
428 xe_assert(xe, exec_queue_registered(q));
430 __guc_exec_queue_policy_start_klv(&policy, q->guc->id);
431 __guc_exec_queue_policy_add_priority(&policy, xe_exec_queue_prio_to_guc[prio]);
432 __guc_exec_queue_policy_add_execution_quantum(&policy, timeslice_us);
433 __guc_exec_queue_policy_add_preemption_timeout(&policy, preempt_timeout_us);
435 xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g,
436 __guc_exec_queue_policy_action_size(&policy), 0, 0);
439 static void set_min_preemption_timeout(struct xe_guc *guc, struct xe_exec_queue *q)
441 struct exec_queue_policy policy;
443 __guc_exec_queue_policy_start_klv(&policy, q->guc->id);
444 __guc_exec_queue_policy_add_preemption_timeout(&policy, 1);
446 xe_guc_ct_send(&guc->ct, (u32 *)&policy.h2g,
447 __guc_exec_queue_policy_action_size(&policy), 0, 0);
450 #define parallel_read(xe_, map_, field_) \
451 xe_map_rd_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \
453 #define parallel_write(xe_, map_, field_, val_) \
454 xe_map_wr_field(xe_, &map_, 0, struct guc_submit_parallel_scratch, \
457 static void __register_mlrc_engine(struct xe_guc *guc,
458 struct xe_exec_queue *q,
459 struct guc_ctxt_registration_info *info)
461 #define MAX_MLRC_REG_SIZE (13 + XE_HW_ENGINE_MAX_INSTANCE * 2)
462 struct xe_device *xe = guc_to_xe(guc);
463 u32 action[MAX_MLRC_REG_SIZE];
467 xe_assert(xe, xe_exec_queue_is_parallel(q));
469 action[len++] = XE_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
470 action[len++] = info->flags;
471 action[len++] = info->context_idx;
472 action[len++] = info->engine_class;
473 action[len++] = info->engine_submit_mask;
474 action[len++] = info->wq_desc_lo;
475 action[len++] = info->wq_desc_hi;
476 action[len++] = info->wq_base_lo;
477 action[len++] = info->wq_base_hi;
478 action[len++] = info->wq_size;
479 action[len++] = q->width;
480 action[len++] = info->hwlrca_lo;
481 action[len++] = info->hwlrca_hi;
483 for (i = 1; i < q->width; ++i) {
484 struct xe_lrc *lrc = q->lrc + i;
486 action[len++] = lower_32_bits(xe_lrc_descriptor(lrc));
487 action[len++] = upper_32_bits(xe_lrc_descriptor(lrc));
490 xe_assert(xe, len <= MAX_MLRC_REG_SIZE);
491 #undef MAX_MLRC_REG_SIZE
493 xe_guc_ct_send(&guc->ct, action, len, 0, 0);
496 static void __register_engine(struct xe_guc *guc,
497 struct guc_ctxt_registration_info *info)
500 XE_GUC_ACTION_REGISTER_CONTEXT,
504 info->engine_submit_mask,
514 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
517 static void register_engine(struct xe_exec_queue *q)
519 struct xe_guc *guc = exec_queue_to_guc(q);
520 struct xe_device *xe = guc_to_xe(guc);
521 struct xe_lrc *lrc = q->lrc;
522 struct guc_ctxt_registration_info info;
524 xe_assert(xe, !exec_queue_registered(q));
526 memset(&info, 0, sizeof(info));
527 info.context_idx = q->guc->id;
528 info.engine_class = xe_engine_class_to_guc_class(q->class);
529 info.engine_submit_mask = q->logical_mask;
530 info.hwlrca_lo = lower_32_bits(xe_lrc_descriptor(lrc));
531 info.hwlrca_hi = upper_32_bits(xe_lrc_descriptor(lrc));
532 info.flags = CONTEXT_REGISTRATION_FLAG_KMD;
534 if (xe_exec_queue_is_parallel(q)) {
535 u32 ggtt_addr = xe_lrc_parallel_ggtt_addr(lrc);
536 struct iosys_map map = xe_lrc_parallel_map(lrc);
538 info.wq_desc_lo = lower_32_bits(ggtt_addr +
539 offsetof(struct guc_submit_parallel_scratch, wq_desc));
540 info.wq_desc_hi = upper_32_bits(ggtt_addr +
541 offsetof(struct guc_submit_parallel_scratch, wq_desc));
542 info.wq_base_lo = lower_32_bits(ggtt_addr +
543 offsetof(struct guc_submit_parallel_scratch, wq[0]));
544 info.wq_base_hi = upper_32_bits(ggtt_addr +
545 offsetof(struct guc_submit_parallel_scratch, wq[0]));
546 info.wq_size = WQ_SIZE;
548 q->guc->wqi_head = 0;
549 q->guc->wqi_tail = 0;
550 xe_map_memset(xe, &map, 0, 0, PARALLEL_SCRATCH_SIZE - WQ_SIZE);
551 parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
555 * We must keep a reference for LR engines if engine is registered with
556 * the GuC as jobs signal immediately and can't destroy an engine if the
557 * GuC has a reference to it.
559 if (xe_exec_queue_is_lr(q))
560 xe_exec_queue_get(q);
562 set_exec_queue_registered(q);
563 trace_xe_exec_queue_register(q);
564 if (xe_exec_queue_is_parallel(q))
565 __register_mlrc_engine(guc, q, &info);
567 __register_engine(guc, &info);
568 init_policies(guc, q);
571 static u32 wq_space_until_wrap(struct xe_exec_queue *q)
573 return (WQ_SIZE - q->guc->wqi_tail);
576 static int wq_wait_for_space(struct xe_exec_queue *q, u32 wqi_size)
578 struct xe_guc *guc = exec_queue_to_guc(q);
579 struct xe_device *xe = guc_to_xe(guc);
580 struct iosys_map map = xe_lrc_parallel_map(q->lrc);
581 unsigned int sleep_period_ms = 1;
583 #define AVAILABLE_SPACE \
584 CIRC_SPACE(q->guc->wqi_tail, q->guc->wqi_head, WQ_SIZE)
585 if (wqi_size > AVAILABLE_SPACE) {
587 q->guc->wqi_head = parallel_read(xe, map, wq_desc.head);
588 if (wqi_size > AVAILABLE_SPACE) {
589 if (sleep_period_ms == 1024) {
590 xe_gt_reset_async(q->gt);
594 msleep(sleep_period_ms);
595 sleep_period_ms <<= 1;
599 #undef AVAILABLE_SPACE
604 static int wq_noop_append(struct xe_exec_queue *q)
606 struct xe_guc *guc = exec_queue_to_guc(q);
607 struct xe_device *xe = guc_to_xe(guc);
608 struct iosys_map map = xe_lrc_parallel_map(q->lrc);
609 u32 len_dw = wq_space_until_wrap(q) / sizeof(u32) - 1;
611 if (wq_wait_for_space(q, wq_space_until_wrap(q)))
614 xe_assert(xe, FIELD_FIT(WQ_LEN_MASK, len_dw));
616 parallel_write(xe, map, wq[q->guc->wqi_tail / sizeof(u32)],
617 FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_NOOP) |
618 FIELD_PREP(WQ_LEN_MASK, len_dw));
619 q->guc->wqi_tail = 0;
624 static void wq_item_append(struct xe_exec_queue *q)
626 struct xe_guc *guc = exec_queue_to_guc(q);
627 struct xe_device *xe = guc_to_xe(guc);
628 struct iosys_map map = xe_lrc_parallel_map(q->lrc);
629 #define WQ_HEADER_SIZE 4 /* Includes 1 LRC address too */
630 u32 wqi[XE_HW_ENGINE_MAX_INSTANCE + (WQ_HEADER_SIZE - 1)];
631 u32 wqi_size = (q->width + (WQ_HEADER_SIZE - 1)) * sizeof(u32);
632 u32 len_dw = (wqi_size / sizeof(u32)) - 1;
635 if (wqi_size > wq_space_until_wrap(q)) {
636 if (wq_noop_append(q))
639 if (wq_wait_for_space(q, wqi_size))
642 wqi[i++] = FIELD_PREP(WQ_TYPE_MASK, WQ_TYPE_MULTI_LRC) |
643 FIELD_PREP(WQ_LEN_MASK, len_dw);
644 wqi[i++] = xe_lrc_descriptor(q->lrc);
645 wqi[i++] = FIELD_PREP(WQ_GUC_ID_MASK, q->guc->id) |
646 FIELD_PREP(WQ_RING_TAIL_MASK, q->lrc->ring.tail / sizeof(u64));
648 for (j = 1; j < q->width; ++j) {
649 struct xe_lrc *lrc = q->lrc + j;
651 wqi[i++] = lrc->ring.tail / sizeof(u64);
654 xe_assert(xe, i == wqi_size / sizeof(u32));
656 iosys_map_incr(&map, offsetof(struct guc_submit_parallel_scratch,
657 wq[q->guc->wqi_tail / sizeof(u32)]));
658 xe_map_memcpy_to(xe, &map, 0, wqi, wqi_size);
659 q->guc->wqi_tail += wqi_size;
660 xe_assert(xe, q->guc->wqi_tail <= WQ_SIZE);
664 map = xe_lrc_parallel_map(q->lrc);
665 parallel_write(xe, map, wq_desc.tail, q->guc->wqi_tail);
668 #define RESUME_PENDING ~0x0ull
669 static void submit_exec_queue(struct xe_exec_queue *q)
671 struct xe_guc *guc = exec_queue_to_guc(q);
672 struct xe_device *xe = guc_to_xe(guc);
673 struct xe_lrc *lrc = q->lrc;
678 bool extra_submit = false;
680 xe_assert(xe, exec_queue_registered(q));
682 if (xe_exec_queue_is_parallel(q))
685 xe_lrc_write_ctx_reg(lrc, CTX_RING_TAIL, lrc->ring.tail);
687 if (exec_queue_suspended(q) && !xe_exec_queue_is_parallel(q))
690 if (!exec_queue_enabled(q) && !exec_queue_suspended(q)) {
691 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
692 action[len++] = q->guc->id;
693 action[len++] = GUC_CONTEXT_ENABLE;
694 g2h_len = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
696 if (xe_exec_queue_is_parallel(q))
699 q->guc->resume_time = RESUME_PENDING;
700 set_exec_queue_pending_enable(q);
701 set_exec_queue_enabled(q);
702 trace_xe_exec_queue_scheduling_enable(q);
704 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT;
705 action[len++] = q->guc->id;
706 trace_xe_exec_queue_submit(q);
709 xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
713 action[len++] = XE_GUC_ACTION_SCHED_CONTEXT;
714 action[len++] = q->guc->id;
715 trace_xe_exec_queue_submit(q);
717 xe_guc_ct_send(&guc->ct, action, len, 0, 0);
721 static struct dma_fence *
722 guc_exec_queue_run_job(struct drm_sched_job *drm_job)
724 struct xe_sched_job *job = to_xe_sched_job(drm_job);
725 struct xe_exec_queue *q = job->q;
726 struct xe_guc *guc = exec_queue_to_guc(q);
727 struct xe_device *xe = guc_to_xe(guc);
728 bool lr = xe_exec_queue_is_lr(q);
730 xe_assert(xe, !(exec_queue_destroyed(q) || exec_queue_pending_disable(q)) ||
731 exec_queue_banned(q) || exec_queue_suspended(q));
733 trace_xe_sched_job_run(job);
735 if (!exec_queue_killed_or_banned(q) && !xe_sched_job_is_error(job)) {
736 if (!exec_queue_registered(q))
738 if (!lr) /* LR jobs are emitted in the exec IOCTL */
739 q->ring_ops->emit_job(job);
740 submit_exec_queue(q);
744 xe_sched_job_set_error(job, -EOPNOTSUPP);
746 } else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
749 return dma_fence_get(job->fence);
753 static void guc_exec_queue_free_job(struct drm_sched_job *drm_job)
755 struct xe_sched_job *job = to_xe_sched_job(drm_job);
757 trace_xe_sched_job_free(job);
758 xe_sched_job_put(job);
761 static int guc_read_stopped(struct xe_guc *guc)
763 return atomic_read(&guc->submission_state.stopped);
766 #define MAKE_SCHED_CONTEXT_ACTION(q, enable_disable) \
768 XE_GUC_ACTION_SCHED_CONTEXT_MODE_SET, \
770 GUC_CONTEXT_##enable_disable, \
773 static void disable_scheduling_deregister(struct xe_guc *guc,
774 struct xe_exec_queue *q)
776 MAKE_SCHED_CONTEXT_ACTION(q, DISABLE);
777 struct xe_device *xe = guc_to_xe(guc);
780 set_min_preemption_timeout(guc, q);
782 ret = wait_event_timeout(guc->ct.wq, !exec_queue_pending_enable(q) ||
783 guc_read_stopped(guc), HZ * 5);
785 struct xe_gpu_scheduler *sched = &q->guc->sched;
787 drm_warn(&xe->drm, "Pending enable failed to respond");
788 xe_sched_submission_start(sched);
789 xe_gt_reset_async(q->gt);
790 xe_sched_tdr_queue_imm(sched);
794 clear_exec_queue_enabled(q);
795 set_exec_queue_pending_disable(q);
796 set_exec_queue_destroyed(q);
797 trace_xe_exec_queue_scheduling_disable(q);
800 * Reserve space for both G2H here as the 2nd G2H is sent from a G2H
801 * handler and we are not allowed to reserved G2H space in handlers.
803 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
804 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET +
805 G2H_LEN_DW_DEREGISTER_CONTEXT, 2);
808 static void guc_exec_queue_print(struct xe_exec_queue *q, struct drm_printer *p);
810 #if IS_ENABLED(CONFIG_DRM_XE_SIMPLE_ERROR_CAPTURE)
811 static void simple_error_capture(struct xe_exec_queue *q)
813 struct xe_guc *guc = exec_queue_to_guc(q);
814 struct drm_printer p = drm_err_printer("");
815 struct xe_hw_engine *hwe;
816 enum xe_hw_engine_id id;
817 u32 adj_logical_mask = q->logical_mask;
818 u32 width_mask = (0x1 << q->width) - 1;
822 if (q->vm && !q->vm->error_capture.capture_once) {
823 q->vm->error_capture.capture_once = true;
824 cookie = dma_fence_begin_signalling();
825 for (i = 0; q->width > 1 && i < XE_HW_ENGINE_MAX_INSTANCE;) {
826 if (adj_logical_mask & BIT(i)) {
827 adj_logical_mask |= width_mask << i;
834 xe_force_wake_get(gt_to_fw(guc_to_gt(guc)), XE_FORCEWAKE_ALL);
835 xe_guc_ct_print(&guc->ct, &p, true);
836 guc_exec_queue_print(q, &p);
837 for_each_hw_engine(hwe, guc_to_gt(guc), id) {
838 if (hwe->class != q->hwe->class ||
839 !(BIT(hwe->logical_instance) & adj_logical_mask))
841 xe_hw_engine_print(hwe, &p);
843 xe_analyze_vm(&p, q->vm, q->gt->info.id);
844 xe_force_wake_put(gt_to_fw(guc_to_gt(guc)), XE_FORCEWAKE_ALL);
845 dma_fence_end_signalling(cookie);
849 static void simple_error_capture(struct xe_exec_queue *q)
854 static void xe_guc_exec_queue_trigger_cleanup(struct xe_exec_queue *q)
856 struct xe_guc *guc = exec_queue_to_guc(q);
857 struct xe_device *xe = guc_to_xe(guc);
859 /** to wakeup xe_wait_user_fence ioctl if exec queue is reset */
860 wake_up_all(&xe->ufence_wq);
862 if (xe_exec_queue_is_lr(q))
863 queue_work(guc_to_gt(guc)->ordered_wq, &q->guc->lr_tdr);
865 xe_sched_tdr_queue_imm(&q->guc->sched);
868 static void xe_guc_exec_queue_lr_cleanup(struct work_struct *w)
870 struct xe_guc_exec_queue *ge =
871 container_of(w, struct xe_guc_exec_queue, lr_tdr);
872 struct xe_exec_queue *q = ge->q;
873 struct xe_guc *guc = exec_queue_to_guc(q);
874 struct xe_device *xe = guc_to_xe(guc);
875 struct xe_gpu_scheduler *sched = &ge->sched;
877 xe_assert(xe, xe_exec_queue_is_lr(q));
878 trace_xe_exec_queue_lr_cleanup(q);
880 /* Kill the run_job / process_msg entry points */
881 xe_sched_submission_stop(sched);
884 * Engine state now mostly stable, disable scheduling / deregister if
885 * needed. This cleanup routine might be called multiple times, where
886 * the actual async engine deregister drops the final engine ref.
887 * Calling disable_scheduling_deregister will mark the engine as
888 * destroyed and fire off the CT requests to disable scheduling /
889 * deregister, which we only want to do once. We also don't want to mark
890 * the engine as pending_disable again as this may race with the
891 * xe_guc_deregister_done_handler() which treats it as an unexpected
894 if (exec_queue_registered(q) && !exec_queue_destroyed(q)) {
895 struct xe_guc *guc = exec_queue_to_guc(q);
898 set_exec_queue_banned(q);
899 disable_scheduling_deregister(guc, q);
902 * Must wait for scheduling to be disabled before signalling
903 * any fences, if GT broken the GT reset code should signal us.
905 ret = wait_event_timeout(guc->ct.wq,
906 !exec_queue_pending_disable(q) ||
907 guc_read_stopped(guc), HZ * 5);
909 drm_warn(&xe->drm, "Schedule disable failed to respond");
910 xe_sched_submission_start(sched);
911 xe_gt_reset_async(q->gt);
916 xe_sched_submission_start(sched);
919 static enum drm_gpu_sched_stat
920 guc_exec_queue_timedout_job(struct drm_sched_job *drm_job)
922 struct xe_sched_job *job = to_xe_sched_job(drm_job);
923 struct xe_sched_job *tmp_job;
924 struct xe_exec_queue *q = job->q;
925 struct xe_gpu_scheduler *sched = &q->guc->sched;
926 struct xe_device *xe = guc_to_xe(exec_queue_to_guc(q));
930 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &job->fence->flags)) {
931 xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_KERNEL));
932 xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_VM && !exec_queue_killed(q)));
934 drm_notice(&xe->drm, "Timedout job: seqno=%u, guc_id=%d, flags=0x%lx",
935 xe_sched_job_seqno(job), q->guc->id, q->flags);
936 simple_error_capture(q);
939 drm_dbg(&xe->drm, "Timedout signaled job: seqno=%u, guc_id=%d, flags=0x%lx",
940 xe_sched_job_seqno(job), q->guc->id, q->flags);
942 trace_xe_sched_job_timedout(job);
944 /* Kill the run_job entry point */
945 xe_sched_submission_stop(sched);
948 * Kernel jobs should never fail, nor should VM jobs if they do
949 * somethings has gone wrong and the GT needs a reset
951 if (q->flags & EXEC_QUEUE_FLAG_KERNEL ||
952 (q->flags & EXEC_QUEUE_FLAG_VM && !exec_queue_killed(q))) {
953 if (!xe_sched_invalidate_job(job, 2)) {
954 xe_sched_add_pending_job(sched, job);
955 xe_sched_submission_start(sched);
956 xe_gt_reset_async(q->gt);
961 /* Engine state now stable, disable scheduling if needed */
962 if (exec_queue_registered(q)) {
963 struct xe_guc *guc = exec_queue_to_guc(q);
966 if (exec_queue_reset(q))
968 set_exec_queue_banned(q);
969 if (!exec_queue_destroyed(q)) {
970 xe_exec_queue_get(q);
971 disable_scheduling_deregister(guc, q);
975 * Must wait for scheduling to be disabled before signalling
976 * any fences, if GT broken the GT reset code should signal us.
978 * FIXME: Tests can generate a ton of 0x6000 (IOMMU CAT fault
979 * error) messages which can cause the schedule disable to get
980 * lost. If this occurs, trigger a GT reset to recover.
983 ret = wait_event_timeout(guc->ct.wq,
984 !exec_queue_pending_disable(q) ||
985 guc_read_stopped(guc), HZ * 5);
986 if (!ret || guc_read_stopped(guc)) {
987 drm_warn(&xe->drm, "Schedule disable failed to respond");
988 xe_sched_add_pending_job(sched, job);
989 xe_sched_submission_start(sched);
990 xe_gt_reset_async(q->gt);
991 xe_sched_tdr_queue_imm(sched);
996 /* Stop fence signaling */
997 xe_hw_fence_irq_stop(q->fence_irq);
1000 * Fence state now stable, stop / start scheduler which cleans up any
1001 * fences that are complete
1003 xe_sched_add_pending_job(sched, job);
1004 xe_sched_submission_start(sched);
1005 xe_guc_exec_queue_trigger_cleanup(q);
1007 /* Mark all outstanding jobs as bad, thus completing them */
1008 spin_lock(&sched->base.job_list_lock);
1009 list_for_each_entry(tmp_job, &sched->base.pending_list, drm.list)
1010 xe_sched_job_set_error(tmp_job, !i++ ? err : -ECANCELED);
1011 spin_unlock(&sched->base.job_list_lock);
1013 /* Start fence signaling */
1014 xe_hw_fence_irq_start(q->fence_irq);
1017 return DRM_GPU_SCHED_STAT_NOMINAL;
1020 static void __guc_exec_queue_fini_async(struct work_struct *w)
1022 struct xe_guc_exec_queue *ge =
1023 container_of(w, struct xe_guc_exec_queue, fini_async);
1024 struct xe_exec_queue *q = ge->q;
1025 struct xe_guc *guc = exec_queue_to_guc(q);
1027 trace_xe_exec_queue_destroy(q);
1029 if (xe_exec_queue_is_lr(q))
1030 cancel_work_sync(&ge->lr_tdr);
1031 release_guc_id(guc, q);
1032 xe_sched_entity_fini(&ge->entity);
1033 xe_sched_fini(&ge->sched);
1036 xe_exec_queue_fini(q);
1039 static void guc_exec_queue_fini_async(struct xe_exec_queue *q)
1041 INIT_WORK(&q->guc->fini_async, __guc_exec_queue_fini_async);
1043 /* We must block on kernel engines so slabs are empty on driver unload */
1044 if (q->flags & EXEC_QUEUE_FLAG_PERMANENT)
1045 __guc_exec_queue_fini_async(&q->guc->fini_async);
1047 queue_work(system_wq, &q->guc->fini_async);
1050 static void __guc_exec_queue_fini(struct xe_guc *guc, struct xe_exec_queue *q)
1053 * Might be done from within the GPU scheduler, need to do async as we
1054 * fini the scheduler when the engine is fini'd, the scheduler can't
1055 * complete fini within itself (circular dependency). Async resolves
1056 * this we and don't really care when everything is fini'd, just that it
1059 guc_exec_queue_fini_async(q);
1062 static void __guc_exec_queue_process_msg_cleanup(struct xe_sched_msg *msg)
1064 struct xe_exec_queue *q = msg->private_data;
1065 struct xe_guc *guc = exec_queue_to_guc(q);
1066 struct xe_device *xe = guc_to_xe(guc);
1068 xe_assert(xe, !(q->flags & EXEC_QUEUE_FLAG_PERMANENT));
1069 trace_xe_exec_queue_cleanup_entity(q);
1071 if (exec_queue_registered(q))
1072 disable_scheduling_deregister(guc, q);
1074 __guc_exec_queue_fini(guc, q);
1077 static bool guc_exec_queue_allowed_to_change_state(struct xe_exec_queue *q)
1079 return !exec_queue_killed_or_banned(q) && exec_queue_registered(q);
1082 static void __guc_exec_queue_process_msg_set_sched_props(struct xe_sched_msg *msg)
1084 struct xe_exec_queue *q = msg->private_data;
1085 struct xe_guc *guc = exec_queue_to_guc(q);
1087 if (guc_exec_queue_allowed_to_change_state(q))
1088 init_policies(guc, q);
1092 static void suspend_fence_signal(struct xe_exec_queue *q)
1094 struct xe_guc *guc = exec_queue_to_guc(q);
1095 struct xe_device *xe = guc_to_xe(guc);
1097 xe_assert(xe, exec_queue_suspended(q) || exec_queue_killed(q) ||
1098 guc_read_stopped(guc));
1099 xe_assert(xe, q->guc->suspend_pending);
1101 q->guc->suspend_pending = false;
1103 wake_up(&q->guc->suspend_wait);
1106 static void __guc_exec_queue_process_msg_suspend(struct xe_sched_msg *msg)
1108 struct xe_exec_queue *q = msg->private_data;
1109 struct xe_guc *guc = exec_queue_to_guc(q);
1111 if (guc_exec_queue_allowed_to_change_state(q) && !exec_queue_suspended(q) &&
1112 exec_queue_enabled(q)) {
1113 wait_event(guc->ct.wq, q->guc->resume_time != RESUME_PENDING ||
1114 guc_read_stopped(guc));
1116 if (!guc_read_stopped(guc)) {
1117 MAKE_SCHED_CONTEXT_ACTION(q, DISABLE);
1118 s64 since_resume_ms =
1119 ktime_ms_delta(ktime_get(),
1120 q->guc->resume_time);
1121 s64 wait_ms = q->vm->preempt.min_run_period_ms -
1124 if (wait_ms > 0 && q->guc->resume_time)
1127 set_exec_queue_suspended(q);
1128 clear_exec_queue_enabled(q);
1129 set_exec_queue_pending_disable(q);
1130 trace_xe_exec_queue_scheduling_disable(q);
1132 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
1133 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
1135 } else if (q->guc->suspend_pending) {
1136 set_exec_queue_suspended(q);
1137 suspend_fence_signal(q);
1141 static void __guc_exec_queue_process_msg_resume(struct xe_sched_msg *msg)
1143 struct xe_exec_queue *q = msg->private_data;
1144 struct xe_guc *guc = exec_queue_to_guc(q);
1146 if (guc_exec_queue_allowed_to_change_state(q)) {
1147 MAKE_SCHED_CONTEXT_ACTION(q, ENABLE);
1149 q->guc->resume_time = RESUME_PENDING;
1150 clear_exec_queue_suspended(q);
1151 set_exec_queue_pending_enable(q);
1152 set_exec_queue_enabled(q);
1153 trace_xe_exec_queue_scheduling_enable(q);
1155 xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action),
1156 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, 1);
1158 clear_exec_queue_suspended(q);
1162 #define CLEANUP 1 /* Non-zero values to catch uninitialized msg */
1163 #define SET_SCHED_PROPS 2
1167 static void guc_exec_queue_process_msg(struct xe_sched_msg *msg)
1169 trace_xe_sched_msg_recv(msg);
1171 switch (msg->opcode) {
1173 __guc_exec_queue_process_msg_cleanup(msg);
1175 case SET_SCHED_PROPS:
1176 __guc_exec_queue_process_msg_set_sched_props(msg);
1179 __guc_exec_queue_process_msg_suspend(msg);
1182 __guc_exec_queue_process_msg_resume(msg);
1185 XE_WARN_ON("Unknown message type");
1189 static const struct drm_sched_backend_ops drm_sched_ops = {
1190 .run_job = guc_exec_queue_run_job,
1191 .free_job = guc_exec_queue_free_job,
1192 .timedout_job = guc_exec_queue_timedout_job,
1195 static const struct xe_sched_backend_ops xe_sched_ops = {
1196 .process_msg = guc_exec_queue_process_msg,
1199 static int guc_exec_queue_init(struct xe_exec_queue *q)
1201 struct xe_gpu_scheduler *sched;
1202 struct xe_guc *guc = exec_queue_to_guc(q);
1203 struct xe_device *xe = guc_to_xe(guc);
1204 struct xe_guc_exec_queue *ge;
1208 xe_assert(xe, xe_device_uc_enabled(guc_to_xe(guc)));
1210 ge = kzalloc(sizeof(*ge), GFP_KERNEL);
1216 init_waitqueue_head(&ge->suspend_wait);
1218 timeout = (q->vm && xe_vm_in_lr_mode(q->vm)) ? MAX_SCHEDULE_TIMEOUT :
1219 q->hwe->eclass->sched_props.job_timeout_ms;
1220 err = xe_sched_init(&ge->sched, &drm_sched_ops, &xe_sched_ops,
1222 q->lrc[0].ring.size / MAX_JOB_SIZE_BYTES, 64,
1223 timeout, guc_to_gt(guc)->ordered_wq, NULL,
1224 q->name, gt_to_xe(q->gt)->drm.dev);
1229 err = xe_sched_entity_init(&ge->entity, sched);
1233 if (xe_exec_queue_is_lr(q))
1234 INIT_WORK(&q->guc->lr_tdr, xe_guc_exec_queue_lr_cleanup);
1236 mutex_lock(&guc->submission_state.lock);
1238 err = alloc_guc_id(guc, q);
1242 q->entity = &ge->entity;
1244 if (guc_read_stopped(guc))
1245 xe_sched_stop(sched);
1247 mutex_unlock(&guc->submission_state.lock);
1249 xe_exec_queue_assign_name(q, q->guc->id);
1251 trace_xe_exec_queue_create(q);
1256 xe_sched_entity_fini(&ge->entity);
1258 xe_sched_fini(&ge->sched);
1265 static void guc_exec_queue_kill(struct xe_exec_queue *q)
1267 trace_xe_exec_queue_kill(q);
1268 set_exec_queue_killed(q);
1269 xe_guc_exec_queue_trigger_cleanup(q);
1272 static void guc_exec_queue_add_msg(struct xe_exec_queue *q, struct xe_sched_msg *msg,
1275 INIT_LIST_HEAD(&msg->link);
1276 msg->opcode = opcode;
1277 msg->private_data = q;
1279 trace_xe_sched_msg_add(msg);
1280 xe_sched_add_msg(&q->guc->sched, msg);
1283 #define STATIC_MSG_CLEANUP 0
1284 #define STATIC_MSG_SUSPEND 1
1285 #define STATIC_MSG_RESUME 2
1286 static void guc_exec_queue_fini(struct xe_exec_queue *q)
1288 struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_CLEANUP;
1290 if (!(q->flags & EXEC_QUEUE_FLAG_PERMANENT))
1291 guc_exec_queue_add_msg(q, msg, CLEANUP);
1293 __guc_exec_queue_fini(exec_queue_to_guc(q), q);
1296 static int guc_exec_queue_set_priority(struct xe_exec_queue *q,
1297 enum xe_exec_queue_priority priority)
1299 struct xe_sched_msg *msg;
1301 if (q->sched_props.priority == priority || exec_queue_killed_or_banned(q))
1304 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1308 q->sched_props.priority = priority;
1309 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
1314 static int guc_exec_queue_set_timeslice(struct xe_exec_queue *q, u32 timeslice_us)
1316 struct xe_sched_msg *msg;
1318 if (q->sched_props.timeslice_us == timeslice_us ||
1319 exec_queue_killed_or_banned(q))
1322 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1326 q->sched_props.timeslice_us = timeslice_us;
1327 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
1332 static int guc_exec_queue_set_preempt_timeout(struct xe_exec_queue *q,
1333 u32 preempt_timeout_us)
1335 struct xe_sched_msg *msg;
1337 if (q->sched_props.preempt_timeout_us == preempt_timeout_us ||
1338 exec_queue_killed_or_banned(q))
1341 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
1345 q->sched_props.preempt_timeout_us = preempt_timeout_us;
1346 guc_exec_queue_add_msg(q, msg, SET_SCHED_PROPS);
1351 static int guc_exec_queue_set_job_timeout(struct xe_exec_queue *q, u32 job_timeout_ms)
1353 struct xe_gpu_scheduler *sched = &q->guc->sched;
1354 struct xe_guc *guc = exec_queue_to_guc(q);
1355 struct xe_device *xe = guc_to_xe(guc);
1357 xe_assert(xe, !exec_queue_registered(q));
1358 xe_assert(xe, !exec_queue_banned(q));
1359 xe_assert(xe, !exec_queue_killed(q));
1361 sched->base.timeout = job_timeout_ms;
1366 static int guc_exec_queue_suspend(struct xe_exec_queue *q)
1368 struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_SUSPEND;
1370 if (exec_queue_killed_or_banned(q) || q->guc->suspend_pending)
1373 q->guc->suspend_pending = true;
1374 guc_exec_queue_add_msg(q, msg, SUSPEND);
1379 static void guc_exec_queue_suspend_wait(struct xe_exec_queue *q)
1381 struct xe_guc *guc = exec_queue_to_guc(q);
1383 wait_event(q->guc->suspend_wait, !q->guc->suspend_pending ||
1384 guc_read_stopped(guc));
1387 static void guc_exec_queue_resume(struct xe_exec_queue *q)
1389 struct xe_sched_msg *msg = q->guc->static_msgs + STATIC_MSG_RESUME;
1390 struct xe_guc *guc = exec_queue_to_guc(q);
1391 struct xe_device *xe = guc_to_xe(guc);
1393 xe_assert(xe, !q->guc->suspend_pending);
1395 guc_exec_queue_add_msg(q, msg, RESUME);
1398 static bool guc_exec_queue_reset_status(struct xe_exec_queue *q)
1400 return exec_queue_reset(q);
1404 * All of these functions are an abstraction layer which other parts of XE can
1405 * use to trap into the GuC backend. All of these functions, aside from init,
1406 * really shouldn't do much other than trap into the DRM scheduler which
1407 * synchronizes these operations.
1409 static const struct xe_exec_queue_ops guc_exec_queue_ops = {
1410 .init = guc_exec_queue_init,
1411 .kill = guc_exec_queue_kill,
1412 .fini = guc_exec_queue_fini,
1413 .set_priority = guc_exec_queue_set_priority,
1414 .set_timeslice = guc_exec_queue_set_timeslice,
1415 .set_preempt_timeout = guc_exec_queue_set_preempt_timeout,
1416 .set_job_timeout = guc_exec_queue_set_job_timeout,
1417 .suspend = guc_exec_queue_suspend,
1418 .suspend_wait = guc_exec_queue_suspend_wait,
1419 .resume = guc_exec_queue_resume,
1420 .reset_status = guc_exec_queue_reset_status,
1423 static void guc_exec_queue_stop(struct xe_guc *guc, struct xe_exec_queue *q)
1425 struct xe_gpu_scheduler *sched = &q->guc->sched;
1427 /* Stop scheduling + flush any DRM scheduler operations */
1428 xe_sched_submission_stop(sched);
1430 /* Clean up lost G2H + reset engine state */
1431 if (exec_queue_registered(q)) {
1432 if ((exec_queue_banned(q) && exec_queue_destroyed(q)) ||
1433 xe_exec_queue_is_lr(q))
1434 xe_exec_queue_put(q);
1435 else if (exec_queue_destroyed(q))
1436 __guc_exec_queue_fini(guc, q);
1438 if (q->guc->suspend_pending) {
1439 set_exec_queue_suspended(q);
1440 suspend_fence_signal(q);
1442 atomic_and(EXEC_QUEUE_STATE_DESTROYED | ENGINE_STATE_SUSPENDED,
1444 q->guc->resume_time = 0;
1445 trace_xe_exec_queue_stop(q);
1448 * Ban any engine (aside from kernel and engines used for VM ops) with a
1449 * started but not complete job or if a job has gone through a GT reset
1452 if (!(q->flags & (EXEC_QUEUE_FLAG_KERNEL | EXEC_QUEUE_FLAG_VM))) {
1453 struct xe_sched_job *job = xe_sched_first_pending_job(sched);
1456 if ((xe_sched_job_started(job) &&
1457 !xe_sched_job_completed(job)) ||
1458 xe_sched_invalidate_job(job, 2)) {
1459 trace_xe_sched_job_ban(job);
1460 xe_sched_tdr_queue_imm(&q->guc->sched);
1461 set_exec_queue_banned(q);
1467 int xe_guc_submit_reset_prepare(struct xe_guc *guc)
1472 * Using an atomic here rather than submission_state.lock as this
1473 * function can be called while holding the CT lock (engine reset
1474 * failure). submission_state.lock needs the CT lock to resubmit jobs.
1475 * Atomic is not ideal, but it works to prevent against concurrent reset
1476 * and releasing any TDRs waiting on guc->submission_state.stopped.
1478 ret = atomic_fetch_or(1, &guc->submission_state.stopped);
1480 wake_up_all(&guc->ct.wq);
1485 void xe_guc_submit_reset_wait(struct xe_guc *guc)
1487 wait_event(guc->ct.wq, !guc_read_stopped(guc));
1490 int xe_guc_submit_stop(struct xe_guc *guc)
1492 struct xe_exec_queue *q;
1493 unsigned long index;
1494 struct xe_device *xe = guc_to_xe(guc);
1496 xe_assert(xe, guc_read_stopped(guc) == 1);
1498 mutex_lock(&guc->submission_state.lock);
1500 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
1501 guc_exec_queue_stop(guc, q);
1503 mutex_unlock(&guc->submission_state.lock);
1506 * No one can enter the backend at this point, aside from new engine
1507 * creation which is protected by guc->submission_state.lock.
1513 static void guc_exec_queue_start(struct xe_exec_queue *q)
1515 struct xe_gpu_scheduler *sched = &q->guc->sched;
1517 if (!exec_queue_killed_or_banned(q)) {
1520 trace_xe_exec_queue_resubmit(q);
1521 for (i = 0; i < q->width; ++i)
1522 xe_lrc_set_ring_head(q->lrc + i, q->lrc[i].ring.tail);
1523 xe_sched_resubmit_jobs(sched);
1526 xe_sched_submission_start(sched);
1529 int xe_guc_submit_start(struct xe_guc *guc)
1531 struct xe_exec_queue *q;
1532 unsigned long index;
1533 struct xe_device *xe = guc_to_xe(guc);
1535 xe_assert(xe, guc_read_stopped(guc) == 1);
1537 mutex_lock(&guc->submission_state.lock);
1538 atomic_dec(&guc->submission_state.stopped);
1539 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
1540 guc_exec_queue_start(q);
1541 mutex_unlock(&guc->submission_state.lock);
1543 wake_up_all(&guc->ct.wq);
1548 static struct xe_exec_queue *
1549 g2h_exec_queue_lookup(struct xe_guc *guc, u32 guc_id)
1551 struct xe_device *xe = guc_to_xe(guc);
1552 struct xe_exec_queue *q;
1554 if (unlikely(guc_id >= GUC_ID_MAX)) {
1555 drm_err(&xe->drm, "Invalid guc_id %u", guc_id);
1559 q = xa_load(&guc->submission_state.exec_queue_lookup, guc_id);
1561 drm_err(&xe->drm, "Not engine present for guc_id %u", guc_id);
1565 xe_assert(xe, guc_id >= q->guc->id);
1566 xe_assert(xe, guc_id < (q->guc->id + q->width));
1571 static void deregister_exec_queue(struct xe_guc *guc, struct xe_exec_queue *q)
1574 XE_GUC_ACTION_DEREGISTER_CONTEXT,
1578 trace_xe_exec_queue_deregister(q);
1580 xe_guc_ct_send_g2h_handler(&guc->ct, action, ARRAY_SIZE(action));
1583 int xe_guc_sched_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
1585 struct xe_device *xe = guc_to_xe(guc);
1586 struct xe_exec_queue *q;
1587 u32 guc_id = msg[0];
1589 if (unlikely(len < 2)) {
1590 drm_err(&xe->drm, "Invalid length %u", len);
1594 q = g2h_exec_queue_lookup(guc, guc_id);
1598 if (unlikely(!exec_queue_pending_enable(q) &&
1599 !exec_queue_pending_disable(q))) {
1600 drm_err(&xe->drm, "Unexpected engine state 0x%04x",
1601 atomic_read(&q->guc->state));
1605 trace_xe_exec_queue_scheduling_done(q);
1607 if (exec_queue_pending_enable(q)) {
1608 q->guc->resume_time = ktime_get();
1609 clear_exec_queue_pending_enable(q);
1611 wake_up_all(&guc->ct.wq);
1613 clear_exec_queue_pending_disable(q);
1614 if (q->guc->suspend_pending) {
1615 suspend_fence_signal(q);
1617 if (exec_queue_banned(q)) {
1619 wake_up_all(&guc->ct.wq);
1621 deregister_exec_queue(guc, q);
1628 int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
1630 struct xe_device *xe = guc_to_xe(guc);
1631 struct xe_exec_queue *q;
1632 u32 guc_id = msg[0];
1634 if (unlikely(len < 1)) {
1635 drm_err(&xe->drm, "Invalid length %u", len);
1639 q = g2h_exec_queue_lookup(guc, guc_id);
1643 if (!exec_queue_destroyed(q) || exec_queue_pending_disable(q) ||
1644 exec_queue_pending_enable(q) || exec_queue_enabled(q)) {
1645 drm_err(&xe->drm, "Unexpected engine state 0x%04x",
1646 atomic_read(&q->guc->state));
1650 trace_xe_exec_queue_deregister_done(q);
1652 clear_exec_queue_registered(q);
1654 if (exec_queue_banned(q) || xe_exec_queue_is_lr(q))
1655 xe_exec_queue_put(q);
1657 __guc_exec_queue_fini(guc, q);
1662 int xe_guc_exec_queue_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
1664 struct xe_device *xe = guc_to_xe(guc);
1665 struct xe_exec_queue *q;
1666 u32 guc_id = msg[0];
1668 if (unlikely(len < 1)) {
1669 drm_err(&xe->drm, "Invalid length %u", len);
1673 q = g2h_exec_queue_lookup(guc, guc_id);
1677 drm_info(&xe->drm, "Engine reset: guc_id=%d", guc_id);
1679 /* FIXME: Do error capture, most likely async */
1681 trace_xe_exec_queue_reset(q);
1684 * A banned engine is a NOP at this point (came from
1685 * guc_exec_queue_timedout_job). Otherwise, kick drm scheduler to cancel
1686 * jobs by setting timeout of the job to the minimum value kicking
1687 * guc_exec_queue_timedout_job.
1689 set_exec_queue_reset(q);
1690 if (!exec_queue_banned(q))
1691 xe_guc_exec_queue_trigger_cleanup(q);
1696 int xe_guc_exec_queue_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
1699 struct xe_device *xe = guc_to_xe(guc);
1700 struct xe_exec_queue *q;
1701 u32 guc_id = msg[0];
1703 if (unlikely(len < 1)) {
1704 drm_err(&xe->drm, "Invalid length %u", len);
1708 q = g2h_exec_queue_lookup(guc, guc_id);
1712 drm_dbg(&xe->drm, "Engine memory cat error: guc_id=%d", guc_id);
1713 trace_xe_exec_queue_memory_cat_error(q);
1715 /* Treat the same as engine reset */
1716 set_exec_queue_reset(q);
1717 if (!exec_queue_banned(q))
1718 xe_guc_exec_queue_trigger_cleanup(q);
1723 int xe_guc_exec_queue_reset_failure_handler(struct xe_guc *guc, u32 *msg, u32 len)
1725 struct xe_device *xe = guc_to_xe(guc);
1726 u8 guc_class, instance;
1729 if (unlikely(len != 3)) {
1730 drm_err(&xe->drm, "Invalid length %u", len);
1738 /* Unexpected failure of a hardware feature, log an actual error */
1739 drm_err(&xe->drm, "GuC engine reset request failed on %d:%d because 0x%08X",
1740 guc_class, instance, reason);
1742 xe_gt_reset_async(guc_to_gt(guc));
1748 guc_exec_queue_wq_snapshot_capture(struct xe_exec_queue *q,
1749 struct xe_guc_submit_exec_queue_snapshot *snapshot)
1751 struct xe_guc *guc = exec_queue_to_guc(q);
1752 struct xe_device *xe = guc_to_xe(guc);
1753 struct iosys_map map = xe_lrc_parallel_map(q->lrc);
1756 snapshot->guc.wqi_head = q->guc->wqi_head;
1757 snapshot->guc.wqi_tail = q->guc->wqi_tail;
1758 snapshot->parallel.wq_desc.head = parallel_read(xe, map, wq_desc.head);
1759 snapshot->parallel.wq_desc.tail = parallel_read(xe, map, wq_desc.tail);
1760 snapshot->parallel.wq_desc.status = parallel_read(xe, map,
1763 if (snapshot->parallel.wq_desc.head !=
1764 snapshot->parallel.wq_desc.tail) {
1765 for (i = snapshot->parallel.wq_desc.head;
1766 i != snapshot->parallel.wq_desc.tail;
1767 i = (i + sizeof(u32)) % WQ_SIZE)
1768 snapshot->parallel.wq[i / sizeof(u32)] =
1769 parallel_read(xe, map, wq[i / sizeof(u32)]);
1774 guc_exec_queue_wq_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot,
1775 struct drm_printer *p)
1779 drm_printf(p, "\tWQ head: %u (internal), %d (memory)\n",
1780 snapshot->guc.wqi_head, snapshot->parallel.wq_desc.head);
1781 drm_printf(p, "\tWQ tail: %u (internal), %d (memory)\n",
1782 snapshot->guc.wqi_tail, snapshot->parallel.wq_desc.tail);
1783 drm_printf(p, "\tWQ status: %u\n", snapshot->parallel.wq_desc.status);
1785 if (snapshot->parallel.wq_desc.head !=
1786 snapshot->parallel.wq_desc.tail) {
1787 for (i = snapshot->parallel.wq_desc.head;
1788 i != snapshot->parallel.wq_desc.tail;
1789 i = (i + sizeof(u32)) % WQ_SIZE)
1790 drm_printf(p, "\tWQ[%zu]: 0x%08x\n", i / sizeof(u32),
1791 snapshot->parallel.wq[i / sizeof(u32)]);
1796 * xe_guc_exec_queue_snapshot_capture - Take a quick snapshot of the GuC Engine.
1797 * @q: Xe exec queue.
1799 * This can be printed out in a later stage like during dev_coredump
1802 * Returns: a GuC Submit Engine snapshot object that must be freed by the
1803 * caller, using `xe_guc_exec_queue_snapshot_free`.
1805 struct xe_guc_submit_exec_queue_snapshot *
1806 xe_guc_exec_queue_snapshot_capture(struct xe_exec_queue *q)
1808 struct xe_guc *guc = exec_queue_to_guc(q);
1809 struct xe_device *xe = guc_to_xe(guc);
1810 struct xe_gpu_scheduler *sched = &q->guc->sched;
1811 struct xe_sched_job *job;
1812 struct xe_guc_submit_exec_queue_snapshot *snapshot;
1815 snapshot = kzalloc(sizeof(*snapshot), GFP_ATOMIC);
1818 drm_err(&xe->drm, "Skipping GuC Engine snapshot entirely.\n");
1822 snapshot->guc.id = q->guc->id;
1823 memcpy(&snapshot->name, &q->name, sizeof(snapshot->name));
1824 snapshot->class = q->class;
1825 snapshot->logical_mask = q->logical_mask;
1826 snapshot->width = q->width;
1827 snapshot->refcount = kref_read(&q->refcount);
1828 snapshot->sched_timeout = sched->base.timeout;
1829 snapshot->sched_props.timeslice_us = q->sched_props.timeslice_us;
1830 snapshot->sched_props.preempt_timeout_us =
1831 q->sched_props.preempt_timeout_us;
1833 snapshot->lrc = kmalloc_array(q->width, sizeof(struct lrc_snapshot),
1836 if (!snapshot->lrc) {
1837 drm_err(&xe->drm, "Skipping GuC Engine LRC snapshot.\n");
1839 for (i = 0; i < q->width; ++i) {
1840 struct xe_lrc *lrc = q->lrc + i;
1842 snapshot->lrc[i].context_desc =
1843 lower_32_bits(xe_lrc_ggtt_addr(lrc));
1844 snapshot->lrc[i].head = xe_lrc_ring_head(lrc);
1845 snapshot->lrc[i].tail.internal = lrc->ring.tail;
1846 snapshot->lrc[i].tail.memory =
1847 xe_lrc_read_ctx_reg(lrc, CTX_RING_TAIL);
1848 snapshot->lrc[i].start_seqno = xe_lrc_start_seqno(lrc);
1849 snapshot->lrc[i].seqno = xe_lrc_seqno(lrc);
1853 snapshot->schedule_state = atomic_read(&q->guc->state);
1854 snapshot->exec_queue_flags = q->flags;
1856 snapshot->parallel_execution = xe_exec_queue_is_parallel(q);
1857 if (snapshot->parallel_execution)
1858 guc_exec_queue_wq_snapshot_capture(q, snapshot);
1860 spin_lock(&sched->base.job_list_lock);
1861 snapshot->pending_list_size = list_count_nodes(&sched->base.pending_list);
1862 snapshot->pending_list = kmalloc_array(snapshot->pending_list_size,
1863 sizeof(struct pending_list_snapshot),
1866 if (!snapshot->pending_list) {
1867 drm_err(&xe->drm, "Skipping GuC Engine pending_list snapshot.\n");
1870 list_for_each_entry(job, &sched->base.pending_list, drm.list) {
1871 snapshot->pending_list[i].seqno =
1872 xe_sched_job_seqno(job);
1873 snapshot->pending_list[i].fence =
1874 dma_fence_is_signaled(job->fence) ? 1 : 0;
1875 snapshot->pending_list[i].finished =
1876 dma_fence_is_signaled(&job->drm.s_fence->finished)
1882 spin_unlock(&sched->base.job_list_lock);
1888 * xe_guc_exec_queue_snapshot_print - Print out a given GuC Engine snapshot.
1889 * @snapshot: GuC Submit Engine snapshot object.
1890 * @p: drm_printer where it will be printed out.
1892 * This function prints out a given GuC Submit Engine snapshot object.
1895 xe_guc_exec_queue_snapshot_print(struct xe_guc_submit_exec_queue_snapshot *snapshot,
1896 struct drm_printer *p)
1903 drm_printf(p, "\nGuC ID: %d\n", snapshot->guc.id);
1904 drm_printf(p, "\tName: %s\n", snapshot->name);
1905 drm_printf(p, "\tClass: %d\n", snapshot->class);
1906 drm_printf(p, "\tLogical mask: 0x%x\n", snapshot->logical_mask);
1907 drm_printf(p, "\tWidth: %d\n", snapshot->width);
1908 drm_printf(p, "\tRef: %d\n", snapshot->refcount);
1909 drm_printf(p, "\tTimeout: %ld (ms)\n", snapshot->sched_timeout);
1910 drm_printf(p, "\tTimeslice: %u (us)\n",
1911 snapshot->sched_props.timeslice_us);
1912 drm_printf(p, "\tPreempt timeout: %u (us)\n",
1913 snapshot->sched_props.preempt_timeout_us);
1915 for (i = 0; snapshot->lrc && i < snapshot->width; ++i) {
1916 drm_printf(p, "\tHW Context Desc: 0x%08x\n",
1917 snapshot->lrc[i].context_desc);
1918 drm_printf(p, "\tLRC Head: (memory) %u\n",
1919 snapshot->lrc[i].head);
1920 drm_printf(p, "\tLRC Tail: (internal) %u, (memory) %u\n",
1921 snapshot->lrc[i].tail.internal,
1922 snapshot->lrc[i].tail.memory);
1923 drm_printf(p, "\tStart seqno: (memory) %d\n",
1924 snapshot->lrc[i].start_seqno);
1925 drm_printf(p, "\tSeqno: (memory) %d\n", snapshot->lrc[i].seqno);
1927 drm_printf(p, "\tSchedule State: 0x%x\n", snapshot->schedule_state);
1928 drm_printf(p, "\tFlags: 0x%lx\n", snapshot->exec_queue_flags);
1930 if (snapshot->parallel_execution)
1931 guc_exec_queue_wq_snapshot_print(snapshot, p);
1933 for (i = 0; snapshot->pending_list && i < snapshot->pending_list_size;
1935 drm_printf(p, "\tJob: seqno=%d, fence=%d, finished=%d\n",
1936 snapshot->pending_list[i].seqno,
1937 snapshot->pending_list[i].fence,
1938 snapshot->pending_list[i].finished);
1942 * xe_guc_exec_queue_snapshot_free - Free all allocated objects for a given
1944 * @snapshot: GuC Submit Engine snapshot object.
1946 * This function free all the memory that needed to be allocated at capture
1949 void xe_guc_exec_queue_snapshot_free(struct xe_guc_submit_exec_queue_snapshot *snapshot)
1954 kfree(snapshot->lrc);
1955 kfree(snapshot->pending_list);
1959 static void guc_exec_queue_print(struct xe_exec_queue *q, struct drm_printer *p)
1961 struct xe_guc_submit_exec_queue_snapshot *snapshot;
1963 snapshot = xe_guc_exec_queue_snapshot_capture(q);
1964 xe_guc_exec_queue_snapshot_print(snapshot, p);
1965 xe_guc_exec_queue_snapshot_free(snapshot);
1969 * xe_guc_submit_print - GuC Submit Print.
1971 * @p: drm_printer where it will be printed out.
1973 * This function capture and prints snapshots of **all** GuC Engines.
1975 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p)
1977 struct xe_exec_queue *q;
1978 unsigned long index;
1980 if (!xe_device_uc_enabled(guc_to_xe(guc)))
1983 mutex_lock(&guc->submission_state.lock);
1984 xa_for_each(&guc->submission_state.exec_queue_lookup, index, q)
1985 guc_exec_queue_print(q, p);
1986 mutex_unlock(&guc->submission_state.lock);