Skip to contentSkip to conversation

GuC submission support

PATCH In review Matthew Brost 10 participants 170 messages Jun 24, 2021 – Oct 27, 2021 Page 2 of 3

Subsystems (2)
lore
Share & tools
lore
#73 lore raw

Re: [PATCH 23/47] drm/i915/guc: Update GuC debugfs to support new GuC

In reply to Matthew Brost
Quoted 197 lines · Matthew Brost
Quoted 116 lines · John Harrison
Quoted 115 lines · Matthew Brost

Update GuC debugfs to support the new GuC structures.

---
   drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     | 22 ++++++++
   drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |  3 ++
   .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    | 23 +++++++-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 52 +++++++++++++++++++
   .../gpu/drm/i915/gt/uc/intel_guc_submission.h |  4 ++
   drivers/gpu/drm/i915/i915_debugfs.c           |  1 +
   6 files changed, 104 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +22 -0
index e0f92e28350c..4ed074df88e5 100644
@@ -1135,3 +1135,25 @@ void intel_guc_ct_event_handler(struct intel_guc_ct *ct)
   	ct_try_receive_message(ct);
   }
+
+void intel_guc_log_ct_info(struct intel_guc_ct *ct,
+			   struct drm_printer *p)
+{
+	if (!ct->enabled) {
+		drm_puts(p, "CT disabled\n");
+		return;
+	}
+
+	drm_printf(p, "H2G Space: %u\n",
+		   atomic_read(&ct->ctbs.send.space) * 4);
+	drm_printf(p, "Head: %u\n",
+		   ct->ctbs.send.desc->head);
+	drm_printf(p, "Tail: %u\n",
+		   ct->ctbs.send.desc->tail);
+	drm_printf(p, "G2H Space: %u\n",
+		   atomic_read(&ct->ctbs.recv.space) * 4);
+	drm_printf(p, "Head: %u\n",
+		   ct->ctbs.recv.desc->head);
+	drm_printf(p, "Tail: %u\n",
+		   ct->ctbs.recv.desc->tail);
+}
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h +3 -0
index ab1b79ab960b..f62eb06b32fc 100644
@@ -16,6 +16,7 @@
   struct i915_vma;
   struct intel_guc;
+struct drm_printer;
   /**
    * DOC: Command Transport (CT).
@@ -106,4 +107,6 @@ int intel_guc_ct_send(struct intel_guc_ct *ct, const u32 *action, u32 len,
   		      u32 *response_buf, u32 response_buf_size, u32 flags);
   void intel_guc_ct_event_handler(struct intel_guc_ct *ct);
+void intel_guc_log_ct_info(struct intel_guc_ct *ct, struct drm_printer *p);
+
   #endif /* _INTEL_GUC_CT_H_ */
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +22 -1
index fe7cb7b29a1e..62b9ce0fafaa 100644
@@ -9,6 +9,8 @@
   #include "intel_guc.h"
   #include "intel_guc_debugfs.h"
   #include "intel_guc_log_debugfs.h"
+#include "gt/uc/intel_guc_ct.h"
+#include "gt/uc/intel_guc_submission.h"
   static int guc_info_show(struct seq_file *m, void *data)
   {
@@ -22,16 +24,35 @@ static int guc_info_show(struct seq_file *m, void *data)
   	drm_puts(&p, "\n");
   	intel_guc_log_info(&guc->log, &p);
-	/* Add more as required ... */
+	if (!intel_guc_submission_is_used(guc))
+		return 0;
+
+	intel_guc_log_ct_info(&guc->ct, &p);
+	intel_guc_log_submission_info(guc, &p);
   	return 0;
   }
   DEFINE_GT_DEBUGFS_ATTRIBUTE(guc_info);
+static int guc_registered_contexts_show(struct seq_file *m, void *data)
+{
+	struct intel_guc *guc = m->private;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	intel_guc_log_context_info(guc, &p);
+
+	return 0;
+}
+DEFINE_GT_DEBUGFS_ATTRIBUTE(guc_registered_contexts);
+
   void intel_guc_debugfs_register(struct intel_guc *guc, struct dentry *root)
   {
   	static const struct debugfs_gt_file files[] = {
   		{ "guc_info", &guc_info_fops, NULL },
+		{ "guc_registered_contexts", &guc_registered_contexts_fops, NULL },
   	};
   	if (!intel_guc_is_supported(guc))
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
index d1a28283a9ae..89b3c7e5d15b 100644
@@ -1600,3 +1600,55 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
   	return 0;
   }
+
+void intel_guc_log_submission_info(struct intel_guc *guc,
+				   struct drm_printer *p)
+{
+	struct i915_sched_engine *sched_engine = guc->sched_engine;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c int intel_guc_sched_done_process_msg(struct intel_guc *guc, · line 1614

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	struct rb_node *rb;
+	unsigned long flags;
+
+	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
+		   atomic_read(&guc->outstanding_submission_g2h));
+	drm_printf(p, "GuC tasklet count: %u\n\n",
+		   atomic_read(&sched_engine->tasklet.count));

Does sched_engine need a null check?

Yes it does. Have this fixed locally already.

Quoted 60 lines
Quoted 60 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +40 -0
+
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	drm_printf(p, "Requests in GuC submit tasklet:\n");
+	for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) {
+		struct i915_priolist *pl = to_priolist(rb);
+		struct i915_request *rq;
+
+		priolist_for_each_request(rq, pl)
+			drm_printf(p, "guc_id=%u, seqno=%llu\n",
+				   rq->context->guc_id,
+				   rq->fence.seqno);
+	}
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	drm_printf(p, "\n");
+}
+
+void intel_guc_log_context_info(struct intel_guc *guc,
+				struct drm_printer *p)
+{
+	struct intel_context *ce;
+	unsigned long index;
+
+	xa_for_each(&guc->context_lookup, index, ce) {
+		drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id);
+		drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca);
+		drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n",
+			   ce->ring->head,
+			   ce->lrc_reg_state[CTX_RING_HEAD]);
+		drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n",
+			   ce->ring->tail,
+			   ce->lrc_reg_state[CTX_RING_TAIL]);
+		drm_printf(p, "\t\tContext Pin Count: %u\n",
+			   atomic_read(&ce->pin_count));
+		drm_printf(p, "\t\tGuC ID Ref Count: %u\n",
+			   atomic_read(&ce->guc_id_ref));
+		drm_printf(p, "\t\tSchedule State: 0x%x, 0x%x\n\n",
+			   ce->guc_state.sched_state,
+			   atomic_read(&ce->guc_sched_state_no_lock));
+	}
+}
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +4 -0
index 3f7005018939..6453e2bfa151 100644
@@ -10,6 +10,7 @@
   #include "intel_guc.h"
+struct drm_printer;
   struct intel_engine_cs;
   void intel_guc_submission_init_early(struct intel_guc *guc);
@@ -20,6 +21,9 @@ void intel_guc_submission_fini(struct intel_guc *guc);
   int intel_guc_preempt_work_create(struct intel_guc *guc);
   void intel_guc_preempt_work_destroy(struct intel_guc *guc);
   int intel_guc_submission_setup(struct intel_engine_cs *engine);
+void intel_guc_log_submission_info(struct intel_guc *guc,
+				   struct drm_printer *p);
+void intel_guc_log_context_info(struct intel_guc *guc, struct drm_printer *p);
   static inline bool intel_guc_submission_is_supported(struct intel_guc *guc)
   {
drivers/gpu/drm/i915/i915_debugfs.c +0 -0
index 277800987bf8..a9084789deff 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_debugfs.c line 53

Quoted 7 lines
drivers/gpu/drm/i915/i915_debugfs.c +1 -0
@@ -50,6 +50,7 @@
   #include "i915_trace.h"
   #include "intel_pm.h"
   #include "intel_sideband.h"
+#include "gt/intel_lrc_reg.h"

Obsolete include again?

Quoted 3 lines

CTX_RING_TAIL /CTX_RING_HEAD are in gt/intel_lrc_reg.h

Matt

But those are not being added to i915_debugfs.c, only to intel_guc_submission.c. So why is the include being added here?

John.

Quoted 5 lines
Quoted 5 lines

John.

Quoted 2 lines
drivers/gpu/drm/i915/i915_debugfs.c +0 -0
   static inline struct drm_i915_private *node_to_i915(struct drm_info_node *node)
   {

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#74 lore raw

Re: [PATCH 23/47] drm/i915/guc: Update GuC debugfs to support new GuC

In reply to Matthew Brost
Quoted 18 lines · Matthew Brost

Update GuC debugfs to support the new GuC structures.

---
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     | 22 ++++++++
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |  3 ++
 .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    | 23 +++++++-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 52 +++++++++++++++++++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.h |  4 ++
 drivers/gpu/drm/i915/i915_debugfs.c           |  1 +
 6 files changed, 104 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +0 -0
index e0f92e28350c..4ed074df88e5 100644

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c void intel_guc_ct_event_handler(struct intel_guc_ct *ct) · line 1139

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +2 -0
@@ -1135,3 +1135,25 @@ void intel_guc_ct_event_handler(struct intel_guc_ct *ct)
 
 	ct_try_receive_message(ct);
 }
+
+void intel_guc_log_ct_info(struct intel_guc_ct *ct,

this is not "guc log" function, it is "guc ct" one, so:

void intel_guc_ct_print_info(struct intel_guc_ct *ct,

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c void intel_guc_ct_event_handler(struct intel_guc_ct *ct) · line 1143

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +4 -0
+			   struct drm_printer *p)
+{
+	if (!ct->enabled) {
+		drm_puts(p, "CT disabled\n");

nit: maybe

  drm_puts(p, "CT %s\n", enableddisabled(false));

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c void intel_guc_ct_event_handler(struct intel_guc_ct *ct) · line 1148

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +5 -0
+		return;
+	}
+
+	drm_printf(p, "H2G Space: %u\n",
+		   atomic_read(&ct->ctbs.send.space) * 4);

don't you want to print size ?
or GGTT offset ?

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c void intel_guc_ct_event_handler(struct intel_guc_ct *ct) · line 1158

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +10 -0
+	drm_printf(p, "Head: %u\n",
+		   ct->ctbs.send.desc->head);
+	drm_printf(p, "Tail: %u\n",
+		   ct->ctbs.send.desc->tail);
+	drm_printf(p, "G2H Space: %u\n",
+		   atomic_read(&ct->ctbs.recv.space) * 4);
+	drm_printf(p, "Head: %u\n",
+		   ct->ctbs.recv.desc->head);
+	drm_printf(p, "Tail: %u\n",
+		   ct->ctbs.recv.desc->tail);

hmm, what about adding helper:

static void dump_ctb(struct intel_guc_ct_buffer *ctb, *p)

  {
	drm_printf(p, "Size: %u\n", ctb->size);
	drm_printf(p, "Space: %u\n", atomic_read(&ctb->space) * 4);
	drm_printf(p, "Head: %u\n", ctb->desc->head);
	drm_printf(p, "Tail: %u\n", ctb->desc->tail);

}

and then:

	drm_printf(p, "H2G:\n");
	dump_ctb(&ct->ctbs.send, p);
	drm_printf(p, "G2H:\n");
	dump_ctb(&ct->ctbs.recv, p);

or

	dump_ctb(&ct->ctbs.send, "H2G", p);
	dump_ctb(&ct->ctbs.recv, "G2H", p);
Quoted 75 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +1 -0
+}
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h +3 -0
index ab1b79ab960b..f62eb06b32fc 100644
@@ -16,6 +16,7 @@
 
 struct i915_vma;
 struct intel_guc;
+struct drm_printer;
 
 /**
  * DOC: Command Transport (CT).
@@ -106,4 +107,6 @@ int intel_guc_ct_send(struct intel_guc_ct *ct, const u32 *action, u32 len,
 		      u32 *response_buf, u32 response_buf_size, u32 flags);
 void intel_guc_ct_event_handler(struct intel_guc_ct *ct);
 
+void intel_guc_log_ct_info(struct intel_guc_ct *ct, struct drm_printer *p);
+
 #endif /* _INTEL_GUC_CT_H_ */
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +22 -1
index fe7cb7b29a1e..62b9ce0fafaa 100644
@@ -9,6 +9,8 @@
 #include "intel_guc.h"
 #include "intel_guc_debugfs.h"
 #include "intel_guc_log_debugfs.h"
+#include "gt/uc/intel_guc_ct.h"
+#include "gt/uc/intel_guc_submission.h"
 
 static int guc_info_show(struct seq_file *m, void *data)
 {
@@ -22,16 +24,35 @@ static int guc_info_show(struct seq_file *m, void *data)
 	drm_puts(&p, "\n");
 	intel_guc_log_info(&guc->log, &p);
 
-	/* Add more as required ... */
+	if (!intel_guc_submission_is_used(guc))
+		return 0;
+
+	intel_guc_log_ct_info(&guc->ct, &p);
+	intel_guc_log_submission_info(guc, &p);
 
 	return 0;
 }
 DEFINE_GT_DEBUGFS_ATTRIBUTE(guc_info);
 
+static int guc_registered_contexts_show(struct seq_file *m, void *data)
+{
+	struct intel_guc *guc = m->private;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	intel_guc_log_context_info(guc, &p);
+
+	return 0;
+}
+DEFINE_GT_DEBUGFS_ATTRIBUTE(guc_registered_contexts);
+
 void intel_guc_debugfs_register(struct intel_guc *guc, struct dentry *root)
 {
 	static const struct debugfs_gt_file files[] = {
 		{ "guc_info", &guc_info_fops, NULL },
+		{ "guc_registered_contexts", &guc_registered_contexts_fops, NULL },
 	};
 
 	if (!intel_guc_is_supported(guc))
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
index d1a28283a9ae..89b3c7e5d15b 100644

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c int intel_guc_sched_done_process_msg(struct intel_guc *guc, · line 1604

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -0
@@ -1600,3 +1600,55 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
 
 	return 0;
 }
+
+void intel_guc_log_submission_info(struct intel_guc *guc,

use correct prefix:

void intel_guc_submission_print_info(struct intel_guc *guc,

Quoted 17 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +17 -0
+				   struct drm_printer *p)
+{
+	struct i915_sched_engine *sched_engine = guc->sched_engine;
+	struct rb_node *rb;
+	unsigned long flags;
+
+	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
+		   atomic_read(&guc->outstanding_submission_g2h));
+	drm_printf(p, "GuC tasklet count: %u\n\n",
+		   atomic_read(&sched_engine->tasklet.count));
+
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	drm_printf(p, "Requests in GuC submit tasklet:\n");
+	for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) {
+		struct i915_priolist *pl = to_priolist(rb);
+		struct i915_request *rq;
+

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c int intel_guc_sched_done_process_msg(struct intel_guc *guc, · line 1631

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+		priolist_for_each_request(rq, pl)
+			drm_printf(p, "guc_id=%u, seqno=%llu\n",
+				   rq->context->guc_id,
+				   rq->fence.seqno);
+	}
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	drm_printf(p, "\n");
+}
+
+void intel_guc_log_context_info(struct intel_guc *guc,

use correct prefix:

void intel_guc_submission_print_context_info(struct intel_guc *guc,

Michal

Quoted 58 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +23 -0
+				struct drm_printer *p)
+{
+	struct intel_context *ce;
+	unsigned long index;
+
+	xa_for_each(&guc->context_lookup, index, ce) {
+		drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id);
+		drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca);
+		drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n",
+			   ce->ring->head,
+			   ce->lrc_reg_state[CTX_RING_HEAD]);
+		drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n",
+			   ce->ring->tail,
+			   ce->lrc_reg_state[CTX_RING_TAIL]);
+		drm_printf(p, "\t\tContext Pin Count: %u\n",
+			   atomic_read(&ce->pin_count));
+		drm_printf(p, "\t\tGuC ID Ref Count: %u\n",
+			   atomic_read(&ce->guc_id_ref));
+		drm_printf(p, "\t\tSchedule State: 0x%x, 0x%x\n\n",
+			   ce->guc_state.sched_state,
+			   atomic_read(&ce->guc_sched_state_no_lock));
+	}
+}
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +4 -0
index 3f7005018939..6453e2bfa151 100644
@@ -10,6 +10,7 @@
 
 #include "intel_guc.h"
 
+struct drm_printer;
 struct intel_engine_cs;
 
 void intel_guc_submission_init_early(struct intel_guc *guc);
@@ -20,6 +21,9 @@ void intel_guc_submission_fini(struct intel_guc *guc);
 int intel_guc_preempt_work_create(struct intel_guc *guc);
 void intel_guc_preempt_work_destroy(struct intel_guc *guc);
 int intel_guc_submission_setup(struct intel_engine_cs *engine);
+void intel_guc_log_submission_info(struct intel_guc *guc,
+				   struct drm_printer *p);
+void intel_guc_log_context_info(struct intel_guc *guc, struct drm_printer *p);
 
 static inline bool intel_guc_submission_is_supported(struct intel_guc *guc)
 {
drivers/gpu/drm/i915/i915_debugfs.c +1 -0
index 277800987bf8..a9084789deff 100644
@@ -50,6 +50,7 @@
 #include "i915_trace.h"
 #include "intel_pm.h"
 #include "intel_sideband.h"
+#include "gt/intel_lrc_reg.h"
 
 static inline struct drm_i915_private *node_to_i915(struct drm_info_node *node)
 {
#75 lore raw

Re: [PATCH 23/47] drm/i915/guc: Update GuC debugfs to support new GuC

Quoted 22 lines · Michal Wajdeczko
Quoted 19 lines · Matthew Brost

Update GuC debugfs to support the new GuC structures.

---
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     | 22 ++++++++
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |  3 ++
 .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    | 23 +++++++-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 52 +++++++++++++++++++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.h |  4 ++
 drivers/gpu/drm/i915/i915_debugfs.c           |  1 +
 6 files changed, 104 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +0 -0
index e0f92e28350c..4ed074df88e5 100644
@@ -1135,3 +1135,25 @@ void intel_guc_ct_event_handler(struct intel_guc_ct *ct)
 
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c void intel_guc_ct_event_handler(struct intel_guc_ct *ct) · line 1139

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +2 -0
 	ct_try_receive_message(ct);
 }
+
+void intel_guc_log_ct_info(struct intel_guc_ct *ct,

this is not "guc log" function, it is "guc ct" one, so:

void intel_guc_ct_print_info(struct intel_guc_ct *ct,

Sure.

Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c void intel_guc_ct_event_handler(struct intel_guc_ct *ct) · line 1143

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +4 -0
+			   struct drm_printer *p)
+{
+	if (!ct->enabled) {
+		drm_puts(p, "CT disabled\n");

nit: maybe

  drm_puts(p, "CT %s\n", enableddisabled(false));

Sure.

Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c void intel_guc_ct_event_handler(struct intel_guc_ct *ct) · line 1148

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +5 -0
+		return;
+	}
+
+	drm_printf(p, "H2G Space: %u\n",
+		   atomic_read(&ct->ctbs.send.space) * 4);

don't you want to print size ?
or GGTT offset ?

I don't think so.

Quoted 28 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c void intel_guc_ct_event_handler(struct intel_guc_ct *ct) · line 1158

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +10 -0
+	drm_printf(p, "Head: %u\n",
+		   ct->ctbs.send.desc->head);
+	drm_printf(p, "Tail: %u\n",
+		   ct->ctbs.send.desc->tail);
+	drm_printf(p, "G2H Space: %u\n",
+		   atomic_read(&ct->ctbs.recv.space) * 4);
+	drm_printf(p, "Head: %u\n",
+		   ct->ctbs.recv.desc->head);
+	drm_printf(p, "Tail: %u\n",
+		   ct->ctbs.recv.desc->tail);

hmm, what about adding helper:

static void dump_ctb(struct intel_guc_ct_buffer *ctb, *p)

  {
	drm_printf(p, "Size: %u\n", ctb->size);
	drm_printf(p, "Space: %u\n", atomic_read(&ctb->space) * 4);
	drm_printf(p, "Head: %u\n", ctb->desc->head);
	drm_printf(p, "Tail: %u\n", ctb->desc->tail);

}

and then:

	drm_printf(p, "H2G:\n");
	dump_ctb(&ct->ctbs.send, p);
	drm_printf(p, "G2H:\n");
	dump_ctb(&ct->ctbs.recv, p);
Quoted 6 lines

or

	dump_ctb(&ct->ctbs.send, "H2G", p);
	dump_ctb(&ct->ctbs.recv, "G2H", p);

Seems unnecessary.

Quoted 76 lines
Quoted 76 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +1 -0
+}
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h +3 -0
index ab1b79ab960b..f62eb06b32fc 100644
@@ -16,6 +16,7 @@
 
 struct i915_vma;
 struct intel_guc;
+struct drm_printer;
 
 /**
  * DOC: Command Transport (CT).
@@ -106,4 +107,6 @@ int intel_guc_ct_send(struct intel_guc_ct *ct, const u32 *action, u32 len,
 		      u32 *response_buf, u32 response_buf_size, u32 flags);
 void intel_guc_ct_event_handler(struct intel_guc_ct *ct);
 
+void intel_guc_log_ct_info(struct intel_guc_ct *ct, struct drm_printer *p);
+
 #endif /* _INTEL_GUC_CT_H_ */
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +22 -1
index fe7cb7b29a1e..62b9ce0fafaa 100644
@@ -9,6 +9,8 @@
 #include "intel_guc.h"
 #include "intel_guc_debugfs.h"
 #include "intel_guc_log_debugfs.h"
+#include "gt/uc/intel_guc_ct.h"
+#include "gt/uc/intel_guc_submission.h"
 
 static int guc_info_show(struct seq_file *m, void *data)
 {
@@ -22,16 +24,35 @@ static int guc_info_show(struct seq_file *m, void *data)
 	drm_puts(&p, "\n");
 	intel_guc_log_info(&guc->log, &p);
 
-	/* Add more as required ... */
+	if (!intel_guc_submission_is_used(guc))
+		return 0;
+
+	intel_guc_log_ct_info(&guc->ct, &p);
+	intel_guc_log_submission_info(guc, &p);
 
 	return 0;
 }
 DEFINE_GT_DEBUGFS_ATTRIBUTE(guc_info);
 
+static int guc_registered_contexts_show(struct seq_file *m, void *data)
+{
+	struct intel_guc *guc = m->private;
+	struct drm_printer p = drm_seq_file_printer(m);
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	intel_guc_log_context_info(guc, &p);
+
+	return 0;
+}
+DEFINE_GT_DEBUGFS_ATTRIBUTE(guc_registered_contexts);
+
 void intel_guc_debugfs_register(struct intel_guc *guc, struct dentry *root)
 {
 	static const struct debugfs_gt_file files[] = {
 		{ "guc_info", &guc_info_fops, NULL },
+		{ "guc_registered_contexts", &guc_registered_contexts_fops, NULL },
 	};
 
 	if (!intel_guc_is_supported(guc))
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
index d1a28283a9ae..89b3c7e5d15b 100644
@@ -1600,3 +1600,55 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
 
Quoted 7 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c int intel_guc_sched_done_process_msg(struct intel_guc *guc, · line 1604

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -0
 	return 0;
 }
+
+void intel_guc_log_submission_info(struct intel_guc *guc,

use correct prefix:

I think correct is wrong term here, use the way I'd name seems more accurate.

Quoted 2 lines

void intel_guc_submission_print_info(struct intel_guc *guc,

But, yes will change.

Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +21 -0
+				   struct drm_printer *p)
+{
+	struct i915_sched_engine *sched_engine = guc->sched_engine;
+	struct rb_node *rb;
+	unsigned long flags;
+
+	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
+		   atomic_read(&guc->outstanding_submission_g2h));
+	drm_printf(p, "GuC tasklet count: %u\n\n",
+		   atomic_read(&sched_engine->tasklet.count));
+
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	drm_printf(p, "Requests in GuC submit tasklet:\n");
+	for (rb = rb_first_cached(&sched_engine->queue); rb; rb = rb_next(rb)) {
+		struct i915_priolist *pl = to_priolist(rb);
+		struct i915_request *rq;
+
+		priolist_for_each_request(rq, pl)
+			drm_printf(p, "guc_id=%u, seqno=%llu\n",
+				   rq->context->guc_id,
+				   rq->fence.seqno);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c int intel_guc_sched_done_process_msg(struct intel_guc *guc, · line 1631

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+	}
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	drm_printf(p, "\n");
+}
+
+void intel_guc_log_context_info(struct intel_guc *guc,

use correct prefix:

Same as above.

Matt

Quoted 59 lines

void intel_guc_submission_print_context_info(struct intel_guc *guc,

Michal

Quoted 55 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +23 -0
+				struct drm_printer *p)
+{
+	struct intel_context *ce;
+	unsigned long index;
+
+	xa_for_each(&guc->context_lookup, index, ce) {
+		drm_printf(p, "GuC lrc descriptor %u:\n", ce->guc_id);
+		drm_printf(p, "\tHW Context Desc: 0x%08x\n", ce->lrc.lrca);
+		drm_printf(p, "\t\tLRC Head: Internal %u, Memory %u\n",
+			   ce->ring->head,
+			   ce->lrc_reg_state[CTX_RING_HEAD]);
+		drm_printf(p, "\t\tLRC Tail: Internal %u, Memory %u\n",
+			   ce->ring->tail,
+			   ce->lrc_reg_state[CTX_RING_TAIL]);
+		drm_printf(p, "\t\tContext Pin Count: %u\n",
+			   atomic_read(&ce->pin_count));
+		drm_printf(p, "\t\tGuC ID Ref Count: %u\n",
+			   atomic_read(&ce->guc_id_ref));
+		drm_printf(p, "\t\tSchedule State: 0x%x, 0x%x\n\n",
+			   ce->guc_state.sched_state,
+			   atomic_read(&ce->guc_sched_state_no_lock));
+	}
+}
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +4 -0
index 3f7005018939..6453e2bfa151 100644
@@ -10,6 +10,7 @@
 
 #include "intel_guc.h"
 
+struct drm_printer;
 struct intel_engine_cs;
 
 void intel_guc_submission_init_early(struct intel_guc *guc);
@@ -20,6 +21,9 @@ void intel_guc_submission_fini(struct intel_guc *guc);
 int intel_guc_preempt_work_create(struct intel_guc *guc);
 void intel_guc_preempt_work_destroy(struct intel_guc *guc);
 int intel_guc_submission_setup(struct intel_engine_cs *engine);
+void intel_guc_log_submission_info(struct intel_guc *guc,
+				   struct drm_printer *p);
+void intel_guc_log_context_info(struct intel_guc *guc, struct drm_printer *p);
 
 static inline bool intel_guc_submission_is_supported(struct intel_guc *guc)
 {
drivers/gpu/drm/i915/i915_debugfs.c +1 -0
index 277800987bf8..a9084789deff 100644
@@ -50,6 +50,7 @@
 #include "i915_trace.h"
 #include "intel_pm.h"
 #include "intel_sideband.h"
+#include "gt/intel_lrc_reg.h"
 
Quoted 3 lines

drivers/gpu/drm/i915/i915_debugfs.c line 56

Quoted 3 lines
drivers/gpu/drm/i915/i915_debugfs.c +0 -0
 static inline struct drm_i915_private *node_to_i915(struct drm_info_node *node)
 {

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#76 lore raw

PATCH 24/47 drm/i915/guc: Add several request trace points

In reply to Matthew Brost

Add trace points for request dependencies and GuC submit. Extended existing request trace points to include submit fence value,, guc_id, and ring tail value.

---
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  3 ++
 drivers/gpu/drm/i915/i915_request.c           |  3 ++
 drivers/gpu/drm/i915/i915_trace.h             | 39 ++++++++++++++++++-
 3 files changed, 43 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index 89b3c7e5d15b..c2327eebc09c 100644
@@ -422,6 +422,7 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
 			guc->stalled_request = last;
 			return false;
 		}
+		trace_i915_request_guc_submit(last);
 	}
 
 	guc->stalled_request = NULL;
@@ -642,6 +643,8 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
 	ret = guc_add_request(guc, rq);
 	if (ret == -EBUSY)
 		guc->stalled_request = rq;
+	else
+		trace_i915_request_guc_submit(rq);
 
 	return ret;
 }
drivers/gpu/drm/i915/i915_request.c +3 -0
index d92c9f25c9f4..7f7aa096e873 100644
@@ -1344,6 +1344,9 @@ __i915_request_await_execution(struct i915_request *to,
 			return err;
 	}
 
+	trace_i915_request_dep_to(to);
+	trace_i915_request_dep_from(from);
+
 	/* Couple the dependency tree for PI on this exposed to->fence */
 	if (to->engine->sched_engine->schedule) {
 		err = i915_sched_node_add_dependency(&to->sched,
drivers/gpu/drm/i915/i915_trace.h +37 -2
index 6778ad2a14a4..b02d04b6c8f6 100644
@@ -794,22 +794,27 @@ DECLARE_EVENT_CLASS(i915_request,
 	    TP_STRUCT__entry(
 			     __field(u32, dev)
 			     __field(u64, ctx)
+			     __field(u32, guc_id)
 			     __field(u16, class)
 			     __field(u16, instance)
 			     __field(u32, seqno)
+			     __field(u32, tail)
 			     ),
 
 	    TP_fast_assign(
 			   __entry->dev = rq->engine->i915->drm.primary->index;
 			   __entry->class = rq->engine->uabi_class;
 			   __entry->instance = rq->engine->uabi_instance;
+			   __entry->guc_id = rq->context->guc_id;
 			   __entry->ctx = rq->fence.context;
 			   __entry->seqno = rq->fence.seqno;
+			   __entry->tail = rq->tail;
 			   ),
 
-	    TP_printk("dev=%u, engine=%u:%u, ctx=%llu, seqno=%u",
+	    TP_printk("dev=%u, engine=%u:%u, guc_id=%u, ctx=%llu, seqno=%u, tail=%u",
 		      __entry->dev, __entry->class, __entry->instance,
-		      __entry->ctx, __entry->seqno)
+		      __entry->guc_id, __entry->ctx, __entry->seqno,
+		      __entry->tail)
 );
 
 DEFINE_EVENT(i915_request, i915_request_add,
@@ -818,6 +823,21 @@ DEFINE_EVENT(i915_request, i915_request_add,
 );
 
 #if defined(CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS)
+DEFINE_EVENT(i915_request, i915_request_dep_to,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
+DEFINE_EVENT(i915_request, i915_request_dep_from,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
+DEFINE_EVENT(i915_request, i915_request_guc_submit,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
 DEFINE_EVENT(i915_request, i915_request_submit,
 	     TP_PROTO(struct i915_request *rq),
 	     TP_ARGS(rq)
@@ -887,6 +907,21 @@ TRACE_EVENT(i915_request_out,
 
 #else
 #if !defined(TRACE_HEADER_MULTI_READ)
+static inline void
+trace_i915_request_dep_to(struct i915_request *rq)
+{
+}
+
+static inline void
+trace_i915_request_dep_from(struct i915_request *rq)
+{
+}
+
+static inline void
+trace_i915_request_guc_submit(struct i915_request *rq)
+{
+}
+
 static inline void
 trace_i915_request_submit(struct i915_request *rq)
 {
Signature · 6 lines
2.28.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Reviewed-by on v1 24/47
#77 lore raw

Re: [PATCH 24/47] drm/i915/guc: Add several request trace points

Quoted 2 lines · Matthew Brost

Add trace points for request dependencies and GuC submit. Extended existing request trace points to include submit fence value,, guc_id,

Excessive punctuation. Or maybe should say 'fence value, tail, guc_id'? With that fixed:

Quoted 123 lines

and ring tail value.

---
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  3 ++
  drivers/gpu/drm/i915/i915_request.c           |  3 ++
  drivers/gpu/drm/i915/i915_trace.h             | 39 ++++++++++++++++++-
  3 files changed, 43 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index 89b3c7e5d15b..c2327eebc09c 100644
@@ -422,6 +422,7 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  			guc->stalled_request = last;
  			return false;
  		}
+		trace_i915_request_guc_submit(last);
  	}
 
  	guc->stalled_request = NULL;
@@ -642,6 +643,8 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
  	ret = guc_add_request(guc, rq);
  	if (ret == -EBUSY)
  		guc->stalled_request = rq;
+	else
+		trace_i915_request_guc_submit(rq);
 
  	return ret;
  }
drivers/gpu/drm/i915/i915_request.c +3 -0
index d92c9f25c9f4..7f7aa096e873 100644
@@ -1344,6 +1344,9 @@ __i915_request_await_execution(struct i915_request *to,
  			return err;
  	}
 
+	trace_i915_request_dep_to(to);
+	trace_i915_request_dep_from(from);
+
  	/* Couple the dependency tree for PI on this exposed to->fence */
  	if (to->engine->sched_engine->schedule) {
  		err = i915_sched_node_add_dependency(&to->sched,
drivers/gpu/drm/i915/i915_trace.h +37 -2
index 6778ad2a14a4..b02d04b6c8f6 100644
@@ -794,22 +794,27 @@ DECLARE_EVENT_CLASS(i915_request,
  	    TP_STRUCT__entry(
  			     __field(u32, dev)
  			     __field(u64, ctx)
+			     __field(u32, guc_id)
  			     __field(u16, class)
  			     __field(u16, instance)
  			     __field(u32, seqno)
+			     __field(u32, tail)
  			     ),
 
  	    TP_fast_assign(
  			   __entry->dev = rq->engine->i915->drm.primary->index;
  			   __entry->class = rq->engine->uabi_class;
  			   __entry->instance = rq->engine->uabi_instance;
+			   __entry->guc_id = rq->context->guc_id;
  			   __entry->ctx = rq->fence.context;
  			   __entry->seqno = rq->fence.seqno;
+			   __entry->tail = rq->tail;
  			   ),
 
-	    TP_printk("dev=%u, engine=%u:%u, ctx=%llu, seqno=%u",
+	    TP_printk("dev=%u, engine=%u:%u, guc_id=%u, ctx=%llu, seqno=%u, tail=%u",
  		      __entry->dev, __entry->class, __entry->instance,
-		      __entry->ctx, __entry->seqno)
+		      __entry->guc_id, __entry->ctx, __entry->seqno,
+		      __entry->tail)
  );
 
  DEFINE_EVENT(i915_request, i915_request_add,
@@ -818,6 +823,21 @@ DEFINE_EVENT(i915_request, i915_request_add,
  );
 
  #if defined(CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS)
+DEFINE_EVENT(i915_request, i915_request_dep_to,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
+DEFINE_EVENT(i915_request, i915_request_dep_from,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
+DEFINE_EVENT(i915_request, i915_request_guc_submit,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
  DEFINE_EVENT(i915_request, i915_request_submit,
  	     TP_PROTO(struct i915_request *rq),
  	     TP_ARGS(rq)
@@ -887,6 +907,21 @@ TRACE_EVENT(i915_request_out,
 
  #else
  #if !defined(TRACE_HEADER_MULTI_READ)
+static inline void
+trace_i915_request_dep_to(struct i915_request *rq)
+{
+}
+
+static inline void
+trace_i915_request_dep_from(struct i915_request *rq)
+{
+}
+
+static inline void
+trace_i915_request_guc_submit(struct i915_request *rq)
+{
+}
+
  static inline void
  trace_i915_request_submit(struct i915_request *rq)
  {
#78 lore raw

Re: [Intel-gfx] [PATCH 24/47] drm/i915/guc: Add several request trace points

In reply to Matthew Brost
Quoted 38 lines · Matthew Brost

Add trace points for request dependencies and GuC submit. Extended existing request trace points to include submit fence value,, guc_id, and ring tail value.

---
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  3 ++
  drivers/gpu/drm/i915/i915_request.c           |  3 ++
  drivers/gpu/drm/i915/i915_trace.h             | 39 ++++++++++++++++++-
  3 files changed, 43 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index 89b3c7e5d15b..c2327eebc09c 100644
@@ -422,6 +422,7 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  			guc->stalled_request = last;
  			return false;
  		}
+		trace_i915_request_guc_submit(last);
  	}
 
  	guc->stalled_request = NULL;
@@ -642,6 +643,8 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
  	ret = guc_add_request(guc, rq);
  	if (ret == -EBUSY)
  		guc->stalled_request = rq;
+	else
+		trace_i915_request_guc_submit(rq);
 
  	return ret;
  }
drivers/gpu/drm/i915/i915_request.c +0 -0
index d92c9f25c9f4..7f7aa096e873 100644

drivers/gpu/drm/i915/i915_request.c __i915_request_await_execution(struct i915_request *to, · line 1348

Quoted 5 lines
drivers/gpu/drm/i915/i915_request.c +2 -0
@@ -1344,6 +1344,9 @@ __i915_request_await_execution(struct i915_request *to,
  			return err;
  	}
 
+	trace_i915_request_dep_to(to);
+	trace_i915_request_dep_from(from);

Are those two guaranteed to be atomic ie. no other dep_to/dep_from can get injected in the middle of them and if so what guarantees that?

Actually we had an internal discussion going in November 2019 on these very tracepoints which I think was left hanging in the air.

There I was suggesting you create a single tracepoint in the format of "from -> to", so it's clear without any doubt what is going on.

I also suggested this should out outside the GuC patch since it is backend agnostic.

I also asked why only this flavour of dependencies and not all. You said this was the handy one for debugging GuC backend issues. I said in that case you should name it trace_i915_request_await_request so it is clearer it does not cover all dependencies.

As it stands it is a bit misleadingly named, has that question mark around atomicity, and also is not GuC specific. So really I wouldn't think it passes the bar in the current state. Regards,

Tvrtko

P.S. Same discussion from 2019 also talked about trace_i915_request_guc_submit and how it exactly aligns to existing request in tracepoint. You were saying the new one is handy because it corresponds with H2G, as the last request_in of the group would trigger it. I was saying that then you could either know implicitly last request_in triggers H2G, or that you could consider adding explicit H2G tracepoints.

Quoted 83 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
+
  	/* Couple the dependency tree for PI on this exposed to->fence */
  	if (to->engine->sched_engine->schedule) {
  		err = i915_sched_node_add_dependency(&to->sched,
drivers/gpu/drm/i915/i915_trace.h +37 -2
index 6778ad2a14a4..b02d04b6c8f6 100644
@@ -794,22 +794,27 @@ DECLARE_EVENT_CLASS(i915_request,
  	    TP_STRUCT__entry(
  			     __field(u32, dev)
  			     __field(u64, ctx)
+			     __field(u32, guc_id)
  			     __field(u16, class)
  			     __field(u16, instance)
  			     __field(u32, seqno)
+			     __field(u32, tail)
  			     ),
 
  	    TP_fast_assign(
  			   __entry->dev = rq->engine->i915->drm.primary->index;
  			   __entry->class = rq->engine->uabi_class;
  			   __entry->instance = rq->engine->uabi_instance;
+			   __entry->guc_id = rq->context->guc_id;
  			   __entry->ctx = rq->fence.context;
  			   __entry->seqno = rq->fence.seqno;
+			   __entry->tail = rq->tail;
  			   ),
 
-	    TP_printk("dev=%u, engine=%u:%u, ctx=%llu, seqno=%u",
+	    TP_printk("dev=%u, engine=%u:%u, guc_id=%u, ctx=%llu, seqno=%u, tail=%u",
  		      __entry->dev, __entry->class, __entry->instance,
-		      __entry->ctx, __entry->seqno)
+		      __entry->guc_id, __entry->ctx, __entry->seqno,
+		      __entry->tail)
  );
 
  DEFINE_EVENT(i915_request, i915_request_add,
@@ -818,6 +823,21 @@ DEFINE_EVENT(i915_request, i915_request_add,
  );
 
  #if defined(CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS)
+DEFINE_EVENT(i915_request, i915_request_dep_to,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
+DEFINE_EVENT(i915_request, i915_request_dep_from,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
+DEFINE_EVENT(i915_request, i915_request_guc_submit,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
  DEFINE_EVENT(i915_request, i915_request_submit,
  	     TP_PROTO(struct i915_request *rq),
  	     TP_ARGS(rq)
@@ -887,6 +907,21 @@ TRACE_EVENT(i915_request_out,
 
  #else
  #if !defined(TRACE_HEADER_MULTI_READ)
+static inline void
+trace_i915_request_dep_to(struct i915_request *rq)
+{
+}
+
+static inline void
+trace_i915_request_dep_from(struct i915_request *rq)
+{
+}
+
+static inline void
+trace_i915_request_guc_submit(struct i915_request *rq)
+{
+}
+
  static inline void
  trace_i915_request_submit(struct i915_request *rq)
  {
#79 lore raw

Re: [Intel-gfx] [PATCH 24/47] drm/i915/guc: Add several request trace points

Quoted 37 lines · Tvrtko Ursulin
Quoted 35 lines · Matthew Brost

Add trace points for request dependencies and GuC submit. Extended existing request trace points to include submit fence value,, guc_id, and ring tail value.

---
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  3 ++
  drivers/gpu/drm/i915/i915_request.c           |  3 ++
  drivers/gpu/drm/i915/i915_trace.h             | 39 ++++++++++++++++++-
  3 files changed, 43 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index 89b3c7e5d15b..c2327eebc09c 100644
@@ -422,6 +422,7 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  			guc->stalled_request = last;
  			return false;
  		}
+		trace_i915_request_guc_submit(last);
  	}
  	guc->stalled_request = NULL;
@@ -642,6 +643,8 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
  	ret = guc_add_request(guc, rq);
  	if (ret == -EBUSY)
  		guc->stalled_request = rq;
+	else
+		trace_i915_request_guc_submit(rq);
  	return ret;
  }
drivers/gpu/drm/i915/i915_request.c +0 -0
index d92c9f25c9f4..7f7aa096e873 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c __i915_request_await_execution(struct i915_request *to, · line 1347

Quoted 5 lines
drivers/gpu/drm/i915/i915_request.c +2 -0
@@ -1344,6 +1344,9 @@ __i915_request_await_execution(struct i915_request *to,
  			return err;
  	}
+	trace_i915_request_dep_to(to);
+	trace_i915_request_dep_from(from);

Are those two guaranteed to be atomic ie. no other dep_to/dep_from can get injected in the middle of them and if so what guarantees that?

These are not atomic but in practice I've never seen an out of order tracepoints.

Quoted 6 lines

Actually we had an internal discussion going in November 2019 on these very tracepoints which I think was left hanging in the air.

There I was suggesting you create a single tracepoint in the format of "from -> to", so it's clear without any doubt what is going on.

Not sure if it worth adding a custom trace point fo rthis.

Quoted 2 lines

I also suggested this should out outside the GuC patch since it is backend agnostic.

I guess, but it really matter?

Quoted 6 lines

I also asked why only this flavour of dependencies and not all. You said this was the handy one for debugging GuC backend issues. I said in that case you should name it trace_i915_request_await_request so it is clearer it does not cover all dependencies.

Can't we look at the code? For kernel dev trace point I don't this it is to much to ask a developer to grep around the code. Also you likely only turn these on if you know what you are doing anyways.

Quoted 3 lines

As it stands it is a bit misleadingly named, has that question mark around atomicity, and also is not GuC specific. So really I wouldn't think it passes the bar in the current state.

I'll just delete them.

Quoted 7 lines

Regards,

Tvrtko

P.S. Same discussion from 2019 also talked about trace_i915_request_guc_submit and how it exactly aligns to existing request in tracepoint.

I doesn't align. You literally make the point about how it doesn't align below.

Quoted 4 lines

You were saying the new one is handy because it corresponds
with H2G, as the last request_in of the group would trigger it. I was saying that then you could either know implicitly last request_in triggers H2G, or that you could consider adding explicit H2G tracepoints.

Yes, we have a trace point for every H2G. Again the users of these tracepoints know what they mean.

Matt

Quoted 79 lines
Quoted 79 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
+
  	/* Couple the dependency tree for PI on this exposed to->fence */
  	if (to->engine->sched_engine->schedule) {
  		err = i915_sched_node_add_dependency(&to->sched,
drivers/gpu/drm/i915/i915_trace.h +37 -2
index 6778ad2a14a4..b02d04b6c8f6 100644
@@ -794,22 +794,27 @@ DECLARE_EVENT_CLASS(i915_request,
  	    TP_STRUCT__entry(
  			     __field(u32, dev)
  			     __field(u64, ctx)
+			     __field(u32, guc_id)
  			     __field(u16, class)
  			     __field(u16, instance)
  			     __field(u32, seqno)
+			     __field(u32, tail)
  			     ),
  	    TP_fast_assign(
  			   __entry->dev = rq->engine->i915->drm.primary->index;
  			   __entry->class = rq->engine->uabi_class;
  			   __entry->instance = rq->engine->uabi_instance;
+			   __entry->guc_id = rq->context->guc_id;
  			   __entry->ctx = rq->fence.context;
  			   __entry->seqno = rq->fence.seqno;
+			   __entry->tail = rq->tail;
  			   ),
-	    TP_printk("dev=%u, engine=%u:%u, ctx=%llu, seqno=%u",
+	    TP_printk("dev=%u, engine=%u:%u, guc_id=%u, ctx=%llu, seqno=%u, tail=%u",
  		      __entry->dev, __entry->class, __entry->instance,
-		      __entry->ctx, __entry->seqno)
+		      __entry->guc_id, __entry->ctx, __entry->seqno,
+		      __entry->tail)
  );
  DEFINE_EVENT(i915_request, i915_request_add,
@@ -818,6 +823,21 @@ DEFINE_EVENT(i915_request, i915_request_add,
  );
  #if defined(CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS)
+DEFINE_EVENT(i915_request, i915_request_dep_to,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
+DEFINE_EVENT(i915_request, i915_request_dep_from,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
+DEFINE_EVENT(i915_request, i915_request_guc_submit,
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
+);
+
  DEFINE_EVENT(i915_request, i915_request_submit,
  	     TP_PROTO(struct i915_request *rq),
  	     TP_ARGS(rq)
@@ -887,6 +907,21 @@ TRACE_EVENT(i915_request_out,
  #else
  #if !defined(TRACE_HEADER_MULTI_READ)
+static inline void
+trace_i915_request_dep_to(struct i915_request *rq)
+{
+}
+
+static inline void
+trace_i915_request_dep_from(struct i915_request *rq)
+{
+}
+
+static inline void
+trace_i915_request_guc_submit(struct i915_request *rq)
+{
+}
+
  static inline void
  trace_i915_request_submit(struct i915_request *rq)
  {
 
#80 lore raw

Re: [Intel-gfx] [PATCH 24/47] drm/i915/guc: Add several request trace points

Quoted 51 lines · Matthew Brost
Quoted 37 lines · Tvrtko Ursulin
Quoted 35 lines · Matthew Brost

Add trace points for request dependencies and GuC submit. Extended existing request trace points to include submit fence value,, guc_id, and ring tail value.

---
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  3 ++
   drivers/gpu/drm/i915/i915_request.c           |  3 ++
   drivers/gpu/drm/i915/i915_trace.h             | 39 ++++++++++++++++++-
   3 files changed, 43 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index 89b3c7e5d15b..c2327eebc09c 100644
@@ -422,6 +422,7 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
   			guc->stalled_request = last;
   			return false;
   		}
+		trace_i915_request_guc_submit(last);
   	}
   	guc->stalled_request = NULL;
@@ -642,6 +643,8 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
   	ret = guc_add_request(guc, rq);
   	if (ret == -EBUSY)
   		guc->stalled_request = rq;
+	else
+		trace_i915_request_guc_submit(rq);
   	return ret;
   }
drivers/gpu/drm/i915/i915_request.c +0 -0
index d92c9f25c9f4..7f7aa096e873 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c __i915_request_await_execution(struct i915_request *to, · line 1347

Quoted 5 lines
drivers/gpu/drm/i915/i915_request.c +2 -0
@@ -1344,6 +1344,9 @@ __i915_request_await_execution(struct i915_request *to,
   			return err;
   	}
+	trace_i915_request_dep_to(to);
+	trace_i915_request_dep_from(from);

Are those two guaranteed to be atomic ie. no other dep_to/dep_from can get injected in the middle of them and if so what guarantees that?

These are not atomic but in practice I've never seen an out of order tracepoints.

Quoted 8 lines
Quoted 6 lines

Actually we had an internal discussion going in November 2019 on these very tracepoints which I think was left hanging in the air.

There I was suggesting you create a single tracepoint in the format of "from -> to", so it's clear without any doubt what is going on.

Not sure if it worth adding a custom trace point fo rthis.

Custom as in not inherit from i915_request class you mean? It's not that hard really.

Quoted 4 lines
Quoted 2 lines

I also suggested this should out outside the GuC patch since it is backend agnostic.

I guess, but it really matter?

IMO following best practices and established conventions matters a lot.

Regards,

Tvrtko

#81 lore raw

PATCH 25/47 drm/i915: Add intel_context tracing

In reply to Matthew Brost

Add intel_context tracing. These trace points are particular helpful when debugging the GuC firmware and can be enabled via CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS kernel config option.

---
 drivers/gpu/drm/i915/gt/intel_context.c       |   6 +
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  14 ++
 drivers/gpu/drm/i915/i915_trace.h             | 148 +++++++++++++++++-
 3 files changed, 166 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +6 -0
index 7f97753ab164..b24a1b7a3f88 100644
@@ -8,6 +8,7 @@
 
 #include "i915_drv.h"
 #include "i915_globals.h"
+#include "i915_trace.h"
 
 #include "intel_context.h"
 #include "intel_engine.h"
@@ -28,6 +29,7 @@ static void rcu_context_free(struct rcu_head *rcu)
 {
 	struct intel_context *ce = container_of(rcu, typeof(*ce), rcu);
 
+	trace_intel_context_free(ce);
 	kmem_cache_free(global.slab_ce, ce);
 }
 
@@ -46,6 +48,7 @@ intel_context_create(struct intel_engine_cs *engine)
 		return ERR_PTR(-ENOMEM);
 
 	intel_context_init(ce, engine);
+	trace_intel_context_create(ce);
 	return ce;
 }
 
@@ -268,6 +271,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
 
 	GEM_BUG_ON(!intel_context_is_pinned(ce)); /* no overflow! */
 
+	trace_intel_context_do_pin(ce);
+
 err_unlock:
 	mutex_unlock(&ce->pin_mutex);
 err_post_unpin:
@@ -323,6 +328,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
 	 */
 	intel_context_get(ce);
 	intel_context_active_release(ce);
+	trace_intel_context_do_unpin(ce);
 	intel_context_put(ce);
 }
 
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +14 -0
index c2327eebc09c..d605af0d66e6 100644
@@ -348,6 +348,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
 
 	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
 	if (!enabled && !err) {
+		trace_intel_context_sched_enable(ce);
 		atomic_inc(&guc->outstanding_submission_g2h);
 		set_context_enabled(ce);
 	} else if (!enabled) {
@@ -812,6 +813,8 @@ static int register_context(struct intel_context *ce)
 	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
 		ce->guc_id * sizeof(struct guc_lrc_desc);
 
+	trace_intel_context_register(ce);
+
 	return __guc_action_register_context(guc, ce->guc_id, offset);
 }
 
@@ -831,6 +834,8 @@ static int deregister_context(struct intel_context *ce, u32 guc_id)
 {
 	struct intel_guc *guc = ce_to_guc(ce);
 
+	trace_intel_context_deregister(ce);
+
 	return __guc_action_deregister_context(guc, guc_id);
 }
 
@@ -905,6 +910,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
 	 * GuC before registering this context.
 	 */
 	if (context_registered) {
+		trace_intel_context_steal_guc_id(ce);
 		set_context_wait_for_deregister_to_register(ce);
 		intel_context_get(ce);
 
@@ -963,6 +969,7 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
 
 	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
 
+	trace_intel_context_sched_disable(ce);
 	intel_context_get(ce);
 
 	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
@@ -1119,6 +1126,9 @@ static void __guc_signal_context_fence(struct intel_context *ce)
 
 	lockdep_assert_held(&ce->guc_state.lock);
 
+	if (!list_empty(&ce->guc_state.fences))
+		trace_intel_context_fence_release(ce);
+
 	list_for_each_entry(rq, &ce->guc_state.fences, guc_fence_link)
 		i915_sw_fence_complete(&rq->submit);
 
@@ -1529,6 +1539,8 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
 	if (unlikely(!ce))
 		return -EPROTO;
 
+	trace_intel_context_deregister_done(ce);
+
 	if (context_wait_for_deregister_to_register(ce)) {
 		struct intel_runtime_pm *runtime_pm =
 			&ce->engine->gt->i915->runtime_pm;
@@ -1580,6 +1592,8 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
 		return -EPROTO;
 	}
 
+	trace_intel_context_sched_done(ce);
+
 	if (context_pending_enable(ce)) {
 		clr_context_pending_enable(ce);
 	} else if (context_pending_disable(ce)) {
drivers/gpu/drm/i915/i915_trace.h +146 -2

166 lines not on this page · raw message

Signature · 1 line
2.28.0
#82 lore raw

Re: [PATCH 25/47] drm/i915: Add intel_context tracing

Quoted 136 lines · Matthew Brost

Add intel_context tracing. These trace points are particular helpful when debugging the GuC firmware and can be enabled via CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS kernel config option.

---
  drivers/gpu/drm/i915/gt/intel_context.c       |   6 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  14 ++
  drivers/gpu/drm/i915/i915_trace.h             | 148 +++++++++++++++++-
  3 files changed, 166 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +6 -0
index 7f97753ab164..b24a1b7a3f88 100644
@@ -8,6 +8,7 @@
 
  #include "i915_drv.h"
  #include "i915_globals.h"
+#include "i915_trace.h"
 
  #include "intel_context.h"
  #include "intel_engine.h"
@@ -28,6 +29,7 @@ static void rcu_context_free(struct rcu_head *rcu)
  {
  	struct intel_context *ce = container_of(rcu, typeof(*ce), rcu);
 
+	trace_intel_context_free(ce);
  	kmem_cache_free(global.slab_ce, ce);
  }
 
@@ -46,6 +48,7 @@ intel_context_create(struct intel_engine_cs *engine)
  		return ERR_PTR(-ENOMEM);
 
  	intel_context_init(ce, engine);
+	trace_intel_context_create(ce);
  	return ce;
  }
 
@@ -268,6 +271,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
 
  	GEM_BUG_ON(!intel_context_is_pinned(ce)); /* no overflow! */
 
+	trace_intel_context_do_pin(ce);
+
  err_unlock:
  	mutex_unlock(&ce->pin_mutex);
  err_post_unpin:
@@ -323,6 +328,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
  	 */
  	intel_context_get(ce);
  	intel_context_active_release(ce);
+	trace_intel_context_do_unpin(ce);
  	intel_context_put(ce);
  }
 
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +14 -0
index c2327eebc09c..d605af0d66e6 100644
@@ -348,6 +348,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
 
  	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
  	if (!enabled && !err) {
+		trace_intel_context_sched_enable(ce);
  		atomic_inc(&guc->outstanding_submission_g2h);
  		set_context_enabled(ce);
  	} else if (!enabled) {
@@ -812,6 +813,8 @@ static int register_context(struct intel_context *ce)
  	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
  		ce->guc_id * sizeof(struct guc_lrc_desc);
 
+	trace_intel_context_register(ce);
+
  	return __guc_action_register_context(guc, ce->guc_id, offset);
  }
 
@@ -831,6 +834,8 @@ static int deregister_context(struct intel_context *ce, u32 guc_id)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
 
+	trace_intel_context_deregister(ce);
+
  	return __guc_action_deregister_context(guc, guc_id);
  }
 
@@ -905,6 +910,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
  	 * GuC before registering this context.
  	 */
  	if (context_registered) {
+		trace_intel_context_steal_guc_id(ce);
  		set_context_wait_for_deregister_to_register(ce);
  		intel_context_get(ce);
 
@@ -963,6 +969,7 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
 
  	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
 
+	trace_intel_context_sched_disable(ce);
  	intel_context_get(ce);
 
  	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
@@ -1119,6 +1126,9 @@ static void __guc_signal_context_fence(struct intel_context *ce)
 
  	lockdep_assert_held(&ce->guc_state.lock);
 
+	if (!list_empty(&ce->guc_state.fences))
+		trace_intel_context_fence_release(ce);
+
  	list_for_each_entry(rq, &ce->guc_state.fences, guc_fence_link)
  		i915_sw_fence_complete(&rq->submit);
 
@@ -1529,6 +1539,8 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  	if (unlikely(!ce))
  		return -EPROTO;
 
+	trace_intel_context_deregister_done(ce);
+
  	if (context_wait_for_deregister_to_register(ce)) {
  		struct intel_runtime_pm *runtime_pm =
  			&ce->engine->gt->i915->runtime_pm;
@@ -1580,6 +1592,8 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
  		return -EPROTO;
  	}
 
+	trace_intel_context_sched_done(ce);
+
  	if (context_pending_enable(ce)) {
  		clr_context_pending_enable(ce);
  	} else if (context_pending_disable(ce)) {
drivers/gpu/drm/i915/i915_trace.h +0 -0
index b02d04b6c8f6..97c2e83984ed 100644

drivers/gpu/drm/i915/i915_trace.h DECLARE_EVENT_CLASS(i915_request, · line 822

Quoted 7 lines
drivers/gpu/drm/i915/i915_trace.h +2 -2
@@ -818,8 +818,8 @@ DECLARE_EVENT_CLASS(i915_request,
  );
 
  DEFINE_EVENT(i915_request, i915_request_add,
-	    TP_PROTO(struct i915_request *rq),
-	    TP_ARGS(rq)
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)

Is this an intentional white space change?

Quoted 161 lines
drivers/gpu/drm/i915/i915_trace.h +144 -0
  );
 
  #if defined(CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS)
@@ -905,6 +905,90 @@ TRACE_EVENT(i915_request_out,
  			      __entry->ctx, __entry->seqno, __entry->completed)
  );
 
+DECLARE_EVENT_CLASS(intel_context,
+	    TP_PROTO(struct intel_context *ce),
+	    TP_ARGS(ce),
+
+	    TP_STRUCT__entry(
+			     __field(u32, guc_id)
+			     __field(int, pin_count)
+			     __field(u32, sched_state)
+			     __field(u32, guc_sched_state_no_lock)
+			     ),
+
+	    TP_fast_assign(
+			   __entry->guc_id = ce->guc_id;
+			   __entry->pin_count = atomic_read(&ce->pin_count);
+			   __entry->sched_state = ce->guc_state.sched_state;
+			   __entry->guc_sched_state_no_lock =
+			   atomic_read(&ce->guc_sched_state_no_lock);
+			   ),
+
+	    TP_printk("guc_id=%d, pin_count=%d sched_state=0x%x,0x%x",
+		      __entry->guc_id, __entry->pin_count, __entry->sched_state,
+		      __entry->guc_sched_state_no_lock)
+);
+
+DEFINE_EVENT(intel_context, intel_context_register,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_deregister,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_deregister_done,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_enable,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_disable,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_done,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_create,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_fence_release,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_free,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_steal_guc_id,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_do_pin,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_do_unpin,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
  #else
  #if !defined(TRACE_HEADER_MULTI_READ)
  static inline void
@@ -941,6 +1025,66 @@ static inline void
  trace_i915_request_out(struct i915_request *rq)
  {
  }
+
+static inline void
+trace_intel_context_register(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_deregister(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_deregister_done(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_enable(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_disable(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_done(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_create(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_fence_release(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_free(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_steal_guc_id(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_do_pin(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_do_unpin(struct intel_context *ce)
+{
+}
  #endif
  #endif
 
#83 lore raw

Re: [PATCH 25/47] drm/i915: Add intel_context tracing

Quoted 113 lines · John Harrison
Quoted 112 lines · Matthew Brost

Add intel_context tracing. These trace points are particular helpful when debugging the GuC firmware and can be enabled via CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS kernel config option.

---
  drivers/gpu/drm/i915/gt/intel_context.c       |   6 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  14 ++
  drivers/gpu/drm/i915/i915_trace.h             | 148 +++++++++++++++++-
  3 files changed, 166 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +6 -0
index 7f97753ab164..b24a1b7a3f88 100644
@@ -8,6 +8,7 @@
  #include "i915_drv.h"
  #include "i915_globals.h"
+#include "i915_trace.h"
  #include "intel_context.h"
  #include "intel_engine.h"
@@ -28,6 +29,7 @@ static void rcu_context_free(struct rcu_head *rcu)
  {
  	struct intel_context *ce = container_of(rcu, typeof(*ce), rcu);
+	trace_intel_context_free(ce);
  	kmem_cache_free(global.slab_ce, ce);
  }
@@ -46,6 +48,7 @@ intel_context_create(struct intel_engine_cs *engine)
  		return ERR_PTR(-ENOMEM);
  	intel_context_init(ce, engine);
+	trace_intel_context_create(ce);
  	return ce;
  }
@@ -268,6 +271,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
  	GEM_BUG_ON(!intel_context_is_pinned(ce)); /* no overflow! */
+	trace_intel_context_do_pin(ce);
+
  err_unlock:
  	mutex_unlock(&ce->pin_mutex);
  err_post_unpin:
@@ -323,6 +328,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
  	 */
  	intel_context_get(ce);
  	intel_context_active_release(ce);
+	trace_intel_context_do_unpin(ce);
  	intel_context_put(ce);
  }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +14 -0
index c2327eebc09c..d605af0d66e6 100644
@@ -348,6 +348,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
  	if (!enabled && !err) {
+		trace_intel_context_sched_enable(ce);
  		atomic_inc(&guc->outstanding_submission_g2h);
  		set_context_enabled(ce);
  	} else if (!enabled) {
@@ -812,6 +813,8 @@ static int register_context(struct intel_context *ce)
  	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
  		ce->guc_id * sizeof(struct guc_lrc_desc);
+	trace_intel_context_register(ce);
+
  	return __guc_action_register_context(guc, ce->guc_id, offset);
  }
@@ -831,6 +834,8 @@ static int deregister_context(struct intel_context *ce, u32 guc_id)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
+	trace_intel_context_deregister(ce);
+
  	return __guc_action_deregister_context(guc, guc_id);
  }
@@ -905,6 +910,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
  	 * GuC before registering this context.
  	 */
  	if (context_registered) {
+		trace_intel_context_steal_guc_id(ce);
  		set_context_wait_for_deregister_to_register(ce);
  		intel_context_get(ce);
@@ -963,6 +969,7 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
  	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
+	trace_intel_context_sched_disable(ce);
  	intel_context_get(ce);
  	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
@@ -1119,6 +1126,9 @@ static void __guc_signal_context_fence(struct intel_context *ce)
  	lockdep_assert_held(&ce->guc_state.lock);
+	if (!list_empty(&ce->guc_state.fences))
+		trace_intel_context_fence_release(ce);
+
  	list_for_each_entry(rq, &ce->guc_state.fences, guc_fence_link)
  		i915_sw_fence_complete(&rq->submit);
@@ -1529,6 +1539,8 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  	if (unlikely(!ce))
  		return -EPROTO;
+	trace_intel_context_deregister_done(ce);
+
  	if (context_wait_for_deregister_to_register(ce)) {
  		struct intel_runtime_pm *runtime_pm =
  			&ce->engine->gt->i915->runtime_pm;
@@ -1580,6 +1592,8 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
  		return -EPROTO;
  	}
+	trace_intel_context_sched_done(ce);
+
  	if (context_pending_enable(ce)) {
  		clr_context_pending_enable(ce);
  	} else if (context_pending_disable(ce)) {
drivers/gpu/drm/i915/i915_trace.h +0 -0
index b02d04b6c8f6..97c2e83984ed 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_trace.h DECLARE_EVENT_CLASS(i915_request, · line 821

Quoted 7 lines
drivers/gpu/drm/i915/i915_trace.h +2 -2
@@ -818,8 +818,8 @@ DECLARE_EVENT_CLASS(i915_request,
  );
  DEFINE_EVENT(i915_request, i915_request_add,
-	    TP_PROTO(struct i915_request *rq),
-	    TP_ARGS(rq)
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)

Is this an intentional white space change?

Yea, probably should be in the previous patch though. Before this change the arguments were misaligned.

Matt

Quoted 150 lines
Quoted 150 lines
drivers/gpu/drm/i915/i915_trace.h +138 -0
  );
  #if defined(CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS)
@@ -905,6 +905,90 @@ TRACE_EVENT(i915_request_out,
  			      __entry->ctx, __entry->seqno, __entry->completed)
  );
+DECLARE_EVENT_CLASS(intel_context,
+	    TP_PROTO(struct intel_context *ce),
+	    TP_ARGS(ce),
+
+	    TP_STRUCT__entry(
+			     __field(u32, guc_id)
+			     __field(int, pin_count)
+			     __field(u32, sched_state)
+			     __field(u32, guc_sched_state_no_lock)
+			     ),
+
+	    TP_fast_assign(
+			   __entry->guc_id = ce->guc_id;
+			   __entry->pin_count = atomic_read(&ce->pin_count);
+			   __entry->sched_state = ce->guc_state.sched_state;
+			   __entry->guc_sched_state_no_lock =
+			   atomic_read(&ce->guc_sched_state_no_lock);
+			   ),
+
+	    TP_printk("guc_id=%d, pin_count=%d sched_state=0x%x,0x%x",
+		      __entry->guc_id, __entry->pin_count, __entry->sched_state,
+		      __entry->guc_sched_state_no_lock)
+);
+
+DEFINE_EVENT(intel_context, intel_context_register,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_deregister,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_deregister_done,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_enable,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_disable,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_done,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_create,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_fence_release,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_free,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_steal_guc_id,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_do_pin,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_do_unpin,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
  #else
  #if !defined(TRACE_HEADER_MULTI_READ)
  static inline void
@@ -941,6 +1025,66 @@ static inline void
  trace_i915_request_out(struct i915_request *rq)
  {
  }
+
+static inline void
+trace_intel_context_register(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_deregister(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_deregister_done(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_enable(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_disable(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_done(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_create(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_fence_release(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_free(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_steal_guc_id(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_do_pin(struct intel_context *ce)
+{
Quoted 9 lines

drivers/gpu/drm/i915/i915_trace.h static inline void · line 1089

Quoted 8 lines
drivers/gpu/drm/i915/i915_trace.h +6 -0
+}
+
+static inline void
+trace_intel_context_do_unpin(struct intel_context *ce)
+{
+}
  #endif
  #endif

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reviewed-by
#84 lore raw

Re: [PATCH 25/47] drm/i915: Add intel_context tracing

Quoted 123 lines · Matthew Brost
Quoted 113 lines · John Harrison
Quoted 112 lines · Matthew Brost

Add intel_context tracing. These trace points are particular helpful when debugging the GuC firmware and can be enabled via CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS kernel config option.

---
   drivers/gpu/drm/i915/gt/intel_context.c       |   6 +
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  14 ++
   drivers/gpu/drm/i915/i915_trace.h             | 148 +++++++++++++++++-
   3 files changed, 166 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +6 -0
index 7f97753ab164..b24a1b7a3f88 100644
@@ -8,6 +8,7 @@
   #include "i915_drv.h"
   #include "i915_globals.h"
+#include "i915_trace.h"
   #include "intel_context.h"
   #include "intel_engine.h"
@@ -28,6 +29,7 @@ static void rcu_context_free(struct rcu_head *rcu)
   {
   	struct intel_context *ce = container_of(rcu, typeof(*ce), rcu);
+	trace_intel_context_free(ce);
   	kmem_cache_free(global.slab_ce, ce);
   }
@@ -46,6 +48,7 @@ intel_context_create(struct intel_engine_cs *engine)
   		return ERR_PTR(-ENOMEM);
   	intel_context_init(ce, engine);
+	trace_intel_context_create(ce);
   	return ce;
   }
@@ -268,6 +271,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
   	GEM_BUG_ON(!intel_context_is_pinned(ce)); /* no overflow! */
+	trace_intel_context_do_pin(ce);
+
   err_unlock:
   	mutex_unlock(&ce->pin_mutex);
   err_post_unpin:
@@ -323,6 +328,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
   	 */
   	intel_context_get(ce);
   	intel_context_active_release(ce);
+	trace_intel_context_do_unpin(ce);
   	intel_context_put(ce);
   }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +14 -0
index c2327eebc09c..d605af0d66e6 100644
@@ -348,6 +348,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
   	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
   	if (!enabled && !err) {
+		trace_intel_context_sched_enable(ce);
   		atomic_inc(&guc->outstanding_submission_g2h);
   		set_context_enabled(ce);
   	} else if (!enabled) {
@@ -812,6 +813,8 @@ static int register_context(struct intel_context *ce)
   	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
   		ce->guc_id * sizeof(struct guc_lrc_desc);
+	trace_intel_context_register(ce);
+
   	return __guc_action_register_context(guc, ce->guc_id, offset);
   }
@@ -831,6 +834,8 @@ static int deregister_context(struct intel_context *ce, u32 guc_id)
   {
   	struct intel_guc *guc = ce_to_guc(ce);
+	trace_intel_context_deregister(ce);
+
   	return __guc_action_deregister_context(guc, guc_id);
   }
@@ -905,6 +910,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
   	 * GuC before registering this context.
   	 */
   	if (context_registered) {
+		trace_intel_context_steal_guc_id(ce);
   		set_context_wait_for_deregister_to_register(ce);
   		intel_context_get(ce);
@@ -963,6 +969,7 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
   	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
+	trace_intel_context_sched_disable(ce);
   	intel_context_get(ce);
   	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
@@ -1119,6 +1126,9 @@ static void __guc_signal_context_fence(struct intel_context *ce)
   	lockdep_assert_held(&ce->guc_state.lock);
+	if (!list_empty(&ce->guc_state.fences))
+		trace_intel_context_fence_release(ce);
+
   	list_for_each_entry(rq, &ce->guc_state.fences, guc_fence_link)
   		i915_sw_fence_complete(&rq->submit);
@@ -1529,6 +1539,8 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
   	if (unlikely(!ce))
   		return -EPROTO;
+	trace_intel_context_deregister_done(ce);
+
   	if (context_wait_for_deregister_to_register(ce)) {
   		struct intel_runtime_pm *runtime_pm =
   			&ce->engine->gt->i915->runtime_pm;
@@ -1580,6 +1592,8 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
   		return -EPROTO;
   	}
+	trace_intel_context_sched_done(ce);
+
   	if (context_pending_enable(ce)) {
   		clr_context_pending_enable(ce);
   	} else if (context_pending_disable(ce)) {
drivers/gpu/drm/i915/i915_trace.h +0 -0
index b02d04b6c8f6..97c2e83984ed 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_trace.h DECLARE_EVENT_CLASS(i915_request, · line 821

Quoted 7 lines
drivers/gpu/drm/i915/i915_trace.h +2 -2
@@ -818,8 +818,8 @@ DECLARE_EVENT_CLASS(i915_request,
   );
   DEFINE_EVENT(i915_request, i915_request_add,
-	    TP_PROTO(struct i915_request *rq),
-	    TP_ARGS(rq)
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)

Is this an intentional white space change?

Quoted 4 lines

Yea, probably should be in the previous patch though. Before this change the arguments were misaligned.

Matt

Okay, one can never tell if the alignment is out for reals or just because the email viewer and/or diff prefixes are playing silly buggers with tab spacing. And yeah, would make more sense to bump the change into the request trace point patch. With that done...

Quoted 151 lines
Quoted 151 lines
Quoted 151 lines
drivers/gpu/drm/i915/i915_trace.h +138 -0
   );
   #if defined(CONFIG_DRM_I915_LOW_LEVEL_TRACEPOINTS)
@@ -905,6 +905,90 @@ TRACE_EVENT(i915_request_out,
   			      __entry->ctx, __entry->seqno, __entry->completed)
   );
+DECLARE_EVENT_CLASS(intel_context,
+	    TP_PROTO(struct intel_context *ce),
+	    TP_ARGS(ce),
+
+	    TP_STRUCT__entry(
+			     __field(u32, guc_id)
+			     __field(int, pin_count)
+			     __field(u32, sched_state)
+			     __field(u32, guc_sched_state_no_lock)
+			     ),
+
+	    TP_fast_assign(
+			   __entry->guc_id = ce->guc_id;
+			   __entry->pin_count = atomic_read(&ce->pin_count);
+			   __entry->sched_state = ce->guc_state.sched_state;
+			   __entry->guc_sched_state_no_lock =
+			   atomic_read(&ce->guc_sched_state_no_lock);
+			   ),
+
+	    TP_printk("guc_id=%d, pin_count=%d sched_state=0x%x,0x%x",
+		      __entry->guc_id, __entry->pin_count, __entry->sched_state,
+		      __entry->guc_sched_state_no_lock)
+);
+
+DEFINE_EVENT(intel_context, intel_context_register,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_deregister,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_deregister_done,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_enable,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_disable,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_sched_done,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_create,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_fence_release,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_free,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_steal_guc_id,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_do_pin,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
+DEFINE_EVENT(intel_context, intel_context_do_unpin,
+	     TP_PROTO(struct intel_context *ce),
+	     TP_ARGS(ce)
+);
+
   #else
   #if !defined(TRACE_HEADER_MULTI_READ)
   static inline void
@@ -941,6 +1025,66 @@ static inline void
   trace_i915_request_out(struct i915_request *rq)
   {
   }
+
+static inline void
+trace_intel_context_register(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_deregister(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_deregister_done(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_enable(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_disable(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_sched_done(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_create(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_fence_release(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_free(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_steal_guc_id(struct intel_context *ce)
+{
+}
+
+static inline void
+trace_intel_context_do_pin(struct intel_context *ce)
+{
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/i915/i915_trace.h static inline void · line 1089

Quoted 8 lines
drivers/gpu/drm/i915/i915_trace.h +6 -0
+}
+
+static inline void
+trace_intel_context_do_unpin(struct intel_context *ce)
+{
+}
   #endif
   #endif

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#85 lore raw

PATCH 26/47 drm/i915/guc: GuC virtual engines

In reply to Matthew Brost

Implement GuC virtual engines. Rather simple implementation, basically just allocate an engine, setup context enter / exit function to virtual engine specific functions, set all other variables / functions to guc versions, and set the engine mask to that of all the siblings.

Cc Daniele Ceraolo Spurio <[email protected]>
---
 drivers/gpu/drm/i915/gem/i915_gem_context.c   |  19 +-
 drivers/gpu/drm/i915/gem/i915_gem_context.h   |   1 +
 drivers/gpu/drm/i915/gt/intel_context_types.h |  10 +
 drivers/gpu/drm/i915/gt/intel_engine.h        |  45 +++-
 drivers/gpu/drm/i915/gt/intel_engine_cs.c     |  14 +
 .../drm/i915/gt/intel_execlists_submission.c  | 186 +++++++------
 .../drm/i915/gt/intel_execlists_submission.h  |  11 -
 drivers/gpu/drm/i915/gt/selftest_execlists.c  |  20 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 253 +++++++++++++++++-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.h |   2 +
 10 files changed, 429 insertions(+), 132 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +5 -14
index 5c07e6abf16a..8a9293e0ca92 100644
@@ -72,7 +72,6 @@
 #include "gt/intel_context_param.h"
 #include "gt/intel_engine_heartbeat.h"
 #include "gt/intel_engine_user.h"
-#include "gt/intel_execlists_submission.h" /* virtual_engine */
 #include "gt/intel_gpu_commands.h"
 #include "gt/intel_ring.h"
 
@@ -1568,9 +1567,6 @@ set_engines__load_balance(struct i915_user_extension __user *base, void *data)
 	if (!HAS_EXECLISTS(i915))
 		return -ENODEV;
 
-	if (intel_uc_uses_guc_submission(&i915->gt.uc))
-		return -ENODEV; /* not implement yet */
-
 	if (get_user(idx, &ext->engine_index))
 		return -EFAULT;
 
@@ -1627,7 +1623,7 @@ set_engines__load_balance(struct i915_user_extension __user *base, void *data)
 		}
 	}
 
-	ce = intel_execlists_create_virtual(siblings, n);
+	ce = intel_engine_create_virtual(siblings, n);
 	if (IS_ERR(ce)) {
 		err = PTR_ERR(ce);
 		goto out_siblings;
@@ -1723,13 +1719,9 @@ set_engines__bond(struct i915_user_extension __user *base, void *data)
 		 * A non-virtual engine has no siblings to choose between; and
 		 * a submit fence will always be directed to the one engine.
 		 */
-		if (intel_engine_is_virtual(virtual)) {
-			err = intel_virtual_engine_attach_bond(virtual,
-							       master,
-							       bond);
-			if (err)
-				return err;
-		}
+		err = intel_engine_attach_bond(virtual, master, bond);
+		if (err)
+			return err;
 	}
 
 	return 0;
@@ -2116,8 +2108,7 @@ static int clone_engines(struct i915_gem_context *dst,
 		 * the virtual engine instead.
 		 */
 		if (intel_engine_is_virtual(engine))
-			clone->engines[n] =
-				intel_execlists_clone_virtual(engine);
+			clone->engines[n] = intel_engine_clone_virtual(engine);
 		else
 			clone->engines[n] = intel_context_create(engine);
 		if (IS_ERR_OR_NULL(clone->engines[n])) {
drivers/gpu/drm/i915/gem/i915_gem_context.h +1 -0
index b5c908f3f4f2..ba772762f7b9 100644
@@ -10,6 +10,7 @@
 #include "i915_gem_context_types.h"
 
 #include "gt/intel_context.h"
+#include "gt/intel_engine.h"
 
 #include "i915_drv.h"
 #include "i915_gem.h"
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -0
index e7af6a2368f8..6945963a31ba 100644
@@ -47,6 +47,16 @@ struct intel_context_ops {
 
 	void (*reset)(struct intel_context *ce);
 	void (*destroy)(struct kref *kref);
+
+	/* virtual engine/context interface */
+	struct intel_context *(*create_virtual)(struct intel_engine_cs **engine,
+						unsigned int count);
+	struct intel_context *(*clone_virtual)(struct intel_engine_cs *engine);
+	struct intel_engine_cs *(*get_sibling)(struct intel_engine_cs *engine,
+					       unsigned int sibling);
+	int (*attach_bond)(struct intel_engine_cs *engine,
+			   const struct intel_engine_cs *master,
+			   const struct intel_engine_cs *sibling);
 };
 
 struct intel_context {
drivers/gpu/drm/i915/gt/intel_engine.h +44 -1

57 lines not on this page · raw message

drivers/gpu/drm/i915/gt/intel_engine_cs.c +14 -0
index 88694822716a..d13b1716c29e 100644
@@ -1736,6 +1736,20 @@ ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine, ktime_t *now)
 	return total;
 }
 
+struct intel_context *
+intel_engine_create_virtual(struct intel_engine_cs **siblings,
+			    unsigned int count)
+{
+	if (count == 0)
+		return ERR_PTR(-EINVAL);
+
+	if (count == 1)
+		return intel_context_create(siblings[0]);
+
+	GEM_BUG_ON(!siblings[0]->cops->create_virtual);
+	return siblings[0]->cops->create_virtual(siblings, count);
+}
+
 static bool match_ring(struct i915_request *rq)
 {
 	u32 ring = ENGINE_READ(rq->engine, RING_START);
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +99 -87

233 lines not on this page · raw message

drivers/gpu/drm/i915/gt/intel_execlists_submission.h +0 -11
index 4ca9b475e252..74041b1994af 100644
@@ -32,15 +32,4 @@ void intel_execlists_show_requests(struct intel_engine_cs *engine,
 							int indent),
 				   unsigned int max);
 
-struct intel_context *
-intel_execlists_create_virtual(struct intel_engine_cs **siblings,
-			       unsigned int count);
-
-struct intel_context *
-intel_execlists_clone_virtual(struct intel_engine_cs *src);
-
-int intel_virtual_engine_attach_bond(struct intel_engine_cs *engine,
-				     const struct intel_engine_cs *master,
-				     const struct intel_engine_cs *sibling);
-
 #endif /* __INTEL_EXECLISTS_SUBMISSION_H__ */
drivers/gpu/drm/i915/gt/selftest_execlists.c +10 -10
index 08896ae027d5..88aac9977e09 100644
@@ -3727,7 +3727,7 @@ static int nop_virtual_engine(struct intel_gt *gt,
 	GEM_BUG_ON(!nctx || nctx > ARRAY_SIZE(ve));
 
 	for (n = 0; n < nctx; n++) {
-		ve[n] = intel_execlists_create_virtual(siblings, nsibling);
+		ve[n] = intel_engine_create_virtual(siblings, nsibling);
 		if (IS_ERR(ve[n])) {
 			err = PTR_ERR(ve[n]);
 			nctx = n;
@@ -3923,7 +3923,7 @@ static int mask_virtual_engine(struct intel_gt *gt,
 	 * restrict it to our desired engine within the virtual engine.
 	 */
 
-	ve = intel_execlists_create_virtual(siblings, nsibling);
+	ve = intel_engine_create_virtual(siblings, nsibling);
 	if (IS_ERR(ve)) {
 		err = PTR_ERR(ve);
 		goto out_close;
@@ -4054,7 +4054,7 @@ static int slicein_virtual_engine(struct intel_gt *gt,
 		i915_request_add(rq);
 	}
 
-	ce = intel_execlists_create_virtual(siblings, nsibling);
+	ce = intel_engine_create_virtual(siblings, nsibling);
 	if (IS_ERR(ce)) {
 		err = PTR_ERR(ce);
 		goto out;
@@ -4106,7 +4106,7 @@ static int sliceout_virtual_engine(struct intel_gt *gt,
 
 	/* XXX We do not handle oversubscription and fairness with normal rq */
 	for (n = 0; n < nsibling; n++) {
-		ce = intel_execlists_create_virtual(siblings, nsibling);
+		ce = intel_engine_create_virtual(siblings, nsibling);
 		if (IS_ERR(ce)) {
 			err = PTR_ERR(ce);
 			goto out;
@@ -4208,7 +4208,7 @@ static int preserved_virtual_engine(struct intel_gt *gt,
 	if (err)
 		goto out_scratch;
 
-	ve = intel_execlists_create_virtual(siblings, nsibling);
+	ve = intel_engine_create_virtual(siblings, nsibling);
 	if (IS_ERR(ve)) {
 		err = PTR_ERR(ve);
 		goto out_scratch;
@@ -4431,16 +4431,16 @@ static int bond_virtual_engine(struct intel_gt *gt,
 		for (n = 0; n < nsibling; n++) {
 			struct intel_context *ve;
 
-			ve = intel_execlists_create_virtual(siblings, nsibling);
+			ve = intel_engine_create_virtual(siblings, nsibling);
 			if (IS_ERR(ve)) {
 				err = PTR_ERR(ve);
 				onstack_fence_fini(&fence);
 				goto out;
 			}
 
-			err = intel_virtual_engine_attach_bond(ve->engine,
-							       master,
-							       siblings[n]);
+			err = intel_engine_attach_bond(ve->engine,
+						       master,
+						       siblings[n]);
 			if (err) {
 				intel_context_put(ve);
 				onstack_fence_fini(&fence);
@@ -4576,7 +4576,7 @@ static int reset_virtual_engine(struct intel_gt *gt,
 	if (igt_spinner_init(&spin, gt))
 		return -ENOMEM;
 
-	ve = intel_execlists_create_virtual(siblings, nsibling);
+	ve = intel_engine_create_virtual(siblings, nsibling);
 	if (IS_ERR(ve)) {
 		err = PTR_ERR(ve);
 		goto out_spin;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +244 -9

312 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +2 -0
index 6453e2bfa151..95df5ab06031 100644
@@ -25,6 +25,8 @@ void intel_guc_log_submission_info(struct intel_guc *guc,
 				   struct drm_printer *p);
 void intel_guc_log_context_info(struct intel_guc *guc, struct drm_printer *p);
 
+bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve);
+
 static inline bool intel_guc_submission_is_supported(struct intel_guc *guc)
 {
 	/* XXX: GuC submission is unavailable for now */
Signature · 6 lines
2.28.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#86 lore raw

Re: [PATCH 26/47] drm/i915/guc: GuC virtual engines

Quoted 98 lines · Matthew Brost

Implement GuC virtual engines. Rather simple implementation, basically just allocate an engine, setup context enter / exit function to virtual engine specific functions, set all other variables / functions to guc versions, and set the engine mask to that of all the siblings.

Cc Daniele Ceraolo Spurio <[email protected]>
---
  drivers/gpu/drm/i915/gem/i915_gem_context.c   |  19 +-
  drivers/gpu/drm/i915/gem/i915_gem_context.h   |   1 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  10 +
  drivers/gpu/drm/i915/gt/intel_engine.h        |  45 +++-
  drivers/gpu/drm/i915/gt/intel_engine_cs.c     |  14 +
  .../drm/i915/gt/intel_execlists_submission.c  | 186 +++++++------
  .../drm/i915/gt/intel_execlists_submission.h  |  11 -
  drivers/gpu/drm/i915/gt/selftest_execlists.c  |  20 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 253 +++++++++++++++++-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.h |   2 +
  10 files changed, 429 insertions(+), 132 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +5 -14
index 5c07e6abf16a..8a9293e0ca92 100644
@@ -72,7 +72,6 @@
  #include "gt/intel_context_param.h"
  #include "gt/intel_engine_heartbeat.h"
  #include "gt/intel_engine_user.h"
-#include "gt/intel_execlists_submission.h" /* virtual_engine */
  #include "gt/intel_gpu_commands.h"
  #include "gt/intel_ring.h"
 
@@ -1568,9 +1567,6 @@ set_engines__load_balance(struct i915_user_extension __user *base, void *data)
  	if (!HAS_EXECLISTS(i915))
  		return -ENODEV;
 
-	if (intel_uc_uses_guc_submission(&i915->gt.uc))
-		return -ENODEV; /* not implement yet */
-
  	if (get_user(idx, &ext->engine_index))
  		return -EFAULT;
 
@@ -1627,7 +1623,7 @@ set_engines__load_balance(struct i915_user_extension __user *base, void *data)
  		}
  	}
 
-	ce = intel_execlists_create_virtual(siblings, n);
+	ce = intel_engine_create_virtual(siblings, n);
  	if (IS_ERR(ce)) {
  		err = PTR_ERR(ce);
  		goto out_siblings;
@@ -1723,13 +1719,9 @@ set_engines__bond(struct i915_user_extension __user *base, void *data)
  		 * A non-virtual engine has no siblings to choose between; and
  		 * a submit fence will always be directed to the one engine.
  		 */
-		if (intel_engine_is_virtual(virtual)) {
-			err = intel_virtual_engine_attach_bond(virtual,
-							       master,
-							       bond);
-			if (err)
-				return err;
-		}
+		err = intel_engine_attach_bond(virtual, master, bond);
+		if (err)
+			return err;
  	}
 
  	return 0;
@@ -2116,8 +2108,7 @@ static int clone_engines(struct i915_gem_context *dst,
  		 * the virtual engine instead.
  		 */
  		if (intel_engine_is_virtual(engine))
-			clone->engines[n] =
-				intel_execlists_clone_virtual(engine);
+			clone->engines[n] = intel_engine_clone_virtual(engine);
  		else
  			clone->engines[n] = intel_context_create(engine);
  		if (IS_ERR_OR_NULL(clone->engines[n])) {
drivers/gpu/drm/i915/gem/i915_gem_context.h +1 -0
index b5c908f3f4f2..ba772762f7b9 100644
@@ -10,6 +10,7 @@
  #include "i915_gem_context_types.h"
 
  #include "gt/intel_context.h"
+#include "gt/intel_engine.h"
 
  #include "i915_drv.h"
  #include "i915_gem.h"
drivers/gpu/drm/i915/gt/intel_context_types.h +0 -0
index e7af6a2368f8..6945963a31ba 100644
@@ -47,6 +47,16 @@ struct intel_context_ops {
 
  	void (*reset)(struct intel_context *ce);
  	void (*destroy)(struct kref *kref);

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context_ops { · line 59

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -0
+
+	/* virtual engine/context interface */
+	struct intel_context *(*create_virtual)(struct intel_engine_cs **engine,
+						unsigned int count);
+	struct intel_context *(*clone_virtual)(struct intel_engine_cs *engine);
+	struct intel_engine_cs *(*get_sibling)(struct intel_engine_cs *engine,
+					       unsigned int sibling);
+	int (*attach_bond)(struct intel_engine_cs *engine,
+			   const struct intel_engine_cs *master,
+			   const struct intel_engine_cs *sibling);

Cloning and bonding for VE have been removed, so can be dropped. I'll skip reviewing all the related code in this patch.

Quoted 15 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +0 -0
  };
 
  struct intel_context {
drivers/gpu/drm/i915/gt/intel_engine.h +4 -0
index f911c1224ab2..923eaee627b3 100644
@@ -273,13 +273,56 @@ intel_engine_has_preempt_reset(const struct intel_engine_cs *engine)
  	return intel_engine_has_preemption(engine);
  }
 
+struct intel_context *
+intel_engine_create_virtual(struct intel_engine_cs **siblings,
+			    unsigned int count);
+

drivers/gpu/drm/i915/gt/intel_engine.h intel_engine_has_preempt_reset(const struct intel_engine_cs *engine) · line 289

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_engine.h +10 -0
+static inline bool
+intel_virtual_engine_has_heartbeat(const struct intel_engine_cs *engine)
+{
+	if (intel_engine_uses_guc(engine))
+		return intel_guc_virtual_engine_has_heartbeat(engine);
+	else
+		GEM_BUG_ON("Only should be called in GuC submission");
+
+	return false;
+}

This could use a better explanation. Maybe something like:

static inline bool
intel_virtual_engine_has_heartbeat(const struct intel_engine_cs *engine) {
/*
* For non-GuC submission we expect the back-end to look at the
* heartbeat status of the actual physical engine that the work
* has been (or is being) scheduled on, so we should only reach
* here with GuC submission enabled.
  */

	GEM_BUG_ON(!intel_engine_uses_guc(engine));

	return intel_guc_virtual_engine_has_heartbeat(engine);
}
Quoted 493 lines
drivers/gpu/drm/i915/gt/intel_engine.h +30 -1
+
  static inline bool
  intel_engine_has_heartbeat(const struct intel_engine_cs *engine)
  {
  	if (!IS_ACTIVE(CONFIG_DRM_I915_HEARTBEAT_INTERVAL))
  		return false;
 
-	return READ_ONCE(engine->props.heartbeat_interval_ms);
+	if (intel_engine_is_virtual(engine))
+		return intel_virtual_engine_has_heartbeat(engine);
+	else
+		return READ_ONCE(engine->props.heartbeat_interval_ms);
+}
+
+static inline struct intel_context *
+intel_engine_clone_virtual(struct intel_engine_cs *src)
+{
+	GEM_BUG_ON(!intel_engine_is_virtual(src));
+	return src->cops->clone_virtual(src);
+}
+
+static inline int
+intel_engine_attach_bond(struct intel_engine_cs *engine,
+			 const struct intel_engine_cs *master,
+			 const struct intel_engine_cs *sibling)
+{
+	if (!engine->cops->attach_bond)
+		return 0;
+
+	return engine->cops->attach_bond(engine, master, sibling);
+}
+
+static inline struct intel_engine_cs *
+intel_engine_get_sibling(struct intel_engine_cs *engine, unsigned int sibling)
+{
+	GEM_BUG_ON(!intel_engine_is_virtual(engine));
+	return engine->cops->get_sibling(engine, sibling);
  }
 
  #endif /* _INTEL_RINGBUFFER_H_ */
drivers/gpu/drm/i915/gt/intel_engine_cs.c +14 -0
index 88694822716a..d13b1716c29e 100644
@@ -1736,6 +1736,20 @@ ktime_t intel_engine_get_busy_time(struct intel_engine_cs *engine, ktime_t *now)
  	return total;
  }
 
+struct intel_context *
+intel_engine_create_virtual(struct intel_engine_cs **siblings,
+			    unsigned int count)
+{
+	if (count == 0)
+		return ERR_PTR(-EINVAL);
+
+	if (count == 1)
+		return intel_context_create(siblings[0]);
+
+	GEM_BUG_ON(!siblings[0]->cops->create_virtual);
+	return siblings[0]->cops->create_virtual(siblings, count);
+}
+
  static bool match_ring(struct i915_request *rq)
  {
  	u32 ring = ENGINE_READ(rq->engine, RING_START);
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +99 -87
index cdb2126a159a..bd4ced794ff9 100644
@@ -205,6 +205,9 @@ static struct virtual_engine *to_virtual_engine(struct intel_engine_cs *engine)
  	return container_of(engine, struct virtual_engine, base);
  }
 
+static struct intel_context *
+execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count);
+
  static struct i915_request *
  __active_request(const struct intel_timeline * const tl,
  		 struct i915_request *rq,
@@ -2560,6 +2563,8 @@ static const struct intel_context_ops execlists_context_ops = {
 
  	.reset = lrc_reset,
  	.destroy = lrc_destroy,
+
+	.create_virtual = execlists_create_virtual,
  };
 
  static int emit_pdps(struct i915_request *rq)
@@ -3506,6 +3511,94 @@ static void virtual_context_exit(struct intel_context *ce)
  		intel_engine_pm_put(ve->siblings[n]);
  }
 
+static struct intel_engine_cs *
+virtual_get_sibling(struct intel_engine_cs *engine, unsigned int sibling)
+{
+	struct virtual_engine *ve = to_virtual_engine(engine);
+
+	if (sibling >= ve->num_siblings)
+		return NULL;
+
+	return ve->siblings[sibling];
+}
+
+static struct intel_context *
+virtual_clone(struct intel_engine_cs *src)
+{
+	struct virtual_engine *se = to_virtual_engine(src);
+	struct intel_context *dst;
+
+	dst = execlists_create_virtual(se->siblings, se->num_siblings);
+	if (IS_ERR(dst))
+		return dst;
+
+	if (se->num_bonds) {
+		struct virtual_engine *de = to_virtual_engine(dst->engine);
+
+		de->bonds = kmemdup(se->bonds,
+				    sizeof(*se->bonds) * se->num_bonds,
+				    GFP_KERNEL);
+		if (!de->bonds) {
+			intel_context_put(dst);
+			return ERR_PTR(-ENOMEM);
+		}
+
+		de->num_bonds = se->num_bonds;
+	}
+
+	return dst;
+}
+
+static struct ve_bond *
+virtual_find_bond(struct virtual_engine *ve,
+		  const struct intel_engine_cs *master)
+{
+	int i;
+
+	for (i = 0; i < ve->num_bonds; i++) {
+		if (ve->bonds[i].master == master)
+			return &ve->bonds[i];
+	}
+
+	return NULL;
+}
+
+static int virtual_attach_bond(struct intel_engine_cs *engine,
+			       const struct intel_engine_cs *master,
+			       const struct intel_engine_cs *sibling)
+{
+	struct virtual_engine *ve = to_virtual_engine(engine);
+	struct ve_bond *bond;
+	int n;
+
+	/* Sanity check the sibling is part of the virtual engine */
+	for (n = 0; n < ve->num_siblings; n++)
+		if (sibling == ve->siblings[n])
+			break;
+	if (n == ve->num_siblings)
+		return -EINVAL;
+
+	bond = virtual_find_bond(ve, master);
+	if (bond) {
+		bond->sibling_mask |= sibling->mask;
+		return 0;
+	}
+
+	bond = krealloc(ve->bonds,
+			sizeof(*bond) * (ve->num_bonds + 1),
+			GFP_KERNEL);
+	if (!bond)
+		return -ENOMEM;
+
+	bond[ve->num_bonds].master = master;
+	bond[ve->num_bonds].sibling_mask = sibling->mask;
+
+	ve->bonds = bond;
+	ve->num_bonds++;
+
+	return 0;
+}
+
  static const struct intel_context_ops virtual_context_ops = {
  	.flags = COPS_HAS_INFLIGHT,
 
@@ -3520,6 +3613,10 @@ static const struct intel_context_ops virtual_context_ops = {
  	.exit = virtual_context_exit,
 
  	.destroy = virtual_context_destroy,
+
+	.clone_virtual = virtual_clone,
+	.get_sibling = virtual_get_sibling,
+	.attach_bond = virtual_attach_bond,
  };
 
  static intel_engine_mask_t virtual_submission_mask(struct virtual_engine *ve)
@@ -3668,20 +3765,6 @@ static void virtual_submit_request(struct i915_request *rq)
  	spin_unlock_irqrestore(&ve->base.sched_engine->lock, flags);
  }
 
-static struct ve_bond *
-virtual_find_bond(struct virtual_engine *ve,
-		  const struct intel_engine_cs *master)
-{
-	int i;
-
-	for (i = 0; i < ve->num_bonds; i++) {
-		if (ve->bonds[i].master == master)
-			return &ve->bonds[i];
-	}
-
-	return NULL;
-}
-
  static void
  virtual_bond_execute(struct i915_request *rq, struct dma_fence *signal)
  {
@@ -3704,20 +3787,13 @@ virtual_bond_execute(struct i915_request *rq, struct dma_fence *signal)
  	to_request(signal)->execution_mask &= ~allowed;
  }
 
-struct intel_context *
-intel_execlists_create_virtual(struct intel_engine_cs **siblings,
-			       unsigned int count)
+static struct intel_context *
+execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  {
  	struct virtual_engine *ve;
  	unsigned int n;
  	int err;
 
-	if (count == 0)
-		return ERR_PTR(-EINVAL);
-
-	if (count == 1)
-		return intel_context_create(siblings[0]);
-
  	ve = kzalloc(struct_size(ve, siblings, count), GFP_KERNEL);
  	if (!ve)
  		return ERR_PTR(-ENOMEM);
@@ -3850,70 +3926,6 @@ intel_execlists_create_virtual(struct intel_engine_cs **siblings,
  	return ERR_PTR(err);
  }
 
-struct intel_context *
-intel_execlists_clone_virtual(struct intel_engine_cs *src)
-{
-	struct virtual_engine *se = to_virtual_engine(src);
-	struct intel_context *dst;
-
-	dst = intel_execlists_create_virtual(se->siblings,
-					     se->num_siblings);
-	if (IS_ERR(dst))
-		return dst;
-
-	if (se->num_bonds) {
-		struct virtual_engine *de = to_virtual_engine(dst->engine);
-
-		de->bonds = kmemdup(se->bonds,
-				    sizeof(*se->bonds) * se->num_bonds,
-				    GFP_KERNEL);
-		if (!de->bonds) {
-			intel_context_put(dst);
-			return ERR_PTR(-ENOMEM);
-		}
-
-		de->num_bonds = se->num_bonds;
-	}
-
-	return dst;
-}
-
-int intel_virtual_engine_attach_bond(struct intel_engine_cs *engine,
-				     const struct intel_engine_cs *master,
-				     const struct intel_engine_cs *sibling)
-{
-	struct virtual_engine *ve = to_virtual_engine(engine);
-	struct ve_bond *bond;
-	int n;
-
-	/* Sanity check the sibling is part of the virtual engine */
-	for (n = 0; n < ve->num_siblings; n++)
-		if (sibling == ve->siblings[n])
-			break;
-	if (n == ve->num_siblings)
-		return -EINVAL;
-
-	bond = virtual_find_bond(ve, master);
-	if (bond) {
-		bond->sibling_mask |= sibling->mask;
-		return 0;
-	}
-
-	bond = krealloc(ve->bonds,
-			sizeof(*bond) * (ve->num_bonds + 1),
-			GFP_KERNEL);
-	if (!bond)
-		return -ENOMEM;
-
-	bond[ve->num_bonds].master = master;
-	bond[ve->num_bonds].sibling_mask = sibling->mask;
-
-	ve->bonds = bond;
-	ve->num_bonds++;
-
-	return 0;
-}
-
  void intel_execlists_show_requests(struct intel_engine_cs *engine,
  				   struct drm_printer *m,
  				   void (*show_request)(struct drm_printer *m,
drivers/gpu/drm/i915/gt/intel_execlists_submission.h +0 -11
index 4ca9b475e252..74041b1994af 100644
@@ -32,15 +32,4 @@ void intel_execlists_show_requests(struct intel_engine_cs *engine,
  							int indent),
  				   unsigned int max);
 
-struct intel_context *
-intel_execlists_create_virtual(struct intel_engine_cs **siblings,
-			       unsigned int count);
-
-struct intel_context *
-intel_execlists_clone_virtual(struct intel_engine_cs *src);
-
-int intel_virtual_engine_attach_bond(struct intel_engine_cs *engine,
-				     const struct intel_engine_cs *master,
-				     const struct intel_engine_cs *sibling);
-
  #endif /* __INTEL_EXECLISTS_SUBMISSION_H__ */
drivers/gpu/drm/i915/gt/selftest_execlists.c +10 -10
index 08896ae027d5..88aac9977e09 100644
@@ -3727,7 +3727,7 @@ static int nop_virtual_engine(struct intel_gt *gt,
  	GEM_BUG_ON(!nctx || nctx > ARRAY_SIZE(ve));
 
  	for (n = 0; n < nctx; n++) {
-		ve[n] = intel_execlists_create_virtual(siblings, nsibling);
+		ve[n] = intel_engine_create_virtual(siblings, nsibling);
  		if (IS_ERR(ve[n])) {
  			err = PTR_ERR(ve[n]);
  			nctx = n;
@@ -3923,7 +3923,7 @@ static int mask_virtual_engine(struct intel_gt *gt,
  	 * restrict it to our desired engine within the virtual engine.
  	 */
 
-	ve = intel_execlists_create_virtual(siblings, nsibling);
+	ve = intel_engine_create_virtual(siblings, nsibling);
  	if (IS_ERR(ve)) {
  		err = PTR_ERR(ve);
  		goto out_close;
@@ -4054,7 +4054,7 @@ static int slicein_virtual_engine(struct intel_gt *gt,
  		i915_request_add(rq);
  	}
 
-	ce = intel_execlists_create_virtual(siblings, nsibling);
+	ce = intel_engine_create_virtual(siblings, nsibling);
  	if (IS_ERR(ce)) {
  		err = PTR_ERR(ce);
  		goto out;
@@ -4106,7 +4106,7 @@ static int sliceout_virtual_engine(struct intel_gt *gt,
 
  	/* XXX We do not handle oversubscription and fairness with normal rq */
  	for (n = 0; n < nsibling; n++) {
-		ce = intel_execlists_create_virtual(siblings, nsibling);
+		ce = intel_engine_create_virtual(siblings, nsibling);
  		if (IS_ERR(ce)) {
  			err = PTR_ERR(ce);
  			goto out;
@@ -4208,7 +4208,7 @@ static int preserved_virtual_engine(struct intel_gt *gt,
  	if (err)
  		goto out_scratch;
 
-	ve = intel_execlists_create_virtual(siblings, nsibling);
+	ve = intel_engine_create_virtual(siblings, nsibling);
  	if (IS_ERR(ve)) {
  		err = PTR_ERR(ve);
  		goto out_scratch;
@@ -4431,16 +4431,16 @@ static int bond_virtual_engine(struct intel_gt *gt,
  		for (n = 0; n < nsibling; n++) {
  			struct intel_context *ve;
 
-			ve = intel_execlists_create_virtual(siblings, nsibling);
+			ve = intel_engine_create_virtual(siblings, nsibling);
  			if (IS_ERR(ve)) {
  				err = PTR_ERR(ve);
  				onstack_fence_fini(&fence);
  				goto out;
  			}
 
-			err = intel_virtual_engine_attach_bond(ve->engine,
-							       master,
-							       siblings[n]);
+			err = intel_engine_attach_bond(ve->engine,
+						       master,
+						       siblings[n]);
  			if (err) {
  				intel_context_put(ve);
  				onstack_fence_fini(&fence);
@@ -4576,7 +4576,7 @@ static int reset_virtual_engine(struct intel_gt *gt,
  	if (igt_spinner_init(&spin, gt))
  		return -ENOMEM;
 
-	ve = intel_execlists_create_virtual(siblings, nsibling);
+	ve = intel_engine_create_virtual(siblings, nsibling);
  	if (IS_ERR(ve)) {
  		err = PTR_ERR(ve);
  		goto out_spin;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +45 -6
index d605af0d66e6..ccbcf024b31b 100644
@@ -60,6 +60,15 @@
   *
   */
 
+/* GuC Virtual Engine */
+struct guc_virtual_engine {
+	struct intel_engine_cs base;
+	struct intel_context context;
+};
+
+static struct intel_context *
+guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count);
+
  #define GUC_REQUEST_SIZE 64 /* bytes */
 
  /*
@@ -928,20 +937,35 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
  	return ret;
  }
 
-static int guc_context_pre_pin(struct intel_context *ce,
-			       struct i915_gem_ww_ctx *ww,
-			       void **vaddr)
+static int __guc_context_pre_pin(struct intel_context *ce,
+				 struct intel_engine_cs *engine,
+				 struct i915_gem_ww_ctx *ww,
+				 void **vaddr)
  {
-	return lrc_pre_pin(ce, ce->engine, ww, vaddr);
+	return lrc_pre_pin(ce, engine, ww, vaddr);
  }
 
-static int guc_context_pin(struct intel_context *ce, void *vaddr)
+static int __guc_context_pin(struct intel_context *ce,
+			     struct intel_engine_cs *engine,
+			     void *vaddr)
  {
  	if (i915_ggtt_offset(ce->state) !=
  	    (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
  		set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
 
-	return lrc_pin(ce, ce->engine, vaddr);
+	return lrc_pin(ce, engine, vaddr);
+}
+
+static int guc_context_pre_pin(struct intel_context *ce,
+			       struct i915_gem_ww_ctx *ww,
+			       void **vaddr)
+{
+	return __guc_context_pre_pin(ce, ce->engine, ww, vaddr);
+}
+
+static int guc_context_pin(struct intel_context *ce, void *vaddr)
+{
+	return __guc_context_pin(ce, ce->engine, vaddr);
  }
 
  static void guc_context_unpin(struct intel_context *ce)
@@ -1041,6 +1065,21 @@ static inline void guc_lrc_desc_unpin(struct intel_context *ce)
  	deregister_context(ce, ce->guc_id);
  }
 
+static void __guc_context_destroy(struct intel_context *ce)
+{
+	lrc_fini(ce);
+	intel_context_fini(ce);
+
+	if (intel_engine_is_virtual(ce->engine)) {
+		struct guc_virtual_engine *ve =
+			container_of(ce, typeof(*ve), context);
+
+		kfree(ve);
+	} else {
+		intel_context_free(ce);
+	}
+}
+
  static void guc_context_destroy(struct kref *kref)
  {
  	struct intel_context *ce = container_of(kref, typeof(*ce), ref);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_context_destroy(struct kref *kref) · line 1060

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -1
@@ -1057,7 +1096,7 @@ static void guc_context_destroy(struct kref *kref)
  	if (context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		release_guc_id(guc, ce);
-		lrc_destroy(kref);

AFAICS after this patch we only have 1 use of lrc_destroy inside the execlists file, while we do have 2 open coded implementations (here and execlists VE). Since lrc_fini and intel_context_fini are still always called as a pair, maybe we can replace lrc_destroy with a function that calls those 2 (i.e basically just remove the free() from lrc_destroy)? Can be done as a follow up.

Quoted 146 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +109 -2
+		__guc_context_destroy(ce);
  		return;
  	}
 
@@ -1073,7 +1112,7 @@ static void guc_context_destroy(struct kref *kref)
  	if (context_guc_id_invalid(ce)) {
  		__release_guc_id(guc, ce);
  		spin_unlock_irqrestore(&guc->contexts_lock, flags);
-		lrc_destroy(kref);
+		__guc_context_destroy(ce);
  		return;
  	}
 
@@ -1118,6 +1157,8 @@ static const struct intel_context_ops guc_context_ops = {
 
  	.reset = lrc_reset,
  	.destroy = guc_context_destroy,
+
+	.create_virtual = guc_create_virtual,
  };
 
  static void __guc_signal_context_fence(struct intel_context *ce)
@@ -1246,6 +1287,96 @@ static int guc_request_alloc(struct i915_request *rq)
  	return 0;
  }
 
+static struct intel_engine_cs *
+guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
+{
+	struct intel_engine_cs *engine;
+	intel_engine_mask_t tmp, mask = ve->mask;
+	unsigned int num_siblings = 0;
+
+	for_each_engine_masked(engine, ve->gt, mask, tmp)
+		if (num_siblings++ == sibling)
+			return engine;
+
+	return NULL;
+}
+
+static int guc_virtual_context_pre_pin(struct intel_context *ce,
+				       struct i915_gem_ww_ctx *ww,
+				       void **vaddr)
+{
+	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+
+	return __guc_context_pre_pin(ce, engine, ww, vaddr);
+}
+
+static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
+{
+	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+
+	return __guc_context_pin(ce, engine, vaddr);
+}
+
+static void guc_virtual_context_enter(struct intel_context *ce)
+{
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+	struct intel_engine_cs *engine;
+
+	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+		intel_engine_pm_get(engine);
+
+	intel_timeline_enter(ce->timeline);
+}
+
+static void guc_virtual_context_exit(struct intel_context *ce)
+{
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+	struct intel_engine_cs *engine;
+
+	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+		intel_engine_pm_put(engine);
+
+	intel_timeline_exit(ce->timeline);
+}
+
+static int guc_virtual_context_alloc(struct intel_context *ce)
+{
+	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+
+	return lrc_alloc(ce, engine);
+}
+
+static struct intel_context *guc_clone_virtual(struct intel_engine_cs *src)
+{
+	struct intel_engine_cs *siblings[GUC_MAX_INSTANCES_PER_CLASS], *engine;
+	intel_engine_mask_t tmp, mask = src->mask;
+	unsigned int num_siblings = 0;
+
+	for_each_engine_masked(engine, src->gt, mask, tmp)
+		siblings[num_siblings++] = engine;
+
+	return guc_create_virtual(siblings, num_siblings);
+}
+
+static const struct intel_context_ops virtual_guc_context_ops = {
+	.alloc = guc_virtual_context_alloc,
+
+	.pre_pin = guc_virtual_context_pre_pin,
+	.pin = guc_virtual_context_pin,
+	.unpin = guc_context_unpin,
+	.post_unpin = guc_context_post_unpin,
+
+	.enter = guc_virtual_context_enter,
+	.exit = guc_virtual_context_exit,
+
+	.sched_disable = guc_context_sched_disable,
+
+	.destroy = guc_context_destroy,
+
+	.clone_virtual = guc_clone_virtual,
+	.get_sibling = guc_virtual_get_sibling,
+};
+
  static void sanitize_hwsp(struct intel_engine_cs *engine)
  {
  	struct intel_timeline *tl;
@@ -1557,7 +1688,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  	} else if (context_destroyed(ce)) {
  		/* Context has been destroyed */
  		release_guc_id(guc, ce);
-		lrc_destroy(&ce->ref);
+		__guc_context_destroy(ce);
  	}
 
  	decr_outstanding_submission_g2h(guc);
@@ -1669,3 +1800,107 @@ void intel_guc_log_context_info(struct intel_guc *guc,
  			   atomic_read(&ce->guc_sched_state_no_lock));
  	}
  }
+
+static struct intel_context *
+guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
+{
+	struct guc_virtual_engine *ve;
+	struct intel_guc *guc;
+	unsigned int n;
+	int err;
+
+	ve = kzalloc(sizeof(*ve), GFP_KERNEL);
+	if (!ve)
+		return ERR_PTR(-ENOMEM);
+
+	guc = &siblings[0]->gt->uc.guc;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_log_context_info(struct intel_guc *guc, · line 1826

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+
+	ve->base.i915 = siblings[0]->i915;
+	ve->base.gt = siblings[0]->gt;
+	ve->base.uncore = siblings[0]->uncore;
+	ve->base.id = -1;
+
+	ve->base.uabi_class = I915_ENGINE_CLASS_INVALID;
+	ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
+	ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
+	ve->base.saturated = ALL_ENGINES;

Most of these settings are the same for both execlists and GuC and aren't back-end dependent. Maybe we can have a:

intel_virtual_engine_init_early(struct intel_engine_cs *engine, struct intel_engine_cs *sibling);

And call that from both places? Can be done as a follow-up

Quoted 45 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +45 -0
+	ve->base.breadcrumbs = intel_breadcrumbs_create(&ve->base);
+	if (!ve->base.breadcrumbs) {
+		kfree(ve);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	snprintf(ve->base.name, sizeof(ve->base.name), "virtual");
+
+	ve->base.sched_engine = i915_sched_engine_get(guc->sched_engine);
+
+	ve->base.cops = &virtual_guc_context_ops;
+	ve->base.request_alloc = guc_request_alloc;
+
+	ve->base.submit_request = guc_submit_request;
+
+	ve->base.flags = I915_ENGINE_IS_VIRTUAL;
+
+	intel_context_init(&ve->context, &ve->base);
+
+	for (n = 0; n < count; n++) {
+		struct intel_engine_cs *sibling = siblings[n];
+
+		GEM_BUG_ON(!is_power_of_2(sibling->mask));
+		if (sibling->mask & ve->base.mask) {
+			DRM_DEBUG("duplicate %s entry in load balancer\n",
+				  sibling->name);
+			err = -EINVAL;
+			goto err_put;
+		}
+
+		ve->base.mask |= sibling->mask;
+
+		if (n != 0 && ve->base.class != sibling->class) {
+			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
+				  sibling->class, ve->base.class);
+			err = -EINVAL;
+			goto err_put;
+		} else if (n == 0) {
+			ve->base.class = sibling->class;
+			ve->base.uabi_class = sibling->uabi_class;
+			snprintf(ve->base.name, sizeof(ve->base.name),
+				 "v%dx%d", ve->base.class, count);
+			ve->base.context_size = sibling->context_size;
+
+			ve->base.emit_bb_start = sibling->emit_bb_start;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_log_context_info(struct intel_guc *guc, · line 1881

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+			ve->base.emit_flush = sibling->emit_flush;
+			ve->base.emit_init_breadcrumb =
+				sibling->emit_init_breadcrumb;
+			ve->base.emit_fini_breadcrumb =
+				sibling->emit_fini_breadcrumb;
+			ve->base.emit_fini_breadcrumb_dw =
+				sibling->emit_fini_breadcrumb_dw;
+
+			ve->base.flags |= sibling->flags;
+

Same here, most of these setting from the sibling are the same. intel_virtual_engine_inherit_from_sibling()?

Apart from the various nits the code LGTM, but I'll wait until the next spin for an r-b since a good chunk of the patch is going away.

Daniele

Quoted 38 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +25 -0
+			ve->base.props.timeslice_duration_ms =
+				sibling->props.timeslice_duration_ms;
+		}
+	}
+
+	return &ve->context;
+
+err_put:
+	intel_context_put(&ve->context);
+	return ERR_PTR(err);
+}
+
+
+
+bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve)
+{
+	struct intel_engine_cs *engine;
+	intel_engine_mask_t tmp, mask = ve->mask;
+
+	for_each_engine_masked(engine, ve->gt, mask, tmp)
+		if (READ_ONCE(engine->props.heartbeat_interval_ms))
+			return true;
+
+	return false;
+}
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +2 -0
index 6453e2bfa151..95df5ab06031 100644
@@ -25,6 +25,8 @@ void intel_guc_log_submission_info(struct intel_guc *guc,
  				   struct drm_printer *p);
  void intel_guc_log_context_info(struct intel_guc *guc, struct drm_printer *p);
 
+bool intel_guc_virtual_engine_has_heartbeat(const struct intel_engine_cs *ve);
+
  static inline bool intel_guc_submission_is_supported(struct intel_guc *guc)
  {
  	/* XXX: GuC submission is unavailable for now */
#87 lore raw

PATCH 27/47 drm/i915: Track 'serial' counts for virtual engines

In reply to Matthew Brost

From: John Harrison <[email protected]>

The serial number tracking of engines happens at the backend of request submission and was expecting to only be given physical engines. However, in GuC submission mode, the decomposition of virtual to physical engines does not happen in i915. Instead, requests are submitted to their virtual engine mask all the way through to the hardware (i.e. to GuC). This would mean that the heart beat code thinks the physical engines are idle due to the serial number not incrementing.

This patch updates the tracking to decompose virtual engines into their physical constituents and tracks the request against each. This is not entirely accurate as the GuC will only be issuing the request to one physical engine. However, it is the best that i915 can do given that it has no knowledge of the GuC's scheduling decisions.

---
 drivers/gpu/drm/i915/gt/intel_engine_types.h     |  2 ++
 .../gpu/drm/i915/gt/intel_execlists_submission.c |  6 ++++++
 drivers/gpu/drm/i915/gt/intel_ring_submission.c  |  6 ++++++
 drivers/gpu/drm/i915/gt/mock_engine.c            |  6 ++++++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c    | 16 ++++++++++++++++
 drivers/gpu/drm/i915/i915_request.c              |  4 +++-
 6 files changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/intel_engine_types.h +2 -0
index 5b91068ab277..1dc59e6c9a92 100644
@@ -388,6 +388,8 @@ struct intel_engine_cs {
 	void		(*park)(struct intel_engine_cs *engine);
 	void		(*unpark)(struct intel_engine_cs *engine);
 
+	void		(*bump_serial)(struct intel_engine_cs *engine);
+
 	void		(*set_default_submission)(struct intel_engine_cs *engine);
 
 	const struct intel_context_ops *cops;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +6 -0
index bd4ced794ff9..9cfb8800a0e6 100644
@@ -3203,6 +3203,11 @@ static void execlists_release(struct intel_engine_cs *engine)
 	lrc_fini_wa_ctx(engine);
 }
 
+static void execlist_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
 static void
 logical_ring_default_vfuncs(struct intel_engine_cs *engine)
 {
@@ -3212,6 +3217,7 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
 
 	engine->cops = &execlists_context_ops;
 	engine->request_alloc = execlists_request_alloc;
+	engine->bump_serial = execlist_bump_serial;
 
 	engine->reset.prepare = execlists_reset_prepare;
 	engine->reset.rewind = execlists_reset_rewind;
drivers/gpu/drm/i915/gt/intel_ring_submission.c +6 -0
index 5d42a12ef3d6..e1506b280df1 100644
@@ -1044,6 +1044,11 @@ static void setup_irq(struct intel_engine_cs *engine)
 	}
 }
 
+static void ring_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
 static void setup_common(struct intel_engine_cs *engine)
 {
 	struct drm_i915_private *i915 = engine->i915;
@@ -1063,6 +1068,7 @@ static void setup_common(struct intel_engine_cs *engine)
 
 	engine->cops = &ring_context_ops;
 	engine->request_alloc = ring_request_alloc;
+	engine->bump_serial = ring_bump_serial;
 
 	/*
 	 * Using a global execution timeline; the previous final breadcrumb is
drivers/gpu/drm/i915/gt/mock_engine.c +6 -0
index 68970398e4ef..9203c766db80 100644
@@ -292,6 +292,11 @@ static void mock_engine_release(struct intel_engine_cs *engine)
 	intel_engine_fini_retire(engine);
 }
 
+static void mock_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
 struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
 				    const char *name,
 				    int id)
@@ -318,6 +323,7 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
 
 	engine->base.cops = &mock_context_ops;
 	engine->base.request_alloc = mock_request_alloc;
+	engine->base.bump_serial = mock_bump_serial;
 	engine->base.emit_flush = mock_emit_flush;
 	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
 	engine->base.submit_request = mock_submit_request;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +16 -0
index ccbcf024b31b..d1badd7137b7 100644
@@ -1496,6 +1496,20 @@ static void guc_release(struct intel_engine_cs *engine)
 	lrc_fini_wa_ctx(engine);
 }
 
+static void guc_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
+static void virtual_guc_bump_serial(struct intel_engine_cs *engine)
+{
+	struct intel_engine_cs *e;
+	intel_engine_mask_t tmp, mask = engine->mask;
+
+	for_each_engine_masked(e, engine->gt, mask, tmp)
+		e->serial++;
+}
+
 static void guc_default_vfuncs(struct intel_engine_cs *engine)
 {
 	/* Default vfuncs which can be overridden by each engine. */
@@ -1504,6 +1518,7 @@ static void guc_default_vfuncs(struct intel_engine_cs *engine)
 
 	engine->cops = &guc_context_ops;
 	engine->request_alloc = guc_request_alloc;
+	engine->bump_serial = guc_bump_serial;
 
 	engine->sched_engine->schedule = i915_schedule;
 
@@ -1836,6 +1851,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
 
 	ve->base.cops = &virtual_guc_context_ops;
 	ve->base.request_alloc = guc_request_alloc;
+	ve->base.bump_serial = virtual_guc_bump_serial;
 
 	ve->base.submit_request = guc_submit_request;
 
drivers/gpu/drm/i915/i915_request.c +3 -1
index 7f7aa096e873..de9deb95b8b1 100644
@@ -692,7 +692,9 @@ bool __i915_request_submit(struct i915_request *request)
 				     request->ring->vaddr + request->postfix);
 
 	trace_i915_request_execute(request);
-	engine->serial++;
+	if (engine->bump_serial)
+		engine->bump_serial(engine);
+
 	result = true;
 
 	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
Signature · 6 lines
2.28.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#88 lore raw

Re: [PATCH 27/47] drm/i915: Track 'serial' counts for virtual engines

Quoted 11 lines · Matthew Brost

From: John Harrison <[email protected]>

The serial number tracking of engines happens at the backend of request submission and was expecting to only be given physical engines. However, in GuC submission mode, the decomposition of virtual to physical engines does not happen in i915. Instead, requests are submitted to their virtual engine mask all the way through to the hardware (i.e. to GuC). This would mean that the heart beat code thinks the physical engines are idle due to the serial number not incrementing.

Quoted 8 lines

This patch updates the tracking to decompose virtual engines into their physical constituents and tracks the request against each. This is not entirely accurate as the GuC will only be issuing the request to one physical engine. However, it is the best that i915 can do given that it has no knowledge of the GuC's scheduling decisions.

Need to pull in the updated subject line and commit description from Tvrtko in the RFC patch set review.

John.

Quoted 150 lines
---
  drivers/gpu/drm/i915/gt/intel_engine_types.h     |  2 ++
  .../gpu/drm/i915/gt/intel_execlists_submission.c |  6 ++++++
  drivers/gpu/drm/i915/gt/intel_ring_submission.c  |  6 ++++++
  drivers/gpu/drm/i915/gt/mock_engine.c            |  6 ++++++
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c    | 16 ++++++++++++++++
  drivers/gpu/drm/i915/i915_request.c              |  4 +++-
  6 files changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/intel_engine_types.h +2 -0
index 5b91068ab277..1dc59e6c9a92 100644
@@ -388,6 +388,8 @@ struct intel_engine_cs {
  	void		(*park)(struct intel_engine_cs *engine);
  	void		(*unpark)(struct intel_engine_cs *engine);
 
+	void		(*bump_serial)(struct intel_engine_cs *engine);
+
  	void		(*set_default_submission)(struct intel_engine_cs *engine);
 
  	const struct intel_context_ops *cops;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +6 -0
index bd4ced794ff9..9cfb8800a0e6 100644
@@ -3203,6 +3203,11 @@ static void execlists_release(struct intel_engine_cs *engine)
  	lrc_fini_wa_ctx(engine);
  }
 
+static void execlist_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
  static void
  logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  {
@@ -3212,6 +3217,7 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
 
  	engine->cops = &execlists_context_ops;
  	engine->request_alloc = execlists_request_alloc;
+	engine->bump_serial = execlist_bump_serial;
 
  	engine->reset.prepare = execlists_reset_prepare;
  	engine->reset.rewind = execlists_reset_rewind;
drivers/gpu/drm/i915/gt/intel_ring_submission.c +6 -0
index 5d42a12ef3d6..e1506b280df1 100644
@@ -1044,6 +1044,11 @@ static void setup_irq(struct intel_engine_cs *engine)
  	}
  }
 
+static void ring_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
  static void setup_common(struct intel_engine_cs *engine)
  {
  	struct drm_i915_private *i915 = engine->i915;
@@ -1063,6 +1068,7 @@ static void setup_common(struct intel_engine_cs *engine)
 
  	engine->cops = &ring_context_ops;
  	engine->request_alloc = ring_request_alloc;
+	engine->bump_serial = ring_bump_serial;
 
  	/*
  	 * Using a global execution timeline; the previous final breadcrumb is
drivers/gpu/drm/i915/gt/mock_engine.c +6 -0
index 68970398e4ef..9203c766db80 100644
@@ -292,6 +292,11 @@ static void mock_engine_release(struct intel_engine_cs *engine)
  	intel_engine_fini_retire(engine);
  }
 
+static void mock_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
  struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  				    const char *name,
  				    int id)
@@ -318,6 +323,7 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
 
  	engine->base.cops = &mock_context_ops;
  	engine->base.request_alloc = mock_request_alloc;
+	engine->base.bump_serial = mock_bump_serial;
  	engine->base.emit_flush = mock_emit_flush;
  	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
  	engine->base.submit_request = mock_submit_request;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +16 -0
index ccbcf024b31b..d1badd7137b7 100644
@@ -1496,6 +1496,20 @@ static void guc_release(struct intel_engine_cs *engine)
  	lrc_fini_wa_ctx(engine);
  }
 
+static void guc_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
+static void virtual_guc_bump_serial(struct intel_engine_cs *engine)
+{
+	struct intel_engine_cs *e;
+	intel_engine_mask_t tmp, mask = engine->mask;
+
+	for_each_engine_masked(e, engine->gt, mask, tmp)
+		e->serial++;
+}
+
  static void guc_default_vfuncs(struct intel_engine_cs *engine)
  {
  	/* Default vfuncs which can be overridden by each engine. */
@@ -1504,6 +1518,7 @@ static void guc_default_vfuncs(struct intel_engine_cs *engine)
 
  	engine->cops = &guc_context_ops;
  	engine->request_alloc = guc_request_alloc;
+	engine->bump_serial = guc_bump_serial;
 
  	engine->sched_engine->schedule = i915_schedule;
 
@@ -1836,6 +1851,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
 
  	ve->base.cops = &virtual_guc_context_ops;
  	ve->base.request_alloc = guc_request_alloc;
+	ve->base.bump_serial = virtual_guc_bump_serial;
 
  	ve->base.submit_request = guc_submit_request;
 
drivers/gpu/drm/i915/i915_request.c +3 -1
index 7f7aa096e873..de9deb95b8b1 100644
@@ -692,7 +692,9 @@ bool __i915_request_submit(struct i915_request *request)
  				     request->ring->vaddr + request->postfix);
 
  	trace_i915_request_execute(request);
-	engine->serial++;
+	if (engine->bump_serial)
+		engine->bump_serial(engine);
+
  	result = true;
 
  	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
#89 lore raw

Re: [PATCH 27/47] drm/i915: Track 'serial' counts for virtual engines

Quoted 18 lines · John Harrison
Quoted 17 lines · Matthew Brost

From: John Harrison <[email protected]>

The serial number tracking of engines happens at the backend of request submission and was expecting to only be given physical engines. However, in GuC submission mode, the decomposition of virtual to physical engines does not happen in i915. Instead, requests are submitted to their virtual engine mask all the way through to the hardware (i.e. to GuC). This would mean that the heart beat code thinks the physical engines are idle due to the serial number not incrementing.

This patch updates the tracking to decompose virtual engines into their physical constituents and tracks the request against each. This is not entirely accurate as the GuC will only be issuing the request to one physical engine. However, it is the best that i915 can do given that it has no knowledge of the GuC's scheduling decisions.

Quoted 5 lines
Quoted 2 lines

Need to pull in the updated subject line and commit description from Tvrtko in the RFC patch set review.

Yep, forgot to do this. Will do in next rev.

Matt

Quoted 125 lines

John.

Quoted 123 lines
---
  drivers/gpu/drm/i915/gt/intel_engine_types.h     |  2 ++
  .../gpu/drm/i915/gt/intel_execlists_submission.c |  6 ++++++
  drivers/gpu/drm/i915/gt/intel_ring_submission.c  |  6 ++++++
  drivers/gpu/drm/i915/gt/mock_engine.c            |  6 ++++++
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c    | 16 ++++++++++++++++
  drivers/gpu/drm/i915/i915_request.c              |  4 +++-
  6 files changed, 39 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/intel_engine_types.h +2 -0
index 5b91068ab277..1dc59e6c9a92 100644
@@ -388,6 +388,8 @@ struct intel_engine_cs {
  	void		(*park)(struct intel_engine_cs *engine);
  	void		(*unpark)(struct intel_engine_cs *engine);
+	void		(*bump_serial)(struct intel_engine_cs *engine);
+
  	void		(*set_default_submission)(struct intel_engine_cs *engine);
  	const struct intel_context_ops *cops;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +6 -0
index bd4ced794ff9..9cfb8800a0e6 100644
@@ -3203,6 +3203,11 @@ static void execlists_release(struct intel_engine_cs *engine)
  	lrc_fini_wa_ctx(engine);
  }
+static void execlist_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
  static void
  logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  {
@@ -3212,6 +3217,7 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &execlists_context_ops;
  	engine->request_alloc = execlists_request_alloc;
+	engine->bump_serial = execlist_bump_serial;
  	engine->reset.prepare = execlists_reset_prepare;
  	engine->reset.rewind = execlists_reset_rewind;
drivers/gpu/drm/i915/gt/intel_ring_submission.c +6 -0
index 5d42a12ef3d6..e1506b280df1 100644
@@ -1044,6 +1044,11 @@ static void setup_irq(struct intel_engine_cs *engine)
  	}
  }
+static void ring_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
  static void setup_common(struct intel_engine_cs *engine)
  {
  	struct drm_i915_private *i915 = engine->i915;
@@ -1063,6 +1068,7 @@ static void setup_common(struct intel_engine_cs *engine)
  	engine->cops = &ring_context_ops;
  	engine->request_alloc = ring_request_alloc;
+	engine->bump_serial = ring_bump_serial;
  	/*
  	 * Using a global execution timeline; the previous final breadcrumb is
drivers/gpu/drm/i915/gt/mock_engine.c +6 -0
index 68970398e4ef..9203c766db80 100644
@@ -292,6 +292,11 @@ static void mock_engine_release(struct intel_engine_cs *engine)
  	intel_engine_fini_retire(engine);
  }
+static void mock_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
  struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  				    const char *name,
  				    int id)
@@ -318,6 +323,7 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  	engine->base.cops = &mock_context_ops;
  	engine->base.request_alloc = mock_request_alloc;
+	engine->base.bump_serial = mock_bump_serial;
  	engine->base.emit_flush = mock_emit_flush;
  	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
  	engine->base.submit_request = mock_submit_request;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +16 -0
index ccbcf024b31b..d1badd7137b7 100644
@@ -1496,6 +1496,20 @@ static void guc_release(struct intel_engine_cs *engine)
  	lrc_fini_wa_ctx(engine);
  }
+static void guc_bump_serial(struct intel_engine_cs *engine)
+{
+	engine->serial++;
+}
+
+static void virtual_guc_bump_serial(struct intel_engine_cs *engine)
+{
+	struct intel_engine_cs *e;
+	intel_engine_mask_t tmp, mask = engine->mask;
+
+	for_each_engine_masked(e, engine->gt, mask, tmp)
+		e->serial++;
+}
+
  static void guc_default_vfuncs(struct intel_engine_cs *engine)
  {
  	/* Default vfuncs which can be overridden by each engine. */
@@ -1504,6 +1518,7 @@ static void guc_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &guc_context_ops;
  	engine->request_alloc = guc_request_alloc;
+	engine->bump_serial = guc_bump_serial;
  	engine->sched_engine->schedule = i915_schedule;
@@ -1836,6 +1851,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  	ve->base.cops = &virtual_guc_context_ops;
  	ve->base.request_alloc = guc_request_alloc;
+	ve->base.bump_serial = virtual_guc_bump_serial;
  	ve->base.submit_request = guc_submit_request;
drivers/gpu/drm/i915/i915_request.c +0 -0
index 7f7aa096e873..de9deb95b8b1 100644
@@ -692,7 +692,9 @@ bool __i915_request_submit(struct i915_request *request)
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c bool __i915_request_submit(struct i915_request *request) · line 700

Quoted 8 lines
drivers/gpu/drm/i915/i915_request.c +3 -1
  				     request->ring->vaddr + request->postfix);
  	trace_i915_request_execute(request);
-	engine->serial++;
+	if (engine->bump_serial)
+		engine->bump_serial(engine);
+
  	result = true;
  	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#90 lore raw

PATCH 28/47 drm/i915: Hold reference to intel_context over life of i915_request

In reply to Matthew Brost

Hold a reference to the intel_context over life of an i915_request. Without this an i915_request can exist after the context has been destroyed (e.g. request retired, context closed, but user space holds a reference to the request from an out fence). In the case of GuC submission + virtual engine, the engine that the request references is also destroyed which can trigger bad pointer dref in fence ops (e.g. i915_fence_get_driver_name). We could likely change i915_fence_get_driver_name to avoid touching the engine but let's just be safe and hold the intel_context reference.

---
 drivers/gpu/drm/i915/i915_request.c | 54 ++++++++++++-----------------
 1 file changed, 22 insertions(+), 32 deletions(-)
drivers/gpu/drm/i915/i915_request.c +22 -32
index de9deb95b8b1..dec5a35c9aa2 100644
@@ -126,39 +126,17 @@ static void i915_fence_release(struct dma_fence *fence)
 	i915_sw_fence_fini(&rq->semaphore);
 
 	/*
-	 * Keep one request on each engine for reserved use under mempressure
-	 *
-	 * We do not hold a reference to the engine here and so have to be
-	 * very careful in what rq->engine we poke. The virtual engine is
-	 * referenced via the rq->context and we released that ref during
-	 * i915_request_retire(), ergo we must not dereference a virtual
-	 * engine here. Not that we would want to, as the only consumer of
-	 * the reserved engine->request_pool is the power management parking,
-	 * which must-not-fail, and that is only run on the physical engines.
-	 *
-	 * Since the request must have been executed to be have completed,
-	 * we know that it will have been processed by the HW and will
-	 * not be unsubmitted again, so rq->engine and rq->execution_mask
-	 * at this point is stable. rq->execution_mask will be a single
-	 * bit if the last and _only_ engine it could execution on was a
-	 * physical engine, if it's multiple bits then it started on and
-	 * could still be on a virtual engine. Thus if the mask is not a
-	 * power-of-two we assume that rq->engine may still be a virtual
-	 * engine and so a dangling invalid pointer that we cannot dereference
-	 *
-	 * For example, consider the flow of a bonded request through a virtual
-	 * engine. The request is created with a wide engine mask (all engines
-	 * that we might execute on). On processing the bond, the request mask
-	 * is reduced to one or more engines. If the request is subsequently
-	 * bound to a single engine, it will then be constrained to only
-	 * execute on that engine and never returned to the virtual engine
-	 * after timeslicing away, see __unwind_incomplete_requests(). Thus we
-	 * know that if the rq->execution_mask is a single bit, rq->engine
-	 * can be a physical engine with the exact corresponding mask.
+	 * Keep one request on each engine for reserved use under mempressure,
+	 * do not use with virtual engines as this really is only needed for
+	 * kernel contexts.
 	 */
-	if (is_power_of_2(rq->execution_mask) &&
-	    !cmpxchg(&rq->engine->request_pool, NULL, rq))
+	if (!intel_engine_is_virtual(rq->engine) &&
+	    !cmpxchg(&rq->engine->request_pool, NULL, rq)) {
+		intel_context_put(rq->context);
 		return;
+	}
+
+	intel_context_put(rq->context);
 
 	kmem_cache_free(global.slab_requests, rq);
 }
@@ -977,7 +955,18 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
 		}
 	}
 
-	rq->context = ce;
+	/*
+	 * Hold a reference to the intel_context over life of an i915_request.
+	 * Without this an i915_request can exist after the context has been
+	 * destroyed (e.g. request retired, context closed, but user space holds
+	 * a reference to the request from an out fence). In the case of GuC
+	 * submission + virtual engine, the engine that the request references
+	 * is also destroyed which can trigger bad pointer dref in fence ops
+	 * (e.g. i915_fence_get_driver_name). We could likely change these
+	 * functions to avoid touching the engine but let's just be safe and
+	 * hold the intel_context reference.
+	 */
+	rq->context = intel_context_get(ce);
 	rq->engine = ce->engine;
 	rq->ring = ce->ring;
 	rq->execution_mask = ce->engine->mask;
@@ -1054,6 +1043,7 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
 	GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
 
 err_free:
+	intel_context_put(ce);
 	kmem_cache_free(global.slab_requests, rq);
 err_unreserve:
 	intel_context_unpin(ce);
Signature · 6 lines
2.28.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#91 lore raw

Re: [PATCH 28/47] drm/i915: Hold reference to intel_context over life of i915_request

Quoted 6 lines · Matthew Brost

Hold a reference to the intel_context over life of an i915_request. Without this an i915_request can exist after the context has been destroyed (e.g. request retired, context closed, but user space holds a reference to the request from an out fence). In the case of GuC submission + virtual engine, the engine that the request references is also destroyed which can trigger bad pointer dref in fence ops (e.g.

Maybe quickly explain a why this is different for GuC submission vs execlist? Presumably it is about only decomposing virtual engines to physical ones in execlist mode?

Quoted 49 lines

i915_fence_get_driver_name). We could likely change i915_fence_get_driver_name to avoid touching the engine but let's just be safe and hold the intel_context reference.

---
  drivers/gpu/drm/i915/i915_request.c | 54 ++++++++++++-----------------
  1 file changed, 22 insertions(+), 32 deletions(-)
drivers/gpu/drm/i915/i915_request.c +3 -29
index de9deb95b8b1..dec5a35c9aa2 100644
@@ -126,39 +126,17 @@ static void i915_fence_release(struct dma_fence *fence)
  	i915_sw_fence_fini(&rq->semaphore);
 
  	/*
-	 * Keep one request on each engine for reserved use under mempressure
-	 *
-	 * We do not hold a reference to the engine here and so have to be
-	 * very careful in what rq->engine we poke. The virtual engine is
-	 * referenced via the rq->context and we released that ref during
-	 * i915_request_retire(), ergo we must not dereference a virtual
-	 * engine here. Not that we would want to, as the only consumer of
-	 * the reserved engine->request_pool is the power management parking,
-	 * which must-not-fail, and that is only run on the physical engines.
-	 *
-	 * Since the request must have been executed to be have completed,
-	 * we know that it will have been processed by the HW and will
-	 * not be unsubmitted again, so rq->engine and rq->execution_mask
-	 * at this point is stable. rq->execution_mask will be a single
-	 * bit if the last and _only_ engine it could execution on was a
-	 * physical engine, if it's multiple bits then it started on and
-	 * could still be on a virtual engine. Thus if the mask is not a
-	 * power-of-two we assume that rq->engine may still be a virtual
-	 * engine and so a dangling invalid pointer that we cannot dereference
-	 *
-	 * For example, consider the flow of a bonded request through a virtual
-	 * engine. The request is created with a wide engine mask (all engines
-	 * that we might execute on). On processing the bond, the request mask
-	 * is reduced to one or more engines. If the request is subsequently
-	 * bound to a single engine, it will then be constrained to only
-	 * execute on that engine and never returned to the virtual engine
-	 * after timeslicing away, see __unwind_incomplete_requests(). Thus we
-	 * know that if the rq->execution_mask is a single bit, rq->engine
-	 * can be a physical engine with the exact corresponding mask.
+	 * Keep one request on each engine for reserved use under mempressure,
+	 * do not use with virtual engines as this really is only needed for
+	 * kernel contexts.

drivers/gpu/drm/i915/i915_request.c static void i915_fence_release(struct dma_fence *fence) · line 139

Quoted 10 lines
drivers/gpu/drm/i915/i915_request.c +6 -2
  	 */
-	if (is_power_of_2(rq->execution_mask) &&
-	    !cmpxchg(&rq->engine->request_pool, NULL, rq))
+	if (!intel_engine_is_virtual(rq->engine) &&
+	    !cmpxchg(&rq->engine->request_pool, NULL, rq)) {
+		intel_context_put(rq->context);
  		return;
+	}
+
+	intel_context_put(rq->context);

The put is actually unconditional? So it could be moved before the if?

John.

Quoted 24 lines
drivers/gpu/drm/i915/i915_request.c +12 -1
  	kmem_cache_free(global.slab_requests, rq);
  }
@@ -977,7 +955,18 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
  		}
  	}
 
-	rq->context = ce;
+	/*
+	 * Hold a reference to the intel_context over life of an i915_request.
+	 * Without this an i915_request can exist after the context has been
+	 * destroyed (e.g. request retired, context closed, but user space holds
+	 * a reference to the request from an out fence). In the case of GuC
+	 * submission + virtual engine, the engine that the request references
+	 * is also destroyed which can trigger bad pointer dref in fence ops
+	 * (e.g. i915_fence_get_driver_name). We could likely change these
+	 * functions to avoid touching the engine but let's just be safe and
+	 * hold the intel_context reference.
+	 */
+	rq->context = intel_context_get(ce);
  	rq->engine = ce->engine;
  	rq->ring = ce->ring;
  	rq->execution_mask = ce->engine->mask;

drivers/gpu/drm/i915/i915_request.c __i915_request_create(struct intel_context *ce, gfp_t gfp) · line 1049

Quoted 7 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
@@ -1054,6 +1043,7 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
  	GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
 
  err_free:
+	intel_context_put(ce);
  	kmem_cache_free(global.slab_requests, rq);
  err_unreserve:
  	intel_context_unpin(ce);

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#92 lore raw

Re: [PATCH 28/47] drm/i915: Hold reference to intel_context over life of i915_request

Quoted 11 lines · John Harrison
Quoted 6 lines · Matthew Brost

Hold a reference to the intel_context over life of an i915_request. Without this an i915_request can exist after the context has been destroyed (e.g. request retired, context closed, but user space holds a reference to the request from an out fence). In the case of GuC submission + virtual engine, the engine that the request references is also destroyed which can trigger bad pointer dref in fence ops (e.g.

Maybe quickly explain a why this is different for GuC submission vs execlist? Presumably it is about only decomposing virtual engines to physical ones in execlist mode?

Yes, it because in execlists we always end up pointing to a physical engine in the end while in GuC mode we can be pointing to dynamically allocated virtual engine. I can update the comment.

Quoted 52 lines
Quoted 52 lines

i915_fence_get_driver_name). We could likely change i915_fence_get_driver_name to avoid touching the engine but let's just be safe and hold the intel_context reference.

---
  drivers/gpu/drm/i915/i915_request.c | 54 ++++++++++++-----------------
  1 file changed, 22 insertions(+), 32 deletions(-)
drivers/gpu/drm/i915/i915_request.c +3 -31
index de9deb95b8b1..dec5a35c9aa2 100644
@@ -126,39 +126,17 @@ static void i915_fence_release(struct dma_fence *fence)
  	i915_sw_fence_fini(&rq->semaphore);
  	/*
-	 * Keep one request on each engine for reserved use under mempressure
-	 *
-	 * We do not hold a reference to the engine here and so have to be
-	 * very careful in what rq->engine we poke. The virtual engine is
-	 * referenced via the rq->context and we released that ref during
-	 * i915_request_retire(), ergo we must not dereference a virtual
-	 * engine here. Not that we would want to, as the only consumer of
-	 * the reserved engine->request_pool is the power management parking,
-	 * which must-not-fail, and that is only run on the physical engines.
-	 *
-	 * Since the request must have been executed to be have completed,
-	 * we know that it will have been processed by the HW and will
-	 * not be unsubmitted again, so rq->engine and rq->execution_mask
-	 * at this point is stable. rq->execution_mask will be a single
-	 * bit if the last and _only_ engine it could execution on was a
-	 * physical engine, if it's multiple bits then it started on and
-	 * could still be on a virtual engine. Thus if the mask is not a
-	 * power-of-two we assume that rq->engine may still be a virtual
-	 * engine and so a dangling invalid pointer that we cannot dereference
-	 *
-	 * For example, consider the flow of a bonded request through a virtual
-	 * engine. The request is created with a wide engine mask (all engines
-	 * that we might execute on). On processing the bond, the request mask
-	 * is reduced to one or more engines. If the request is subsequently
-	 * bound to a single engine, it will then be constrained to only
-	 * execute on that engine and never returned to the virtual engine
-	 * after timeslicing away, see __unwind_incomplete_requests(). Thus we
-	 * know that if the rq->execution_mask is a single bit, rq->engine
-	 * can be a physical engine with the exact corresponding mask.
+	 * Keep one request on each engine for reserved use under mempressure,
+	 * do not use with virtual engines as this really is only needed for
+	 * kernel contexts.
  	 */
-	if (is_power_of_2(rq->execution_mask) &&
-	    !cmpxchg(&rq->engine->request_pool, NULL, rq))
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c static void i915_fence_release(struct dma_fence *fence) · line 139

Quoted 7 lines
drivers/gpu/drm/i915/i915_request.c +6 -0
+	if (!intel_engine_is_virtual(rq->engine) &&
+	    !cmpxchg(&rq->engine->request_pool, NULL, rq)) {
+		intel_context_put(rq->context);
  		return;
+	}
+
+	intel_context_put(rq->context);

The put is actually unconditional? So it could be moved before the if?

Yep, I think so.

Matt

Quoted 22 lines

John.

Quoted 20 lines
drivers/gpu/drm/i915/i915_request.c +12 -1
  	kmem_cache_free(global.slab_requests, rq);
  }
@@ -977,7 +955,18 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
  		}
  	}
-	rq->context = ce;
+	/*
+	 * Hold a reference to the intel_context over life of an i915_request.
+	 * Without this an i915_request can exist after the context has been
+	 * destroyed (e.g. request retired, context closed, but user space holds
+	 * a reference to the request from an out fence). In the case of GuC
+	 * submission + virtual engine, the engine that the request references
+	 * is also destroyed which can trigger bad pointer dref in fence ops
+	 * (e.g. i915_fence_get_driver_name). We could likely change these
+	 * functions to avoid touching the engine but let's just be safe and
+	 * hold the intel_context reference.
+	 */
+	rq->context = intel_context_get(ce);
  	rq->engine = ce->engine;
  	rq->ring = ce->ring;
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c __i915_request_create(struct intel_context *ce, gfp_t gfp) · line 1048

Quoted 8 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
  	rq->execution_mask = ce->engine->mask;
@@ -1054,6 +1043,7 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
  	GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
  err_free:
+	intel_context_put(ce);
  	kmem_cache_free(global.slab_requests, rq);
  err_unreserve:
  	intel_context_unpin(ce);

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#93 lore raw

Re: [Intel-gfx] [PATCH 28/47] drm/i915: Hold reference to intel_context over life of i915_request

Quoted 79 lines · Matthew Brost
Quoted 11 lines · John Harrison
Quoted 6 lines · Matthew Brost

Hold a reference to the intel_context over life of an i915_request. Without this an i915_request can exist after the context has been destroyed (e.g. request retired, context closed, but user space holds a reference to the request from an out fence). In the case of GuC submission + virtual engine, the engine that the request references is also destroyed which can trigger bad pointer dref in fence ops (e.g.

Maybe quickly explain a why this is different for GuC submission vs execlist? Presumably it is about only decomposing virtual engines to physical ones in execlist mode?

Yes, it because in execlists we always end up pointing to a physical engine in the end while in GuC mode we can be pointing to dynamically allocated virtual engine. I can update the comment.

Quoted 52 lines
Quoted 52 lines

i915_fence_get_driver_name). We could likely change i915_fence_get_driver_name to avoid touching the engine but let's just be safe and hold the intel_context reference.

---
  drivers/gpu/drm/i915/i915_request.c | 54 ++++++++++++-----------------
  1 file changed, 22 insertions(+), 32 deletions(-)
drivers/gpu/drm/i915/i915_request.c +3 -31
index de9deb95b8b1..dec5a35c9aa2 100644
@@ -126,39 +126,17 @@ static void i915_fence_release(struct dma_fence *fence)
  	i915_sw_fence_fini(&rq->semaphore);
  	/*
-	 * Keep one request on each engine for reserved use under mempressure
-	 *
-	 * We do not hold a reference to the engine here and so have to be
-	 * very careful in what rq->engine we poke. The virtual engine is
-	 * referenced via the rq->context and we released that ref during
-	 * i915_request_retire(), ergo we must not dereference a virtual
-	 * engine here. Not that we would want to, as the only consumer of
-	 * the reserved engine->request_pool is the power management parking,
-	 * which must-not-fail, and that is only run on the physical engines.
-	 *
-	 * Since the request must have been executed to be have completed,
-	 * we know that it will have been processed by the HW and will
-	 * not be unsubmitted again, so rq->engine and rq->execution_mask
-	 * at this point is stable. rq->execution_mask will be a single
-	 * bit if the last and _only_ engine it could execution on was a
-	 * physical engine, if it's multiple bits then it started on and
-	 * could still be on a virtual engine. Thus if the mask is not a
-	 * power-of-two we assume that rq->engine may still be a virtual
-	 * engine and so a dangling invalid pointer that we cannot dereference
-	 *
-	 * For example, consider the flow of a bonded request through a virtual
-	 * engine. The request is created with a wide engine mask (all engines
-	 * that we might execute on). On processing the bond, the request mask
-	 * is reduced to one or more engines. If the request is subsequently
-	 * bound to a single engine, it will then be constrained to only
-	 * execute on that engine and never returned to the virtual engine
-	 * after timeslicing away, see __unwind_incomplete_requests(). Thus we
-	 * know that if the rq->execution_mask is a single bit, rq->engine
-	 * can be a physical engine with the exact corresponding mask.
+	 * Keep one request on each engine for reserved use under mempressure,
+	 * do not use with virtual engines as this really is only needed for
+	 * kernel contexts.
  	 */
-	if (is_power_of_2(rq->execution_mask) &&
-	    !cmpxchg(&rq->engine->request_pool, NULL, rq))
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c static void i915_fence_release(struct dma_fence *fence) · line 139

Quoted 7 lines
drivers/gpu/drm/i915/i915_request.c +6 -0
+	if (!intel_engine_is_virtual(rq->engine) &&
+	    !cmpxchg(&rq->engine->request_pool, NULL, rq)) {
+		intel_context_put(rq->context);
  		return;
+	}
+
+	intel_context_put(rq->context);

The put is actually unconditional? So it could be moved before the if?

Quoted 2 lines

Yep, I think so.

Wait nope. We reference rq->engine which could a virtual engine and the intel_context_put could free that engine. So we need to do the put after we reference it.

Matt

Quoted 33 lines

Matt

Quoted 22 lines

John.

Quoted 20 lines
drivers/gpu/drm/i915/i915_request.c +12 -1
  	kmem_cache_free(global.slab_requests, rq);
  }
@@ -977,7 +955,18 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
  		}
  	}
-	rq->context = ce;
+	/*
+	 * Hold a reference to the intel_context over life of an i915_request.
+	 * Without this an i915_request can exist after the context has been
+	 * destroyed (e.g. request retired, context closed, but user space holds
+	 * a reference to the request from an out fence). In the case of GuC
+	 * submission + virtual engine, the engine that the request references
+	 * is also destroyed which can trigger bad pointer dref in fence ops
+	 * (e.g. i915_fence_get_driver_name). We could likely change these
+	 * functions to avoid touching the engine but let's just be safe and
+	 * hold the intel_context reference.
+	 */
+	rq->context = intel_context_get(ce);
  	rq->engine = ce->engine;
  	rq->ring = ce->ring;
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c __i915_request_create(struct intel_context *ce, gfp_t gfp) · line 1048

Quoted 8 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
  	rq->execution_mask = ce->engine->mask;
@@ -1054,6 +1043,7 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
  	GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
  err_free:
+	intel_context_put(ce);
  	kmem_cache_free(global.slab_requests, rq);
  err_unreserve:
  	intel_context_unpin(ce);
Quoted 4 lines

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reviewed-by
#94 lore raw

Re: [Intel-gfx] [PATCH 28/47] drm/i915: Hold reference to intel_context over life of i915_request

Quoted 77 lines · Matthew Brost
Quoted 76 lines · Matthew Brost
Quoted 11 lines · John Harrison
Quoted 6 lines · Matthew Brost

Hold a reference to the intel_context over life of an i915_request. Without this an i915_request can exist after the context has been destroyed (e.g. request retired, context closed, but user space holds a reference to the request from an out fence). In the case of GuC submission + virtual engine, the engine that the request references is also destroyed which can trigger bad pointer dref in fence ops (e.g.

Maybe quickly explain a why this is different for GuC submission vs execlist? Presumably it is about only decomposing virtual engines to physical ones in execlist mode?

Yes, it because in execlists we always end up pointing to a physical engine in the end while in GuC mode we can be pointing to dynamically allocated virtual engine. I can update the comment.

Quoted 60 lines
Quoted 48 lines

i915_fence_get_driver_name). We could likely change i915_fence_get_driver_name to avoid touching the engine but let's just be safe and hold the intel_context reference.

---
   drivers/gpu/drm/i915/i915_request.c | 54 ++++++++++++-----------------
   1 file changed, 22 insertions(+), 32 deletions(-)
drivers/gpu/drm/i915/i915_request.c +3 -29
index de9deb95b8b1..dec5a35c9aa2 100644
@@ -126,39 +126,17 @@ static void i915_fence_release(struct dma_fence *fence)
   	i915_sw_fence_fini(&rq->semaphore);
   	/*
-	 * Keep one request on each engine for reserved use under mempressure
-	 *
-	 * We do not hold a reference to the engine here and so have to be
-	 * very careful in what rq->engine we poke. The virtual engine is
-	 * referenced via the rq->context and we released that ref during
-	 * i915_request_retire(), ergo we must not dereference a virtual
-	 * engine here. Not that we would want to, as the only consumer of
-	 * the reserved engine->request_pool is the power management parking,
-	 * which must-not-fail, and that is only run on the physical engines.
-	 *
-	 * Since the request must have been executed to be have completed,
-	 * we know that it will have been processed by the HW and will
-	 * not be unsubmitted again, so rq->engine and rq->execution_mask
-	 * at this point is stable. rq->execution_mask will be a single
-	 * bit if the last and _only_ engine it could execution on was a
-	 * physical engine, if it's multiple bits then it started on and
-	 * could still be on a virtual engine. Thus if the mask is not a
-	 * power-of-two we assume that rq->engine may still be a virtual
-	 * engine and so a dangling invalid pointer that we cannot dereference
-	 *
-	 * For example, consider the flow of a bonded request through a virtual
-	 * engine. The request is created with a wide engine mask (all engines
-	 * that we might execute on). On processing the bond, the request mask
-	 * is reduced to one or more engines. If the request is subsequently
-	 * bound to a single engine, it will then be constrained to only
-	 * execute on that engine and never returned to the virtual engine
-	 * after timeslicing away, see __unwind_incomplete_requests(). Thus we
-	 * know that if the rq->execution_mask is a single bit, rq->engine
-	 * can be a physical engine with the exact corresponding mask.
+	 * Keep one request on each engine for reserved use under mempressure,
+	 * do not use with virtual engines as this really is only needed for
+	 * kernel contexts.

drivers/gpu/drm/i915/i915_request.c static void i915_fence_release(struct dma_fence *fence) · line 138

Quoted 10 lines
drivers/gpu/drm/i915/i915_request.c +6 -2
   	 */
-	if (is_power_of_2(rq->execution_mask) &&
-	    !cmpxchg(&rq->engine->request_pool, NULL, rq))
+	if (!intel_engine_is_virtual(rq->engine) &&
+	    !cmpxchg(&rq->engine->request_pool, NULL, rq)) {
+		intel_context_put(rq->context);
   		return;
+	}
+
+	intel_context_put(rq->context);

The put is actually unconditional? So it could be moved before the if?

Quoted 7 lines
Quoted 2 lines

Yep, I think so.

Wait nope. We reference rq->engine which could a virtual engine and the intel_context_put could free that engine. So we need to do the put after we reference it.

Matt

Doh! That's a pretty good reason.

Okay, with a tweaked description to explain about virtual engines being different on GuC vs execlist...

Quoted 29 lines
Quoted 29 lines

Matt

Quoted 26 lines

John.

Quoted 24 lines
drivers/gpu/drm/i915/i915_request.c +12 -1
   	kmem_cache_free(global.slab_requests, rq);
   }
@@ -977,7 +955,18 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
   		}
   	}
-	rq->context = ce;
+	/*
+	 * Hold a reference to the intel_context over life of an i915_request.
+	 * Without this an i915_request can exist after the context has been
+	 * destroyed (e.g. request retired, context closed, but user space holds
+	 * a reference to the request from an out fence). In the case of GuC
+	 * submission + virtual engine, the engine that the request references
+	 * is also destroyed which can trigger bad pointer dref in fence ops
+	 * (e.g. i915_fence_get_driver_name). We could likely change these
+	 * functions to avoid touching the engine but let's just be safe and
+	 * hold the intel_context reference.
+	 */
+	rq->context = intel_context_get(ce);
   	rq->engine = ce->engine;
   	rq->ring = ce->ring;
   	rq->execution_mask = ce->engine->mask;
@@ -1054,6 +1043,7 @@ __i915_request_create(struct intel_context *ce, gfp_t gfp)
   	GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
   err_free:
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

drivers/gpu/drm/i915/i915_request.c __i915_request_create(struct intel_context *ce, gfp_t gfp) · line 1049

Quoted 4 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
+	intel_context_put(ce);
   	kmem_cache_free(global.slab_requests, rq);
   err_unreserve:
   	intel_context_unpin(ce);

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#95 lore raw

PATCH 29/47 drm/i915/guc: Disable bonding extension with GuC submission

In reply to Matthew Brost

Update the bonding extension to return -ENODEV when using GuC submission as this extension fundamentally will not work with the GuC submission interface.

---
 drivers/gpu/drm/i915/gem/i915_gem_context.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/i915/gem/i915_gem_context.c +5 -0
index 8a9293e0ca92..0429aa4172bf 100644
@@ -1674,6 +1674,11 @@ set_engines__bond(struct i915_user_extension __user *base, void *data)
 	}
 	virtual = set->engines->engines[idx]->engine;
 
+	if (intel_engine_uses_guc(virtual)) {
+		DRM_DEBUG("bonding extension not supported with GuC submission");
+		return -ENODEV;
+	}
+
 	err = check_user_mbz(&ext->flags);
 	if (err)
 		return err;
Signature · 1 line
2.28.0
Reviewed-by on v1 29/47
#96 lore raw

Re: [PATCH 29/47] drm/i915/guc: Disable bonding extension with GuC submission

Quoted 5 lines · Matthew Brost

Update the bonding extension to return -ENODEV when using GuC submission as this extension fundamentally will not work with the GuC submission interface.

Quoted 20 lines
---
  drivers/gpu/drm/i915/gem/i915_gem_context.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/gpu/drm/i915/gem/i915_gem_context.c +5 -0
index 8a9293e0ca92..0429aa4172bf 100644
@@ -1674,6 +1674,11 @@ set_engines__bond(struct i915_user_extension __user *base, void *data)
  	}
  	virtual = set->engines->engines[idx]->engine;
 
+	if (intel_engine_uses_guc(virtual)) {
+		DRM_DEBUG("bonding extension not supported with GuC submission");
+		return -ENODEV;
+	}
+
  	err = check_user_mbz(&ext->flags);
  	if (err)
  		return err;
#97 lore raw

PATCH 30/47 drm/i915/guc: Direct all breadcrumbs for a class to single breadcrumbs

In reply to Matthew Brost

With GuC virtual engines the physical engine which a request executes and completes on isn't known to the i915. Therefore we can't attach a request to a physical engines breadcrumbs. To work around this we create a single breadcrumbs per engine class when using GuC submission and direct all physical engine interrupts to this breadcrumbs.

---
 drivers/gpu/drm/i915/gt/intel_breadcrumbs.c   | 41 +++++-------
 drivers/gpu/drm/i915/gt/intel_breadcrumbs.h   | 14 +++-
 .../gpu/drm/i915/gt/intel_breadcrumbs_types.h |  7 ++
 drivers/gpu/drm/i915/gt/intel_engine.h        |  3 +
 drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 28 +++++++-
 drivers/gpu/drm/i915/gt/intel_engine_types.h  |  1 -
 .../drm/i915/gt/intel_execlists_submission.c  |  2 +-
 drivers/gpu/drm/i915/gt/mock_engine.c         |  4 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 67 +++++++++++++++++--
 9 files changed, 131 insertions(+), 36 deletions(-)
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +17 -24
index 38cc42783dfb..2007dc6f6b99 100644
@@ -15,28 +15,14 @@
 #include "intel_gt_pm.h"
 #include "intel_gt_requests.h"
 
-static bool irq_enable(struct intel_engine_cs *engine)
+static bool irq_enable(struct intel_breadcrumbs *b)
 {
-	if (!engine->irq_enable)
-		return false;
-
-	/* Caller disables interrupts */
-	spin_lock(&engine->gt->irq_lock);
-	engine->irq_enable(engine);
-	spin_unlock(&engine->gt->irq_lock);
-
-	return true;
+	return intel_engine_irq_enable(b->irq_engine);
 }
 
-static void irq_disable(struct intel_engine_cs *engine)
+static void irq_disable(struct intel_breadcrumbs *b)
 {
-	if (!engine->irq_disable)
-		return;
-
-	/* Caller disables interrupts */
-	spin_lock(&engine->gt->irq_lock);
-	engine->irq_disable(engine);
-	spin_unlock(&engine->gt->irq_lock);
+	intel_engine_irq_disable(b->irq_engine);
 }
 
 static void __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
@@ -57,7 +43,7 @@ static void __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
 	WRITE_ONCE(b->irq_armed, true);
 
 	/* Requests may have completed before we could enable the interrupt. */
-	if (!b->irq_enabled++ && irq_enable(b->irq_engine))
+	if (!b->irq_enabled++ && b->irq_enable(b))
 		irq_work_queue(&b->irq_work);
 }
 
@@ -76,7 +62,7 @@ static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
 {
 	GEM_BUG_ON(!b->irq_enabled);
 	if (!--b->irq_enabled)
-		irq_disable(b->irq_engine);
+		b->irq_disable(b);
 
 	WRITE_ONCE(b->irq_armed, false);
 	intel_gt_pm_put_async(b->irq_engine->gt);
@@ -281,7 +267,7 @@ intel_breadcrumbs_create(struct intel_engine_cs *irq_engine)
 	if (!b)
 		return NULL;
 
-	b->irq_engine = irq_engine;
+	kref_init(&b->ref);
 
 	spin_lock_init(&b->signalers_lock);
 	INIT_LIST_HEAD(&b->signalers);
@@ -290,6 +276,10 @@ intel_breadcrumbs_create(struct intel_engine_cs *irq_engine)
 	spin_lock_init(&b->irq_lock);
 	init_irq_work(&b->irq_work, signal_irq_work);
 
+	b->irq_engine = irq_engine;
+	b->irq_enable = irq_enable;
+	b->irq_disable = irq_disable;
+
 	return b;
 }
 
@@ -303,9 +293,9 @@ void intel_breadcrumbs_reset(struct intel_breadcrumbs *b)
 	spin_lock_irqsave(&b->irq_lock, flags);
 
 	if (b->irq_enabled)
-		irq_enable(b->irq_engine);
+		b->irq_enable(b);
 	else
-		irq_disable(b->irq_engine);
+		b->irq_disable(b);
 
 	spin_unlock_irqrestore(&b->irq_lock, flags);
 }
@@ -325,11 +315,14 @@ void __intel_breadcrumbs_park(struct intel_breadcrumbs *b)
 	}
 }
 
-void intel_breadcrumbs_free(struct intel_breadcrumbs *b)
+void intel_breadcrumbs_free(struct kref *kref)
 {
+	struct intel_breadcrumbs *b = container_of(kref, typeof(*b), ref);
+
 	irq_work_sync(&b->irq_work);
 	GEM_BUG_ON(!list_empty(&b->signalers));
 	GEM_BUG_ON(b->irq_armed);
+
 	kfree(b);
 }
 
drivers/gpu/drm/i915/gt/intel_breadcrumbs.h +13 -1
index 3ce5ce270b04..72105b74663d 100644
@@ -17,7 +17,7 @@ struct intel_breadcrumbs;
 
 struct intel_breadcrumbs *
 intel_breadcrumbs_create(struct intel_engine_cs *irq_engine);
-void intel_breadcrumbs_free(struct intel_breadcrumbs *b);
+void intel_breadcrumbs_free(struct kref *kref);
 
 void intel_breadcrumbs_reset(struct intel_breadcrumbs *b);
 void __intel_breadcrumbs_park(struct intel_breadcrumbs *b);
@@ -48,4 +48,16 @@ void i915_request_cancel_breadcrumb(struct i915_request *request);
 void intel_context_remove_breadcrumbs(struct intel_context *ce,
 				      struct intel_breadcrumbs *b);
 
+static inline struct intel_breadcrumbs *
+intel_breadcrumbs_get(struct intel_breadcrumbs *b)
+{
+	kref_get(&b->ref);
+	return b;
+}
+
+static inline void intel_breadcrumbs_put(struct intel_breadcrumbs *b)
+{
+	kref_put(&b->ref, intel_breadcrumbs_free);
+}
+
 #endif /* __INTEL_BREADCRUMBS__ */
drivers/gpu/drm/i915/gt/intel_breadcrumbs_types.h +7 -0
index 3a084ce8ff5e..a4e146684be8 100644
@@ -7,10 +7,13 @@
 #define __INTEL_BREADCRUMBS_TYPES__
 
 #include <linux/irq_work.h>
+#include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/spinlock.h>
 #include <linux/types.h>
 
+typedef u8 intel_engine_mask_t;
+
 /*
  * Rather than have every client wait upon all user interrupts,
  * with the herd waking after every interrupt and each doing the
@@ -29,6 +32,7 @@
  * the overhead of waking that client is much preferred.
  */
 struct intel_breadcrumbs {
+	struct kref ref;
 	atomic_t active;
 
 	spinlock_t signalers_lock; /* protects the list of signalers */
@@ -42,7 +46,10 @@ struct intel_breadcrumbs {
 	bool irq_armed;
 
 	/* Not all breadcrumbs are attached to physical HW */
+	intel_engine_mask_t	engine_mask;
 	struct intel_engine_cs *irq_engine;
+	bool	(*irq_enable)(struct intel_breadcrumbs *b);
+	void	(*irq_disable)(struct intel_breadcrumbs *b);
 };
 
 #endif /* __INTEL_BREADCRUMBS_TYPES__ */
drivers/gpu/drm/i915/gt/intel_engine.h +3 -0
index 923eaee627b3..e9e0657f847a 100644
@@ -212,6 +212,9 @@ void intel_engine_get_instdone(const struct intel_engine_cs *engine,
 
 void intel_engine_init_execlists(struct intel_engine_cs *engine);
 
+bool intel_engine_irq_enable(struct intel_engine_cs *engine);
+void intel_engine_irq_disable(struct intel_engine_cs *engine);
+
 static inline void __intel_engine_reset(struct intel_engine_cs *engine,
 					bool stalled)
 {
drivers/gpu/drm/i915/gt/intel_engine_cs.c +26 -2
index d13b1716c29e..69245670b8b0 100644
@@ -739,7 +739,7 @@ static int engine_setup_common(struct intel_engine_cs *engine)
 err_cmd_parser:
 	i915_sched_engine_put(engine->sched_engine);
 err_sched_engine:
-	intel_breadcrumbs_free(engine->breadcrumbs);
+	intel_breadcrumbs_put(engine->breadcrumbs);
 err_status:
 	cleanup_status_page(engine);
 	return err;
@@ -947,7 +947,7 @@ void intel_engine_cleanup_common(struct intel_engine_cs *engine)
 	GEM_BUG_ON(!list_empty(&engine->sched_engine->requests));
 
 	i915_sched_engine_put(engine->sched_engine);
-	intel_breadcrumbs_free(engine->breadcrumbs);
+	intel_breadcrumbs_put(engine->breadcrumbs);
 
 	intel_engine_fini_retire(engine);
 	intel_engine_cleanup_cmd_parser(engine);
@@ -1264,6 +1264,30 @@ bool intel_engines_are_idle(struct intel_gt *gt)
 	return true;
 }
 
+bool intel_engine_irq_enable(struct intel_engine_cs *engine)
+{
+	if (!engine->irq_enable)
+		return false;
+
+	/* Caller disables interrupts */
+	spin_lock(&engine->gt->irq_lock);
+	engine->irq_enable(engine);
+	spin_unlock(&engine->gt->irq_lock);
+
+	return true;
+}
+
+void intel_engine_irq_disable(struct intel_engine_cs *engine)
+{
+	if (!engine->irq_disable)
+		return;
+
+	/* Caller disables interrupts */
+	spin_lock(&engine->gt->irq_lock);
+	engine->irq_disable(engine);
+	spin_unlock(&engine->gt->irq_lock);
+}
+
 void intel_engines_reset_default_submission(struct intel_gt *gt)
 {
 	struct intel_engine_cs *engine;
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -1
index 1dc59e6c9a92..e7cb6a06db9d 100644
@@ -64,7 +64,6 @@ struct intel_gt;
 struct intel_ring;
 struct intel_uncore;
 
-typedef u8 intel_engine_mask_t;
 #define ALL_ENGINES ((intel_engine_mask_t)~0ul)
 
 struct intel_hw_status_page {
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -1
index 9cfb8800a0e6..c10ea6080752 100644
@@ -3419,7 +3419,7 @@ static void rcu_virtual_context_destroy(struct work_struct *wrk)
 	intel_context_fini(&ve->context);
 
 	if (ve->base.breadcrumbs)
-		intel_breadcrumbs_free(ve->base.breadcrumbs);
+		intel_breadcrumbs_put(ve->base.breadcrumbs);
 	if (ve->base.sched_engine)
 		i915_sched_engine_put(ve->base.sched_engine);
 	intel_engine_free_request_pool(&ve->base);
drivers/gpu/drm/i915/gt/mock_engine.c +2 -2
index 9203c766db80..fc5a65ab1937 100644
@@ -284,7 +284,7 @@ static void mock_engine_release(struct intel_engine_cs *engine)
 	GEM_BUG_ON(timer_pending(&mock->hw_delay));
 
 	i915_sched_engine_put(engine->sched_engine);
-	intel_breadcrumbs_free(engine->breadcrumbs);
+	intel_breadcrumbs_put(engine->breadcrumbs);
 
 	intel_context_unpin(engine->kernel_context);
 	intel_context_put(engine->kernel_context);
@@ -376,7 +376,7 @@ int mock_engine_init(struct intel_engine_cs *engine)
 	return 0;
 
 err_breadcrumbs:
-	intel_breadcrumbs_free(engine->breadcrumbs);
+	intel_breadcrumbs_put(engine->breadcrumbs);
 err_schedule:
 	i915_sched_engine_put(engine->sched_engine);
 	return -ENOMEM;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +62 -5

97 lines not on this page · raw message

Signature · 1 line
2.28.0
#98 lore raw

Re: [PATCH 30/47] drm/i915/guc: Direct all breadcrumbs for a class to single breadcrumbs

Quoted 158 lines · Matthew Brost

With GuC virtual engines the physical engine which a request executes and completes on isn't known to the i915. Therefore we can't attach a request to a physical engines breadcrumbs. To work around this we create a single breadcrumbs per engine class when using GuC submission and direct all physical engine interrupts to this breadcrumbs.

---
  drivers/gpu/drm/i915/gt/intel_breadcrumbs.c   | 41 +++++-------
  drivers/gpu/drm/i915/gt/intel_breadcrumbs.h   | 14 +++-
  .../gpu/drm/i915/gt/intel_breadcrumbs_types.h |  7 ++
  drivers/gpu/drm/i915/gt/intel_engine.h        |  3 +
  drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 28 +++++++-
  drivers/gpu/drm/i915/gt/intel_engine_types.h  |  1 -
  .../drm/i915/gt/intel_execlists_submission.c  |  2 +-
  drivers/gpu/drm/i915/gt/mock_engine.c         |  4 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 67 +++++++++++++++++--
  9 files changed, 131 insertions(+), 36 deletions(-)
drivers/gpu/drm/i915/gt/intel_breadcrumbs.c +17 -24
index 38cc42783dfb..2007dc6f6b99 100644
@@ -15,28 +15,14 @@
  #include "intel_gt_pm.h"
  #include "intel_gt_requests.h"
 
-static bool irq_enable(struct intel_engine_cs *engine)
+static bool irq_enable(struct intel_breadcrumbs *b)
  {
-	if (!engine->irq_enable)
-		return false;
-
-	/* Caller disables interrupts */
-	spin_lock(&engine->gt->irq_lock);
-	engine->irq_enable(engine);
-	spin_unlock(&engine->gt->irq_lock);
-
-	return true;
+	return intel_engine_irq_enable(b->irq_engine);
  }
 
-static void irq_disable(struct intel_engine_cs *engine)
+static void irq_disable(struct intel_breadcrumbs *b)
  {
-	if (!engine->irq_disable)
-		return;
-
-	/* Caller disables interrupts */
-	spin_lock(&engine->gt->irq_lock);
-	engine->irq_disable(engine);
-	spin_unlock(&engine->gt->irq_lock);
+	intel_engine_irq_disable(b->irq_engine);
  }
 
  static void __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
@@ -57,7 +43,7 @@ static void __intel_breadcrumbs_arm_irq(struct intel_breadcrumbs *b)
  	WRITE_ONCE(b->irq_armed, true);
 
  	/* Requests may have completed before we could enable the interrupt. */
-	if (!b->irq_enabled++ && irq_enable(b->irq_engine))
+	if (!b->irq_enabled++ && b->irq_enable(b))
  		irq_work_queue(&b->irq_work);
  }
 
@@ -76,7 +62,7 @@ static void __intel_breadcrumbs_disarm_irq(struct intel_breadcrumbs *b)
  {
  	GEM_BUG_ON(!b->irq_enabled);
  	if (!--b->irq_enabled)
-		irq_disable(b->irq_engine);
+		b->irq_disable(b);
 
  	WRITE_ONCE(b->irq_armed, false);
  	intel_gt_pm_put_async(b->irq_engine->gt);
@@ -281,7 +267,7 @@ intel_breadcrumbs_create(struct intel_engine_cs *irq_engine)
  	if (!b)
  		return NULL;
 
-	b->irq_engine = irq_engine;
+	kref_init(&b->ref);
 
  	spin_lock_init(&b->signalers_lock);
  	INIT_LIST_HEAD(&b->signalers);
@@ -290,6 +276,10 @@ intel_breadcrumbs_create(struct intel_engine_cs *irq_engine)
  	spin_lock_init(&b->irq_lock);
  	init_irq_work(&b->irq_work, signal_irq_work);
 
+	b->irq_engine = irq_engine;
+	b->irq_enable = irq_enable;
+	b->irq_disable = irq_disable;
+
  	return b;
  }
 
@@ -303,9 +293,9 @@ void intel_breadcrumbs_reset(struct intel_breadcrumbs *b)
  	spin_lock_irqsave(&b->irq_lock, flags);
 
  	if (b->irq_enabled)
-		irq_enable(b->irq_engine);
+		b->irq_enable(b);
  	else
-		irq_disable(b->irq_engine);
+		b->irq_disable(b);
 
  	spin_unlock_irqrestore(&b->irq_lock, flags);
  }
@@ -325,11 +315,14 @@ void __intel_breadcrumbs_park(struct intel_breadcrumbs *b)
  	}
  }
 
-void intel_breadcrumbs_free(struct intel_breadcrumbs *b)
+void intel_breadcrumbs_free(struct kref *kref)
  {
+	struct intel_breadcrumbs *b = container_of(kref, typeof(*b), ref);
+
  	irq_work_sync(&b->irq_work);
  	GEM_BUG_ON(!list_empty(&b->signalers));
  	GEM_BUG_ON(b->irq_armed);
+
  	kfree(b);
  }
 
drivers/gpu/drm/i915/gt/intel_breadcrumbs.h +13 -1
index 3ce5ce270b04..72105b74663d 100644
@@ -17,7 +17,7 @@ struct intel_breadcrumbs;
 
  struct intel_breadcrumbs *
  intel_breadcrumbs_create(struct intel_engine_cs *irq_engine);
-void intel_breadcrumbs_free(struct intel_breadcrumbs *b);
+void intel_breadcrumbs_free(struct kref *kref);
 
  void intel_breadcrumbs_reset(struct intel_breadcrumbs *b);
  void __intel_breadcrumbs_park(struct intel_breadcrumbs *b);
@@ -48,4 +48,16 @@ void i915_request_cancel_breadcrumb(struct i915_request *request);
  void intel_context_remove_breadcrumbs(struct intel_context *ce,
  				      struct intel_breadcrumbs *b);
 
+static inline struct intel_breadcrumbs *
+intel_breadcrumbs_get(struct intel_breadcrumbs *b)
+{
+	kref_get(&b->ref);
+	return b;
+}
+
+static inline void intel_breadcrumbs_put(struct intel_breadcrumbs *b)
+{
+	kref_put(&b->ref, intel_breadcrumbs_free);
+}
+
  #endif /* __INTEL_BREADCRUMBS__ */
drivers/gpu/drm/i915/gt/intel_breadcrumbs_types.h +0 -0
index 3a084ce8ff5e..a4e146684be8 100644

drivers/gpu/drm/i915/gt/intel_breadcrumbs_types.h line 15

Quoted 9 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs_types.h +2 -0
@@ -7,10 +7,13 @@
  #define __INTEL_BREADCRUMBS_TYPES__
 
  #include <linux/irq_work.h>
+#include <linux/kref.h>
  #include <linux/list.h>
  #include <linux/spinlock.h>
  #include <linux/types.h>
 
+typedef u8 intel_engine_mask_t;

Seems like the wrong place for this. Can it be moved to gt/intel_engine_types.h instead?

Quoted 95 lines
drivers/gpu/drm/i915/gt/intel_breadcrumbs_types.h +5 -0
+
  /*
   * Rather than have every client wait upon all user interrupts,
   * with the herd waking after every interrupt and each doing the
@@ -29,6 +32,7 @@
   * the overhead of waking that client is much preferred.
   */
  struct intel_breadcrumbs {
+	struct kref ref;
  	atomic_t active;
 
  	spinlock_t signalers_lock; /* protects the list of signalers */
@@ -42,7 +46,10 @@ struct intel_breadcrumbs {
  	bool irq_armed;
 
  	/* Not all breadcrumbs are attached to physical HW */
+	intel_engine_mask_t	engine_mask;
  	struct intel_engine_cs *irq_engine;
+	bool	(*irq_enable)(struct intel_breadcrumbs *b);
+	void	(*irq_disable)(struct intel_breadcrumbs *b);
  };
 
  #endif /* __INTEL_BREADCRUMBS_TYPES__ */
drivers/gpu/drm/i915/gt/intel_engine.h +3 -0
index 923eaee627b3..e9e0657f847a 100644
@@ -212,6 +212,9 @@ void intel_engine_get_instdone(const struct intel_engine_cs *engine,
 
  void intel_engine_init_execlists(struct intel_engine_cs *engine);
 
+bool intel_engine_irq_enable(struct intel_engine_cs *engine);
+void intel_engine_irq_disable(struct intel_engine_cs *engine);
+
  static inline void __intel_engine_reset(struct intel_engine_cs *engine,
  					bool stalled)
  {
drivers/gpu/drm/i915/gt/intel_engine_cs.c +26 -2
index d13b1716c29e..69245670b8b0 100644
@@ -739,7 +739,7 @@ static int engine_setup_common(struct intel_engine_cs *engine)
  err_cmd_parser:
  	i915_sched_engine_put(engine->sched_engine);
  err_sched_engine:
-	intel_breadcrumbs_free(engine->breadcrumbs);
+	intel_breadcrumbs_put(engine->breadcrumbs);
  err_status:
  	cleanup_status_page(engine);
  	return err;
@@ -947,7 +947,7 @@ void intel_engine_cleanup_common(struct intel_engine_cs *engine)
  	GEM_BUG_ON(!list_empty(&engine->sched_engine->requests));
 
  	i915_sched_engine_put(engine->sched_engine);
-	intel_breadcrumbs_free(engine->breadcrumbs);
+	intel_breadcrumbs_put(engine->breadcrumbs);
 
  	intel_engine_fini_retire(engine);
  	intel_engine_cleanup_cmd_parser(engine);
@@ -1264,6 +1264,30 @@ bool intel_engines_are_idle(struct intel_gt *gt)
  	return true;
  }
 
+bool intel_engine_irq_enable(struct intel_engine_cs *engine)
+{
+	if (!engine->irq_enable)
+		return false;
+
+	/* Caller disables interrupts */
+	spin_lock(&engine->gt->irq_lock);
+	engine->irq_enable(engine);
+	spin_unlock(&engine->gt->irq_lock);
+
+	return true;
+}
+
+void intel_engine_irq_disable(struct intel_engine_cs *engine)
+{
+	if (!engine->irq_disable)
+		return;
+
+	/* Caller disables interrupts */
+	spin_lock(&engine->gt->irq_lock);
+	engine->irq_disable(engine);
+	spin_unlock(&engine->gt->irq_lock);
+}
+
  void intel_engines_reset_default_submission(struct intel_gt *gt)
  {
  	struct intel_engine_cs *engine;
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
index 1dc59e6c9a92..e7cb6a06db9d 100644

drivers/gpu/drm/i915/gt/intel_engine_types.h struct intel_gt; · line 67

Quoted 4 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -1
@@ -64,7 +64,6 @@ struct intel_gt;
  struct intel_ring;
  struct intel_uncore;
 
-typedef u8 intel_engine_mask_t;

Oh.

The following fixes this for me:

drivers/gpu/drm/i915/gt/intel_breadcrumbs.h +0 -0
index 3ce5ce270b04..ac5cdd6ff3f4 100644
@@ -10,6 +10,7 @@

 #include <linux/irq_work.h>

 #include "intel_engine_types.h"
+#include "gt/intel_breadcrumbs_types.h"

 struct drm_printer;
 struct i915_request;

drivers/gpu/drm/i915/gt/intel_breadcrumbs_types.h +0 -0
index 3a084ce8ff5e..260ccd5c1573 100644
@@ -11,6 +11,8 @@

 #include <linux/spinlock.h>
 #include <linux/types.h>

+#include "gt/intel_engine_types.h"
+
 /*
  * Rather than have every client wait upon all user interrupts,
  * with the herd waking after every interrupt and each doing the

drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
index 1cb9c3b70b29..da15b8b3c1f7 100644
@@ -21,7 +21,6 @@

 #include "i915_pmu.h"
 #include "i915_priolist_types.h"
 #include "i915_selftest.h"
-#include "intel_breadcrumbs_types.h"
 #include "intel_sseu.h"
 #include "intel_timeline_types.h"
 #include "intel_uncore.h"

John.

Quoted 136 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
  #define ALL_ENGINES ((intel_engine_mask_t)~0ul)
 
  struct intel_hw_status_page {
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -1
index 9cfb8800a0e6..c10ea6080752 100644
@@ -3419,7 +3419,7 @@ static void rcu_virtual_context_destroy(struct work_struct *wrk)
  	intel_context_fini(&ve->context);
 
  	if (ve->base.breadcrumbs)
-		intel_breadcrumbs_free(ve->base.breadcrumbs);
+		intel_breadcrumbs_put(ve->base.breadcrumbs);
  	if (ve->base.sched_engine)
  		i915_sched_engine_put(ve->base.sched_engine);
  	intel_engine_free_request_pool(&ve->base);
drivers/gpu/drm/i915/gt/mock_engine.c +2 -2
index 9203c766db80..fc5a65ab1937 100644
@@ -284,7 +284,7 @@ static void mock_engine_release(struct intel_engine_cs *engine)
  	GEM_BUG_ON(timer_pending(&mock->hw_delay));
 
  	i915_sched_engine_put(engine->sched_engine);
-	intel_breadcrumbs_free(engine->breadcrumbs);
+	intel_breadcrumbs_put(engine->breadcrumbs);
 
  	intel_context_unpin(engine->kernel_context);
  	intel_context_put(engine->kernel_context);
@@ -376,7 +376,7 @@ int mock_engine_init(struct intel_engine_cs *engine)
  	return 0;
 
  err_breadcrumbs:
-	intel_breadcrumbs_free(engine->breadcrumbs);
+	intel_breadcrumbs_put(engine->breadcrumbs);
  err_schedule:
  	i915_sched_engine_put(engine->sched_engine);
  	return -ENOMEM;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +60 -5
index d1badd7137b7..83058df5ba01 100644
@@ -1074,6 +1074,9 @@ static void __guc_context_destroy(struct intel_context *ce)
  		struct guc_virtual_engine *ve =
  			container_of(ce, typeof(*ve), context);
 
+		if (ve->base.breadcrumbs)
+			intel_breadcrumbs_put(ve->base.breadcrumbs);
+
  		kfree(ve);
  	} else {
  		intel_context_free(ce);
@@ -1377,6 +1380,62 @@ static const struct intel_context_ops virtual_guc_context_ops = {
  	.get_sibling = guc_virtual_get_sibling,
  };
 
+static bool
+guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b)
+{
+	struct intel_engine_cs *sibling;
+	intel_engine_mask_t tmp, mask = b->engine_mask;
+	bool result = false;
+
+	for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
+		result |= intel_engine_irq_enable(sibling);
+
+	return result;
+}
+
+static void
+guc_irq_disable_breadcrumbs(struct intel_breadcrumbs *b)
+{
+	struct intel_engine_cs *sibling;
+	intel_engine_mask_t tmp, mask = b->engine_mask;
+
+	for_each_engine_masked(sibling, b->irq_engine->gt, mask, tmp)
+		intel_engine_irq_disable(sibling);
+}
+
+static void guc_init_breadcrumbs(struct intel_engine_cs *engine)
+{
+	int i;
+
+       /*
+        * In GuC submission mode we do not know which physical engine a request
+        * will be scheduled on, this creates a problem because the breadcrumb
+        * interrupt is per physical engine. To work around this we attach
+        * requests and direct all breadcrumb interrupts to the first instance
+        * of an engine per class. In addition all breadcrumb interrupts are
+	* enabled / disabled across an engine class in unison.
+        */
+	for (i = 0; i < MAX_ENGINE_INSTANCE; ++i) {
+		struct intel_engine_cs *sibling =
+			engine->gt->engine_class[engine->class][i];
+
+		if (sibling) {
+			if (engine->breadcrumbs != sibling->breadcrumbs) {
+				intel_breadcrumbs_put(engine->breadcrumbs);
+				engine->breadcrumbs =
+					intel_breadcrumbs_get(sibling->breadcrumbs);
+			}
+			break;
+		}
+	}
+
+	if (engine->breadcrumbs) {
+		engine->breadcrumbs->engine_mask |= engine->mask;
+		engine->breadcrumbs->irq_enable = guc_irq_enable_breadcrumbs;
+		engine->breadcrumbs->irq_disable = guc_irq_disable_breadcrumbs;
+	}
+}
+
  static void sanitize_hwsp(struct intel_engine_cs *engine)
  {
  	struct intel_timeline *tl;
@@ -1600,6 +1659,7 @@ int intel_guc_submission_setup(struct intel_engine_cs *engine)
 
  	guc_default_vfuncs(engine);
  	guc_default_irqs(engine);
+	guc_init_breadcrumbs(engine);
 
  	if (engine->class == RENDER_CLASS)
  		rcs_submission_override(engine);
@@ -1839,11 +1899,6 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  	ve->base.instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
  	ve->base.uabi_instance = I915_ENGINE_CLASS_INVALID_VIRTUAL;
  	ve->base.saturated = ALL_ENGINES;
-	ve->base.breadcrumbs = intel_breadcrumbs_create(&ve->base);
-	if (!ve->base.breadcrumbs) {
-		kfree(ve);
-		return ERR_PTR(-ENOMEM);
-	}
 
  	snprintf(ve->base.name, sizeof(ve->base.name), "virtual");
 

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count) · line 1954

Quoted 8 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -0
@@ -1892,6 +1947,8 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  				sibling->emit_fini_breadcrumb;
  			ve->base.emit_fini_breadcrumb_dw =
  				sibling->emit_fini_breadcrumb_dw;
+			ve->base.breadcrumbs =
+				intel_breadcrumbs_get(sibling->breadcrumbs);
 
  			ve->base.flags |= sibling->flags;
 

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#99 lore raw

PATCH 31/47 drm/i915/guc: Reset implementation for new GuC interface

In reply to Matthew Brost

Reset implementation for new GuC interface. This is the legacy reset implementation which is called when the i915 owns the engine hang check. Future patches will offload the engine hang check to GuC but we will continue to maintain this legacy path as a fallback and this code path is also required if the GuC dies.

With the new GuC interface it is not possible to reset individual engines - it is only possible to reset the GPU entirely. This patch forces an entire chip reset if any engine hangs.

---
 drivers/gpu/drm/i915/gt/intel_context.c       |   3 +
 drivers/gpu/drm/i915/gt/intel_context_types.h |   7 +
 drivers/gpu/drm/i915/gt/intel_engine_types.h  |   6 +
 .../drm/i915/gt/intel_execlists_submission.c  |  40 ++
 drivers/gpu/drm/i915/gt/intel_gt_pm.c         |   6 +-
 drivers/gpu/drm/i915/gt/intel_reset.c         |  18 +-
 .../gpu/drm/i915/gt/intel_ring_submission.c   |  22 +
 drivers/gpu/drm/i915/gt/mock_engine.c         |  31 +
 drivers/gpu/drm/i915/gt/uc/intel_guc.c        |  13 -
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   8 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 581 ++++++++++++++----
 drivers/gpu/drm/i915/gt/uc/intel_uc.c         |  39 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc.h         |   3 +
 drivers/gpu/drm/i915/i915_request.c           |  41 +-
 drivers/gpu/drm/i915/i915_request.h           |   2 +
 15 files changed, 649 insertions(+), 171 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index b24a1b7a3f88..2f01437056a8 100644
@@ -392,6 +392,9 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 	spin_lock_init(&ce->guc_state.lock);
 	INIT_LIST_HEAD(&ce->guc_state.fences);
 
+	spin_lock_init(&ce->guc_active.lock);
+	INIT_LIST_HEAD(&ce->guc_active.requests);
+
 	ce->guc_id = GUC_INVALID_LRC_ID;
 	INIT_LIST_HEAD(&ce->guc_id_link);
 
drivers/gpu/drm/i915/gt/intel_context_types.h +7 -0
index 6945963a31ba..b63c8cf7823b 100644
@@ -165,6 +165,13 @@ struct intel_context {
 		struct list_head fences;
 	} guc_state;
 
+	struct {
+		/** lock: protects everything in guc_active */
+		spinlock_t lock;
+		/** requests: active requests on this context */
+		struct list_head requests;
+	} guc_active;
+
 	/* GuC scheduling state that does not require a lock. */
 	atomic_t guc_sched_state_no_lock;
 
drivers/gpu/drm/i915/gt/intel_engine_types.h +6 -0
index e7cb6a06db9d..f9d264c008e8 100644
@@ -426,6 +426,12 @@ struct intel_engine_cs {
 
 	void		(*release)(struct intel_engine_cs *engine);
 
+	/*
+	 * Add / remove request from engine active tracking
+	 */
+	void		(*add_active_request)(struct i915_request *rq);
+	void		(*remove_active_request)(struct i915_request *rq);
+
 	struct intel_engine_execlists execlists;
 
 	/*
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +40 -0
index c10ea6080752..c301a2d088b1 100644
@@ -3118,6 +3118,42 @@ static void execlists_park(struct intel_engine_cs *engine)
 	cancel_timer(&engine->execlists.preempt);
 }
 
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&locked->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
 static bool can_preempt(struct intel_engine_cs *engine)
 {
 	if (GRAPHICS_VER(engine->i915) > 8)
@@ -3218,6 +3254,8 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
 	engine->cops = &execlists_context_ops;
 	engine->request_alloc = execlists_request_alloc;
 	engine->bump_serial = execlist_bump_serial;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
 
 	engine->reset.prepare = execlists_reset_prepare;
 	engine->reset.rewind = execlists_reset_rewind;
@@ -3912,6 +3950,8 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
 			 "v%dx%d", ve->base.class, count);
 		ve->base.context_size = sibling->context_size;
 
+		ve->base.add_active_request = sibling->add_active_request;
+		ve->base.remove_active_request = sibling->remove_active_request;
 		ve->base.emit_bb_start = sibling->emit_bb_start;
 		ve->base.emit_flush = sibling->emit_flush;
 		ve->base.emit_init_breadcrumb = sibling->emit_init_breadcrumb;
drivers/gpu/drm/i915/gt/intel_gt_pm.c +4 -2
index aef3084e8b16..463a6ae605a0 100644
@@ -174,8 +174,6 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
 	if (intel_gt_is_wedged(gt))
 		intel_gt_unset_wedged(gt);
 
-	intel_uc_sanitize(&gt->uc);
-
 	for_each_engine(engine, gt, id)
 		if (engine->reset.prepare)
 			engine->reset.prepare(engine);
@@ -191,6 +189,8 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
 			__intel_engine_reset(engine, false);
 	}
 
+	intel_uc_reset(&gt->uc, false);
+
 	for_each_engine(engine, gt, id)
 		if (engine->reset.finish)
 			engine->reset.finish(engine);
@@ -243,6 +243,8 @@ int intel_gt_resume(struct intel_gt *gt)
 		goto err_wedged;
 	}
 
+	intel_uc_reset_finish(&gt->uc);
+
 	intel_rps_enable(&gt->rps);
 	intel_llc_enable(&gt->llc);
 
drivers/gpu/drm/i915/gt/intel_reset.c +12 -6
index 72251638d4ea..2987282dff6d 100644
@@ -826,6 +826,8 @@ static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
 		__intel_engine_reset(engine, stalled_mask & engine->mask);
 	local_bh_enable();
 
+	intel_uc_reset(&gt->uc, true);
+
 	intel_ggtt_restore_fences(gt->ggtt);
 
 	return err;
@@ -850,6 +852,8 @@ static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
 		if (awake & engine->mask)
 			intel_engine_pm_put(engine);
 	}
+
+	intel_uc_reset_finish(&gt->uc);
 }
 
 static void nop_submit_request(struct i915_request *request)
@@ -903,6 +907,7 @@ static void __intel_gt_set_wedged(struct intel_gt *gt)
 	for_each_engine(engine, gt, id)
 		if (engine->reset.cancel)
 			engine->reset.cancel(engine);
+	intel_uc_cancel_requests(&gt->uc);
 	local_bh_enable();
 
 	reset_finish(gt, awake);
@@ -1191,6 +1196,9 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
 	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
 	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
 
+	if (intel_engine_uses_guc(engine))
+		return -ENODEV;
+
 	if (!intel_engine_pm_get_if_awake(engine))
 		return 0;
 
@@ -1201,13 +1209,10 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
 			   "Resetting %s for %s\n", engine->name, msg);
 	atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
 
-	if (intel_engine_uses_guc(engine))
-		ret = intel_guc_reset_engine(&engine->gt->uc.guc, engine);
-	else
-		ret = intel_gt_reset_engine(engine);
+	ret = intel_gt_reset_engine(engine);
 	if (ret) {
 		/* If we fail here, we expect to fallback to a global reset */
-		ENGINE_TRACE(engine, "Failed to reset, err: %d\n", ret);
+		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
 		goto out;
 	}
 
@@ -1341,7 +1346,8 @@ void intel_gt_handle_error(struct intel_gt *gt,
 	 * Try engine reset when available. We fall back to full reset if
 	 * single reset fails.
 	 */
-	if (intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
+	if (!intel_uc_uses_guc_submission(&gt->uc) &&
+	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
 		local_bh_disable();
 		for_each_engine_masked(engine, gt, engine_mask, tmp) {
 			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
drivers/gpu/drm/i915/gt/intel_ring_submission.c +22 -0
index e1506b280df1..99dcdc8fba12 100644
@@ -1049,6 +1049,25 @@ static void ring_bump_serial(struct intel_engine_cs *engine)
 	engine->serial++;
 }
 
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	spin_lock_irq(&rq->engine->sched_engine->lock);
+	list_del_init(&rq->sched.link);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&rq->engine->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
 static void setup_common(struct intel_engine_cs *engine)
 {
 	struct drm_i915_private *i915 = engine->i915;
@@ -1066,6 +1085,9 @@ static void setup_common(struct intel_engine_cs *engine)
 	engine->reset.cancel = reset_cancel;
 	engine->reset.finish = reset_finish;
 
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
+
 	engine->cops = &ring_context_ops;
 	engine->request_alloc = ring_request_alloc;
 	engine->bump_serial = ring_bump_serial;
drivers/gpu/drm/i915/gt/mock_engine.c +31 -0
index fc5a65ab1937..c12ff3a75ce6 100644
@@ -235,6 +235,35 @@ static void mock_submit_request(struct i915_request *request)
 	spin_unlock_irqrestore(&engine->hw_lock, flags);
 }
 
+static void mock_add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void mock_remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+	spin_unlock_irq(&locked->sched_engine->lock);
+}
+
+
 static void mock_reset_prepare(struct intel_engine_cs *engine)
 {
 }
@@ -327,6 +356,8 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
 	engine->base.emit_flush = mock_emit_flush;
 	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
 	engine->base.submit_request = mock_submit_request;
+	engine->base.add_active_request = mock_add_to_engine;
+	engine->base.remove_active_request = mock_remove_from_engine;
 
 	engine->base.reset.prepare = mock_reset_prepare;
 	engine->base.reset.rewind = mock_reset_rewind;
drivers/gpu/drm/i915/gt/uc/intel_guc.c +0 -13
index 6661dcb02239..9b09395b998f 100644
@@ -572,19 +572,6 @@ int intel_guc_suspend(struct intel_guc *guc)
 	return 0;
 }
 
-/**
- * intel_guc_reset_engine() - ask GuC to reset an engine
- * @guc:	intel_guc structure
- * @engine:	engine to be reset
- */
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine)
-{
-	/* XXX: to be implemented with submission interface rework */
-
-	return -ENODEV;
-}
-
 /**
  * intel_guc_resume() - notify GuC resuming from suspend state
  * @guc:	the guc
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 22eb1e9cca41..40c9868762d7 100644
@@ -242,14 +242,16 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
 
 int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout);
 
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine);
-
 int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
 					  const u32 *msg, u32 len);
 int intel_guc_sched_done_process_msg(struct intel_guc *guc,
 				     const u32 *msg, u32 len);
 
+void intel_guc_submission_reset_prepare(struct intel_guc *guc);
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled);
+void intel_guc_submission_reset_finish(struct intel_guc *guc);
+void intel_guc_submission_cancel_requests(struct intel_guc *guc);
+
 void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
 
 #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +472 -109

878 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/intel_uc.c +38 -1
index 6d8b9233214e..f0b02200aa01 100644
@@ -565,12 +565,49 @@ void intel_uc_reset_prepare(struct intel_uc *uc)
 {
 	struct intel_guc *guc = &uc->guc;
 
-	if (!intel_guc_is_ready(guc))
+
+	/* Nothing to do if GuC isn't supported */
+	if (!intel_uc_supports_guc(uc))
 		return;
 
+	/* Firmware expected to be running when this function is called */
+	if (!intel_guc_is_ready(guc))
+		goto sanitize;
+
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_prepare(guc);
+
+sanitize:
 	__uc_sanitize(uc);
 }
 
+void intel_uc_reset(struct intel_uc *uc, bool stalled)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset(guc, stalled);
+}
+
+void intel_uc_reset_finish(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware expected to be running when this function is called */
+	if (intel_guc_is_fw_running(guc) && intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_finish(guc);
+}
+
+void intel_uc_cancel_requests(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_cancel_requests(guc);
+}
+
 void intel_uc_runtime_suspend(struct intel_uc *uc)
 {
 	struct intel_guc *guc = &uc->guc;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +3 -0
index c4cef885e984..eaa3202192ac 100644
@@ -37,6 +37,9 @@ void intel_uc_driver_late_release(struct intel_uc *uc);
 void intel_uc_driver_remove(struct intel_uc *uc);
 void intel_uc_init_mmio(struct intel_uc *uc);
 void intel_uc_reset_prepare(struct intel_uc *uc);
+void intel_uc_reset(struct intel_uc *uc, bool stalled);
+void intel_uc_reset_finish(struct intel_uc *uc);
+void intel_uc_cancel_requests(struct intel_uc *uc);
 void intel_uc_suspend(struct intel_uc *uc);
 void intel_uc_runtime_suspend(struct intel_uc *uc);
 int intel_uc_resume(struct intel_uc *uc);
drivers/gpu/drm/i915/i915_request.c +4 -37

71 lines not on this page · raw message

drivers/gpu/drm/i915/i915_request.h +2 -0
index f870cd75a001..bcc6340c505e 100644
@@ -649,4 +649,6 @@ bool
 i915_request_active_engine(struct i915_request *rq,
 			   struct intel_engine_cs **active);
 
+void i915_request_notify_execute_cb_imm(struct i915_request *rq);
+
 #endif /* I915_REQUEST_H */
Signature · 1 line
2.28.0
#100 lore raw

Re: [PATCH 31/47] drm/i915/guc: Reset implementation for new GuC interface

Quoted 9 lines · Matthew Brost

Reset implementation for new GuC interface. This is the legacy reset implementation which is called when the i915 owns the engine hang check. Future patches will offload the engine hang check to GuC but we will continue to maintain this legacy path as a fallback and this code path is also required if the GuC dies.

With the new GuC interface it is not possible to reset individual engines - it is only possible to reset the GPU entirely. This patch forces an entire chip reset if any engine hangs.

There seems to be quite a lot more code being changed in the patch than is described above. Sure, it's all in order to support resets but there is a lot happening to request/context management, support for GuC submission enable/disable, etc. It feels like this patch really should be split into a couple of prep patches followed by the actual reset support. Plus see couple of minor comments below.

Quoted 518 lines
---
  drivers/gpu/drm/i915/gt/intel_context.c       |   3 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |   7 +
  drivers/gpu/drm/i915/gt/intel_engine_types.h  |   6 +
  .../drm/i915/gt/intel_execlists_submission.c  |  40 ++
  drivers/gpu/drm/i915/gt/intel_gt_pm.c         |   6 +-
  drivers/gpu/drm/i915/gt/intel_reset.c         |  18 +-
  .../gpu/drm/i915/gt/intel_ring_submission.c   |  22 +
  drivers/gpu/drm/i915/gt/mock_engine.c         |  31 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.c        |  13 -
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   8 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 581 ++++++++++++++----
  drivers/gpu/drm/i915/gt/uc/intel_uc.c         |  39 +-
  drivers/gpu/drm/i915/gt/uc/intel_uc.h         |   3 +
  drivers/gpu/drm/i915/i915_request.c           |  41 +-
  drivers/gpu/drm/i915/i915_request.h           |   2 +
  15 files changed, 649 insertions(+), 171 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index b24a1b7a3f88..2f01437056a8 100644
@@ -392,6 +392,9 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	spin_lock_init(&ce->guc_state.lock);
  	INIT_LIST_HEAD(&ce->guc_state.fences);
 
+	spin_lock_init(&ce->guc_active.lock);
+	INIT_LIST_HEAD(&ce->guc_active.requests);
+
  	ce->guc_id = GUC_INVALID_LRC_ID;
  	INIT_LIST_HEAD(&ce->guc_id_link);
 
drivers/gpu/drm/i915/gt/intel_context_types.h +7 -0
index 6945963a31ba..b63c8cf7823b 100644
@@ -165,6 +165,13 @@ struct intel_context {
  		struct list_head fences;
  	} guc_state;
 
+	struct {
+		/** lock: protects everything in guc_active */
+		spinlock_t lock;
+		/** requests: active requests on this context */
+		struct list_head requests;
+	} guc_active;
+
  	/* GuC scheduling state that does not require a lock. */
  	atomic_t guc_sched_state_no_lock;
 
drivers/gpu/drm/i915/gt/intel_engine_types.h +6 -0
index e7cb6a06db9d..f9d264c008e8 100644
@@ -426,6 +426,12 @@ struct intel_engine_cs {
 
  	void		(*release)(struct intel_engine_cs *engine);
 
+	/*
+	 * Add / remove request from engine active tracking
+	 */
+	void		(*add_active_request)(struct i915_request *rq);
+	void		(*remove_active_request)(struct i915_request *rq);
+
  	struct intel_engine_execlists execlists;
 
  	/*
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +40 -0
index c10ea6080752..c301a2d088b1 100644
@@ -3118,6 +3118,42 @@ static void execlists_park(struct intel_engine_cs *engine)
  	cancel_timer(&engine->execlists.preempt);
  }
 
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&locked->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static bool can_preempt(struct intel_engine_cs *engine)
  {
  	if (GRAPHICS_VER(engine->i915) > 8)
@@ -3218,6 +3254,8 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &execlists_context_ops;
  	engine->request_alloc = execlists_request_alloc;
  	engine->bump_serial = execlist_bump_serial;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
 
  	engine->reset.prepare = execlists_reset_prepare;
  	engine->reset.rewind = execlists_reset_rewind;
@@ -3912,6 +3950,8 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  			 "v%dx%d", ve->base.class, count);
  		ve->base.context_size = sibling->context_size;
 
+		ve->base.add_active_request = sibling->add_active_request;
+		ve->base.remove_active_request = sibling->remove_active_request;
  		ve->base.emit_bb_start = sibling->emit_bb_start;
  		ve->base.emit_flush = sibling->emit_flush;
  		ve->base.emit_init_breadcrumb = sibling->emit_init_breadcrumb;
drivers/gpu/drm/i915/gt/intel_gt_pm.c +4 -2
index aef3084e8b16..463a6ae605a0 100644
@@ -174,8 +174,6 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
  	if (intel_gt_is_wedged(gt))
  		intel_gt_unset_wedged(gt);
 
-	intel_uc_sanitize(&gt->uc);
-
  	for_each_engine(engine, gt, id)
  		if (engine->reset.prepare)
  			engine->reset.prepare(engine);
@@ -191,6 +189,8 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
  			__intel_engine_reset(engine, false);
  	}
 
+	intel_uc_reset(&gt->uc, false);
+
  	for_each_engine(engine, gt, id)
  		if (engine->reset.finish)
  			engine->reset.finish(engine);
@@ -243,6 +243,8 @@ int intel_gt_resume(struct intel_gt *gt)
  		goto err_wedged;
  	}
 
+	intel_uc_reset_finish(&gt->uc);
+
  	intel_rps_enable(&gt->rps);
  	intel_llc_enable(&gt->llc);
 
drivers/gpu/drm/i915/gt/intel_reset.c +12 -6
index 72251638d4ea..2987282dff6d 100644
@@ -826,6 +826,8 @@ static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
  		__intel_engine_reset(engine, stalled_mask & engine->mask);
  	local_bh_enable();
 
+	intel_uc_reset(&gt->uc, true);
+
  	intel_ggtt_restore_fences(gt->ggtt);
 
  	return err;
@@ -850,6 +852,8 @@ static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
  		if (awake & engine->mask)
  			intel_engine_pm_put(engine);
  	}
+
+	intel_uc_reset_finish(&gt->uc);
  }
 
  static void nop_submit_request(struct i915_request *request)
@@ -903,6 +907,7 @@ static void __intel_gt_set_wedged(struct intel_gt *gt)
  	for_each_engine(engine, gt, id)
  		if (engine->reset.cancel)
  			engine->reset.cancel(engine);
+	intel_uc_cancel_requests(&gt->uc);
  	local_bh_enable();
 
  	reset_finish(gt, awake);
@@ -1191,6 +1196,9 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
  	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
  	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
 
+	if (intel_engine_uses_guc(engine))
+		return -ENODEV;
+
  	if (!intel_engine_pm_get_if_awake(engine))
  		return 0;
 
@@ -1201,13 +1209,10 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
  			   "Resetting %s for %s\n", engine->name, msg);
  	atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
 
-	if (intel_engine_uses_guc(engine))
-		ret = intel_guc_reset_engine(&engine->gt->uc.guc, engine);
-	else
-		ret = intel_gt_reset_engine(engine);
+	ret = intel_gt_reset_engine(engine);
  	if (ret) {
  		/* If we fail here, we expect to fallback to a global reset */
-		ENGINE_TRACE(engine, "Failed to reset, err: %d\n", ret);
+		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
  		goto out;
  	}
 
@@ -1341,7 +1346,8 @@ void intel_gt_handle_error(struct intel_gt *gt,
  	 * Try engine reset when available. We fall back to full reset if
  	 * single reset fails.
  	 */
-	if (intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
+	if (!intel_uc_uses_guc_submission(&gt->uc) &&
+	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
  		local_bh_disable();
  		for_each_engine_masked(engine, gt, engine_mask, tmp) {
  			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
drivers/gpu/drm/i915/gt/intel_ring_submission.c +22 -0
index e1506b280df1..99dcdc8fba12 100644
@@ -1049,6 +1049,25 @@ static void ring_bump_serial(struct intel_engine_cs *engine)
  	engine->serial++;
  }
 
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	spin_lock_irq(&rq->engine->sched_engine->lock);
+	list_del_init(&rq->sched.link);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&rq->engine->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static void setup_common(struct intel_engine_cs *engine)
  {
  	struct drm_i915_private *i915 = engine->i915;
@@ -1066,6 +1085,9 @@ static void setup_common(struct intel_engine_cs *engine)
  	engine->reset.cancel = reset_cancel;
  	engine->reset.finish = reset_finish;
 
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
+
  	engine->cops = &ring_context_ops;
  	engine->request_alloc = ring_request_alloc;
  	engine->bump_serial = ring_bump_serial;
drivers/gpu/drm/i915/gt/mock_engine.c +31 -0
index fc5a65ab1937..c12ff3a75ce6 100644
@@ -235,6 +235,35 @@ static void mock_submit_request(struct i915_request *request)
  	spin_unlock_irqrestore(&engine->hw_lock, flags);
  }
 
+static void mock_add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void mock_remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+	spin_unlock_irq(&locked->sched_engine->lock);
+}
+
+
  static void mock_reset_prepare(struct intel_engine_cs *engine)
  {
  }
@@ -327,6 +356,8 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  	engine->base.emit_flush = mock_emit_flush;
  	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
  	engine->base.submit_request = mock_submit_request;
+	engine->base.add_active_request = mock_add_to_engine;
+	engine->base.remove_active_request = mock_remove_from_engine;
 
  	engine->base.reset.prepare = mock_reset_prepare;
  	engine->base.reset.rewind = mock_reset_rewind;
drivers/gpu/drm/i915/gt/uc/intel_guc.c +0 -13
index 6661dcb02239..9b09395b998f 100644
@@ -572,19 +572,6 @@ int intel_guc_suspend(struct intel_guc *guc)
  	return 0;
  }
 
-/**
- * intel_guc_reset_engine() - ask GuC to reset an engine
- * @guc:	intel_guc structure
- * @engine:	engine to be reset
- */
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine)
-{
-	/* XXX: to be implemented with submission interface rework */
-
-	return -ENODEV;
-}
-
  /**
   * intel_guc_resume() - notify GuC resuming from suspend state
   * @guc:	the guc
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 22eb1e9cca41..40c9868762d7 100644
@@ -242,14 +242,16 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
 
  int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout);
 
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine);
-
  int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  					  const u32 *msg, u32 len);
  int intel_guc_sched_done_process_msg(struct intel_guc *guc,
  				     const u32 *msg, u32 len);
 
+void intel_guc_submission_reset_prepare(struct intel_guc *guc);
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled);
+void intel_guc_submission_reset_finish(struct intel_guc *guc);
+void intel_guc_submission_cancel_requests(struct intel_guc *guc);
+
  void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
 
  #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +60 -15
index 83058df5ba01..b8c894ad8caf 100644
@@ -141,7 +141,7 @@ context_wait_for_deregister_to_register(struct intel_context *ce)
  static inline void
  set_context_wait_for_deregister_to_register(struct intel_context *ce)
  {
-	/* Only should be called from guc_lrc_desc_pin() */
+	/* Only should be called from guc_lrc_desc_pin() without lock */
  	ce->guc_state.sched_state |=
  		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
  }
@@ -241,15 +241,31 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
 
  static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
  {
+	guc->lrc_desc_pool_vaddr = NULL;
  	i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
  }
 
+static inline bool guc_submission_initialized(struct intel_guc *guc)
+{
+	return guc->lrc_desc_pool_vaddr != NULL;
+}
+
  static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
  {
-	struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+	if (likely(guc_submission_initialized(guc))) {
+		struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+		unsigned long flags;
 
-	memset(desc, 0, sizeof(*desc));
-	xa_erase_irq(&guc->context_lookup, id);
+		memset(desc, 0, sizeof(*desc));
+
+		/*
+		 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
+		 * the lower level functions directly.
+		 */
+		xa_lock_irqsave(&guc->context_lookup, flags);
+		__xa_erase(&guc->context_lookup, id);
+		xa_unlock_irqrestore(&guc->context_lookup, flags);
+	}
  }
 
  static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
@@ -260,7 +276,15 @@ static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
  static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
  					   struct intel_context *ce)
  {
-	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	unsigned long flags;
+
+	/*
+	 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
+	 * lower level functions directly.
+	 */
+	xa_lock_irqsave(&guc->context_lookup, flags);
+	__xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	xa_unlock_irqrestore(&guc->context_lookup, flags);
  }
 
  static int guc_submission_busy_loop(struct intel_guc* guc,
@@ -331,6 +355,8 @@ int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
  					interruptible, timeout);
  }
 
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);
+
  static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  {
  	int err;
@@ -338,11 +364,22 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	u32 action[3];
  	int len = 0;
  	u32 g2h_len_dw = 0;
-	bool enabled = context_enabled(ce);
+	bool enabled;
 
  	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
  	GEM_BUG_ON(context_guc_id_invalid(ce));
 
+	/*
+	 * Corner case where the GuC firmware was blown away and reloaded while
+	 * this context was pinned.
+	 */
+	if (unlikely(!lrc_desc_registered(guc, ce->guc_id))) {
+		err = guc_lrc_desc_pin(ce, false);
+		if (unlikely(err))
+			goto out;
+	}
+	enabled = context_enabled(ce);
+
  	if (!enabled) {
  		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
  		action[len++] = ce->guc_id;
@@ -365,6 +402,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  		intel_context_put(ce);
  	}
 
+out:
  	return err;
  }
 
@@ -419,15 +457,10 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  	if (submit) {
  		guc_set_lrc_tail(last);
  resubmit:
-		/*
-		 * We only check for -EBUSY here even though it is possible for
-		 * -EDEADLK to be returned. If -EDEADLK is returned, the GuC has
-		 * died and a full GPU needs to be done. The hangcheck will
-		 * eventually detect that the GuC has died and trigger this
-		 * reset so no need to handle -EDEADLK here.
-		 */
  		ret = guc_add_request(guc, last);
-		if (ret == -EBUSY) {
+		if (unlikely(ret == -EIO))
+			goto deadlk;
+		else if (ret == -EBUSY) {
  			tasklet_schedule(&sched_engine->tasklet);
  			guc->stalled_request = last;
  			return false;
@@ -437,6 +470,11 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
 
  	guc->stalled_request = NULL;
  	return submit;
+
+deadlk:
+	sched_engine->tasklet.callback = NULL;
+	tasklet_disable_nosync(&sched_engine->tasklet);
+	return false;
  }
 
  static void guc_submission_tasklet(struct tasklet_struct *t)
@@ -463,27 +501,165 @@ static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
  		intel_engine_signal_breadcrumbs(engine);
  }
 
-static void guc_reset_prepare(struct intel_engine_cs *engine)
+static void __guc_context_destroy(struct intel_context *ce);
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
+static void guc_signal_context_fence(struct intel_context *ce);
+
+static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	unsigned long index, flags;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir) · line 521

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	bool pending_disable, pending_enable, deregister, destroyed;
+
+	xa_for_each(&guc->context_lookup, index, ce) {
+		/* Flush context */
+		spin_lock_irqsave(&ce->guc_state.lock, flags);
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+
+		/*
+		 * Once we are at this point submission_disabled() is guaranteed
+		 * to visible to all callers who set the below flags (see above

to be visible

Quoted 76 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +74 -1
+		 * flush and flushes in reset_prepare). If submission_disabled()
+		 * is set, the caller shouldn't set these flags.
+		 */
+
+		destroyed = context_destroyed(ce);
+		pending_enable = context_pending_enable(ce);
+		pending_disable = context_pending_disable(ce);
+		deregister = context_wait_for_deregister_to_register(ce);
+		init_sched_state(ce);
+
+		if (pending_enable || destroyed || deregister) {
+			atomic_dec(&guc->outstanding_submission_g2h);
+			if (deregister)
+				guc_signal_context_fence(ce);
+			if (destroyed) {
+				release_guc_id(guc, ce);
+				__guc_context_destroy(ce);
+			}
+			if (pending_enable|| deregister)
+				intel_context_put(ce);
+		}
+
+		/* Not mutualy exclusive with above if statement. */
+		if (pending_disable) {
+			guc_signal_context_fence(ce);
+			intel_context_sched_disable_unpin(ce);
+			atomic_dec(&guc->outstanding_submission_g2h);
+			intel_context_put(ce);
+		}
+	}
+}
+
+static inline bool
+submission_disabled(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	return unlikely(!__tasklet_is_enabled(&sched_engine->tasklet));
+}
+
+static void disable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	if (__tasklet_is_enabled(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+		__tasklet_disable_sync_once(&sched_engine->tasklet);
+		sched_engine->tasklet.callback = NULL;
+	}
+}
+
+static void enable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->sched_engine->lock, flags);
+	sched_engine->tasklet.callback = guc_submission_tasklet;
+	wmb();
+	if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
+	    __tasklet_enable(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+
+		/* And kick in case we missed a new request submission. */
+		tasklet_hi_schedule(&sched_engine->tasklet);
+	}
+	spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
+}
+
+static void guc_flush_submissions(struct intel_guc *guc)
  {
-	ENGINE_TRACE(engine, "\n");
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sched_engine->lock, flags);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir) · line 606

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+void intel_guc_submission_reset_prepare(struct intel_guc *guc)
+{
+	int i;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;

Ew. Multi-line if without braces just looks broken even if one line is just a comment.

Quoted 184 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +137 -24
+
+	disable_submission(guc);
+	guc->interrupts.disable(guc);
+
+	/* Flush IRQ handler */
+	spin_lock_irq(&guc_to_gt(guc)->irq_lock);
+	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
+
+	guc_flush_submissions(guc);
 
  	/*
-	 * Prevent request submission to the hardware until we have
-	 * completed the reset in i915_gem_reset_finish(). If a request
-	 * is completed by one engine, it may then queue a request
-	 * to a second via its execlists->tasklet *just* as we are
-	 * calling engine->init_hw() and also writing the ELSP.
-	 * Turning off the execlists->tasklet until the reset is over
-	 * prevents the race.
-	 */
-	__tasklet_disable_sync_once(&engine->sched_engine->tasklet);
+	 * Handle any outstanding G2Hs before reset. Call IRQ handler directly
+	 * each pass as interrupt have been disabled. We always scrub for
+	 * outstanding G2H as it is possible for outstanding_submission_g2h to
+	 * be incremented after the context state update.
+ 	 */
+	for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
+		intel_guc_to_host_event_handler(guc);
+#define wait_for_reset(guc, wait_var) \
+		guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
+		do {
+			wait_for_reset(guc, &guc->outstanding_submission_g2h);
+		} while (!list_empty(&guc->ct.requests.incoming));
+	}
+	scrub_guc_desc_for_outstanding_g2h(guc);
  }
 
-static void guc_reset_state(struct intel_context *ce,
-			    struct intel_engine_cs *engine,
-			    u32 head,
-			    bool scrub)
+static struct intel_engine_cs *
+guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
  {
+	struct intel_engine_cs *engine;
+	intel_engine_mask_t tmp, mask = ve->mask;
+	unsigned int num_siblings = 0;
+
+	for_each_engine_masked(engine, ve->gt, mask, tmp)
+		if (num_siblings++ == sibling)
+			return engine;
+
+	return NULL;
+}
+
+static inline struct intel_engine_cs *
+__context_to_physical_engine(struct intel_context *ce)
+{
+	struct intel_engine_cs *engine = ce->engine;
+
+	if (intel_engine_is_virtual(engine))
+		engine = guc_virtual_get_sibling(engine, 0);
+
+	return engine;
+}
+
+static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
+{
+	struct intel_engine_cs *engine = __context_to_physical_engine(ce);
+
  	GEM_BUG_ON(!intel_context_is_pinned(ce));
 
  	/*
@@ -501,42 +677,147 @@ static void guc_reset_state(struct intel_context *ce,
  	lrc_update_regs(ce, engine, head);
  }
 
-static void guc_reset_rewind(struct intel_engine_cs *engine, bool stalled)
+static void guc_reset_nop(struct intel_engine_cs *engine)
  {
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_request *rq;
+}
+
+static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
+{
+}
+
+static void
+__unwind_incomplete_requests(struct intel_context *ce)
+{
+	struct i915_request *rq, *rn;
+	struct list_head *pl;
+	int prio = I915_PRIORITY_INVALID;
+	struct i915_sched_engine * const sched_engine =
+		ce->engine->sched_engine;
  	unsigned long flags;
 
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry_safe(rq, rn,
+				 &ce->guc_active.requests,
+				 sched.link) {
+		if (i915_request_completed(rq))
+			continue;
+
+		list_del_init(&rq->sched.link);
+		spin_unlock(&ce->guc_active.lock);
+
+		__i915_request_unsubmit(rq);
+
+		/* Push the request back into the queue for later resubmission. */
+		GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
+		if (rq_prio(rq) != prio) {
+			prio = rq_prio(rq);
+			pl = i915_sched_lookup_priolist(sched_engine, prio);
+		}
+		GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
+
+		list_add_tail(&rq->sched.link, pl);
+		set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
 
-	/* Push back any incomplete requests for replay after the reset. */
-	rq = execlists_unwind_incomplete_requests(execlists);
-	if (!rq)
-		goto out_unlock;
+		spin_lock(&ce->guc_active.lock);
+	}
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static struct i915_request *context_find_active_request(struct intel_context *ce)
+{
+	struct i915_request *rq, *active = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ce->guc_active.lock, flags);
+	list_for_each_entry_reverse(rq, &ce->guc_active.requests,
+				    sched.link) {
+		if (i915_request_completed(rq))
+			break;
+
+		active = rq;
+	}
+	spin_unlock_irqrestore(&ce->guc_active.lock, flags);
+
+	return active;
+}
+
+static void __guc_reset_context(struct intel_context *ce, bool stalled)
+{
+	struct i915_request *rq;
+	u32 head;
+
+	/*
+	 * GuC will implicitly mark the context as non-schedulable
+	 * when it sends the reset notification. Make sure our state
+	 * reflects this change. The context will be marked enabled
+	 * on resubmission.
+	 */
+	clr_context_enabled(ce);
+
+	rq = context_find_active_request(ce);
+	if (!rq) {
+		head = ce->ring->tail;
+		stalled = false;
+		goto out_replay;
+	}
 
  	if (!i915_request_started(rq))
  		stalled = false;
 
+	GEM_BUG_ON(i915_active_is_idle(&ce->active));
+	head = intel_ring_wrap(ce->ring, rq->head);
  	__i915_request_reset(rq, stalled);
-	guc_reset_state(rq->context, engine, rq->head, stalled);
 
-out_unlock:
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+out_replay:
+	guc_reset_state(ce, head, stalled);
+	__unwind_incomplete_requests(ce);
  }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_reset_state(struct intel_context *ce, · line 785

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -1
 
-static void guc_reset_cancel(struct intel_engine_cs *engine)
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
+{
+	struct intel_context *ce;
+	unsigned long index;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;

And again.

Quoted 80 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +34 -16
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			__guc_reset_context(ce, stalled);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);
+}
+
+static void guc_cancel_context_requests(struct intel_context *ce)
+{
+	struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
+	struct i915_request *rq;
+	unsigned long flags;
+
+	/* Mark all executing requests as skipped. */
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry(rq, &ce->guc_active.requests, sched.link)
+		i915_request_put(i915_request_mark_eio(rq));
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static void
+guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
  {
-	struct i915_sched_engine * const sched_engine = engine->sched_engine;
  	struct i915_request *rq, *rn;
  	struct rb_node *rb;
  	unsigned long flags;
 
  	/* Can be called during boot if GuC fails to load */
-	if (!engine->gt)
+	if (!sched_engine)
  		return;
 
-	ENGINE_TRACE(engine, "\n");
-
  	/*
  	 * Before we call engine->cancel_requests(), we should have exclusive
  	 * access to the submission state. This is arranged for us by the
@@ -553,21 +834,16 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	 */
  	spin_lock_irqsave(&sched_engine->lock, flags);
 
-	/* Mark all executing requests as skipped. */
-	list_for_each_entry(rq, &sched_engine->requests, sched.link) {
-		i915_request_set_error_once(rq, -EIO);
-		i915_request_mark_complete(rq);
-	}
-
  	/* Flush the queued requests to the timeline list (for retiring). */
  	while ((rb = rb_first_cached(&sched_engine->queue))) {
  		struct i915_priolist *p = to_priolist(rb);
 
  		priolist_for_each_request_consume(rq, rn, p) {
  			list_del_init(&rq->sched.link);
+
  			__i915_request_submit(rq);
-			dma_fence_set_error(&rq->fence, -EIO);
-			i915_request_mark_complete(rq);
+
+			i915_request_put(i915_request_mark_eio(rq));
  		}
 
  		rb_erase_cached(&p->node, &sched_engine->queue);
@@ -582,14 +858,38 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
 
-static void guc_reset_finish(struct intel_engine_cs *engine)
+void intel_guc_submission_cancel_requests(struct intel_guc *guc)
  {
-	if (__tasklet_enable(&engine->sched_engine->tasklet))
-		/* And kick in case we missed a new request submission. */
-		tasklet_hi_schedule(&engine->sched_engine->tasklet);
+	struct intel_context *ce;
+	unsigned long index;
+

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_reset_cancel(struct intel_engine_cs *engine) · line 873

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -2
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			guc_cancel_context_requests(ce);
 
-	ENGINE_TRACE(engine, "depth->%d\n",
-		     atomic_read(&engine->sched_engine->tasklet.count));
+	guc_cancel_sched_engine_requests(guc->sched_engine);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);

This function shares most of the code with 'intel_guc_submission_reset'. Can the xa clean be moved to a common helper?

John.

Quoted 548 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +132 -50
+}
+
+void intel_guc_submission_reset_finish(struct intel_guc *guc)
+{
+	/* Reset called during driver load or during wedge? */
+	if (unlikely(!guc_submission_initialized(guc) ||
+		     test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags)))
+		return;
+
+	/*
+	 * Technically possible for either of these values to be non-zero here,
+	 * but very unlikely + harmless. Regardless let's add a warn so we can
+	 * see in CI if this happens frequently / a precursor to taking down the
+	 * machine.
+	 */
+	GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
+	atomic_set(&guc->outstanding_submission_g2h, 0);
+
+	enable_submission(guc);
  }
 
  /*
@@ -656,6 +956,9 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
  	else
  		trace_i915_request_guc_submit(rq);
 
+	if (unlikely(ret == -EIO))
+		disable_submission(guc);
+
  	return ret;
  }
 
@@ -668,7 +971,8 @@ static void guc_submit_request(struct i915_request *rq)
  	/* Will be called from irq-context when using foreign fences. */
  	spin_lock_irqsave(&sched_engine->lock, flags);
 
-	if (guc->stalled_request || !i915_sched_engine_is_empty(sched_engine))
+	if (submission_disabled(guc) || guc->stalled_request ||
+	    !i915_sched_engine_is_empty(sched_engine))
  		queue_request(sched_engine, rq, rq_prio(rq));
  	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
  		tasklet_hi_schedule(&sched_engine->tasklet);
@@ -805,7 +1109,8 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
 
  static int __guc_action_register_context(struct intel_guc *guc,
  					 u32 guc_id,
-					 u32 offset)
+					 u32 offset,
+					 bool loop)
  {
  	u32 action[] = {
  		INTEL_GUC_ACTION_REGISTER_CONTEXT,
@@ -813,10 +1118,10 @@ static int __guc_action_register_context(struct intel_guc *guc,
  		offset,
  	};
 
-	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
+	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, loop);
  }
 
-static int register_context(struct intel_context *ce)
+static int register_context(struct intel_context *ce, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
  	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
@@ -824,11 +1129,12 @@ static int register_context(struct intel_context *ce)
 
  	trace_intel_context_register(ce);
 
-	return __guc_action_register_context(guc, ce->guc_id, offset);
+	return __guc_action_register_context(guc, ce->guc_id, offset, loop);
  }
 
  static int __guc_action_deregister_context(struct intel_guc *guc,
-					   u32 guc_id)
+					   u32 guc_id,
+					   bool loop)
  {
  	u32 action[] = {
  		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
@@ -836,16 +1142,16 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
  	};
 
  	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
-					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
+					G2H_LEN_DW_DEREGISTER_CONTEXT, loop);
  }
 
-static int deregister_context(struct intel_context *ce, u32 guc_id)
+static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
 
  	trace_intel_context_deregister(ce);
 
-	return __guc_action_deregister_context(guc, guc_id);
+	return __guc_action_deregister_context(guc, guc_id, loop);
  }
 
  static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
@@ -874,7 +1180,7 @@ static void guc_context_policy_init(struct intel_engine_cs *engine,
  	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
  }
 
-static int guc_lrc_desc_pin(struct intel_context *ce)
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  {
  	struct intel_runtime_pm *runtime_pm =
  		&ce->engine->gt->i915->runtime_pm;
@@ -920,18 +1226,44 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
  	 */
  	if (context_registered) {
  		trace_intel_context_steal_guc_id(ce);
-		set_context_wait_for_deregister_to_register(ce);
-		intel_context_get(ce);
+		if (!loop) {
+			set_context_wait_for_deregister_to_register(ce);
+			intel_context_get(ce);
+		} else {
+			bool disabled;
+			unsigned long flags;
+
+			/* Seal race with Reset */
+			spin_lock_irqsave(&ce->guc_state.lock, flags);
+			disabled = submission_disabled(guc);
+			if (likely(!disabled)) {
+				set_context_wait_for_deregister_to_register(ce);
+				intel_context_get(ce);
+			}
+			spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+			if (unlikely(disabled)) {
+				reset_lrc_desc(guc, desc_idx);
+				return 0;	/* Will get registered later */
+			}
+		}
 
  		/*
  		 * If stealing the guc_id, this ce has the same guc_id as the
  		 * context whos guc_id was stole.
  		 */
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = deregister_context(ce, ce->guc_id);
+			ret = deregister_context(ce, ce->guc_id, loop);
+		if (unlikely(ret == -EBUSY)) {
+			clr_context_wait_for_deregister_to_register(ce);
+			intel_context_put(ce);
+		}
  	} else {
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = register_context(ce);
+			ret = register_context(ce, loop);
+		if (unlikely(ret == -EBUSY))
+			reset_lrc_desc(guc, desc_idx);
+		else if (unlikely(ret == -ENODEV))
+			ret = 0;	/* Will get registered later */
  	}
 
  	return ret;
@@ -994,7 +1326,6 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
  	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
 
  	trace_intel_context_sched_disable(ce);
-	intel_context_get(ce);
 
  	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
  				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
@@ -1004,6 +1335,7 @@ static u16 prep_context_pending_disable(struct intel_context *ce)
  {
  	set_context_pending_disable(ce);
  	clr_context_enabled(ce);
+	intel_context_get(ce);
 
  	return ce->guc_id;
  }
@@ -1016,7 +1348,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	u16 guc_id;
  	intel_wakeref_t wakeref;
 
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		clr_context_enabled(ce);
  		goto unpin;
@@ -1034,6 +1366,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	 * request doesn't slip through the 'context_pending_disable' fence.
  	 */
  	if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
  		return;
  	}
  	guc_id = prep_context_pending_disable(ce);
@@ -1050,19 +1383,13 @@ static void guc_context_sched_disable(struct intel_context *ce)
 
  static inline void guc_lrc_desc_unpin(struct intel_context *ce)
  {
-	struct intel_engine_cs *engine = ce->engine;
-	struct intel_guc *guc = &engine->gt->uc.guc;
-	unsigned long flags;
+	struct intel_guc *guc = ce_to_guc(ce);
 
  	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
  	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
  	GEM_BUG_ON(context_enabled(ce));
 
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	set_context_destroyed(ce);
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-
-	deregister_context(ce, ce->guc_id);
+	deregister_context(ce, ce->guc_id, true);
  }
 
  static void __guc_context_destroy(struct intel_context *ce)
@@ -1090,13 +1417,15 @@ static void guc_context_destroy(struct kref *kref)
  	struct intel_guc *guc = &ce->engine->gt->uc.guc;
  	intel_wakeref_t wakeref;
  	unsigned long flags;
+	bool disabled;
 
  	/*
  	 * If the guc_id is invalid this context has been stolen and we can free
  	 * it immediately. Also can be freed immediately if the context is not
  	 * registered with the GuC.
  	 */
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) ||
+	    context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		release_guc_id(guc, ce);
  		__guc_context_destroy(ce);
@@ -1123,6 +1452,18 @@ static void guc_context_destroy(struct kref *kref)
  		list_del_init(&ce->guc_id_link);
  	spin_unlock_irqrestore(&guc->contexts_lock, flags);
 
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled))
+		set_context_destroyed(ce);
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
  	/*
  	 * We defer GuC context deregistration until the context is destroyed
  	 * in order to save on CTBs. With this optimization ideally we only need
@@ -1145,6 +1486,33 @@ static int guc_context_alloc(struct intel_context *ce)
  	return lrc_alloc(ce, ce->engine);
  }
 
+static void add_to_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock(&ce->guc_active.lock);
+	list_move_tail(&rq->sched.link, &ce->guc_active.requests);
+	spin_unlock(&ce->guc_active.lock);
+}
+
+static void remove_from_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock_irq(&ce->guc_active.lock);
+
+	list_del_init(&rq->sched.link);
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&ce->guc_active.lock);
+
+	atomic_dec(&ce->guc_id_ref);
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static const struct intel_context_ops guc_context_ops = {
  	.alloc = guc_context_alloc,
 
@@ -1183,8 +1551,6 @@ static void guc_signal_context_fence(struct intel_context *ce)
  {
  	unsigned long flags;
 
-	GEM_BUG_ON(!context_wait_for_deregister_to_register(ce));
-
  	spin_lock_irqsave(&ce->guc_state.lock, flags);
  	clr_context_wait_for_deregister_to_register(ce);
  	__guc_signal_context_fence(ce);
@@ -1193,8 +1559,9 @@ static void guc_signal_context_fence(struct intel_context *ce)
 
  static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
  {
-	return new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
-		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id);
+	return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
+		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id)) &&
+		!submission_disabled(ce_to_guc(ce));
  }
 
  static int guc_request_alloc(struct i915_request *rq)
@@ -1252,8 +1619,12 @@ static int guc_request_alloc(struct i915_request *rq)
  	if (unlikely(ret < 0))
  		return ret;;
  	if (context_needs_register(ce, !!ret)) {
-		ret = guc_lrc_desc_pin(ce);
+		ret = guc_lrc_desc_pin(ce, true);
  		if (unlikely(ret)) {	/* unwind */
+			if (ret == -EIO) {
+				disable_submission(guc);
+				goto out;	/* GPU will be reset */
+			}
  			atomic_dec(&ce->guc_id_ref);
  			unpin_guc_id(guc, ce);
  			return ret;
@@ -1290,20 +1661,6 @@ static int guc_request_alloc(struct i915_request *rq)
  	return 0;
  }
 
-static struct intel_engine_cs *
-guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
-{
-	struct intel_engine_cs *engine;
-	intel_engine_mask_t tmp, mask = ve->mask;
-	unsigned int num_siblings = 0;
-
-	for_each_engine_masked(engine, ve->gt, mask, tmp)
-		if (num_siblings++ == sibling)
-			return engine;
-
-	return NULL;
-}
-
  static int guc_virtual_context_pre_pin(struct intel_context *ce,
  				       struct i915_gem_ww_ctx *ww,
  				       void **vaddr)
@@ -1512,7 +1869,7 @@ static inline void guc_kernel_context_pin(struct intel_guc *guc,
  {
  	if (context_guc_id_invalid(ce))
  		pin_guc_id(guc, ce);
-	guc_lrc_desc_pin(ce);
+	guc_lrc_desc_pin(ce, true);
  }
 
  static inline void guc_init_lrc_mapping(struct intel_guc *guc)
@@ -1578,13 +1935,15 @@ static void guc_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &guc_context_ops;
  	engine->request_alloc = guc_request_alloc;
  	engine->bump_serial = guc_bump_serial;
+	engine->add_active_request = add_to_context;
+	engine->remove_active_request = remove_from_context;
 
  	engine->sched_engine->schedule = i915_schedule;
 
-	engine->reset.prepare = guc_reset_prepare;
-	engine->reset.rewind = guc_reset_rewind;
-	engine->reset.cancel = guc_reset_cancel;
-	engine->reset.finish = guc_reset_finish;
+	engine->reset.prepare = guc_reset_nop;
+	engine->reset.rewind = guc_rewind_nop;
+	engine->reset.cancel = guc_reset_nop;
+	engine->reset.finish = guc_reset_nop;
 
  	engine->emit_flush = gen8_emit_flush_xcs;
  	engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
@@ -1757,7 +2116,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		 * register this context.
  		 */
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			register_context(ce);
+			register_context(ce, true);
  		guc_signal_context_fence(ce);
  		intel_context_put(ce);
  	} else if (context_destroyed(ce)) {
@@ -1939,6 +2298,10 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  				 "v%dx%d", ve->base.class, count);
  			ve->base.context_size = sibling->context_size;
 
+			ve->base.add_active_request =
+				sibling->add_active_request;
+			ve->base.remove_active_request =
+				sibling->remove_active_request;
  			ve->base.emit_bb_start = sibling->emit_bb_start;
  			ve->base.emit_flush = sibling->emit_flush;
  			ve->base.emit_init_breadcrumb =
drivers/gpu/drm/i915/gt/uc/intel_uc.c +38 -1
index 6d8b9233214e..f0b02200aa01 100644
@@ -565,12 +565,49 @@ void intel_uc_reset_prepare(struct intel_uc *uc)
  {
  	struct intel_guc *guc = &uc->guc;
 
-	if (!intel_guc_is_ready(guc))
+
+	/* Nothing to do if GuC isn't supported */
+	if (!intel_uc_supports_guc(uc))
  		return;
 
+	/* Firmware expected to be running when this function is called */
+	if (!intel_guc_is_ready(guc))
+		goto sanitize;
+
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_prepare(guc);
+
+sanitize:
  	__uc_sanitize(uc);
  }
 
+void intel_uc_reset(struct intel_uc *uc, bool stalled)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset(guc, stalled);
+}
+
+void intel_uc_reset_finish(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware expected to be running when this function is called */
+	if (intel_guc_is_fw_running(guc) && intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_finish(guc);
+}
+
+void intel_uc_cancel_requests(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_cancel_requests(guc);
+}
+
  void intel_uc_runtime_suspend(struct intel_uc *uc)
  {
  	struct intel_guc *guc = &uc->guc;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +3 -0
index c4cef885e984..eaa3202192ac 100644
@@ -37,6 +37,9 @@ void intel_uc_driver_late_release(struct intel_uc *uc);
  void intel_uc_driver_remove(struct intel_uc *uc);
  void intel_uc_init_mmio(struct intel_uc *uc);
  void intel_uc_reset_prepare(struct intel_uc *uc);
+void intel_uc_reset(struct intel_uc *uc, bool stalled);
+void intel_uc_reset_finish(struct intel_uc *uc);
+void intel_uc_cancel_requests(struct intel_uc *uc);
  void intel_uc_suspend(struct intel_uc *uc);
  void intel_uc_runtime_suspend(struct intel_uc *uc);
  int intel_uc_resume(struct intel_uc *uc);
drivers/gpu/drm/i915/i915_request.c +4 -37
index dec5a35c9aa2..192784875a1d 100644
@@ -194,7 +194,7 @@ static bool irq_work_imm(struct irq_work *wrk)
  	return false;
  }
 
-static void __notify_execute_cb_imm(struct i915_request *rq)
+void i915_request_notify_execute_cb_imm(struct i915_request *rq)
  {
  	__notify_execute_cb(rq, irq_work_imm);
  }
@@ -268,37 +268,6 @@ i915_request_active_engine(struct i915_request *rq,
  	return ret;
  }
 
-
-static void remove_from_engine(struct i915_request *rq)
-{
-	struct intel_engine_cs *engine, *locked;
-
-	/*
-	 * Virtual engines complicate acquiring the engine timeline lock,
-	 * as their rq->engine pointer is not stable until under that
-	 * engine lock. The simple ploy we use is to take the lock then
-	 * check that the rq still belongs to the newly locked engine.
-	 */
-	locked = READ_ONCE(rq->engine);
-	spin_lock_irq(&locked->sched_engine->lock);
-	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
-		spin_unlock(&locked->sched_engine->lock);
-		spin_lock(&engine->sched_engine->lock);
-		locked = engine;
-	}
-	list_del_init(&rq->sched.link);
-
-	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
-
-	/* Prevent further __await_execution() registering a cb, then flush */
-	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
-
-	spin_unlock_irq(&locked->sched_engine->lock);
-
-	__notify_execute_cb_imm(rq);
-}
-
  static void __rq_init_watchdog(struct i915_request *rq)
  {
  	rq->watchdog.timer.function = NULL;
@@ -395,9 +364,7 @@ bool i915_request_retire(struct i915_request *rq)
  	 * after removing the breadcrumb and signaling it, so that we do not
  	 * inadvertently attach the breadcrumb to a completed request.
  	 */
-	if (!list_empty(&rq->sched.link))
-		remove_from_engine(rq);
-	atomic_dec(&rq->context->guc_id_ref);
+	rq->engine->remove_active_request(rq);
  	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
 
  	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
@@ -539,7 +506,7 @@ __await_execution(struct i915_request *rq,
  	if (llist_add(&cb->work.node.llist, &signal->execute_cb)) {
  		if (i915_request_is_active(signal) ||
  		    __request_in_flight(signal))
-			__notify_execute_cb_imm(signal);
+			i915_request_notify_execute_cb_imm(signal);
  	}
 
  	return 0;
@@ -676,7 +643,7 @@ bool __i915_request_submit(struct i915_request *request)
  	result = true;
 
  	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
-	list_move_tail(&request->sched.link, &engine->sched_engine->requests);
+	engine->add_active_request(request);
  active:
  	clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags);
  	set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
drivers/gpu/drm/i915/i915_request.h +2 -0
index f870cd75a001..bcc6340c505e 100644
@@ -649,4 +649,6 @@ bool
  i915_request_active_engine(struct i915_request *rq,
  			   struct intel_engine_cs **active);
 
+void i915_request_notify_execute_cb_imm(struct i915_request *rq);
+
  #endif /* I915_REQUEST_H */
#101 lore raw

Re: [PATCH 31/47] drm/i915/guc: Reset implementation for new GuC interface

Quoted 10 lines · John Harrison
Quoted 9 lines · Matthew Brost

Reset implementation for new GuC interface. This is the legacy reset implementation which is called when the i915 owns the engine hang check. Future patches will offload the engine hang check to GuC but we will continue to maintain this legacy path as a fallback and this code path is also required if the GuC dies.

With the new GuC interface it is not possible to reset individual engines - it is only possible to reset the GPU entirely. This patch forces an entire chip reset if any engine hangs.

Quoted 7 lines

There seems to be quite a lot more code being changed in the patch than is described above. Sure, it's all in order to support resets but there is a lot happening to request/context management, support for GuC submission enable/disable, etc. It feels like this patch really should be split into a couple of prep patches followed by the actual reset support. Plus see couple of minor comments below.

Yea, this is probably the most churned on patch we have as getting resets to full work isn't easy. I'll fix the below comments but I don't know if it worth spliting. Everything in the patch is required to get resets to work and I think it is better to have in a single patch so 'git blame' can give you the whole picture.

Matt

Quoted 1,341 lines
Quoted 474 lines
---
  drivers/gpu/drm/i915/gt/intel_context.c       |   3 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |   7 +
  drivers/gpu/drm/i915/gt/intel_engine_types.h  |   6 +
  .../drm/i915/gt/intel_execlists_submission.c  |  40 ++
  drivers/gpu/drm/i915/gt/intel_gt_pm.c         |   6 +-
  drivers/gpu/drm/i915/gt/intel_reset.c         |  18 +-
  .../gpu/drm/i915/gt/intel_ring_submission.c   |  22 +
  drivers/gpu/drm/i915/gt/mock_engine.c         |  31 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.c        |  13 -
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   8 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 581 ++++++++++++++----
  drivers/gpu/drm/i915/gt/uc/intel_uc.c         |  39 +-
  drivers/gpu/drm/i915/gt/uc/intel_uc.h         |   3 +
  drivers/gpu/drm/i915/i915_request.c           |  41 +-
  drivers/gpu/drm/i915/i915_request.h           |   2 +
  15 files changed, 649 insertions(+), 171 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index b24a1b7a3f88..2f01437056a8 100644
@@ -392,6 +392,9 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	spin_lock_init(&ce->guc_state.lock);
  	INIT_LIST_HEAD(&ce->guc_state.fences);
+	spin_lock_init(&ce->guc_active.lock);
+	INIT_LIST_HEAD(&ce->guc_active.requests);
+
  	ce->guc_id = GUC_INVALID_LRC_ID;
  	INIT_LIST_HEAD(&ce->guc_id_link);
drivers/gpu/drm/i915/gt/intel_context_types.h +7 -0
index 6945963a31ba..b63c8cf7823b 100644
@@ -165,6 +165,13 @@ struct intel_context {
  		struct list_head fences;
  	} guc_state;
+	struct {
+		/** lock: protects everything in guc_active */
+		spinlock_t lock;
+		/** requests: active requests on this context */
+		struct list_head requests;
+	} guc_active;
+
  	/* GuC scheduling state that does not require a lock. */
  	atomic_t guc_sched_state_no_lock;
drivers/gpu/drm/i915/gt/intel_engine_types.h +6 -0
index e7cb6a06db9d..f9d264c008e8 100644
@@ -426,6 +426,12 @@ struct intel_engine_cs {
  	void		(*release)(struct intel_engine_cs *engine);
+	/*
+	 * Add / remove request from engine active tracking
+	 */
+	void		(*add_active_request)(struct i915_request *rq);
+	void		(*remove_active_request)(struct i915_request *rq);
+
  	struct intel_engine_execlists execlists;
  	/*
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +40 -0
index c10ea6080752..c301a2d088b1 100644
@@ -3118,6 +3118,42 @@ static void execlists_park(struct intel_engine_cs *engine)
  	cancel_timer(&engine->execlists.preempt);
  }
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&locked->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static bool can_preempt(struct intel_engine_cs *engine)
  {
  	if (GRAPHICS_VER(engine->i915) > 8)
@@ -3218,6 +3254,8 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &execlists_context_ops;
  	engine->request_alloc = execlists_request_alloc;
  	engine->bump_serial = execlist_bump_serial;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
  	engine->reset.prepare = execlists_reset_prepare;
  	engine->reset.rewind = execlists_reset_rewind;
@@ -3912,6 +3950,8 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  			 "v%dx%d", ve->base.class, count);
  		ve->base.context_size = sibling->context_size;
+		ve->base.add_active_request = sibling->add_active_request;
+		ve->base.remove_active_request = sibling->remove_active_request;
  		ve->base.emit_bb_start = sibling->emit_bb_start;
  		ve->base.emit_flush = sibling->emit_flush;
  		ve->base.emit_init_breadcrumb = sibling->emit_init_breadcrumb;
drivers/gpu/drm/i915/gt/intel_gt_pm.c +4 -2
index aef3084e8b16..463a6ae605a0 100644
@@ -174,8 +174,6 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
  	if (intel_gt_is_wedged(gt))
  		intel_gt_unset_wedged(gt);
-	intel_uc_sanitize(&gt->uc);
-
  	for_each_engine(engine, gt, id)
  		if (engine->reset.prepare)
  			engine->reset.prepare(engine);
@@ -191,6 +189,8 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
  			__intel_engine_reset(engine, false);
  	}
+	intel_uc_reset(&gt->uc, false);
+
  	for_each_engine(engine, gt, id)
  		if (engine->reset.finish)
  			engine->reset.finish(engine);
@@ -243,6 +243,8 @@ int intel_gt_resume(struct intel_gt *gt)
  		goto err_wedged;
  	}
+	intel_uc_reset_finish(&gt->uc);
+
  	intel_rps_enable(&gt->rps);
  	intel_llc_enable(&gt->llc);
drivers/gpu/drm/i915/gt/intel_reset.c +12 -6
index 72251638d4ea..2987282dff6d 100644
@@ -826,6 +826,8 @@ static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
  		__intel_engine_reset(engine, stalled_mask & engine->mask);
  	local_bh_enable();
+	intel_uc_reset(&gt->uc, true);
+
  	intel_ggtt_restore_fences(gt->ggtt);
  	return err;
@@ -850,6 +852,8 @@ static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
  		if (awake & engine->mask)
  			intel_engine_pm_put(engine);
  	}
+
+	intel_uc_reset_finish(&gt->uc);
  }
  static void nop_submit_request(struct i915_request *request)
@@ -903,6 +907,7 @@ static void __intel_gt_set_wedged(struct intel_gt *gt)
  	for_each_engine(engine, gt, id)
  		if (engine->reset.cancel)
  			engine->reset.cancel(engine);
+	intel_uc_cancel_requests(&gt->uc);
  	local_bh_enable();
  	reset_finish(gt, awake);
@@ -1191,6 +1196,9 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
  	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
  	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
+	if (intel_engine_uses_guc(engine))
+		return -ENODEV;
+
  	if (!intel_engine_pm_get_if_awake(engine))
  		return 0;
@@ -1201,13 +1209,10 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
  			   "Resetting %s for %s\n", engine->name, msg);
  	atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
-	if (intel_engine_uses_guc(engine))
-		ret = intel_guc_reset_engine(&engine->gt->uc.guc, engine);
-	else
-		ret = intel_gt_reset_engine(engine);
+	ret = intel_gt_reset_engine(engine);
  	if (ret) {
  		/* If we fail here, we expect to fallback to a global reset */
-		ENGINE_TRACE(engine, "Failed to reset, err: %d\n", ret);
+		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
  		goto out;
  	}
@@ -1341,7 +1346,8 @@ void intel_gt_handle_error(struct intel_gt *gt,
  	 * Try engine reset when available. We fall back to full reset if
  	 * single reset fails.
  	 */
-	if (intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
+	if (!intel_uc_uses_guc_submission(&gt->uc) &&
+	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
  		local_bh_disable();
  		for_each_engine_masked(engine, gt, engine_mask, tmp) {
  			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
drivers/gpu/drm/i915/gt/intel_ring_submission.c +22 -0
index e1506b280df1..99dcdc8fba12 100644
@@ -1049,6 +1049,25 @@ static void ring_bump_serial(struct intel_engine_cs *engine)
  	engine->serial++;
  }
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	spin_lock_irq(&rq->engine->sched_engine->lock);
+	list_del_init(&rq->sched.link);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&rq->engine->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static void setup_common(struct intel_engine_cs *engine)
  {
  	struct drm_i915_private *i915 = engine->i915;
@@ -1066,6 +1085,9 @@ static void setup_common(struct intel_engine_cs *engine)
  	engine->reset.cancel = reset_cancel;
  	engine->reset.finish = reset_finish;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
+
  	engine->cops = &ring_context_ops;
  	engine->request_alloc = ring_request_alloc;
  	engine->bump_serial = ring_bump_serial;
drivers/gpu/drm/i915/gt/mock_engine.c +31 -0
index fc5a65ab1937..c12ff3a75ce6 100644
@@ -235,6 +235,35 @@ static void mock_submit_request(struct i915_request *request)
  	spin_unlock_irqrestore(&engine->hw_lock, flags);
  }
+static void mock_add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void mock_remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+	spin_unlock_irq(&locked->sched_engine->lock);
+}
+
+
  static void mock_reset_prepare(struct intel_engine_cs *engine)
  {
  }
@@ -327,6 +356,8 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  	engine->base.emit_flush = mock_emit_flush;
  	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
  	engine->base.submit_request = mock_submit_request;
+	engine->base.add_active_request = mock_add_to_engine;
+	engine->base.remove_active_request = mock_remove_from_engine;
  	engine->base.reset.prepare = mock_reset_prepare;
  	engine->base.reset.rewind = mock_reset_rewind;
drivers/gpu/drm/i915/gt/uc/intel_guc.c +0 -13
index 6661dcb02239..9b09395b998f 100644
@@ -572,19 +572,6 @@ int intel_guc_suspend(struct intel_guc *guc)
  	return 0;
  }
-/**
- * intel_guc_reset_engine() - ask GuC to reset an engine
- * @guc:	intel_guc structure
- * @engine:	engine to be reset
- */
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine)
-{
-	/* XXX: to be implemented with submission interface rework */
-
-	return -ENODEV;
-}
-
  /**
   * intel_guc_resume() - notify GuC resuming from suspend state
   * @guc:	the guc
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 22eb1e9cca41..40c9868762d7 100644
@@ -242,14 +242,16 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
  int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout);
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine);
-
  int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  					  const u32 *msg, u32 len);
  int intel_guc_sched_done_process_msg(struct intel_guc *guc,
  				     const u32 *msg, u32 len);
+void intel_guc_submission_reset_prepare(struct intel_guc *guc);
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled);
+void intel_guc_submission_reset_finish(struct intel_guc *guc);
+void intel_guc_submission_cancel_requests(struct intel_guc *guc);
+
  void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
  #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +60 -15
index 83058df5ba01..b8c894ad8caf 100644
@@ -141,7 +141,7 @@ context_wait_for_deregister_to_register(struct intel_context *ce)
  static inline void
  set_context_wait_for_deregister_to_register(struct intel_context *ce)
  {
-	/* Only should be called from guc_lrc_desc_pin() */
+	/* Only should be called from guc_lrc_desc_pin() without lock */
  	ce->guc_state.sched_state |=
  		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
  }
@@ -241,15 +241,31 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
  static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
  {
+	guc->lrc_desc_pool_vaddr = NULL;
  	i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
  }
+static inline bool guc_submission_initialized(struct intel_guc *guc)
+{
+	return guc->lrc_desc_pool_vaddr != NULL;
+}
+
  static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
  {
-	struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+	if (likely(guc_submission_initialized(guc))) {
+		struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+		unsigned long flags;
-	memset(desc, 0, sizeof(*desc));
-	xa_erase_irq(&guc->context_lookup, id);
+		memset(desc, 0, sizeof(*desc));
+
+		/*
+		 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
+		 * the lower level functions directly.
+		 */
+		xa_lock_irqsave(&guc->context_lookup, flags);
+		__xa_erase(&guc->context_lookup, id);
+		xa_unlock_irqrestore(&guc->context_lookup, flags);
+	}
  }
  static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
@@ -260,7 +276,15 @@ static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
  static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
  					   struct intel_context *ce)
  {
-	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	unsigned long flags;
+
+	/*
+	 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
+	 * lower level functions directly.
+	 */
+	xa_lock_irqsave(&guc->context_lookup, flags);
+	__xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	xa_unlock_irqrestore(&guc->context_lookup, flags);
  }
  static int guc_submission_busy_loop(struct intel_guc* guc,
@@ -331,6 +355,8 @@ int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
  					interruptible, timeout);
  }
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);
+
  static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  {
  	int err;
@@ -338,11 +364,22 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	u32 action[3];
  	int len = 0;
  	u32 g2h_len_dw = 0;
-	bool enabled = context_enabled(ce);
+	bool enabled;
  	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
  	GEM_BUG_ON(context_guc_id_invalid(ce));
+	/*
+	 * Corner case where the GuC firmware was blown away and reloaded while
+	 * this context was pinned.
+	 */
+	if (unlikely(!lrc_desc_registered(guc, ce->guc_id))) {
+		err = guc_lrc_desc_pin(ce, false);
+		if (unlikely(err))
+			goto out;
+	}
+	enabled = context_enabled(ce);
+
  	if (!enabled) {
  		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
  		action[len++] = ce->guc_id;
@@ -365,6 +402,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  		intel_context_put(ce);
  	}
+out:
  	return err;
  }
@@ -419,15 +457,10 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  	if (submit) {
  		guc_set_lrc_tail(last);
  resubmit:
-		/*
-		 * We only check for -EBUSY here even though it is possible for
-		 * -EDEADLK to be returned. If -EDEADLK is returned, the GuC has
-		 * died and a full GPU needs to be done. The hangcheck will
-		 * eventually detect that the GuC has died and trigger this
-		 * reset so no need to handle -EDEADLK here.
-		 */
  		ret = guc_add_request(guc, last);
-		if (ret == -EBUSY) {
+		if (unlikely(ret == -EIO))
+			goto deadlk;
+		else if (ret == -EBUSY) {
  			tasklet_schedule(&sched_engine->tasklet);
  			guc->stalled_request = last;
  			return false;
@@ -437,6 +470,11 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  	guc->stalled_request = NULL;
  	return submit;
+
+deadlk:
+	sched_engine->tasklet.callback = NULL;
+	tasklet_disable_nosync(&sched_engine->tasklet);
+	return false;
  }
  static void guc_submission_tasklet(struct tasklet_struct *t)
@@ -463,27 +501,165 @@ static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
  		intel_engine_signal_breadcrumbs(engine);
  }
-static void guc_reset_prepare(struct intel_engine_cs *engine)
+static void __guc_context_destroy(struct intel_context *ce);
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
+static void guc_signal_context_fence(struct intel_context *ce);
+
+static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	unsigned long index, flags;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir) · line 520

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	bool pending_disable, pending_enable, deregister, destroyed;
+
+	xa_for_each(&guc->context_lookup, index, ce) {
+		/* Flush context */
+		spin_lock_irqsave(&ce->guc_state.lock, flags);
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+
+		/*
+		 * Once we are at this point submission_disabled() is guaranteed
+		 * to visible to all callers who set the below flags (see above

to be visible

Quoted 76 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +74 -1
+		 * flush and flushes in reset_prepare). If submission_disabled()
+		 * is set, the caller shouldn't set these flags.
+		 */
+
+		destroyed = context_destroyed(ce);
+		pending_enable = context_pending_enable(ce);
+		pending_disable = context_pending_disable(ce);
+		deregister = context_wait_for_deregister_to_register(ce);
+		init_sched_state(ce);
+
+		if (pending_enable || destroyed || deregister) {
+			atomic_dec(&guc->outstanding_submission_g2h);
+			if (deregister)
+				guc_signal_context_fence(ce);
+			if (destroyed) {
+				release_guc_id(guc, ce);
+				__guc_context_destroy(ce);
+			}
+			if (pending_enable|| deregister)
+				intel_context_put(ce);
+		}
+
+		/* Not mutualy exclusive with above if statement. */
+		if (pending_disable) {
+			guc_signal_context_fence(ce);
+			intel_context_sched_disable_unpin(ce);
+			atomic_dec(&guc->outstanding_submission_g2h);
+			intel_context_put(ce);
+		}
+	}
+}
+
+static inline bool
+submission_disabled(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	return unlikely(!__tasklet_is_enabled(&sched_engine->tasklet));
+}
+
+static void disable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	if (__tasklet_is_enabled(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+		__tasklet_disable_sync_once(&sched_engine->tasklet);
+		sched_engine->tasklet.callback = NULL;
+	}
+}
+
+static void enable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->sched_engine->lock, flags);
+	sched_engine->tasklet.callback = guc_submission_tasklet;
+	wmb();
+	if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
+	    __tasklet_enable(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+
+		/* And kick in case we missed a new request submission. */
+		tasklet_hi_schedule(&sched_engine->tasklet);
+	}
+	spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
+}
+
+static void guc_flush_submissions(struct intel_guc *guc)
  {
-	ENGINE_TRACE(engine, "\n");
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sched_engine->lock, flags);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir) · line 606

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+void intel_guc_submission_reset_prepare(struct intel_guc *guc)
+{
+	int i;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;

Ew. Multi-line if without braces just looks broken even if one line is just a comment.

Quoted 174 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +137 -24
+
+	disable_submission(guc);
+	guc->interrupts.disable(guc);
+
+	/* Flush IRQ handler */
+	spin_lock_irq(&guc_to_gt(guc)->irq_lock);
+	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
+
+	guc_flush_submissions(guc);
  	/*
-	 * Prevent request submission to the hardware until we have
-	 * completed the reset in i915_gem_reset_finish(). If a request
-	 * is completed by one engine, it may then queue a request
-	 * to a second via its execlists->tasklet *just* as we are
-	 * calling engine->init_hw() and also writing the ELSP.
-	 * Turning off the execlists->tasklet until the reset is over
-	 * prevents the race.
-	 */
-	__tasklet_disable_sync_once(&engine->sched_engine->tasklet);
+	 * Handle any outstanding G2Hs before reset. Call IRQ handler directly
+	 * each pass as interrupt have been disabled. We always scrub for
+	 * outstanding G2H as it is possible for outstanding_submission_g2h to
+	 * be incremented after the context state update.
+ 	 */
+	for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
+		intel_guc_to_host_event_handler(guc);
+#define wait_for_reset(guc, wait_var) \
+		guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
+		do {
+			wait_for_reset(guc, &guc->outstanding_submission_g2h);
+		} while (!list_empty(&guc->ct.requests.incoming));
+	}
+	scrub_guc_desc_for_outstanding_g2h(guc);
  }
-static void guc_reset_state(struct intel_context *ce,
-			    struct intel_engine_cs *engine,
-			    u32 head,
-			    bool scrub)
+static struct intel_engine_cs *
+guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
  {
+	struct intel_engine_cs *engine;
+	intel_engine_mask_t tmp, mask = ve->mask;
+	unsigned int num_siblings = 0;
+
+	for_each_engine_masked(engine, ve->gt, mask, tmp)
+		if (num_siblings++ == sibling)
+			return engine;
+
+	return NULL;
+}
+
+static inline struct intel_engine_cs *
+__context_to_physical_engine(struct intel_context *ce)
+{
+	struct intel_engine_cs *engine = ce->engine;
+
+	if (intel_engine_is_virtual(engine))
+		engine = guc_virtual_get_sibling(engine, 0);
+
+	return engine;
+}
+
+static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
+{
+	struct intel_engine_cs *engine = __context_to_physical_engine(ce);
+
  	GEM_BUG_ON(!intel_context_is_pinned(ce));
  	/*
@@ -501,42 +677,147 @@ static void guc_reset_state(struct intel_context *ce,
  	lrc_update_regs(ce, engine, head);
  }
-static void guc_reset_rewind(struct intel_engine_cs *engine, bool stalled)
+static void guc_reset_nop(struct intel_engine_cs *engine)
  {
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_request *rq;
+}
+
+static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
+{
+}
+
+static void
+__unwind_incomplete_requests(struct intel_context *ce)
+{
+	struct i915_request *rq, *rn;
+	struct list_head *pl;
+	int prio = I915_PRIORITY_INVALID;
+	struct i915_sched_engine * const sched_engine =
+		ce->engine->sched_engine;
  	unsigned long flags;
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry_safe(rq, rn,
+				 &ce->guc_active.requests,
+				 sched.link) {
+		if (i915_request_completed(rq))
+			continue;
+
+		list_del_init(&rq->sched.link);
+		spin_unlock(&ce->guc_active.lock);
+
+		__i915_request_unsubmit(rq);
+
+		/* Push the request back into the queue for later resubmission. */
+		GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
+		if (rq_prio(rq) != prio) {
+			prio = rq_prio(rq);
+			pl = i915_sched_lookup_priolist(sched_engine, prio);
+		}
+		GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
+
+		list_add_tail(&rq->sched.link, pl);
+		set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-	/* Push back any incomplete requests for replay after the reset. */
-	rq = execlists_unwind_incomplete_requests(execlists);
-	if (!rq)
-		goto out_unlock;
+		spin_lock(&ce->guc_active.lock);
+	}
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static struct i915_request *context_find_active_request(struct intel_context *ce)
+{
+	struct i915_request *rq, *active = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ce->guc_active.lock, flags);
+	list_for_each_entry_reverse(rq, &ce->guc_active.requests,
+				    sched.link) {
+		if (i915_request_completed(rq))
+			break;
+
+		active = rq;
+	}
+	spin_unlock_irqrestore(&ce->guc_active.lock, flags);
+
+	return active;
+}
+
+static void __guc_reset_context(struct intel_context *ce, bool stalled)
+{
+	struct i915_request *rq;
+	u32 head;
+
+	/*
+	 * GuC will implicitly mark the context as non-schedulable
+	 * when it sends the reset notification. Make sure our state
+	 * reflects this change. The context will be marked enabled
+	 * on resubmission.
+	 */
+	clr_context_enabled(ce);
+
+	rq = context_find_active_request(ce);
+	if (!rq) {
+		head = ce->ring->tail;
+		stalled = false;
+		goto out_replay;
+	}
  	if (!i915_request_started(rq))
  		stalled = false;
+	GEM_BUG_ON(i915_active_is_idle(&ce->active));
+	head = intel_ring_wrap(ce->ring, rq->head);
  	__i915_request_reset(rq, stalled);
-	guc_reset_state(rq->context, engine, rq->head, stalled);
-out_unlock:
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+out_replay:
+	guc_reset_state(ce, head, stalled);
+	__unwind_incomplete_requests(ce);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_reset_state(struct intel_context *ce, · line 778

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -1
  }
-static void guc_reset_cancel(struct intel_engine_cs *engine)
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
+{
+	struct intel_context *ce;
+	unsigned long index;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;

And again.

Quoted 73 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +33 -16
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			__guc_reset_context(ce, stalled);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);
+}
+
+static void guc_cancel_context_requests(struct intel_context *ce)
+{
+	struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
+	struct i915_request *rq;
+	unsigned long flags;
+
+	/* Mark all executing requests as skipped. */
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry(rq, &ce->guc_active.requests, sched.link)
+		i915_request_put(i915_request_mark_eio(rq));
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static void
+guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
  {
-	struct i915_sched_engine * const sched_engine = engine->sched_engine;
  	struct i915_request *rq, *rn;
  	struct rb_node *rb;
  	unsigned long flags;
  	/* Can be called during boot if GuC fails to load */
-	if (!engine->gt)
+	if (!sched_engine)
  		return;
-	ENGINE_TRACE(engine, "\n");
-
  	/*
  	 * Before we call engine->cancel_requests(), we should have exclusive
  	 * access to the submission state. This is arranged for us by the
@@ -553,21 +834,16 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	 */
  	spin_lock_irqsave(&sched_engine->lock, flags);
-	/* Mark all executing requests as skipped. */
-	list_for_each_entry(rq, &sched_engine->requests, sched.link) {
-		i915_request_set_error_once(rq, -EIO);
-		i915_request_mark_complete(rq);
-	}
-
  	/* Flush the queued requests to the timeline list (for retiring). */
  	while ((rb = rb_first_cached(&sched_engine->queue))) {
  		struct i915_priolist *p = to_priolist(rb);
  		priolist_for_each_request_consume(rq, rn, p) {
  			list_del_init(&rq->sched.link);
+
  			__i915_request_submit(rq);
-			dma_fence_set_error(&rq->fence, -EIO);
-			i915_request_mark_complete(rq);
+
+			i915_request_put(i915_request_mark_eio(rq));
  		}
  		rb_erase_cached(&p->node, &sched_engine->queue);
@@ -582,14 +858,38 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
-static void guc_reset_finish(struct intel_engine_cs *engine)
+void intel_guc_submission_cancel_requests(struct intel_guc *guc)
  {
-	if (__tasklet_enable(&engine->sched_engine->tasklet))
-		/* And kick in case we missed a new request submission. */
-		tasklet_hi_schedule(&engine->sched_engine->tasklet);
+	struct intel_context *ce;
+	unsigned long index;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_reset_cancel(struct intel_engine_cs *engine) · line 871

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -2
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			guc_cancel_context_requests(ce);
-	ENGINE_TRACE(engine, "depth->%d\n",
-		     atomic_read(&engine->sched_engine->tasklet.count));
+	guc_cancel_sched_engine_requests(guc->sched_engine);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);

This function shares most of the code with 'intel_guc_submission_reset'. Can the xa clean be moved to a common helper?

John.

Quoted 492 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +132 -50
+}
+
+void intel_guc_submission_reset_finish(struct intel_guc *guc)
+{
+	/* Reset called during driver load or during wedge? */
+	if (unlikely(!guc_submission_initialized(guc) ||
+		     test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags)))
+		return;
+
+	/*
+	 * Technically possible for either of these values to be non-zero here,
+	 * but very unlikely + harmless. Regardless let's add a warn so we can
+	 * see in CI if this happens frequently / a precursor to taking down the
+	 * machine.
+	 */
+	GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
+	atomic_set(&guc->outstanding_submission_g2h, 0);
+
+	enable_submission(guc);
  }
  /*
@@ -656,6 +956,9 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
  	else
  		trace_i915_request_guc_submit(rq);
+	if (unlikely(ret == -EIO))
+		disable_submission(guc);
+
  	return ret;
  }
@@ -668,7 +971,8 @@ static void guc_submit_request(struct i915_request *rq)
  	/* Will be called from irq-context when using foreign fences. */
  	spin_lock_irqsave(&sched_engine->lock, flags);
-	if (guc->stalled_request || !i915_sched_engine_is_empty(sched_engine))
+	if (submission_disabled(guc) || guc->stalled_request ||
+	    !i915_sched_engine_is_empty(sched_engine))
  		queue_request(sched_engine, rq, rq_prio(rq));
  	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
  		tasklet_hi_schedule(&sched_engine->tasklet);
@@ -805,7 +1109,8 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  static int __guc_action_register_context(struct intel_guc *guc,
  					 u32 guc_id,
-					 u32 offset)
+					 u32 offset,
+					 bool loop)
  {
  	u32 action[] = {
  		INTEL_GUC_ACTION_REGISTER_CONTEXT,
@@ -813,10 +1118,10 @@ static int __guc_action_register_context(struct intel_guc *guc,
  		offset,
  	};
-	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
+	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, loop);
  }
-static int register_context(struct intel_context *ce)
+static int register_context(struct intel_context *ce, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
  	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
@@ -824,11 +1129,12 @@ static int register_context(struct intel_context *ce)
  	trace_intel_context_register(ce);
-	return __guc_action_register_context(guc, ce->guc_id, offset);
+	return __guc_action_register_context(guc, ce->guc_id, offset, loop);
  }
  static int __guc_action_deregister_context(struct intel_guc *guc,
-					   u32 guc_id)
+					   u32 guc_id,
+					   bool loop)
  {
  	u32 action[] = {
  		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
@@ -836,16 +1142,16 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
  	};
  	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
-					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
+					G2H_LEN_DW_DEREGISTER_CONTEXT, loop);
  }
-static int deregister_context(struct intel_context *ce, u32 guc_id)
+static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
  	trace_intel_context_deregister(ce);
-	return __guc_action_deregister_context(guc, guc_id);
+	return __guc_action_deregister_context(guc, guc_id, loop);
  }
  static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
@@ -874,7 +1180,7 @@ static void guc_context_policy_init(struct intel_engine_cs *engine,
  	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
  }
-static int guc_lrc_desc_pin(struct intel_context *ce)
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  {
  	struct intel_runtime_pm *runtime_pm =
  		&ce->engine->gt->i915->runtime_pm;
@@ -920,18 +1226,44 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
  	 */
  	if (context_registered) {
  		trace_intel_context_steal_guc_id(ce);
-		set_context_wait_for_deregister_to_register(ce);
-		intel_context_get(ce);
+		if (!loop) {
+			set_context_wait_for_deregister_to_register(ce);
+			intel_context_get(ce);
+		} else {
+			bool disabled;
+			unsigned long flags;
+
+			/* Seal race with Reset */
+			spin_lock_irqsave(&ce->guc_state.lock, flags);
+			disabled = submission_disabled(guc);
+			if (likely(!disabled)) {
+				set_context_wait_for_deregister_to_register(ce);
+				intel_context_get(ce);
+			}
+			spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+			if (unlikely(disabled)) {
+				reset_lrc_desc(guc, desc_idx);
+				return 0;	/* Will get registered later */
+			}
+		}
  		/*
  		 * If stealing the guc_id, this ce has the same guc_id as the
  		 * context whos guc_id was stole.
  		 */
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = deregister_context(ce, ce->guc_id);
+			ret = deregister_context(ce, ce->guc_id, loop);
+		if (unlikely(ret == -EBUSY)) {
+			clr_context_wait_for_deregister_to_register(ce);
+			intel_context_put(ce);
+		}
  	} else {
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = register_context(ce);
+			ret = register_context(ce, loop);
+		if (unlikely(ret == -EBUSY))
+			reset_lrc_desc(guc, desc_idx);
+		else if (unlikely(ret == -ENODEV))
+			ret = 0;	/* Will get registered later */
  	}
  	return ret;
@@ -994,7 +1326,6 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
  	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
  	trace_intel_context_sched_disable(ce);
-	intel_context_get(ce);
  	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
  				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
@@ -1004,6 +1335,7 @@ static u16 prep_context_pending_disable(struct intel_context *ce)
  {
  	set_context_pending_disable(ce);
  	clr_context_enabled(ce);
+	intel_context_get(ce);
  	return ce->guc_id;
  }
@@ -1016,7 +1348,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	u16 guc_id;
  	intel_wakeref_t wakeref;
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		clr_context_enabled(ce);
  		goto unpin;
@@ -1034,6 +1366,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	 * request doesn't slip through the 'context_pending_disable' fence.
  	 */
  	if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
  		return;
  	}
  	guc_id = prep_context_pending_disable(ce);
@@ -1050,19 +1383,13 @@ static void guc_context_sched_disable(struct intel_context *ce)
  static inline void guc_lrc_desc_unpin(struct intel_context *ce)
  {
-	struct intel_engine_cs *engine = ce->engine;
-	struct intel_guc *guc = &engine->gt->uc.guc;
-	unsigned long flags;
+	struct intel_guc *guc = ce_to_guc(ce);
  	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
  	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
  	GEM_BUG_ON(context_enabled(ce));
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	set_context_destroyed(ce);
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-
-	deregister_context(ce, ce->guc_id);
+	deregister_context(ce, ce->guc_id, true);
  }
  static void __guc_context_destroy(struct intel_context *ce)
@@ -1090,13 +1417,15 @@ static void guc_context_destroy(struct kref *kref)
  	struct intel_guc *guc = &ce->engine->gt->uc.guc;
  	intel_wakeref_t wakeref;
  	unsigned long flags;
+	bool disabled;
  	/*
  	 * If the guc_id is invalid this context has been stolen and we can free
  	 * it immediately. Also can be freed immediately if the context is not
  	 * registered with the GuC.
  	 */
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) ||
+	    context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		release_guc_id(guc, ce);
  		__guc_context_destroy(ce);
@@ -1123,6 +1452,18 @@ static void guc_context_destroy(struct kref *kref)
  		list_del_init(&ce->guc_id_link);
  	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled))
+		set_context_destroyed(ce);
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
  	/*
  	 * We defer GuC context deregistration until the context is destroyed
  	 * in order to save on CTBs. With this optimization ideally we only need
@@ -1145,6 +1486,33 @@ static int guc_context_alloc(struct intel_context *ce)
  	return lrc_alloc(ce, ce->engine);
  }
+static void add_to_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock(&ce->guc_active.lock);
+	list_move_tail(&rq->sched.link, &ce->guc_active.requests);
+	spin_unlock(&ce->guc_active.lock);
+}
+
+static void remove_from_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock_irq(&ce->guc_active.lock);
+
+	list_del_init(&rq->sched.link);
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&ce->guc_active.lock);
+
+	atomic_dec(&ce->guc_id_ref);
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static const struct intel_context_ops guc_context_ops = {
  	.alloc = guc_context_alloc,
@@ -1183,8 +1551,6 @@ static void guc_signal_context_fence(struct intel_context *ce)
  {
  	unsigned long flags;
-	GEM_BUG_ON(!context_wait_for_deregister_to_register(ce));
-
  	spin_lock_irqsave(&ce->guc_state.lock, flags);
  	clr_context_wait_for_deregister_to_register(ce);
  	__guc_signal_context_fence(ce);
@@ -1193,8 +1559,9 @@ static void guc_signal_context_fence(struct intel_context *ce)
  static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
  {
-	return new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
-		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id);
+	return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
+		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id)) &&
+		!submission_disabled(ce_to_guc(ce));
  }
  static int guc_request_alloc(struct i915_request *rq)
@@ -1252,8 +1619,12 @@ static int guc_request_alloc(struct i915_request *rq)
  	if (unlikely(ret < 0))
  		return ret;;
  	if (context_needs_register(ce, !!ret)) {
-		ret = guc_lrc_desc_pin(ce);
+		ret = guc_lrc_desc_pin(ce, true);
  		if (unlikely(ret)) {	/* unwind */
+			if (ret == -EIO) {
+				disable_submission(guc);
+				goto out;	/* GPU will be reset */
+			}
  			atomic_dec(&ce->guc_id_ref);
  			unpin_guc_id(guc, ce);
  			return ret;
@@ -1290,20 +1661,6 @@ static int guc_request_alloc(struct i915_request *rq)
  	return 0;
  }
-static struct intel_engine_cs *
-guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
-{
-	struct intel_engine_cs *engine;
-	intel_engine_mask_t tmp, mask = ve->mask;
-	unsigned int num_siblings = 0;
-
-	for_each_engine_masked(engine, ve->gt, mask, tmp)
-		if (num_siblings++ == sibling)
-			return engine;
-
-	return NULL;
-}
-
  static int guc_virtual_context_pre_pin(struct intel_context *ce,
  				       struct i915_gem_ww_ctx *ww,
  				       void **vaddr)
@@ -1512,7 +1869,7 @@ static inline void guc_kernel_context_pin(struct intel_guc *guc,
  {
  	if (context_guc_id_invalid(ce))
  		pin_guc_id(guc, ce);
-	guc_lrc_desc_pin(ce);
+	guc_lrc_desc_pin(ce, true);
  }
  static inline void guc_init_lrc_mapping(struct intel_guc *guc)
@@ -1578,13 +1935,15 @@ static void guc_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &guc_context_ops;
  	engine->request_alloc = guc_request_alloc;
  	engine->bump_serial = guc_bump_serial;
+	engine->add_active_request = add_to_context;
+	engine->remove_active_request = remove_from_context;
  	engine->sched_engine->schedule = i915_schedule;
-	engine->reset.prepare = guc_reset_prepare;
-	engine->reset.rewind = guc_reset_rewind;
-	engine->reset.cancel = guc_reset_cancel;
-	engine->reset.finish = guc_reset_finish;
+	engine->reset.prepare = guc_reset_nop;
+	engine->reset.rewind = guc_rewind_nop;
+	engine->reset.cancel = guc_reset_nop;
+	engine->reset.finish = guc_reset_nop;
  	engine->emit_flush = gen8_emit_flush_xcs;
  	engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
@@ -1757,7 +2116,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		 * register this context.
  		 */
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			register_context(ce);
+			register_context(ce, true);
  		guc_signal_context_fence(ce);
  		intel_context_put(ce);
  	} else if (context_destroyed(ce)) {
@@ -1939,6 +2298,10 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  				 "v%dx%d", ve->base.class, count);
  			ve->base.context_size = sibling->context_size;
+			ve->base.add_active_request =
+				sibling->add_active_request;
+			ve->base.remove_active_request =
+				sibling->remove_active_request;
  			ve->base.emit_bb_start = sibling->emit_bb_start;
  			ve->base.emit_flush = sibling->emit_flush;
  			ve->base.emit_init_breadcrumb =
drivers/gpu/drm/i915/gt/uc/intel_uc.c +38 -1
index 6d8b9233214e..f0b02200aa01 100644
@@ -565,12 +565,49 @@ void intel_uc_reset_prepare(struct intel_uc *uc)
  {
  	struct intel_guc *guc = &uc->guc;
-	if (!intel_guc_is_ready(guc))
+
+	/* Nothing to do if GuC isn't supported */
+	if (!intel_uc_supports_guc(uc))
  		return;
+	/* Firmware expected to be running when this function is called */
+	if (!intel_guc_is_ready(guc))
+		goto sanitize;
+
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_prepare(guc);
+
+sanitize:
  	__uc_sanitize(uc);
  }
+void intel_uc_reset(struct intel_uc *uc, bool stalled)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset(guc, stalled);
+}
+
+void intel_uc_reset_finish(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware expected to be running when this function is called */
+	if (intel_guc_is_fw_running(guc) && intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_finish(guc);
+}
+
+void intel_uc_cancel_requests(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_cancel_requests(guc);
+}
+
  void intel_uc_runtime_suspend(struct intel_uc *uc)
  {
  	struct intel_guc *guc = &uc->guc;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +3 -0
index c4cef885e984..eaa3202192ac 100644
@@ -37,6 +37,9 @@ void intel_uc_driver_late_release(struct intel_uc *uc);
  void intel_uc_driver_remove(struct intel_uc *uc);
  void intel_uc_init_mmio(struct intel_uc *uc);
  void intel_uc_reset_prepare(struct intel_uc *uc);
+void intel_uc_reset(struct intel_uc *uc, bool stalled);
+void intel_uc_reset_finish(struct intel_uc *uc);
+void intel_uc_cancel_requests(struct intel_uc *uc);
  void intel_uc_suspend(struct intel_uc *uc);
  void intel_uc_runtime_suspend(struct intel_uc *uc);
  int intel_uc_resume(struct intel_uc *uc);
drivers/gpu/drm/i915/i915_request.c +4 -37
index dec5a35c9aa2..192784875a1d 100644
@@ -194,7 +194,7 @@ static bool irq_work_imm(struct irq_work *wrk)
  	return false;
  }
-static void __notify_execute_cb_imm(struct i915_request *rq)
+void i915_request_notify_execute_cb_imm(struct i915_request *rq)
  {
  	__notify_execute_cb(rq, irq_work_imm);
  }
@@ -268,37 +268,6 @@ i915_request_active_engine(struct i915_request *rq,
  	return ret;
  }
-
-static void remove_from_engine(struct i915_request *rq)
-{
-	struct intel_engine_cs *engine, *locked;
-
-	/*
-	 * Virtual engines complicate acquiring the engine timeline lock,
-	 * as their rq->engine pointer is not stable until under that
-	 * engine lock. The simple ploy we use is to take the lock then
-	 * check that the rq still belongs to the newly locked engine.
-	 */
-	locked = READ_ONCE(rq->engine);
-	spin_lock_irq(&locked->sched_engine->lock);
-	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
-		spin_unlock(&locked->sched_engine->lock);
-		spin_lock(&engine->sched_engine->lock);
-		locked = engine;
-	}
-	list_del_init(&rq->sched.link);
-
-	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
-
-	/* Prevent further __await_execution() registering a cb, then flush */
-	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
-
-	spin_unlock_irq(&locked->sched_engine->lock);
-
-	__notify_execute_cb_imm(rq);
-}
-
  static void __rq_init_watchdog(struct i915_request *rq)
  {
  	rq->watchdog.timer.function = NULL;
@@ -395,9 +364,7 @@ bool i915_request_retire(struct i915_request *rq)
  	 * after removing the breadcrumb and signaling it, so that we do not
  	 * inadvertently attach the breadcrumb to a completed request.
  	 */
-	if (!list_empty(&rq->sched.link))
-		remove_from_engine(rq);
-	atomic_dec(&rq->context->guc_id_ref);
+	rq->engine->remove_active_request(rq);
  	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
  	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
@@ -539,7 +506,7 @@ __await_execution(struct i915_request *rq,
  	if (llist_add(&cb->work.node.llist, &signal->execute_cb)) {
  		if (i915_request_is_active(signal) ||
  		    __request_in_flight(signal))
-			__notify_execute_cb_imm(signal);
+			i915_request_notify_execute_cb_imm(signal);
  	}
  	return 0;
@@ -676,7 +643,7 @@ bool __i915_request_submit(struct i915_request *request)
  	result = true;
  	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
-	list_move_tail(&request->sched.link, &engine->sched_engine->requests);
+	engine->add_active_request(request);
  active:
  	clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags);
  	set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
drivers/gpu/drm/i915/i915_request.h +0 -0
index f870cd75a001..bcc6340c505e 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.h bool · line 653

Quoted 8 lines
drivers/gpu/drm/i915/i915_request.h +2 -0
@@ -649,4 +649,6 @@ bool
  i915_request_active_engine(struct i915_request *rq,
  			   struct intel_engine_cs **active);
+void i915_request_notify_execute_cb_imm(struct i915_request *rq);
+
  #endif /* I915_REQUEST_H */

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#102 lore raw

Re: [Intel-gfx] [PATCH 31/47] drm/i915/guc: Reset implementation for new GuC interface

In reply to Matthew Brost
Quoted 9 lines · Matthew Brost

Reset implementation for new GuC interface. This is the legacy reset implementation which is called when the i915 owns the engine hang check. Future patches will offload the engine hang check to GuC but we will continue to maintain this legacy path as a fallback and this code path is also required if the GuC dies.

With the new GuC interface it is not possible to reset individual engines - it is only possible to reset the GPU entirely. This patch forces an entire chip reset if any engine hangs.

No updates after my review comments on 6th of May.

At least:

1. wmb documentation

2. Spin lock cycling I either didn't understand or didn't buy the explanation. I don't remember seeing that pattern elsewhere in the driver - cycle a spinlock to make sure what was updated inside it is visible you said?

3. Dropping the lock protecting the list in the middle of list_for_each_entry_safe and just continuing to iterate like nothing happened. (__unwind_incomplete_requests) Again, perhaps I did not understand your explanation properly but you did appear to write:

"""
We only need the active lock for ce->guc_active.requests list. It is indeed safe to drop the lock.
"""

+ spin_lock(&ce->guc_active.lock);
+ list_for_each_entry_safe(rq, rn,
+ &ce->guc_active.requests,
+ sched.link) {
+ if (i915_request_completed(rq))
+ continue;
+
+ list_del_init(&rq->sched.link);
+ spin_unlock(&ce->guc_active.lock);
...
+ spin_lock(&ce->guc_active.lock);
+ }

Safe iterator guards against list_del but dropping the lock means the state of the overall list can change so next pointer may or may not be valid, requests may be missed, I don't know. Needs a comment explaining why it is safe.

Regards,

Tvrtko

Quoted 1,448 lines
---
  drivers/gpu/drm/i915/gt/intel_context.c       |   3 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |   7 +
  drivers/gpu/drm/i915/gt/intel_engine_types.h  |   6 +
  .../drm/i915/gt/intel_execlists_submission.c  |  40 ++
  drivers/gpu/drm/i915/gt/intel_gt_pm.c         |   6 +-
  drivers/gpu/drm/i915/gt/intel_reset.c         |  18 +-
  .../gpu/drm/i915/gt/intel_ring_submission.c   |  22 +
  drivers/gpu/drm/i915/gt/mock_engine.c         |  31 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.c        |  13 -
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   8 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 581 ++++++++++++++----
  drivers/gpu/drm/i915/gt/uc/intel_uc.c         |  39 +-
  drivers/gpu/drm/i915/gt/uc/intel_uc.h         |   3 +
  drivers/gpu/drm/i915/i915_request.c           |  41 +-
  drivers/gpu/drm/i915/i915_request.h           |   2 +
  15 files changed, 649 insertions(+), 171 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index b24a1b7a3f88..2f01437056a8 100644
@@ -392,6 +392,9 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	spin_lock_init(&ce->guc_state.lock);
  	INIT_LIST_HEAD(&ce->guc_state.fences);
 
+	spin_lock_init(&ce->guc_active.lock);
+	INIT_LIST_HEAD(&ce->guc_active.requests);
+
  	ce->guc_id = GUC_INVALID_LRC_ID;
  	INIT_LIST_HEAD(&ce->guc_id_link);
 
drivers/gpu/drm/i915/gt/intel_context_types.h +7 -0
index 6945963a31ba..b63c8cf7823b 100644
@@ -165,6 +165,13 @@ struct intel_context {
  		struct list_head fences;
  	} guc_state;
 
+	struct {
+		/** lock: protects everything in guc_active */
+		spinlock_t lock;
+		/** requests: active requests on this context */
+		struct list_head requests;
+	} guc_active;
+
  	/* GuC scheduling state that does not require a lock. */
  	atomic_t guc_sched_state_no_lock;
 
drivers/gpu/drm/i915/gt/intel_engine_types.h +6 -0
index e7cb6a06db9d..f9d264c008e8 100644
@@ -426,6 +426,12 @@ struct intel_engine_cs {
 
  	void		(*release)(struct intel_engine_cs *engine);
 
+	/*
+	 * Add / remove request from engine active tracking
+	 */
+	void		(*add_active_request)(struct i915_request *rq);
+	void		(*remove_active_request)(struct i915_request *rq);
+
  	struct intel_engine_execlists execlists;
 
  	/*
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +40 -0
index c10ea6080752..c301a2d088b1 100644
@@ -3118,6 +3118,42 @@ static void execlists_park(struct intel_engine_cs *engine)
  	cancel_timer(&engine->execlists.preempt);
  }
 
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&locked->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static bool can_preempt(struct intel_engine_cs *engine)
  {
  	if (GRAPHICS_VER(engine->i915) > 8)
@@ -3218,6 +3254,8 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &execlists_context_ops;
  	engine->request_alloc = execlists_request_alloc;
  	engine->bump_serial = execlist_bump_serial;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
 
  	engine->reset.prepare = execlists_reset_prepare;
  	engine->reset.rewind = execlists_reset_rewind;
@@ -3912,6 +3950,8 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  			 "v%dx%d", ve->base.class, count);
  		ve->base.context_size = sibling->context_size;
 
+		ve->base.add_active_request = sibling->add_active_request;
+		ve->base.remove_active_request = sibling->remove_active_request;
  		ve->base.emit_bb_start = sibling->emit_bb_start;
  		ve->base.emit_flush = sibling->emit_flush;
  		ve->base.emit_init_breadcrumb = sibling->emit_init_breadcrumb;
drivers/gpu/drm/i915/gt/intel_gt_pm.c +4 -2
index aef3084e8b16..463a6ae605a0 100644
@@ -174,8 +174,6 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
  	if (intel_gt_is_wedged(gt))
  		intel_gt_unset_wedged(gt);
 
-	intel_uc_sanitize(&gt->uc);
-
  	for_each_engine(engine, gt, id)
  		if (engine->reset.prepare)
  			engine->reset.prepare(engine);
@@ -191,6 +189,8 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
  			__intel_engine_reset(engine, false);
  	}
 
+	intel_uc_reset(&gt->uc, false);
+
  	for_each_engine(engine, gt, id)
  		if (engine->reset.finish)
  			engine->reset.finish(engine);
@@ -243,6 +243,8 @@ int intel_gt_resume(struct intel_gt *gt)
  		goto err_wedged;
  	}
 
+	intel_uc_reset_finish(&gt->uc);
+
  	intel_rps_enable(&gt->rps);
  	intel_llc_enable(&gt->llc);
 
drivers/gpu/drm/i915/gt/intel_reset.c +12 -6
index 72251638d4ea..2987282dff6d 100644
@@ -826,6 +826,8 @@ static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
  		__intel_engine_reset(engine, stalled_mask & engine->mask);
  	local_bh_enable();
 
+	intel_uc_reset(&gt->uc, true);
+
  	intel_ggtt_restore_fences(gt->ggtt);
 
  	return err;
@@ -850,6 +852,8 @@ static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
  		if (awake & engine->mask)
  			intel_engine_pm_put(engine);
  	}
+
+	intel_uc_reset_finish(&gt->uc);
  }
 
  static void nop_submit_request(struct i915_request *request)
@@ -903,6 +907,7 @@ static void __intel_gt_set_wedged(struct intel_gt *gt)
  	for_each_engine(engine, gt, id)
  		if (engine->reset.cancel)
  			engine->reset.cancel(engine);
+	intel_uc_cancel_requests(&gt->uc);
  	local_bh_enable();
 
  	reset_finish(gt, awake);
@@ -1191,6 +1196,9 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
  	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
  	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
 
+	if (intel_engine_uses_guc(engine))
+		return -ENODEV;
+
  	if (!intel_engine_pm_get_if_awake(engine))
  		return 0;
 
@@ -1201,13 +1209,10 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
  			   "Resetting %s for %s\n", engine->name, msg);
  	atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
 
-	if (intel_engine_uses_guc(engine))
-		ret = intel_guc_reset_engine(&engine->gt->uc.guc, engine);
-	else
-		ret = intel_gt_reset_engine(engine);
+	ret = intel_gt_reset_engine(engine);
  	if (ret) {
  		/* If we fail here, we expect to fallback to a global reset */
-		ENGINE_TRACE(engine, "Failed to reset, err: %d\n", ret);
+		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
  		goto out;
  	}
 
@@ -1341,7 +1346,8 @@ void intel_gt_handle_error(struct intel_gt *gt,
  	 * Try engine reset when available. We fall back to full reset if
  	 * single reset fails.
  	 */
-	if (intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
+	if (!intel_uc_uses_guc_submission(&gt->uc) &&
+	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
  		local_bh_disable();
  		for_each_engine_masked(engine, gt, engine_mask, tmp) {
  			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
drivers/gpu/drm/i915/gt/intel_ring_submission.c +22 -0
index e1506b280df1..99dcdc8fba12 100644
@@ -1049,6 +1049,25 @@ static void ring_bump_serial(struct intel_engine_cs *engine)
  	engine->serial++;
  }
 
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	spin_lock_irq(&rq->engine->sched_engine->lock);
+	list_del_init(&rq->sched.link);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&rq->engine->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static void setup_common(struct intel_engine_cs *engine)
  {
  	struct drm_i915_private *i915 = engine->i915;
@@ -1066,6 +1085,9 @@ static void setup_common(struct intel_engine_cs *engine)
  	engine->reset.cancel = reset_cancel;
  	engine->reset.finish = reset_finish;
 
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
+
  	engine->cops = &ring_context_ops;
  	engine->request_alloc = ring_request_alloc;
  	engine->bump_serial = ring_bump_serial;
drivers/gpu/drm/i915/gt/mock_engine.c +31 -0
index fc5a65ab1937..c12ff3a75ce6 100644
@@ -235,6 +235,35 @@ static void mock_submit_request(struct i915_request *request)
  	spin_unlock_irqrestore(&engine->hw_lock, flags);
  }
 
+static void mock_add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void mock_remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+	spin_unlock_irq(&locked->sched_engine->lock);
+}
+
+
  static void mock_reset_prepare(struct intel_engine_cs *engine)
  {
  }
@@ -327,6 +356,8 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  	engine->base.emit_flush = mock_emit_flush;
  	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
  	engine->base.submit_request = mock_submit_request;
+	engine->base.add_active_request = mock_add_to_engine;
+	engine->base.remove_active_request = mock_remove_from_engine;
 
  	engine->base.reset.prepare = mock_reset_prepare;
  	engine->base.reset.rewind = mock_reset_rewind;
drivers/gpu/drm/i915/gt/uc/intel_guc.c +0 -13
index 6661dcb02239..9b09395b998f 100644
@@ -572,19 +572,6 @@ int intel_guc_suspend(struct intel_guc *guc)
  	return 0;
  }
 
-/**
- * intel_guc_reset_engine() - ask GuC to reset an engine
- * @guc:	intel_guc structure
- * @engine:	engine to be reset
- */
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine)
-{
-	/* XXX: to be implemented with submission interface rework */
-
-	return -ENODEV;
-}
-
  /**
   * intel_guc_resume() - notify GuC resuming from suspend state
   * @guc:	the guc
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 22eb1e9cca41..40c9868762d7 100644
@@ -242,14 +242,16 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
 
  int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout);
 
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine);
-
  int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  					  const u32 *msg, u32 len);
  int intel_guc_sched_done_process_msg(struct intel_guc *guc,
  				     const u32 *msg, u32 len);
 
+void intel_guc_submission_reset_prepare(struct intel_guc *guc);
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled);
+void intel_guc_submission_reset_finish(struct intel_guc *guc);
+void intel_guc_submission_cancel_requests(struct intel_guc *guc);
+
  void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
 
  #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +472 -109
index 83058df5ba01..b8c894ad8caf 100644
@@ -141,7 +141,7 @@ context_wait_for_deregister_to_register(struct intel_context *ce)
  static inline void
  set_context_wait_for_deregister_to_register(struct intel_context *ce)
  {
-	/* Only should be called from guc_lrc_desc_pin() */
+	/* Only should be called from guc_lrc_desc_pin() without lock */
  	ce->guc_state.sched_state |=
  		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
  }
@@ -241,15 +241,31 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
 
  static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
  {
+	guc->lrc_desc_pool_vaddr = NULL;
  	i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
  }
 
+static inline bool guc_submission_initialized(struct intel_guc *guc)
+{
+	return guc->lrc_desc_pool_vaddr != NULL;
+}
+
  static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
  {
-	struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+	if (likely(guc_submission_initialized(guc))) {
+		struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+		unsigned long flags;
 
-	memset(desc, 0, sizeof(*desc));
-	xa_erase_irq(&guc->context_lookup, id);
+		memset(desc, 0, sizeof(*desc));
+
+		/*
+		 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
+		 * the lower level functions directly.
+		 */
+		xa_lock_irqsave(&guc->context_lookup, flags);
+		__xa_erase(&guc->context_lookup, id);
+		xa_unlock_irqrestore(&guc->context_lookup, flags);
+	}
  }
 
  static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
@@ -260,7 +276,15 @@ static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
  static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
  					   struct intel_context *ce)
  {
-	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	unsigned long flags;
+
+	/*
+	 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
+	 * lower level functions directly.
+	 */
+	xa_lock_irqsave(&guc->context_lookup, flags);
+	__xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	xa_unlock_irqrestore(&guc->context_lookup, flags);
  }
 
  static int guc_submission_busy_loop(struct intel_guc* guc,
@@ -331,6 +355,8 @@ int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
  					interruptible, timeout);
  }
 
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);
+
  static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  {
  	int err;
@@ -338,11 +364,22 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	u32 action[3];
  	int len = 0;
  	u32 g2h_len_dw = 0;
-	bool enabled = context_enabled(ce);
+	bool enabled;
 
  	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
  	GEM_BUG_ON(context_guc_id_invalid(ce));
 
+	/*
+	 * Corner case where the GuC firmware was blown away and reloaded while
+	 * this context was pinned.
+	 */
+	if (unlikely(!lrc_desc_registered(guc, ce->guc_id))) {
+		err = guc_lrc_desc_pin(ce, false);
+		if (unlikely(err))
+			goto out;
+	}
+	enabled = context_enabled(ce);
+
  	if (!enabled) {
  		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
  		action[len++] = ce->guc_id;
@@ -365,6 +402,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  		intel_context_put(ce);
  	}
 
+out:
  	return err;
  }
 
@@ -419,15 +457,10 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  	if (submit) {
  		guc_set_lrc_tail(last);
  resubmit:
-		/*
-		 * We only check for -EBUSY here even though it is possible for
-		 * -EDEADLK to be returned. If -EDEADLK is returned, the GuC has
-		 * died and a full GPU needs to be done. The hangcheck will
-		 * eventually detect that the GuC has died and trigger this
-		 * reset so no need to handle -EDEADLK here.
-		 */
  		ret = guc_add_request(guc, last);
-		if (ret == -EBUSY) {
+		if (unlikely(ret == -EIO))
+			goto deadlk;
+		else if (ret == -EBUSY) {
  			tasklet_schedule(&sched_engine->tasklet);
  			guc->stalled_request = last;
  			return false;
@@ -437,6 +470,11 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
 
  	guc->stalled_request = NULL;
  	return submit;
+
+deadlk:
+	sched_engine->tasklet.callback = NULL;
+	tasklet_disable_nosync(&sched_engine->tasklet);
+	return false;
  }
 
  static void guc_submission_tasklet(struct tasklet_struct *t)
@@ -463,27 +501,165 @@ static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
  		intel_engine_signal_breadcrumbs(engine);
  }
 
-static void guc_reset_prepare(struct intel_engine_cs *engine)
+static void __guc_context_destroy(struct intel_context *ce);
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
+static void guc_signal_context_fence(struct intel_context *ce);
+
+static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	unsigned long index, flags;
+	bool pending_disable, pending_enable, deregister, destroyed;
+
+	xa_for_each(&guc->context_lookup, index, ce) {
+		/* Flush context */
+		spin_lock_irqsave(&ce->guc_state.lock, flags);
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+
+		/*
+		 * Once we are at this point submission_disabled() is guaranteed
+		 * to visible to all callers who set the below flags (see above
+		 * flush and flushes in reset_prepare). If submission_disabled()
+		 * is set, the caller shouldn't set these flags.
+		 */
+
+		destroyed = context_destroyed(ce);
+		pending_enable = context_pending_enable(ce);
+		pending_disable = context_pending_disable(ce);
+		deregister = context_wait_for_deregister_to_register(ce);
+		init_sched_state(ce);
+
+		if (pending_enable || destroyed || deregister) {
+			atomic_dec(&guc->outstanding_submission_g2h);
+			if (deregister)
+				guc_signal_context_fence(ce);
+			if (destroyed) {
+				release_guc_id(guc, ce);
+				__guc_context_destroy(ce);
+			}
+			if (pending_enable|| deregister)
+				intel_context_put(ce);
+		}
+
+		/* Not mutualy exclusive with above if statement. */
+		if (pending_disable) {
+			guc_signal_context_fence(ce);
+			intel_context_sched_disable_unpin(ce);
+			atomic_dec(&guc->outstanding_submission_g2h);
+			intel_context_put(ce);
+		}
+	}
+}
+
+static inline bool
+submission_disabled(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	return unlikely(!__tasklet_is_enabled(&sched_engine->tasklet));
+}
+
+static void disable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	if (__tasklet_is_enabled(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+		__tasklet_disable_sync_once(&sched_engine->tasklet);
+		sched_engine->tasklet.callback = NULL;
+	}
+}
+
+static void enable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->sched_engine->lock, flags);
+	sched_engine->tasklet.callback = guc_submission_tasklet;
+	wmb();
+	if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
+	    __tasklet_enable(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+
+		/* And kick in case we missed a new request submission. */
+		tasklet_hi_schedule(&sched_engine->tasklet);
+	}
+	spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
+}
+
+static void guc_flush_submissions(struct intel_guc *guc)
  {
-	ENGINE_TRACE(engine, "\n");
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+void intel_guc_submission_reset_prepare(struct intel_guc *guc)
+{
+	int i;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;
+
+	disable_submission(guc);
+	guc->interrupts.disable(guc);
+
+	/* Flush IRQ handler */
+	spin_lock_irq(&guc_to_gt(guc)->irq_lock);
+	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
+
+	guc_flush_submissions(guc);
 
  	/*
-	 * Prevent request submission to the hardware until we have
-	 * completed the reset in i915_gem_reset_finish(). If a request
-	 * is completed by one engine, it may then queue a request
-	 * to a second via its execlists->tasklet *just* as we are
-	 * calling engine->init_hw() and also writing the ELSP.
-	 * Turning off the execlists->tasklet until the reset is over
-	 * prevents the race.
-	 */
-	__tasklet_disable_sync_once(&engine->sched_engine->tasklet);
+	 * Handle any outstanding G2Hs before reset. Call IRQ handler directly
+	 * each pass as interrupt have been disabled. We always scrub for
+	 * outstanding G2H as it is possible for outstanding_submission_g2h to
+	 * be incremented after the context state update.
+ 	 */
+	for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
+		intel_guc_to_host_event_handler(guc);
+#define wait_for_reset(guc, wait_var) \
+		guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
+		do {
+			wait_for_reset(guc, &guc->outstanding_submission_g2h);
+		} while (!list_empty(&guc->ct.requests.incoming));
+	}
+	scrub_guc_desc_for_outstanding_g2h(guc);
  }
 
-static void guc_reset_state(struct intel_context *ce,
-			    struct intel_engine_cs *engine,
-			    u32 head,
-			    bool scrub)
+static struct intel_engine_cs *
+guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
  {
+	struct intel_engine_cs *engine;
+	intel_engine_mask_t tmp, mask = ve->mask;
+	unsigned int num_siblings = 0;
+
+	for_each_engine_masked(engine, ve->gt, mask, tmp)
+		if (num_siblings++ == sibling)
+			return engine;
+
+	return NULL;
+}
+
+static inline struct intel_engine_cs *
+__context_to_physical_engine(struct intel_context *ce)
+{
+	struct intel_engine_cs *engine = ce->engine;
+
+	if (intel_engine_is_virtual(engine))
+		engine = guc_virtual_get_sibling(engine, 0);
+
+	return engine;
+}
+
+static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
+{
+	struct intel_engine_cs *engine = __context_to_physical_engine(ce);
+
  	GEM_BUG_ON(!intel_context_is_pinned(ce));
 
  	/*
@@ -501,42 +677,147 @@ static void guc_reset_state(struct intel_context *ce,
  	lrc_update_regs(ce, engine, head);
  }
 
-static void guc_reset_rewind(struct intel_engine_cs *engine, bool stalled)
+static void guc_reset_nop(struct intel_engine_cs *engine)
  {
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_request *rq;
+}
+
+static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
+{
+}
+
+static void
+__unwind_incomplete_requests(struct intel_context *ce)
+{
+	struct i915_request *rq, *rn;
+	struct list_head *pl;
+	int prio = I915_PRIORITY_INVALID;
+	struct i915_sched_engine * const sched_engine =
+		ce->engine->sched_engine;
  	unsigned long flags;
 
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry_safe(rq, rn,
+				 &ce->guc_active.requests,
+				 sched.link) {
+		if (i915_request_completed(rq))
+			continue;
+
+		list_del_init(&rq->sched.link);
+		spin_unlock(&ce->guc_active.lock);
+
+		__i915_request_unsubmit(rq);
+
+		/* Push the request back into the queue for later resubmission. */
+		GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
+		if (rq_prio(rq) != prio) {
+			prio = rq_prio(rq);
+			pl = i915_sched_lookup_priolist(sched_engine, prio);
+		}
+		GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
+
+		list_add_tail(&rq->sched.link, pl);
+		set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
 
-	/* Push back any incomplete requests for replay after the reset. */
-	rq = execlists_unwind_incomplete_requests(execlists);
-	if (!rq)
-		goto out_unlock;
+		spin_lock(&ce->guc_active.lock);
+	}
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static struct i915_request *context_find_active_request(struct intel_context *ce)
+{
+	struct i915_request *rq, *active = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ce->guc_active.lock, flags);
+	list_for_each_entry_reverse(rq, &ce->guc_active.requests,
+				    sched.link) {
+		if (i915_request_completed(rq))
+			break;
+
+		active = rq;
+	}
+	spin_unlock_irqrestore(&ce->guc_active.lock, flags);
+
+	return active;
+}
+
+static void __guc_reset_context(struct intel_context *ce, bool stalled)
+{
+	struct i915_request *rq;
+	u32 head;
+
+	/*
+	 * GuC will implicitly mark the context as non-schedulable
+	 * when it sends the reset notification. Make sure our state
+	 * reflects this change. The context will be marked enabled
+	 * on resubmission.
+	 */
+	clr_context_enabled(ce);
+
+	rq = context_find_active_request(ce);
+	if (!rq) {
+		head = ce->ring->tail;
+		stalled = false;
+		goto out_replay;
+	}
 
  	if (!i915_request_started(rq))
  		stalled = false;
 
+	GEM_BUG_ON(i915_active_is_idle(&ce->active));
+	head = intel_ring_wrap(ce->ring, rq->head);
  	__i915_request_reset(rq, stalled);
-	guc_reset_state(rq->context, engine, rq->head, stalled);
 
-out_unlock:
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+out_replay:
+	guc_reset_state(ce, head, stalled);
+	__unwind_incomplete_requests(ce);
  }
 
-static void guc_reset_cancel(struct intel_engine_cs *engine)
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
+{
+	struct intel_context *ce;
+	unsigned long index;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			__guc_reset_context(ce, stalled);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);
+}
+
+static void guc_cancel_context_requests(struct intel_context *ce)
+{
+	struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
+	struct i915_request *rq;
+	unsigned long flags;
+
+	/* Mark all executing requests as skipped. */
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry(rq, &ce->guc_active.requests, sched.link)
+		i915_request_put(i915_request_mark_eio(rq));
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static void
+guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
  {
-	struct i915_sched_engine * const sched_engine = engine->sched_engine;
  	struct i915_request *rq, *rn;
  	struct rb_node *rb;
  	unsigned long flags;
 
  	/* Can be called during boot if GuC fails to load */
-	if (!engine->gt)
+	if (!sched_engine)
  		return;
 
-	ENGINE_TRACE(engine, "\n");
-
  	/*
  	 * Before we call engine->cancel_requests(), we should have exclusive
  	 * access to the submission state. This is arranged for us by the
@@ -553,21 +834,16 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	 */
  	spin_lock_irqsave(&sched_engine->lock, flags);
 
-	/* Mark all executing requests as skipped. */
-	list_for_each_entry(rq, &sched_engine->requests, sched.link) {
-		i915_request_set_error_once(rq, -EIO);
-		i915_request_mark_complete(rq);
-	}
-
  	/* Flush the queued requests to the timeline list (for retiring). */
  	while ((rb = rb_first_cached(&sched_engine->queue))) {
  		struct i915_priolist *p = to_priolist(rb);
 
  		priolist_for_each_request_consume(rq, rn, p) {
  			list_del_init(&rq->sched.link);
+
  			__i915_request_submit(rq);
-			dma_fence_set_error(&rq->fence, -EIO);
-			i915_request_mark_complete(rq);
+
+			i915_request_put(i915_request_mark_eio(rq));
  		}
 
  		rb_erase_cached(&p->node, &sched_engine->queue);
@@ -582,14 +858,38 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
 
-static void guc_reset_finish(struct intel_engine_cs *engine)
+void intel_guc_submission_cancel_requests(struct intel_guc *guc)
  {
-	if (__tasklet_enable(&engine->sched_engine->tasklet))
-		/* And kick in case we missed a new request submission. */
-		tasklet_hi_schedule(&engine->sched_engine->tasklet);
+	struct intel_context *ce;
+	unsigned long index;
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			guc_cancel_context_requests(ce);
 
-	ENGINE_TRACE(engine, "depth->%d\n",
-		     atomic_read(&engine->sched_engine->tasklet.count));
+	guc_cancel_sched_engine_requests(guc->sched_engine);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);
+}
+
+void intel_guc_submission_reset_finish(struct intel_guc *guc)
+{
+	/* Reset called during driver load or during wedge? */
+	if (unlikely(!guc_submission_initialized(guc) ||
+		     test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags)))
+		return;
+
+	/*
+	 * Technically possible for either of these values to be non-zero here,
+	 * but very unlikely + harmless. Regardless let's add a warn so we can
+	 * see in CI if this happens frequently / a precursor to taking down the
+	 * machine.
+	 */
+	GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
+	atomic_set(&guc->outstanding_submission_g2h, 0);
+
+	enable_submission(guc);
  }
 
  /*
@@ -656,6 +956,9 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
  	else
  		trace_i915_request_guc_submit(rq);
 
+	if (unlikely(ret == -EIO))
+		disable_submission(guc);
+
  	return ret;
  }
 
@@ -668,7 +971,8 @@ static void guc_submit_request(struct i915_request *rq)
  	/* Will be called from irq-context when using foreign fences. */
  	spin_lock_irqsave(&sched_engine->lock, flags);
 
-	if (guc->stalled_request || !i915_sched_engine_is_empty(sched_engine))
+	if (submission_disabled(guc) || guc->stalled_request ||
+	    !i915_sched_engine_is_empty(sched_engine))
  		queue_request(sched_engine, rq, rq_prio(rq));
  	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
  		tasklet_hi_schedule(&sched_engine->tasklet);
@@ -805,7 +1109,8 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
 
  static int __guc_action_register_context(struct intel_guc *guc,
  					 u32 guc_id,
-					 u32 offset)
+					 u32 offset,
+					 bool loop)
  {
  	u32 action[] = {
  		INTEL_GUC_ACTION_REGISTER_CONTEXT,
@@ -813,10 +1118,10 @@ static int __guc_action_register_context(struct intel_guc *guc,
  		offset,
  	};
 
-	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
+	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, loop);
  }
 
-static int register_context(struct intel_context *ce)
+static int register_context(struct intel_context *ce, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
  	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
@@ -824,11 +1129,12 @@ static int register_context(struct intel_context *ce)
 
  	trace_intel_context_register(ce);
 
-	return __guc_action_register_context(guc, ce->guc_id, offset);
+	return __guc_action_register_context(guc, ce->guc_id, offset, loop);
  }
 
  static int __guc_action_deregister_context(struct intel_guc *guc,
-					   u32 guc_id)
+					   u32 guc_id,
+					   bool loop)
  {
  	u32 action[] = {
  		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
@@ -836,16 +1142,16 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
  	};
 
  	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
-					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
+					G2H_LEN_DW_DEREGISTER_CONTEXT, loop);
  }
 
-static int deregister_context(struct intel_context *ce, u32 guc_id)
+static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
 
  	trace_intel_context_deregister(ce);
 
-	return __guc_action_deregister_context(guc, guc_id);
+	return __guc_action_deregister_context(guc, guc_id, loop);
  }
 
  static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
@@ -874,7 +1180,7 @@ static void guc_context_policy_init(struct intel_engine_cs *engine,
  	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
  }
 
-static int guc_lrc_desc_pin(struct intel_context *ce)
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  {
  	struct intel_runtime_pm *runtime_pm =
  		&ce->engine->gt->i915->runtime_pm;
@@ -920,18 +1226,44 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
  	 */
  	if (context_registered) {
  		trace_intel_context_steal_guc_id(ce);
-		set_context_wait_for_deregister_to_register(ce);
-		intel_context_get(ce);
+		if (!loop) {
+			set_context_wait_for_deregister_to_register(ce);
+			intel_context_get(ce);
+		} else {
+			bool disabled;
+			unsigned long flags;
+
+			/* Seal race with Reset */
+			spin_lock_irqsave(&ce->guc_state.lock, flags);
+			disabled = submission_disabled(guc);
+			if (likely(!disabled)) {
+				set_context_wait_for_deregister_to_register(ce);
+				intel_context_get(ce);
+			}
+			spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+			if (unlikely(disabled)) {
+				reset_lrc_desc(guc, desc_idx);
+				return 0;	/* Will get registered later */
+			}
+		}
 
  		/*
  		 * If stealing the guc_id, this ce has the same guc_id as the
  		 * context whos guc_id was stole.
  		 */
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = deregister_context(ce, ce->guc_id);
+			ret = deregister_context(ce, ce->guc_id, loop);
+		if (unlikely(ret == -EBUSY)) {
+			clr_context_wait_for_deregister_to_register(ce);
+			intel_context_put(ce);
+		}
  	} else {
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = register_context(ce);
+			ret = register_context(ce, loop);
+		if (unlikely(ret == -EBUSY))
+			reset_lrc_desc(guc, desc_idx);
+		else if (unlikely(ret == -ENODEV))
+			ret = 0;	/* Will get registered later */
  	}
 
  	return ret;
@@ -994,7 +1326,6 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
  	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
 
  	trace_intel_context_sched_disable(ce);
-	intel_context_get(ce);
 
  	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
  				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
@@ -1004,6 +1335,7 @@ static u16 prep_context_pending_disable(struct intel_context *ce)
  {
  	set_context_pending_disable(ce);
  	clr_context_enabled(ce);
+	intel_context_get(ce);
 
  	return ce->guc_id;
  }
@@ -1016,7 +1348,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	u16 guc_id;
  	intel_wakeref_t wakeref;
 
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		clr_context_enabled(ce);
  		goto unpin;
@@ -1034,6 +1366,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	 * request doesn't slip through the 'context_pending_disable' fence.
  	 */
  	if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
  		return;
  	}
  	guc_id = prep_context_pending_disable(ce);
@@ -1050,19 +1383,13 @@ static void guc_context_sched_disable(struct intel_context *ce)
 
  static inline void guc_lrc_desc_unpin(struct intel_context *ce)
  {
-	struct intel_engine_cs *engine = ce->engine;
-	struct intel_guc *guc = &engine->gt->uc.guc;
-	unsigned long flags;
+	struct intel_guc *guc = ce_to_guc(ce);
 
  	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
  	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
  	GEM_BUG_ON(context_enabled(ce));
 
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	set_context_destroyed(ce);
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-
-	deregister_context(ce, ce->guc_id);
+	deregister_context(ce, ce->guc_id, true);
  }
 
  static void __guc_context_destroy(struct intel_context *ce)
@@ -1090,13 +1417,15 @@ static void guc_context_destroy(struct kref *kref)
  	struct intel_guc *guc = &ce->engine->gt->uc.guc;
  	intel_wakeref_t wakeref;
  	unsigned long flags;
+	bool disabled;
 
  	/*
  	 * If the guc_id is invalid this context has been stolen and we can free
  	 * it immediately. Also can be freed immediately if the context is not
  	 * registered with the GuC.
  	 */
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) ||
+	    context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		release_guc_id(guc, ce);
  		__guc_context_destroy(ce);
@@ -1123,6 +1452,18 @@ static void guc_context_destroy(struct kref *kref)
  		list_del_init(&ce->guc_id_link);
  	spin_unlock_irqrestore(&guc->contexts_lock, flags);
 
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled))
+		set_context_destroyed(ce);
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
  	/*
  	 * We defer GuC context deregistration until the context is destroyed
  	 * in order to save on CTBs. With this optimization ideally we only need
@@ -1145,6 +1486,33 @@ static int guc_context_alloc(struct intel_context *ce)
  	return lrc_alloc(ce, ce->engine);
  }
 
+static void add_to_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock(&ce->guc_active.lock);
+	list_move_tail(&rq->sched.link, &ce->guc_active.requests);
+	spin_unlock(&ce->guc_active.lock);
+}
+
+static void remove_from_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock_irq(&ce->guc_active.lock);
+
+	list_del_init(&rq->sched.link);
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&ce->guc_active.lock);
+
+	atomic_dec(&ce->guc_id_ref);
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static const struct intel_context_ops guc_context_ops = {
  	.alloc = guc_context_alloc,
 
@@ -1183,8 +1551,6 @@ static void guc_signal_context_fence(struct intel_context *ce)
  {
  	unsigned long flags;
 
-	GEM_BUG_ON(!context_wait_for_deregister_to_register(ce));
-
  	spin_lock_irqsave(&ce->guc_state.lock, flags);
  	clr_context_wait_for_deregister_to_register(ce);
  	__guc_signal_context_fence(ce);
@@ -1193,8 +1559,9 @@ static void guc_signal_context_fence(struct intel_context *ce)
 
  static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
  {
-	return new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
-		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id);
+	return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
+		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id)) &&
+		!submission_disabled(ce_to_guc(ce));
  }
 
  static int guc_request_alloc(struct i915_request *rq)
@@ -1252,8 +1619,12 @@ static int guc_request_alloc(struct i915_request *rq)
  	if (unlikely(ret < 0))
  		return ret;;
  	if (context_needs_register(ce, !!ret)) {
-		ret = guc_lrc_desc_pin(ce);
+		ret = guc_lrc_desc_pin(ce, true);
  		if (unlikely(ret)) {	/* unwind */
+			if (ret == -EIO) {
+				disable_submission(guc);
+				goto out;	/* GPU will be reset */
+			}
  			atomic_dec(&ce->guc_id_ref);
  			unpin_guc_id(guc, ce);
  			return ret;
@@ -1290,20 +1661,6 @@ static int guc_request_alloc(struct i915_request *rq)
  	return 0;
  }
 
-static struct intel_engine_cs *
-guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
-{
-	struct intel_engine_cs *engine;
-	intel_engine_mask_t tmp, mask = ve->mask;
-	unsigned int num_siblings = 0;
-
-	for_each_engine_masked(engine, ve->gt, mask, tmp)
-		if (num_siblings++ == sibling)
-			return engine;
-
-	return NULL;
-}
-
  static int guc_virtual_context_pre_pin(struct intel_context *ce,
  				       struct i915_gem_ww_ctx *ww,
  				       void **vaddr)
@@ -1512,7 +1869,7 @@ static inline void guc_kernel_context_pin(struct intel_guc *guc,
  {
  	if (context_guc_id_invalid(ce))
  		pin_guc_id(guc, ce);
-	guc_lrc_desc_pin(ce);
+	guc_lrc_desc_pin(ce, true);
  }
 
  static inline void guc_init_lrc_mapping(struct intel_guc *guc)
@@ -1578,13 +1935,15 @@ static void guc_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &guc_context_ops;
  	engine->request_alloc = guc_request_alloc;
  	engine->bump_serial = guc_bump_serial;
+	engine->add_active_request = add_to_context;
+	engine->remove_active_request = remove_from_context;
 
  	engine->sched_engine->schedule = i915_schedule;
 
-	engine->reset.prepare = guc_reset_prepare;
-	engine->reset.rewind = guc_reset_rewind;
-	engine->reset.cancel = guc_reset_cancel;
-	engine->reset.finish = guc_reset_finish;
+	engine->reset.prepare = guc_reset_nop;
+	engine->reset.rewind = guc_rewind_nop;
+	engine->reset.cancel = guc_reset_nop;
+	engine->reset.finish = guc_reset_nop;
 
  	engine->emit_flush = gen8_emit_flush_xcs;
  	engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
@@ -1757,7 +2116,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		 * register this context.
  		 */
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			register_context(ce);
+			register_context(ce, true);
  		guc_signal_context_fence(ce);
  		intel_context_put(ce);
  	} else if (context_destroyed(ce)) {
@@ -1939,6 +2298,10 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  				 "v%dx%d", ve->base.class, count);
  			ve->base.context_size = sibling->context_size;
 
+			ve->base.add_active_request =
+				sibling->add_active_request;
+			ve->base.remove_active_request =
+				sibling->remove_active_request;
  			ve->base.emit_bb_start = sibling->emit_bb_start;
  			ve->base.emit_flush = sibling->emit_flush;
  			ve->base.emit_init_breadcrumb =
drivers/gpu/drm/i915/gt/uc/intel_uc.c +38 -1
index 6d8b9233214e..f0b02200aa01 100644
@@ -565,12 +565,49 @@ void intel_uc_reset_prepare(struct intel_uc *uc)
  {
  	struct intel_guc *guc = &uc->guc;
 
-	if (!intel_guc_is_ready(guc))
+
+	/* Nothing to do if GuC isn't supported */
+	if (!intel_uc_supports_guc(uc))
  		return;
 
+	/* Firmware expected to be running when this function is called */
+	if (!intel_guc_is_ready(guc))
+		goto sanitize;
+
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_prepare(guc);
+
+sanitize:
  	__uc_sanitize(uc);
  }
 
+void intel_uc_reset(struct intel_uc *uc, bool stalled)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset(guc, stalled);
+}
+
+void intel_uc_reset_finish(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware expected to be running when this function is called */
+	if (intel_guc_is_fw_running(guc) && intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_finish(guc);
+}
+
+void intel_uc_cancel_requests(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_cancel_requests(guc);
+}
+
  void intel_uc_runtime_suspend(struct intel_uc *uc)
  {
  	struct intel_guc *guc = &uc->guc;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +3 -0
index c4cef885e984..eaa3202192ac 100644
@@ -37,6 +37,9 @@ void intel_uc_driver_late_release(struct intel_uc *uc);
  void intel_uc_driver_remove(struct intel_uc *uc);
  void intel_uc_init_mmio(struct intel_uc *uc);
  void intel_uc_reset_prepare(struct intel_uc *uc);
+void intel_uc_reset(struct intel_uc *uc, bool stalled);
+void intel_uc_reset_finish(struct intel_uc *uc);
+void intel_uc_cancel_requests(struct intel_uc *uc);
  void intel_uc_suspend(struct intel_uc *uc);
  void intel_uc_runtime_suspend(struct intel_uc *uc);
  int intel_uc_resume(struct intel_uc *uc);
drivers/gpu/drm/i915/i915_request.c +4 -37
index dec5a35c9aa2..192784875a1d 100644
@@ -194,7 +194,7 @@ static bool irq_work_imm(struct irq_work *wrk)
  	return false;
  }
 
-static void __notify_execute_cb_imm(struct i915_request *rq)
+void i915_request_notify_execute_cb_imm(struct i915_request *rq)
  {
  	__notify_execute_cb(rq, irq_work_imm);
  }
@@ -268,37 +268,6 @@ i915_request_active_engine(struct i915_request *rq,
  	return ret;
  }
 
-
-static void remove_from_engine(struct i915_request *rq)
-{
-	struct intel_engine_cs *engine, *locked;
-
-	/*
-	 * Virtual engines complicate acquiring the engine timeline lock,
-	 * as their rq->engine pointer is not stable until under that
-	 * engine lock. The simple ploy we use is to take the lock then
-	 * check that the rq still belongs to the newly locked engine.
-	 */
-	locked = READ_ONCE(rq->engine);
-	spin_lock_irq(&locked->sched_engine->lock);
-	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
-		spin_unlock(&locked->sched_engine->lock);
-		spin_lock(&engine->sched_engine->lock);
-		locked = engine;
-	}
-	list_del_init(&rq->sched.link);
-
-	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
-
-	/* Prevent further __await_execution() registering a cb, then flush */
-	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
-
-	spin_unlock_irq(&locked->sched_engine->lock);
-
-	__notify_execute_cb_imm(rq);
-}
-
  static void __rq_init_watchdog(struct i915_request *rq)
  {
  	rq->watchdog.timer.function = NULL;
@@ -395,9 +364,7 @@ bool i915_request_retire(struct i915_request *rq)
  	 * after removing the breadcrumb and signaling it, so that we do not
  	 * inadvertently attach the breadcrumb to a completed request.
  	 */
-	if (!list_empty(&rq->sched.link))
-		remove_from_engine(rq);
-	atomic_dec(&rq->context->guc_id_ref);
+	rq->engine->remove_active_request(rq);
  	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
 
  	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
@@ -539,7 +506,7 @@ __await_execution(struct i915_request *rq,
  	if (llist_add(&cb->work.node.llist, &signal->execute_cb)) {
  		if (i915_request_is_active(signal) ||
  		    __request_in_flight(signal))
-			__notify_execute_cb_imm(signal);
+			i915_request_notify_execute_cb_imm(signal);
  	}
 
  	return 0;
@@ -676,7 +643,7 @@ bool __i915_request_submit(struct i915_request *request)
  	result = true;
 
  	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
-	list_move_tail(&request->sched.link, &engine->sched_engine->requests);
+	engine->add_active_request(request);
  active:
  	clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags);
  	set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
drivers/gpu/drm/i915/i915_request.h +2 -0
index f870cd75a001..bcc6340c505e 100644
@@ -649,4 +649,6 @@ bool
  i915_request_active_engine(struct i915_request *rq,
  			   struct intel_engine_cs **active);
 
+void i915_request_notify_execute_cb_imm(struct i915_request *rq);
+
  #endif /* I915_REQUEST_H */
#103 lore raw

Re: [Intel-gfx] [PATCH 31/47] drm/i915/guc: Reset implementation for new GuC interface

Quoted 12 lines · Tvrtko Ursulin
Quoted 9 lines · Matthew Brost

Reset implementation for new GuC interface. This is the legacy reset implementation which is called when the i915 owns the engine hang check. Future patches will offload the engine hang check to GuC but we will continue to maintain this legacy path as a fallback and this code path is also required if the GuC dies.

With the new GuC interface it is not possible to reset individual engines - it is only possible to reset the GPU entirely. This patch forces an entire chip reset if any engine hangs.

Quoted 6 lines

No updates after my review comments on 6th of May.

At least:

1. wmb documentation

Yea, missed this. Checkpatch yelled at me too. Will be fixed in next rev.

Quoted 5 lines

2. Spin lock cycling I either didn't understand or didn't buy the explanation. I don't remember seeing that pattern elsewhere in the driver - cycle a spinlock to make sure what was updated inside it is visible you said?

I did respond - not really my fault if you don't understand a fairly simple concept but I'll explain again.

Change a variable
Cycle a lock
At this point we know anyone that acquires above lock the variable change is visible.

I can't be the first person in the Linux kernel to do this nor in the i915.

This basically allows to seal all the reset races without a BKL.

Also I told you I explain in this a doc patch that will get reposted after GuC submission lands:
https://patchwork.freedesktop.org/patch/432408/?series=89844&rev=1

Quoted 5 lines

3. Dropping the lock protecting the list in the middle of list_for_each_entry_safe and just continuing to iterate like nothing happened. (__unwind_incomplete_requests) Again, perhaps I did not understand your explanation properly but you did appear to write:

To be honest looking at the code now we likely don't need to drop the look but regardless I don't think we should change this for the following reasons.

1. I assure you this is safe and works. I can add a better comment explaining this though.
2. This is thoroughly tested and resets are the hardest thing to get stable and working.
3. This code is literally going to get deleted when we move to the DRM scheduler as all the tracking / unwinding / resubmission will be in the DRM scheduler core.
4. A 2 second search of the driver found that we do the same thing in intel_gt_retire_requests_timeout so this isn't unprecedented.

Matt

Quoted 1,358 lines

"""
We only need the active lock for ce->guc_active.requests list. It is indeed safe to drop the lock.
"""

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -0
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry_safe(rq, rn,
+				 &ce->guc_active.requests,
+				 sched.link) {
+		if (i915_request_completed(rq))
+			continue;
+
+		list_del_init(&rq->sched.link);
+		spin_unlock(&ce->guc_active.lock);
...
+		spin_lock(&ce->guc_active.lock);
+	}

Safe iterator guards against list_del but dropping the lock means the state of the overall list can change so next pointer may or may not be valid, requests may be missed, I don't know. Needs a comment explaining why it is safe.

Regards,

Tvrtko

Quoted 1,331 lines
---
  drivers/gpu/drm/i915/gt/intel_context.c       |   3 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |   7 +
  drivers/gpu/drm/i915/gt/intel_engine_types.h  |   6 +
  .../drm/i915/gt/intel_execlists_submission.c  |  40 ++
  drivers/gpu/drm/i915/gt/intel_gt_pm.c         |   6 +-
  drivers/gpu/drm/i915/gt/intel_reset.c         |  18 +-
  .../gpu/drm/i915/gt/intel_ring_submission.c   |  22 +
  drivers/gpu/drm/i915/gt/mock_engine.c         |  31 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.c        |  13 -
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   8 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 581 ++++++++++++++----
  drivers/gpu/drm/i915/gt/uc/intel_uc.c         |  39 +-
  drivers/gpu/drm/i915/gt/uc/intel_uc.h         |   3 +
  drivers/gpu/drm/i915/i915_request.c           |  41 +-
  drivers/gpu/drm/i915/i915_request.h           |   2 +
  15 files changed, 649 insertions(+), 171 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index b24a1b7a3f88..2f01437056a8 100644
@@ -392,6 +392,9 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	spin_lock_init(&ce->guc_state.lock);
  	INIT_LIST_HEAD(&ce->guc_state.fences);
+	spin_lock_init(&ce->guc_active.lock);
+	INIT_LIST_HEAD(&ce->guc_active.requests);
+
  	ce->guc_id = GUC_INVALID_LRC_ID;
  	INIT_LIST_HEAD(&ce->guc_id_link);
drivers/gpu/drm/i915/gt/intel_context_types.h +7 -0
index 6945963a31ba..b63c8cf7823b 100644
@@ -165,6 +165,13 @@ struct intel_context {
  		struct list_head fences;
  	} guc_state;
+	struct {
+		/** lock: protects everything in guc_active */
+		spinlock_t lock;
+		/** requests: active requests on this context */
+		struct list_head requests;
+	} guc_active;
+
  	/* GuC scheduling state that does not require a lock. */
  	atomic_t guc_sched_state_no_lock;
drivers/gpu/drm/i915/gt/intel_engine_types.h +6 -0
index e7cb6a06db9d..f9d264c008e8 100644
@@ -426,6 +426,12 @@ struct intel_engine_cs {
  	void		(*release)(struct intel_engine_cs *engine);
+	/*
+	 * Add / remove request from engine active tracking
+	 */
+	void		(*add_active_request)(struct i915_request *rq);
+	void		(*remove_active_request)(struct i915_request *rq);
+
  	struct intel_engine_execlists execlists;
  	/*
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +40 -0
index c10ea6080752..c301a2d088b1 100644
@@ -3118,6 +3118,42 @@ static void execlists_park(struct intel_engine_cs *engine)
  	cancel_timer(&engine->execlists.preempt);
  }
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&locked->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static bool can_preempt(struct intel_engine_cs *engine)
  {
  	if (GRAPHICS_VER(engine->i915) > 8)
@@ -3218,6 +3254,8 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &execlists_context_ops;
  	engine->request_alloc = execlists_request_alloc;
  	engine->bump_serial = execlist_bump_serial;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
  	engine->reset.prepare = execlists_reset_prepare;
  	engine->reset.rewind = execlists_reset_rewind;
@@ -3912,6 +3950,8 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  			 "v%dx%d", ve->base.class, count);
  		ve->base.context_size = sibling->context_size;
+		ve->base.add_active_request = sibling->add_active_request;
+		ve->base.remove_active_request = sibling->remove_active_request;
  		ve->base.emit_bb_start = sibling->emit_bb_start;
  		ve->base.emit_flush = sibling->emit_flush;
  		ve->base.emit_init_breadcrumb = sibling->emit_init_breadcrumb;
drivers/gpu/drm/i915/gt/intel_gt_pm.c +4 -2
index aef3084e8b16..463a6ae605a0 100644
@@ -174,8 +174,6 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
  	if (intel_gt_is_wedged(gt))
  		intel_gt_unset_wedged(gt);
-	intel_uc_sanitize(&gt->uc);
-
  	for_each_engine(engine, gt, id)
  		if (engine->reset.prepare)
  			engine->reset.prepare(engine);
@@ -191,6 +189,8 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
  			__intel_engine_reset(engine, false);
  	}
+	intel_uc_reset(&gt->uc, false);
+
  	for_each_engine(engine, gt, id)
  		if (engine->reset.finish)
  			engine->reset.finish(engine);
@@ -243,6 +243,8 @@ int intel_gt_resume(struct intel_gt *gt)
  		goto err_wedged;
  	}
+	intel_uc_reset_finish(&gt->uc);
+
  	intel_rps_enable(&gt->rps);
  	intel_llc_enable(&gt->llc);
drivers/gpu/drm/i915/gt/intel_reset.c +12 -6
index 72251638d4ea..2987282dff6d 100644
@@ -826,6 +826,8 @@ static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
  		__intel_engine_reset(engine, stalled_mask & engine->mask);
  	local_bh_enable();
+	intel_uc_reset(&gt->uc, true);
+
  	intel_ggtt_restore_fences(gt->ggtt);
  	return err;
@@ -850,6 +852,8 @@ static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
  		if (awake & engine->mask)
  			intel_engine_pm_put(engine);
  	}
+
+	intel_uc_reset_finish(&gt->uc);
  }
  static void nop_submit_request(struct i915_request *request)
@@ -903,6 +907,7 @@ static void __intel_gt_set_wedged(struct intel_gt *gt)
  	for_each_engine(engine, gt, id)
  		if (engine->reset.cancel)
  			engine->reset.cancel(engine);
+	intel_uc_cancel_requests(&gt->uc);
  	local_bh_enable();
  	reset_finish(gt, awake);
@@ -1191,6 +1196,9 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
  	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
  	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
+	if (intel_engine_uses_guc(engine))
+		return -ENODEV;
+
  	if (!intel_engine_pm_get_if_awake(engine))
  		return 0;
@@ -1201,13 +1209,10 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
  			   "Resetting %s for %s\n", engine->name, msg);
  	atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
-	if (intel_engine_uses_guc(engine))
-		ret = intel_guc_reset_engine(&engine->gt->uc.guc, engine);
-	else
-		ret = intel_gt_reset_engine(engine);
+	ret = intel_gt_reset_engine(engine);
  	if (ret) {
  		/* If we fail here, we expect to fallback to a global reset */
-		ENGINE_TRACE(engine, "Failed to reset, err: %d\n", ret);
+		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
  		goto out;
  	}
@@ -1341,7 +1346,8 @@ void intel_gt_handle_error(struct intel_gt *gt,
  	 * Try engine reset when available. We fall back to full reset if
  	 * single reset fails.
  	 */
-	if (intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
+	if (!intel_uc_uses_guc_submission(&gt->uc) &&
+	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
  		local_bh_disable();
  		for_each_engine_masked(engine, gt, engine_mask, tmp) {
  			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
drivers/gpu/drm/i915/gt/intel_ring_submission.c +22 -0
index e1506b280df1..99dcdc8fba12 100644
@@ -1049,6 +1049,25 @@ static void ring_bump_serial(struct intel_engine_cs *engine)
  	engine->serial++;
  }
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	spin_lock_irq(&rq->engine->sched_engine->lock);
+	list_del_init(&rq->sched.link);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&rq->engine->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static void setup_common(struct intel_engine_cs *engine)
  {
  	struct drm_i915_private *i915 = engine->i915;
@@ -1066,6 +1085,9 @@ static void setup_common(struct intel_engine_cs *engine)
  	engine->reset.cancel = reset_cancel;
  	engine->reset.finish = reset_finish;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
+
  	engine->cops = &ring_context_ops;
  	engine->request_alloc = ring_request_alloc;
  	engine->bump_serial = ring_bump_serial;
drivers/gpu/drm/i915/gt/mock_engine.c +31 -0
index fc5a65ab1937..c12ff3a75ce6 100644
@@ -235,6 +235,35 @@ static void mock_submit_request(struct i915_request *request)
  	spin_unlock_irqrestore(&engine->hw_lock, flags);
  }
+static void mock_add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void mock_remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+	spin_unlock_irq(&locked->sched_engine->lock);
+}
+
+
  static void mock_reset_prepare(struct intel_engine_cs *engine)
  {
  }
@@ -327,6 +356,8 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
  	engine->base.emit_flush = mock_emit_flush;
  	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
  	engine->base.submit_request = mock_submit_request;
+	engine->base.add_active_request = mock_add_to_engine;
+	engine->base.remove_active_request = mock_remove_from_engine;
  	engine->base.reset.prepare = mock_reset_prepare;
  	engine->base.reset.rewind = mock_reset_rewind;
drivers/gpu/drm/i915/gt/uc/intel_guc.c +0 -13
index 6661dcb02239..9b09395b998f 100644
@@ -572,19 +572,6 @@ int intel_guc_suspend(struct intel_guc *guc)
  	return 0;
  }
-/**
- * intel_guc_reset_engine() - ask GuC to reset an engine
- * @guc:	intel_guc structure
- * @engine:	engine to be reset
- */
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine)
-{
-	/* XXX: to be implemented with submission interface rework */
-
-	return -ENODEV;
-}
-
  /**
   * intel_guc_resume() - notify GuC resuming from suspend state
   * @guc:	the guc
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 22eb1e9cca41..40c9868762d7 100644
@@ -242,14 +242,16 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
  int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout);
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine);
-
  int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  					  const u32 *msg, u32 len);
  int intel_guc_sched_done_process_msg(struct intel_guc *guc,
  				     const u32 *msg, u32 len);
+void intel_guc_submission_reset_prepare(struct intel_guc *guc);
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled);
+void intel_guc_submission_reset_finish(struct intel_guc *guc);
+void intel_guc_submission_cancel_requests(struct intel_guc *guc);
+
  void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
  #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +472 -109
index 83058df5ba01..b8c894ad8caf 100644
@@ -141,7 +141,7 @@ context_wait_for_deregister_to_register(struct intel_context *ce)
  static inline void
  set_context_wait_for_deregister_to_register(struct intel_context *ce)
  {
-	/* Only should be called from guc_lrc_desc_pin() */
+	/* Only should be called from guc_lrc_desc_pin() without lock */
  	ce->guc_state.sched_state |=
  		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
  }
@@ -241,15 +241,31 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
  static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
  {
+	guc->lrc_desc_pool_vaddr = NULL;
  	i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
  }
+static inline bool guc_submission_initialized(struct intel_guc *guc)
+{
+	return guc->lrc_desc_pool_vaddr != NULL;
+}
+
  static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
  {
-	struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+	if (likely(guc_submission_initialized(guc))) {
+		struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+		unsigned long flags;
-	memset(desc, 0, sizeof(*desc));
-	xa_erase_irq(&guc->context_lookup, id);
+		memset(desc, 0, sizeof(*desc));
+
+		/*
+		 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
+		 * the lower level functions directly.
+		 */
+		xa_lock_irqsave(&guc->context_lookup, flags);
+		__xa_erase(&guc->context_lookup, id);
+		xa_unlock_irqrestore(&guc->context_lookup, flags);
+	}
  }
  static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
@@ -260,7 +276,15 @@ static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
  static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
  					   struct intel_context *ce)
  {
-	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	unsigned long flags;
+
+	/*
+	 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
+	 * lower level functions directly.
+	 */
+	xa_lock_irqsave(&guc->context_lookup, flags);
+	__xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	xa_unlock_irqrestore(&guc->context_lookup, flags);
  }
  static int guc_submission_busy_loop(struct intel_guc* guc,
@@ -331,6 +355,8 @@ int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
  					interruptible, timeout);
  }
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);
+
  static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  {
  	int err;
@@ -338,11 +364,22 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	u32 action[3];
  	int len = 0;
  	u32 g2h_len_dw = 0;
-	bool enabled = context_enabled(ce);
+	bool enabled;
  	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
  	GEM_BUG_ON(context_guc_id_invalid(ce));
+	/*
+	 * Corner case where the GuC firmware was blown away and reloaded while
+	 * this context was pinned.
+	 */
+	if (unlikely(!lrc_desc_registered(guc, ce->guc_id))) {
+		err = guc_lrc_desc_pin(ce, false);
+		if (unlikely(err))
+			goto out;
+	}
+	enabled = context_enabled(ce);
+
  	if (!enabled) {
  		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
  		action[len++] = ce->guc_id;
@@ -365,6 +402,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  		intel_context_put(ce);
  	}
+out:
  	return err;
  }
@@ -419,15 +457,10 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  	if (submit) {
  		guc_set_lrc_tail(last);
  resubmit:
-		/*
-		 * We only check for -EBUSY here even though it is possible for
-		 * -EDEADLK to be returned. If -EDEADLK is returned, the GuC has
-		 * died and a full GPU needs to be done. The hangcheck will
-		 * eventually detect that the GuC has died and trigger this
-		 * reset so no need to handle -EDEADLK here.
-		 */
  		ret = guc_add_request(guc, last);
-		if (ret == -EBUSY) {
+		if (unlikely(ret == -EIO))
+			goto deadlk;
+		else if (ret == -EBUSY) {
  			tasklet_schedule(&sched_engine->tasklet);
  			guc->stalled_request = last;
  			return false;
@@ -437,6 +470,11 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
  	guc->stalled_request = NULL;
  	return submit;
+
+deadlk:
+	sched_engine->tasklet.callback = NULL;
+	tasklet_disable_nosync(&sched_engine->tasklet);
+	return false;
  }
  static void guc_submission_tasklet(struct tasklet_struct *t)
@@ -463,27 +501,165 @@ static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
  		intel_engine_signal_breadcrumbs(engine);
  }
-static void guc_reset_prepare(struct intel_engine_cs *engine)
+static void __guc_context_destroy(struct intel_context *ce);
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
+static void guc_signal_context_fence(struct intel_context *ce);
+
+static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	unsigned long index, flags;
+	bool pending_disable, pending_enable, deregister, destroyed;
+
+	xa_for_each(&guc->context_lookup, index, ce) {
+		/* Flush context */
+		spin_lock_irqsave(&ce->guc_state.lock, flags);
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+
+		/*
+		 * Once we are at this point submission_disabled() is guaranteed
+		 * to visible to all callers who set the below flags (see above
+		 * flush and flushes in reset_prepare). If submission_disabled()
+		 * is set, the caller shouldn't set these flags.
+		 */
+
+		destroyed = context_destroyed(ce);
+		pending_enable = context_pending_enable(ce);
+		pending_disable = context_pending_disable(ce);
+		deregister = context_wait_for_deregister_to_register(ce);
+		init_sched_state(ce);
+
+		if (pending_enable || destroyed || deregister) {
+			atomic_dec(&guc->outstanding_submission_g2h);
+			if (deregister)
+				guc_signal_context_fence(ce);
+			if (destroyed) {
+				release_guc_id(guc, ce);
+				__guc_context_destroy(ce);
+			}
+			if (pending_enable|| deregister)
+				intel_context_put(ce);
+		}
+
+		/* Not mutualy exclusive with above if statement. */
+		if (pending_disable) {
+			guc_signal_context_fence(ce);
+			intel_context_sched_disable_unpin(ce);
+			atomic_dec(&guc->outstanding_submission_g2h);
+			intel_context_put(ce);
+		}
+	}
+}
+
+static inline bool
+submission_disabled(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	return unlikely(!__tasklet_is_enabled(&sched_engine->tasklet));
+}
+
+static void disable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	if (__tasklet_is_enabled(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+		__tasklet_disable_sync_once(&sched_engine->tasklet);
+		sched_engine->tasklet.callback = NULL;
+	}
+}
+
+static void enable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->sched_engine->lock, flags);
+	sched_engine->tasklet.callback = guc_submission_tasklet;
+	wmb();
+	if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
+	    __tasklet_enable(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+
+		/* And kick in case we missed a new request submission. */
+		tasklet_hi_schedule(&sched_engine->tasklet);
+	}
+	spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
+}
+
+static void guc_flush_submissions(struct intel_guc *guc)
  {
-	ENGINE_TRACE(engine, "\n");
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+void intel_guc_submission_reset_prepare(struct intel_guc *guc)
+{
+	int i;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;
+
+	disable_submission(guc);
+	guc->interrupts.disable(guc);
+
+	/* Flush IRQ handler */
+	spin_lock_irq(&guc_to_gt(guc)->irq_lock);
+	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
+
+	guc_flush_submissions(guc);
  	/*
-	 * Prevent request submission to the hardware until we have
-	 * completed the reset in i915_gem_reset_finish(). If a request
-	 * is completed by one engine, it may then queue a request
-	 * to a second via its execlists->tasklet *just* as we are
-	 * calling engine->init_hw() and also writing the ELSP.
-	 * Turning off the execlists->tasklet until the reset is over
-	 * prevents the race.
-	 */
-	__tasklet_disable_sync_once(&engine->sched_engine->tasklet);
+	 * Handle any outstanding G2Hs before reset. Call IRQ handler directly
+	 * each pass as interrupt have been disabled. We always scrub for
+	 * outstanding G2H as it is possible for outstanding_submission_g2h to
+	 * be incremented after the context state update.
+ 	 */
+	for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
+		intel_guc_to_host_event_handler(guc);
+#define wait_for_reset(guc, wait_var) \
+		guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
+		do {
+			wait_for_reset(guc, &guc->outstanding_submission_g2h);
+		} while (!list_empty(&guc->ct.requests.incoming));
+	}
+	scrub_guc_desc_for_outstanding_g2h(guc);
  }
-static void guc_reset_state(struct intel_context *ce,
-			    struct intel_engine_cs *engine,
-			    u32 head,
-			    bool scrub)
+static struct intel_engine_cs *
+guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
  {
+	struct intel_engine_cs *engine;
+	intel_engine_mask_t tmp, mask = ve->mask;
+	unsigned int num_siblings = 0;
+
+	for_each_engine_masked(engine, ve->gt, mask, tmp)
+		if (num_siblings++ == sibling)
+			return engine;
+
+	return NULL;
+}
+
+static inline struct intel_engine_cs *
+__context_to_physical_engine(struct intel_context *ce)
+{
+	struct intel_engine_cs *engine = ce->engine;
+
+	if (intel_engine_is_virtual(engine))
+		engine = guc_virtual_get_sibling(engine, 0);
+
+	return engine;
+}
+
+static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
+{
+	struct intel_engine_cs *engine = __context_to_physical_engine(ce);
+
  	GEM_BUG_ON(!intel_context_is_pinned(ce));
  	/*
@@ -501,42 +677,147 @@ static void guc_reset_state(struct intel_context *ce,
  	lrc_update_regs(ce, engine, head);
  }
-static void guc_reset_rewind(struct intel_engine_cs *engine, bool stalled)
+static void guc_reset_nop(struct intel_engine_cs *engine)
  {
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_request *rq;
+}
+
+static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
+{
+}
+
+static void
+__unwind_incomplete_requests(struct intel_context *ce)
+{
+	struct i915_request *rq, *rn;
+	struct list_head *pl;
+	int prio = I915_PRIORITY_INVALID;
+	struct i915_sched_engine * const sched_engine =
+		ce->engine->sched_engine;
  	unsigned long flags;
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry_safe(rq, rn,
+				 &ce->guc_active.requests,
+				 sched.link) {
+		if (i915_request_completed(rq))
+			continue;
+
+		list_del_init(&rq->sched.link);
+		spin_unlock(&ce->guc_active.lock);
+
+		__i915_request_unsubmit(rq);
+
+		/* Push the request back into the queue for later resubmission. */
+		GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
+		if (rq_prio(rq) != prio) {
+			prio = rq_prio(rq);
+			pl = i915_sched_lookup_priolist(sched_engine, prio);
+		}
+		GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
+
+		list_add_tail(&rq->sched.link, pl);
+		set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-	/* Push back any incomplete requests for replay after the reset. */
-	rq = execlists_unwind_incomplete_requests(execlists);
-	if (!rq)
-		goto out_unlock;
+		spin_lock(&ce->guc_active.lock);
+	}
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static struct i915_request *context_find_active_request(struct intel_context *ce)
+{
+	struct i915_request *rq, *active = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ce->guc_active.lock, flags);
+	list_for_each_entry_reverse(rq, &ce->guc_active.requests,
+				    sched.link) {
+		if (i915_request_completed(rq))
+			break;
+
+		active = rq;
+	}
+	spin_unlock_irqrestore(&ce->guc_active.lock, flags);
+
+	return active;
+}
+
+static void __guc_reset_context(struct intel_context *ce, bool stalled)
+{
+	struct i915_request *rq;
+	u32 head;
+
+	/*
+	 * GuC will implicitly mark the context as non-schedulable
+	 * when it sends the reset notification. Make sure our state
+	 * reflects this change. The context will be marked enabled
+	 * on resubmission.
+	 */
+	clr_context_enabled(ce);
+
+	rq = context_find_active_request(ce);
+	if (!rq) {
+		head = ce->ring->tail;
+		stalled = false;
+		goto out_replay;
+	}
  	if (!i915_request_started(rq))
  		stalled = false;
+	GEM_BUG_ON(i915_active_is_idle(&ce->active));
+	head = intel_ring_wrap(ce->ring, rq->head);
  	__i915_request_reset(rq, stalled);
-	guc_reset_state(rq->context, engine, rq->head, stalled);
-out_unlock:
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+out_replay:
+	guc_reset_state(ce, head, stalled);
+	__unwind_incomplete_requests(ce);
  }
-static void guc_reset_cancel(struct intel_engine_cs *engine)
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
+{
+	struct intel_context *ce;
+	unsigned long index;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			__guc_reset_context(ce, stalled);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);
+}
+
+static void guc_cancel_context_requests(struct intel_context *ce)
+{
+	struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
+	struct i915_request *rq;
+	unsigned long flags;
+
+	/* Mark all executing requests as skipped. */
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry(rq, &ce->guc_active.requests, sched.link)
+		i915_request_put(i915_request_mark_eio(rq));
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static void
+guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
  {
-	struct i915_sched_engine * const sched_engine = engine->sched_engine;
  	struct i915_request *rq, *rn;
  	struct rb_node *rb;
  	unsigned long flags;
  	/* Can be called during boot if GuC fails to load */
-	if (!engine->gt)
+	if (!sched_engine)
  		return;
-	ENGINE_TRACE(engine, "\n");
-
  	/*
  	 * Before we call engine->cancel_requests(), we should have exclusive
  	 * access to the submission state. This is arranged for us by the
@@ -553,21 +834,16 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	 */
  	spin_lock_irqsave(&sched_engine->lock, flags);
-	/* Mark all executing requests as skipped. */
-	list_for_each_entry(rq, &sched_engine->requests, sched.link) {
-		i915_request_set_error_once(rq, -EIO);
-		i915_request_mark_complete(rq);
-	}
-
  	/* Flush the queued requests to the timeline list (for retiring). */
  	while ((rb = rb_first_cached(&sched_engine->queue))) {
  		struct i915_priolist *p = to_priolist(rb);
  		priolist_for_each_request_consume(rq, rn, p) {
  			list_del_init(&rq->sched.link);
+
  			__i915_request_submit(rq);
-			dma_fence_set_error(&rq->fence, -EIO);
-			i915_request_mark_complete(rq);
+
+			i915_request_put(i915_request_mark_eio(rq));
  		}
  		rb_erase_cached(&p->node, &sched_engine->queue);
@@ -582,14 +858,38 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
-static void guc_reset_finish(struct intel_engine_cs *engine)
+void intel_guc_submission_cancel_requests(struct intel_guc *guc)
  {
-	if (__tasklet_enable(&engine->sched_engine->tasklet))
-		/* And kick in case we missed a new request submission. */
-		tasklet_hi_schedule(&engine->sched_engine->tasklet);
+	struct intel_context *ce;
+	unsigned long index;
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			guc_cancel_context_requests(ce);
-	ENGINE_TRACE(engine, "depth->%d\n",
-		     atomic_read(&engine->sched_engine->tasklet.count));
+	guc_cancel_sched_engine_requests(guc->sched_engine);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);
+}
+
+void intel_guc_submission_reset_finish(struct intel_guc *guc)
+{
+	/* Reset called during driver load or during wedge? */
+	if (unlikely(!guc_submission_initialized(guc) ||
+		     test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags)))
+		return;
+
+	/*
+	 * Technically possible for either of these values to be non-zero here,
+	 * but very unlikely + harmless. Regardless let's add a warn so we can
+	 * see in CI if this happens frequently / a precursor to taking down the
+	 * machine.
+	 */
+	GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
+	atomic_set(&guc->outstanding_submission_g2h, 0);
+
+	enable_submission(guc);
  }
  /*
@@ -656,6 +956,9 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
  	else
  		trace_i915_request_guc_submit(rq);
+	if (unlikely(ret == -EIO))
+		disable_submission(guc);
+
  	return ret;
  }
@@ -668,7 +971,8 @@ static void guc_submit_request(struct i915_request *rq)
  	/* Will be called from irq-context when using foreign fences. */
  	spin_lock_irqsave(&sched_engine->lock, flags);
-	if (guc->stalled_request || !i915_sched_engine_is_empty(sched_engine))
+	if (submission_disabled(guc) || guc->stalled_request ||
+	    !i915_sched_engine_is_empty(sched_engine))
  		queue_request(sched_engine, rq, rq_prio(rq));
  	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
  		tasklet_hi_schedule(&sched_engine->tasklet);
@@ -805,7 +1109,8 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  static int __guc_action_register_context(struct intel_guc *guc,
  					 u32 guc_id,
-					 u32 offset)
+					 u32 offset,
+					 bool loop)
  {
  	u32 action[] = {
  		INTEL_GUC_ACTION_REGISTER_CONTEXT,
@@ -813,10 +1118,10 @@ static int __guc_action_register_context(struct intel_guc *guc,
  		offset,
  	};
-	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
+	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, loop);
  }
-static int register_context(struct intel_context *ce)
+static int register_context(struct intel_context *ce, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
  	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
@@ -824,11 +1129,12 @@ static int register_context(struct intel_context *ce)
  	trace_intel_context_register(ce);
-	return __guc_action_register_context(guc, ce->guc_id, offset);
+	return __guc_action_register_context(guc, ce->guc_id, offset, loop);
  }
  static int __guc_action_deregister_context(struct intel_guc *guc,
-					   u32 guc_id)
+					   u32 guc_id,
+					   bool loop)
  {
  	u32 action[] = {
  		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
@@ -836,16 +1142,16 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
  	};
  	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
-					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
+					G2H_LEN_DW_DEREGISTER_CONTEXT, loop);
  }
-static int deregister_context(struct intel_context *ce, u32 guc_id)
+static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
  	trace_intel_context_deregister(ce);
-	return __guc_action_deregister_context(guc, guc_id);
+	return __guc_action_deregister_context(guc, guc_id, loop);
  }
  static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
@@ -874,7 +1180,7 @@ static void guc_context_policy_init(struct intel_engine_cs *engine,
  	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
  }
-static int guc_lrc_desc_pin(struct intel_context *ce)
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  {
  	struct intel_runtime_pm *runtime_pm =
  		&ce->engine->gt->i915->runtime_pm;
@@ -920,18 +1226,44 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
  	 */
  	if (context_registered) {
  		trace_intel_context_steal_guc_id(ce);
-		set_context_wait_for_deregister_to_register(ce);
-		intel_context_get(ce);
+		if (!loop) {
+			set_context_wait_for_deregister_to_register(ce);
+			intel_context_get(ce);
+		} else {
+			bool disabled;
+			unsigned long flags;
+
+			/* Seal race with Reset */
+			spin_lock_irqsave(&ce->guc_state.lock, flags);
+			disabled = submission_disabled(guc);
+			if (likely(!disabled)) {
+				set_context_wait_for_deregister_to_register(ce);
+				intel_context_get(ce);
+			}
+			spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+			if (unlikely(disabled)) {
+				reset_lrc_desc(guc, desc_idx);
+				return 0;	/* Will get registered later */
+			}
+		}
  		/*
  		 * If stealing the guc_id, this ce has the same guc_id as the
  		 * context whos guc_id was stole.
  		 */
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = deregister_context(ce, ce->guc_id);
+			ret = deregister_context(ce, ce->guc_id, loop);
+		if (unlikely(ret == -EBUSY)) {
+			clr_context_wait_for_deregister_to_register(ce);
+			intel_context_put(ce);
+		}
  	} else {
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = register_context(ce);
+			ret = register_context(ce, loop);
+		if (unlikely(ret == -EBUSY))
+			reset_lrc_desc(guc, desc_idx);
+		else if (unlikely(ret == -ENODEV))
+			ret = 0;	/* Will get registered later */
  	}
  	return ret;
@@ -994,7 +1326,6 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
  	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
  	trace_intel_context_sched_disable(ce);
-	intel_context_get(ce);
  	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
  				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
@@ -1004,6 +1335,7 @@ static u16 prep_context_pending_disable(struct intel_context *ce)
  {
  	set_context_pending_disable(ce);
  	clr_context_enabled(ce);
+	intel_context_get(ce);
  	return ce->guc_id;
  }
@@ -1016,7 +1348,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	u16 guc_id;
  	intel_wakeref_t wakeref;
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		clr_context_enabled(ce);
  		goto unpin;
@@ -1034,6 +1366,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	 * request doesn't slip through the 'context_pending_disable' fence.
  	 */
  	if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
  		return;
  	}
  	guc_id = prep_context_pending_disable(ce);
@@ -1050,19 +1383,13 @@ static void guc_context_sched_disable(struct intel_context *ce)
  static inline void guc_lrc_desc_unpin(struct intel_context *ce)
  {
-	struct intel_engine_cs *engine = ce->engine;
-	struct intel_guc *guc = &engine->gt->uc.guc;
-	unsigned long flags;
+	struct intel_guc *guc = ce_to_guc(ce);
  	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
  	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
  	GEM_BUG_ON(context_enabled(ce));
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	set_context_destroyed(ce);
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-
-	deregister_context(ce, ce->guc_id);
+	deregister_context(ce, ce->guc_id, true);
  }
  static void __guc_context_destroy(struct intel_context *ce)
@@ -1090,13 +1417,15 @@ static void guc_context_destroy(struct kref *kref)
  	struct intel_guc *guc = &ce->engine->gt->uc.guc;
  	intel_wakeref_t wakeref;
  	unsigned long flags;
+	bool disabled;
  	/*
  	 * If the guc_id is invalid this context has been stolen and we can free
  	 * it immediately. Also can be freed immediately if the context is not
  	 * registered with the GuC.
  	 */
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) ||
+	    context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id)) {
  		release_guc_id(guc, ce);
  		__guc_context_destroy(ce);
@@ -1123,6 +1452,18 @@ static void guc_context_destroy(struct kref *kref)
  		list_del_init(&ce->guc_id_link);
  	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled))
+		set_context_destroyed(ce);
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
  	/*
  	 * We defer GuC context deregistration until the context is destroyed
  	 * in order to save on CTBs. With this optimization ideally we only need
@@ -1145,6 +1486,33 @@ static int guc_context_alloc(struct intel_context *ce)
  	return lrc_alloc(ce, ce->engine);
  }
+static void add_to_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock(&ce->guc_active.lock);
+	list_move_tail(&rq->sched.link, &ce->guc_active.requests);
+	spin_unlock(&ce->guc_active.lock);
+}
+
+static void remove_from_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock_irq(&ce->guc_active.lock);
+
+	list_del_init(&rq->sched.link);
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&ce->guc_active.lock);
+
+	atomic_dec(&ce->guc_id_ref);
+	i915_request_notify_execute_cb_imm(rq);
+}
+
  static const struct intel_context_ops guc_context_ops = {
  	.alloc = guc_context_alloc,
@@ -1183,8 +1551,6 @@ static void guc_signal_context_fence(struct intel_context *ce)
  {
  	unsigned long flags;
-	GEM_BUG_ON(!context_wait_for_deregister_to_register(ce));
-
  	spin_lock_irqsave(&ce->guc_state.lock, flags);
  	clr_context_wait_for_deregister_to_register(ce);
  	__guc_signal_context_fence(ce);
@@ -1193,8 +1559,9 @@ static void guc_signal_context_fence(struct intel_context *ce)
  static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
  {
-	return new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
-		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id);
+	return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
+		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id)) &&
+		!submission_disabled(ce_to_guc(ce));
  }
  static int guc_request_alloc(struct i915_request *rq)
@@ -1252,8 +1619,12 @@ static int guc_request_alloc(struct i915_request *rq)
  	if (unlikely(ret < 0))
  		return ret;;
  	if (context_needs_register(ce, !!ret)) {
-		ret = guc_lrc_desc_pin(ce);
+		ret = guc_lrc_desc_pin(ce, true);
  		if (unlikely(ret)) {	/* unwind */
+			if (ret == -EIO) {
+				disable_submission(guc);
+				goto out;	/* GPU will be reset */
+			}
  			atomic_dec(&ce->guc_id_ref);
  			unpin_guc_id(guc, ce);
  			return ret;
@@ -1290,20 +1661,6 @@ static int guc_request_alloc(struct i915_request *rq)
  	return 0;
  }
-static struct intel_engine_cs *
-guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
-{
-	struct intel_engine_cs *engine;
-	intel_engine_mask_t tmp, mask = ve->mask;
-	unsigned int num_siblings = 0;
-
-	for_each_engine_masked(engine, ve->gt, mask, tmp)
-		if (num_siblings++ == sibling)
-			return engine;
-
-	return NULL;
-}
-
  static int guc_virtual_context_pre_pin(struct intel_context *ce,
  				       struct i915_gem_ww_ctx *ww,
  				       void **vaddr)
@@ -1512,7 +1869,7 @@ static inline void guc_kernel_context_pin(struct intel_guc *guc,
  {
  	if (context_guc_id_invalid(ce))
  		pin_guc_id(guc, ce);
-	guc_lrc_desc_pin(ce);
+	guc_lrc_desc_pin(ce, true);
  }
  static inline void guc_init_lrc_mapping(struct intel_guc *guc)
@@ -1578,13 +1935,15 @@ static void guc_default_vfuncs(struct intel_engine_cs *engine)
  	engine->cops = &guc_context_ops;
  	engine->request_alloc = guc_request_alloc;
  	engine->bump_serial = guc_bump_serial;
+	engine->add_active_request = add_to_context;
+	engine->remove_active_request = remove_from_context;
  	engine->sched_engine->schedule = i915_schedule;
-	engine->reset.prepare = guc_reset_prepare;
-	engine->reset.rewind = guc_reset_rewind;
-	engine->reset.cancel = guc_reset_cancel;
-	engine->reset.finish = guc_reset_finish;
+	engine->reset.prepare = guc_reset_nop;
+	engine->reset.rewind = guc_rewind_nop;
+	engine->reset.cancel = guc_reset_nop;
+	engine->reset.finish = guc_reset_nop;
  	engine->emit_flush = gen8_emit_flush_xcs;
  	engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
@@ -1757,7 +2116,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		 * register this context.
  		 */
  		with_intel_runtime_pm(runtime_pm, wakeref)
-			register_context(ce);
+			register_context(ce, true);
  		guc_signal_context_fence(ce);
  		intel_context_put(ce);
  	} else if (context_destroyed(ce)) {
@@ -1939,6 +2298,10 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  				 "v%dx%d", ve->base.class, count);
  			ve->base.context_size = sibling->context_size;
+			ve->base.add_active_request =
+				sibling->add_active_request;
+			ve->base.remove_active_request =
+				sibling->remove_active_request;
  			ve->base.emit_bb_start = sibling->emit_bb_start;
  			ve->base.emit_flush = sibling->emit_flush;
  			ve->base.emit_init_breadcrumb =
drivers/gpu/drm/i915/gt/uc/intel_uc.c +38 -1
index 6d8b9233214e..f0b02200aa01 100644
@@ -565,12 +565,49 @@ void intel_uc_reset_prepare(struct intel_uc *uc)
  {
  	struct intel_guc *guc = &uc->guc;
-	if (!intel_guc_is_ready(guc))
+
+	/* Nothing to do if GuC isn't supported */
+	if (!intel_uc_supports_guc(uc))
  		return;
+	/* Firmware expected to be running when this function is called */
+	if (!intel_guc_is_ready(guc))
+		goto sanitize;
+
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_prepare(guc);
+
+sanitize:
  	__uc_sanitize(uc);
  }
+void intel_uc_reset(struct intel_uc *uc, bool stalled)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset(guc, stalled);
+}
+
+void intel_uc_reset_finish(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware expected to be running when this function is called */
+	if (intel_guc_is_fw_running(guc) && intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_finish(guc);
+}
+
+void intel_uc_cancel_requests(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_cancel_requests(guc);
+}
+
  void intel_uc_runtime_suspend(struct intel_uc *uc)
  {
  	struct intel_guc *guc = &uc->guc;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +3 -0
index c4cef885e984..eaa3202192ac 100644
@@ -37,6 +37,9 @@ void intel_uc_driver_late_release(struct intel_uc *uc);
  void intel_uc_driver_remove(struct intel_uc *uc);
  void intel_uc_init_mmio(struct intel_uc *uc);
  void intel_uc_reset_prepare(struct intel_uc *uc);
+void intel_uc_reset(struct intel_uc *uc, bool stalled);
+void intel_uc_reset_finish(struct intel_uc *uc);
+void intel_uc_cancel_requests(struct intel_uc *uc);
  void intel_uc_suspend(struct intel_uc *uc);
  void intel_uc_runtime_suspend(struct intel_uc *uc);
  int intel_uc_resume(struct intel_uc *uc);
drivers/gpu/drm/i915/i915_request.c +4 -37
index dec5a35c9aa2..192784875a1d 100644
@@ -194,7 +194,7 @@ static bool irq_work_imm(struct irq_work *wrk)
  	return false;
  }
-static void __notify_execute_cb_imm(struct i915_request *rq)
+void i915_request_notify_execute_cb_imm(struct i915_request *rq)
  {
  	__notify_execute_cb(rq, irq_work_imm);
  }
@@ -268,37 +268,6 @@ i915_request_active_engine(struct i915_request *rq,
  	return ret;
  }
-
-static void remove_from_engine(struct i915_request *rq)
-{
-	struct intel_engine_cs *engine, *locked;
-
-	/*
-	 * Virtual engines complicate acquiring the engine timeline lock,
-	 * as their rq->engine pointer is not stable until under that
-	 * engine lock. The simple ploy we use is to take the lock then
-	 * check that the rq still belongs to the newly locked engine.
-	 */
-	locked = READ_ONCE(rq->engine);
-	spin_lock_irq(&locked->sched_engine->lock);
-	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
-		spin_unlock(&locked->sched_engine->lock);
-		spin_lock(&engine->sched_engine->lock);
-		locked = engine;
-	}
-	list_del_init(&rq->sched.link);
-
-	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
-
-	/* Prevent further __await_execution() registering a cb, then flush */
-	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
-
-	spin_unlock_irq(&locked->sched_engine->lock);
-
-	__notify_execute_cb_imm(rq);
-}
-
  static void __rq_init_watchdog(struct i915_request *rq)
  {
  	rq->watchdog.timer.function = NULL;
@@ -395,9 +364,7 @@ bool i915_request_retire(struct i915_request *rq)
  	 * after removing the breadcrumb and signaling it, so that we do not
  	 * inadvertently attach the breadcrumb to a completed request.
  	 */
-	if (!list_empty(&rq->sched.link))
-		remove_from_engine(rq);
-	atomic_dec(&rq->context->guc_id_ref);
+	rq->engine->remove_active_request(rq);
  	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
  	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
@@ -539,7 +506,7 @@ __await_execution(struct i915_request *rq,
  	if (llist_add(&cb->work.node.llist, &signal->execute_cb)) {
  		if (i915_request_is_active(signal) ||
  		    __request_in_flight(signal))
-			__notify_execute_cb_imm(signal);
+			i915_request_notify_execute_cb_imm(signal);
  	}
  	return 0;
@@ -676,7 +643,7 @@ bool __i915_request_submit(struct i915_request *request)
  	result = true;
  	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
-	list_move_tail(&request->sched.link, &engine->sched_engine->requests);
+	engine->add_active_request(request);
  active:
  	clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags);
  	set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
drivers/gpu/drm/i915/i915_request.h +0 -1
index f870cd75a001..bcc6340c505e 100644
--- a/drivers/gpu/drm/i915/i915_request.h
Quoted 8 lines

drivers/gpu/drm/i915/i915_request.h bool · line 654

Quoted 8 lines

+++ b/drivers/gpu/drm/i915/i915_request.h

drivers/gpu/drm/i915/i915_request.h +2 -0
@@ -649,4 +649,6 @@ bool
  i915_request_active_engine(struct i915_request *rq,
  			   struct intel_engine_cs **active);
+void i915_request_notify_execute_cb_imm(struct i915_request *rq);
+
  #endif /* I915_REQUEST_H */
 

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#104 lore raw

Re: [Intel-gfx] [PATCH 31/47] drm/i915/guc: Reset implementation for new GuC interface

Quoted 32 lines · Matthew Brost
Quoted 12 lines · Tvrtko Ursulin
Quoted 9 lines · Matthew Brost

Reset implementation for new GuC interface. This is the legacy reset implementation which is called when the i915 owns the engine hang check. Future patches will offload the engine hang check to GuC but we will continue to maintain this legacy path as a fallback and this code path is also required if the GuC dies.

With the new GuC interface it is not possible to reset individual engines - it is only possible to reset the GPU entirely. This patch forces an entire chip reset if any engine hangs.

Quoted 6 lines

No updates after my review comments on 6th of May.

At least:

1. wmb documentation

Yea, missed this. Checkpatch yelled at me too. Will be fixed in next rev.

Quoted 5 lines

2. Spin lock cycling I either didn't understand or didn't buy the explanation. I don't remember seeing that pattern elsewhere in the driver - cycle a spinlock to make sure what was updated inside it is visible you said?

I did respond - not really my fault if you don't understand a fairly simple concept but I'll explain again.

Quoted 7 lines

Change a variable
Cycle a lock
At this point we know anyone that acquires above lock the variable change is visible.

I can't be the first person in the Linux kernel to do this nor in the i915.

Don't know, did not do an exhaustive search. I can understand it being used to make sure any lock taking sections would exit, if they happened to be running simultaneously to the lock cycling code, but you seem to be describing it being used as a memory barrier.

So either a code comment or just use a memory barrier is my ask. There is a requirement to comment memory barriers anyway so if this is effectively one of them it's pretty clear cut.

Quoted 12 lines

This basically allows to seal all the reset races without a BKL.

Also I told you I explain in this a doc patch that will get reposted after GuC submission lands:
https://patchwork.freedesktop.org/patch/432408/?series=89844&rev=1

Quoted 5 lines

3. Dropping the lock protecting the list in the middle of list_for_each_entry_safe and just continuing to iterate like nothing happened. (__unwind_incomplete_requests) Again, perhaps I did not understand your explanation properly but you did appear to write:

Quoted 3 lines

To be honest looking at the code now we likely don't need to drop the look but regardless I don't think we should change this for the following reasons.

Then don't?

Quoted 2 lines

1. I assure you this is safe and works. I can add a better comment explaining this though.

Yes please for a comment. Assurances are all good until a new bug is found.

Quoted 2 lines

2. This is thoroughly tested and resets are the hardest thing to get stable and working.

Well new bugs are found even after statements of things being well tested so I'd err on the side of caution. And I don't mean your code here but as a general principle.

Quoted 3 lines

3. This code is literally going to get deleted when we move to the DRM scheduler as all the tracking / unwinding / resubmission will be in the DRM scheduler core.

Yeah, but if that cannot be guaranteed to happen in the same kernel release then lets not put dodgy code in.

Quoted 2 lines

4. A 2 second search of the driver found that we do the same thing in intel_gt_retire_requests_timeout so this isn't unprecedented.

The code there is bit different. It uses list_safe_reset_next after re-acquiring the lock and only then unlinks the current element from the list.

It all boils down to whether something can modify the list in parallel in your case. If it can't, just don't take the lock but instead put a comment saying why the lock does not need to be taken would be my suggestion. That way you avoid having to explain why the iteration is not broken.

Regards,

Tvrtko

Quoted 1,369 lines

Matt

Quoted 1,366 lines

"""
We only need the active lock for ce->guc_active.requests list. It is indeed safe to drop the lock.
"""

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -0
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry_safe(rq, rn,
+				 &ce->guc_active.requests,
+				 sched.link) {
+		if (i915_request_completed(rq))
+			continue;
+
+		list_del_init(&rq->sched.link);
+		spin_unlock(&ce->guc_active.lock);
...
+		spin_lock(&ce->guc_active.lock);
+	}

Safe iterator guards against list_del but dropping the lock means the state of the overall list can change so next pointer may or may not be valid, requests may be missed, I don't know. Needs a comment explaining why it is safe.

Regards,

Tvrtko

Quoted 1,339 lines
---
   drivers/gpu/drm/i915/gt/intel_context.c       |   3 +
   drivers/gpu/drm/i915/gt/intel_context_types.h |   7 +
   drivers/gpu/drm/i915/gt/intel_engine_types.h  |   6 +
   .../drm/i915/gt/intel_execlists_submission.c  |  40 ++
   drivers/gpu/drm/i915/gt/intel_gt_pm.c         |   6 +-
   drivers/gpu/drm/i915/gt/intel_reset.c         |  18 +-
   .../gpu/drm/i915/gt/intel_ring_submission.c   |  22 +
   drivers/gpu/drm/i915/gt/mock_engine.c         |  31 +
   drivers/gpu/drm/i915/gt/uc/intel_guc.c        |  13 -
   drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   8 +-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 581 ++++++++++++++----
   drivers/gpu/drm/i915/gt/uc/intel_uc.c         |  39 +-
   drivers/gpu/drm/i915/gt/uc/intel_uc.h         |   3 +
   drivers/gpu/drm/i915/i915_request.c           |  41 +-
   drivers/gpu/drm/i915/i915_request.h           |   2 +
   15 files changed, 649 insertions(+), 171 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index b24a1b7a3f88..2f01437056a8 100644
@@ -392,6 +392,9 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
   	spin_lock_init(&ce->guc_state.lock);
   	INIT_LIST_HEAD(&ce->guc_state.fences);
+	spin_lock_init(&ce->guc_active.lock);
+	INIT_LIST_HEAD(&ce->guc_active.requests);
+
   	ce->guc_id = GUC_INVALID_LRC_ID;
   	INIT_LIST_HEAD(&ce->guc_id_link);
drivers/gpu/drm/i915/gt/intel_context_types.h +7 -0
index 6945963a31ba..b63c8cf7823b 100644
@@ -165,6 +165,13 @@ struct intel_context {
   		struct list_head fences;
   	} guc_state;
+	struct {
+		/** lock: protects everything in guc_active */
+		spinlock_t lock;
+		/** requests: active requests on this context */
+		struct list_head requests;
+	} guc_active;
+
   	/* GuC scheduling state that does not require a lock. */
   	atomic_t guc_sched_state_no_lock;
drivers/gpu/drm/i915/gt/intel_engine_types.h +6 -0
index e7cb6a06db9d..f9d264c008e8 100644
@@ -426,6 +426,12 @@ struct intel_engine_cs {
   	void		(*release)(struct intel_engine_cs *engine);
+	/*
+	 * Add / remove request from engine active tracking
+	 */
+	void		(*add_active_request)(struct i915_request *rq);
+	void		(*remove_active_request)(struct i915_request *rq);
+
   	struct intel_engine_execlists execlists;
   	/*
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +40 -0
index c10ea6080752..c301a2d088b1 100644
@@ -3118,6 +3118,42 @@ static void execlists_park(struct intel_engine_cs *engine)
   	cancel_timer(&engine->execlists.preempt);
   }
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&locked->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
   static bool can_preempt(struct intel_engine_cs *engine)
   {
   	if (GRAPHICS_VER(engine->i915) > 8)
@@ -3218,6 +3254,8 @@ logical_ring_default_vfuncs(struct intel_engine_cs *engine)
   	engine->cops = &execlists_context_ops;
   	engine->request_alloc = execlists_request_alloc;
   	engine->bump_serial = execlist_bump_serial;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
   	engine->reset.prepare = execlists_reset_prepare;
   	engine->reset.rewind = execlists_reset_rewind;
@@ -3912,6 +3950,8 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
   			 "v%dx%d", ve->base.class, count);
   		ve->base.context_size = sibling->context_size;
+		ve->base.add_active_request = sibling->add_active_request;
+		ve->base.remove_active_request = sibling->remove_active_request;
   		ve->base.emit_bb_start = sibling->emit_bb_start;
   		ve->base.emit_flush = sibling->emit_flush;
   		ve->base.emit_init_breadcrumb = sibling->emit_init_breadcrumb;
drivers/gpu/drm/i915/gt/intel_gt_pm.c +4 -2
index aef3084e8b16..463a6ae605a0 100644
@@ -174,8 +174,6 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
   	if (intel_gt_is_wedged(gt))
   		intel_gt_unset_wedged(gt);
-	intel_uc_sanitize(&gt->uc);
-
   	for_each_engine(engine, gt, id)
   		if (engine->reset.prepare)
   			engine->reset.prepare(engine);
@@ -191,6 +189,8 @@ static void gt_sanitize(struct intel_gt *gt, bool force)
   			__intel_engine_reset(engine, false);
   	}
+	intel_uc_reset(&gt->uc, false);
+
   	for_each_engine(engine, gt, id)
   		if (engine->reset.finish)
   			engine->reset.finish(engine);
@@ -243,6 +243,8 @@ int intel_gt_resume(struct intel_gt *gt)
   		goto err_wedged;
   	}
+	intel_uc_reset_finish(&gt->uc);
+
   	intel_rps_enable(&gt->rps);
   	intel_llc_enable(&gt->llc);
drivers/gpu/drm/i915/gt/intel_reset.c +12 -6
index 72251638d4ea..2987282dff6d 100644
@@ -826,6 +826,8 @@ static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
   		__intel_engine_reset(engine, stalled_mask & engine->mask);
   	local_bh_enable();
+	intel_uc_reset(&gt->uc, true);
+
   	intel_ggtt_restore_fences(gt->ggtt);
   	return err;
@@ -850,6 +852,8 @@ static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
   		if (awake & engine->mask)
   			intel_engine_pm_put(engine);
   	}
+
+	intel_uc_reset_finish(&gt->uc);
   }
   static void nop_submit_request(struct i915_request *request)
@@ -903,6 +907,7 @@ static void __intel_gt_set_wedged(struct intel_gt *gt)
   	for_each_engine(engine, gt, id)
   		if (engine->reset.cancel)
   			engine->reset.cancel(engine);
+	intel_uc_cancel_requests(&gt->uc);
   	local_bh_enable();
   	reset_finish(gt, awake);
@@ -1191,6 +1196,9 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
   	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
   	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
+	if (intel_engine_uses_guc(engine))
+		return -ENODEV;
+
   	if (!intel_engine_pm_get_if_awake(engine))
   		return 0;
@@ -1201,13 +1209,10 @@ int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
   			   "Resetting %s for %s\n", engine->name, msg);
   	atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
-	if (intel_engine_uses_guc(engine))
-		ret = intel_guc_reset_engine(&engine->gt->uc.guc, engine);
-	else
-		ret = intel_gt_reset_engine(engine);
+	ret = intel_gt_reset_engine(engine);
   	if (ret) {
   		/* If we fail here, we expect to fallback to a global reset */
-		ENGINE_TRACE(engine, "Failed to reset, err: %d\n", ret);
+		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
   		goto out;
   	}
@@ -1341,7 +1346,8 @@ void intel_gt_handle_error(struct intel_gt *gt,
   	 * Try engine reset when available. We fall back to full reset if
   	 * single reset fails.
   	 */
-	if (intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
+	if (!intel_uc_uses_guc_submission(&gt->uc) &&
+	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
   		local_bh_disable();
   		for_each_engine_masked(engine, gt, engine_mask, tmp) {
   			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
drivers/gpu/drm/i915/gt/intel_ring_submission.c +22 -0
index e1506b280df1..99dcdc8fba12 100644
@@ -1049,6 +1049,25 @@ static void ring_bump_serial(struct intel_engine_cs *engine)
   	engine->serial++;
   }
+static void add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void remove_from_engine(struct i915_request *rq)
+{
+	spin_lock_irq(&rq->engine->sched_engine->lock);
+	list_del_init(&rq->sched.link);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&rq->engine->sched_engine->lock);
+
+	i915_request_notify_execute_cb_imm(rq);
+}
+
   static void setup_common(struct intel_engine_cs *engine)
   {
   	struct drm_i915_private *i915 = engine->i915;
@@ -1066,6 +1085,9 @@ static void setup_common(struct intel_engine_cs *engine)
   	engine->reset.cancel = reset_cancel;
   	engine->reset.finish = reset_finish;
+	engine->add_active_request = add_to_engine;
+	engine->remove_active_request = remove_from_engine;
+
   	engine->cops = &ring_context_ops;
   	engine->request_alloc = ring_request_alloc;
   	engine->bump_serial = ring_bump_serial;
drivers/gpu/drm/i915/gt/mock_engine.c +31 -0
index fc5a65ab1937..c12ff3a75ce6 100644
@@ -235,6 +235,35 @@ static void mock_submit_request(struct i915_request *request)
   	spin_unlock_irqrestore(&engine->hw_lock, flags);
   }
+static void mock_add_to_engine(struct i915_request *rq)
+{
+	lockdep_assert_held(&rq->engine->sched_engine->lock);
+	list_move_tail(&rq->sched.link, &rq->engine->sched_engine->requests);
+}
+
+static void mock_remove_from_engine(struct i915_request *rq)
+{
+	struct intel_engine_cs *engine, *locked;
+
+	/*
+	 * Virtual engines complicate acquiring the engine timeline lock,
+	 * as their rq->engine pointer is not stable until under that
+	 * engine lock. The simple ploy we use is to take the lock then
+	 * check that the rq still belongs to the newly locked engine.
+	 */
+
+	locked = READ_ONCE(rq->engine);
+	spin_lock_irq(&locked->sched_engine->lock);
+	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
+		spin_unlock(&locked->sched_engine->lock);
+		spin_lock(&engine->sched_engine->lock);
+		locked = engine;
+	}
+	list_del_init(&rq->sched.link);
+	spin_unlock_irq(&locked->sched_engine->lock);
+}
+
+
   static void mock_reset_prepare(struct intel_engine_cs *engine)
   {
   }
@@ -327,6 +356,8 @@ struct intel_engine_cs *mock_engine(struct drm_i915_private *i915,
   	engine->base.emit_flush = mock_emit_flush;
   	engine->base.emit_fini_breadcrumb = mock_emit_breadcrumb;
   	engine->base.submit_request = mock_submit_request;
+	engine->base.add_active_request = mock_add_to_engine;
+	engine->base.remove_active_request = mock_remove_from_engine;
   	engine->base.reset.prepare = mock_reset_prepare;
   	engine->base.reset.rewind = mock_reset_rewind;
drivers/gpu/drm/i915/gt/uc/intel_guc.c +0 -13
index 6661dcb02239..9b09395b998f 100644
@@ -572,19 +572,6 @@ int intel_guc_suspend(struct intel_guc *guc)
   	return 0;
   }
-/**
- * intel_guc_reset_engine() - ask GuC to reset an engine
- * @guc:	intel_guc structure
- * @engine:	engine to be reset
- */
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine)
-{
-	/* XXX: to be implemented with submission interface rework */
-
-	return -ENODEV;
-}
-
   /**
    * intel_guc_resume() - notify GuC resuming from suspend state
    * @guc:	the guc
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 22eb1e9cca41..40c9868762d7 100644
@@ -242,14 +242,16 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
   int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout);
-int intel_guc_reset_engine(struct intel_guc *guc,
-			   struct intel_engine_cs *engine);
-
   int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
   					  const u32 *msg, u32 len);
   int intel_guc_sched_done_process_msg(struct intel_guc *guc,
   				     const u32 *msg, u32 len);
+void intel_guc_submission_reset_prepare(struct intel_guc *guc);
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled);
+void intel_guc_submission_reset_finish(struct intel_guc *guc);
+void intel_guc_submission_cancel_requests(struct intel_guc *guc);
+
   void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
   #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +472 -109
index 83058df5ba01..b8c894ad8caf 100644
@@ -141,7 +141,7 @@ context_wait_for_deregister_to_register(struct intel_context *ce)
   static inline void
   set_context_wait_for_deregister_to_register(struct intel_context *ce)
   {
-	/* Only should be called from guc_lrc_desc_pin() */
+	/* Only should be called from guc_lrc_desc_pin() without lock */
   	ce->guc_state.sched_state |=
   		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
   }
@@ -241,15 +241,31 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
   static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
   {
+	guc->lrc_desc_pool_vaddr = NULL;
   	i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
   }
+static inline bool guc_submission_initialized(struct intel_guc *guc)
+{
+	return guc->lrc_desc_pool_vaddr != NULL;
+}
+
   static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
   {
-	struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+	if (likely(guc_submission_initialized(guc))) {
+		struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+		unsigned long flags;
-	memset(desc, 0, sizeof(*desc));
-	xa_erase_irq(&guc->context_lookup, id);
+		memset(desc, 0, sizeof(*desc));
+
+		/*
+		 * xarray API doesn't have xa_erase_irqsave wrapper, so calling
+		 * the lower level functions directly.
+		 */
+		xa_lock_irqsave(&guc->context_lookup, flags);
+		__xa_erase(&guc->context_lookup, id);
+		xa_unlock_irqrestore(&guc->context_lookup, flags);
+	}
   }
   static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
@@ -260,7 +276,15 @@ static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
   static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
   					   struct intel_context *ce)
   {
-	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	unsigned long flags;
+
+	/*
+	 * xarray API doesn't have xa_save_irqsave wrapper, so calling the
+	 * lower level functions directly.
+	 */
+	xa_lock_irqsave(&guc->context_lookup, flags);
+	__xa_store(&guc->context_lookup, id, ce, GFP_ATOMIC);
+	xa_unlock_irqrestore(&guc->context_lookup, flags);
   }
   static int guc_submission_busy_loop(struct intel_guc* guc,
@@ -331,6 +355,8 @@ int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
   					interruptible, timeout);
   }
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop);
+
   static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
   {
   	int err;
@@ -338,11 +364,22 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
   	u32 action[3];
   	int len = 0;
   	u32 g2h_len_dw = 0;
-	bool enabled = context_enabled(ce);
+	bool enabled;
   	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
   	GEM_BUG_ON(context_guc_id_invalid(ce));
+	/*
+	 * Corner case where the GuC firmware was blown away and reloaded while
+	 * this context was pinned.
+	 */
+	if (unlikely(!lrc_desc_registered(guc, ce->guc_id))) {
+		err = guc_lrc_desc_pin(ce, false);
+		if (unlikely(err))
+			goto out;
+	}
+	enabled = context_enabled(ce);
+
   	if (!enabled) {
   		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
   		action[len++] = ce->guc_id;
@@ -365,6 +402,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
   		intel_context_put(ce);
   	}
+out:
   	return err;
   }
@@ -419,15 +457,10 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
   	if (submit) {
   		guc_set_lrc_tail(last);
   resubmit:
-		/*
-		 * We only check for -EBUSY here even though it is possible for
-		 * -EDEADLK to be returned. If -EDEADLK is returned, the GuC has
-		 * died and a full GPU needs to be done. The hangcheck will
-		 * eventually detect that the GuC has died and trigger this
-		 * reset so no need to handle -EDEADLK here.
-		 */
   		ret = guc_add_request(guc, last);
-		if (ret == -EBUSY) {
+		if (unlikely(ret == -EIO))
+			goto deadlk;
+		else if (ret == -EBUSY) {
   			tasklet_schedule(&sched_engine->tasklet);
   			guc->stalled_request = last;
   			return false;
@@ -437,6 +470,11 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
   	guc->stalled_request = NULL;
   	return submit;
+
+deadlk:
+	sched_engine->tasklet.callback = NULL;
+	tasklet_disable_nosync(&sched_engine->tasklet);
+	return false;
   }
   static void guc_submission_tasklet(struct tasklet_struct *t)
@@ -463,27 +501,165 @@ static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
   		intel_engine_signal_breadcrumbs(engine);
   }
-static void guc_reset_prepare(struct intel_engine_cs *engine)
+static void __guc_context_destroy(struct intel_context *ce);
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce);
+static void guc_signal_context_fence(struct intel_context *ce);
+
+static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	unsigned long index, flags;
+	bool pending_disable, pending_enable, deregister, destroyed;
+
+	xa_for_each(&guc->context_lookup, index, ce) {
+		/* Flush context */
+		spin_lock_irqsave(&ce->guc_state.lock, flags);
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+
+		/*
+		 * Once we are at this point submission_disabled() is guaranteed
+		 * to visible to all callers who set the below flags (see above
+		 * flush and flushes in reset_prepare). If submission_disabled()
+		 * is set, the caller shouldn't set these flags.
+		 */
+
+		destroyed = context_destroyed(ce);
+		pending_enable = context_pending_enable(ce);
+		pending_disable = context_pending_disable(ce);
+		deregister = context_wait_for_deregister_to_register(ce);
+		init_sched_state(ce);
+
+		if (pending_enable || destroyed || deregister) {
+			atomic_dec(&guc->outstanding_submission_g2h);
+			if (deregister)
+				guc_signal_context_fence(ce);
+			if (destroyed) {
+				release_guc_id(guc, ce);
+				__guc_context_destroy(ce);
+			}
+			if (pending_enable|| deregister)
+				intel_context_put(ce);
+		}
+
+		/* Not mutualy exclusive with above if statement. */
+		if (pending_disable) {
+			guc_signal_context_fence(ce);
+			intel_context_sched_disable_unpin(ce);
+			atomic_dec(&guc->outstanding_submission_g2h);
+			intel_context_put(ce);
+		}
+	}
+}
+
+static inline bool
+submission_disabled(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	return unlikely(!__tasklet_is_enabled(&sched_engine->tasklet));
+}
+
+static void disable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+
+	if (__tasklet_is_enabled(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+		__tasklet_disable_sync_once(&sched_engine->tasklet);
+		sched_engine->tasklet.callback = NULL;
+	}
+}
+
+static void enable_submission(struct intel_guc *guc)
+{
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->sched_engine->lock, flags);
+	sched_engine->tasklet.callback = guc_submission_tasklet;
+	wmb();
+	if (!__tasklet_is_enabled(&sched_engine->tasklet) &&
+	    __tasklet_enable(&sched_engine->tasklet)) {
+		GEM_BUG_ON(!guc->ct.enabled);
+
+		/* And kick in case we missed a new request submission. */
+		tasklet_hi_schedule(&sched_engine->tasklet);
+	}
+	spin_unlock_irqrestore(&guc->sched_engine->lock, flags);
+}
+
+static void guc_flush_submissions(struct intel_guc *guc)
   {
-	ENGINE_TRACE(engine, "\n");
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+void intel_guc_submission_reset_prepare(struct intel_guc *guc)
+{
+	int i;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;
+
+	disable_submission(guc);
+	guc->interrupts.disable(guc);
+
+	/* Flush IRQ handler */
+	spin_lock_irq(&guc_to_gt(guc)->irq_lock);
+	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
+
+	guc_flush_submissions(guc);
   	/*
-	 * Prevent request submission to the hardware until we have
-	 * completed the reset in i915_gem_reset_finish(). If a request
-	 * is completed by one engine, it may then queue a request
-	 * to a second via its execlists->tasklet *just* as we are
-	 * calling engine->init_hw() and also writing the ELSP.
-	 * Turning off the execlists->tasklet until the reset is over
-	 * prevents the race.
-	 */
-	__tasklet_disable_sync_once(&engine->sched_engine->tasklet);
+	 * Handle any outstanding G2Hs before reset. Call IRQ handler directly
+	 * each pass as interrupt have been disabled. We always scrub for
+	 * outstanding G2H as it is possible for outstanding_submission_g2h to
+	 * be incremented after the context state update.
+ 	 */
+	for (i = 0; i < 4 && atomic_read(&guc->outstanding_submission_g2h); ++i) {
+		intel_guc_to_host_event_handler(guc);
+#define wait_for_reset(guc, wait_var) \
+		guc_wait_for_pending_msg(guc, wait_var, false, (HZ / 20))
+		do {
+			wait_for_reset(guc, &guc->outstanding_submission_g2h);
+		} while (!list_empty(&guc->ct.requests.incoming));
+	}
+	scrub_guc_desc_for_outstanding_g2h(guc);
   }
-static void guc_reset_state(struct intel_context *ce,
-			    struct intel_engine_cs *engine,
-			    u32 head,
-			    bool scrub)
+static struct intel_engine_cs *
+guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
   {
+	struct intel_engine_cs *engine;
+	intel_engine_mask_t tmp, mask = ve->mask;
+	unsigned int num_siblings = 0;
+
+	for_each_engine_masked(engine, ve->gt, mask, tmp)
+		if (num_siblings++ == sibling)
+			return engine;
+
+	return NULL;
+}
+
+static inline struct intel_engine_cs *
+__context_to_physical_engine(struct intel_context *ce)
+{
+	struct intel_engine_cs *engine = ce->engine;
+
+	if (intel_engine_is_virtual(engine))
+		engine = guc_virtual_get_sibling(engine, 0);
+
+	return engine;
+}
+
+static void guc_reset_state(struct intel_context *ce, u32 head, bool scrub)
+{
+	struct intel_engine_cs *engine = __context_to_physical_engine(ce);
+
   	GEM_BUG_ON(!intel_context_is_pinned(ce));
   	/*
@@ -501,42 +677,147 @@ static void guc_reset_state(struct intel_context *ce,
   	lrc_update_regs(ce, engine, head);
   }
-static void guc_reset_rewind(struct intel_engine_cs *engine, bool stalled)
+static void guc_reset_nop(struct intel_engine_cs *engine)
   {
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_request *rq;
+}
+
+static void guc_rewind_nop(struct intel_engine_cs *engine, bool stalled)
+{
+}
+
+static void
+__unwind_incomplete_requests(struct intel_context *ce)
+{
+	struct i915_request *rq, *rn;
+	struct list_head *pl;
+	int prio = I915_PRIORITY_INVALID;
+	struct i915_sched_engine * const sched_engine =
+		ce->engine->sched_engine;
   	unsigned long flags;
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry_safe(rq, rn,
+				 &ce->guc_active.requests,
+				 sched.link) {
+		if (i915_request_completed(rq))
+			continue;
+
+		list_del_init(&rq->sched.link);
+		spin_unlock(&ce->guc_active.lock);
+
+		__i915_request_unsubmit(rq);
+
+		/* Push the request back into the queue for later resubmission. */
+		GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
+		if (rq_prio(rq) != prio) {
+			prio = rq_prio(rq);
+			pl = i915_sched_lookup_priolist(sched_engine, prio);
+		}
+		GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
+
+		list_add_tail(&rq->sched.link, pl);
+		set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-	/* Push back any incomplete requests for replay after the reset. */
-	rq = execlists_unwind_incomplete_requests(execlists);
-	if (!rq)
-		goto out_unlock;
+		spin_lock(&ce->guc_active.lock);
+	}
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static struct i915_request *context_find_active_request(struct intel_context *ce)
+{
+	struct i915_request *rq, *active = NULL;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ce->guc_active.lock, flags);
+	list_for_each_entry_reverse(rq, &ce->guc_active.requests,
+				    sched.link) {
+		if (i915_request_completed(rq))
+			break;
+
+		active = rq;
+	}
+	spin_unlock_irqrestore(&ce->guc_active.lock, flags);
+
+	return active;
+}
+
+static void __guc_reset_context(struct intel_context *ce, bool stalled)
+{
+	struct i915_request *rq;
+	u32 head;
+
+	/*
+	 * GuC will implicitly mark the context as non-schedulable
+	 * when it sends the reset notification. Make sure our state
+	 * reflects this change. The context will be marked enabled
+	 * on resubmission.
+	 */
+	clr_context_enabled(ce);
+
+	rq = context_find_active_request(ce);
+	if (!rq) {
+		head = ce->ring->tail;
+		stalled = false;
+		goto out_replay;
+	}
   	if (!i915_request_started(rq))
   		stalled = false;
+	GEM_BUG_ON(i915_active_is_idle(&ce->active));
+	head = intel_ring_wrap(ce->ring, rq->head);
   	__i915_request_reset(rq, stalled);
-	guc_reset_state(rq->context, engine, rq->head, stalled);
-out_unlock:
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+out_replay:
+	guc_reset_state(ce, head, stalled);
+	__unwind_incomplete_requests(ce);
   }
-static void guc_reset_cancel(struct intel_engine_cs *engine)
+void intel_guc_submission_reset(struct intel_guc *guc, bool stalled)
+{
+	struct intel_context *ce;
+	unsigned long index;
+
+	if (unlikely(!guc_submission_initialized(guc)))
+		/* Reset called during driver load? GuC not yet initialised! */
+		return;
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			__guc_reset_context(ce, stalled);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);
+}
+
+static void guc_cancel_context_requests(struct intel_context *ce)
+{
+	struct i915_sched_engine *sched_engine = ce_to_guc(ce)->sched_engine;
+	struct i915_request *rq;
+	unsigned long flags;
+
+	/* Mark all executing requests as skipped. */
+	spin_lock_irqsave(&sched_engine->lock, flags);
+	spin_lock(&ce->guc_active.lock);
+	list_for_each_entry(rq, &ce->guc_active.requests, sched.link)
+		i915_request_put(i915_request_mark_eio(rq));
+	spin_unlock(&ce->guc_active.lock);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+static void
+guc_cancel_sched_engine_requests(struct i915_sched_engine *sched_engine)
   {
-	struct i915_sched_engine * const sched_engine = engine->sched_engine;
   	struct i915_request *rq, *rn;
   	struct rb_node *rb;
   	unsigned long flags;
   	/* Can be called during boot if GuC fails to load */
-	if (!engine->gt)
+	if (!sched_engine)
   		return;
-	ENGINE_TRACE(engine, "\n");
-
   	/*
   	 * Before we call engine->cancel_requests(), we should have exclusive
   	 * access to the submission state. This is arranged for us by the
@@ -553,21 +834,16 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
   	 */
   	spin_lock_irqsave(&sched_engine->lock, flags);
-	/* Mark all executing requests as skipped. */
-	list_for_each_entry(rq, &sched_engine->requests, sched.link) {
-		i915_request_set_error_once(rq, -EIO);
-		i915_request_mark_complete(rq);
-	}
-
   	/* Flush the queued requests to the timeline list (for retiring). */
   	while ((rb = rb_first_cached(&sched_engine->queue))) {
   		struct i915_priolist *p = to_priolist(rb);
   		priolist_for_each_request_consume(rq, rn, p) {
   			list_del_init(&rq->sched.link);
+
   			__i915_request_submit(rq);
-			dma_fence_set_error(&rq->fence, -EIO);
-			i915_request_mark_complete(rq);
+
+			i915_request_put(i915_request_mark_eio(rq));
   		}
   		rb_erase_cached(&p->node, &sched_engine->queue);
@@ -582,14 +858,38 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
   	spin_unlock_irqrestore(&sched_engine->lock, flags);
   }
-static void guc_reset_finish(struct intel_engine_cs *engine)
+void intel_guc_submission_cancel_requests(struct intel_guc *guc)
   {
-	if (__tasklet_enable(&engine->sched_engine->tasklet))
-		/* And kick in case we missed a new request submission. */
-		tasklet_hi_schedule(&engine->sched_engine->tasklet);
+	struct intel_context *ce;
+	unsigned long index;
+
+	xa_for_each(&guc->context_lookup, index, ce)
+		if (intel_context_is_pinned(ce))
+			guc_cancel_context_requests(ce);
-	ENGINE_TRACE(engine, "depth->%d\n",
-		     atomic_read(&engine->sched_engine->tasklet.count));
+	guc_cancel_sched_engine_requests(guc->sched_engine);
+
+	/* GuC is blown away, drop all references to contexts */
+	xa_destroy(&guc->context_lookup);
+}
+
+void intel_guc_submission_reset_finish(struct intel_guc *guc)
+{
+	/* Reset called during driver load or during wedge? */
+	if (unlikely(!guc_submission_initialized(guc) ||
+		     test_bit(I915_WEDGED, &guc_to_gt(guc)->reset.flags)))
+		return;
+
+	/*
+	 * Technically possible for either of these values to be non-zero here,
+	 * but very unlikely + harmless. Regardless let's add a warn so we can
+	 * see in CI if this happens frequently / a precursor to taking down the
+	 * machine.
+	 */
+	GEM_WARN_ON(atomic_read(&guc->outstanding_submission_g2h));
+	atomic_set(&guc->outstanding_submission_g2h, 0);
+
+	enable_submission(guc);
   }
   /*
@@ -656,6 +956,9 @@ static int guc_bypass_tasklet_submit(struct intel_guc *guc,
   	else
   		trace_i915_request_guc_submit(rq);
+	if (unlikely(ret == -EIO))
+		disable_submission(guc);
+
   	return ret;
   }
@@ -668,7 +971,8 @@ static void guc_submit_request(struct i915_request *rq)
   	/* Will be called from irq-context when using foreign fences. */
   	spin_lock_irqsave(&sched_engine->lock, flags);
-	if (guc->stalled_request || !i915_sched_engine_is_empty(sched_engine))
+	if (submission_disabled(guc) || guc->stalled_request ||
+	    !i915_sched_engine_is_empty(sched_engine))
   		queue_request(sched_engine, rq, rq_prio(rq));
   	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
   		tasklet_hi_schedule(&sched_engine->tasklet);
@@ -805,7 +1109,8 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
   static int __guc_action_register_context(struct intel_guc *guc,
   					 u32 guc_id,
-					 u32 offset)
+					 u32 offset,
+					 bool loop)
   {
   	u32 action[] = {
   		INTEL_GUC_ACTION_REGISTER_CONTEXT,
@@ -813,10 +1118,10 @@ static int __guc_action_register_context(struct intel_guc *guc,
   		offset,
   	};
-	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
+	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action), 0, loop);
   }
-static int register_context(struct intel_context *ce)
+static int register_context(struct intel_context *ce, bool loop)
   {
   	struct intel_guc *guc = ce_to_guc(ce);
   	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
@@ -824,11 +1129,12 @@ static int register_context(struct intel_context *ce)
   	trace_intel_context_register(ce);
-	return __guc_action_register_context(guc, ce->guc_id, offset);
+	return __guc_action_register_context(guc, ce->guc_id, offset, loop);
   }
   static int __guc_action_deregister_context(struct intel_guc *guc,
-					   u32 guc_id)
+					   u32 guc_id,
+					   bool loop)
   {
   	u32 action[] = {
   		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
@@ -836,16 +1142,16 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
   	};
   	return guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
-					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
+					G2H_LEN_DW_DEREGISTER_CONTEXT, loop);
   }
-static int deregister_context(struct intel_context *ce, u32 guc_id)
+static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
   {
   	struct intel_guc *guc = ce_to_guc(ce);
   	trace_intel_context_deregister(ce);
-	return __guc_action_deregister_context(guc, guc_id);
+	return __guc_action_deregister_context(guc, guc_id, loop);
   }
   static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
@@ -874,7 +1180,7 @@ static void guc_context_policy_init(struct intel_engine_cs *engine,
   	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
   }
-static int guc_lrc_desc_pin(struct intel_context *ce)
+static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
   {
   	struct intel_runtime_pm *runtime_pm =
   		&ce->engine->gt->i915->runtime_pm;
@@ -920,18 +1226,44 @@ static int guc_lrc_desc_pin(struct intel_context *ce)
   	 */
   	if (context_registered) {
   		trace_intel_context_steal_guc_id(ce);
-		set_context_wait_for_deregister_to_register(ce);
-		intel_context_get(ce);
+		if (!loop) {
+			set_context_wait_for_deregister_to_register(ce);
+			intel_context_get(ce);
+		} else {
+			bool disabled;
+			unsigned long flags;
+
+			/* Seal race with Reset */
+			spin_lock_irqsave(&ce->guc_state.lock, flags);
+			disabled = submission_disabled(guc);
+			if (likely(!disabled)) {
+				set_context_wait_for_deregister_to_register(ce);
+				intel_context_get(ce);
+			}
+			spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+			if (unlikely(disabled)) {
+				reset_lrc_desc(guc, desc_idx);
+				return 0;	/* Will get registered later */
+			}
+		}
   		/*
   		 * If stealing the guc_id, this ce has the same guc_id as the
   		 * context whos guc_id was stole.
   		 */
   		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = deregister_context(ce, ce->guc_id);
+			ret = deregister_context(ce, ce->guc_id, loop);
+		if (unlikely(ret == -EBUSY)) {
+			clr_context_wait_for_deregister_to_register(ce);
+			intel_context_put(ce);
+		}
   	} else {
   		with_intel_runtime_pm(runtime_pm, wakeref)
-			ret = register_context(ce);
+			ret = register_context(ce, loop);
+		if (unlikely(ret == -EBUSY))
+			reset_lrc_desc(guc, desc_idx);
+		else if (unlikely(ret == -ENODEV))
+			ret = 0;	/* Will get registered later */
   	}
   	return ret;
@@ -994,7 +1326,6 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
   	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
   	trace_intel_context_sched_disable(ce);
-	intel_context_get(ce);
   	guc_submission_busy_loop(guc, action, ARRAY_SIZE(action),
   				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
@@ -1004,6 +1335,7 @@ static u16 prep_context_pending_disable(struct intel_context *ce)
   {
   	set_context_pending_disable(ce);
   	clr_context_enabled(ce);
+	intel_context_get(ce);
   	return ce->guc_id;
   }
@@ -1016,7 +1348,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
   	u16 guc_id;
   	intel_wakeref_t wakeref;
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
   	    !lrc_desc_registered(guc, ce->guc_id)) {
   		clr_context_enabled(ce);
   		goto unpin;
@@ -1034,6 +1366,7 @@ static void guc_context_sched_disable(struct intel_context *ce)
   	 * request doesn't slip through the 'context_pending_disable' fence.
   	 */
   	if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
+		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
   		return;
   	}
   	guc_id = prep_context_pending_disable(ce);
@@ -1050,19 +1383,13 @@ static void guc_context_sched_disable(struct intel_context *ce)
   static inline void guc_lrc_desc_unpin(struct intel_context *ce)
   {
-	struct intel_engine_cs *engine = ce->engine;
-	struct intel_guc *guc = &engine->gt->uc.guc;
-	unsigned long flags;
+	struct intel_guc *guc = ce_to_guc(ce);
   	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
   	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
   	GEM_BUG_ON(context_enabled(ce));
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	set_context_destroyed(ce);
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-
-	deregister_context(ce, ce->guc_id);
+	deregister_context(ce, ce->guc_id, true);
   }
   static void __guc_context_destroy(struct intel_context *ce)
@@ -1090,13 +1417,15 @@ static void guc_context_destroy(struct kref *kref)
   	struct intel_guc *guc = &ce->engine->gt->uc.guc;
   	intel_wakeref_t wakeref;
   	unsigned long flags;
+	bool disabled;
   	/*
   	 * If the guc_id is invalid this context has been stolen and we can free
   	 * it immediately. Also can be freed immediately if the context is not
   	 * registered with the GuC.
   	 */
-	if (context_guc_id_invalid(ce) ||
+	if (submission_disabled(guc) ||
+	    context_guc_id_invalid(ce) ||
   	    !lrc_desc_registered(guc, ce->guc_id)) {
   		release_guc_id(guc, ce);
   		__guc_context_destroy(ce);
@@ -1123,6 +1452,18 @@ static void guc_context_destroy(struct kref *kref)
   		list_del_init(&ce->guc_id_link);
   	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled))
+		set_context_destroyed(ce);
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
   	/*
   	 * We defer GuC context deregistration until the context is destroyed
   	 * in order to save on CTBs. With this optimization ideally we only need
@@ -1145,6 +1486,33 @@ static int guc_context_alloc(struct intel_context *ce)
   	return lrc_alloc(ce, ce->engine);
   }
+static void add_to_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock(&ce->guc_active.lock);
+	list_move_tail(&rq->sched.link, &ce->guc_active.requests);
+	spin_unlock(&ce->guc_active.lock);
+}
+
+static void remove_from_context(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+
+	spin_lock_irq(&ce->guc_active.lock);
+
+	list_del_init(&rq->sched.link);
+	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+
+	/* Prevent further __await_execution() registering a cb, then flush */
+	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
+
+	spin_unlock_irq(&ce->guc_active.lock);
+
+	atomic_dec(&ce->guc_id_ref);
+	i915_request_notify_execute_cb_imm(rq);
+}
+
   static const struct intel_context_ops guc_context_ops = {
   	.alloc = guc_context_alloc,
@@ -1183,8 +1551,6 @@ static void guc_signal_context_fence(struct intel_context *ce)
   {
   	unsigned long flags;
-	GEM_BUG_ON(!context_wait_for_deregister_to_register(ce));
-
   	spin_lock_irqsave(&ce->guc_state.lock, flags);
   	clr_context_wait_for_deregister_to_register(ce);
   	__guc_signal_context_fence(ce);
@@ -1193,8 +1559,9 @@ static void guc_signal_context_fence(struct intel_context *ce)
   static bool context_needs_register(struct intel_context *ce, bool new_guc_id)
   {
-	return new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
-		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id);
+	return (new_guc_id || test_bit(CONTEXT_LRCA_DIRTY, &ce->flags) ||
+		!lrc_desc_registered(ce_to_guc(ce), ce->guc_id)) &&
+		!submission_disabled(ce_to_guc(ce));
   }
   static int guc_request_alloc(struct i915_request *rq)
@@ -1252,8 +1619,12 @@ static int guc_request_alloc(struct i915_request *rq)
   	if (unlikely(ret < 0))
   		return ret;;
   	if (context_needs_register(ce, !!ret)) {
-		ret = guc_lrc_desc_pin(ce);
+		ret = guc_lrc_desc_pin(ce, true);
   		if (unlikely(ret)) {	/* unwind */
+			if (ret == -EIO) {
+				disable_submission(guc);
+				goto out;	/* GPU will be reset */
+			}
   			atomic_dec(&ce->guc_id_ref);
   			unpin_guc_id(guc, ce);
   			return ret;
@@ -1290,20 +1661,6 @@ static int guc_request_alloc(struct i915_request *rq)
   	return 0;
   }
-static struct intel_engine_cs *
-guc_virtual_get_sibling(struct intel_engine_cs *ve, unsigned int sibling)
-{
-	struct intel_engine_cs *engine;
-	intel_engine_mask_t tmp, mask = ve->mask;
-	unsigned int num_siblings = 0;
-
-	for_each_engine_masked(engine, ve->gt, mask, tmp)
-		if (num_siblings++ == sibling)
-			return engine;
-
-	return NULL;
-}
-
   static int guc_virtual_context_pre_pin(struct intel_context *ce,
   				       struct i915_gem_ww_ctx *ww,
   				       void **vaddr)
@@ -1512,7 +1869,7 @@ static inline void guc_kernel_context_pin(struct intel_guc *guc,
   {
   	if (context_guc_id_invalid(ce))
   		pin_guc_id(guc, ce);
-	guc_lrc_desc_pin(ce);
+	guc_lrc_desc_pin(ce, true);
   }
   static inline void guc_init_lrc_mapping(struct intel_guc *guc)
@@ -1578,13 +1935,15 @@ static void guc_default_vfuncs(struct intel_engine_cs *engine)
   	engine->cops = &guc_context_ops;
   	engine->request_alloc = guc_request_alloc;
   	engine->bump_serial = guc_bump_serial;
+	engine->add_active_request = add_to_context;
+	engine->remove_active_request = remove_from_context;
   	engine->sched_engine->schedule = i915_schedule;
-	engine->reset.prepare = guc_reset_prepare;
-	engine->reset.rewind = guc_reset_rewind;
-	engine->reset.cancel = guc_reset_cancel;
-	engine->reset.finish = guc_reset_finish;
+	engine->reset.prepare = guc_reset_nop;
+	engine->reset.rewind = guc_rewind_nop;
+	engine->reset.cancel = guc_reset_nop;
+	engine->reset.finish = guc_reset_nop;
   	engine->emit_flush = gen8_emit_flush_xcs;
   	engine->emit_init_breadcrumb = gen8_emit_init_breadcrumb;
@@ -1757,7 +2116,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
   		 * register this context.
   		 */
   		with_intel_runtime_pm(runtime_pm, wakeref)
-			register_context(ce);
+			register_context(ce, true);
   		guc_signal_context_fence(ce);
   		intel_context_put(ce);
   	} else if (context_destroyed(ce)) {
@@ -1939,6 +2298,10 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
   				 "v%dx%d", ve->base.class, count);
   			ve->base.context_size = sibling->context_size;
+			ve->base.add_active_request =
+				sibling->add_active_request;
+			ve->base.remove_active_request =
+				sibling->remove_active_request;
   			ve->base.emit_bb_start = sibling->emit_bb_start;
   			ve->base.emit_flush = sibling->emit_flush;
   			ve->base.emit_init_breadcrumb =
drivers/gpu/drm/i915/gt/uc/intel_uc.c +38 -1
index 6d8b9233214e..f0b02200aa01 100644
@@ -565,12 +565,49 @@ void intel_uc_reset_prepare(struct intel_uc *uc)
   {
   	struct intel_guc *guc = &uc->guc;
-	if (!intel_guc_is_ready(guc))
+
+	/* Nothing to do if GuC isn't supported */
+	if (!intel_uc_supports_guc(uc))
   		return;
+	/* Firmware expected to be running when this function is called */
+	if (!intel_guc_is_ready(guc))
+		goto sanitize;
+
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_prepare(guc);
+
+sanitize:
   	__uc_sanitize(uc);
   }
+void intel_uc_reset(struct intel_uc *uc, bool stalled)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset(guc, stalled);
+}
+
+void intel_uc_reset_finish(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware expected to be running when this function is called */
+	if (intel_guc_is_fw_running(guc) && intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_reset_finish(guc);
+}
+
+void intel_uc_cancel_requests(struct intel_uc *uc)
+{
+	struct intel_guc *guc = &uc->guc;
+
+	/* Firmware can not be running when this function is called  */
+	if (intel_uc_uses_guc_submission(uc))
+		intel_guc_submission_cancel_requests(guc);
+}
+
   void intel_uc_runtime_suspend(struct intel_uc *uc)
   {
   	struct intel_guc *guc = &uc->guc;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +3 -0
index c4cef885e984..eaa3202192ac 100644
@@ -37,6 +37,9 @@ void intel_uc_driver_late_release(struct intel_uc *uc);
   void intel_uc_driver_remove(struct intel_uc *uc);
   void intel_uc_init_mmio(struct intel_uc *uc);
   void intel_uc_reset_prepare(struct intel_uc *uc);
+void intel_uc_reset(struct intel_uc *uc, bool stalled);
+void intel_uc_reset_finish(struct intel_uc *uc);
+void intel_uc_cancel_requests(struct intel_uc *uc);
   void intel_uc_suspend(struct intel_uc *uc);
   void intel_uc_runtime_suspend(struct intel_uc *uc);
   int intel_uc_resume(struct intel_uc *uc);
drivers/gpu/drm/i915/i915_request.c +4 -37
index dec5a35c9aa2..192784875a1d 100644
@@ -194,7 +194,7 @@ static bool irq_work_imm(struct irq_work *wrk)
   	return false;
   }
-static void __notify_execute_cb_imm(struct i915_request *rq)
+void i915_request_notify_execute_cb_imm(struct i915_request *rq)
   {
   	__notify_execute_cb(rq, irq_work_imm);
   }
@@ -268,37 +268,6 @@ i915_request_active_engine(struct i915_request *rq,
   	return ret;
   }
-
-static void remove_from_engine(struct i915_request *rq)
-{
-	struct intel_engine_cs *engine, *locked;
-
-	/*
-	 * Virtual engines complicate acquiring the engine timeline lock,
-	 * as their rq->engine pointer is not stable until under that
-	 * engine lock. The simple ploy we use is to take the lock then
-	 * check that the rq still belongs to the newly locked engine.
-	 */
-	locked = READ_ONCE(rq->engine);
-	spin_lock_irq(&locked->sched_engine->lock);
-	while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
-		spin_unlock(&locked->sched_engine->lock);
-		spin_lock(&engine->sched_engine->lock);
-		locked = engine;
-	}
-	list_del_init(&rq->sched.link);
-
-	clear_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-	clear_bit(I915_FENCE_FLAG_HOLD, &rq->fence.flags);
-
-	/* Prevent further __await_execution() registering a cb, then flush */
-	set_bit(I915_FENCE_FLAG_ACTIVE, &rq->fence.flags);
-
-	spin_unlock_irq(&locked->sched_engine->lock);
-
-	__notify_execute_cb_imm(rq);
-}
-
   static void __rq_init_watchdog(struct i915_request *rq)
   {
   	rq->watchdog.timer.function = NULL;
@@ -395,9 +364,7 @@ bool i915_request_retire(struct i915_request *rq)
   	 * after removing the breadcrumb and signaling it, so that we do not
   	 * inadvertently attach the breadcrumb to a completed request.
   	 */
-	if (!list_empty(&rq->sched.link))
-		remove_from_engine(rq);
-	atomic_dec(&rq->context->guc_id_ref);
+	rq->engine->remove_active_request(rq);
   	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
   	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
@@ -539,7 +506,7 @@ __await_execution(struct i915_request *rq,
   	if (llist_add(&cb->work.node.llist, &signal->execute_cb)) {
   		if (i915_request_is_active(signal) ||
   		    __request_in_flight(signal))
-			__notify_execute_cb_imm(signal);
+			i915_request_notify_execute_cb_imm(signal);
   	}
   	return 0;
@@ -676,7 +643,7 @@ bool __i915_request_submit(struct i915_request *request)
   	result = true;
   	GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
-	list_move_tail(&request->sched.link, &engine->sched_engine->requests);
+	engine->add_active_request(request);
   active:
   	clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags);
   	set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
drivers/gpu/drm/i915/i915_request.h +2 -0
index f870cd75a001..bcc6340c505e 100644
@@ -649,4 +649,6 @@ bool
   i915_request_active_engine(struct i915_request *rq,
   			   struct intel_engine_cs **active);
+void i915_request_notify_execute_cb_imm(struct i915_request *rq);
+
   #endif /* I915_REQUEST_H */