Skip to contentSkip to conversation

GuC submission support

PATCH In review Matthew Brost 6 participants 111 messages Jul 16, 2021 – Jul 26, 2021 Page 1 of 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover GuC submission support

As discussed in [1], [2] we are enabling GuC submission support in the i915. This is a subset of the patches in step 5 described in [1], basically it is absolute to enable CI with GuC submission on gen11+ platforms.

This series itself will likely be broken down into smaller patch sets to merge. Likely into CTBs changes, basic submission, virtual engines, and resets.

A following series will address the missing patches remaining from [1].

Locally tested on TGL machine and basic tests seem to be passing.

v2: Address all review comments in [3], include several more patches to make CI [4] happy.

[1] https://patchwork.freedesktop.org/series/89844/
[2] https://patchwork.freedesktop.org/series/91417/
[3] https://patchwork.freedesktop.org/series/91840/
[4] https://patchwork.freedesktop.org/series/91885/

Daniele Ceraolo Spurio (1):
drm/i915/guc: Unblock GuC submission on Gen11+

John Harrison (12):
drm/i915: Track 'serial' counts for virtual engines drm/i915/guc: Provide mmio list to be saved/restored on engine reset drm/i915/guc: Don't complain about reset races drm/i915/guc: Enable GuC engine reset
drm/i915/guc: Fix for error capture after full GPU reset with GuC drm/i915/guc: Hook GuC scheduling policies up drm/i915/guc: Connect reset modparam updates to GuC policy flags drm/i915/guc: Include scheduling policies in the debugfs state dump drm/i915/guc: Add golden context to GuC ADS drm/i915/selftest: Better error reporting from hangcheck selftest drm/i915/selftest: Fix hangcheck self test for GuC submission drm/i915/selftest: Bump selftest timeouts for hangcheck

Matthew Brost (36):
drm/i915/guc: Add new GuC interface defines and structures drm/i915/guc: Remove GuC stage descriptor, add LRC descriptor drm/i915/guc: Add LRC descriptor context lookup array drm/i915/guc: Implement GuC submission tasklet drm/i915/guc: Add bypass tasklet submission path to GuC drm/i915/guc: Implement GuC context operations for new inteface drm/i915/guc: Insert fence on context when deregistering drm/i915/guc: Defer context unpin until scheduling is disabled drm/i915/guc: Disable engine barriers with GuC during unpin drm/i915/guc: Extend deregistration fence to schedule disable drm/i915: Disable preempt busywait when using GuC scheduling drm/i915/guc: Ensure request ordering via completion fences drm/i915/guc: Disable semaphores when using GuC scheduling drm/i915/guc: Ensure G2H response has space in buffer drm/i915/guc: Update intel_gt_wait_for_idle to work with GuC drm/i915/guc: Update GuC debugfs to support new GuC drm/i915/guc: Add several request trace points
drm/i915: Add intel_context tracing
drm/i915/guc: GuC virtual engines
drm/i915: Hold reference to intel_context over life of i915_request drm/i915/guc: Disable bonding extension with GuC submission drm/i915/guc: Direct all breadcrumbs for a class to single breadcrumbs drm/i915: Add i915_sched_engine destroy vfunc
drm/i915: Move active request tracking to a vfunc drm/i915/guc: Reset implementation for new GuC interface drm/i915: Reset GPU immediately if submission is disabled drm/i915/guc: Add disable interrupts to guc sanitize drm/i915/guc: Suspend/resume implementation for new interface drm/i915/guc: Handle context reset notification drm/i915/guc: Handle engine reset failure notification drm/i915/guc: Enable the timer expired interrupt for GuC drm/i915/guc: Capture error state on context reset drm/i915/guc: Implement banned contexts for GuC submission drm/i915/guc: Support request cancellation drm/i915/selftest: Increase some timeouts in live_requests drm/i915/guc: Implement GuC priority management

Rahul Kumar Singh (2):
drm/i915/selftest: Fix workarounds selftest for GuC submission drm/i915/selftest: Fix MOCS selftest for GuC submission

 drivers/gpu/drm/i915/Makefile                 |    1 +
 drivers/gpu/drm/i915/gem/i915_gem_context.c   |   21 +-
 drivers/gpu/drm/i915/gem/i915_gem_context.h   |    1 +
 drivers/gpu/drm/i915/gem/i915_gem_mman.c      |    3 +-
 drivers/gpu/drm/i915/gt/gen8_engine_cs.c      |    6 +-
 drivers/gpu/drm/i915/gt/intel_breadcrumbs.c   |   44 +-
 drivers/gpu/drm/i915/gt/intel_breadcrumbs.h   |   16 +-
 .../gpu/drm/i915/gt/intel_breadcrumbs_types.h |    7 +
 drivers/gpu/drm/i915/gt/intel_context.c       |   50 +-
 drivers/gpu/drm/i915/gt/intel_context.h       |   50 +-
 drivers/gpu/drm/i915/gt/intel_context_types.h |   63 +-
 drivers/gpu/drm/i915/gt/intel_engine.h        |   54 +-
 drivers/gpu/drm/i915/gt/intel_engine_cs.c     |  182 +-
 .../gpu/drm/i915/gt/intel_engine_heartbeat.c  |   71 +-
 .../gpu/drm/i915/gt/intel_engine_heartbeat.h  |    4 +
 drivers/gpu/drm/i915/gt/intel_engine_types.h  |   13 +-
 drivers/gpu/drm/i915/gt/intel_engine_user.c   |    4 +
 .../drm/i915/gt/intel_execlists_submission.c  |   95 +-
 .../drm/i915/gt/intel_execlists_submission.h  |    4 -
 drivers/gpu/drm/i915/gt/intel_gt.c            |   21 +
 drivers/gpu/drm/i915/gt/intel_gt.h            |    2 +
 drivers/gpu/drm/i915/gt/intel_gt_pm.c         |    6 +-
 drivers/gpu/drm/i915/gt/intel_gt_requests.c   |   21 +-
 drivers/gpu/drm/i915/gt/intel_gt_requests.h   |    7 +-
 drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |    1 -
 drivers/gpu/drm/i915/gt/intel_reset.c         |   50 +-
 .../gpu/drm/i915/gt/intel_ring_submission.c   |   48 +
 drivers/gpu/drm/i915/gt/intel_rps.c           |    4 +
 drivers/gpu/drm/i915/gt/intel_workarounds.c   |   46 +-
 .../gpu/drm/i915/gt/intel_workarounds_types.h |    1 +
 drivers/gpu/drm/i915/gt/mock_engine.c         |   40 +-
 drivers/gpu/drm/i915/gt/selftest_context.c    |   10 +
 .../drm/i915/gt/selftest_engine_heartbeat.c   |   22 +
 .../drm/i915/gt/selftest_engine_heartbeat.h   |    2 +
 drivers/gpu/drm/i915/gt/selftest_execlists.c  |   12 +-
 drivers/gpu/drm/i915/gt/selftest_hangcheck.c  |  314 +-
 drivers/gpu/drm/i915/gt/selftest_mocs.c       |   50 +-
 .../gpu/drm/i915/gt/selftest_workarounds.c    |  132 +-
 .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  |   15 +
 drivers/gpu/drm/i915/gt/uc/intel_guc.c        |   82 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  101 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |  461 ++-
 drivers/gpu/drm/i915/gt/uc/intel_guc_ads.h    |    4 +
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |  132 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |   16 +-
 .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    |   25 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |   88 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 2776 +++++++++++++++--
 .../gpu/drm/i915/gt/uc/intel_guc_submission.h |   18 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc.c         |  101 +-
 drivers/gpu/drm/i915/gt/uc/intel_uc.h         |   11 +
 drivers/gpu/drm/i915/i915_debugfs_params.c    |   31 +
 drivers/gpu/drm/i915/i915_gem_evict.c         |    1 +
 drivers/gpu/drm/i915/i915_gpu_error.c         |   25 +-
 drivers/gpu/drm/i915/i915_reg.h               |    2 +
 drivers/gpu/drm/i915/i915_request.c           |  174 +-
 drivers/gpu/drm/i915/i915_request.h           |   29 +
 drivers/gpu/drm/i915/i915_scheduler.c         |   16 +-
 drivers/gpu/drm/i915/i915_scheduler.h         |   10 +-
 drivers/gpu/drm/i915/i915_scheduler_types.h   |   22 +
 drivers/gpu/drm/i915/i915_trace.h             |  219 +-
 drivers/gpu/drm/i915/selftests/i915_request.c |    4 +-
 .../gpu/drm/i915/selftests/igt_flush_test.c   |    2 +-
 .../gpu/drm/i915/selftests/igt_live_test.c    |    2 +-
 .../i915/selftests/intel_scheduler_helpers.c  |   89 +
 .../i915/selftests/intel_scheduler_helpers.h  |   35 +
 .../gpu/drm/i915/selftests/mock_gem_device.c  |    3 +-
 include/uapi/drm/i915_drm.h                   |    9 +
 68 files changed, 5119 insertions(+), 862 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/selftests/intel_scheduler_helpers.c
 create mode 100644 drivers/gpu/drm/i915/selftests/intel_scheduler_helpers.h
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/51 drm/i915/guc: Add new GuC interface defines and structures

Add new GuC interface defines and structures while maintaining old ones in parallel.

---
 .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  | 14 +++++++
 drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   | 41 +++++++++++++++++++
 2 files changed, 55 insertions(+)
drivers/gpu/drm/i915/gt/uc/abi/guc_actions_abi.h +14 -0
index 2d6198e63ebe..57e18babdf4b 100644
@@ -124,10 +124,24 @@ enum intel_guc_action {
 	INTEL_GUC_ACTION_FORCE_LOG_BUFFER_FLUSH = 0x302,
 	INTEL_GUC_ACTION_ENTER_S_STATE = 0x501,
 	INTEL_GUC_ACTION_EXIT_S_STATE = 0x502,
+	INTEL_GUC_ACTION_GLOBAL_SCHED_POLICY_CHANGE = 0x506,
+	INTEL_GUC_ACTION_SCHED_CONTEXT = 0x1000,
+	INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET = 0x1001,
+	INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_DONE = 0x1002,
+	INTEL_GUC_ACTION_SCHED_ENGINE_MODE_SET = 0x1003,
+	INTEL_GUC_ACTION_SCHED_ENGINE_MODE_DONE = 0x1004,
+	INTEL_GUC_ACTION_SET_CONTEXT_PRIORITY = 0x1005,
+	INTEL_GUC_ACTION_SET_CONTEXT_EXECUTION_QUANTUM = 0x1006,
+	INTEL_GUC_ACTION_SET_CONTEXT_PREEMPTION_TIMEOUT = 0x1007,
+	INTEL_GUC_ACTION_CONTEXT_RESET_NOTIFICATION = 0x1008,
+	INTEL_GUC_ACTION_ENGINE_FAILURE_NOTIFICATION = 0x1009,
 	INTEL_GUC_ACTION_SLPC_REQUEST = 0x3003,
 	INTEL_GUC_ACTION_AUTHENTICATE_HUC = 0x4000,
+	INTEL_GUC_ACTION_REGISTER_CONTEXT = 0x4502,
+	INTEL_GUC_ACTION_DEREGISTER_CONTEXT = 0x4503,
 	INTEL_GUC_ACTION_REGISTER_COMMAND_TRANSPORT_BUFFER = 0x4505,
 	INTEL_GUC_ACTION_DEREGISTER_COMMAND_TRANSPORT_BUFFER = 0x4506,
+	INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE = 0x4600,
 	INTEL_GUC_ACTION_LIMIT
 };
 
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +41 -0

59 lines not on this page · raw message

Signature · 6 lines
2.28.0

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

PATCH 2/51 drm/i915/guc: Remove GuC stage descriptor, add LRC descriptor

In reply to Matthew Brost

Remove old GuC stage descriptor, add LRC descriptor which will be used by the new GuC interface implemented in this patch series.

v2:
(John Harrison)
- s/lrc/LRC/g

---
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  4 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   | 65 -----------------
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 72 ++++++-------------
 3 files changed, 25 insertions(+), 116 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +2 -2
index 72e4653222e2..2625d2d5959f 100644
@@ -43,8 +43,8 @@ struct intel_guc {
 	struct i915_vma *ads_vma;
 	struct __guc_ads_blob *ads_blob;
 
-	struct i915_vma *stage_desc_pool;
-	void *stage_desc_pool_vaddr;
+	struct i915_vma *lrc_desc_pool;
+	void *lrc_desc_pool_vaddr;
 
 	/* Control params for fw initialization */
 	u32 params[GUC_CTL_MAX_DWORDS];
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -65

77 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +23 -49

121 lines not on this page · raw message

Signature · 6 lines
2.28.0

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

PATCH 3/51 drm/i915/guc: Add LRC descriptor context lookup array

In reply to Matthew Brost

Add LRC descriptor context lookup array which can resolve the intel_context from the LRC descriptor index. In addition to lookup, it can determine if the LRC descriptor context is currently registered with the GuC by checking if an entry for a descriptor index is present. Future patches in the series will make use of this array.

v2:
(Michal)
- "linux/xarray.h" -> <linux/xarray.h>
- s/lrc/LRC
(John H)
- Fix commit message

---
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  5 +++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 32 +++++++++++++++++--
 2 files changed, 35 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -0
index 2625d2d5959f..35783558d261 100644
@@ -6,6 +6,8 @@
 #ifndef _INTEL_GUC_H_
 #define _INTEL_GUC_H_
 
+#include <linux/xarray.h>
+
 #include "intel_uncore.h"
 #include "intel_guc_fw.h"
 #include "intel_guc_fwif.h"
@@ -46,6 +48,9 @@ struct intel_guc {
 	struct i915_vma *lrc_desc_pool;
 	void *lrc_desc_pool_vaddr;
 
+	/* guc_id to intel_context lookup */
+	struct xarray context_lookup;
+
 	/* Control params for fw initialization */
 	u32 params[GUC_CTL_MAX_DWORDS];
 
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +30 -2
index a366890fb840..23a94a896a0b 100644
@@ -65,8 +65,6 @@ static inline struct i915_priolist *to_priolist(struct rb_node *rb)
 	return rb_entry(rb, struct i915_priolist, node);
 }
 
-/* Future patches will use this function */
-__attribute__ ((unused))
 static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
 {
 	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
@@ -76,6 +74,15 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
 	return &base[index];
 }
 
+static inline struct intel_context *__get_context(struct intel_guc *guc, u32 id)
+{
+	struct intel_context *ce = xa_load(&guc->context_lookup, id);
+
+	GEM_BUG_ON(id >= GUC_MAX_LRC_DESCRIPTORS);
+
+	return ce;
+}
+
 static int guc_lrc_desc_pool_create(struct intel_guc *guc)
 {
 	u32 size;
@@ -96,6 +103,25 @@ static void guc_lrc_desc_pool_destroy(struct intel_guc *guc)
 	i915_vma_unpin_and_release(&guc->lrc_desc_pool, I915_VMA_RELEASE_MAP);
 }
 
+static inline void reset_lrc_desc(struct intel_guc *guc, u32 id)
+{
+	struct guc_lrc_desc *desc = __get_lrc_desc(guc, id);
+
+	memset(desc, 0, sizeof(*desc));
+	xa_erase_irq(&guc->context_lookup, id);
+}
+
+static inline bool lrc_desc_registered(struct intel_guc *guc, u32 id)
+{
+	return __get_context(guc, 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);
+}
+
 static void guc_add_request(struct intel_guc *guc, struct i915_request *rq)
 {
 	/* Leaving stub as this function will be used in future patches */
@@ -400,6 +426,8 @@ int intel_guc_submission_init(struct intel_guc *guc)
 	 */
 	GEM_BUG_ON(!guc->lrc_desc_pool);
 
+	xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
+
 	return 0;
 }
 
Signature · 1 line
2.28.0
#4 lore raw

PATCH 4/51 drm/i915/guc: Implement GuC submission tasklet

In reply to Matthew Brost

Implement GuC submission tasklet for new interface. The new GuC interface uses H2G to submit contexts to the GuC. Since H2G use a single channel, a single tasklet submits is used for the submission path.

Also the per engine interrupt handler has been updated to disable the rescheduling of the physical engine tasklet, when using GuC scheduling, as the physical engine tasklet is no longer used.

In this patch the field, guc_id, has been added to intel_context and is not assigned. Patches later in the series will assign this value.

v2:
(John Harrison)
- Clean up some comments

---
 drivers/gpu/drm/i915/gt/intel_context_types.h |   9 +
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   4 +
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 231 +++++++++---------
 3 files changed, 127 insertions(+), 117 deletions(-)
drivers/gpu/drm/i915/gt/intel_context_types.h +9 -0
index 90026c177105..6d99631d19b9 100644
@@ -137,6 +137,15 @@ struct intel_context {
 	struct intel_sseu sseu;
 
 	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
+
+	/* GuC scheduling state flags that do not require a lock. */
+	atomic_t guc_sched_state_no_lock;
+
+	/*
+	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
+	 * in the series will.
+	 */
+	u16 guc_id;
 };
 
 #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 35783558d261..8c7b92f699f1 100644
@@ -30,6 +30,10 @@ struct intel_guc {
 	struct intel_guc_log log;
 	struct intel_guc_ct ct;
 
+	/* Global engine used to submit requests to GuC */
+	struct i915_sched_engine *sched_engine;
+	struct i915_request *stalled_request;
+
 	/* intel_guc_recv interrupt related state */
 	spinlock_t irq_lock;
 	unsigned int msg_enabled_mask;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +114 -117

349 lines not on this page · raw message

Signature · 6 lines
2.28.0

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

Re: [PATCH 04/51] drm/i915/guc: Implement GuC submission tasklet

Quoted 3 lines · Matthew Brost

Implement GuC submission tasklet for new interface. The new GuC interface uses H2G to submit contexts to the GuC. Since H2G use a single channel, a single tasklet submits is used for the submission path.

This still needs fixing - 'a single tasklet submits is used' is not valid English.

It also seems that the idea of splitting all the deletes of old code into a separate patch didn't happen. It really does obfuscate things significantly having completely unrelated deletes and adds interspersed :(.

John.

Quoted 407 lines

Also the per engine interrupt handler has been updated to disable the rescheduling of the physical engine tasklet, when using GuC scheduling, as the physical engine tasklet is no longer used.

In this patch the field, guc_id, has been added to intel_context and is not assigned. Patches later in the series will assign this value.

v2:
(John Harrison)
- Clean up some comments

---
  drivers/gpu/drm/i915/gt/intel_context_types.h |   9 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   4 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 231 +++++++++---------
  3 files changed, 127 insertions(+), 117 deletions(-)
drivers/gpu/drm/i915/gt/intel_context_types.h +9 -0
index 90026c177105..6d99631d19b9 100644
@@ -137,6 +137,15 @@ struct intel_context {
  	struct intel_sseu sseu;
 
  	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
+
+	/* GuC scheduling state flags that do not require a lock. */
+	atomic_t guc_sched_state_no_lock;
+
+	/*
+	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
+	 * in the series will.
+	 */
+	u16 guc_id;
  };
 
  #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 35783558d261..8c7b92f699f1 100644
@@ -30,6 +30,10 @@ struct intel_guc {
  	struct intel_guc_log log;
  	struct intel_guc_ct ct;
 
+	/* Global engine used to submit requests to GuC */
+	struct i915_sched_engine *sched_engine;
+	struct i915_request *stalled_request;
+
  	/* intel_guc_recv interrupt related state */
  	spinlock_t irq_lock;
  	unsigned int msg_enabled_mask;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +107 -117
index 23a94a896a0b..ca0717166a27 100644
@@ -60,6 +60,31 @@
 
  #define GUC_REQUEST_SIZE 64 /* bytes */
 
+/*
+ * Below is a set of functions which control the GuC scheduling state which do
+ * not require a lock as all state transitions are mutually exclusive. i.e. It
+ * is not possible for the context pinning code and submission, for the same
+ * context, to be executing simultaneously. We still need an atomic as it is
+ * possible for some of the bits to changing at the same time though.
+ */
+#define SCHED_STATE_NO_LOCK_ENABLED			BIT(0)
+static inline bool context_enabled(struct intel_context *ce)
+{
+	return (atomic_read(&ce->guc_sched_state_no_lock) &
+		SCHED_STATE_NO_LOCK_ENABLED);
+}
+
+static inline void set_context_enabled(struct intel_context *ce)
+{
+	atomic_or(SCHED_STATE_NO_LOCK_ENABLED, &ce->guc_sched_state_no_lock);
+}
+
+static inline void clr_context_enabled(struct intel_context *ce)
+{
+	atomic_and((u32)~SCHED_STATE_NO_LOCK_ENABLED,
+		   &ce->guc_sched_state_no_lock);
+}
+
  static inline struct i915_priolist *to_priolist(struct rb_node *rb)
  {
  	return rb_entry(rb, struct i915_priolist, node);
@@ -122,37 +147,29 @@ static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
  	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
  }
 
-static void guc_add_request(struct intel_guc *guc, struct i915_request *rq)
+static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  {
-	/* Leaving stub as this function will be used in future patches */
-}
+	int err;
+	struct intel_context *ce = rq->context;
+	u32 action[3];
+	int len = 0;
+	bool enabled = context_enabled(ce);
 
-/*
- * When we're doing submissions using regular execlists backend, writing to
- * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
- * pinned in mappable aperture portion of GGTT are visible to command streamer.
- * Writes done by GuC on our behalf are not guaranteeing such ordering,
- * therefore, to ensure the flush, we're issuing a POSTING READ.
- */
-static void flush_ggtt_writes(struct i915_vma *vma)
-{
-	if (i915_vma_is_map_and_fenceable(vma))
-		intel_uncore_posting_read_fw(vma->vm->gt->uncore,
-					     GUC_STATUS);
-}
+	if (!enabled) {
+		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
+		action[len++] = ce->guc_id;
+		action[len++] = GUC_CONTEXT_ENABLE;
+	} else {
+		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT;
+		action[len++] = ce->guc_id;
+	}
 
-static void guc_submit(struct intel_engine_cs *engine,
-		       struct i915_request **out,
-		       struct i915_request **end)
-{
-	struct intel_guc *guc = &engine->gt->uc.guc;
+	err = intel_guc_send_nb(guc, action, len);
 
-	do {
-		struct i915_request *rq = *out++;
+	if (!enabled && !err)
+		set_context_enabled(ce);
 
-		flush_ggtt_writes(rq->ring->vma);
-		guc_add_request(guc, rq);
-	} while (out != end);
+	return err;
  }
 
  static inline int rq_prio(const struct i915_request *rq)
@@ -160,125 +177,88 @@ static inline int rq_prio(const struct i915_request *rq)
  	return rq->sched.attr.priority;
  }
 
-static struct i915_request *schedule_in(struct i915_request *rq, int idx)
+static int guc_dequeue_one_context(struct intel_guc *guc)
  {
-	trace_i915_request_in(rq, idx);
-
-	/*
-	 * Currently we are not tracking the rq->context being inflight
-	 * (ce->inflight = rq->engine). It is only used by the execlists
-	 * backend at the moment, a similar counting strategy would be
-	 * required if we generalise the inflight tracking.
-	 */
-
-	__intel_gt_pm_get(rq->engine->gt);
-	return i915_request_get(rq);
-}
-
-static void schedule_out(struct i915_request *rq)
-{
-	trace_i915_request_out(rq);
-
-	intel_gt_pm_put_async(rq->engine->gt);
-	i915_request_put(rq);
-}
-
-static void __guc_dequeue(struct intel_engine_cs *engine)
-{
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_sched_engine * const sched_engine = engine->sched_engine;
-	struct i915_request **first = execlists->inflight;
-	struct i915_request ** const last_port = first + execlists->port_mask;
-	struct i915_request *last = first[0];
-	struct i915_request **port;
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	struct i915_request *last = NULL;
  	bool submit = false;
  	struct rb_node *rb;
+	int ret;
 
  	lockdep_assert_held(&sched_engine->lock);
 
-	if (last) {
-		if (*++first)
-			return;
-
-		last = NULL;
+	if (guc->stalled_request) {
+		submit = true;
+		last = guc->stalled_request;
+		goto resubmit;
  	}
 
-	/*
-	 * We write directly into the execlists->inflight queue and don't use
-	 * the execlists->pending queue, as we don't have a distinct switch
-	 * event.
-	 */
-	port = first;
  	while ((rb = rb_first_cached(&sched_engine->queue))) {
  		struct i915_priolist *p = to_priolist(rb);
  		struct i915_request *rq, *rn;
 
  		priolist_for_each_request_consume(rq, rn, p) {
-			if (last && rq->context != last->context) {
-				if (port == last_port)
-					goto done;
-
-				*port = schedule_in(last,
-						    port - execlists->inflight);
-				port++;
-			}
+			if (last && rq->context != last->context)
+				goto done;
 
  			list_del_init(&rq->sched.link);
+
  			__i915_request_submit(rq);
-			submit = true;
+
+			trace_i915_request_in(rq, 0);
  			last = rq;
+			submit = true;
  		}
 
  		rb_erase_cached(&p->node, &sched_engine->queue);
  		i915_priolist_free(p);
  	}
  done:
-	sched_engine->queue_priority_hint =
-		rb ? to_priolist(rb)->priority : INT_MIN;
  	if (submit) {
-		*port = schedule_in(last, port - execlists->inflight);
-		*++port = NULL;
-		guc_submit(engine, first, port);
+		last->context->lrc_reg_state[CTX_RING_TAIL] =
+			intel_ring_set_tail(last->ring, last->tail);
+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 GT reset 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) {
+			tasklet_schedule(&sched_engine->tasklet);
+			guc->stalled_request = last;
+			return false;
+		}
  	}
-	execlists->active = execlists->inflight;
+
+	guc->stalled_request = NULL;
+	return submit;
  }
 
  static void guc_submission_tasklet(struct tasklet_struct *t)
  {
  	struct i915_sched_engine *sched_engine =
  		from_tasklet(sched_engine, t, tasklet);
-	struct intel_engine_cs * const engine = sched_engine->private_data;
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_request **port, *rq;
  	unsigned long flags;
+	bool loop;
 
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
-
-	for (port = execlists->inflight; (rq = *port); port++) {
-		if (!i915_request_completed(rq))
-			break;
-
-		schedule_out(rq);
-	}
-	if (port != execlists->inflight) {
-		int idx = port - execlists->inflight;
-		int rem = ARRAY_SIZE(execlists->inflight) - idx;
-		memmove(execlists->inflight, port, rem * sizeof(*port));
-	}
+	spin_lock_irqsave(&sched_engine->lock, flags);
 
-	__guc_dequeue(engine);
+	do {
+		loop = guc_dequeue_one_context(sched_engine->private_data);
+	} while (loop);
 
-	i915_sched_engine_reset_on_empty(engine->sched_engine);
+	i915_sched_engine_reset_on_empty(sched_engine);
 
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
 
  static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
  {
-	if (iir & GT_RENDER_USER_INTERRUPT) {
+	if (iir & GT_RENDER_USER_INTERRUPT)
  		intel_engine_signal_breadcrumbs(engine);
-		tasklet_hi_schedule(&engine->sched_engine->tasklet);
-	}
  }
 
  static void guc_reset_prepare(struct intel_engine_cs *engine)
@@ -349,6 +329,10 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	struct rb_node *rb;
  	unsigned long flags;
 
+	/* Can be called during boot if GuC fails to load */
+	if (!engine->gt)
+		return;
+
  	ENGINE_TRACE(engine, "\n");
 
  	/*
@@ -433,8 +417,11 @@ int intel_guc_submission_init(struct intel_guc *guc)
 
  void intel_guc_submission_fini(struct intel_guc *guc)
  {
-	if (guc->lrc_desc_pool)
-		guc_lrc_desc_pool_destroy(guc);
+	if (!guc->lrc_desc_pool)
+		return;
+
+	guc_lrc_desc_pool_destroy(guc);
+	i915_sched_engine_put(guc->sched_engine);
  }
 
  static int guc_context_alloc(struct intel_context *ce)
@@ -499,32 +486,32 @@ static int guc_request_alloc(struct i915_request *request)
  	return 0;
  }
 
-static inline void queue_request(struct intel_engine_cs *engine,
+static inline void queue_request(struct i915_sched_engine *sched_engine,
  				 struct i915_request *rq,
  				 int prio)
  {
  	GEM_BUG_ON(!list_empty(&rq->sched.link));
  	list_add_tail(&rq->sched.link,
-		      i915_sched_lookup_priolist(engine->sched_engine, prio));
+		      i915_sched_lookup_priolist(sched_engine, prio));
  	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
  }
 
  static void guc_submit_request(struct i915_request *rq)
  {
-	struct intel_engine_cs *engine = rq->engine;
+	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
  	unsigned long flags;
 
  	/* Will be called from irq-context when using foreign fences. */
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
+	spin_lock_irqsave(&sched_engine->lock, flags);
 
-	queue_request(engine, rq, rq_prio(rq));
+	queue_request(sched_engine, rq, rq_prio(rq));
 
-	GEM_BUG_ON(i915_sched_engine_is_empty(engine->sched_engine));
+	GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
  	GEM_BUG_ON(list_empty(&rq->sched.link));
 
-	tasklet_hi_schedule(&engine->sched_engine->tasklet);
+	tasklet_hi_schedule(&sched_engine->tasklet);
 
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
 
  static void sanitize_hwsp(struct intel_engine_cs *engine)
@@ -602,8 +589,6 @@ static void guc_release(struct intel_engine_cs *engine)
  {
  	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
 
-	tasklet_kill(&engine->sched_engine->tasklet);
-
  	intel_engine_cleanup_common(engine);
  	lrc_fini_wa_ctx(engine);
  }
@@ -674,6 +659,7 @@ static inline void guc_default_irqs(struct intel_engine_cs *engine)
  int intel_guc_submission_setup(struct intel_engine_cs *engine)
  {
  	struct drm_i915_private *i915 = engine->i915;
+	struct intel_guc *guc = &engine->gt->uc.guc;
 
  	/*
  	 * The setup relies on several assumptions (e.g. irqs always enabled)
@@ -681,7 +667,18 @@ int intel_guc_submission_setup(struct intel_engine_cs *engine)
  	 */
  	GEM_BUG_ON(GRAPHICS_VER(i915) < 11);
 
-	tasklet_setup(&engine->sched_engine->tasklet, guc_submission_tasklet);
+	if (!guc->sched_engine) {
+		guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL);
+		if (!guc->sched_engine)
+			return -ENOMEM;
+

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c int intel_guc_submission_setup(struct intel_engine_cs *engine) · line 684

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+		guc->sched_engine->schedule = i915_schedule;
+		guc->sched_engine->private_data = guc;
+		tasklet_setup(&guc->sched_engine->tasklet,
+			      guc_submission_tasklet);
+	}
+	i915_sched_engine_put(engine->sched_engine);
+	engine->sched_engine = i915_sched_engine_get(guc->sched_engine);
 
  	guc_default_vfuncs(engine);
  	guc_default_irqs(engine);

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

#6 lore raw

Re: [PATCH 04/51] drm/i915/guc: Implement GuC submission tasklet

Quoted 7 lines · John Harrison
Quoted 3 lines · Matthew Brost

Implement GuC submission tasklet for new interface. The new GuC interface uses H2G to submit contexts to the GuC. Since H2G use a single channel, a single tasklet submits is used for the submission path.

This still needs fixing - 'a single tasklet submits is used' is not valid English.

Will fix.

Quoted 4 lines

It also seems that the idea of splitting all the deletes of old code into a separate patch didn't happen. It really does obfuscate things significantly having completely unrelated deletes and adds interspersed :(.

I don't recall promising to do that.

Matt

Quoted 380 lines

John.

Quoted 376 lines

Also the per engine interrupt handler has been updated to disable the rescheduling of the physical engine tasklet, when using GuC scheduling, as the physical engine tasklet is no longer used.

In this patch the field, guc_id, has been added to intel_context and is not assigned. Patches later in the series will assign this value.

v2:
(John Harrison)
- Clean up some comments

---
  drivers/gpu/drm/i915/gt/intel_context_types.h |   9 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   4 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 231 +++++++++---------
  3 files changed, 127 insertions(+), 117 deletions(-)
drivers/gpu/drm/i915/gt/intel_context_types.h +9 -0
index 90026c177105..6d99631d19b9 100644
@@ -137,6 +137,15 @@ struct intel_context {
  	struct intel_sseu sseu;
  	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
+
+	/* GuC scheduling state flags that do not require a lock. */
+	atomic_t guc_sched_state_no_lock;
+
+	/*
+	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
+	 * in the series will.
+	 */
+	u16 guc_id;
  };
  #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 35783558d261..8c7b92f699f1 100644
@@ -30,6 +30,10 @@ struct intel_guc {
  	struct intel_guc_log log;
  	struct intel_guc_ct ct;
+	/* Global engine used to submit requests to GuC */
+	struct i915_sched_engine *sched_engine;
+	struct i915_request *stalled_request;
+
  	/* intel_guc_recv interrupt related state */
  	spinlock_t irq_lock;
  	unsigned int msg_enabled_mask;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +114 -117
index 23a94a896a0b..ca0717166a27 100644
@@ -60,6 +60,31 @@
  #define GUC_REQUEST_SIZE 64 /* bytes */
+/*
+ * Below is a set of functions which control the GuC scheduling state which do
+ * not require a lock as all state transitions are mutually exclusive. i.e. It
+ * is not possible for the context pinning code and submission, for the same
+ * context, to be executing simultaneously. We still need an atomic as it is
+ * possible for some of the bits to changing at the same time though.
+ */
+#define SCHED_STATE_NO_LOCK_ENABLED			BIT(0)
+static inline bool context_enabled(struct intel_context *ce)
+{
+	return (atomic_read(&ce->guc_sched_state_no_lock) &
+		SCHED_STATE_NO_LOCK_ENABLED);
+}
+
+static inline void set_context_enabled(struct intel_context *ce)
+{
+	atomic_or(SCHED_STATE_NO_LOCK_ENABLED, &ce->guc_sched_state_no_lock);
+}
+
+static inline void clr_context_enabled(struct intel_context *ce)
+{
+	atomic_and((u32)~SCHED_STATE_NO_LOCK_ENABLED,
+		   &ce->guc_sched_state_no_lock);
+}
+
  static inline struct i915_priolist *to_priolist(struct rb_node *rb)
  {
  	return rb_entry(rb, struct i915_priolist, node);
@@ -122,37 +147,29 @@ static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
  	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
  }
-static void guc_add_request(struct intel_guc *guc, struct i915_request *rq)
+static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  {
-	/* Leaving stub as this function will be used in future patches */
-}
+	int err;
+	struct intel_context *ce = rq->context;
+	u32 action[3];
+	int len = 0;
+	bool enabled = context_enabled(ce);
-/*
- * When we're doing submissions using regular execlists backend, writing to
- * ELSP from CPU side is enough to make sure that writes to ringbuffer pages
- * pinned in mappable aperture portion of GGTT are visible to command streamer.
- * Writes done by GuC on our behalf are not guaranteeing such ordering,
- * therefore, to ensure the flush, we're issuing a POSTING READ.
- */
-static void flush_ggtt_writes(struct i915_vma *vma)
-{
-	if (i915_vma_is_map_and_fenceable(vma))
-		intel_uncore_posting_read_fw(vma->vm->gt->uncore,
-					     GUC_STATUS);
-}
+	if (!enabled) {
+		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
+		action[len++] = ce->guc_id;
+		action[len++] = GUC_CONTEXT_ENABLE;
+	} else {
+		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT;
+		action[len++] = ce->guc_id;
+	}
-static void guc_submit(struct intel_engine_cs *engine,
-		       struct i915_request **out,
-		       struct i915_request **end)
-{
-	struct intel_guc *guc = &engine->gt->uc.guc;
+	err = intel_guc_send_nb(guc, action, len);
-	do {
-		struct i915_request *rq = *out++;
+	if (!enabled && !err)
+		set_context_enabled(ce);
-		flush_ggtt_writes(rq->ring->vma);
-		guc_add_request(guc, rq);
-	} while (out != end);
+	return err;
  }
  static inline int rq_prio(const struct i915_request *rq)
@@ -160,125 +177,88 @@ static inline int rq_prio(const struct i915_request *rq)
  	return rq->sched.attr.priority;
  }
-static struct i915_request *schedule_in(struct i915_request *rq, int idx)
+static int guc_dequeue_one_context(struct intel_guc *guc)
  {
-	trace_i915_request_in(rq, idx);
-
-	/*
-	 * Currently we are not tracking the rq->context being inflight
-	 * (ce->inflight = rq->engine). It is only used by the execlists
-	 * backend at the moment, a similar counting strategy would be
-	 * required if we generalise the inflight tracking.
-	 */
-
-	__intel_gt_pm_get(rq->engine->gt);
-	return i915_request_get(rq);
-}
-
-static void schedule_out(struct i915_request *rq)
-{
-	trace_i915_request_out(rq);
-
-	intel_gt_pm_put_async(rq->engine->gt);
-	i915_request_put(rq);
-}
-
-static void __guc_dequeue(struct intel_engine_cs *engine)
-{
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_sched_engine * const sched_engine = engine->sched_engine;
-	struct i915_request **first = execlists->inflight;
-	struct i915_request ** const last_port = first + execlists->port_mask;
-	struct i915_request *last = first[0];
-	struct i915_request **port;
+	struct i915_sched_engine * const sched_engine = guc->sched_engine;
+	struct i915_request *last = NULL;
  	bool submit = false;
  	struct rb_node *rb;
+	int ret;
  	lockdep_assert_held(&sched_engine->lock);
-	if (last) {
-		if (*++first)
-			return;
-
-		last = NULL;
+	if (guc->stalled_request) {
+		submit = true;
+		last = guc->stalled_request;
+		goto resubmit;
  	}
-	/*
-	 * We write directly into the execlists->inflight queue and don't use
-	 * the execlists->pending queue, as we don't have a distinct switch
-	 * event.
-	 */
-	port = first;
  	while ((rb = rb_first_cached(&sched_engine->queue))) {
  		struct i915_priolist *p = to_priolist(rb);
  		struct i915_request *rq, *rn;
  		priolist_for_each_request_consume(rq, rn, p) {
-			if (last && rq->context != last->context) {
-				if (port == last_port)
-					goto done;
-
-				*port = schedule_in(last,
-						    port - execlists->inflight);
-				port++;
-			}
+			if (last && rq->context != last->context)
+				goto done;
  			list_del_init(&rq->sched.link);
+
  			__i915_request_submit(rq);
-			submit = true;
+
+			trace_i915_request_in(rq, 0);
  			last = rq;
+			submit = true;
  		}
  		rb_erase_cached(&p->node, &sched_engine->queue);
  		i915_priolist_free(p);
  	}
  done:
-	sched_engine->queue_priority_hint =
-		rb ? to_priolist(rb)->priority : INT_MIN;
  	if (submit) {
-		*port = schedule_in(last, port - execlists->inflight);
-		*++port = NULL;
-		guc_submit(engine, first, port);
+		last->context->lrc_reg_state[CTX_RING_TAIL] =
+			intel_ring_set_tail(last->ring, last->tail);
+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 GT reset 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) {
+			tasklet_schedule(&sched_engine->tasklet);
+			guc->stalled_request = last;
+			return false;
+		}
  	}
-	execlists->active = execlists->inflight;
+
+	guc->stalled_request = NULL;
+	return submit;
  }
  static void guc_submission_tasklet(struct tasklet_struct *t)
  {
  	struct i915_sched_engine *sched_engine =
  		from_tasklet(sched_engine, t, tasklet);
-	struct intel_engine_cs * const engine = sched_engine->private_data;
-	struct intel_engine_execlists * const execlists = &engine->execlists;
-	struct i915_request **port, *rq;
  	unsigned long flags;
+	bool loop;
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
-
-	for (port = execlists->inflight; (rq = *port); port++) {
-		if (!i915_request_completed(rq))
-			break;
-
-		schedule_out(rq);
-	}
-	if (port != execlists->inflight) {
-		int idx = port - execlists->inflight;
-		int rem = ARRAY_SIZE(execlists->inflight) - idx;
-		memmove(execlists->inflight, port, rem * sizeof(*port));
-	}
+	spin_lock_irqsave(&sched_engine->lock, flags);
-	__guc_dequeue(engine);
+	do {
+		loop = guc_dequeue_one_context(sched_engine->private_data);
+	} while (loop);
-	i915_sched_engine_reset_on_empty(engine->sched_engine);
+	i915_sched_engine_reset_on_empty(sched_engine);
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
  static void cs_irq_handler(struct intel_engine_cs *engine, u16 iir)
  {
-	if (iir & GT_RENDER_USER_INTERRUPT) {
+	if (iir & GT_RENDER_USER_INTERRUPT)
  		intel_engine_signal_breadcrumbs(engine);
-		tasklet_hi_schedule(&engine->sched_engine->tasklet);
-	}
  }
  static void guc_reset_prepare(struct intel_engine_cs *engine)
@@ -349,6 +329,10 @@ static void guc_reset_cancel(struct intel_engine_cs *engine)
  	struct rb_node *rb;
  	unsigned long flags;
+	/* Can be called during boot if GuC fails to load */
+	if (!engine->gt)
+		return;
+
  	ENGINE_TRACE(engine, "\n");
  	/*
@@ -433,8 +417,11 @@ int intel_guc_submission_init(struct intel_guc *guc)
  void intel_guc_submission_fini(struct intel_guc *guc)
  {
-	if (guc->lrc_desc_pool)
-		guc_lrc_desc_pool_destroy(guc);
+	if (!guc->lrc_desc_pool)
+		return;
+
+	guc_lrc_desc_pool_destroy(guc);
+	i915_sched_engine_put(guc->sched_engine);
  }
  static int guc_context_alloc(struct intel_context *ce)
@@ -499,32 +486,32 @@ static int guc_request_alloc(struct i915_request *request)
  	return 0;
  }
-static inline void queue_request(struct intel_engine_cs *engine,
+static inline void queue_request(struct i915_sched_engine *sched_engine,
  				 struct i915_request *rq,
  				 int prio)
  {
  	GEM_BUG_ON(!list_empty(&rq->sched.link));
  	list_add_tail(&rq->sched.link,
-		      i915_sched_lookup_priolist(engine->sched_engine, prio));
+		      i915_sched_lookup_priolist(sched_engine, prio));
  	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
  }
  static void guc_submit_request(struct i915_request *rq)
  {
-	struct intel_engine_cs *engine = rq->engine;
+	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
  	unsigned long flags;
  	/* Will be called from irq-context when using foreign fences. */
-	spin_lock_irqsave(&engine->sched_engine->lock, flags);
+	spin_lock_irqsave(&sched_engine->lock, flags);
-	queue_request(engine, rq, rq_prio(rq));
+	queue_request(sched_engine, rq, rq_prio(rq));
-	GEM_BUG_ON(i915_sched_engine_is_empty(engine->sched_engine));
+	GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
  	GEM_BUG_ON(list_empty(&rq->sched.link));
-	tasklet_hi_schedule(&engine->sched_engine->tasklet);
+	tasklet_hi_schedule(&sched_engine->tasklet);
-	spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
  static void sanitize_hwsp(struct intel_engine_cs *engine)
@@ -602,8 +589,6 @@ static void guc_release(struct intel_engine_cs *engine)
  {
  	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
-	tasklet_kill(&engine->sched_engine->tasklet);
-
  	intel_engine_cleanup_common(engine);
  	lrc_fini_wa_ctx(engine);
  }
@@ -674,6 +659,7 @@ static inline void guc_default_irqs(struct intel_engine_cs *engine)
  int intel_guc_submission_setup(struct intel_engine_cs *engine)
  {
  	struct drm_i915_private *i915 = engine->i915;
+	struct intel_guc *guc = &engine->gt->uc.guc;
  	/*
  	 * The setup relies on several assumptions (e.g. irqs always enabled)
@@ -681,7 +667,18 @@ int intel_guc_submission_setup(struct intel_engine_cs *engine)
  	 */
  	GEM_BUG_ON(GRAPHICS_VER(i915) < 11);
-	tasklet_setup(&engine->sched_engine->tasklet, guc_submission_tasklet);
+	if (!guc->sched_engine) {
+		guc->sched_engine = i915_sched_engine_create(ENGINE_VIRTUAL);
+		if (!guc->sched_engine)
+			return -ENOMEM;
+
+		guc->sched_engine->schedule = i915_schedule;
+		guc->sched_engine->private_data = guc;
+		tasklet_setup(&guc->sched_engine->tasklet,
+			      guc_submission_tasklet);
+	}
+	i915_sched_engine_put(engine->sched_engine);
+	engine->sched_engine = i915_sched_engine_get(guc->sched_engine);
  	guc_default_vfuncs(engine);
  	guc_default_irqs(engine);
#7 lore raw

Re: [PATCH 04/51] drm/i915/guc: Implement GuC submission tasklet

Quoted 10 lines · Matthew Brost
Quoted 7 lines · John Harrison
Quoted 3 lines · Matthew Brost

Implement GuC submission tasklet for new interface. The new GuC interface uses H2G to submit contexts to the GuC. Since H2G use a single channel, a single tasklet submits is used for the submission path.

This still needs fixing - 'a single tasklet submits is used' is not valid English.

Will fix.

Quoted 7 lines
Quoted 4 lines

It also seems that the idea of splitting all the deletes of old code into a separate patch didn't happen. It really does obfuscate things significantly having completely unrelated deletes and adds interspersed :(.

I don't recall promising to do that.

Matt

"No promises but perhaps I'll do this in the next rev."

Well, this is the next rev. So I am expressing my disappointment that it didn't happen. Reviewability of patches is important.

John.

#8 lore raw

PATCH 5/51 drm/i915/guc: Add bypass tasklet submission path to GuC

In reply to Matthew Brost

Add bypass tasklet submission path to GuC. The tasklet is only used if H2G channel has backpresure.

---
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 37 +++++++++++++++----
 1 file changed, 29 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +29 -8
index ca0717166a27..53b4a5eb4a85 100644
@@ -172,6 +172,12 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
 	return err;
 }
 
+static inline void guc_set_lrc_tail(struct i915_request *rq)
+{
+	rq->context->lrc_reg_state[CTX_RING_TAIL] =
+		intel_ring_set_tail(rq->ring, rq->tail);
+}
+
 static inline int rq_prio(const struct i915_request *rq)
 {
 	return rq->sched.attr.priority;
@@ -215,8 +221,7 @@ static int guc_dequeue_one_context(struct intel_guc *guc)
 	}
 done:
 	if (submit) {
-		last->context->lrc_reg_state[CTX_RING_TAIL] =
-			intel_ring_set_tail(last->ring, last->tail);
+		guc_set_lrc_tail(last);
 resubmit:
 		/*
 		 * We only check for -EBUSY here even though it is possible for
@@ -496,20 +501,36 @@ static inline void queue_request(struct i915_sched_engine *sched_engine,
 	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
 }
 
+static int guc_bypass_tasklet_submit(struct intel_guc *guc,
+				     struct i915_request *rq)
+{
+	int ret;
+
+	__i915_request_submit(rq);
+
+	trace_i915_request_in(rq, 0);
+
+	guc_set_lrc_tail(rq);
+	ret = guc_add_request(guc, rq);
+	if (ret == -EBUSY)
+		guc->stalled_request = rq;
+
+	return ret;
+}
+
 static void guc_submit_request(struct i915_request *rq)
 {
 	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
+	struct intel_guc *guc = &rq->engine->gt->uc.guc;
 	unsigned long flags;
 
 	/* Will be called from irq-context when using foreign fences. */
 	spin_lock_irqsave(&sched_engine->lock, flags);
 
-	queue_request(sched_engine, rq, rq_prio(rq));
-
-	GEM_BUG_ON(i915_sched_engine_is_empty(sched_engine));
-	GEM_BUG_ON(list_empty(&rq->sched.link));
-
-	tasklet_hi_schedule(&sched_engine->tasklet);
+	if (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);
 
 	spin_unlock_irqrestore(&sched_engine->lock, flags);
 }
Signature · 1 line
2.28.0
#9 lore raw

PATCH 6/51 drm/i915/guc: Implement GuC context operations for new inteface

In reply to Matthew Brost

Implement GuC context operations which includes GuC specific operations alloc, pin, unpin, and destroy.

v2:
(Daniel Vetter)
- Use msleep_interruptible rather than cond_resched in busy loop
(Michal)
- Remove C++ style comment

---
 drivers/gpu/drm/i915/gt/intel_context.c       |   5 +
 drivers/gpu/drm/i915/gt/intel_context_types.h |  22 +-
 drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |   1 -
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  40 ++
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   4 +
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 666 ++++++++++++++++--
 drivers/gpu/drm/i915/i915_reg.h               |   1 +
 drivers/gpu/drm/i915/i915_request.c           |   1 +
 8 files changed, 685 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +5 -0
index bd63813c8a80..32fd6647154b 100644
@@ -384,6 +384,11 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 
 	mutex_init(&ce->pin_mutex);
 
+	spin_lock_init(&ce->guc_state.lock);
+
+	ce->guc_id = GUC_INVALID_LRC_ID;
+	INIT_LIST_HEAD(&ce->guc_id_link);
+
 	i915_active_init(&ce->active,
 			 __intel_context_active, __intel_context_retire, 0);
 }
drivers/gpu/drm/i915/gt/intel_context_types.h +19 -3
index 6d99631d19b9..606c480aec26 100644
@@ -96,6 +96,7 @@ struct intel_context {
 #define CONTEXT_BANNED			6
 #define CONTEXT_FORCE_SINGLE_SUBMISSION	7
 #define CONTEXT_NOPREEMPT		8
+#define CONTEXT_LRCA_DIRTY		9
 
 	struct {
 		u64 timeout_us;
@@ -138,14 +139,29 @@ struct intel_context {
 
 	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
 
+	struct {
+		/** lock: protects everything in guc_state */
+		spinlock_t lock;
+		/**
+		 * sched_state: scheduling state of this context using GuC
+		 * submission
+		 */
+		u8 sched_state;
+	} guc_state;
+
 	/* GuC scheduling state flags that do not require a lock. */
 	atomic_t guc_sched_state_no_lock;
 
+	/* GuC LRC descriptor ID */
+	u16 guc_id;
+
+	/* GuC LRC descriptor reference count */
+	atomic_t guc_id_ref;
+
 	/*
-	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
-	 * in the series will.
+	 * GuC ID link - in list when unpinned but guc_id still valid in GuC
 	 */
-	u16 guc_id;
+	struct list_head guc_id_link;
 };
 
 #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/intel_lrc_reg.h +0 -1
index 41e5350a7a05..49d4857ad9b7 100644
@@ -87,7 +87,6 @@
 #define GEN11_CSB_WRITE_PTR_MASK	(GEN11_CSB_PTR_MASK << 0)
 
 #define MAX_CONTEXT_HW_ID	(1 << 21) /* exclusive */
-#define MAX_GUC_CONTEXT_HW_ID	(1 << 20) /* exclusive */
 #define GEN11_MAX_CONTEXT_HW_ID	(1 << 11) /* exclusive */
 /* in Gen12 ID 0x7FF is reserved to indicate idle */
 #define GEN12_MAX_CONTEXT_HW_ID	(GEN11_MAX_CONTEXT_HW_ID - 1)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +40 -0
index 8c7b92f699f1..30773cd699f5 100644
@@ -7,6 +7,7 @@
 #define _INTEL_GUC_H_
 
 #include <linux/xarray.h>
+#include <linux/delay.h>
 
 #include "intel_uncore.h"
 #include "intel_guc_fw.h"
@@ -44,6 +45,14 @@ struct intel_guc {
 		void (*disable)(struct intel_guc *guc);
 	} interrupts;
 
+	/*
+	 * contexts_lock protects the pool of free guc ids and a linked list of
+	 * guc ids available to be stolen
+	 */
+	spinlock_t contexts_lock;
+	struct ida guc_ids;
+	struct list_head guc_id_list;
+
 	bool submission_selected;
 
 	struct i915_vma *ads_vma;
@@ -101,6 +110,34 @@ intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
 				 response_buf, response_buf_size, 0);
 }
 
+static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
+					   const u32 *action,
+					   u32 len,
+					   bool loop)
+{
+	int err;
+	unsigned int sleep_period_ms = 1;
+	bool not_atomic = !in_atomic() && !irqs_disabled();
+
+	/* No sleeping with spin locks, just busy loop */
+	might_sleep_if(loop && not_atomic);
+
+retry:
+	err = intel_guc_send_nb(guc, action, len);
+	if (unlikely(err == -EBUSY && loop)) {
+		if (likely(not_atomic)) {
+			if (msleep_interruptible(sleep_period_ms))
+				return -EINTR;
+			sleep_period_ms = sleep_period_ms << 1;
+		} else {
+			cpu_relax();
+		}
+		goto retry;
+	}
+
+	return err;
+}
+
 static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
 {
 	intel_guc_ct_event_handler(&guc->ct);
@@ -202,6 +239,9 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
 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);
+
 void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
 
 #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +4 -0
index 83ec60ea3f89..28ff82c5be45 100644
@@ -928,6 +928,10 @@ static int ct_process_request(struct intel_guc_ct *ct, struct ct_incoming_msg *r
 	case INTEL_GUC_ACTION_DEFAULT:
 		ret = intel_guc_to_host_process_recv_msg(guc, payload, len);
 		break;
+	case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
+		ret = intel_guc_deregister_done_process_msg(guc, payload,
+							    len);
+		break;
 	default:
 		ret = -EOPNOTSUPP;
 		break;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +615 -51

756 lines not on this page · raw message

drivers/gpu/drm/i915/i915_reg.h +1 -0
index 943fe485c662..204c95c39353 100644
@@ -4142,6 +4142,7 @@ enum {
 	FAULT_AND_CONTINUE /* Unsupported */
 };
 
+#define CTX_GTT_ADDRESS_MASK GENMASK(31, 12)
 #define GEN8_CTX_VALID (1 << 0)
 #define GEN8_CTX_FORCE_PD_RESTORE (1 << 1)
 #define GEN8_CTX_FORCE_RESTORE (1 << 2)
drivers/gpu/drm/i915/i915_request.c +1 -0
index 86b4c9f2613d..b48c4905d3fc 100644
@@ -407,6 +407,7 @@ bool i915_request_retire(struct i915_request *rq)
 	 */
 	if (!list_empty(&rq->sched.link))
 		remove_from_engine(rq);
+	atomic_dec(&rq->context->guc_id_ref);
 	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
 
 	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
Signature · 1 line
2.28.0
#10 lore raw

Re: [PATCH 06/51] drm/i915/guc: Implement GuC context operations for new inteface

Quoted 332 lines · Matthew Brost

Implement GuC context operations which includes GuC specific operations alloc, pin, unpin, and destroy.

v2:
(Daniel Vetter)
- Use msleep_interruptible rather than cond_resched in busy loop
(Michal)
- Remove C++ style comment

---
  drivers/gpu/drm/i915/gt/intel_context.c       |   5 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  22 +-
  drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |   1 -
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  40 ++
  drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   4 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 666 ++++++++++++++++--
  drivers/gpu/drm/i915/i915_reg.h               |   1 +
  drivers/gpu/drm/i915/i915_request.c           |   1 +
  8 files changed, 685 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +5 -0
index bd63813c8a80..32fd6647154b 100644
@@ -384,6 +384,11 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 
  	mutex_init(&ce->pin_mutex);
 
+	spin_lock_init(&ce->guc_state.lock);
+
+	ce->guc_id = GUC_INVALID_LRC_ID;
+	INIT_LIST_HEAD(&ce->guc_id_link);
+
  	i915_active_init(&ce->active,
  			 __intel_context_active, __intel_context_retire, 0);
  }
drivers/gpu/drm/i915/gt/intel_context_types.h +19 -3
index 6d99631d19b9..606c480aec26 100644
@@ -96,6 +96,7 @@ struct intel_context {
  #define CONTEXT_BANNED			6
  #define CONTEXT_FORCE_SINGLE_SUBMISSION	7
  #define CONTEXT_NOPREEMPT		8
+#define CONTEXT_LRCA_DIRTY		9
 
  	struct {
  		u64 timeout_us;
@@ -138,14 +139,29 @@ struct intel_context {
 
  	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
 
+	struct {
+		/** lock: protects everything in guc_state */
+		spinlock_t lock;
+		/**
+		 * sched_state: scheduling state of this context using GuC
+		 * submission
+		 */
+		u8 sched_state;
+	} guc_state;
+
  	/* GuC scheduling state flags that do not require a lock. */
  	atomic_t guc_sched_state_no_lock;
 
+	/* GuC LRC descriptor ID */
+	u16 guc_id;
+
+	/* GuC LRC descriptor reference count */
+	atomic_t guc_id_ref;
+
  	/*
-	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
-	 * in the series will.
+	 * GuC ID link - in list when unpinned but guc_id still valid in GuC
  	 */
-	u16 guc_id;
+	struct list_head guc_id_link;
  };
 
  #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/intel_lrc_reg.h +0 -1
index 41e5350a7a05..49d4857ad9b7 100644
@@ -87,7 +87,6 @@
  #define GEN11_CSB_WRITE_PTR_MASK	(GEN11_CSB_PTR_MASK << 0)
 
  #define MAX_CONTEXT_HW_ID	(1 << 21) /* exclusive */
-#define MAX_GUC_CONTEXT_HW_ID	(1 << 20) /* exclusive */
  #define GEN11_MAX_CONTEXT_HW_ID	(1 << 11) /* exclusive */
  /* in Gen12 ID 0x7FF is reserved to indicate idle */
  #define GEN12_MAX_CONTEXT_HW_ID	(GEN11_MAX_CONTEXT_HW_ID - 1)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +40 -0
index 8c7b92f699f1..30773cd699f5 100644
@@ -7,6 +7,7 @@
  #define _INTEL_GUC_H_
 
  #include <linux/xarray.h>
+#include <linux/delay.h>
 
  #include "intel_uncore.h"
  #include "intel_guc_fw.h"
@@ -44,6 +45,14 @@ struct intel_guc {
  		void (*disable)(struct intel_guc *guc);
  	} interrupts;
 
+	/*
+	 * contexts_lock protects the pool of free guc ids and a linked list of
+	 * guc ids available to be stolen
+	 */
+	spinlock_t contexts_lock;
+	struct ida guc_ids;
+	struct list_head guc_id_list;
+
  	bool submission_selected;
 
  	struct i915_vma *ads_vma;
@@ -101,6 +110,34 @@ intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
  				 response_buf, response_buf_size, 0);
  }
 
+static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
+					   const u32 *action,
+					   u32 len,
+					   bool loop)
+{
+	int err;
+	unsigned int sleep_period_ms = 1;
+	bool not_atomic = !in_atomic() && !irqs_disabled();
+
+	/* No sleeping with spin locks, just busy loop */
+	might_sleep_if(loop && not_atomic);
+
+retry:
+	err = intel_guc_send_nb(guc, action, len);
+	if (unlikely(err == -EBUSY && loop)) {
+		if (likely(not_atomic)) {
+			if (msleep_interruptible(sleep_period_ms))
+				return -EINTR;
+			sleep_period_ms = sleep_period_ms << 1;
+		} else {
+			cpu_relax();
+		}
+		goto retry;
+	}
+
+	return err;
+}
+
  static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
  {
  	intel_guc_ct_event_handler(&guc->ct);
@@ -202,6 +239,9 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
  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);
+
  void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
 
  #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +4 -0
index 83ec60ea3f89..28ff82c5be45 100644
@@ -928,6 +928,10 @@ static int ct_process_request(struct intel_guc_ct *ct, struct ct_incoming_msg *r
  	case INTEL_GUC_ACTION_DEFAULT:
  		ret = intel_guc_to_host_process_recv_msg(guc, payload, len);
  		break;
+	case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
+		ret = intel_guc_deregister_done_process_msg(guc, payload,
+							    len);
+		break;
  	default:
  		ret = -EOPNOTSUPP;
  		break;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +110 -2
index 53b4a5eb4a85..a47b3813b4d0 100644
@@ -13,7 +13,9 @@
  #include "gt/intel_gt.h"
  #include "gt/intel_gt_irq.h"
  #include "gt/intel_gt_pm.h"
+#include "gt/intel_gt_requests.h"
  #include "gt/intel_lrc.h"
+#include "gt/intel_lrc_reg.h"
  #include "gt/intel_mocs.h"
  #include "gt/intel_ring.h"
 
@@ -85,6 +87,73 @@ static inline void clr_context_enabled(struct intel_context *ce)
  		   &ce->guc_sched_state_no_lock);
  }
 
+/*
+ * Below is a set of functions which control the GuC scheduling state which
+ * require a lock, aside from the special case where the functions are called
+ * from guc_lrc_desc_pin(). In that case it isn't possible for any other code
+ * path to be executing on the context.
+ */
+#define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER	BIT(0)
+#define SCHED_STATE_DESTROYED				BIT(1)
+static inline void init_sched_state(struct intel_context *ce)
+{
+	/* Only should be called from guc_lrc_desc_pin() */
+	atomic_set(&ce->guc_sched_state_no_lock, 0);
+	ce->guc_state.sched_state = 0;
+}
+
+static inline bool
+context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state &
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);
+}
+
+static inline void
+set_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	/* Only should be called from guc_lrc_desc_pin() */
+	ce->guc_state.sched_state |=
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
+}
+
+static inline void
+clr_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state =
+		(ce->guc_state.sched_state &
+		 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);
+}
+
+static inline bool
+context_destroyed(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state & SCHED_STATE_DESTROYED);
+}
+
+static inline void
+set_context_destroyed(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
+}
+
+static inline bool context_guc_id_invalid(struct intel_context *ce)
+{
+	return (ce->guc_id == GUC_INVALID_LRC_ID);
+}
+
+static inline void set_context_guc_id_invalid(struct intel_context *ce)
+{
+	ce->guc_id = GUC_INVALID_LRC_ID;
+}
+
+static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
+{
+	return &ce->engine->gt->uc.guc;
+}
+
  static inline struct i915_priolist *to_priolist(struct rb_node *rb)
  {
  	return rb_entry(rb, struct i915_priolist, node);
@@ -155,6 +224,9 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	int len = 0;
  	bool enabled = context_enabled(ce);
 
+	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
+	GEM_BUG_ON(context_guc_id_invalid(ce));
+
  	if (!enabled) {
  		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
  		action[len++] = ce->guc_id;
@@ -417,6 +489,10 @@ int intel_guc_submission_init(struct intel_guc *guc)
 
  	xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
 
+	spin_lock_init(&guc->contexts_lock);
+	INIT_LIST_HEAD(&guc->guc_id_list);
+	ida_init(&guc->guc_ids);
+
  	return 0;
  }
 
@@ -429,9 +505,303 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  	i915_sched_engine_put(guc->sched_engine);
  }
 
-static int guc_context_alloc(struct intel_context *ce)
+static inline void queue_request(struct i915_sched_engine *sched_engine,
+				 struct i915_request *rq,
+				 int prio)
  {
-	return lrc_alloc(ce, ce->engine);
+	GEM_BUG_ON(!list_empty(&rq->sched.link));
+	list_add_tail(&rq->sched.link,
+		      i915_sched_lookup_priolist(sched_engine, prio));
+	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+}
+
+static int guc_bypass_tasklet_submit(struct intel_guc *guc,
+				     struct i915_request *rq)
+{
+	int ret;
+
+	__i915_request_submit(rq);
+
+	trace_i915_request_in(rq, 0);
+
+	guc_set_lrc_tail(rq);
+	ret = guc_add_request(guc, rq);
+	if (ret == -EBUSY)
+		guc->stalled_request = rq;
+
+	return ret;
+}
+
+static void guc_submit_request(struct i915_request *rq)
+{
+	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
+	struct intel_guc *guc = &rq->engine->gt->uc.guc;
+	unsigned long flags;
+
+	/* Will be called from irq-context when using foreign fences. */
+	spin_lock_irqsave(&sched_engine->lock, flags);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 552

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+
+	if (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);
+
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+#define GUC_ID_START	64	/* First 64 guc_ids reserved */

Reserved for what?

Quoted 222 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +222 -0
+static int new_guc_id(struct intel_guc *guc)
+{
+	return ida_simple_get(&guc->guc_ids, GUC_ID_START,
+			      GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+}
+
+static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	if (!context_guc_id_invalid(ce)) {
+		ida_simple_remove(&guc->guc_ids, ce->guc_id);
+		reset_lrc_desc(guc, ce->guc_id);
+		set_context_guc_id_invalid(ce);
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+}
+
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	__release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int steal_guc_id(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	int guc_id;
+
+	if (!list_empty(&guc->guc_id_list)) {
+		ce = list_first_entry(&guc->guc_id_list,
+				      struct intel_context,
+				      guc_id_link);
+
+		GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+		GEM_BUG_ON(context_guc_id_invalid(ce));
+
+		list_del_init(&ce->guc_id_link);
+		guc_id = ce->guc_id;
+		set_context_guc_id_invalid(ce);
+		return guc_id;
+	} else {
+		return -EAGAIN;
+	}
+}
+
+static int assign_guc_id(struct intel_guc *guc, u16 *out)
+{
+	int ret;
+
+	ret = new_guc_id(guc);
+	if (unlikely(ret < 0)) {
+		ret = steal_guc_id(guc);
+		if (ret < 0)
+			return ret;
+	}
+
+	*out = ret;
+	return 0;
+}
+
+#define PIN_GUC_ID_TRIES	4
+static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	int ret = 0;
+	unsigned long flags, tries = PIN_GUC_ID_TRIES;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+
+try_again:
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+
+	if (context_guc_id_invalid(ce)) {
+		ret = assign_guc_id(guc, &ce->guc_id);
+		if (ret)
+			goto out_unlock;
+		ret = 1;	/* Indidcates newly assigned guc_id */
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	atomic_inc(&ce->guc_id_ref);
+
+out_unlock:
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * -EAGAIN indicates no guc_ids are available, let's retire any
+	 * outstanding requests to see if that frees up a guc_id. If the first
+	 * retire didn't help, insert a sleep with the timeslice duration before
+	 * attempting to retire more requests. Double the sleep period each
+	 * subsequent pass before finally giving up. The sleep period has max of
+	 * 100ms and minimum of 1ms.
+	 */
+	if (ret == -EAGAIN && --tries) {
+		if (PIN_GUC_ID_TRIES - tries > 1) {
+			unsigned int timeslice_shifted =
+				ce->engine->props.timeslice_duration_ms <<
+				(PIN_GUC_ID_TRIES - tries - 2);
+			unsigned int max = min_t(unsigned int, 100,
+						 timeslice_shifted);
+
+			msleep(max_t(unsigned int, max, 1));
+		}
+		intel_gt_retire_requests(guc_to_gt(guc));
+		goto try_again;
+	}
+
+	return ret;
+}
+
+static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref) < 0);
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id_link) &&
+	    !atomic_read(&ce->guc_id_ref))
+		list_add_tail(&ce->guc_id_link, &guc->guc_id_list);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int __guc_action_register_context(struct intel_guc *guc,
+					 u32 guc_id,
+					 u32 offset)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_REGISTER_CONTEXT,
+		guc_id,
+		offset,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int register_context(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
+		ce->guc_id * sizeof(struct guc_lrc_desc);
+
+	return __guc_action_register_context(guc, ce->guc_id, offset);
+}
+
+static int __guc_action_deregister_context(struct intel_guc *guc,
+					   u32 guc_id)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
+		guc_id,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int deregister_context(struct intel_context *ce, u32 guc_id)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	return __guc_action_deregister_context(guc, guc_id);
+}
+
+static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
+{
+	switch (class) {
+	case RENDER_CLASS:
+		return mask >> RCS0;
+	case VIDEO_ENHANCEMENT_CLASS:
+		return mask >> VECS0;
+	case VIDEO_DECODE_CLASS:
+		return mask >> VCS0;
+	case COPY_ENGINE_CLASS:
+		return mask >> BCS0;
+	default:
+		GEM_BUG_ON("Invalid Class");
+		return 0;
+	}
+}
+
+static void guc_context_policy_init(struct intel_engine_cs *engine,
+				    struct guc_lrc_desc *desc)
+{
+	desc->policy_flags = 0;
+
+	desc->execution_quantum = CONTEXT_POLICY_DEFAULT_EXECUTION_QUANTUM_US;
+	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
+}
+
+static int guc_lrc_desc_pin(struct intel_context *ce)
+{
+	struct intel_runtime_pm *runtime_pm =
+		&ce->engine->gt->i915->runtime_pm;
+	struct intel_engine_cs *engine = ce->engine;
+	struct intel_guc *guc = &engine->gt->uc.guc;
+	u32 desc_idx = ce->guc_id;
+	struct guc_lrc_desc *desc;
+	bool context_registered;
+	intel_wakeref_t wakeref;
+	int ret = 0;
+
+	GEM_BUG_ON(!engine->mask);
+
+	/*
+	 * Ensure LRC + CT vmas are is same region as write barrier is done
+	 * based on CT vma region.
+	 */
+	GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
+		   i915_gem_object_is_lmem(ce->ring->vma->obj));
+
+	context_registered = lrc_desc_registered(guc, desc_idx);
+
+	reset_lrc_desc(guc, desc_idx);
+	set_lrc_desc_registered(guc, desc_idx, ce);
+
+	desc = __get_lrc_desc(guc, desc_idx);
+	desc->engine_class = engine_class_to_guc_class(engine->class);
+	desc->engine_submit_mask = adjust_engine_mask(engine->class,
+						      engine->mask);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 784

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	desc->hw_context_desc = ce->lrc.lrca;
+	desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+	guc_context_policy_init(engine, desc);
+	init_sched_state(ce);
+
+	/*
+	 * The context_lookup xarray is used to determine if the hardware
+	 * context is currently registered. There are two cases in which it
+	 * could be regisgered either the guc_id has been stole from from

'regisgered' and 'stole' should be 'stolen'

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 795

Quoted 11 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -0
+	 * another context or the lrc descriptor address of this context has
+	 * changed. In either case the context needs to be deregistered with the
+	 * GuC before registering this context.
+	 */
+	if (context_registered) {
+		set_context_wait_for_deregister_to_register(ce);
+		intel_context_get(ce);
+
+		/*
+		 * If stealing the guc_id, this ce has the same guc_id as the
+		 * context whos guc_id was stole.

whose guc_id was stolen

Quoted 30 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+		 */
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			ret = deregister_context(ce, ce->guc_id);
+	} else {
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			ret = register_context(ce);
+	}
+
+	return ret;
  }
 
  static int guc_context_pre_pin(struct intel_context *ce,
@@ -443,36 +813,139 @@ static int guc_context_pre_pin(struct intel_context *ce,
 
  static int guc_context_pin(struct intel_context *ce, 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);
  }
 
+static void guc_context_unpin(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+}

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 839

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+
+static void guc_context_post_unpin(struct intel_context *ce)
+{
+	lrc_post_unpin(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;

Use ce_to_guc?

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	unsigned long flags;
+
+	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
+	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
+
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	set_context_destroyed(ce);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 856

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+
+	deregister_context(ce, ce->guc_id);
+}
+
+static void guc_context_destroy(struct kref *kref)
+{
+	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
+	struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;
+	struct intel_guc *guc = &ce->engine->gt->uc.guc;

Also ce_to_guc?

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	intel_wakeref_t wakeref;
+	unsigned long flags;
+
+	/*
+	 * 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) ||
+	    !lrc_desc_registered(guc, ce->guc_id)) {

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 876

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+		release_guc_id(guc, ce);
+		lrc_destroy(kref);
+		return;
+	}
+
+	/*
+	 * We have to acquire the context spinlock and check guc_id again, if it
+	 * is valid it hasn't been stolen and needs to be deregistered. We
+	 * delete this context from the list of unpinned guc_ids available to
+	 * stole to seal a race with guc_lrc_desc_pin(). When the G2H CTB

stole -> steal

Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+	 * returns indicating this context has been deregistered the guc_id is
+	 * returned to the pool of available guc_ids.
+	 */
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (context_guc_id_invalid(ce)) {
+		__release_guc_id(guc, ce);
+		spin_unlock_irqrestore(&guc->contexts_lock, flags);
+		lrc_destroy(kref);
+		return;
+	}
+
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 899

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * We defer GuC context deregistration until the context is destroyed
+	 * in order to save on CTBs. With this optimization ideally we only need
+	 * 1 CTB to register the context during the first pin and 1 CTB to
+	 * deregister the context when the context is destroyed. Without this
+	 * optimization, a CTB would be needed every pin & unpin.
+	 *
+	 * XXX: Need to acqiure the runtime wakeref as this can be triggered

FIXME rather than XXX?

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 900

Quoted 1 line
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+	 * from context_free_worker when not runtime wakeref is held.

when runtime wakeref is not held

Quoted 118 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +44 -42
+	 * guc_lrc_desc_unpin requires the runtime as a GuC register is written
+	 * in H2G CTB to deregister the context. A future patch may defer this
+	 * H2G CTB if the runtime wakeref is zero.
+	 */
+	with_intel_runtime_pm(runtime_pm, wakeref)
+		guc_lrc_desc_unpin(ce);
+}
+
+static int guc_context_alloc(struct intel_context *ce)
+{
+	return lrc_alloc(ce, ce->engine);
+}
+
  static const struct intel_context_ops guc_context_ops = {
  	.alloc = guc_context_alloc,
 
  	.pre_pin = guc_context_pre_pin,
  	.pin = guc_context_pin,
-	.unpin = lrc_unpin,
-	.post_unpin = lrc_post_unpin,
+	.unpin = guc_context_unpin,
+	.post_unpin = guc_context_post_unpin,
 
  	.enter = intel_context_enter_engine,
  	.exit = intel_context_exit_engine,
 
  	.reset = lrc_reset,
-	.destroy = lrc_destroy,
+	.destroy = guc_context_destroy,
  };
 
-static int guc_request_alloc(struct i915_request *request)
+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);
+}
+
+static int guc_request_alloc(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+	struct intel_guc *guc = ce_to_guc(ce);
  	int ret;
 
-	GEM_BUG_ON(!intel_context_is_pinned(request->context));
+	GEM_BUG_ON(!intel_context_is_pinned(rq->context));
 
  	/*
  	 * Flush enough space to reduce the likelihood of waiting after
  	 * we start building the request - in which case we will just
  	 * have to repeat work.
  	 */
-	request->reserved_space += GUC_REQUEST_SIZE;
+	rq->reserved_space += GUC_REQUEST_SIZE;
 
  	/*
  	 * Note that after this point, we have committed to using
@@ -483,56 +956,47 @@ static int guc_request_alloc(struct i915_request *request)
  	 */
 
  	/* Unconditionally invalidate GPU caches and TLBs. */
-	ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
+	ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
  	if (ret)
  		return ret;
 
-	request->reserved_space -= GUC_REQUEST_SIZE;
-	return 0;
-}
+	rq->reserved_space -= GUC_REQUEST_SIZE;
 
-static inline void queue_request(struct i915_sched_engine *sched_engine,
-				 struct i915_request *rq,
-				 int prio)
-{
-	GEM_BUG_ON(!list_empty(&rq->sched.link));
-	list_add_tail(&rq->sched.link,
-		      i915_sched_lookup_priolist(sched_engine, prio));
-	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-}
-
-static int guc_bypass_tasklet_submit(struct intel_guc *guc,
-				     struct i915_request *rq)
-{
-	int ret;
-
-	__i915_request_submit(rq);
-
-	trace_i915_request_in(rq, 0);
-
-	guc_set_lrc_tail(rq);
-	ret = guc_add_request(guc, rq);
-	if (ret == -EBUSY)
-		guc->stalled_request = rq;
-
-	return ret;
-}
-
-static void guc_submit_request(struct i915_request *rq)
-{
-	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
-	struct intel_guc *guc = &rq->engine->gt->uc.guc;
-	unsigned long flags;
+	/*
+	 * Call pin_guc_id here rather than in the pinning step as with
+	 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
+	 * guc_ids and creating horrible race conditions. This is especially bad
+	 * when guc_ids are being stolen due to over subscription. By the time
+	 * this function is reached, it is guaranteed that the guc_id will be
+	 * persistent until the generated request is retired. Thus, sealing these
+	 * race conditions. It is still safe to fail here if guc_ids are
+	 * exhausted and return -EAGAIN to the user indicating that they can try
+	 * again in the future.
+	 *
+	 * There is no need for a lock here as the timeline mutex ensures at
+	 * most one context can be executing this code path at once. The
+	 * guc_id_ref is incremented once for every request in flight and
+	 * decremented on each retire. When it is zero, a lock around the

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_request_alloc(struct i915_request *request) · line 987

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -2
+	 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
+	 */
+	if (atomic_add_unless(&ce->guc_id_ref, 1, 0))
+		return 0;
 
-	/* Will be called from irq-context when using foreign fences. */
-	spin_lock_irqsave(&sched_engine->lock, flags);
+	ret = pin_guc_id(guc, ce);	/* returns 1 if new guc_id assigned */
+	if (unlikely(ret < 0))
+		return ret;;

;;

Quoted 35 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +21 -5
+	if (context_needs_register(ce, !!ret)) {
+		ret = guc_lrc_desc_pin(ce);
+		if (unlikely(ret)) {	/* unwind */
+			atomic_dec(&ce->guc_id_ref);
+			unpin_guc_id(guc, ce);
+			return ret;
+		}
+	}
 
-	if (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);
+	clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
 
-	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	return 0;
  }
 
  static void sanitize_hwsp(struct intel_engine_cs *engine)
@@ -606,6 +1070,46 @@ static void guc_set_default_submission(struct intel_engine_cs *engine)
  	engine->submit_request = guc_submit_request;
  }
 
+static inline void guc_kernel_context_pin(struct intel_guc *guc,
+					  struct intel_context *ce)
+{
+	if (context_guc_id_invalid(ce))
+		pin_guc_id(guc, ce);
+	guc_lrc_desc_pin(ce);
+}
+
+static inline void guc_init_lrc_mapping(struct intel_guc *guc)
+{
+	struct intel_gt *gt = guc_to_gt(guc);

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

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	struct intel_engine_cs *engine;
+	enum intel_engine_id id;
+
+	/* make sure all descriptors are clean... */
+	xa_destroy(&guc->context_lookup);
+
+	/*
+	 * Some contexts might have been pinned before we enabled GuC
+	 * submission, so we need to add them to the GuC bookeeping.
+	 * Also, after a reset the GuC we want to make sure that the information

reset of the GuC

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

Quoted 1 line
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+	 * shared with GuC is properly reset. The kernel lrcs are not attached

LRCs

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

Quoted 3 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+	 * to the gem_context, so they need to be added separately.
+	 *
+	 * Note: we purposely do not check the error return of

check the return of gldp for errors

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

Quoted 3 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+	 * guc_lrc_desc_pin, because that function can only fail in two cases.
+	 * One, if there aren't enough free IDs, but we're guaranteed to have
+	 * enough here (we're either only pinning a handful of lrc on first boot

LRCs and same on line below

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

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+	 * or we're re-pinning lrcs that were already pinned before the reset).
+	 * Two, if the GuC has died and CTBs can't make forward progress.
+	 * Presumably, the GuC should be alive as this function is called on
+	 * driver load or after a reset. Even if it is dead, another full GPU

GPU -> GT

Quoted 22 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -0
+	 * reset will be triggered and this function would be called again.
+	 */
+
+	for_each_engine(engine, gt, id)
+		if (engine->kernel_context)
+			guc_kernel_context_pin(guc, engine->kernel_context);
+}
+
  static void guc_release(struct intel_engine_cs *engine)
  {
  	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
@@ -718,6 +1222,7 @@ int intel_guc_submission_setup(struct intel_engine_cs *engine)
 
  void intel_guc_submission_enable(struct intel_guc *guc)
  {
+	guc_init_lrc_mapping(guc);
  }
 
  void intel_guc_submission_disable(struct intel_guc *guc)
@@ -743,3 +1248,62 @@ void intel_guc_submission_init_early(struct intel_guc *guc)
  {
  	guc->submission_selected = __guc_submission_selected(guc);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_init_early(struct intel_guc *guc) · line 1259

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -0
  }
+
+static inline struct intel_context *
+g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
+{
+	struct intel_context *ce;
+
+	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Invalid desc_idx %u", desc_idx);

Agree these should not be BUG_ONs or similar, but a drm_err seems appropriate. If either this or the error below occurs the something has gone quite wrong and it should be reported.

Quoted 12 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +12 -0
+		return NULL;
+	}
+
+	ce = __get_context(guc, desc_idx);
+	if (unlikely(!ce)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Context is NULL, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
+	return ce;
+}

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_init_early(struct intel_guc *guc) · line 1281

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+
+int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
+					  const u32 *msg,
+					  u32 len)
+{
+	struct intel_context *ce;
+	u32 desc_idx = msg[0];
+
+	if (unlikely(len < 1)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);

As above, should be a drm_err I would say.

Quoted 45 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +28 -0
+		return -EPROTO;
+	}
+
+	ce = g2h_context_lookup(guc, desc_idx);
+	if (unlikely(!ce))
+		return -EPROTO;
+
+	if (context_wait_for_deregister_to_register(ce)) {
+		struct intel_runtime_pm *runtime_pm =
+			&ce->engine->gt->i915->runtime_pm;
+		intel_wakeref_t wakeref;
+
+		/*
+		 * Previous owner of this guc_id has been deregistered, now safe
+		 * register this context.
+		 */
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			register_context(ce);
+		clr_context_wait_for_deregister_to_register(ce);
+		intel_context_put(ce);
+	} else if (context_destroyed(ce)) {
+		/* Context has been destroyed */
+		release_guc_id(guc, ce);
+		lrc_destroy(&ce->ref);
+	}
+
+	return 0;
+}
drivers/gpu/drm/i915/i915_reg.h +1 -0
index 943fe485c662..204c95c39353 100644
@@ -4142,6 +4142,7 @@ enum {
  	FAULT_AND_CONTINUE /* Unsupported */
  };
 
+#define CTX_GTT_ADDRESS_MASK GENMASK(31, 12)
  #define GEN8_CTX_VALID (1 << 0)
  #define GEN8_CTX_FORCE_PD_RESTORE (1 << 1)
  #define GEN8_CTX_FORCE_RESTORE (1 << 2)
drivers/gpu/drm/i915/i915_request.c +0 -0
index 86b4c9f2613d..b48c4905d3fc 100644

drivers/gpu/drm/i915/i915_request.c bool i915_request_retire(struct i915_request *rq) · line 413

Quoted 7 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
@@ -407,6 +407,7 @@ bool i915_request_retire(struct i915_request *rq)
  	 */
  	if (!list_empty(&rq->sched.link))
  		remove_from_engine(rq);
+	atomic_dec(&rq->context->guc_id_ref);
  	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
 
  	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */

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

#11 lore raw

Re: [PATCH 06/51] drm/i915/guc: Implement GuC context operations for new inteface

Quoted 314 lines · John Harrison
Quoted 313 lines · Matthew Brost

Implement GuC context operations which includes GuC specific operations alloc, pin, unpin, and destroy.

v2:
(Daniel Vetter)
- Use msleep_interruptible rather than cond_resched in busy loop
(Michal)
- Remove C++ style comment

---
  drivers/gpu/drm/i915/gt/intel_context.c       |   5 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  22 +-
  drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |   1 -
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  40 ++
  drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   4 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 666 ++++++++++++++++--
  drivers/gpu/drm/i915/i915_reg.h               |   1 +
  drivers/gpu/drm/i915/i915_request.c           |   1 +
  8 files changed, 685 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +5 -0
index bd63813c8a80..32fd6647154b 100644
@@ -384,6 +384,11 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	mutex_init(&ce->pin_mutex);
+	spin_lock_init(&ce->guc_state.lock);
+
+	ce->guc_id = GUC_INVALID_LRC_ID;
+	INIT_LIST_HEAD(&ce->guc_id_link);
+
  	i915_active_init(&ce->active,
  			 __intel_context_active, __intel_context_retire, 0);
  }
drivers/gpu/drm/i915/gt/intel_context_types.h +19 -3
index 6d99631d19b9..606c480aec26 100644
@@ -96,6 +96,7 @@ struct intel_context {
  #define CONTEXT_BANNED			6
  #define CONTEXT_FORCE_SINGLE_SUBMISSION	7
  #define CONTEXT_NOPREEMPT		8
+#define CONTEXT_LRCA_DIRTY		9
  	struct {
  		u64 timeout_us;
@@ -138,14 +139,29 @@ struct intel_context {
  	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
+	struct {
+		/** lock: protects everything in guc_state */
+		spinlock_t lock;
+		/**
+		 * sched_state: scheduling state of this context using GuC
+		 * submission
+		 */
+		u8 sched_state;
+	} guc_state;
+
  	/* GuC scheduling state flags that do not require a lock. */
  	atomic_t guc_sched_state_no_lock;
+	/* GuC LRC descriptor ID */
+	u16 guc_id;
+
+	/* GuC LRC descriptor reference count */
+	atomic_t guc_id_ref;
+
  	/*
-	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
-	 * in the series will.
+	 * GuC ID link - in list when unpinned but guc_id still valid in GuC
  	 */
-	u16 guc_id;
+	struct list_head guc_id_link;
  };
  #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/intel_lrc_reg.h +0 -1
index 41e5350a7a05..49d4857ad9b7 100644
@@ -87,7 +87,6 @@
  #define GEN11_CSB_WRITE_PTR_MASK	(GEN11_CSB_PTR_MASK << 0)
  #define MAX_CONTEXT_HW_ID	(1 << 21) /* exclusive */
-#define MAX_GUC_CONTEXT_HW_ID	(1 << 20) /* exclusive */
  #define GEN11_MAX_CONTEXT_HW_ID	(1 << 11) /* exclusive */
  /* in Gen12 ID 0x7FF is reserved to indicate idle */
  #define GEN12_MAX_CONTEXT_HW_ID	(GEN11_MAX_CONTEXT_HW_ID - 1)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +40 -0
index 8c7b92f699f1..30773cd699f5 100644
@@ -7,6 +7,7 @@
  #define _INTEL_GUC_H_
  #include <linux/xarray.h>
+#include <linux/delay.h>
  #include "intel_uncore.h"
  #include "intel_guc_fw.h"
@@ -44,6 +45,14 @@ struct intel_guc {
  		void (*disable)(struct intel_guc *guc);
  	} interrupts;
+	/*
+	 * contexts_lock protects the pool of free guc ids and a linked list of
+	 * guc ids available to be stolen
+	 */
+	spinlock_t contexts_lock;
+	struct ida guc_ids;
+	struct list_head guc_id_list;
+
  	bool submission_selected;
  	struct i915_vma *ads_vma;
@@ -101,6 +110,34 @@ intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
  				 response_buf, response_buf_size, 0);
  }
+static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
+					   const u32 *action,
+					   u32 len,
+					   bool loop)
+{
+	int err;
+	unsigned int sleep_period_ms = 1;
+	bool not_atomic = !in_atomic() && !irqs_disabled();
+
+	/* No sleeping with spin locks, just busy loop */
+	might_sleep_if(loop && not_atomic);
+
+retry:
+	err = intel_guc_send_nb(guc, action, len);
+	if (unlikely(err == -EBUSY && loop)) {
+		if (likely(not_atomic)) {
+			if (msleep_interruptible(sleep_period_ms))
+				return -EINTR;
+			sleep_period_ms = sleep_period_ms << 1;
+		} else {
+			cpu_relax();
+		}
+		goto retry;
+	}
+
+	return err;
+}
+
  static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
  {
  	intel_guc_ct_event_handler(&guc->ct);
@@ -202,6 +239,9 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
  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);
+
  void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
  #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +4 -0
index 83ec60ea3f89..28ff82c5be45 100644
@@ -928,6 +928,10 @@ static int ct_process_request(struct intel_guc_ct *ct, struct ct_incoming_msg *r
  	case INTEL_GUC_ACTION_DEFAULT:
  		ret = intel_guc_to_host_process_recv_msg(guc, payload, len);
  		break;
+	case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
+		ret = intel_guc_deregister_done_process_msg(guc, payload,
+							    len);
+		break;
  	default:
  		ret = -EOPNOTSUPP;
  		break;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +113 -2
index 53b4a5eb4a85..a47b3813b4d0 100644
@@ -13,7 +13,9 @@
  #include "gt/intel_gt.h"
  #include "gt/intel_gt_irq.h"
  #include "gt/intel_gt_pm.h"
+#include "gt/intel_gt_requests.h"
  #include "gt/intel_lrc.h"
+#include "gt/intel_lrc_reg.h"
  #include "gt/intel_mocs.h"
  #include "gt/intel_ring.h"
@@ -85,6 +87,73 @@ static inline void clr_context_enabled(struct intel_context *ce)
  		   &ce->guc_sched_state_no_lock);
  }
+/*
+ * Below is a set of functions which control the GuC scheduling state which
+ * require a lock, aside from the special case where the functions are called
+ * from guc_lrc_desc_pin(). In that case it isn't possible for any other code
+ * path to be executing on the context.
+ */
+#define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER	BIT(0)
+#define SCHED_STATE_DESTROYED				BIT(1)
+static inline void init_sched_state(struct intel_context *ce)
+{
+	/* Only should be called from guc_lrc_desc_pin() */
+	atomic_set(&ce->guc_sched_state_no_lock, 0);
+	ce->guc_state.sched_state = 0;
+}
+
+static inline bool
+context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state &
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);
+}
+
+static inline void
+set_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	/* Only should be called from guc_lrc_desc_pin() */
+	ce->guc_state.sched_state |=
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
+}
+
+static inline void
+clr_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state =
+		(ce->guc_state.sched_state &
+		 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);
+}
+
+static inline bool
+context_destroyed(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state & SCHED_STATE_DESTROYED);
+}
+
+static inline void
+set_context_destroyed(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
+}
+
+static inline bool context_guc_id_invalid(struct intel_context *ce)
+{
+	return (ce->guc_id == GUC_INVALID_LRC_ID);
+}
+
+static inline void set_context_guc_id_invalid(struct intel_context *ce)
+{
+	ce->guc_id = GUC_INVALID_LRC_ID;
+}
+
+static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
+{
+	return &ce->engine->gt->uc.guc;
+}
+
  static inline struct i915_priolist *to_priolist(struct rb_node *rb)
  {
  	return rb_entry(rb, struct i915_priolist, node);
@@ -155,6 +224,9 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	int len = 0;
  	bool enabled = context_enabled(ce);
+	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
+	GEM_BUG_ON(context_guc_id_invalid(ce));
+
  	if (!enabled) {
  		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
  		action[len++] = ce->guc_id;
@@ -417,6 +489,10 @@ int intel_guc_submission_init(struct intel_guc *guc)
  	xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
+	spin_lock_init(&guc->contexts_lock);
+	INIT_LIST_HEAD(&guc->guc_id_list);
+	ida_init(&guc->guc_ids);
+
  	return 0;
  }
@@ -429,9 +505,303 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  	i915_sched_engine_put(guc->sched_engine);
  }
-static int guc_context_alloc(struct intel_context *ce)
+static inline void queue_request(struct i915_sched_engine *sched_engine,
+				 struct i915_request *rq,
+				 int prio)
  {
-	return lrc_alloc(ce, ce->engine);
+	GEM_BUG_ON(!list_empty(&rq->sched.link));
+	list_add_tail(&rq->sched.link,
+		      i915_sched_lookup_priolist(sched_engine, prio));
+	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+}
+
+static int guc_bypass_tasklet_submit(struct intel_guc *guc,
+				     struct i915_request *rq)
+{
+	int ret;
+
+	__i915_request_submit(rq);
+
+	trace_i915_request_in(rq, 0);
+
+	guc_set_lrc_tail(rq);
+	ret = guc_add_request(guc, rq);
+	if (ret == -EBUSY)
+		guc->stalled_request = rq;
+
+	return ret;
+}
+
+static void guc_submit_request(struct i915_request *rq)
+{
+	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
+	struct intel_guc *guc = &rq->engine->gt->uc.guc;
+	unsigned long flags;
+
+	/* 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))
+		queue_request(sched_engine, rq, rq_prio(rq));
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 552

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
+		tasklet_hi_schedule(&sched_engine->tasklet);
+
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+#define GUC_ID_START	64	/* First 64 guc_ids reserved */

Reserved for what?

A memory corruption which I found today. This is no longer needed.

Quoted 225 lines
Quoted 225 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +225 -0
+static int new_guc_id(struct intel_guc *guc)
+{
+	return ida_simple_get(&guc->guc_ids, GUC_ID_START,
+			      GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+}
+
+static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	if (!context_guc_id_invalid(ce)) {
+		ida_simple_remove(&guc->guc_ids, ce->guc_id);
+		reset_lrc_desc(guc, ce->guc_id);
+		set_context_guc_id_invalid(ce);
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+}
+
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	__release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int steal_guc_id(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	int guc_id;
+
+	if (!list_empty(&guc->guc_id_list)) {
+		ce = list_first_entry(&guc->guc_id_list,
+				      struct intel_context,
+				      guc_id_link);
+
+		GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+		GEM_BUG_ON(context_guc_id_invalid(ce));
+
+		list_del_init(&ce->guc_id_link);
+		guc_id = ce->guc_id;
+		set_context_guc_id_invalid(ce);
+		return guc_id;
+	} else {
+		return -EAGAIN;
+	}
+}
+
+static int assign_guc_id(struct intel_guc *guc, u16 *out)
+{
+	int ret;
+
+	ret = new_guc_id(guc);
+	if (unlikely(ret < 0)) {
+		ret = steal_guc_id(guc);
+		if (ret < 0)
+			return ret;
+	}
+
+	*out = ret;
+	return 0;
+}
+
+#define PIN_GUC_ID_TRIES	4
+static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	int ret = 0;
+	unsigned long flags, tries = PIN_GUC_ID_TRIES;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+
+try_again:
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+
+	if (context_guc_id_invalid(ce)) {
+		ret = assign_guc_id(guc, &ce->guc_id);
+		if (ret)
+			goto out_unlock;
+		ret = 1;	/* Indidcates newly assigned guc_id */
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	atomic_inc(&ce->guc_id_ref);
+
+out_unlock:
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * -EAGAIN indicates no guc_ids are available, let's retire any
+	 * outstanding requests to see if that frees up a guc_id. If the first
+	 * retire didn't help, insert a sleep with the timeslice duration before
+	 * attempting to retire more requests. Double the sleep period each
+	 * subsequent pass before finally giving up. The sleep period has max of
+	 * 100ms and minimum of 1ms.
+	 */
+	if (ret == -EAGAIN && --tries) {
+		if (PIN_GUC_ID_TRIES - tries > 1) {
+			unsigned int timeslice_shifted =
+				ce->engine->props.timeslice_duration_ms <<
+				(PIN_GUC_ID_TRIES - tries - 2);
+			unsigned int max = min_t(unsigned int, 100,
+						 timeslice_shifted);
+
+			msleep(max_t(unsigned int, max, 1));
+		}
+		intel_gt_retire_requests(guc_to_gt(guc));
+		goto try_again;
+	}
+
+	return ret;
+}
+
+static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref) < 0);
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id_link) &&
+	    !atomic_read(&ce->guc_id_ref))
+		list_add_tail(&ce->guc_id_link, &guc->guc_id_list);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int __guc_action_register_context(struct intel_guc *guc,
+					 u32 guc_id,
+					 u32 offset)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_REGISTER_CONTEXT,
+		guc_id,
+		offset,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int register_context(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
+		ce->guc_id * sizeof(struct guc_lrc_desc);
+
+	return __guc_action_register_context(guc, ce->guc_id, offset);
+}
+
+static int __guc_action_deregister_context(struct intel_guc *guc,
+					   u32 guc_id)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
+		guc_id,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int deregister_context(struct intel_context *ce, u32 guc_id)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	return __guc_action_deregister_context(guc, guc_id);
+}
+
+static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
+{
+	switch (class) {
+	case RENDER_CLASS:
+		return mask >> RCS0;
+	case VIDEO_ENHANCEMENT_CLASS:
+		return mask >> VECS0;
+	case VIDEO_DECODE_CLASS:
+		return mask >> VCS0;
+	case COPY_ENGINE_CLASS:
+		return mask >> BCS0;
+	default:
+		GEM_BUG_ON("Invalid Class");
+		return 0;
+	}
+}
+
+static void guc_context_policy_init(struct intel_engine_cs *engine,
+				    struct guc_lrc_desc *desc)
+{
+	desc->policy_flags = 0;
+
+	desc->execution_quantum = CONTEXT_POLICY_DEFAULT_EXECUTION_QUANTUM_US;
+	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
+}
+
+static int guc_lrc_desc_pin(struct intel_context *ce)
+{
+	struct intel_runtime_pm *runtime_pm =
+		&ce->engine->gt->i915->runtime_pm;
+	struct intel_engine_cs *engine = ce->engine;
+	struct intel_guc *guc = &engine->gt->uc.guc;
+	u32 desc_idx = ce->guc_id;
+	struct guc_lrc_desc *desc;
+	bool context_registered;
+	intel_wakeref_t wakeref;
+	int ret = 0;
+
+	GEM_BUG_ON(!engine->mask);
+
+	/*
+	 * Ensure LRC + CT vmas are is same region as write barrier is done
+	 * based on CT vma region.
+	 */
+	GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
+		   i915_gem_object_is_lmem(ce->ring->vma->obj));
+
+	context_registered = lrc_desc_registered(guc, desc_idx);
+
+	reset_lrc_desc(guc, desc_idx);
+	set_lrc_desc_registered(guc, desc_idx, ce);
+
+	desc = __get_lrc_desc(guc, desc_idx);
+	desc->engine_class = engine_class_to_guc_class(engine->class);
+	desc->engine_submit_mask = adjust_engine_mask(engine->class,
+						      engine->mask);
+	desc->hw_context_desc = ce->lrc.lrca;
+	desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 784

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	guc_context_policy_init(engine, desc);
+	init_sched_state(ce);
+
+	/*
+	 * The context_lookup xarray is used to determine if the hardware
+	 * context is currently registered. There are two cases in which it
+	 * could be regisgered either the guc_id has been stole from from

'regisgered' and 'stole' should be 'stolen'

Yep.

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+	 * another context or the lrc descriptor address of this context has
+	 * changed. In either case the context needs to be deregistered with the
+	 * GuC before registering this context.
+	 */
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 795

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	if (context_registered) {
+		set_context_wait_for_deregister_to_register(ce);
+		intel_context_get(ce);
+
+		/*
+		 * If stealing the guc_id, this ce has the same guc_id as the
+		 * context whos guc_id was stole.

whose guc_id was stolen

Yep.

Quoted 30 lines
Quoted 30 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +23 -0
+		 */
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			ret = deregister_context(ce, ce->guc_id);
+	} else {
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			ret = register_context(ce);
+	}
+
+	return ret;
  }
  static int guc_context_pre_pin(struct intel_context *ce,
@@ -443,36 +813,139 @@ static int guc_context_pre_pin(struct intel_context *ce,
  static int guc_context_pin(struct intel_context *ce, 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);
  }
+static void guc_context_unpin(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+}
+
+static void guc_context_post_unpin(struct intel_context *ce)
+{
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 839

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	lrc_post_unpin(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;

Use ce_to_guc?

Yes.

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	unsigned long flags;
+
+	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
+	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
+
+	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);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 856

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static void guc_context_destroy(struct kref *kref)
+{
+	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
+	struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;
+	struct intel_guc *guc = &ce->engine->gt->uc.guc;

Also ce_to_guc?

Yes.

Quoted 38 lines
Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	intel_wakeref_t wakeref;
+	unsigned long flags;
+
+	/*
+	 * 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) ||
+	    !lrc_desc_registered(guc, ce->guc_id)) {

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 876

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+		release_guc_id(guc, ce);
+		lrc_destroy(kref);
+		return;
+	}
+
+	/*
+	 * We have to acquire the context spinlock and check guc_id again, if it
+	 * is valid it hasn't been stolen and needs to be deregistered. We
+	 * delete this context from the list of unpinned guc_ids available to
+	 * stole to seal a race with guc_lrc_desc_pin(). When the G2H CTB

stole -> steal

Quoted 16 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +16 -0
+	 * returns indicating this context has been deregistered the guc_id is
+	 * returned to the pool of available guc_ids.
+	 */
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (context_guc_id_invalid(ce)) {
+		__release_guc_id(guc, ce);
+		spin_unlock_irqrestore(&guc->contexts_lock, flags);
+		lrc_destroy(kref);
+		return;
+	}
+
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 899

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	 * We defer GuC context deregistration until the context is destroyed
+	 * in order to save on CTBs. With this optimization ideally we only need
+	 * 1 CTB to register the context during the first pin and 1 CTB to
+	 * deregister the context when the context is destroyed. Without this
+	 * optimization, a CTB would be needed every pin & unpin.
+	 *
+	 * XXX: Need to acqiure the runtime wakeref as this can be triggered

FIXME rather than XXX?

I think XXX is correct as it isn't broken. After this series gets merged have a patch in the follow on series that fixes this.

Quoted 3 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 900

Quoted 1 line
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+	 * from context_free_worker when not runtime wakeref is held.

when runtime wakeref is not held

Yep.

Quoted 153 lines
Quoted 107 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +43 -42
+	 * guc_lrc_desc_unpin requires the runtime as a GuC register is written
+	 * in H2G CTB to deregister the context. A future patch may defer this
+	 * H2G CTB if the runtime wakeref is zero.
+	 */
+	with_intel_runtime_pm(runtime_pm, wakeref)
+		guc_lrc_desc_unpin(ce);
+}
+
+static int guc_context_alloc(struct intel_context *ce)
+{
+	return lrc_alloc(ce, ce->engine);
+}
+
  static const struct intel_context_ops guc_context_ops = {
  	.alloc = guc_context_alloc,
  	.pre_pin = guc_context_pre_pin,
  	.pin = guc_context_pin,
-	.unpin = lrc_unpin,
-	.post_unpin = lrc_post_unpin,
+	.unpin = guc_context_unpin,
+	.post_unpin = guc_context_post_unpin,
  	.enter = intel_context_enter_engine,
  	.exit = intel_context_exit_engine,
  	.reset = lrc_reset,
-	.destroy = lrc_destroy,
+	.destroy = guc_context_destroy,
  };
-static int guc_request_alloc(struct i915_request *request)
+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);
+}
+
+static int guc_request_alloc(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+	struct intel_guc *guc = ce_to_guc(ce);
  	int ret;
-	GEM_BUG_ON(!intel_context_is_pinned(request->context));
+	GEM_BUG_ON(!intel_context_is_pinned(rq->context));
  	/*
  	 * Flush enough space to reduce the likelihood of waiting after
  	 * we start building the request - in which case we will just
  	 * have to repeat work.
  	 */
-	request->reserved_space += GUC_REQUEST_SIZE;
+	rq->reserved_space += GUC_REQUEST_SIZE;
  	/*
  	 * Note that after this point, we have committed to using
@@ -483,56 +956,47 @@ static int guc_request_alloc(struct i915_request *request)
  	 */
  	/* Unconditionally invalidate GPU caches and TLBs. */
-	ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
+	ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
  	if (ret)
  		return ret;
-	request->reserved_space -= GUC_REQUEST_SIZE;
-	return 0;
-}
+	rq->reserved_space -= GUC_REQUEST_SIZE;
-static inline void queue_request(struct i915_sched_engine *sched_engine,
-				 struct i915_request *rq,
-				 int prio)
-{
-	GEM_BUG_ON(!list_empty(&rq->sched.link));
-	list_add_tail(&rq->sched.link,
-		      i915_sched_lookup_priolist(sched_engine, prio));
-	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-}
-
-static int guc_bypass_tasklet_submit(struct intel_guc *guc,
-				     struct i915_request *rq)
-{
-	int ret;
-
-	__i915_request_submit(rq);
-
-	trace_i915_request_in(rq, 0);
-
-	guc_set_lrc_tail(rq);
-	ret = guc_add_request(guc, rq);
-	if (ret == -EBUSY)
-		guc->stalled_request = rq;
-
-	return ret;
-}
-
-static void guc_submit_request(struct i915_request *rq)
-{
-	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
-	struct intel_guc *guc = &rq->engine->gt->uc.guc;
-	unsigned long flags;
+	/*
+	 * Call pin_guc_id here rather than in the pinning step as with
+	 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
+	 * guc_ids and creating horrible race conditions. This is especially bad
+	 * when guc_ids are being stolen due to over subscription. By the time
+	 * this function is reached, it is guaranteed that the guc_id will be
+	 * persistent until the generated request is retired. Thus, sealing these
+	 * race conditions. It is still safe to fail here if guc_ids are
+	 * exhausted and return -EAGAIN to the user indicating that they can try
+	 * again in the future.
+	 *
+	 * There is no need for a lock here as the timeline mutex ensures at
+	 * most one context can be executing this code path at once. The
+	 * guc_id_ref is incremented once for every request in flight and

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_request_alloc(struct i915_request *request) · line 983

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -2
+	 * decremented on each retire. When it is zero, a lock around the
+	 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
+	 */
+	if (atomic_add_unless(&ce->guc_id_ref, 1, 0))
+		return 0;
-	/* Will be called from irq-context when using foreign fences. */
-	spin_lock_irqsave(&sched_engine->lock, flags);
+	ret = pin_guc_id(guc, ce);	/* returns 1 if new guc_id assigned */
+	if (unlikely(ret < 0))
+		return ret;;

;;

Quoted 34 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +24 -5
+	if (context_needs_register(ce, !!ret)) {
+		ret = guc_lrc_desc_pin(ce);
+		if (unlikely(ret)) {	/* unwind */
+			atomic_dec(&ce->guc_id_ref);
+			unpin_guc_id(guc, ce);
+			return ret;
+		}
+	}
-	if (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);
+	clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);
-	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	return 0;
  }
  static void sanitize_hwsp(struct intel_engine_cs *engine)
@@ -606,6 +1070,46 @@ static void guc_set_default_submission(struct intel_engine_cs *engine)
  	engine->submit_request = guc_submit_request;
  }
+static inline void guc_kernel_context_pin(struct intel_guc *guc,
+					  struct intel_context *ce)
+{
+	if (context_guc_id_invalid(ce))
+		pin_guc_id(guc, ce);
+	guc_lrc_desc_pin(ce);
+}
+
+static inline void guc_init_lrc_mapping(struct intel_guc *guc)
+{
+	struct intel_gt *gt = guc_to_gt(guc);
+	struct intel_engine_cs *engine;
+	enum intel_engine_id id;
+
Quoted 9 lines

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

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	/* make sure all descriptors are clean... */
+	xa_destroy(&guc->context_lookup);
+
+	/*
+	 * Some contexts might have been pinned before we enabled GuC
+	 * submission, so we need to add them to the GuC bookeeping.
+	 * Also, after a reset the GuC we want to make sure that the information

reset of the GuC

Yep.

Quoted 3 lines

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

Quoted 1 line
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+	 * shared with GuC is properly reset. The kernel lrcs are not attached

LRCs

Yep.

Quoted 4 lines

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

Quoted 3 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+	 * to the gem_context, so they need to be added separately.
+	 *
+	 * Note: we purposely do not check the error return of

check the return of gldp for errors

Yep.

Quoted 11 lines

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

Quoted 3 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+	 * guc_lrc_desc_pin, because that function can only fail in two cases.
+	 * One, if there aren't enough free IDs, but we're guaranteed to have
+	 * enough here (we're either only pinning a handful of lrc on first boot

LRCs and same on line below

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

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+	 * or we're re-pinning lrcs that were already pinned before the reset).
+	 * Two, if the GuC has died and CTBs can't make forward progress.
+	 * Presumably, the GuC should be alive as this function is called on
+	 * driver load or after a reset. Even if it is dead, another full GPU

GPU -> GT

This comment it stale. Need just update the whole thing.

Quoted 25 lines
Quoted 25 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+	 * reset will be triggered and this function would be called again.
+	 */
+
+	for_each_engine(engine, gt, id)
+		if (engine->kernel_context)
+			guc_kernel_context_pin(guc, engine->kernel_context);
+}
+
  static void guc_release(struct intel_engine_cs *engine)
  {
  	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
@@ -718,6 +1222,7 @@ int intel_guc_submission_setup(struct intel_engine_cs *engine)
  void intel_guc_submission_enable(struct intel_guc *guc)
  {
+	guc_init_lrc_mapping(guc);
  }
  void intel_guc_submission_disable(struct intel_guc *guc)
@@ -743,3 +1248,62 @@ void intel_guc_submission_init_early(struct intel_guc *guc)
  {
  	guc->submission_selected = __guc_submission_selected(guc);
  }
+
+static inline struct intel_context *
+g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
+{
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_init_early(struct intel_guc *guc) · line 1259

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+	struct intel_context *ce;
+
+	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Invalid desc_idx %u", desc_idx);

Agree these should not be BUG_ONs or similar, but a drm_err seems appropriate. If either this or the error below occurs the something has gone quite wrong and it should be reported.

Sure.

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +15 -0
+		return NULL;
+	}
+
+	ce = __get_context(guc, desc_idx);
+	if (unlikely(!ce)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Context is NULL, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
+	return ce;
+}
+
+int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
+					  const u32 *msg,
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_init_early(struct intel_guc *guc) · line 1281

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+					  u32 len)
+{
+	struct intel_context *ce;
+	u32 desc_idx = msg[0];
+
+	if (unlikely(len < 1)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);

As above, should be a drm_err I would say.

Sure.

Matt

Quoted 42 lines
Quoted 42 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +28 -0
+		return -EPROTO;
+	}
+
+	ce = g2h_context_lookup(guc, desc_idx);
+	if (unlikely(!ce))
+		return -EPROTO;
+
+	if (context_wait_for_deregister_to_register(ce)) {
+		struct intel_runtime_pm *runtime_pm =
+			&ce->engine->gt->i915->runtime_pm;
+		intel_wakeref_t wakeref;
+
+		/*
+		 * Previous owner of this guc_id has been deregistered, now safe
+		 * register this context.
+		 */
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			register_context(ce);
+		clr_context_wait_for_deregister_to_register(ce);
+		intel_context_put(ce);
+	} else if (context_destroyed(ce)) {
+		/* Context has been destroyed */
+		release_guc_id(guc, ce);
+		lrc_destroy(&ce->ref);
+	}
+
+	return 0;
+}
drivers/gpu/drm/i915/i915_reg.h +1 -0
index 943fe485c662..204c95c39353 100644
@@ -4142,6 +4142,7 @@ enum {
  	FAULT_AND_CONTINUE /* Unsupported */
  };
+#define CTX_GTT_ADDRESS_MASK GENMASK(31, 12)
  #define GEN8_CTX_VALID (1 << 0)
  #define GEN8_CTX_FORCE_PD_RESTORE (1 << 1)
  #define GEN8_CTX_FORCE_RESTORE (1 << 2)
drivers/gpu/drm/i915/i915_request.c +0 -1
index 86b4c9f2613d..b48c4905d3fc 100644
--- a/drivers/gpu/drm/i915/i915_request.c
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c bool i915_request_retire(struct i915_request *rq) · line 412

Quoted 8 lines

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

drivers/gpu/drm/i915/i915_request.c +1 -0
@@ -407,6 +407,7 @@ bool i915_request_retire(struct i915_request *rq)
  	 */
  	if (!list_empty(&rq->sched.link))
  		remove_from_engine(rq);
+	atomic_dec(&rq->context->guc_id_ref);
  	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
  	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */

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

#12 lore raw

Re: [PATCH 06/51] drm/i915/guc: Implement GuC context operations for new inteface

In reply to Matthew Brost
Quoted 137 lines · Matthew Brost

Implement GuC context operations which includes GuC specific operations alloc, pin, unpin, and destroy.

v2:
(Daniel Vetter)
- Use msleep_interruptible rather than cond_resched in busy loop
(Michal)
- Remove C++ style comment

---
  drivers/gpu/drm/i915/gt/intel_context.c       |   5 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  22 +-
  drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |   1 -
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  40 ++
  drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   4 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 666 ++++++++++++++++--
  drivers/gpu/drm/i915/i915_reg.h               |   1 +
  drivers/gpu/drm/i915/i915_request.c           |   1 +
  8 files changed, 685 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +5 -0
index bd63813c8a80..32fd6647154b 100644
@@ -384,6 +384,11 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 
  	mutex_init(&ce->pin_mutex);
 
+	spin_lock_init(&ce->guc_state.lock);
+
+	ce->guc_id = GUC_INVALID_LRC_ID;
+	INIT_LIST_HEAD(&ce->guc_id_link);
+
  	i915_active_init(&ce->active,
  			 __intel_context_active, __intel_context_retire, 0);
  }
drivers/gpu/drm/i915/gt/intel_context_types.h +19 -3
index 6d99631d19b9..606c480aec26 100644
@@ -96,6 +96,7 @@ struct intel_context {
  #define CONTEXT_BANNED			6
  #define CONTEXT_FORCE_SINGLE_SUBMISSION	7
  #define CONTEXT_NOPREEMPT		8
+#define CONTEXT_LRCA_DIRTY		9
 
  	struct {
  		u64 timeout_us;
@@ -138,14 +139,29 @@ struct intel_context {
 
  	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
 
+	struct {
+		/** lock: protects everything in guc_state */
+		spinlock_t lock;
+		/**
+		 * sched_state: scheduling state of this context using GuC
+		 * submission
+		 */
+		u8 sched_state;
+	} guc_state;
+
  	/* GuC scheduling state flags that do not require a lock. */
  	atomic_t guc_sched_state_no_lock;
 
+	/* GuC LRC descriptor ID */
+	u16 guc_id;
+
+	/* GuC LRC descriptor reference count */
+	atomic_t guc_id_ref;
+
  	/*
-	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
-	 * in the series will.
+	 * GuC ID link - in list when unpinned but guc_id still valid in GuC
  	 */
-	u16 guc_id;
+	struct list_head guc_id_link;
  };
 
  #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/intel_lrc_reg.h +0 -1
index 41e5350a7a05..49d4857ad9b7 100644
@@ -87,7 +87,6 @@
  #define GEN11_CSB_WRITE_PTR_MASK	(GEN11_CSB_PTR_MASK << 0)
 
  #define MAX_CONTEXT_HW_ID	(1 << 21) /* exclusive */
-#define MAX_GUC_CONTEXT_HW_ID	(1 << 20) /* exclusive */
  #define GEN11_MAX_CONTEXT_HW_ID	(1 << 11) /* exclusive */
  /* in Gen12 ID 0x7FF is reserved to indicate idle */
  #define GEN12_MAX_CONTEXT_HW_ID	(GEN11_MAX_CONTEXT_HW_ID - 1)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +20 -0
index 8c7b92f699f1..30773cd699f5 100644
@@ -7,6 +7,7 @@
  #define _INTEL_GUC_H_
 
  #include <linux/xarray.h>
+#include <linux/delay.h>
 
  #include "intel_uncore.h"
  #include "intel_guc_fw.h"
@@ -44,6 +45,14 @@ struct intel_guc {
  		void (*disable)(struct intel_guc *guc);
  	} interrupts;
 
+	/*
+	 * contexts_lock protects the pool of free guc ids and a linked list of
+	 * guc ids available to be stolen
+	 */
+	spinlock_t contexts_lock;
+	struct ida guc_ids;
+	struct list_head guc_id_list;
+
  	bool submission_selected;
 
  	struct i915_vma *ads_vma;
@@ -101,6 +110,34 @@ intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
  				 response_buf, response_buf_size, 0);
  }
 
+static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
+					   const u32 *action,
+					   u32 len,
+					   bool loop)
+{
+	int err;
+	unsigned int sleep_period_ms = 1;
+	bool not_atomic = !in_atomic() && !irqs_disabled();
+
+	/* No sleeping with spin locks, just busy loop */
+	might_sleep_if(loop && not_atomic);

drivers/gpu/drm/i915/gt/uc/intel_guc.h intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len, · line 133

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +10 -0
+
+retry:
+	err = intel_guc_send_nb(guc, action, len);
+	if (unlikely(err == -EBUSY && loop)) {
+		if (likely(not_atomic)) {
+			if (msleep_interruptible(sleep_period_ms))
+				return -EINTR;
+			sleep_period_ms = sleep_period_ms << 1;
+		} else {
+			cpu_relax();

Potentially something we can change later, but if we're in atomic context we can't keep looping without a timeout while we get -EBUSY, we need to bail out early.

Quoted 50 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +10 -0
+		}
+		goto retry;
+	}
+
+	return err;
+}
+
  static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
  {
  	intel_guc_ct_event_handler(&guc->ct);
@@ -202,6 +239,9 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
  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);
+
  void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
 
  #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +4 -0
index 83ec60ea3f89..28ff82c5be45 100644
@@ -928,6 +928,10 @@ static int ct_process_request(struct intel_guc_ct *ct, struct ct_incoming_msg *r
  	case INTEL_GUC_ACTION_DEFAULT:
  		ret = intel_guc_to_host_process_recv_msg(guc, payload, len);
  		break;
+	case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
+		ret = intel_guc_deregister_done_process_msg(guc, payload,
+							    len);
+		break;
  	default:
  		ret = -EOPNOTSUPP;
  		break;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -0
index 53b4a5eb4a85..a47b3813b4d0 100644
@@ -13,7 +13,9 @@
  #include "gt/intel_gt.h"
  #include "gt/intel_gt_irq.h"
  #include "gt/intel_gt_pm.h"
+#include "gt/intel_gt_requests.h"
  #include "gt/intel_lrc.h"
+#include "gt/intel_lrc_reg.h"
  #include "gt/intel_mocs.h"
  #include "gt/intel_ring.h"
 

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 95

Quoted 9 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
@@ -85,6 +87,73 @@ static inline void clr_context_enabled(struct intel_context *ce)
  		   &ce->guc_sched_state_no_lock);
  }
 
+/*
+ * Below is a set of functions which control the GuC scheduling state which
+ * require a lock, aside from the special case where the functions are called
+ * from guc_lrc_desc_pin(). In that case it isn't possible for any other code
+ * path to be executing on the context.
+ */

Is there a reason to avoid taking the lock in guc_lrc_desc_pin, even if it isn't strictly needed? I'd prefer to avoid the asymmetry in the locking scheme if possible, as that might case trouble if thing change in the future.

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+#define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER	BIT(0)
+#define SCHED_STATE_DESTROYED				BIT(1)
+static inline void init_sched_state(struct intel_context *ce)
+{

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 109

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	/* Only should be called from guc_lrc_desc_pin() */
+	atomic_set(&ce->guc_sched_state_no_lock, 0);
+	ce->guc_state.sched_state = 0;
+}
+
+static inline bool
+context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state &
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

No need for (). Below as well.

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static inline void
+set_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	/* Only should be called from guc_lrc_desc_pin() */
+	ce->guc_state.sched_state |=

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 126

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
+}
+
+static inline void
+clr_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state =
+		(ce->guc_state.sched_state &
+		 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

nit: can also use

ce->guc_state.sched_state &=
~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER

Quoted 157 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +133 -2
+}
+
+static inline bool
+context_destroyed(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state & SCHED_STATE_DESTROYED);
+}
+
+static inline void
+set_context_destroyed(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
+}
+
+static inline bool context_guc_id_invalid(struct intel_context *ce)
+{
+	return (ce->guc_id == GUC_INVALID_LRC_ID);
+}
+
+static inline void set_context_guc_id_invalid(struct intel_context *ce)
+{
+	ce->guc_id = GUC_INVALID_LRC_ID;
+}
+
+static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
+{
+	return &ce->engine->gt->uc.guc;
+}
+
  static inline struct i915_priolist *to_priolist(struct rb_node *rb)
  {
  	return rb_entry(rb, struct i915_priolist, node);
@@ -155,6 +224,9 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	int len = 0;
  	bool enabled = context_enabled(ce);
 
+	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
+	GEM_BUG_ON(context_guc_id_invalid(ce));
+
  	if (!enabled) {
  		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
  		action[len++] = ce->guc_id;
@@ -417,6 +489,10 @@ int intel_guc_submission_init(struct intel_guc *guc)
 
  	xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
 
+	spin_lock_init(&guc->contexts_lock);
+	INIT_LIST_HEAD(&guc->guc_id_list);
+	ida_init(&guc->guc_ids);
+
  	return 0;
  }
 
@@ -429,9 +505,303 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  	i915_sched_engine_put(guc->sched_engine);
  }
 
-static int guc_context_alloc(struct intel_context *ce)
+static inline void queue_request(struct i915_sched_engine *sched_engine,
+				 struct i915_request *rq,
+				 int prio)
  {
-	return lrc_alloc(ce, ce->engine);
+	GEM_BUG_ON(!list_empty(&rq->sched.link));
+	list_add_tail(&rq->sched.link,
+		      i915_sched_lookup_priolist(sched_engine, prio));
+	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+}
+
+static int guc_bypass_tasklet_submit(struct intel_guc *guc,
+				     struct i915_request *rq)
+{
+	int ret;
+
+	__i915_request_submit(rq);
+
+	trace_i915_request_in(rq, 0);
+
+	guc_set_lrc_tail(rq);
+	ret = guc_add_request(guc, rq);
+	if (ret == -EBUSY)
+		guc->stalled_request = rq;
+
+	return ret;
+}
+
+static void guc_submit_request(struct i915_request *rq)
+{
+	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
+	struct intel_guc *guc = &rq->engine->gt->uc.guc;
+	unsigned long flags;
+
+	/* 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))
+		queue_request(sched_engine, rq, rq_prio(rq));
+	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
+		tasklet_hi_schedule(&sched_engine->tasklet);
+
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+#define GUC_ID_START	64	/* First 64 guc_ids reserved */
+static int new_guc_id(struct intel_guc *guc)
+{
+	return ida_simple_get(&guc->guc_ids, GUC_ID_START,
+			      GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+}
+
+static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	if (!context_guc_id_invalid(ce)) {
+		ida_simple_remove(&guc->guc_ids, ce->guc_id);
+		reset_lrc_desc(guc, ce->guc_id);
+		set_context_guc_id_invalid(ce);
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+}
+
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	__release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int steal_guc_id(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	int guc_id;
+
+	if (!list_empty(&guc->guc_id_list)) {
+		ce = list_first_entry(&guc->guc_id_list,
+				      struct intel_context,
+				      guc_id_link);
+
+		GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+		GEM_BUG_ON(context_guc_id_invalid(ce));
+
+		list_del_init(&ce->guc_id_link);
+		guc_id = ce->guc_id;
+		set_context_guc_id_invalid(ce);
+		return guc_id;
+	} else {
+		return -EAGAIN;
+	}
+}
+
+static int assign_guc_id(struct intel_guc *guc, u16 *out)
+{
+	int ret;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 614

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+
+	ret = new_guc_id(guc);
+	if (unlikely(ret < 0)) {
+		ret = steal_guc_id(guc);
+		if (ret < 0)
+			return ret;
+	}
+
+	*out = ret;
+	return 0;

Is it worth adding spinlock_held asserts for guc->contexts_lock in these ID functions? Doubles up as a documentation of what locking we expect.

Quoted 107 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +107 -0
+}
+
+#define PIN_GUC_ID_TRIES	4
+static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	int ret = 0;
+	unsigned long flags, tries = PIN_GUC_ID_TRIES;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+
+try_again:
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+
+	if (context_guc_id_invalid(ce)) {
+		ret = assign_guc_id(guc, &ce->guc_id);
+		if (ret)
+			goto out_unlock;
+		ret = 1;	/* Indidcates newly assigned guc_id */
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	atomic_inc(&ce->guc_id_ref);
+
+out_unlock:
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * -EAGAIN indicates no guc_ids are available, let's retire any
+	 * outstanding requests to see if that frees up a guc_id. If the first
+	 * retire didn't help, insert a sleep with the timeslice duration before
+	 * attempting to retire more requests. Double the sleep period each
+	 * subsequent pass before finally giving up. The sleep period has max of
+	 * 100ms and minimum of 1ms.
+	 */
+	if (ret == -EAGAIN && --tries) {
+		if (PIN_GUC_ID_TRIES - tries > 1) {
+			unsigned int timeslice_shifted =
+				ce->engine->props.timeslice_duration_ms <<
+				(PIN_GUC_ID_TRIES - tries - 2);
+			unsigned int max = min_t(unsigned int, 100,
+						 timeslice_shifted);
+
+			msleep(max_t(unsigned int, max, 1));
+		}
+		intel_gt_retire_requests(guc_to_gt(guc));
+		goto try_again;
+	}
+
+	return ret;
+}
+
+static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref) < 0);
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id_link) &&
+	    !atomic_read(&ce->guc_id_ref))
+		list_add_tail(&ce->guc_id_link, &guc->guc_id_list);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int __guc_action_register_context(struct intel_guc *guc,
+					 u32 guc_id,
+					 u32 offset)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_REGISTER_CONTEXT,
+		guc_id,
+		offset,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int register_context(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
+		ce->guc_id * sizeof(struct guc_lrc_desc);
+
+	return __guc_action_register_context(guc, ce->guc_id, offset);
+}
+
+static int __guc_action_deregister_context(struct intel_guc *guc,
+					   u32 guc_id)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
+		guc_id,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int deregister_context(struct intel_context *ce, u32 guc_id)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	return __guc_action_deregister_context(guc, guc_id);
+}
+
+static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
+{
+	switch (class) {

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 731

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	case RENDER_CLASS:
+		return mask >> RCS0;
+	case VIDEO_ENHANCEMENT_CLASS:
+		return mask >> VECS0;
+	case VIDEO_DECODE_CLASS:
+		return mask >> VCS0;
+	case COPY_ENGINE_CLASS:
+		return mask >> BCS0;
+	default:
+		GEM_BUG_ON("Invalid Class");

we usually use MISSING_CASE for this type of errors.

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+		return 0;
+	}
+}
+
+static void guc_context_policy_init(struct intel_engine_cs *engine,
+				    struct guc_lrc_desc *desc)
+{

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 748

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	desc->policy_flags = 0;
+
+	desc->execution_quantum = CONTEXT_POLICY_DEFAULT_EXECUTION_QUANTUM_US;
+	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
+}
+
+static int guc_lrc_desc_pin(struct intel_context *ce)
+{
+	struct intel_runtime_pm *runtime_pm =
+		&ce->engine->gt->i915->runtime_pm;

If you move this after the engine var below you can skip the ce->engine jump. Also, you can shorten the pointer chasing by using engine->uncore->rpm.

Quoted 26 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +26 -0
+	struct intel_engine_cs *engine = ce->engine;
+	struct intel_guc *guc = &engine->gt->uc.guc;
+	u32 desc_idx = ce->guc_id;
+	struct guc_lrc_desc *desc;
+	bool context_registered;
+	intel_wakeref_t wakeref;
+	int ret = 0;
+
+	GEM_BUG_ON(!engine->mask);
+
+	/*
+	 * Ensure LRC + CT vmas are is same region as write barrier is done
+	 * based on CT vma region.
+	 */
+	GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
+		   i915_gem_object_is_lmem(ce->ring->vma->obj));
+
+	context_registered = lrc_desc_registered(guc, desc_idx);
+
+	reset_lrc_desc(guc, desc_idx);
+	set_lrc_desc_registered(guc, desc_idx, ce);
+
+	desc = __get_lrc_desc(guc, desc_idx);
+	desc->engine_class = engine_class_to_guc_class(engine->class);
+	desc->engine_submit_mask = adjust_engine_mask(engine->class,
+						      engine->mask);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 784

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	desc->hw_context_desc = ce->lrc.lrca;
+	desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+	guc_context_policy_init(engine, desc);
+	init_sched_state(ce);
+
+	/*
+	 * The context_lookup xarray is used to determine if the hardware
+	 * context is currently registered. There are two cases in which it
+	 * could be regisgered either the guc_id has been stole from from

typo regisgered

Quoted 24 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+	 * another context or the lrc descriptor address of this context has
+	 * changed. In either case the context needs to be deregistered with the
+	 * GuC before registering this context.
+	 */
+	if (context_registered) {
+		set_context_wait_for_deregister_to_register(ce);
+		intel_context_get(ce);
+
+		/*
+		 * 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);
+	} else {
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			ret = register_context(ce);
+	}
+
+	return ret;
  }
 
  static int guc_context_pre_pin(struct intel_context *ce,

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 818

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
@@ -443,36 +813,139 @@ static int guc_context_pre_pin(struct intel_context *ce,
 
  static int guc_context_pin(struct intel_context *ce, void *vaddr)
  {
+	if (i915_ggtt_offset(ce->state) !=
+	    (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
+		set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Shouldn't this be set after lrc_pin()? I can see ce->lrc.lrca being re-assigned in there; it looks like it can only change if the context is new, but for future-proofing IMO better to re-order here.

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 820

Quoted 2 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+
  	return lrc_pin(ce, ce->engine, vaddr);

Could use a comment to say that the GuC context pinning call is delayed until request alloc and to look at the comment in the latter function for details (or move the explanation here and refer here from request alloc).

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 833

Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -0
  }
 
+static void guc_context_unpin(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+}
+
+static void guc_context_post_unpin(struct intel_context *ce)
+{
+	lrc_post_unpin(ce);

why do we need this function? we can just pass lrc_post_unpin to the ops (like we already do).

Quoted 12 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +12 -0
+}
+
+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;
+
+	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
+	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
+
+	spin_lock_irqsave(&ce->guc_state.lock, flags);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 855

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	set_context_destroyed(ce);
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+
+	deregister_context(ce, ce->guc_id);
+}
+
+static void guc_context_destroy(struct kref *kref)
+{
+	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
+	struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;

same as above, going through engine->uncore->rpm is shorter

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 867

Quoted 12 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +12 -0
+	struct intel_guc *guc = &ce->engine->gt->uc.guc;
+	intel_wakeref_t wakeref;
+	unsigned long flags;
+
+	/*
+	 * 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) ||
+	    !lrc_desc_registered(guc, ce->guc_id)) {
+		release_guc_id(guc, ce);

it feels a bit weird that we call release_guc_id in the case where the id is invalid. The code handles it fine, but still the flow doesn't feel clean. Not a blocker.

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+		lrc_destroy(kref);
+		return;
+	}
+
+	/*

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 882

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	 * We have to acquire the context spinlock and check guc_id again, if it
+	 * is valid it hasn't been stolen and needs to be deregistered. We
+	 * delete this context from the list of unpinned guc_ids available to
+	 * stole to seal a race with guc_lrc_desc_pin(). When the G2H CTB
+	 * returns indicating this context has been deregistered the guc_id is
+	 * returned to the pool of available guc_ids.
+	 */
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (context_guc_id_invalid(ce)) {
+		__release_guc_id(guc, ce);

But here the call to __release_guc_id is unneded, right? the ce doesn't own the ID anymore and both the steal and the release function already clean ce->guc_id_link, so there should be nothing left to clean.

Quoted 136 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +62 -42
+		spin_unlock_irqrestore(&guc->contexts_lock, flags);
+		lrc_destroy(kref);
+		return;
+	}
+
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * We defer GuC context deregistration until the context is destroyed
+	 * in order to save on CTBs. With this optimization ideally we only need
+	 * 1 CTB to register the context during the first pin and 1 CTB to
+	 * deregister the context when the context is destroyed. Without this
+	 * optimization, a CTB would be needed every pin & unpin.
+	 *
+	 * XXX: Need to acqiure the runtime wakeref as this can be triggered
+	 * from context_free_worker when not runtime wakeref is held.
+	 * guc_lrc_desc_unpin requires the runtime as a GuC register is written
+	 * in H2G CTB to deregister the context. A future patch may defer this
+	 * H2G CTB if the runtime wakeref is zero.
+	 */
+	with_intel_runtime_pm(runtime_pm, wakeref)
+		guc_lrc_desc_unpin(ce);
+}
+
+static int guc_context_alloc(struct intel_context *ce)
+{
+	return lrc_alloc(ce, ce->engine);
+}
+
  static const struct intel_context_ops guc_context_ops = {
  	.alloc = guc_context_alloc,
 
  	.pre_pin = guc_context_pre_pin,
  	.pin = guc_context_pin,
-	.unpin = lrc_unpin,
-	.post_unpin = lrc_post_unpin,
+	.unpin = guc_context_unpin,
+	.post_unpin = guc_context_post_unpin,
 
  	.enter = intel_context_enter_engine,
  	.exit = intel_context_exit_engine,
 
  	.reset = lrc_reset,
-	.destroy = lrc_destroy,
+	.destroy = guc_context_destroy,
  };
 
-static int guc_request_alloc(struct i915_request *request)
+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);
+}
+
+static int guc_request_alloc(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+	struct intel_guc *guc = ce_to_guc(ce);
  	int ret;
 
-	GEM_BUG_ON(!intel_context_is_pinned(request->context));
+	GEM_BUG_ON(!intel_context_is_pinned(rq->context));
 
  	/*
  	 * Flush enough space to reduce the likelihood of waiting after
  	 * we start building the request - in which case we will just
  	 * have to repeat work.
  	 */
-	request->reserved_space += GUC_REQUEST_SIZE;
+	rq->reserved_space += GUC_REQUEST_SIZE;
 
  	/*
  	 * Note that after this point, we have committed to using
@@ -483,56 +956,47 @@ static int guc_request_alloc(struct i915_request *request)
  	 */
 
  	/* Unconditionally invalidate GPU caches and TLBs. */
-	ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
+	ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
  	if (ret)
  		return ret;
 
-	request->reserved_space -= GUC_REQUEST_SIZE;
-	return 0;
-}
+	rq->reserved_space -= GUC_REQUEST_SIZE;
 
-static inline void queue_request(struct i915_sched_engine *sched_engine,
-				 struct i915_request *rq,
-				 int prio)
-{
-	GEM_BUG_ON(!list_empty(&rq->sched.link));
-	list_add_tail(&rq->sched.link,
-		      i915_sched_lookup_priolist(sched_engine, prio));
-	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-}
-
-static int guc_bypass_tasklet_submit(struct intel_guc *guc,
-				     struct i915_request *rq)
-{
-	int ret;
-
-	__i915_request_submit(rq);
-
-	trace_i915_request_in(rq, 0);
-
-	guc_set_lrc_tail(rq);
-	ret = guc_add_request(guc, rq);
-	if (ret == -EBUSY)
-		guc->stalled_request = rq;
-
-	return ret;
-}
-
-static void guc_submit_request(struct i915_request *rq)
-{
-	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
-	struct intel_guc *guc = &rq->engine->gt->uc.guc;
-	unsigned long flags;
+	/*
+	 * Call pin_guc_id here rather than in the pinning step as with
+	 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
+	 * guc_ids and creating horrible race conditions. This is especially bad
+	 * when guc_ids are being stolen due to over subscription. By the time
+	 * this function is reached, it is guaranteed that the guc_id will be
+	 * persistent until the generated request is retired. Thus, sealing these
+	 * race conditions. It is still safe to fail here if guc_ids are
+	 * exhausted and return -EAGAIN to the user indicating that they can try
+	 * again in the future.
+	 *
+	 * There is no need for a lock here as the timeline mutex ensures at
+	 * most one context can be executing this code path at once. The
+	 * guc_id_ref is incremented once for every request in flight and
+	 * decremented on each retire. When it is zero, a lock around the

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_request_alloc(struct i915_request *request) · line 987

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -2
+	 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
+	 */
+	if (atomic_add_unless(&ce->guc_id_ref, 1, 0))
+		return 0;
 
-	/* Will be called from irq-context when using foreign fences. */
-	spin_lock_irqsave(&sched_engine->lock, flags);
+	ret = pin_guc_id(guc, ce);	/* returns 1 if new guc_id assigned */
+	if (unlikely(ret < 0))
+		return ret;;

typo ";;"

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+	if (context_needs_register(ce, !!ret)) {
+		ret = guc_lrc_desc_pin(ce);
+		if (unlikely(ret)) {	/* unwind */
+			atomic_dec(&ce->guc_id_ref);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_request_alloc(struct i915_request *request) · line 997

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -4
+			unpin_guc_id(guc, ce);
+			return ret;
+		}
+	}
 
-	if (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);
+	clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Might be worth moving the pinning to a separate function to keep the request_alloc focused on the request. Can be done as a follow up.

Quoted 141 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +101 -1
-	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	return 0;
  }
 
  static void sanitize_hwsp(struct intel_engine_cs *engine)
@@ -606,6 +1070,46 @@ static void guc_set_default_submission(struct intel_engine_cs *engine)
  	engine->submit_request = guc_submit_request;
  }
 
+static inline void guc_kernel_context_pin(struct intel_guc *guc,
+					  struct intel_context *ce)
+{
+	if (context_guc_id_invalid(ce))
+		pin_guc_id(guc, ce);
+	guc_lrc_desc_pin(ce);
+}
+
+static inline void guc_init_lrc_mapping(struct intel_guc *guc)
+{
+	struct intel_gt *gt = guc_to_gt(guc);
+	struct intel_engine_cs *engine;
+	enum intel_engine_id id;
+
+	/* make sure all descriptors are clean... */
+	xa_destroy(&guc->context_lookup);
+
+	/*
+	 * Some contexts might have been pinned before we enabled GuC
+	 * submission, so we need to add them to the GuC bookeeping.
+	 * Also, after a reset the GuC we want to make sure that the information
+	 * shared with GuC is properly reset. The kernel lrcs are not attached
+	 * to the gem_context, so they need to be added separately.
+	 *
+	 * Note: we purposely do not check the error return of
+	 * guc_lrc_desc_pin, because that function can only fail in two cases.
+	 * One, if there aren't enough free IDs, but we're guaranteed to have
+	 * enough here (we're either only pinning a handful of lrc on first boot
+	 * or we're re-pinning lrcs that were already pinned before the reset).
+	 * Two, if the GuC has died and CTBs can't make forward progress.
+	 * Presumably, the GuC should be alive as this function is called on
+	 * driver load or after a reset. Even if it is dead, another full GPU
+	 * reset will be triggered and this function would be called again.
+	 */
+
+	for_each_engine(engine, gt, id)
+		if (engine->kernel_context)
+			guc_kernel_context_pin(guc, engine->kernel_context);
+}
+
  static void guc_release(struct intel_engine_cs *engine)
  {
  	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
@@ -718,6 +1222,7 @@ int intel_guc_submission_setup(struct intel_engine_cs *engine)
 
  void intel_guc_submission_enable(struct intel_guc *guc)
  {
+	guc_init_lrc_mapping(guc);
  }
 
  void intel_guc_submission_disable(struct intel_guc *guc)
@@ -743,3 +1248,62 @@ void intel_guc_submission_init_early(struct intel_guc *guc)
  {
  	guc->submission_selected = __guc_submission_selected(guc);
  }
+
+static inline struct intel_context *
+g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
+{
+	struct intel_context *ce;
+
+	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Invalid desc_idx %u", desc_idx);
+		return NULL;
+	}
+
+	ce = __get_context(guc, desc_idx);
+	if (unlikely(!ce)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Context is NULL, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
+	return ce;
+}
+
+int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
+					  const u32 *msg,
+					  u32 len)
+{
+	struct intel_context *ce;
+	u32 desc_idx = msg[0];
+
+	if (unlikely(len < 1)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
+		return -EPROTO;
+	}
+
+	ce = g2h_context_lookup(guc, desc_idx);
+	if (unlikely(!ce))
+		return -EPROTO;
+
+	if (context_wait_for_deregister_to_register(ce)) {
+		struct intel_runtime_pm *runtime_pm =
+			&ce->engine->gt->i915->runtime_pm;
+		intel_wakeref_t wakeref;
+
+		/*
+		 * Previous owner of this guc_id has been deregistered, now safe
+		 * register this context.
+		 */
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			register_context(ce);
+		clr_context_wait_for_deregister_to_register(ce);
+		intel_context_put(ce);
+	} else if (context_destroyed(ce)) {
+		/* Context has been destroyed */
+		release_guc_id(guc, ce);
+		lrc_destroy(&ce->ref);
+	}
+
+	return 0;
+}
drivers/gpu/drm/i915/i915_reg.h +1 -0
index 943fe485c662..204c95c39353 100644
@@ -4142,6 +4142,7 @@ enum {
  	FAULT_AND_CONTINUE /* Unsupported */
  };
 
+#define CTX_GTT_ADDRESS_MASK GENMASK(31, 12)
  #define GEN8_CTX_VALID (1 << 0)
  #define GEN8_CTX_FORCE_PD_RESTORE (1 << 1)
  #define GEN8_CTX_FORCE_RESTORE (1 << 2)
drivers/gpu/drm/i915/i915_request.c +0 -0
index 86b4c9f2613d..b48c4905d3fc 100644

drivers/gpu/drm/i915/i915_request.c bool i915_request_retire(struct i915_request *rq) · line 410

Quoted 4 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
@@ -407,6 +407,7 @@ bool i915_request_retire(struct i915_request *rq)
  	 */
  	if (!list_empty(&rq->sched.link))
  		remove_from_engine(rq);
+	atomic_dec(&rq->context->guc_id_ref);

Does this work/make sense  if GuC is disabled?

Daniele

Quoted 3 lines
drivers/gpu/drm/i915/i915_request.c +0 -0
  	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
 
  	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
#13 lore raw

Re: [PATCH 06/51] drm/i915/guc: Implement GuC context operations for new inteface

Quoted 133 lines · Daniele Ceraolo Spurio
Quoted 130 lines · Matthew Brost

Implement GuC context operations which includes GuC specific operations alloc, pin, unpin, and destroy.

v2:
(Daniel Vetter)
- Use msleep_interruptible rather than cond_resched in busy loop
(Michal)
- Remove C++ style comment

---
  drivers/gpu/drm/i915/gt/intel_context.c       |   5 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  22 +-
  drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |   1 -
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  40 ++
  drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   4 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 666 ++++++++++++++++--
  drivers/gpu/drm/i915/i915_reg.h               |   1 +
  drivers/gpu/drm/i915/i915_request.c           |   1 +
  8 files changed, 685 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +5 -0
index bd63813c8a80..32fd6647154b 100644
@@ -384,6 +384,11 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	mutex_init(&ce->pin_mutex);
+	spin_lock_init(&ce->guc_state.lock);
+
+	ce->guc_id = GUC_INVALID_LRC_ID;
+	INIT_LIST_HEAD(&ce->guc_id_link);
+
  	i915_active_init(&ce->active,
  			 __intel_context_active, __intel_context_retire, 0);
  }
drivers/gpu/drm/i915/gt/intel_context_types.h +19 -3
index 6d99631d19b9..606c480aec26 100644
@@ -96,6 +96,7 @@ struct intel_context {
  #define CONTEXT_BANNED			6
  #define CONTEXT_FORCE_SINGLE_SUBMISSION	7
  #define CONTEXT_NOPREEMPT		8
+#define CONTEXT_LRCA_DIRTY		9
  	struct {
  		u64 timeout_us;
@@ -138,14 +139,29 @@ struct intel_context {
  	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
+	struct {
+		/** lock: protects everything in guc_state */
+		spinlock_t lock;
+		/**
+		 * sched_state: scheduling state of this context using GuC
+		 * submission
+		 */
+		u8 sched_state;
+	} guc_state;
+
  	/* GuC scheduling state flags that do not require a lock. */
  	atomic_t guc_sched_state_no_lock;
+	/* GuC LRC descriptor ID */
+	u16 guc_id;
+
+	/* GuC LRC descriptor reference count */
+	atomic_t guc_id_ref;
+
  	/*
-	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
-	 * in the series will.
+	 * GuC ID link - in list when unpinned but guc_id still valid in GuC
  	 */
-	u16 guc_id;
+	struct list_head guc_id_link;
  };
  #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/intel_lrc_reg.h +0 -1
index 41e5350a7a05..49d4857ad9b7 100644
@@ -87,7 +87,6 @@
  #define GEN11_CSB_WRITE_PTR_MASK	(GEN11_CSB_PTR_MASK << 0)
  #define MAX_CONTEXT_HW_ID	(1 << 21) /* exclusive */
-#define MAX_GUC_CONTEXT_HW_ID	(1 << 20) /* exclusive */
  #define GEN11_MAX_CONTEXT_HW_ID	(1 << 11) /* exclusive */
  /* in Gen12 ID 0x7FF is reserved to indicate idle */
  #define GEN12_MAX_CONTEXT_HW_ID	(GEN11_MAX_CONTEXT_HW_ID - 1)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +26 -0
index 8c7b92f699f1..30773cd699f5 100644
@@ -7,6 +7,7 @@
  #define _INTEL_GUC_H_
  #include <linux/xarray.h>
+#include <linux/delay.h>
  #include "intel_uncore.h"
  #include "intel_guc_fw.h"
@@ -44,6 +45,14 @@ struct intel_guc {
  		void (*disable)(struct intel_guc *guc);
  	} interrupts;
+	/*
+	 * contexts_lock protects the pool of free guc ids and a linked list of
+	 * guc ids available to be stolen
+	 */
+	spinlock_t contexts_lock;
+	struct ida guc_ids;
+	struct list_head guc_id_list;
+
  	bool submission_selected;
  	struct i915_vma *ads_vma;
@@ -101,6 +110,34 @@ intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
  				 response_buf, response_buf_size, 0);
  }
+static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
+					   const u32 *action,
+					   u32 len,
+					   bool loop)
+{
+	int err;
+	unsigned int sleep_period_ms = 1;
+	bool not_atomic = !in_atomic() && !irqs_disabled();
+
+	/* No sleeping with spin locks, just busy loop */
+	might_sleep_if(loop && not_atomic);
+
+retry:
+	err = intel_guc_send_nb(guc, action, len);
+	if (unlikely(err == -EBUSY && loop)) {
+		if (likely(not_atomic)) {
+			if (msleep_interruptible(sleep_period_ms))
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc.h intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len, · line 133

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
+				return -EINTR;
+			sleep_period_ms = sleep_period_ms << 1;
+		} else {
+			cpu_relax();

Potentially something we can change later, but if we're in atomic context we can't keep looping without a timeout while we get -EBUSY, we need to bail out early.

Eventually intel_guc_send_nb will pop with a different return code after 1 second I think if the GuC is hung.

Quoted 50 lines
Quoted 50 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +10 -0
+		}
+		goto retry;
+	}
+
+	return err;
+}
+
  static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
  {
  	intel_guc_ct_event_handler(&guc->ct);
@@ -202,6 +239,9 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
  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);
+
  void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
  #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +4 -0
index 83ec60ea3f89..28ff82c5be45 100644
@@ -928,6 +928,10 @@ static int ct_process_request(struct intel_guc_ct *ct, struct ct_incoming_msg *r
  	case INTEL_GUC_ACTION_DEFAULT:
  		ret = intel_guc_to_host_process_recv_msg(guc, payload, len);
  		break;
+	case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
+		ret = intel_guc_deregister_done_process_msg(guc, payload,
+							    len);
+		break;
  	default:
  		ret = -EOPNOTSUPP;
  		break;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index 53b4a5eb4a85..a47b3813b4d0 100644
@@ -13,7 +13,9 @@
  #include "gt/intel_gt.h"
  #include "gt/intel_gt_irq.h"
  #include "gt/intel_gt_pm.h"
+#include "gt/intel_gt_requests.h"
  #include "gt/intel_lrc.h"
+#include "gt/intel_lrc_reg.h"
  #include "gt/intel_mocs.h"
  #include "gt/intel_ring.h"
@@ -85,6 +87,73 @@ static inline void clr_context_enabled(struct intel_context *ce)
  		   &ce->guc_sched_state_no_lock);
  }
+/*
Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 95

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+ * Below is a set of functions which control the GuC scheduling state which
+ * require a lock, aside from the special case where the functions are called
+ * from guc_lrc_desc_pin(). In that case it isn't possible for any other code
+ * path to be executing on the context.
+ */

Is there a reason to avoid taking the lock in guc_lrc_desc_pin, even if it isn't strictly needed? I'd prefer to avoid the asymmetry in the locking

If this code is called from releasing a fence a circular dependency is created. Once we move the DRM scheduler the locking becomes way easier and we clean this up then. We already have task for locking clean up.

Quoted 2 lines

scheme if possible, as that might case trouble if thing change in the future.

Again this is temporary code, so I think we can live with this until the DRM scheduler lands.

Quoted 18 lines
Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+#define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER	BIT(0)
+#define SCHED_STATE_DESTROYED				BIT(1)
+static inline void init_sched_state(struct intel_context *ce)
+{

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 109

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	/* Only should be called from guc_lrc_desc_pin() */
+	atomic_set(&ce->guc_sched_state_no_lock, 0);
+	ce->guc_state.sched_state = 0;
+}
+
+static inline bool
+context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state &
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

No need for (). Below as well.

Yep.

Quoted 20 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static inline void
+set_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	/* Only should be called from guc_lrc_desc_pin() */
+	ce->guc_state.sched_state |=

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 126

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
+}
+
+static inline void
+clr_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state =
+		(ce->guc_state.sched_state &
+		 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

nit: can also use

Quoted 3 lines

ce->guc_state.sched_state &=
~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER

Yep.

Quoted 158 lines
Quoted 158 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +138 -2
+}
+
+static inline bool
+context_destroyed(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state & SCHED_STATE_DESTROYED);
+}
+
+static inline void
+set_context_destroyed(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
+}
+
+static inline bool context_guc_id_invalid(struct intel_context *ce)
+{
+	return (ce->guc_id == GUC_INVALID_LRC_ID);
+}
+
+static inline void set_context_guc_id_invalid(struct intel_context *ce)
+{
+	ce->guc_id = GUC_INVALID_LRC_ID;
+}
+
+static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
+{
+	return &ce->engine->gt->uc.guc;
+}
+
  static inline struct i915_priolist *to_priolist(struct rb_node *rb)
  {
  	return rb_entry(rb, struct i915_priolist, node);
@@ -155,6 +224,9 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  	int len = 0;
  	bool enabled = context_enabled(ce);
+	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
+	GEM_BUG_ON(context_guc_id_invalid(ce));
+
  	if (!enabled) {
  		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
  		action[len++] = ce->guc_id;
@@ -417,6 +489,10 @@ int intel_guc_submission_init(struct intel_guc *guc)
  	xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
+	spin_lock_init(&guc->contexts_lock);
+	INIT_LIST_HEAD(&guc->guc_id_list);
+	ida_init(&guc->guc_ids);
+
  	return 0;
  }
@@ -429,9 +505,303 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  	i915_sched_engine_put(guc->sched_engine);
  }
-static int guc_context_alloc(struct intel_context *ce)
+static inline void queue_request(struct i915_sched_engine *sched_engine,
+				 struct i915_request *rq,
+				 int prio)
  {
-	return lrc_alloc(ce, ce->engine);
+	GEM_BUG_ON(!list_empty(&rq->sched.link));
+	list_add_tail(&rq->sched.link,
+		      i915_sched_lookup_priolist(sched_engine, prio));
+	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+}
+
+static int guc_bypass_tasklet_submit(struct intel_guc *guc,
+				     struct i915_request *rq)
+{
+	int ret;
+
+	__i915_request_submit(rq);
+
+	trace_i915_request_in(rq, 0);
+
+	guc_set_lrc_tail(rq);
+	ret = guc_add_request(guc, rq);
+	if (ret == -EBUSY)
+		guc->stalled_request = rq;
+
+	return ret;
+}
+
+static void guc_submit_request(struct i915_request *rq)
+{
+	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
+	struct intel_guc *guc = &rq->engine->gt->uc.guc;
+	unsigned long flags;
+
+	/* 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))
+		queue_request(sched_engine, rq, rq_prio(rq));
+	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
+		tasklet_hi_schedule(&sched_engine->tasklet);
+
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+#define GUC_ID_START	64	/* First 64 guc_ids reserved */
+static int new_guc_id(struct intel_guc *guc)
+{
+	return ida_simple_get(&guc->guc_ids, GUC_ID_START,
+			      GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+}
+
+static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	if (!context_guc_id_invalid(ce)) {
+		ida_simple_remove(&guc->guc_ids, ce->guc_id);
+		reset_lrc_desc(guc, ce->guc_id);
+		set_context_guc_id_invalid(ce);
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+}
+
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	__release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int steal_guc_id(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	int guc_id;
+
+	if (!list_empty(&guc->guc_id_list)) {
+		ce = list_first_entry(&guc->guc_id_list,
+				      struct intel_context,
+				      guc_id_link);
+
+		GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+		GEM_BUG_ON(context_guc_id_invalid(ce));
+
+		list_del_init(&ce->guc_id_link);
+		guc_id = ce->guc_id;
+		set_context_guc_id_invalid(ce);
+		return guc_id;
+	} else {
+		return -EAGAIN;
+	}
+}
+
+static int assign_guc_id(struct intel_guc *guc, u16 *out)
+{
+	int ret;
+
+	ret = new_guc_id(guc);
+	if (unlikely(ret < 0)) {
+		ret = steal_guc_id(guc);
+		if (ret < 0)
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 614

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+			return ret;
+	}
+
+	*out = ret;
+	return 0;

Is it worth adding spinlock_held asserts for guc->contexts_lock in these ID functions? Doubles up as a documentation of what locking we expect.

Never a bad idea to have asserts in the code. Will add.

Quoted 111 lines
Quoted 111 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +111 -0
+}
+
+#define PIN_GUC_ID_TRIES	4
+static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	int ret = 0;
+	unsigned long flags, tries = PIN_GUC_ID_TRIES;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+
+try_again:
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+
+	if (context_guc_id_invalid(ce)) {
+		ret = assign_guc_id(guc, &ce->guc_id);
+		if (ret)
+			goto out_unlock;
+		ret = 1;	/* Indidcates newly assigned guc_id */
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	atomic_inc(&ce->guc_id_ref);
+
+out_unlock:
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * -EAGAIN indicates no guc_ids are available, let's retire any
+	 * outstanding requests to see if that frees up a guc_id. If the first
+	 * retire didn't help, insert a sleep with the timeslice duration before
+	 * attempting to retire more requests. Double the sleep period each
+	 * subsequent pass before finally giving up. The sleep period has max of
+	 * 100ms and minimum of 1ms.
+	 */
+	if (ret == -EAGAIN && --tries) {
+		if (PIN_GUC_ID_TRIES - tries > 1) {
+			unsigned int timeslice_shifted =
+				ce->engine->props.timeslice_duration_ms <<
+				(PIN_GUC_ID_TRIES - tries - 2);
+			unsigned int max = min_t(unsigned int, 100,
+						 timeslice_shifted);
+
+			msleep(max_t(unsigned int, max, 1));
+		}
+		intel_gt_retire_requests(guc_to_gt(guc));
+		goto try_again;
+	}
+
+	return ret;
+}
+
+static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref) < 0);
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id_link) &&
+	    !atomic_read(&ce->guc_id_ref))
+		list_add_tail(&ce->guc_id_link, &guc->guc_id_list);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int __guc_action_register_context(struct intel_guc *guc,
+					 u32 guc_id,
+					 u32 offset)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_REGISTER_CONTEXT,
+		guc_id,
+		offset,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int register_context(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
+		ce->guc_id * sizeof(struct guc_lrc_desc);
+
+	return __guc_action_register_context(guc, ce->guc_id, offset);
+}
+
+static int __guc_action_deregister_context(struct intel_guc *guc,
+					   u32 guc_id)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
+		guc_id,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int deregister_context(struct intel_context *ce, u32 guc_id)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	return __guc_action_deregister_context(guc, guc_id);
+}
+
+static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
+{
+	switch (class) {
+	case RENDER_CLASS:
+		return mask >> RCS0;
+	case VIDEO_ENHANCEMENT_CLASS:
+		return mask >> VECS0;
Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 731

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+	case VIDEO_DECODE_CLASS:
+		return mask >> VCS0;
+	case COPY_ENGINE_CLASS:
+		return mask >> BCS0;
+	default:
+		GEM_BUG_ON("Invalid Class");

we usually use MISSING_CASE for this type of errors.

As soon as start talking in logical space (series immediatly after this gets merged) this function gets deleted but will fix.

Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+		return 0;
+	}
+}
+
+static void guc_context_policy_init(struct intel_engine_cs *engine,
+				    struct guc_lrc_desc *desc)
+{
+	desc->policy_flags = 0;
+
+	desc->execution_quantum = CONTEXT_POLICY_DEFAULT_EXECUTION_QUANTUM_US;
+	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
+}
+
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 748

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+static int guc_lrc_desc_pin(struct intel_context *ce)
+{
+	struct intel_runtime_pm *runtime_pm =
+		&ce->engine->gt->i915->runtime_pm;

If you move this after the engine var below you can skip the ce->engine jump. Also, you can shorten the pointer chasing by using engine->uncore->rpm.

Sure.

Quoted 30 lines
Quoted 30 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +30 -0
+	struct intel_engine_cs *engine = ce->engine;
+	struct intel_guc *guc = &engine->gt->uc.guc;
+	u32 desc_idx = ce->guc_id;
+	struct guc_lrc_desc *desc;
+	bool context_registered;
+	intel_wakeref_t wakeref;
+	int ret = 0;
+
+	GEM_BUG_ON(!engine->mask);
+
+	/*
+	 * Ensure LRC + CT vmas are is same region as write barrier is done
+	 * based on CT vma region.
+	 */
+	GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
+		   i915_gem_object_is_lmem(ce->ring->vma->obj));
+
+	context_registered = lrc_desc_registered(guc, desc_idx);
+
+	reset_lrc_desc(guc, desc_idx);
+	set_lrc_desc_registered(guc, desc_idx, ce);
+
+	desc = __get_lrc_desc(guc, desc_idx);
+	desc->engine_class = engine_class_to_guc_class(engine->class);
+	desc->engine_submit_mask = adjust_engine_mask(engine->class,
+						      engine->mask);
+	desc->hw_context_desc = ce->lrc.lrca;
+	desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+	guc_context_policy_init(engine, desc);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 784

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+	init_sched_state(ce);
+
+	/*
+	 * The context_lookup xarray is used to determine if the hardware
+	 * context is currently registered. There are two cases in which it
+	 * could be regisgered either the guc_id has been stole from from

typo regisgered

John got this one. Fixed.

Quoted 24 lines
Quoted 24 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+	 * another context or the lrc descriptor address of this context has
+	 * changed. In either case the context needs to be deregistered with the
+	 * GuC before registering this context.
+	 */
+	if (context_registered) {
+		set_context_wait_for_deregister_to_register(ce);
+		intel_context_get(ce);
+
+		/*
+		 * 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);
+	} else {
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			ret = register_context(ce);
+	}
+
+	return ret;
  }
  static int guc_context_pre_pin(struct intel_context *ce,
@@ -443,36 +813,139 @@ static int guc_context_pre_pin(struct intel_context *ce,
  static int guc_context_pin(struct intel_context *ce, void *vaddr)
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 818

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
  {
+	if (i915_ggtt_offset(ce->state) !=
+	    (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
+		set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Shouldn't this be set after lrc_pin()? I can see ce->lrc.lrca being re-assigned in there; it looks like it can only change if the context is new, but for future-proofing IMO better to re-order here.

No this is right. ce->state is assigned in lr_pre_pin and ce->lrc.lrca is assigned in lrc_pin. To catch the change you have to check between those two functions.

Quoted 7 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 820

Quoted 2 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+
  	return lrc_pin(ce, ce->engine, vaddr);

Could use a comment to say that the GuC context pinning call is delayed until request alloc and to look at the comment in the latter function for details (or move the explanation here and refer here from request alloc).

Yes.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
  }
+static void guc_context_unpin(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 833

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+}
+
+static void guc_context_post_unpin(struct intel_context *ce)
+{
+	lrc_post_unpin(ce);

why do we need this function? we can just pass lrc_post_unpin to the ops (like we already do).

We don't before style reasons I think having a GuC specific wrapper is correct.

Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +16 -0
+}
+
+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;
+
+	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
+	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
+
+	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);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 855

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+}
+
+static void guc_context_destroy(struct kref *kref)
+{
+	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
+	struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;

same as above, going through engine->uncore->rpm is shorter

Sure.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -0
+	struct intel_guc *guc = &ce->engine->gt->uc.guc;
+	intel_wakeref_t wakeref;
+	unsigned long flags;
+
+	/*
+	 * 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.
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 867

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+	 */
+	if (context_guc_id_invalid(ce) ||
+	    !lrc_desc_registered(guc, ce->guc_id)) {
+		release_guc_id(guc, ce);

it feels a bit weird that we call release_guc_id in the case where the id is invalid. The code handles it fine, but still the flow doesn't feel clean. Not a blocker.

I understand. Let's see if this can get cleaned up at some point.

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -0
+		lrc_destroy(kref);
+		return;
+	}
+
+	/*
+	 * We have to acquire the context spinlock and check guc_id again, if it
+	 * is valid it hasn't been stolen and needs to be deregistered. We
+	 * delete this context from the list of unpinned guc_ids available to
+	 * stole to seal a race with guc_lrc_desc_pin(). When the G2H CTB
+	 * returns indicating this context has been deregistered the guc_id is
+	 * returned to the pool of available guc_ids.
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 882

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+	 */
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (context_guc_id_invalid(ce)) {
+		__release_guc_id(guc, ce);

But here the call to __release_guc_id is unneded, right? the ce doesn't own the ID anymore and both the steal and the release function already clean ce->guc_id_link, so there should be nothing left to clean.

This is dead code. Will delete.

Quoted 129 lines
Quoted 129 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +65 -42
+		spin_unlock_irqrestore(&guc->contexts_lock, flags);
+		lrc_destroy(kref);
+		return;
+	}
+
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * We defer GuC context deregistration until the context is destroyed
+	 * in order to save on CTBs. With this optimization ideally we only need
+	 * 1 CTB to register the context during the first pin and 1 CTB to
+	 * deregister the context when the context is destroyed. Without this
+	 * optimization, a CTB would be needed every pin & unpin.
+	 *
+	 * XXX: Need to acqiure the runtime wakeref as this can be triggered
+	 * from context_free_worker when not runtime wakeref is held.
+	 * guc_lrc_desc_unpin requires the runtime as a GuC register is written
+	 * in H2G CTB to deregister the context. A future patch may defer this
+	 * H2G CTB if the runtime wakeref is zero.
+	 */
+	with_intel_runtime_pm(runtime_pm, wakeref)
+		guc_lrc_desc_unpin(ce);
+}
+
+static int guc_context_alloc(struct intel_context *ce)
+{
+	return lrc_alloc(ce, ce->engine);
+}
+
  static const struct intel_context_ops guc_context_ops = {
  	.alloc = guc_context_alloc,
  	.pre_pin = guc_context_pre_pin,
  	.pin = guc_context_pin,
-	.unpin = lrc_unpin,
-	.post_unpin = lrc_post_unpin,
+	.unpin = guc_context_unpin,
+	.post_unpin = guc_context_post_unpin,
  	.enter = intel_context_enter_engine,
  	.exit = intel_context_exit_engine,
  	.reset = lrc_reset,
-	.destroy = lrc_destroy,
+	.destroy = guc_context_destroy,
  };
-static int guc_request_alloc(struct i915_request *request)
+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);
+}
+
+static int guc_request_alloc(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+	struct intel_guc *guc = ce_to_guc(ce);
  	int ret;
-	GEM_BUG_ON(!intel_context_is_pinned(request->context));
+	GEM_BUG_ON(!intel_context_is_pinned(rq->context));
  	/*
  	 * Flush enough space to reduce the likelihood of waiting after
  	 * we start building the request - in which case we will just
  	 * have to repeat work.
  	 */
-	request->reserved_space += GUC_REQUEST_SIZE;
+	rq->reserved_space += GUC_REQUEST_SIZE;
  	/*
  	 * Note that after this point, we have committed to using
@@ -483,56 +956,47 @@ static int guc_request_alloc(struct i915_request *request)
  	 */
  	/* Unconditionally invalidate GPU caches and TLBs. */
-	ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
+	ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
  	if (ret)
  		return ret;
-	request->reserved_space -= GUC_REQUEST_SIZE;
-	return 0;
-}
+	rq->reserved_space -= GUC_REQUEST_SIZE;
-static inline void queue_request(struct i915_sched_engine *sched_engine,
-				 struct i915_request *rq,
-				 int prio)
-{
-	GEM_BUG_ON(!list_empty(&rq->sched.link));
-	list_add_tail(&rq->sched.link,
-		      i915_sched_lookup_priolist(sched_engine, prio));
-	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-}
-
-static int guc_bypass_tasklet_submit(struct intel_guc *guc,
-				     struct i915_request *rq)
-{
-	int ret;
-
-	__i915_request_submit(rq);
-
-	trace_i915_request_in(rq, 0);
-
-	guc_set_lrc_tail(rq);
-	ret = guc_add_request(guc, rq);
-	if (ret == -EBUSY)
-		guc->stalled_request = rq;
-
-	return ret;
-}
-
-static void guc_submit_request(struct i915_request *rq)
-{
-	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
-	struct intel_guc *guc = &rq->engine->gt->uc.guc;
-	unsigned long flags;
+	/*
+	 * Call pin_guc_id here rather than in the pinning step as with
+	 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
+	 * guc_ids and creating horrible race conditions. This is especially bad
+	 * when guc_ids are being stolen due to over subscription. By the time
+	 * this function is reached, it is guaranteed that the guc_id will be
+	 * persistent until the generated request is retired. Thus, sealing these
+	 * race conditions. It is still safe to fail here if guc_ids are
+	 * exhausted and return -EAGAIN to the user indicating that they can try
+	 * again in the future.
+	 *
+	 * There is no need for a lock here as the timeline mutex ensures at
+	 * most one context can be executing this code path at once. The
+	 * guc_id_ref is incremented once for every request in flight and
+	 * decremented on each retire. When it is zero, a lock around the
+	 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
+	 */
+	if (atomic_add_unless(&ce->guc_id_ref, 1, 0))
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_request_alloc(struct i915_request *request) · line 987

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -2
+		return 0;
-	/* Will be called from irq-context when using foreign fences. */
-	spin_lock_irqsave(&sched_engine->lock, flags);
+	ret = pin_guc_id(guc, ce);	/* returns 1 if new guc_id assigned */
+	if (unlikely(ret < 0))
+		return ret;;

typo ";;"

Yep.

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -1
+	if (context_needs_register(ce, !!ret)) {
+		ret = guc_lrc_desc_pin(ce);
+		if (unlikely(ret)) {	/* unwind */
+			atomic_dec(&ce->guc_id_ref);
+			unpin_guc_id(guc, ce);
+			return ret;
+		}
+	}
-	if (guc->stalled_request || !i915_sched_engine_is_empty(sched_engine))
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_request_alloc(struct i915_request *request) · line 997

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -3
-		queue_request(sched_engine, rq, rq_prio(rq));
-	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
-		tasklet_hi_schedule(&sched_engine->tasklet);
+	clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Might be worth moving the pinning to a separate function to keep the request_alloc focused on the request. Can be done as a follow up.

Sure. Let me see how that looks in a follow up.

Quoted 133 lines
Quoted 133 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +101 -1
-	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	return 0;
  }
  static void sanitize_hwsp(struct intel_engine_cs *engine)
@@ -606,6 +1070,46 @@ static void guc_set_default_submission(struct intel_engine_cs *engine)
  	engine->submit_request = guc_submit_request;
  }
+static inline void guc_kernel_context_pin(struct intel_guc *guc,
+					  struct intel_context *ce)
+{
+	if (context_guc_id_invalid(ce))
+		pin_guc_id(guc, ce);
+	guc_lrc_desc_pin(ce);
+}
+
+static inline void guc_init_lrc_mapping(struct intel_guc *guc)
+{
+	struct intel_gt *gt = guc_to_gt(guc);
+	struct intel_engine_cs *engine;
+	enum intel_engine_id id;
+
+	/* make sure all descriptors are clean... */
+	xa_destroy(&guc->context_lookup);
+
+	/*
+	 * Some contexts might have been pinned before we enabled GuC
+	 * submission, so we need to add them to the GuC bookeeping.
+	 * Also, after a reset the GuC we want to make sure that the information
+	 * shared with GuC is properly reset. The kernel lrcs are not attached
+	 * to the gem_context, so they need to be added separately.
+	 *
+	 * Note: we purposely do not check the error return of
+	 * guc_lrc_desc_pin, because that function can only fail in two cases.
+	 * One, if there aren't enough free IDs, but we're guaranteed to have
+	 * enough here (we're either only pinning a handful of lrc on first boot
+	 * or we're re-pinning lrcs that were already pinned before the reset).
+	 * Two, if the GuC has died and CTBs can't make forward progress.
+	 * Presumably, the GuC should be alive as this function is called on
+	 * driver load or after a reset. Even if it is dead, another full GPU
+	 * reset will be triggered and this function would be called again.
+	 */
+
+	for_each_engine(engine, gt, id)
+		if (engine->kernel_context)
+			guc_kernel_context_pin(guc, engine->kernel_context);
+}
+
  static void guc_release(struct intel_engine_cs *engine)
  {
  	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
@@ -718,6 +1222,7 @@ int intel_guc_submission_setup(struct intel_engine_cs *engine)
  void intel_guc_submission_enable(struct intel_guc *guc)
  {
+	guc_init_lrc_mapping(guc);
  }
  void intel_guc_submission_disable(struct intel_guc *guc)
@@ -743,3 +1248,62 @@ void intel_guc_submission_init_early(struct intel_guc *guc)
  {
  	guc->submission_selected = __guc_submission_selected(guc);
  }
+
+static inline struct intel_context *
+g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
+{
+	struct intel_context *ce;
+
+	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Invalid desc_idx %u", desc_idx);
+		return NULL;
+	}
+
+	ce = __get_context(guc, desc_idx);
+	if (unlikely(!ce)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Context is NULL, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
+	return ce;
+}
+
+int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
+					  const u32 *msg,
+					  u32 len)
+{
+	struct intel_context *ce;
+	u32 desc_idx = msg[0];
+
+	if (unlikely(len < 1)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
+		return -EPROTO;
+	}
+
+	ce = g2h_context_lookup(guc, desc_idx);
+	if (unlikely(!ce))
+		return -EPROTO;
+
+	if (context_wait_for_deregister_to_register(ce)) {
+		struct intel_runtime_pm *runtime_pm =
+			&ce->engine->gt->i915->runtime_pm;
+		intel_wakeref_t wakeref;
+
+		/*
+		 * Previous owner of this guc_id has been deregistered, now safe
+		 * register this context.
+		 */
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			register_context(ce);
+		clr_context_wait_for_deregister_to_register(ce);
+		intel_context_put(ce);
+	} else if (context_destroyed(ce)) {
+		/* Context has been destroyed */
+		release_guc_id(guc, ce);
+		lrc_destroy(&ce->ref);
+	}
+
+	return 0;
+}
drivers/gpu/drm/i915/i915_reg.h +1 -0
index 943fe485c662..204c95c39353 100644
@@ -4142,6 +4142,7 @@ enum {
  	FAULT_AND_CONTINUE /* Unsupported */
  };
+#define CTX_GTT_ADDRESS_MASK GENMASK(31, 12)
  #define GEN8_CTX_VALID (1 << 0)
  #define GEN8_CTX_FORCE_PD_RESTORE (1 << 1)
  #define GEN8_CTX_FORCE_RESTORE (1 << 2)
drivers/gpu/drm/i915/i915_request.c +0 -1
index 86b4c9f2613d..b48c4905d3fc 100644
--- a/drivers/gpu/drm/i915/i915_request.c
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c bool i915_request_retire(struct i915_request *rq) · line 410

Quoted 6 lines

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

drivers/gpu/drm/i915/i915_request.c +1 -0
@@ -407,6 +407,7 @@ bool i915_request_retire(struct i915_request *rq)
  	 */
  	if (!list_empty(&rq->sched.link))
  		remove_from_engine(rq);
+	atomic_dec(&rq->context->guc_id_ref);

Does this work/make sense  if GuC is disabled?

It doesn't matter if the GuC is disabled as this var isn't used if it is. A following patch adds a vfunc here and pushes this in the GuC backend.

Matt

Quoted 5 lines

Daniele

Quoted 2 lines
drivers/gpu/drm/i915/i915_request.c +0 -0
  	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
  	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
#14 lore raw

Re: [PATCH 06/51] drm/i915/guc: Implement GuC context operations for new inteface

Quoted 141 lines · Matthew Brost
Quoted 131 lines · Daniele Ceraolo Spurio
Quoted 129 lines · Matthew Brost

Implement GuC context operations which includes GuC specific operations alloc, pin, unpin, and destroy.

v2:
(Daniel Vetter)
- Use msleep_interruptible rather than cond_resched in busy loop
(Michal)
- Remove C++ style comment

---
   drivers/gpu/drm/i915/gt/intel_context.c       |   5 +
   drivers/gpu/drm/i915/gt/intel_context_types.h |  22 +-
   drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |   1 -
   drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  40 ++
   drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   4 +
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 666 ++++++++++++++++--
   drivers/gpu/drm/i915/i915_reg.h               |   1 +
   drivers/gpu/drm/i915/i915_request.c           |   1 +
   8 files changed, 685 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +5 -0
index bd63813c8a80..32fd6647154b 100644
@@ -384,6 +384,11 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
   	mutex_init(&ce->pin_mutex);
+	spin_lock_init(&ce->guc_state.lock);
+
+	ce->guc_id = GUC_INVALID_LRC_ID;
+	INIT_LIST_HEAD(&ce->guc_id_link);
+
   	i915_active_init(&ce->active,
   			 __intel_context_active, __intel_context_retire, 0);
   }
drivers/gpu/drm/i915/gt/intel_context_types.h +19 -3
index 6d99631d19b9..606c480aec26 100644
@@ -96,6 +96,7 @@ struct intel_context {
   #define CONTEXT_BANNED			6
   #define CONTEXT_FORCE_SINGLE_SUBMISSION	7
   #define CONTEXT_NOPREEMPT		8
+#define CONTEXT_LRCA_DIRTY		9
   	struct {
   		u64 timeout_us;
@@ -138,14 +139,29 @@ struct intel_context {
   	u8 wa_bb_page; /* if set, page num reserved for context workarounds */
+	struct {
+		/** lock: protects everything in guc_state */
+		spinlock_t lock;
+		/**
+		 * sched_state: scheduling state of this context using GuC
+		 * submission
+		 */
+		u8 sched_state;
+	} guc_state;
+
   	/* GuC scheduling state flags that do not require a lock. */
   	atomic_t guc_sched_state_no_lock;
+	/* GuC LRC descriptor ID */
+	u16 guc_id;
+
+	/* GuC LRC descriptor reference count */
+	atomic_t guc_id_ref;
+
   	/*
-	 * GuC LRC descriptor ID - Not assigned in this patch but future patches
-	 * in the series will.
+	 * GuC ID link - in list when unpinned but guc_id still valid in GuC
   	 */
-	u16 guc_id;
+	struct list_head guc_id_link;
   };
   #endif /* __INTEL_CONTEXT_TYPES__ */
drivers/gpu/drm/i915/gt/intel_lrc_reg.h +0 -1
index 41e5350a7a05..49d4857ad9b7 100644
@@ -87,7 +87,6 @@
   #define GEN11_CSB_WRITE_PTR_MASK	(GEN11_CSB_PTR_MASK << 0)
   #define MAX_CONTEXT_HW_ID	(1 << 21) /* exclusive */
-#define MAX_GUC_CONTEXT_HW_ID	(1 << 20) /* exclusive */
   #define GEN11_MAX_CONTEXT_HW_ID	(1 << 11) /* exclusive */
   /* in Gen12 ID 0x7FF is reserved to indicate idle */
   #define GEN12_MAX_CONTEXT_HW_ID	(GEN11_MAX_CONTEXT_HW_ID - 1)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +25 -0
index 8c7b92f699f1..30773cd699f5 100644
@@ -7,6 +7,7 @@
   #define _INTEL_GUC_H_
   #include <linux/xarray.h>
+#include <linux/delay.h>
   #include "intel_uncore.h"
   #include "intel_guc_fw.h"
@@ -44,6 +45,14 @@ struct intel_guc {
   		void (*disable)(struct intel_guc *guc);
   	} interrupts;
+	/*
+	 * contexts_lock protects the pool of free guc ids and a linked list of
+	 * guc ids available to be stolen
+	 */
+	spinlock_t contexts_lock;
+	struct ida guc_ids;
+	struct list_head guc_id_list;
+
   	bool submission_selected;
   	struct i915_vma *ads_vma;
@@ -101,6 +110,34 @@ intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
   				 response_buf, response_buf_size, 0);
   }
+static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
+					   const u32 *action,
+					   u32 len,
+					   bool loop)
+{
+	int err;
+	unsigned int sleep_period_ms = 1;
+	bool not_atomic = !in_atomic() && !irqs_disabled();
+
+	/* No sleeping with spin locks, just busy loop */
+	might_sleep_if(loop && not_atomic);
+
+retry:
+	err = intel_guc_send_nb(guc, action, len);
+	if (unlikely(err == -EBUSY && loop)) {
+		if (likely(not_atomic)) {
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc.h intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len, · line 133

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -0
+			if (msleep_interruptible(sleep_period_ms))
+				return -EINTR;
+			sleep_period_ms = sleep_period_ms << 1;
+		} else {
+			cpu_relax();

Potentially something we can change later, but if we're in atomic context we can't keep looping without a timeout while we get -EBUSY, we need to bail out early.

Quoted 2 lines

Eventually intel_guc_send_nb will pop with a different return code after 1 second I think if the GuC is hung.

I know, the point is that 1 second is too long in atomic context. This has to either complete quick in atomic or be forked off to a worker. Can fix as a follow up.

Daniele

Quoted 886 lines
Quoted 50 lines
Quoted 50 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +10 -0
+		}
+		goto retry;
+	}
+
+	return err;
+}
+
   static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
   {
   	intel_guc_ct_event_handler(&guc->ct);
@@ -202,6 +239,9 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
   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);
+
   void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
   #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +4 -0
index 83ec60ea3f89..28ff82c5be45 100644
@@ -928,6 +928,10 @@ static int ct_process_request(struct intel_guc_ct *ct, struct ct_incoming_msg *r
   	case INTEL_GUC_ACTION_DEFAULT:
   		ret = intel_guc_to_host_process_recv_msg(guc, payload, len);
   		break;
+	case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
+		ret = intel_guc_deregister_done_process_msg(guc, payload,
+							    len);
+		break;
   	default:
   		ret = -EOPNOTSUPP;
   		break;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -0
index 53b4a5eb4a85..a47b3813b4d0 100644
@@ -13,7 +13,9 @@
   #include "gt/intel_gt.h"
   #include "gt/intel_gt_irq.h"
   #include "gt/intel_gt_pm.h"
+#include "gt/intel_gt_requests.h"
   #include "gt/intel_lrc.h"
+#include "gt/intel_lrc_reg.h"
   #include "gt/intel_mocs.h"
   #include "gt/intel_ring.h"
@@ -85,6 +87,73 @@ static inline void clr_context_enabled(struct intel_context *ce)
   		   &ce->guc_sched_state_no_lock);
   }
Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 95

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+/*
+ * Below is a set of functions which control the GuC scheduling state which
+ * require a lock, aside from the special case where the functions are called
+ * from guc_lrc_desc_pin(). In that case it isn't possible for any other code
+ * path to be executing on the context.
+ */

Is there a reason to avoid taking the lock in guc_lrc_desc_pin, even if it isn't strictly needed? I'd prefer to avoid the asymmetry in the locking

If this code is called from releasing a fence a circular dependency is created. Once we move the DRM scheduler the locking becomes way easier and we clean this up then. We already have task for locking clean up.

Quoted 2 lines

scheme if possible, as that might case trouble if thing change in the future.

Again this is temporary code, so I think we can live with this until the DRM scheduler lands.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+#define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER	BIT(0)
+#define SCHED_STATE_DESTROYED				BIT(1)
+static inline void init_sched_state(struct intel_context *ce)
+{
+	/* Only should be called from guc_lrc_desc_pin() */
+	atomic_set(&ce->guc_sched_state_no_lock, 0);
+	ce->guc_state.sched_state = 0;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 109

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static inline bool
+context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state &
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

No need for (). Below as well.

Yep.

Quoted 19 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static inline void
+set_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	/* Only should be called from guc_lrc_desc_pin() */
+	ce->guc_state.sched_state |=

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 126

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+		SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
+}
+
+static inline void
+clr_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state =
+		(ce->guc_state.sched_state &
+		 ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

nit: can also use

Quoted 3 lines

ce->guc_state.sched_state &=
~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER

Yep.

Quoted 156 lines
Quoted 156 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +137 -2
+}
+
+static inline bool
+context_destroyed(struct intel_context *ce)
+{
+	return (ce->guc_state.sched_state & SCHED_STATE_DESTROYED);
+}
+
+static inline void
+set_context_destroyed(struct intel_context *ce)
+{
+	lockdep_assert_held(&ce->guc_state.lock);
+	ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
+}
+
+static inline bool context_guc_id_invalid(struct intel_context *ce)
+{
+	return (ce->guc_id == GUC_INVALID_LRC_ID);
+}
+
+static inline void set_context_guc_id_invalid(struct intel_context *ce)
+{
+	ce->guc_id = GUC_INVALID_LRC_ID;
+}
+
+static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
+{
+	return &ce->engine->gt->uc.guc;
+}
+
   static inline struct i915_priolist *to_priolist(struct rb_node *rb)
   {
   	return rb_entry(rb, struct i915_priolist, node);
@@ -155,6 +224,9 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
   	int len = 0;
   	bool enabled = context_enabled(ce);
+	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
+	GEM_BUG_ON(context_guc_id_invalid(ce));
+
   	if (!enabled) {
   		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
   		action[len++] = ce->guc_id;
@@ -417,6 +489,10 @@ int intel_guc_submission_init(struct intel_guc *guc)
   	xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
+	spin_lock_init(&guc->contexts_lock);
+	INIT_LIST_HEAD(&guc->guc_id_list);
+	ida_init(&guc->guc_ids);
+
   	return 0;
   }
@@ -429,9 +505,303 @@ void intel_guc_submission_fini(struct intel_guc *guc)
   	i915_sched_engine_put(guc->sched_engine);
   }
-static int guc_context_alloc(struct intel_context *ce)
+static inline void queue_request(struct i915_sched_engine *sched_engine,
+				 struct i915_request *rq,
+				 int prio)
   {
-	return lrc_alloc(ce, ce->engine);
+	GEM_BUG_ON(!list_empty(&rq->sched.link));
+	list_add_tail(&rq->sched.link,
+		      i915_sched_lookup_priolist(sched_engine, prio));
+	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+}
+
+static int guc_bypass_tasklet_submit(struct intel_guc *guc,
+				     struct i915_request *rq)
+{
+	int ret;
+
+	__i915_request_submit(rq);
+
+	trace_i915_request_in(rq, 0);
+
+	guc_set_lrc_tail(rq);
+	ret = guc_add_request(guc, rq);
+	if (ret == -EBUSY)
+		guc->stalled_request = rq;
+
+	return ret;
+}
+
+static void guc_submit_request(struct i915_request *rq)
+{
+	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
+	struct intel_guc *guc = &rq->engine->gt->uc.guc;
+	unsigned long flags;
+
+	/* 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))
+		queue_request(sched_engine, rq, rq_prio(rq));
+	else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
+		tasklet_hi_schedule(&sched_engine->tasklet);
+
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+#define GUC_ID_START	64	/* First 64 guc_ids reserved */
+static int new_guc_id(struct intel_guc *guc)
+{
+	return ida_simple_get(&guc->guc_ids, GUC_ID_START,
+			      GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+}
+
+static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	if (!context_guc_id_invalid(ce)) {
+		ida_simple_remove(&guc->guc_ids, ce->guc_id);
+		reset_lrc_desc(guc, ce->guc_id);
+		set_context_guc_id_invalid(ce);
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+}
+
+static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	__release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int steal_guc_id(struct intel_guc *guc)
+{
+	struct intel_context *ce;
+	int guc_id;
+
+	if (!list_empty(&guc->guc_id_list)) {
+		ce = list_first_entry(&guc->guc_id_list,
+				      struct intel_context,
+				      guc_id_link);
+
+		GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+		GEM_BUG_ON(context_guc_id_invalid(ce));
+
+		list_del_init(&ce->guc_id_link);
+		guc_id = ce->guc_id;
+		set_context_guc_id_invalid(ce);
+		return guc_id;
+	} else {
+		return -EAGAIN;
+	}
+}
+
+static int assign_guc_id(struct intel_guc *guc, u16 *out)
+{
+	int ret;
+
+	ret = new_guc_id(guc);
+	if (unlikely(ret < 0)) {
+		ret = steal_guc_id(guc);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 614

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+		if (ret < 0)
+			return ret;
+	}
+
+	*out = ret;
+	return 0;

Is it worth adding spinlock_held asserts for guc->contexts_lock in these ID functions? Doubles up as a documentation of what locking we expect.

Never a bad idea to have asserts in the code. Will add.

Quoted 110 lines
Quoted 110 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +110 -0
+}
+
+#define PIN_GUC_ID_TRIES	4
+static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	int ret = 0;
+	unsigned long flags, tries = PIN_GUC_ID_TRIES;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+
+try_again:
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+
+	if (context_guc_id_invalid(ce)) {
+		ret = assign_guc_id(guc, &ce->guc_id);
+		if (ret)
+			goto out_unlock;
+		ret = 1;	/* Indidcates newly assigned guc_id */
+	}
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	atomic_inc(&ce->guc_id_ref);
+
+out_unlock:
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * -EAGAIN indicates no guc_ids are available, let's retire any
+	 * outstanding requests to see if that frees up a guc_id. If the first
+	 * retire didn't help, insert a sleep with the timeslice duration before
+	 * attempting to retire more requests. Double the sleep period each
+	 * subsequent pass before finally giving up. The sleep period has max of
+	 * 100ms and minimum of 1ms.
+	 */
+	if (ret == -EAGAIN && --tries) {
+		if (PIN_GUC_ID_TRIES - tries > 1) {
+			unsigned int timeslice_shifted =
+				ce->engine->props.timeslice_duration_ms <<
+				(PIN_GUC_ID_TRIES - tries - 2);
+			unsigned int max = min_t(unsigned int, 100,
+						 timeslice_shifted);
+
+			msleep(max_t(unsigned int, max, 1));
+		}
+		intel_gt_retire_requests(guc_to_gt(guc));
+		goto try_again;
+	}
+
+	return ret;
+}
+
+static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+	unsigned long flags;
+
+	GEM_BUG_ON(atomic_read(&ce->guc_id_ref) < 0);
+
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id_link) &&
+	    !atomic_read(&ce->guc_id_ref))
+		list_add_tail(&ce->guc_id_link, &guc->guc_id_list);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int __guc_action_register_context(struct intel_guc *guc,
+					 u32 guc_id,
+					 u32 offset)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_REGISTER_CONTEXT,
+		guc_id,
+		offset,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int register_context(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+	u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
+		ce->guc_id * sizeof(struct guc_lrc_desc);
+
+	return __guc_action_register_context(guc, ce->guc_id, offset);
+}
+
+static int __guc_action_deregister_context(struct intel_guc *guc,
+					   u32 guc_id)
+{
+	u32 action[] = {
+		INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
+		guc_id,
+	};
+
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+}
+
+static int deregister_context(struct intel_context *ce, u32 guc_id)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	return __guc_action_deregister_context(guc, guc_id);
+}
+
+static intel_engine_mask_t adjust_engine_mask(u8 class, intel_engine_mask_t mask)
+{
+	switch (class) {
+	case RENDER_CLASS:
+		return mask >> RCS0;
+	case VIDEO_ENHANCEMENT_CLASS:
Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 731

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+		return mask >> VECS0;
+	case VIDEO_DECODE_CLASS:
+		return mask >> VCS0;
+	case COPY_ENGINE_CLASS:
+		return mask >> BCS0;
+	default:
+		GEM_BUG_ON("Invalid Class");

we usually use MISSING_CASE for this type of errors.

As soon as start talking in logical space (series immediatly after this gets merged) this function gets deleted but will fix.

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +12 -0
+		return 0;
+	}
+}
+
+static void guc_context_policy_init(struct intel_engine_cs *engine,
+				    struct guc_lrc_desc *desc)
+{
+	desc->policy_flags = 0;
+
+	desc->execution_quantum = CONTEXT_POLICY_DEFAULT_EXECUTION_QUANTUM_US;
+	desc->preemption_timeout = CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
+}
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 748

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+
+static int guc_lrc_desc_pin(struct intel_context *ce)
+{
+	struct intel_runtime_pm *runtime_pm =
+		&ce->engine->gt->i915->runtime_pm;

If you move this after the engine var below you can skip the ce->engine jump. Also, you can shorten the pointer chasing by using engine->uncore->rpm.

Sure.

Quoted 29 lines
Quoted 29 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +29 -0
+	struct intel_engine_cs *engine = ce->engine;
+	struct intel_guc *guc = &engine->gt->uc.guc;
+	u32 desc_idx = ce->guc_id;
+	struct guc_lrc_desc *desc;
+	bool context_registered;
+	intel_wakeref_t wakeref;
+	int ret = 0;
+
+	GEM_BUG_ON(!engine->mask);
+
+	/*
+	 * Ensure LRC + CT vmas are is same region as write barrier is done
+	 * based on CT vma region.
+	 */
+	GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) !=
+		   i915_gem_object_is_lmem(ce->ring->vma->obj));
+
+	context_registered = lrc_desc_registered(guc, desc_idx);
+
+	reset_lrc_desc(guc, desc_idx);
+	set_lrc_desc_registered(guc, desc_idx, ce);
+
+	desc = __get_lrc_desc(guc, desc_idx);
+	desc->engine_class = engine_class_to_guc_class(engine->class);
+	desc->engine_submit_mask = adjust_engine_mask(engine->class,
+						      engine->mask);
+	desc->hw_context_desc = ce->lrc.lrca;
+	desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 784

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	guc_context_policy_init(engine, desc);
+	init_sched_state(ce);
+
+	/*
+	 * The context_lookup xarray is used to determine if the hardware
+	 * context is currently registered. There are two cases in which it
+	 * could be regisgered either the guc_id has been stole from from

typo regisgered

John got this one. Fixed.

Quoted 23 lines
Quoted 23 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+	 * another context or the lrc descriptor address of this context has
+	 * changed. In either case the context needs to be deregistered with the
+	 * GuC before registering this context.
+	 */
+	if (context_registered) {
+		set_context_wait_for_deregister_to_register(ce);
+		intel_context_get(ce);
+
+		/*
+		 * 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);
+	} else {
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			ret = register_context(ce);
+	}
+
+	return ret;
   }
   static int guc_context_pre_pin(struct intel_context *ce,
@@ -443,36 +813,139 @@ static int guc_context_pre_pin(struct intel_context *ce,
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 818

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
   static int guc_context_pin(struct intel_context *ce, void *vaddr)
   {
+	if (i915_ggtt_offset(ce->state) !=
+	    (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
+		set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Shouldn't this be set after lrc_pin()? I can see ce->lrc.lrca being re-assigned in there; it looks like it can only change if the context is new, but for future-proofing IMO better to re-order here.

No this is right. ce->state is assigned in lr_pre_pin and ce->lrc.lrca is assigned in lrc_pin. To catch the change you have to check between those two functions.

Quoted 6 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 820

Quoted 2 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+
   	return lrc_pin(ce, ce->engine, vaddr);

Could use a comment to say that the GuC context pinning call is delayed until request alloc and to look at the comment in the latter function for details (or move the explanation here and refer here from request alloc).

Yes.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
   }
+static void guc_context_unpin(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	unpin_guc_id(guc, ce);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 833

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+	lrc_unpin(ce);
+}
+
+static void guc_context_post_unpin(struct intel_context *ce)
+{
+	lrc_post_unpin(ce);

why do we need this function? we can just pass lrc_post_unpin to the ops (like we already do).

We don't before style reasons I think having a GuC specific wrapper is correct.

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +15 -0
+}
+
+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;
+
+	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
+	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
+
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	set_context_destroyed(ce);
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 855

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	deregister_context(ce, ce->guc_id);
+}
+
+static void guc_context_destroy(struct kref *kref)
+{
+	struct intel_context *ce = container_of(kref, typeof(*ce), ref);
+	struct intel_runtime_pm *runtime_pm = &ce->engine->gt->i915->runtime_pm;

same as above, going through engine->uncore->rpm is shorter

Sure.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+	struct intel_guc *guc = &ce->engine->gt->uc.guc;
+	intel_wakeref_t wakeref;
+	unsigned long flags;
+
+	/*
+	 * 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
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 867

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+	 * registered with the GuC.
+	 */
+	if (context_guc_id_invalid(ce) ||
+	    !lrc_desc_registered(guc, ce->guc_id)) {
+		release_guc_id(guc, ce);

it feels a bit weird that we call release_guc_id in the case where the id is invalid. The code handles it fine, but still the flow doesn't feel clean. Not a blocker.

I understand. Let's see if this can get cleaned up at some point.

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+		lrc_destroy(kref);
+		return;
+	}
+
+	/*
+	 * We have to acquire the context spinlock and check guc_id again, if it
+	 * is valid it hasn't been stolen and needs to be deregistered. We
+	 * delete this context from the list of unpinned guc_ids available to
+	 * stole to seal a race with guc_lrc_desc_pin(). When the G2H CTB
+	 * returns indicating this context has been deregistered the guc_id is
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 882

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+	 * returned to the pool of available guc_ids.
+	 */
+	spin_lock_irqsave(&guc->contexts_lock, flags);
+	if (context_guc_id_invalid(ce)) {
+		__release_guc_id(guc, ce);

But here the call to __release_guc_id is unneded, right? the ce doesn't own the ID anymore and both the steal and the release function already clean ce->guc_id_link, so there should be nothing left to clean.

This is dead code. Will delete.

Quoted 128 lines
Quoted 128 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +64 -42
+		spin_unlock_irqrestore(&guc->contexts_lock, flags);
+		lrc_destroy(kref);
+		return;
+	}
+
+	if (!list_empty(&ce->guc_id_link))
+		list_del_init(&ce->guc_id_link);
+	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+	/*
+	 * We defer GuC context deregistration until the context is destroyed
+	 * in order to save on CTBs. With this optimization ideally we only need
+	 * 1 CTB to register the context during the first pin and 1 CTB to
+	 * deregister the context when the context is destroyed. Without this
+	 * optimization, a CTB would be needed every pin & unpin.
+	 *
+	 * XXX: Need to acqiure the runtime wakeref as this can be triggered
+	 * from context_free_worker when not runtime wakeref is held.
+	 * guc_lrc_desc_unpin requires the runtime as a GuC register is written
+	 * in H2G CTB to deregister the context. A future patch may defer this
+	 * H2G CTB if the runtime wakeref is zero.
+	 */
+	with_intel_runtime_pm(runtime_pm, wakeref)
+		guc_lrc_desc_unpin(ce);
+}
+
+static int guc_context_alloc(struct intel_context *ce)
+{
+	return lrc_alloc(ce, ce->engine);
+}
+
   static const struct intel_context_ops guc_context_ops = {
   	.alloc = guc_context_alloc,
   	.pre_pin = guc_context_pre_pin,
   	.pin = guc_context_pin,
-	.unpin = lrc_unpin,
-	.post_unpin = lrc_post_unpin,
+	.unpin = guc_context_unpin,
+	.post_unpin = guc_context_post_unpin,
   	.enter = intel_context_enter_engine,
   	.exit = intel_context_exit_engine,
   	.reset = lrc_reset,
-	.destroy = lrc_destroy,
+	.destroy = guc_context_destroy,
   };
-static int guc_request_alloc(struct i915_request *request)
+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);
+}
+
+static int guc_request_alloc(struct i915_request *rq)
+{
+	struct intel_context *ce = rq->context;
+	struct intel_guc *guc = ce_to_guc(ce);
   	int ret;
-	GEM_BUG_ON(!intel_context_is_pinned(request->context));
+	GEM_BUG_ON(!intel_context_is_pinned(rq->context));
   	/*
   	 * Flush enough space to reduce the likelihood of waiting after
   	 * we start building the request - in which case we will just
   	 * have to repeat work.
   	 */
-	request->reserved_space += GUC_REQUEST_SIZE;
+	rq->reserved_space += GUC_REQUEST_SIZE;
   	/*
   	 * Note that after this point, we have committed to using
@@ -483,56 +956,47 @@ static int guc_request_alloc(struct i915_request *request)
   	 */
   	/* Unconditionally invalidate GPU caches and TLBs. */
-	ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
+	ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
   	if (ret)
   		return ret;
-	request->reserved_space -= GUC_REQUEST_SIZE;
-	return 0;
-}
+	rq->reserved_space -= GUC_REQUEST_SIZE;
-static inline void queue_request(struct i915_sched_engine *sched_engine,
-				 struct i915_request *rq,
-				 int prio)
-{
-	GEM_BUG_ON(!list_empty(&rq->sched.link));
-	list_add_tail(&rq->sched.link,
-		      i915_sched_lookup_priolist(sched_engine, prio));
-	set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-}
-
-static int guc_bypass_tasklet_submit(struct intel_guc *guc,
-				     struct i915_request *rq)
-{
-	int ret;
-
-	__i915_request_submit(rq);
-
-	trace_i915_request_in(rq, 0);
-
-	guc_set_lrc_tail(rq);
-	ret = guc_add_request(guc, rq);
-	if (ret == -EBUSY)
-		guc->stalled_request = rq;
-
-	return ret;
-}
-
-static void guc_submit_request(struct i915_request *rq)
-{
-	struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
-	struct intel_guc *guc = &rq->engine->gt->uc.guc;
-	unsigned long flags;
+	/*
+	 * Call pin_guc_id here rather than in the pinning step as with
+	 * dma_resv, contexts can be repeatedly pinned / unpinned trashing the
+	 * guc_ids and creating horrible race conditions. This is especially bad
+	 * when guc_ids are being stolen due to over subscription. By the time
+	 * this function is reached, it is guaranteed that the guc_id will be
+	 * persistent until the generated request is retired. Thus, sealing these
+	 * race conditions. It is still safe to fail here if guc_ids are
+	 * exhausted and return -EAGAIN to the user indicating that they can try
+	 * again in the future.
+	 *
+	 * There is no need for a lock here as the timeline mutex ensures at
+	 * most one context can be executing this code path at once. The
+	 * guc_id_ref is incremented once for every request in flight and
+	 * decremented on each retire. When it is zero, a lock around the
+	 * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
+	 */
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_request_alloc(struct i915_request *request) · line 987

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -2
+	if (atomic_add_unless(&ce->guc_id_ref, 1, 0))
+		return 0;
-	/* Will be called from irq-context when using foreign fences. */
-	spin_lock_irqsave(&sched_engine->lock, flags);
+	ret = pin_guc_id(guc, ce);	/* returns 1 if new guc_id assigned */
+	if (unlikely(ret < 0))
+		return ret;;

typo ";;"

Yep.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -0
+	if (context_needs_register(ce, !!ret)) {
+		ret = guc_lrc_desc_pin(ce);
+		if (unlikely(ret)) {	/* unwind */
+			atomic_dec(&ce->guc_id_ref);
+			unpin_guc_id(guc, ce);
+			return ret;
+		}
+	}
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_request_alloc(struct i915_request *request) · line 997

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -4
-	if (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);
+	clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Might be worth moving the pinning to a separate function to keep the request_alloc focused on the request. Can be done as a follow up.

Sure. Let me see how that looks in a follow up.

Quoted 132 lines
Quoted 132 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +101 -1
-	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	return 0;
   }
   static void sanitize_hwsp(struct intel_engine_cs *engine)
@@ -606,6 +1070,46 @@ static void guc_set_default_submission(struct intel_engine_cs *engine)
   	engine->submit_request = guc_submit_request;
   }
+static inline void guc_kernel_context_pin(struct intel_guc *guc,
+					  struct intel_context *ce)
+{
+	if (context_guc_id_invalid(ce))
+		pin_guc_id(guc, ce);
+	guc_lrc_desc_pin(ce);
+}
+
+static inline void guc_init_lrc_mapping(struct intel_guc *guc)
+{
+	struct intel_gt *gt = guc_to_gt(guc);
+	struct intel_engine_cs *engine;
+	enum intel_engine_id id;
+
+	/* make sure all descriptors are clean... */
+	xa_destroy(&guc->context_lookup);
+
+	/*
+	 * Some contexts might have been pinned before we enabled GuC
+	 * submission, so we need to add them to the GuC bookeeping.
+	 * Also, after a reset the GuC we want to make sure that the information
+	 * shared with GuC is properly reset. The kernel lrcs are not attached
+	 * to the gem_context, so they need to be added separately.
+	 *
+	 * Note: we purposely do not check the error return of
+	 * guc_lrc_desc_pin, because that function can only fail in two cases.
+	 * One, if there aren't enough free IDs, but we're guaranteed to have
+	 * enough here (we're either only pinning a handful of lrc on first boot
+	 * or we're re-pinning lrcs that were already pinned before the reset).
+	 * Two, if the GuC has died and CTBs can't make forward progress.
+	 * Presumably, the GuC should be alive as this function is called on
+	 * driver load or after a reset. Even if it is dead, another full GPU
+	 * reset will be triggered and this function would be called again.
+	 */
+
+	for_each_engine(engine, gt, id)
+		if (engine->kernel_context)
+			guc_kernel_context_pin(guc, engine->kernel_context);
+}
+
   static void guc_release(struct intel_engine_cs *engine)
   {
   	engine->sanitize = NULL; /* no longer in control, nothing to sanitize */
@@ -718,6 +1222,7 @@ int intel_guc_submission_setup(struct intel_engine_cs *engine)
   void intel_guc_submission_enable(struct intel_guc *guc)
   {
+	guc_init_lrc_mapping(guc);
   }
   void intel_guc_submission_disable(struct intel_guc *guc)
@@ -743,3 +1248,62 @@ void intel_guc_submission_init_early(struct intel_guc *guc)
   {
   	guc->submission_selected = __guc_submission_selected(guc);
   }
+
+static inline struct intel_context *
+g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
+{
+	struct intel_context *ce;
+
+	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Invalid desc_idx %u", desc_idx);
+		return NULL;
+	}
+
+	ce = __get_context(guc, desc_idx);
+	if (unlikely(!ce)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm,
+			"Context is NULL, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
+	return ce;
+}
+
+int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
+					  const u32 *msg,
+					  u32 len)
+{
+	struct intel_context *ce;
+	u32 desc_idx = msg[0];
+
+	if (unlikely(len < 1)) {
+		drm_dbg(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
+		return -EPROTO;
+	}
+
+	ce = g2h_context_lookup(guc, desc_idx);
+	if (unlikely(!ce))
+		return -EPROTO;
+
+	if (context_wait_for_deregister_to_register(ce)) {
+		struct intel_runtime_pm *runtime_pm =
+			&ce->engine->gt->i915->runtime_pm;
+		intel_wakeref_t wakeref;
+
+		/*
+		 * Previous owner of this guc_id has been deregistered, now safe
+		 * register this context.
+		 */
+		with_intel_runtime_pm(runtime_pm, wakeref)
+			register_context(ce);
+		clr_context_wait_for_deregister_to_register(ce);
+		intel_context_put(ce);
+	} else if (context_destroyed(ce)) {
+		/* Context has been destroyed */
+		release_guc_id(guc, ce);
+		lrc_destroy(&ce->ref);
+	}
+
+	return 0;
+}
drivers/gpu/drm/i915/i915_reg.h +1 -0
index 943fe485c662..204c95c39353 100644
@@ -4142,6 +4142,7 @@ enum {
   	FAULT_AND_CONTINUE /* Unsupported */
   };
+#define CTX_GTT_ADDRESS_MASK GENMASK(31, 12)
   #define GEN8_CTX_VALID (1 << 0)
   #define GEN8_CTX_FORCE_PD_RESTORE (1 << 1)
   #define GEN8_CTX_FORCE_RESTORE (1 << 2)
drivers/gpu/drm/i915/i915_request.c +0 -0
index 86b4c9f2613d..b48c4905d3fc 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c bool i915_request_retire(struct i915_request *rq) · line 410

Quoted 7 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
@@ -407,6 +407,7 @@ bool i915_request_retire(struct i915_request *rq)
   	 */
   	if (!list_empty(&rq->sched.link))
   		remove_from_engine(rq);
+	atomic_dec(&rq->context->guc_id_ref);

Does this work/make sense  if GuC is disabled?

Quoted 10 lines

It doesn't matter if the GuC is disabled as this var isn't used if it is. A following patch adds a vfunc here and pushes this in the GuC backend.

Matt

Quoted 4 lines

Daniele

Quoted 2 lines
drivers/gpu/drm/i915/i915_request.c +0 -0
   	GEM_BUG_ON(!llist_empty(&rq->execute_cb));
   	__list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */

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

#15 lore raw

Re: [Intel-gfx] [PATCH 06/51] drm/i915/guc: Implement GuC context operations for new inteface

Quoted 157 lines · Daniele Ceraolo Spurio
Quoted 154 lines · Matthew Brost
Quoted 153 lines · Daniele Ceraolo Spurio
Quoted 135 lines · Matthew Brost

Implement GuC context operations which includes GuC specific operations alloc, pin, unpin, and destroy.

v2:
   (Daniel Vetter)
    - Use msleep_interruptible rather than cond_resched in busy loop (Michal)
    - Remove C++ style comment

---
   drivers/gpu/drm/i915/gt/intel_context.c       |   5 + drivers/gpu/drm/i915/gt/intel_context_types.h |  22 +- drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |   1 - drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  40 ++ drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   4 + .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 666
++++++++++++++++--
   drivers/gpu/drm/i915/i915_reg.h               |   1 + drivers/gpu/drm/i915/i915_request.c           |   1 +

   8 files changed, 685 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +5 -0
index bd63813c8a80..32fd6647154b 100644
@@ -384,6 +384,11 @@ intel_context_init(struct intel_context *ce,
 struct intel_engine_cs *engine)
        mutex_init(&ce->pin_mutex);
+    spin_lock_init(&ce->guc_state.lock);
+
+    ce->guc_id = GUC_INVALID_LRC_ID;
+    INIT_LIST_HEAD(&ce->guc_id_link);
+
        i915_active_init(&ce->active,
                 __intel_context_active, __intel_context_retire, 0);
    }
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -0
index 6d99631d19b9..606c480aec26 100644
@@ -96,6 +96,7 @@ struct intel_context {
    #define CONTEXT_BANNED            6
    #define CONTEXT_FORCE_SINGLE_SUBMISSION    7
    #define CONTEXT_NOPREEMPT        8
+#define CONTEXT_LRCA_DIRTY        9
        struct {
            u64 timeout_us;
@@ -138,14 +139,29 @@ struct intel_context {

       u8 wa_bb_page; /* if set, page num reserved for context workarounds */
+    struct {
+        /** lock: protects everything in guc_state */
+        spinlock_t lock;
+        /**
+         * sched_state: scheduling state of this context using GuC +         * submission
+         */
+        u8 sched_state;
+    } guc_state;
+
       /* GuC scheduling state flags that do not require a lock. */ atomic_t guc_sched_state_no_lock;
+    /* GuC LRC descriptor ID */
+    u16 guc_id;

drivers/gpu/drm/i915/gt/intel_context_types.h +4 -0
+
+    /* GuC LRC descriptor reference count */
+    atomic_t guc_id_ref;
+

       /*
-     * GuC LRC descriptor ID - Not assigned in this patch but future patches
-     * in the series will.
+     * GuC ID link - in list when unpinned but guc_id still valid in GuC
        */
-    u16 guc_id;
+    struct list_head guc_id_link;
   };
   #endif /* __INTEL_CONTEXT_TYPES__ */

drivers/gpu/drm/i915/gt/intel_lrc_reg.h +0 -1
index 41e5350a7a05..49d4857ad9b7 100644
@@ -87,7 +87,6 @@
    #define GEN11_CSB_WRITE_PTR_MASK    (GEN11_CSB_PTR_MASK << 0)
    #define MAX_CONTEXT_HW_ID    (1 << 21) /* exclusive */
-#define MAX_GUC_CONTEXT_HW_ID    (1 << 20) /* exclusive */
    #define GEN11_MAX_CONTEXT_HW_ID    (1 << 11) /* exclusive */
    /* in Gen12 ID 0x7FF is reserved to indicate idle */
    #define GEN12_MAX_CONTEXT_HW_ID    (GEN11_MAX_CONTEXT_HW_ID - 1)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +3 -0
index 8c7b92f699f1..30773cd699f5 100644
@@ -7,6 +7,7 @@
    #define _INTEL_GUC_H_
    #include <linux/xarray.h>
+#include <linux/delay.h>
    #include "intel_uncore.h"
    #include "intel_guc_fw.h"
@@ -44,6 +45,14 @@ struct intel_guc {
            void (*disable)(struct intel_guc *guc);
        } interrupts;
+    /*
+     * contexts_lock protects the pool of free guc ids and a linked

list of
+     * guc ids available to be stolen
+     */
+    spinlock_t contexts_lock;
+    struct ida guc_ids;
+    struct list_head guc_id_list;
+
       bool submission_selected;
       struct i915_vma *ads_vma;

drivers/gpu/drm/i915/gt/uc/intel_guc.h +11 -0
@@ -101,6 +110,34 @@ intel_guc_send_and_receive(struct intel_guc
 *guc, const u32 *action, u32 len,
                     response_buf, response_buf_size, 0);
    }
+static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
+                       const u32 *action,
+                       u32 len,
+                       bool loop)
+{
+    int err;
+    unsigned int sleep_period_ms = 1;
+    bool not_atomic = !in_atomic() && !irqs_disabled();
+
+    /* No sleeping with spin locks, just busy loop */
+    might_sleep_if(loop && not_atomic);

drivers/gpu/drm/i915/gt/uc/intel_guc.h intel_guc_send_and_receive(struct intel_guc · line 133

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +10 -0
+
+retry:
+    err = intel_guc_send_nb(guc, action, len);
+    if (unlikely(err == -EBUSY && loop)) {
+        if (likely(not_atomic)) {
+            if (msleep_interruptible(sleep_period_ms))
+                return -EINTR;
+            sleep_period_ms = sleep_period_ms << 1;
+        } else {
+            cpu_relax();

Potentially something we can change later, but if we're in atomic context we
can't keep looping without a timeout while we get -EBUSY, we need to bail
out early.

Quoted 6 lines
Quoted 2 lines

Eventually intel_guc_send_nb will pop with a different return code after 1 second I think if the GuC is hung.

I know, the point is that 1 second is too long in atomic context. This has to either complete quick in atomic or be forked off to a worker. Can fix as a follow up.

if we implement fallback to a worker, then maybe we can get rid of this busy loop completely, as it was supposed to be only needed for rare cases when CTBs were full due to unlikely congestion either caused by us (host driver didn't read G2H on time or filled up H2G faster than GuC could proceed it) or GuC actual failure (no H2G processing at all).

Michal

Quoted 986 lines

Daniele

Quoted 967 lines

Quoted 60 lines
Quoted 60 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +7 -0
+        }
+        goto retry;
+    }
+
+    return err;
+}
+

   static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
   {
       intel_guc_ct_event_handler(&guc->ct);

drivers/gpu/drm/i915/gt/uc/intel_guc.h +3 -0
@@ -202,6 +239,9 @@ static inline void intel_guc_disable_msg(struct
 intel_guc *guc, u32 mask)
    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);
+

   void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
   #endif

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +0 -0
index 83ec60ea3f89..28ff82c5be45 100644
@@ -928,6 +928,10 @@ static int ct_process_request(struct

intel_guc_ct *ct, struct ct_incoming_msg *r
       case INTEL_GUC_ACTION_DEFAULT:
           ret = intel_guc_to_host_process_recv_msg(guc, payload, len); break;
+    case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
+        ret = intel_guc_deregister_done_process_msg(guc, payload, +                                len);
+        break;
       default:
           ret = -EOPNOTSUPP;
           break;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
index 53b4a5eb4a85..a47b3813b4d0 100644
@@ -13,7 +13,9 @@
    #include "gt/intel_gt.h"
    #include "gt/intel_gt_irq.h"
    #include "gt/intel_gt_pm.h"
+#include "gt/intel_gt_requests.h"
    #include "gt/intel_lrc.h"
+#include "gt/intel_lrc_reg.h"
    #include "gt/intel_mocs.h"
    #include "gt/intel_ring.h"
@@ -85,6 +87,73 @@ static inline void clr_context_enabled(struct
 intel_context *ce)
               &ce->guc_sched_state_no_lock);
    }
+/*
+ * Below is a set of functions which control the GuC scheduling

state which
+ * require a lock, aside from the special case where the functions

Quoted 8 lines
Quoted 5 lines

are called
+ * from guc_lrc_desc_pin(). In that case it isn't possible for any other code
+ * path to be executing on the context.
+ */

Is there a reason to avoid taking the lock in guc_lrc_desc_pin, even if it
isn't strictly needed? I'd prefer to avoid the asymmetry in the locking

If this code is called from releasing a fence a circular dependency is created. Once we move the DRM scheduler the locking becomes way easier and we clean this up then. We already have task for locking clean up.

Quoted 2 lines

scheme if possible, as that might case trouble if thing change in the future.

Again this is temporary code, so I think we can live with this until the DRM scheduler lands.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+#define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER    BIT(0)
+#define SCHED_STATE_DESTROYED                BIT(1)
+static inline void init_sched_state(struct intel_context *ce)
+{
+    /* Only should be called from guc_lrc_desc_pin() */
+    atomic_set(&ce->guc_sched_state_no_lock, 0);
+    ce->guc_state.sched_state = 0;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 109

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static inline bool
+context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+    return (ce->guc_state.sched_state &
+        SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

No need for (). Below as well.

Yep.

Quoted 19 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static inline void
+set_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+    /* Only should be called from guc_lrc_desc_pin() */
+    ce->guc_state.sched_state |=

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 126

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+        SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
+}
+
+static inline void
+clr_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+    lockdep_assert_held(&ce->guc_state.lock);
+    ce->guc_state.sched_state =
+        (ce->guc_state.sched_state &
+         ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

nit: can also use

Quoted 3 lines

ce->guc_state.sched_state &=
    ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER

Yep.

Quoted 165 lines
Quoted 165 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +21 -0
+}
+
+static inline bool
+context_destroyed(struct intel_context *ce)
+{
+    return (ce->guc_state.sched_state & SCHED_STATE_DESTROYED);
+}
+
+static inline void
+set_context_destroyed(struct intel_context *ce)
+{
+    lockdep_assert_held(&ce->guc_state.lock);
+    ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
+}
+
+static inline bool context_guc_id_invalid(struct intel_context *ce)
+{
+    return (ce->guc_id == GUC_INVALID_LRC_ID);
+}
+
+static inline void set_context_guc_id_invalid(struct intel_context

*ce)

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -0
+{
+    ce->guc_id = GUC_INVALID_LRC_ID;
+}
+
+static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
+{
+    return &ce->engine->gt->uc.guc;
+}
+

   static inline struct i915_priolist *to_priolist(struct rb_node *rb) {
       return rb_entry(rb, struct i915_priolist, node);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +70 -2
@@ -155,6 +224,9 @@ static int guc_add_request(struct intel_guc
 *guc, struct i915_request *rq)
        int len = 0;
        bool enabled = context_enabled(ce);
+    GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
+    GEM_BUG_ON(context_guc_id_invalid(ce));
+
        if (!enabled) {
            action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
            action[len++] = ce->guc_id;
@@ -417,6 +489,10 @@ int intel_guc_submission_init(struct intel_guc
 *guc)
        xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
+    spin_lock_init(&guc->contexts_lock);
+    INIT_LIST_HEAD(&guc->guc_id_list);
+    ida_init(&guc->guc_ids);
+
        return 0;
    }
@@ -429,9 +505,303 @@ void intel_guc_submission_fini(struct
 intel_guc *guc)
        i915_sched_engine_put(guc->sched_engine);
    }
-static int guc_context_alloc(struct intel_context *ce)
+static inline void queue_request(struct i915_sched_engine
 *sched_engine,
+                 struct i915_request *rq,
+                 int prio)
    {
-    return lrc_alloc(ce, ce->engine);
+    GEM_BUG_ON(!list_empty(&rq->sched.link));
+    list_add_tail(&rq->sched.link,
+              i915_sched_lookup_priolist(sched_engine, prio));
+    set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+}
+
+static int guc_bypass_tasklet_submit(struct intel_guc *guc,
+                     struct i915_request *rq)
+{
+    int ret;
+
+    __i915_request_submit(rq);
+
+    trace_i915_request_in(rq, 0);
+
+    guc_set_lrc_tail(rq);
+    ret = guc_add_request(guc, rq);
+    if (ret == -EBUSY)
+        guc->stalled_request = rq;
+
+    return ret;
+}
+
+static void guc_submit_request(struct i915_request *rq)
+{
+    struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
+    struct intel_guc *guc = &rq->engine->gt->uc.guc;
+    unsigned long flags;
+
+    /* 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))
+        queue_request(sched_engine, rq, rq_prio(rq));
+    else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
+        tasklet_hi_schedule(&sched_engine->tasklet);
+
+    spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+#define GUC_ID_START    64    /* First 64 guc_ids reserved */
+static int new_guc_id(struct intel_guc *guc)
+{
+    return ida_simple_get(&guc->guc_ids, GUC_ID_START,
+                  GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+                  __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+}
+
+static void __release_guc_id(struct intel_guc *guc, struct
 intel_context *ce)
+{
+    if (!context_guc_id_invalid(ce)) {
+        ida_simple_remove(&guc->guc_ids, ce->guc_id);
+        reset_lrc_desc(guc, ce->guc_id);
+        set_context_guc_id_invalid(ce);
+    }
+    if (!list_empty(&ce->guc_id_link))
+        list_del_init(&ce->guc_id_link);
+}
+
+static void release_guc_id(struct intel_guc *guc, struct

intel_context *ce)

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +38 -0
+{
+    unsigned long flags;
+
+    spin_lock_irqsave(&guc->contexts_lock, flags);
+    __release_guc_id(guc, ce);
+    spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int steal_guc_id(struct intel_guc *guc)
+{
+    struct intel_context *ce;
+    int guc_id;
+
+    if (!list_empty(&guc->guc_id_list)) {
+        ce = list_first_entry(&guc->guc_id_list,
+                      struct intel_context,
+                      guc_id_link);
+
+        GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+        GEM_BUG_ON(context_guc_id_invalid(ce));
+
+        list_del_init(&ce->guc_id_link);
+        guc_id = ce->guc_id;
+        set_context_guc_id_invalid(ce);
+        return guc_id;
+    } else {
+        return -EAGAIN;
+    }
+}
+
+static int assign_guc_id(struct intel_guc *guc, u16 *out)
+{
+    int ret;
+
+    ret = new_guc_id(guc);
+    if (unlikely(ret < 0)) {
+        ret = steal_guc_id(guc);
+        if (ret < 0)
Quoted 9 lines
Quoted 5 lines

+            return ret;
+    }
+
+    *out = ret;
+    return 0;

Is it worth adding spinlock_held asserts for guc->contexts_lock in these ID
functions? Doubles up as a documentation of what locking we expect.

Never a bad idea to have asserts in the code. Will add.

Quoted 118 lines
Quoted 118 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +29 -0
+}
+
+#define PIN_GUC_ID_TRIES    4
+static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+    int ret = 0;
+    unsigned long flags, tries = PIN_GUC_ID_TRIES;
+
+    GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+
+try_again:
+    spin_lock_irqsave(&guc->contexts_lock, flags);
+
+    if (context_guc_id_invalid(ce)) {
+        ret = assign_guc_id(guc, &ce->guc_id);
+        if (ret)
+            goto out_unlock;
+        ret = 1;    /* Indidcates newly assigned guc_id */
+    }
+    if (!list_empty(&ce->guc_id_link))
+        list_del_init(&ce->guc_id_link);
+    atomic_inc(&ce->guc_id_ref);
+
+out_unlock:
+    spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+    /*
+     * -EAGAIN indicates no guc_ids are available, let's retire any
+     * outstanding requests to see if that frees up a guc_id. If

the first
+     * retire didn't help, insert a sleep with the timeslice duration before
+     * attempting to retire more requests. Double the sleep period each
+     * subsequent pass before finally giving up. The sleep period has max of
+     * 100ms and minimum of 1ms.
+     */
+    if (ret == -EAGAIN && --tries) {
+        if (PIN_GUC_ID_TRIES - tries > 1) {
+            unsigned int timeslice_shifted =
+                ce->engine->props.timeslice_duration_ms << +                (PIN_GUC_ID_TRIES - tries - 2);
+            unsigned int max = min_t(unsigned int, 100, +                         timeslice_shifted);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -0
+
+            msleep(max_t(unsigned int, max, 1));
+        }
+        intel_gt_retire_requests(guc_to_gt(guc));
+        goto try_again;
+    }
+
+    return ret;
+}
+
+static void unpin_guc_id(struct intel_guc *guc, struct

intel_context *ce)

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +23 -0
+{
+    unsigned long flags;
+
+    GEM_BUG_ON(atomic_read(&ce->guc_id_ref) < 0);
+
+    spin_lock_irqsave(&guc->contexts_lock, flags);
+    if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id_link) &&
+        !atomic_read(&ce->guc_id_ref))
+        list_add_tail(&ce->guc_id_link, &guc->guc_id_list);
+    spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int __guc_action_register_context(struct intel_guc *guc,
+                     u32 guc_id,
+                     u32 offset)
+{
+    u32 action[] = {
+        INTEL_GUC_ACTION_REGISTER_CONTEXT,
+        guc_id,
+        offset,
+    };
+
+    return intel_guc_send_busy_loop(guc, action,

ARRAY_SIZE(action), true);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+}
+
+static int register_context(struct intel_context *ce)
+{
+    struct intel_guc *guc = ce_to_guc(ce);
+    u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
+        ce->guc_id * sizeof(struct guc_lrc_desc);
+
+    return __guc_action_register_context(guc, ce->guc_id, offset);
+}
+
+static int __guc_action_deregister_context(struct intel_guc *guc,
+                       u32 guc_id)
+{
+    u32 action[] = {
+        INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
+        guc_id,
+    };
+
+    return intel_guc_send_busy_loop(guc, action,

ARRAY_SIZE(action), true);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+}
+
+static int deregister_context(struct intel_context *ce, u32 guc_id)
+{
+    struct intel_guc *guc = ce_to_guc(ce);
+
+    return __guc_action_deregister_context(guc, guc_id);
+}
+
+static intel_engine_mask_t adjust_engine_mask(u8 class,

intel_engine_mask_t mask)
+{
+    switch (class) {
+    case RENDER_CLASS:
+        return mask >> RCS0;
+    case VIDEO_ENHANCEMENT_CLASS:

Quoted 8 lines
Quoted 7 lines

+        return mask >> VECS0;
+    case VIDEO_DECODE_CLASS:
+        return mask >> VCS0;
+    case COPY_ENGINE_CLASS:
+        return mask >> BCS0;
+    default:
+        GEM_BUG_ON("Invalid Class");

we usually use MISSING_CASE for this type of errors.

As soon as start talking in logical space (series immediatly after this gets merged) this function gets deleted but will fix.

Quoted 14 lines
Quoted 14 lines

+        return 0;
+    }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -0
+}
+
+static void guc_context_policy_init(struct intel_engine_cs *engine,
+                    struct guc_lrc_desc *desc)
+{
+    desc->policy_flags = 0;
+
+    desc->execution_quantum =

CONTEXT_POLICY_DEFAULT_EXECUTION_QUANTUM_US;
+    desc->preemption_timeout =
CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
+}

Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 748

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+
+static int guc_lrc_desc_pin(struct intel_context *ce)
+{
+    struct intel_runtime_pm *runtime_pm =
+        &ce->engine->gt->i915->runtime_pm;

If you move this after the engine var below you can skip the ce->engine jump. Also, you can shorten the pointer chasing by using engine->uncore->rpm.

Sure.

Quoted 31 lines
Quoted 31 lines

+    struct intel_engine_cs *engine = ce->engine;
+    struct intel_guc *guc = &engine->gt->uc.guc;
+    u32 desc_idx = ce->guc_id;
+    struct guc_lrc_desc *desc;
+    bool context_registered;
+    intel_wakeref_t wakeref;
+    int ret = 0;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+
+    GEM_BUG_ON(!engine->mask);
+
+    /*
+     * Ensure LRC + CT vmas are is same region as write barrier is

done
+     * based on CT vma region.
+     */
+    GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) != +           i915_gem_object_is_lmem(ce->ring->vma->obj));

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +14 -0
+
+    context_registered = lrc_desc_registered(guc, desc_idx);
+
+    reset_lrc_desc(guc, desc_idx);
+    set_lrc_desc_registered(guc, desc_idx, ce);
+
+    desc = __get_lrc_desc(guc, desc_idx);
+    desc->engine_class = engine_class_to_guc_class(engine->class);
+    desc->engine_submit_mask = adjust_engine_mask(engine->class,
+                              engine->mask);
+    desc->hw_context_desc = ce->lrc.lrca;
+    desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+    desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+    guc_context_policy_init(engine, desc);
Quoted 9 lines
Quoted 7 lines

+    init_sched_state(ce);
+
+    /*
+     * The context_lookup xarray is used to determine if the hardware +     * context is currently registered. There are two cases in which it
+     * could be regisgered either the guc_id has been stole from from

typo regisgered

John got this one. Fixed.

Quoted 26 lines
Quoted 26 lines

+     * another context or the lrc descriptor address of this context has
+     * changed. In either case the context needs to be deregistered with the
+     * GuC before registering this context.
+     */
+    if (context_registered) {
+        set_context_wait_for_deregister_to_register(ce); +        intel_context_get(ce);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+
+        /*
+         * 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);
+    } else {
+        with_intel_runtime_pm(runtime_pm, wakeref)
+            ret = register_context(ce);
+    }
+
+    return ret;

   }
   static int guc_context_pre_pin(struct intel_context *ce,

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
@@ -443,36 +813,139 @@ static int guc_context_pre_pin(struct
 intel_context *ce,
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 818

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
    static int guc_context_pin(struct intel_context *ce, void *vaddr)
    {
+    if (i915_ggtt_offset(ce->state) !=
+        (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
+        set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Shouldn't this be set after lrc_pin()? I can see ce->lrc.lrca being re-assigned in there; it looks like it can only change if the context is new, but for future-proofing IMO better to re-order here.

No this is right. ce->state is assigned in lr_pre_pin and ce->lrc.lrca is assigned in lrc_pin. To catch the change you have to check between those two functions.

Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 820

Quoted 2 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+
        return lrc_pin(ce, ce->engine, vaddr);

Could use a comment to say that the GuC context pinning call is delayed until request alloc and to look at the comment in the latter function for
details (or move the explanation here and refer here from request alloc).

Yes.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
    }
+static void guc_context_unpin(struct intel_context *ce)
+{
+    struct intel_guc *guc = ce_to_guc(ce);
+
+    unpin_guc_id(guc, ce);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 833

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+    lrc_unpin(ce);
+}
+
+static void guc_context_post_unpin(struct intel_context *ce)
+{
+    lrc_post_unpin(ce);

why do we need this function? we can just pass lrc_post_unpin to the ops (like we already do).

We don't before style reasons I think having a GuC specific wrapper is correct.

Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +16 -0
+}
+
+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;
+
+    GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
+    GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
+
+    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);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce,

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+}
+
+static void guc_context_destroy(struct kref *kref)
+{
+    struct intel_context *ce = container_of(kref, typeof(*ce), ref);
+    struct intel_runtime_pm *runtime_pm =
 &ce->engine->gt->i915->runtime_pm;

same as above, going through engine->uncore->rpm is shorter

Sure.

Quoted 14 lines
Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+    struct intel_guc *guc = &ce->engine->gt->uc.guc;
+    intel_wakeref_t wakeref;
+    unsigned long flags;
+
+    /*
+     * If the guc_id is invalid this context has been stolen and we
Quoted 8 lines

can free
+     * it immediately. Also can be freed immediately if the context is not
+     * registered with the GuC.
+     */
+    if (context_guc_id_invalid(ce) ||
+        !lrc_desc_registered(guc, ce->guc_id)) {
+        release_guc_id(guc, ce);

Quoted 6 lines

it feels a bit weird that we call release_guc_id in the case where the id is
invalid. The code handles it fine, but still the flow doesn't feel clean.
Not a blocker.

I understand. Let's see if this can get cleaned up at some point.

Quoted 18 lines
Quoted 10 lines

+        lrc_destroy(kref);
+        return;
+    }
+
+    /*
+     * We have to acquire the context spinlock and check guc_id again, if it
+     * is valid it hasn't been stolen and needs to be deregistered. We +     * delete this context from the list of unpinned guc_ids available to

Quoted 8 lines

+     * stole to seal a race with guc_lrc_desc_pin(). When the G2H CTB +     * returns indicating this context has been deregistered the guc_id is
+     * returned to the pool of available guc_ids.
+     */
+    spin_lock_irqsave(&guc->contexts_lock, flags);
+    if (context_guc_id_invalid(ce)) {
+        __release_guc_id(guc, ce);

Quoted 5 lines

But here the call to __release_guc_id is unneded, right? the ce doesn't own
the ID anymore and both the steal and the release function already clean ce->guc_id_link, so there should be nothing left to clean.

This is dead code. Will delete.

Quoted 146 lines
Quoted 146 lines

+        spin_unlock_irqrestore(&guc->contexts_lock, flags); +        lrc_destroy(kref);
+        return;
+    }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+
+    if (!list_empty(&ce->guc_id_link))
+        list_del_init(&ce->guc_id_link);
+    spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+    /*
+     * We defer GuC context deregistration until the context is

destroyed
+     * in order to save on CTBs. With this optimization ideally we only need
+     * 1 CTB to register the context during the first pin and 1 CTB to +     * deregister the context when the context is destroyed. Without this
+     * optimization, a CTB would be needed every pin & unpin. +     *
+     * XXX: Need to acqiure the runtime wakeref as this can be triggered
+     * from context_free_worker when not runtime wakeref is held. +     * guc_lrc_desc_unpin requires the runtime as a GuC register is written
+     * in H2G CTB to deregister the context. A future patch may defer this
+     * H2G CTB if the runtime wakeref is zero.
+     */
+    with_intel_runtime_pm(runtime_pm, wakeref)
+        guc_lrc_desc_unpin(ce);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static int guc_context_alloc(struct intel_context *ce)
+{
+    return lrc_alloc(ce, ce->engine);
+}
+

   static const struct intel_context_ops guc_context_ops = { .alloc = guc_context_alloc,
       .pre_pin = guc_context_pre_pin,
       .pin = guc_context_pin,
-    .unpin = lrc_unpin,
-    .post_unpin = lrc_post_unpin,
+    .unpin = guc_context_unpin,
+    .post_unpin = guc_context_post_unpin,
       .enter = intel_context_enter_engine,
       .exit = intel_context_exit_engine,
       .reset = lrc_reset,
-    .destroy = lrc_destroy,
+    .destroy = guc_context_destroy,
   };

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -1
-static int guc_request_alloc(struct i915_request *request)
+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);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+}
+
+static int guc_request_alloc(struct i915_request *rq)
+{
+    struct intel_context *ce = rq->context;
+    struct intel_guc *guc = ce_to_guc(ce);

       int ret;
-    GEM_BUG_ON(!intel_context_is_pinned(request->context)); +    GEM_BUG_ON(!intel_context_is_pinned(rq->context));
       /*
        * Flush enough space to reduce the likelihood of waiting after
        * we start building the request - in which case we will just
        * have to repeat work.
        */
-    request->reserved_space += GUC_REQUEST_SIZE;
+    rq->reserved_space += GUC_REQUEST_SIZE;
       /*
        * Note that after this point, we have committed to using

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -36
@@ -483,56 +956,47 @@ static int guc_request_alloc(struct
 i915_request *request)
         */
        /* Unconditionally invalidate GPU caches and TLBs. */
-    ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
+    ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
        if (ret)
            return ret;
-    request->reserved_space -= GUC_REQUEST_SIZE;
-    return 0;
-}
+    rq->reserved_space -= GUC_REQUEST_SIZE;
-static inline void queue_request(struct i915_sched_engine
 *sched_engine,
-                 struct i915_request *rq,
-                 int prio)
-{
-    GEM_BUG_ON(!list_empty(&rq->sched.link));
-    list_add_tail(&rq->sched.link,
-              i915_sched_lookup_priolist(sched_engine, prio));
-    set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-}
-
-static int guc_bypass_tasklet_submit(struct intel_guc *guc,
-                     struct i915_request *rq)
-{
-    int ret;
-
-    __i915_request_submit(rq);
-
-    trace_i915_request_in(rq, 0);
-
-    guc_set_lrc_tail(rq);
-    ret = guc_add_request(guc, rq);
-    if (ret == -EBUSY)
-        guc->stalled_request = rq;
-
-    return ret;
-}
-
-static void guc_submit_request(struct i915_request *rq)
-{
-    struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
-    struct intel_guc *guc = &rq->engine->gt->uc.guc;
-    unsigned long flags;
+    /*
+     * Call pin_guc_id here rather than in the pinning step as with
+     * dma_resv, contexts can be repeatedly pinned / unpinned

trashing the
+     * guc_ids and creating horrible race conditions. This is especially bad
+     * when guc_ids are being stolen due to over subscription. By the time
+     * this function is reached, it is guaranteed that the guc_id will be
+     * persistent until the generated request is retired. Thus, sealing these
+     * race conditions. It is still safe to fail here if guc_ids are +     * exhausted and return -EAGAIN to the user indicating that they can try
+     * again in the future.
+     *
+     * There is no need for a lock here as the timeline mutex ensures at
+     * most one context can be executing this code path at once. The +     * guc_id_ref is incremented once for every request in flight and +     * decremented on each retire. When it is zero, a lock around the +     * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
+     */
+    if (atomic_add_unless(&ce->guc_id_ref, 1, 0))

Quoted 9 lines
Quoted 7 lines

+        return 0;
-    /* Will be called from irq-context when using foreign fences. */ -    spin_lock_irqsave(&sched_engine->lock, flags);
+    ret = pin_guc_id(guc, ce);    /* returns 1 if new guc_id assigned */
+    if (unlikely(ret < 0))
+        return ret;;

typo ";;"

Yep.

Quoted 15 lines
Quoted 6 lines

+    if (context_needs_register(ce, !!ret)) {
+        ret = guc_lrc_desc_pin(ce);
+        if (unlikely(ret)) {    /* unwind */
+            atomic_dec(&ce->guc_id_ref);
+            unpin_guc_id(guc, ce);
+            return ret;

Quoted 8 lines

+        }
+    }
-    if (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); +    clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Quoted 3 lines

Might be worth moving the pinning to a separate function to keep the request_alloc focused on the request. Can be done as a follow up.

Sure. Let me see how that looks in a follow up.

Quoted 148 lines
Quoted 148 lines

-    spin_unlock_irqrestore(&sched_engine->lock, flags); +    return 0;
   }
   static void sanitize_hwsp(struct intel_engine_cs *engine)

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +21 -0
@@ -606,6 +1070,46 @@ static void guc_set_default_submission(struct
 intel_engine_cs *engine)
        engine->submit_request = guc_submit_request;
    }
+static inline void guc_kernel_context_pin(struct intel_guc *guc,
+                      struct intel_context *ce)
+{
+    if (context_guc_id_invalid(ce))
+        pin_guc_id(guc, ce);
+    guc_lrc_desc_pin(ce);
+}
+
+static inline void guc_init_lrc_mapping(struct intel_guc *guc)
+{
+    struct intel_gt *gt = guc_to_gt(guc);
+    struct intel_engine_cs *engine;
+    enum intel_engine_id id;
+
+    /* make sure all descriptors are clean... */
+    xa_destroy(&guc->context_lookup);
+
+    /*
+     * Some contexts might have been pinned before we enabled GuC
+     * submission, so we need to add them to the GuC bookeeping.
+     * Also, after a reset the GuC we want to make sure that the

information
+     * shared with GuC is properly reset. The kernel lrcs are not attached
+     * to the gem_context, so they need to be added separately. +     *
+     * Note: we purposely do not check the error return of +     * guc_lrc_desc_pin, because that function can only fail in two cases.
+     * One, if there aren't enough free IDs, but we're guaranteed to have
+     * enough here (we're either only pinning a handful of lrc on first boot
+     * or we're re-pinning lrcs that were already pinned before the reset).
+     * Two, if the GuC has died and CTBs can't make forward progress. +     * Presumably, the GuC should be alive as this function is called on
+     * driver load or after a reset. Even if it is dead, another full GPU
+     * reset will be triggered and this function would be called again.
+     */

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+
+    for_each_engine(engine, gt, id)
+        if (engine->kernel_context)
+            guc_kernel_context_pin(guc, engine->kernel_context);
+}
+

   static void guc_release(struct intel_engine_cs *engine) {
       engine->sanitize = NULL; /* no longer in control, nothing to sanitize */

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
@@ -718,6 +1222,7 @@ int intel_guc_submission_setup(struct
 intel_engine_cs *engine)
    void intel_guc_submission_enable(struct intel_guc *guc)
    {
+    guc_init_lrc_mapping(guc);
    }
    void intel_guc_submission_disable(struct intel_guc *guc)
@@ -743,3 +1248,62 @@ void intel_guc_submission_init_early(struct
 intel_guc *guc)
    {
        guc->submission_selected = __guc_submission_selected(guc);

   }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +45 -0
+
+static inline struct intel_context *
+g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
+{
+    struct intel_context *ce;
+
+    if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+        drm_dbg(&guc_to_gt(guc)->i915->drm,
+            "Invalid desc_idx %u", desc_idx);
+        return NULL;
+    }
+
+    ce = __get_context(guc, desc_idx);
+    if (unlikely(!ce)) {
+        drm_dbg(&guc_to_gt(guc)->i915->drm,
+            "Context is NULL, desc_idx %u", desc_idx);
+        return NULL;
+    }
+
+    return ce;
+}
+
+int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
+                      const u32 *msg,
+                      u32 len)
+{
+    struct intel_context *ce;
+    u32 desc_idx = msg[0];
+
+    if (unlikely(len < 1)) {
+        drm_dbg(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
+        return -EPROTO;
+    }
+
+    ce = g2h_context_lookup(guc, desc_idx);
+    if (unlikely(!ce))
+        return -EPROTO;
+
+    if (context_wait_for_deregister_to_register(ce)) {
+        struct intel_runtime_pm *runtime_pm =
+            &ce->engine->gt->i915->runtime_pm;
+        intel_wakeref_t wakeref;
+
+        /*
+         * Previous owner of this guc_id has been deregistered, now

safe
+         * register this context.
+         */
+        with_intel_runtime_pm(runtime_pm, wakeref) +            register_context(ce);
+        clr_context_wait_for_deregister_to_register(ce); +        intel_context_put(ce);
+    } else if (context_destroyed(ce)) {
+        /* Context has been destroyed */
+        release_guc_id(guc, ce);
+        lrc_destroy(&ce->ref);
+    }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+
+    return 0;
+}
drivers/gpu/drm/i915/i915_reg.h +0 -0
index 943fe485c662..204c95c39353 100644
@@ -4142,6 +4142,7 @@ enum {

       FAULT_AND_CONTINUE /* Unsupported */
   };
+#define CTX_GTT_ADDRESS_MASK GENMASK(31, 12)
   #define GEN8_CTX_VALID (1 << 0)
   #define GEN8_CTX_FORCE_PD_RESTORE (1 << 1)
   #define GEN8_CTX_FORCE_RESTORE (1 << 2)

drivers/gpu/drm/i915/i915_request.c +0 -0
index 86b4c9f2613d..b48c4905d3fc 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c bool i915_request_retire(struct i915_request *rq) · line 410

Quoted 7 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
@@ -407,6 +407,7 @@ bool i915_request_retire(struct i915_request *rq)
         */
        if (!list_empty(&rq->sched.link))
            remove_from_engine(rq);
+    atomic_dec(&rq->context->guc_id_ref);

Does this work/make sense  if GuC is disabled?

Quoted 11 lines

It doesn't matter if the GuC is disabled as this var isn't used if it is. A following patch adds a vfunc here and pushes this in the GuC backend.

Matt

Quoted 5 lines

Daniele

Quoted 3 lines
drivers/gpu/drm/i915/i915_request.c +0 -0
        GEM_BUG_ON(!llist_empty(&rq->execute_cb));

       __list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */

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

#16 lore raw

Re: [Intel-gfx] [PATCH 06/51] drm/i915/guc: Implement GuC context operations for new inteface

Quoted 163 lines · Michal Wajdeczko
Quoted 160 lines · Daniele Ceraolo Spurio
Quoted 156 lines · Matthew Brost
Quoted 144 lines · Daniele Ceraolo Spurio
Quoted 142 lines · Matthew Brost

Implement GuC context operations which includes GuC specific operations alloc, pin, unpin, and destroy.

v2:
   (Daniel Vetter)
    - Use msleep_interruptible rather than cond_resched in busy loop (Michal)
    - Remove C++ style comment

---
   drivers/gpu/drm/i915/gt/intel_context.c       |   5 + drivers/gpu/drm/i915/gt/intel_context_types.h |  22 +- drivers/gpu/drm/i915/gt/intel_lrc_reg.h       |   1 - drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  40 ++ drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   4 + .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 666
++++++++++++++++--
   drivers/gpu/drm/i915/i915_reg.h               |   1 + drivers/gpu/drm/i915/i915_request.c           |   1 +

   8 files changed, 685 insertions(+), 55 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +5 -0
index bd63813c8a80..32fd6647154b 100644
@@ -384,6 +384,11 @@ intel_context_init(struct intel_context *ce,
 struct intel_engine_cs *engine)
        mutex_init(&ce->pin_mutex);
+    spin_lock_init(&ce->guc_state.lock);
+
+    ce->guc_id = GUC_INVALID_LRC_ID;
+    INIT_LIST_HEAD(&ce->guc_id_link);
+
        i915_active_init(&ce->active,
                 __intel_context_active, __intel_context_retire, 0);
    }
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -0
index 6d99631d19b9..606c480aec26 100644
@@ -96,6 +96,7 @@ struct intel_context {
    #define CONTEXT_BANNED            6
    #define CONTEXT_FORCE_SINGLE_SUBMISSION    7
    #define CONTEXT_NOPREEMPT        8
+#define CONTEXT_LRCA_DIRTY        9
        struct {
            u64 timeout_us;
@@ -138,14 +139,29 @@ struct intel_context {

       u8 wa_bb_page; /* if set, page num reserved for context workarounds */
+    struct {
+        /** lock: protects everything in guc_state */
+        spinlock_t lock;
+        /**
+         * sched_state: scheduling state of this context using GuC +         * submission
+         */
+        u8 sched_state;
+    } guc_state;
+
       /* GuC scheduling state flags that do not require a lock. */ atomic_t guc_sched_state_no_lock;
+    /* GuC LRC descriptor ID */
+    u16 guc_id;

drivers/gpu/drm/i915/gt/intel_context_types.h +4 -0
+
+    /* GuC LRC descriptor reference count */
+    atomic_t guc_id_ref;
+

       /*
-     * GuC LRC descriptor ID - Not assigned in this patch but future patches
-     * in the series will.
+     * GuC ID link - in list when unpinned but guc_id still valid in GuC
        */
-    u16 guc_id;
+    struct list_head guc_id_link;
   };
   #endif /* __INTEL_CONTEXT_TYPES__ */

drivers/gpu/drm/i915/gt/intel_lrc_reg.h +0 -1
index 41e5350a7a05..49d4857ad9b7 100644
@@ -87,7 +87,6 @@
    #define GEN11_CSB_WRITE_PTR_MASK    (GEN11_CSB_PTR_MASK << 0)
    #define MAX_CONTEXT_HW_ID    (1 << 21) /* exclusive */
-#define MAX_GUC_CONTEXT_HW_ID    (1 << 20) /* exclusive */
    #define GEN11_MAX_CONTEXT_HW_ID    (1 << 11) /* exclusive */
    /* in Gen12 ID 0x7FF is reserved to indicate idle */
    #define GEN12_MAX_CONTEXT_HW_ID    (GEN11_MAX_CONTEXT_HW_ID - 1)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +3 -0
index 8c7b92f699f1..30773cd699f5 100644
@@ -7,6 +7,7 @@
    #define _INTEL_GUC_H_
    #include <linux/xarray.h>
+#include <linux/delay.h>
    #include "intel_uncore.h"
    #include "intel_guc_fw.h"
@@ -44,6 +45,14 @@ struct intel_guc {
            void (*disable)(struct intel_guc *guc);
        } interrupts;
+    /*
+     * contexts_lock protects the pool of free guc ids and a linked

list of
+     * guc ids available to be stolen
+     */
+    spinlock_t contexts_lock;
+    struct ida guc_ids;
+    struct list_head guc_id_list;
+
       bool submission_selected;
       struct i915_vma *ads_vma;

drivers/gpu/drm/i915/gt/uc/intel_guc.h +18 -0
@@ -101,6 +110,34 @@ intel_guc_send_and_receive(struct intel_guc
 *guc, const u32 *action, u32 len,
                     response_buf, response_buf_size, 0);
    }
+static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
+                       const u32 *action,
+                       u32 len,
+                       bool loop)
+{
+    int err;
+    unsigned int sleep_period_ms = 1;
+    bool not_atomic = !in_atomic() && !irqs_disabled();
+
+    /* No sleeping with spin locks, just busy loop */
+    might_sleep_if(loop && not_atomic);
+
+retry:
+    err = intel_guc_send_nb(guc, action, len);
+    if (unlikely(err == -EBUSY && loop)) {
+        if (likely(not_atomic)) {
+            if (msleep_interruptible(sleep_period_ms))
+                return -EINTR;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc.h intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len, · line 133

Quoted 3 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +3 -0
+            sleep_period_ms = sleep_period_ms << 1;
+        } else {
+            cpu_relax();

Potentially something we can change later, but if we're in atomic context we
can't keep looping without a timeout while we get -EBUSY, we need to bail
out early.

Eventually intel_guc_send_nb will pop with a different return code after 1 second I think if the GuC is hung.

Quoted 7 lines
Quoted 3 lines

I know, the point is that 1 second is too long in atomic context. This has to either complete quick in atomic or be forked off to a worker. Can fix as a follow up.

if we implement fallback to a worker, then maybe we can get rid of this busy loop completely, as it was supposed to be only needed for rare cases when CTBs were full due to unlikely congestion either caused by us

Right. That last project I worked on we had a circular buffer to communicate with the HW and fell back to kthread (or workqueue?) when the buffer the was full. We likely could do something like that but if we have malloc memory that gets tricky if we are atomic contexts. This is good one to write down and revisit a bit later.

Matt

Quoted 991 lines

(host driver didn't read G2H on time or filled up H2G faster than GuC could proceed it) or GuC actual failure (no H2G processing at all).

Michal

Quoted 986 lines

Daniele

Quoted 967 lines

Quoted 60 lines
Quoted 60 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +7 -0
+        }
+        goto retry;
+    }
+
+    return err;
+}
+

   static inline void intel_guc_to_host_event_handler(struct intel_guc *guc)
   {
       intel_guc_ct_event_handler(&guc->ct);

drivers/gpu/drm/i915/gt/uc/intel_guc.h +3 -0
@@ -202,6 +239,9 @@ static inline void intel_guc_disable_msg(struct
 intel_guc *guc, u32 mask)
    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);
+

   void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
   #endif

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +0 -0
index 83ec60ea3f89..28ff82c5be45 100644
@@ -928,6 +928,10 @@ static int ct_process_request(struct

intel_guc_ct *ct, struct ct_incoming_msg *r
       case INTEL_GUC_ACTION_DEFAULT:
           ret = intel_guc_to_host_process_recv_msg(guc, payload, len); break;
+    case INTEL_GUC_ACTION_DEREGISTER_CONTEXT_DONE:
+        ret = intel_guc_deregister_done_process_msg(guc, payload, +                                len);
+        break;
       default:
           ret = -EOPNOTSUPP;
           break;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
index 53b4a5eb4a85..a47b3813b4d0 100644
@@ -13,7 +13,9 @@
    #include "gt/intel_gt.h"
    #include "gt/intel_gt_irq.h"
    #include "gt/intel_gt_pm.h"
+#include "gt/intel_gt_requests.h"
    #include "gt/intel_lrc.h"
+#include "gt/intel_lrc_reg.h"
    #include "gt/intel_mocs.h"
    #include "gt/intel_ring.h"
@@ -85,6 +87,73 @@ static inline void clr_context_enabled(struct
 intel_context *ce)
               &ce->guc_sched_state_no_lock);
    }
+/*
+ * Below is a set of functions which control the GuC scheduling

state which
+ * require a lock, aside from the special case where the functions

Quoted 8 lines
Quoted 5 lines

are called
+ * from guc_lrc_desc_pin(). In that case it isn't possible for any other code
+ * path to be executing on the context.
+ */

Is there a reason to avoid taking the lock in guc_lrc_desc_pin, even if it
isn't strictly needed? I'd prefer to avoid the asymmetry in the locking

If this code is called from releasing a fence a circular dependency is created. Once we move the DRM scheduler the locking becomes way easier and we clean this up then. We already have task for locking clean up.

Quoted 2 lines

scheme if possible, as that might case trouble if thing change in the future.

Again this is temporary code, so I think we can live with this until the DRM scheduler lands.

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+#define SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER    BIT(0)
+#define SCHED_STATE_DESTROYED                BIT(1)
+static inline void init_sched_state(struct intel_context *ce)
+{
+    /* Only should be called from guc_lrc_desc_pin() */
+    atomic_set(&ce->guc_sched_state_no_lock, 0);
+    ce->guc_state.sched_state = 0;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 109

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static inline bool
+context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+    return (ce->guc_state.sched_state &
+        SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

No need for (). Below as well.

Yep.

Quoted 19 lines
Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static inline void
+set_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+    /* Only should be called from guc_lrc_desc_pin() */
+    ce->guc_state.sched_state |=

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static inline void clr_context_enabled(struct intel_context *ce) · line 126

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+        SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER;
+}
+
+static inline void
+clr_context_wait_for_deregister_to_register(struct intel_context *ce)
+{
+    lockdep_assert_held(&ce->guc_state.lock);
+    ce->guc_state.sched_state =
+        (ce->guc_state.sched_state &
+         ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER);

nit: can also use

Quoted 3 lines

ce->guc_state.sched_state &=
    ~SCHED_STATE_WAIT_FOR_DEREGISTER_TO_REGISTER

Yep.

Quoted 165 lines
Quoted 165 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +21 -0
+}
+
+static inline bool
+context_destroyed(struct intel_context *ce)
+{
+    return (ce->guc_state.sched_state & SCHED_STATE_DESTROYED);
+}
+
+static inline void
+set_context_destroyed(struct intel_context *ce)
+{
+    lockdep_assert_held(&ce->guc_state.lock);
+    ce->guc_state.sched_state |= SCHED_STATE_DESTROYED;
+}
+
+static inline bool context_guc_id_invalid(struct intel_context *ce)
+{
+    return (ce->guc_id == GUC_INVALID_LRC_ID);
+}
+
+static inline void set_context_guc_id_invalid(struct intel_context

*ce)

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -0
+{
+    ce->guc_id = GUC_INVALID_LRC_ID;
+}
+
+static inline struct intel_guc *ce_to_guc(struct intel_context *ce)
+{
+    return &ce->engine->gt->uc.guc;
+}
+

   static inline struct i915_priolist *to_priolist(struct rb_node *rb) {
       return rb_entry(rb, struct i915_priolist, node);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +70 -2
@@ -155,6 +224,9 @@ static int guc_add_request(struct intel_guc
 *guc, struct i915_request *rq)
        int len = 0;
        bool enabled = context_enabled(ce);
+    GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
+    GEM_BUG_ON(context_guc_id_invalid(ce));
+
        if (!enabled) {
            action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_SET;
            action[len++] = ce->guc_id;
@@ -417,6 +489,10 @@ int intel_guc_submission_init(struct intel_guc
 *guc)
        xa_init_flags(&guc->context_lookup, XA_FLAGS_LOCK_IRQ);
+    spin_lock_init(&guc->contexts_lock);
+    INIT_LIST_HEAD(&guc->guc_id_list);
+    ida_init(&guc->guc_ids);
+
        return 0;
    }
@@ -429,9 +505,303 @@ void intel_guc_submission_fini(struct
 intel_guc *guc)
        i915_sched_engine_put(guc->sched_engine);
    }
-static int guc_context_alloc(struct intel_context *ce)
+static inline void queue_request(struct i915_sched_engine
 *sched_engine,
+                 struct i915_request *rq,
+                 int prio)
    {
-    return lrc_alloc(ce, ce->engine);
+    GEM_BUG_ON(!list_empty(&rq->sched.link));
+    list_add_tail(&rq->sched.link,
+              i915_sched_lookup_priolist(sched_engine, prio));
+    set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
+}
+
+static int guc_bypass_tasklet_submit(struct intel_guc *guc,
+                     struct i915_request *rq)
+{
+    int ret;
+
+    __i915_request_submit(rq);
+
+    trace_i915_request_in(rq, 0);
+
+    guc_set_lrc_tail(rq);
+    ret = guc_add_request(guc, rq);
+    if (ret == -EBUSY)
+        guc->stalled_request = rq;
+
+    return ret;
+}
+
+static void guc_submit_request(struct i915_request *rq)
+{
+    struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
+    struct intel_guc *guc = &rq->engine->gt->uc.guc;
+    unsigned long flags;
+
+    /* 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))
+        queue_request(sched_engine, rq, rq_prio(rq));
+    else if (guc_bypass_tasklet_submit(guc, rq) == -EBUSY)
+        tasklet_hi_schedule(&sched_engine->tasklet);
+
+    spin_unlock_irqrestore(&sched_engine->lock, flags);
+}
+
+#define GUC_ID_START    64    /* First 64 guc_ids reserved */
+static int new_guc_id(struct intel_guc *guc)
+{
+    return ida_simple_get(&guc->guc_ids, GUC_ID_START,
+                  GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+                  __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+}
+
+static void __release_guc_id(struct intel_guc *guc, struct
 intel_context *ce)
+{
+    if (!context_guc_id_invalid(ce)) {
+        ida_simple_remove(&guc->guc_ids, ce->guc_id);
+        reset_lrc_desc(guc, ce->guc_id);
+        set_context_guc_id_invalid(ce);
+    }
+    if (!list_empty(&ce->guc_id_link))
+        list_del_init(&ce->guc_id_link);
+}
+
+static void release_guc_id(struct intel_guc *guc, struct

intel_context *ce)

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +38 -0
+{
+    unsigned long flags;
+
+    spin_lock_irqsave(&guc->contexts_lock, flags);
+    __release_guc_id(guc, ce);
+    spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int steal_guc_id(struct intel_guc *guc)
+{
+    struct intel_context *ce;
+    int guc_id;
+
+    if (!list_empty(&guc->guc_id_list)) {
+        ce = list_first_entry(&guc->guc_id_list,
+                      struct intel_context,
+                      guc_id_link);
+
+        GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+        GEM_BUG_ON(context_guc_id_invalid(ce));
+
+        list_del_init(&ce->guc_id_link);
+        guc_id = ce->guc_id;
+        set_context_guc_id_invalid(ce);
+        return guc_id;
+    } else {
+        return -EAGAIN;
+    }
+}
+
+static int assign_guc_id(struct intel_guc *guc, u16 *out)
+{
+    int ret;
+
+    ret = new_guc_id(guc);
+    if (unlikely(ret < 0)) {
+        ret = steal_guc_id(guc);
+        if (ret < 0)
Quoted 9 lines
Quoted 5 lines

+            return ret;
+    }
+
+    *out = ret;
+    return 0;

Is it worth adding spinlock_held asserts for guc->contexts_lock in these ID
functions? Doubles up as a documentation of what locking we expect.

Never a bad idea to have asserts in the code. Will add.

Quoted 118 lines
Quoted 118 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +29 -0
+}
+
+#define PIN_GUC_ID_TRIES    4
+static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
+{
+    int ret = 0;
+    unsigned long flags, tries = PIN_GUC_ID_TRIES;
+
+    GEM_BUG_ON(atomic_read(&ce->guc_id_ref));
+
+try_again:
+    spin_lock_irqsave(&guc->contexts_lock, flags);
+
+    if (context_guc_id_invalid(ce)) {
+        ret = assign_guc_id(guc, &ce->guc_id);
+        if (ret)
+            goto out_unlock;
+        ret = 1;    /* Indidcates newly assigned guc_id */
+    }
+    if (!list_empty(&ce->guc_id_link))
+        list_del_init(&ce->guc_id_link);
+    atomic_inc(&ce->guc_id_ref);
+
+out_unlock:
+    spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+    /*
+     * -EAGAIN indicates no guc_ids are available, let's retire any
+     * outstanding requests to see if that frees up a guc_id. If

the first
+     * retire didn't help, insert a sleep with the timeslice duration before
+     * attempting to retire more requests. Double the sleep period each
+     * subsequent pass before finally giving up. The sleep period has max of
+     * 100ms and minimum of 1ms.
+     */
+    if (ret == -EAGAIN && --tries) {
+        if (PIN_GUC_ID_TRIES - tries > 1) {
+            unsigned int timeslice_shifted =
+                ce->engine->props.timeslice_duration_ms << +                (PIN_GUC_ID_TRIES - tries - 2);
+            unsigned int max = min_t(unsigned int, 100, +                         timeslice_shifted);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -0
+
+            msleep(max_t(unsigned int, max, 1));
+        }
+        intel_gt_retire_requests(guc_to_gt(guc));
+        goto try_again;
+    }
+
+    return ret;
+}
+
+static void unpin_guc_id(struct intel_guc *guc, struct

intel_context *ce)

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +23 -0
+{
+    unsigned long flags;
+
+    GEM_BUG_ON(atomic_read(&ce->guc_id_ref) < 0);
+
+    spin_lock_irqsave(&guc->contexts_lock, flags);
+    if (!context_guc_id_invalid(ce) && list_empty(&ce->guc_id_link) &&
+        !atomic_read(&ce->guc_id_ref))
+        list_add_tail(&ce->guc_id_link, &guc->guc_id_list);
+    spin_unlock_irqrestore(&guc->contexts_lock, flags);
+}
+
+static int __guc_action_register_context(struct intel_guc *guc,
+                     u32 guc_id,
+                     u32 offset)
+{
+    u32 action[] = {
+        INTEL_GUC_ACTION_REGISTER_CONTEXT,
+        guc_id,
+        offset,
+    };
+
+    return intel_guc_send_busy_loop(guc, action,

ARRAY_SIZE(action), true);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+}
+
+static int register_context(struct intel_context *ce)
+{
+    struct intel_guc *guc = ce_to_guc(ce);
+    u32 offset = intel_guc_ggtt_offset(guc, guc->lrc_desc_pool) +
+        ce->guc_id * sizeof(struct guc_lrc_desc);
+
+    return __guc_action_register_context(guc, ce->guc_id, offset);
+}
+
+static int __guc_action_deregister_context(struct intel_guc *guc,
+                       u32 guc_id)
+{
+    u32 action[] = {
+        INTEL_GUC_ACTION_DEREGISTER_CONTEXT,
+        guc_id,
+    };
+
+    return intel_guc_send_busy_loop(guc, action,

ARRAY_SIZE(action), true);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+}
+
+static int deregister_context(struct intel_context *ce, u32 guc_id)
+{
+    struct intel_guc *guc = ce_to_guc(ce);
+
+    return __guc_action_deregister_context(guc, guc_id);
+}
+
+static intel_engine_mask_t adjust_engine_mask(u8 class,

intel_engine_mask_t mask)
+{
+    switch (class) {
+    case RENDER_CLASS:
+        return mask >> RCS0;
+    case VIDEO_ENHANCEMENT_CLASS:

Quoted 8 lines
Quoted 7 lines

+        return mask >> VECS0;
+    case VIDEO_DECODE_CLASS:
+        return mask >> VCS0;
+    case COPY_ENGINE_CLASS:
+        return mask >> BCS0;
+    default:
+        GEM_BUG_ON("Invalid Class");

we usually use MISSING_CASE for this type of errors.

As soon as start talking in logical space (series immediatly after this gets merged) this function gets deleted but will fix.

Quoted 14 lines
Quoted 14 lines

+        return 0;
+    }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -0
+}
+
+static void guc_context_policy_init(struct intel_engine_cs *engine,
+                    struct guc_lrc_desc *desc)
+{
+    desc->policy_flags = 0;
+
+    desc->execution_quantum =

CONTEXT_POLICY_DEFAULT_EXECUTION_QUANTUM_US;
+    desc->preemption_timeout =
CONTEXT_POLICY_DEFAULT_PREEMPTION_TIME_US;
+}

Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c void intel_guc_submission_fini(struct intel_guc *guc) · line 748

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+
+static int guc_lrc_desc_pin(struct intel_context *ce)
+{
+    struct intel_runtime_pm *runtime_pm =
+        &ce->engine->gt->i915->runtime_pm;

If you move this after the engine var below you can skip the ce->engine jump. Also, you can shorten the pointer chasing by using engine->uncore->rpm.

Sure.

Quoted 31 lines
Quoted 31 lines

+    struct intel_engine_cs *engine = ce->engine;
+    struct intel_guc *guc = &engine->gt->uc.guc;
+    u32 desc_idx = ce->guc_id;
+    struct guc_lrc_desc *desc;
+    bool context_registered;
+    intel_wakeref_t wakeref;
+    int ret = 0;

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
+
+    GEM_BUG_ON(!engine->mask);
+
+    /*
+     * Ensure LRC + CT vmas are is same region as write barrier is

done
+     * based on CT vma region.
+     */
+    GEM_BUG_ON(i915_gem_object_is_lmem(guc->ct.vma->obj) != +           i915_gem_object_is_lmem(ce->ring->vma->obj));

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +14 -0
+
+    context_registered = lrc_desc_registered(guc, desc_idx);
+
+    reset_lrc_desc(guc, desc_idx);
+    set_lrc_desc_registered(guc, desc_idx, ce);
+
+    desc = __get_lrc_desc(guc, desc_idx);
+    desc->engine_class = engine_class_to_guc_class(engine->class);
+    desc->engine_submit_mask = adjust_engine_mask(engine->class,
+                              engine->mask);
+    desc->hw_context_desc = ce->lrc.lrca;
+    desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+    desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+    guc_context_policy_init(engine, desc);
Quoted 9 lines
Quoted 7 lines

+    init_sched_state(ce);
+
+    /*
+     * The context_lookup xarray is used to determine if the hardware +     * context is currently registered. There are two cases in which it
+     * could be regisgered either the guc_id has been stole from from

typo regisgered

John got this one. Fixed.

Quoted 26 lines
Quoted 26 lines

+     * another context or the lrc descriptor address of this context has
+     * changed. In either case the context needs to be deregistered with the
+     * GuC before registering this context.
+     */
+    if (context_registered) {
+        set_context_wait_for_deregister_to_register(ce); +        intel_context_get(ce);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+
+        /*
+         * 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);
+    } else {
+        with_intel_runtime_pm(runtime_pm, wakeref)
+            ret = register_context(ce);
+    }
+
+    return ret;

   }
   static int guc_context_pre_pin(struct intel_context *ce,

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
@@ -443,36 +813,139 @@ static int guc_context_pre_pin(struct
 intel_context *ce,
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 818

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
    static int guc_context_pin(struct intel_context *ce, void *vaddr)
    {
+    if (i915_ggtt_offset(ce->state) !=
+        (ce->lrc.lrca & CTX_GTT_ADDRESS_MASK))
+        set_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Shouldn't this be set after lrc_pin()? I can see ce->lrc.lrca being re-assigned in there; it looks like it can only change if the context is new, but for future-proofing IMO better to re-order here.

No this is right. ce->state is assigned in lr_pre_pin and ce->lrc.lrca is assigned in lrc_pin. To catch the change you have to check between those two functions.

Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 820

Quoted 2 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
+
        return lrc_pin(ce, ce->engine, vaddr);

Could use a comment to say that the GuC context pinning call is delayed until request alloc and to look at the comment in the latter function for
details (or move the explanation here and refer here from request alloc).

Yes.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -0
    }
+static void guc_context_unpin(struct intel_context *ce)
+{
+    struct intel_guc *guc = ce_to_guc(ce);
+
+    unpin_guc_id(guc, ce);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce, · line 833

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+    lrc_unpin(ce);
+}
+
+static void guc_context_post_unpin(struct intel_context *ce)
+{
+    lrc_post_unpin(ce);

why do we need this function? we can just pass lrc_post_unpin to the ops (like we already do).

We don't before style reasons I think having a GuC specific wrapper is correct.

Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +16 -0
+}
+
+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;
+
+    GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id));
+    GEM_BUG_ON(ce != __get_context(guc, ce->guc_id));
+
+    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);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_context_pre_pin(struct intel_context *ce,

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+}
+
+static void guc_context_destroy(struct kref *kref)
+{
+    struct intel_context *ce = container_of(kref, typeof(*ce), ref);
+    struct intel_runtime_pm *runtime_pm =
 &ce->engine->gt->i915->runtime_pm;

same as above, going through engine->uncore->rpm is shorter

Sure.

Quoted 14 lines
Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+    struct intel_guc *guc = &ce->engine->gt->uc.guc;
+    intel_wakeref_t wakeref;
+    unsigned long flags;
+
+    /*
+     * If the guc_id is invalid this context has been stolen and we
Quoted 8 lines

can free
+     * it immediately. Also can be freed immediately if the context is not
+     * registered with the GuC.
+     */
+    if (context_guc_id_invalid(ce) ||
+        !lrc_desc_registered(guc, ce->guc_id)) {
+        release_guc_id(guc, ce);

Quoted 6 lines

it feels a bit weird that we call release_guc_id in the case where the id is
invalid. The code handles it fine, but still the flow doesn't feel clean.
Not a blocker.

I understand. Let's see if this can get cleaned up at some point.

Quoted 18 lines
Quoted 10 lines

+        lrc_destroy(kref);
+        return;
+    }
+
+    /*
+     * We have to acquire the context spinlock and check guc_id again, if it
+     * is valid it hasn't been stolen and needs to be deregistered. We +     * delete this context from the list of unpinned guc_ids available to

Quoted 8 lines

+     * stole to seal a race with guc_lrc_desc_pin(). When the G2H CTB +     * returns indicating this context has been deregistered the guc_id is
+     * returned to the pool of available guc_ids.
+     */
+    spin_lock_irqsave(&guc->contexts_lock, flags);
+    if (context_guc_id_invalid(ce)) {
+        __release_guc_id(guc, ce);

Quoted 5 lines

But here the call to __release_guc_id is unneded, right? the ce doesn't own
the ID anymore and both the steal and the release function already clean ce->guc_id_link, so there should be nothing left to clean.

This is dead code. Will delete.

Quoted 146 lines
Quoted 146 lines

+        spin_unlock_irqrestore(&guc->contexts_lock, flags); +        lrc_destroy(kref);
+        return;
+    }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+
+    if (!list_empty(&ce->guc_id_link))
+        list_del_init(&ce->guc_id_link);
+    spin_unlock_irqrestore(&guc->contexts_lock, flags);
+
+    /*
+     * We defer GuC context deregistration until the context is

destroyed
+     * in order to save on CTBs. With this optimization ideally we only need
+     * 1 CTB to register the context during the first pin and 1 CTB to +     * deregister the context when the context is destroyed. Without this
+     * optimization, a CTB would be needed every pin & unpin. +     *
+     * XXX: Need to acqiure the runtime wakeref as this can be triggered
+     * from context_free_worker when not runtime wakeref is held. +     * guc_lrc_desc_unpin requires the runtime as a GuC register is written
+     * in H2G CTB to deregister the context. A future patch may defer this
+     * H2G CTB if the runtime wakeref is zero.
+     */
+    with_intel_runtime_pm(runtime_pm, wakeref)
+        guc_lrc_desc_unpin(ce);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -0
+}
+
+static int guc_context_alloc(struct intel_context *ce)
+{
+    return lrc_alloc(ce, ce->engine);
+}
+

   static const struct intel_context_ops guc_context_ops = { .alloc = guc_context_alloc,
       .pre_pin = guc_context_pre_pin,
       .pin = guc_context_pin,
-    .unpin = lrc_unpin,
-    .post_unpin = lrc_post_unpin,
+    .unpin = guc_context_unpin,
+    .post_unpin = guc_context_post_unpin,
       .enter = intel_context_enter_engine,
       .exit = intel_context_exit_engine,
       .reset = lrc_reset,
-    .destroy = lrc_destroy,
+    .destroy = guc_context_destroy,
   };

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -1
-static int guc_request_alloc(struct i915_request *request)
+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);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+}
+
+static int guc_request_alloc(struct i915_request *rq)
+{
+    struct intel_context *ce = rq->context;
+    struct intel_guc *guc = ce_to_guc(ce);

       int ret;
-    GEM_BUG_ON(!intel_context_is_pinned(request->context)); +    GEM_BUG_ON(!intel_context_is_pinned(rq->context));
       /*
        * Flush enough space to reduce the likelihood of waiting after
        * we start building the request - in which case we will just
        * have to repeat work.
        */
-    request->reserved_space += GUC_REQUEST_SIZE;
+    rq->reserved_space += GUC_REQUEST_SIZE;
       /*
        * Note that after this point, we have committed to using

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -36
@@ -483,56 +956,47 @@ static int guc_request_alloc(struct
 i915_request *request)
         */
        /* Unconditionally invalidate GPU caches and TLBs. */
-    ret = request->engine->emit_flush(request, EMIT_INVALIDATE);
+    ret = rq->engine->emit_flush(rq, EMIT_INVALIDATE);
        if (ret)
            return ret;
-    request->reserved_space -= GUC_REQUEST_SIZE;
-    return 0;
-}
+    rq->reserved_space -= GUC_REQUEST_SIZE;
-static inline void queue_request(struct i915_sched_engine
 *sched_engine,
-                 struct i915_request *rq,
-                 int prio)
-{
-    GEM_BUG_ON(!list_empty(&rq->sched.link));
-    list_add_tail(&rq->sched.link,
-              i915_sched_lookup_priolist(sched_engine, prio));
-    set_bit(I915_FENCE_FLAG_PQUEUE, &rq->fence.flags);
-}
-
-static int guc_bypass_tasklet_submit(struct intel_guc *guc,
-                     struct i915_request *rq)
-{
-    int ret;
-
-    __i915_request_submit(rq);
-
-    trace_i915_request_in(rq, 0);
-
-    guc_set_lrc_tail(rq);
-    ret = guc_add_request(guc, rq);
-    if (ret == -EBUSY)
-        guc->stalled_request = rq;
-
-    return ret;
-}
-
-static void guc_submit_request(struct i915_request *rq)
-{
-    struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
-    struct intel_guc *guc = &rq->engine->gt->uc.guc;
-    unsigned long flags;
+    /*
+     * Call pin_guc_id here rather than in the pinning step as with
+     * dma_resv, contexts can be repeatedly pinned / unpinned

trashing the
+     * guc_ids and creating horrible race conditions. This is especially bad
+     * when guc_ids are being stolen due to over subscription. By the time
+     * this function is reached, it is guaranteed that the guc_id will be
+     * persistent until the generated request is retired. Thus, sealing these
+     * race conditions. It is still safe to fail here if guc_ids are +     * exhausted and return -EAGAIN to the user indicating that they can try
+     * again in the future.
+     *
+     * There is no need for a lock here as the timeline mutex ensures at
+     * most one context can be executing this code path at once. The +     * guc_id_ref is incremented once for every request in flight and +     * decremented on each retire. When it is zero, a lock around the +     * increment (in pin_guc_id) is needed to seal a race with unpin_guc_id.
+     */
+    if (atomic_add_unless(&ce->guc_id_ref, 1, 0))

Quoted 9 lines
Quoted 7 lines

+        return 0;
-    /* Will be called from irq-context when using foreign fences. */ -    spin_lock_irqsave(&sched_engine->lock, flags);
+    ret = pin_guc_id(guc, ce);    /* returns 1 if new guc_id assigned */
+    if (unlikely(ret < 0))
+        return ret;;

typo ";;"

Yep.

Quoted 15 lines
Quoted 6 lines

+    if (context_needs_register(ce, !!ret)) {
+        ret = guc_lrc_desc_pin(ce);
+        if (unlikely(ret)) {    /* unwind */
+            atomic_dec(&ce->guc_id_ref);
+            unpin_guc_id(guc, ce);
+            return ret;

Quoted 8 lines

+        }
+    }
-    if (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); +    clear_bit(CONTEXT_LRCA_DIRTY, &ce->flags);

Quoted 3 lines

Might be worth moving the pinning to a separate function to keep the request_alloc focused on the request. Can be done as a follow up.

Sure. Let me see how that looks in a follow up.

Quoted 148 lines
Quoted 148 lines

-    spin_unlock_irqrestore(&sched_engine->lock, flags); +    return 0;
   }
   static void sanitize_hwsp(struct intel_engine_cs *engine)

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +21 -0
@@ -606,6 +1070,46 @@ static void guc_set_default_submission(struct
 intel_engine_cs *engine)
        engine->submit_request = guc_submit_request;
    }
+static inline void guc_kernel_context_pin(struct intel_guc *guc,
+                      struct intel_context *ce)
+{
+    if (context_guc_id_invalid(ce))
+        pin_guc_id(guc, ce);
+    guc_lrc_desc_pin(ce);
+}
+
+static inline void guc_init_lrc_mapping(struct intel_guc *guc)
+{
+    struct intel_gt *gt = guc_to_gt(guc);
+    struct intel_engine_cs *engine;
+    enum intel_engine_id id;
+
+    /* make sure all descriptors are clean... */
+    xa_destroy(&guc->context_lookup);
+
+    /*
+     * Some contexts might have been pinned before we enabled GuC
+     * submission, so we need to add them to the GuC bookeeping.
+     * Also, after a reset the GuC we want to make sure that the

information
+     * shared with GuC is properly reset. The kernel lrcs are not attached
+     * to the gem_context, so they need to be added separately. +     *
+     * Note: we purposely do not check the error return of +     * guc_lrc_desc_pin, because that function can only fail in two cases.
+     * One, if there aren't enough free IDs, but we're guaranteed to have
+     * enough here (we're either only pinning a handful of lrc on first boot
+     * or we're re-pinning lrcs that were already pinned before the reset).
+     * Two, if the GuC has died and CTBs can't make forward progress. +     * Presumably, the GuC should be alive as this function is called on
+     * driver load or after a reset. Even if it is dead, another full GPU
+     * reset will be triggered and this function would be called again.
+     */

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+
+    for_each_engine(engine, gt, id)
+        if (engine->kernel_context)
+            guc_kernel_context_pin(guc, engine->kernel_context);
+}
+

   static void guc_release(struct intel_engine_cs *engine) {
       engine->sanitize = NULL; /* no longer in control, nothing to sanitize */

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +1 -0
@@ -718,6 +1222,7 @@ int intel_guc_submission_setup(struct
 intel_engine_cs *engine)
    void intel_guc_submission_enable(struct intel_guc *guc)
    {
+    guc_init_lrc_mapping(guc);
    }
    void intel_guc_submission_disable(struct intel_guc *guc)
@@ -743,3 +1248,62 @@ void intel_guc_submission_init_early(struct
 intel_guc *guc)
    {
        guc->submission_selected = __guc_submission_selected(guc);

   }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +45 -0
+
+static inline struct intel_context *
+g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
+{
+    struct intel_context *ce;
+
+    if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+        drm_dbg(&guc_to_gt(guc)->i915->drm,
+            "Invalid desc_idx %u", desc_idx);
+        return NULL;
+    }
+
+    ce = __get_context(guc, desc_idx);
+    if (unlikely(!ce)) {
+        drm_dbg(&guc_to_gt(guc)->i915->drm,
+            "Context is NULL, desc_idx %u", desc_idx);
+        return NULL;
+    }
+
+    return ce;
+}
+
+int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
+                      const u32 *msg,
+                      u32 len)
+{
+    struct intel_context *ce;
+    u32 desc_idx = msg[0];
+
+    if (unlikely(len < 1)) {
+        drm_dbg(&guc_to_gt(guc)->i915->drm, "Invalid length %u", len);
+        return -EPROTO;
+    }
+
+    ce = g2h_context_lookup(guc, desc_idx);
+    if (unlikely(!ce))
+        return -EPROTO;
+
+    if (context_wait_for_deregister_to_register(ce)) {
+        struct intel_runtime_pm *runtime_pm =
+            &ce->engine->gt->i915->runtime_pm;
+        intel_wakeref_t wakeref;
+
+        /*
+         * Previous owner of this guc_id has been deregistered, now

safe
+         * register this context.
+         */
+        with_intel_runtime_pm(runtime_pm, wakeref) +            register_context(ce);
+        clr_context_wait_for_deregister_to_register(ce); +        intel_context_put(ce);
+    } else if (context_destroyed(ce)) {
+        /* Context has been destroyed */
+        release_guc_id(guc, ce);
+        lrc_destroy(&ce->ref);
+    }

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+
+    return 0;
+}
drivers/gpu/drm/i915/i915_reg.h +0 -0
index 943fe485c662..204c95c39353 100644
@@ -4142,6 +4142,7 @@ enum {

       FAULT_AND_CONTINUE /* Unsupported */
   };
+#define CTX_GTT_ADDRESS_MASK GENMASK(31, 12)
   #define GEN8_CTX_VALID (1 << 0)
   #define GEN8_CTX_FORCE_PD_RESTORE (1 << 1)
   #define GEN8_CTX_FORCE_RESTORE (1 << 2)

drivers/gpu/drm/i915/i915_request.c +0 -0
index 86b4c9f2613d..b48c4905d3fc 100644
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c bool i915_request_retire(struct i915_request *rq) · line 410

Quoted 7 lines
drivers/gpu/drm/i915/i915_request.c +1 -0
@@ -407,6 +407,7 @@ bool i915_request_retire(struct i915_request *rq)
         */
        if (!list_empty(&rq->sched.link))
            remove_from_engine(rq);
+    atomic_dec(&rq->context->guc_id_ref);

Does this work/make sense  if GuC is disabled?

Quoted 11 lines

It doesn't matter if the GuC is disabled as this var isn't used if it is. A following patch adds a vfunc here and pushes this in the GuC backend.

Matt

Quoted 5 lines

Daniele

Quoted 3 lines
drivers/gpu/drm/i915/i915_request.c +0 -0
        GEM_BUG_ON(!llist_empty(&rq->execute_cb));

       __list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */

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

#17 lore raw

PATCH 7/51 drm/i915/guc: Insert fence on context when deregistering

In reply to Matthew Brost

Sometimes during context pinning a context with the same guc_id is registered with the GuC. In this a case deregister must be done before the context can be registered. A fence is inserted on all requests while the deregister is in flight. Once the G2H is received indicating the deregistration is complete the context is registered and the fence is released.

v2:
(John H)
- Fix commit message

---
 drivers/gpu/drm/i915/gt/intel_context.c       |  1 +
 drivers/gpu/drm/i915/gt/intel_context_types.h |  5 ++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 51 ++++++++++++++++++-
 drivers/gpu/drm/i915/i915_request.h           |  8 +++
 4 files changed, 63 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +1 -0
index 32fd6647154b..ad7197c5910f 100644
@@ -385,6 +385,7 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 	mutex_init(&ce->pin_mutex);
 
 	spin_lock_init(&ce->guc_state.lock);
+	INIT_LIST_HEAD(&ce->guc_state.fences);
 
 	ce->guc_id = GUC_INVALID_LRC_ID;
 	INIT_LIST_HEAD(&ce->guc_id_link);
drivers/gpu/drm/i915/gt/intel_context_types.h +5 -0
index 606c480aec26..e0e3a937f709 100644
@@ -147,6 +147,11 @@ struct intel_context {
 		 * submission
 		 */
 		u8 sched_state;
+		/*
+		 * fences: maintains of list of requests that have a submit
+		 * fence related to GuC submission
+		 */
+		struct list_head fences;
 	} guc_state;
 
 	/* GuC scheduling state flags that do not require a lock. */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +49 -2

81 lines not on this page · raw message

drivers/gpu/drm/i915/i915_request.h +8 -0
index 5deb65ec5fa5..717e5b292046 100644
@@ -285,6 +285,14 @@ struct i915_request {
 		struct hrtimer timer;
 	} watchdog;
 
+	/*
+	 * Requests may need to be stalled when using GuC submission waiting for
+	 * certain GuC operations to complete. If that is the case, stalled
+	 * requests are added to a per context list of stalled requests. The
+	 * below list_head is the link in that list.
+	 */
+	struct list_head guc_fence_link;
+
 	I915_SELFTEST_DECLARE(struct {
 		struct list_head link;
 		unsigned long delay;
Signature · 6 lines
2.28.0

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

PATCH 8/51 drm/i915/guc: Defer context unpin until scheduling is disabled

In reply to Matthew Brost

With GuC scheduling, it isn't safe to unpin a context while scheduling is enabled for that context as the GuC may touch some of the pinned state (e.g. LRC). To ensure scheduling isn't enabled when an unpin is done, a call back is added to intel_context_unpin when pin count == 1 to disable scheduling for that context. When the response CTB is received it is safe to do the final unpin.

Future patches may add a heuristic / delay to schedule the disable call back to avoid thrashing on schedule enable / disable.

---
 drivers/gpu/drm/i915/gt/intel_context.c       |   4 +-
 drivers/gpu/drm/i915/gt/intel_context.h       |  27 +++-
 drivers/gpu/drm/i915/gt/intel_context_types.h |   2 +
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   2 +
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   3 +
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 147 +++++++++++++++++-
 6 files changed, 181 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +2 -2
index ad7197c5910f..3d5b4116617f 100644
@@ -306,9 +306,9 @@ int __intel_context_do_pin(struct intel_context *ce)
 	return err;
 }
 
-void intel_context_unpin(struct intel_context *ce)
+void __intel_context_do_unpin(struct intel_context *ce, int sub)
 {
-	if (!atomic_dec_and_test(&ce->pin_count))
+	if (!atomic_sub_and_test(sub, &ce->pin_count))
 		return;
 
 	CE_TRACE(ce, "unpin\n");
drivers/gpu/drm/i915/gt/intel_context.h +26 -1
index b10cbe8fee99..974ef85320c2 100644
@@ -113,7 +113,32 @@ static inline void __intel_context_pin(struct intel_context *ce)
 	atomic_inc(&ce->pin_count);
 }
 
-void intel_context_unpin(struct intel_context *ce);
+void __intel_context_do_unpin(struct intel_context *ce, int sub);
+
+static inline void intel_context_sched_disable_unpin(struct intel_context *ce)
+{
+	__intel_context_do_unpin(ce, 2);
+}
+
+static inline void intel_context_unpin(struct intel_context *ce)
+{
+	if (!ce->ops->sched_disable) {
+		__intel_context_do_unpin(ce, 1);
+	} else {
+		/*
+		 * Move ownership of this pin to the scheduling disable which is
+		 * an async operation. When that operation completes the above
+		 * intel_context_sched_disable_unpin is called potentially
+		 * unpinning the context.
+		 */
+		while (!atomic_add_unless(&ce->pin_count, -1, 1)) {
+			if (atomic_cmpxchg(&ce->pin_count, 1, 2) == 1) {
+				ce->ops->sched_disable(ce);
+				break;
+			}
+		}
+	}
+}
 
 void intel_context_enter_engine(struct intel_context *ce);
 void intel_context_exit_engine(struct intel_context *ce);
drivers/gpu/drm/i915/gt/intel_context_types.h +2 -0
index e0e3a937f709..4a5518d295c2 100644
@@ -43,6 +43,8 @@ struct intel_context_ops {
 	void (*enter)(struct intel_context *ce);
 	void (*exit)(struct intel_context *ce);
 
+	void (*sched_disable)(struct intel_context *ce);
+
 	void (*reset)(struct intel_context *ce);
 	void (*destroy)(struct kref *kref);
 };
drivers/gpu/drm/i915/gt/uc/intel_guc.h +2 -0
index 30773cd699f5..03b7222b04a2 100644
@@ -241,6 +241,8 @@ int intel_guc_reset_engine(struct intel_guc *guc,
 
 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_load_status(struct intel_guc *guc, struct drm_printer *p);
 
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +3 -0
index 28ff82c5be45..019b25ff1888 100644
@@ -932,6 +932,9 @@ static int ct_process_request(struct intel_guc_ct *ct, struct ct_incoming_msg *r
 		ret = intel_guc_deregister_done_process_msg(guc, payload,
 							    len);
 		break;
+	case INTEL_GUC_ACTION_SCHED_CONTEXT_MODE_DONE:
+		ret = intel_guc_sched_done_process_msg(guc, payload, len);
+		break;
 	default:
 		ret = -EOPNOTSUPP;
 		break;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +146 -1

205 lines not on this page · raw message

Signature · 6 lines
2.28.0

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

PATCH 9/51 drm/i915/guc: Disable engine barriers with GuC during unpin

In reply to Matthew Brost

Disable engine barriers for unpinning with GuC. This feature isn't needed with the GuC as it disables context scheduling before unpinning which guarantees the HW will not reference the context. Hence it is not necessary to defer unpinning until a kernel context request completes on each engine in the context engine mask.

---
 drivers/gpu/drm/i915/gt/intel_context.c    |  2 +-
 drivers/gpu/drm/i915/gt/selftest_context.c | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/intel_context.c +1 -1
index 3d5b4116617f..91349d071e0e 100644
@@ -80,7 +80,7 @@ static int intel_context_active_acquire(struct intel_context *ce)
 
 	__i915_active_acquire(&ce->active);
 
-	if (intel_context_is_barrier(ce))
+	if (intel_context_is_barrier(ce) || intel_engine_uses_guc(ce->engine))
 		return 0;
 
 	/* Preallocate tracking nodes */
drivers/gpu/drm/i915/gt/selftest_context.c +10 -0
index 26685b927169..fa7b99a671dd 100644
@@ -209,7 +209,13 @@ static int __live_active_context(struct intel_engine_cs *engine)
 	 * This test makes sure that the context is kept alive until a
 	 * subsequent idle-barrier (emitted when the engine wakeref hits 0
 	 * with no more outstanding requests).
+	 *
+	 * In GuC submission mode we don't use idle barriers and we instead
+	 * get a message from the GuC to signal that it is safe to unpin the
+	 * context from memory.
 	 */
+	if (intel_engine_uses_guc(engine))
+		return 0;
 
 	if (intel_engine_pm_is_awake(engine)) {
 		pr_err("%s is awake before starting %s!\n",
@@ -357,7 +363,11 @@ static int __live_remote_context(struct intel_engine_cs *engine)
 	 * on the context image remotely (intel_context_prepare_remote_request),
 	 * which inserts foreign fences into intel_context.active, does not
 	 * clobber the idle-barrier.
+	 *
+	 * In GuC submission mode we don't use idle barriers.
 	 */
+	if (intel_engine_uses_guc(engine))
+		return 0;
 
 	if (intel_engine_pm_is_awake(engine)) {
 		pr_err("%s is awake before starting %s!\n",
Signature · 6 lines
2.28.0

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

PATCH 10/51 drm/i915/guc: Extend deregistration fence to schedule disable

In reply to Matthew Brost

Extend the deregistration context fence to fence whne a GuC context has scheduling disable pending.

v2:
(John H)
- Update comment why we check the pin count within spin lock

---
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 40 +++++++++++++++----
 1 file changed, 33 insertions(+), 7 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +33 -7
index 5519c988a6ca..9dc1a256e185 100644
@@ -922,7 +922,22 @@ static void guc_context_sched_disable(struct intel_context *ce)
 		goto unpin;
 
 	spin_lock_irqsave(&ce->guc_state.lock, flags);
+
+	/*
+	 * We have to check if the context has been pinned again as another pin
+	 * operation is allowed to pass this function. Checking the pin count,
+	 * within ce->guc_state.lock, synchronizes this function with
+	 * guc_request_alloc ensuring a request doesn't slip through the
+	 * 'context_pending_disable' fence. Checking within the spin lock (can't
+	 * sleep) ensures another process doesn't pin this context and generate
+	 * a request before we set the 'context_pending_disable' flag here.
+	 */
+	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);
+
 	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
 
 	with_intel_runtime_pm(runtime_pm, wakeref)
@@ -1127,19 +1142,22 @@ static int guc_request_alloc(struct i915_request *rq)
 out:
 	/*
 	 * We block all requests on this context if a G2H is pending for a
-	 * context deregistration as the GuC will fail a context registration
-	 * while this G2H is pending. Once a G2H returns, the fence is released
-	 * that is blocking these requests (see guc_signal_context_fence).
+	 * schedule disable or context deregistration as the GuC will fail a
+	 * schedule enable or context registration if either G2H is pending
+	 * respectfully. Once a G2H returns, the fence is released that is
+	 * blocking these requests (see guc_signal_context_fence).
 	 *
-	 * We can safely check the below field outside of the lock as it isn't
-	 * possible for this field to transition from being clear to set but
+	 * We can safely check the below fields outside of the lock as it isn't
+	 * possible for these fields to transition from being clear to set but
 	 * converse is possible, hence the need for the check within the lock.
 	 */
-	if (likely(!context_wait_for_deregister_to_register(ce)))
+	if (likely(!context_wait_for_deregister_to_register(ce) &&
+		   !context_pending_disable(ce)))
 		return 0;
 
 	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	if (context_wait_for_deregister_to_register(ce)) {
+	if (context_wait_for_deregister_to_register(ce) ||
+	    context_pending_disable(ce)) {
 		i915_sw_fence_await(&rq->submit);
 
 		list_add_tail(&rq->guc_fence_link, &ce->guc_state.fences);
@@ -1488,10 +1506,18 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
 	if (context_pending_enable(ce)) {
 		clr_context_pending_enable(ce);
 	} else if (context_pending_disable(ce)) {
+		/*
+		 * Unpin must be done before __guc_signal_context_fence,
+		 * otherwise a race exists between the requests getting
+		 * submitted + retired before this unpin completes resulting in
+		 * the pin_count going to zero and the context still being
+		 * enabled.
+		 */
 		intel_context_sched_disable_unpin(ce);
 
 		spin_lock_irqsave(&ce->guc_state.lock, flags);
 		clr_context_pending_disable(ce);
+		__guc_signal_context_fence(ce);
 		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
 	}
 
Signature · 6 lines
2.28.0

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

PATCH 11/51 drm/i915: Disable preempt busywait when using GuC scheduling

In reply to Matthew Brost

Disable preempt busywait when using GuC scheduling. This isn't needed as the GuC controls preemption when scheduling.

v2:
(John H):
- Fix commit message

---
 drivers/gpu/drm/i915/gt/gen8_engine_cs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/gen8_engine_cs.c +4 -2
index 87b06572fd2e..f7aae502ec3d 100644
@@ -506,7 +506,8 @@ gen8_emit_fini_breadcrumb_tail(struct i915_request *rq, u32 *cs)
 	*cs++ = MI_USER_INTERRUPT;
 
 	*cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
-	if (intel_engine_has_semaphores(rq->engine))
+	if (intel_engine_has_semaphores(rq->engine) &&
+	    !intel_uc_uses_guc_submission(&rq->engine->gt->uc))
 		cs = emit_preempt_busywait(rq, cs);
 
 	rq->tail = intel_ring_offset(rq, cs);
@@ -598,7 +599,8 @@ gen12_emit_fini_breadcrumb_tail(struct i915_request *rq, u32 *cs)
 	*cs++ = MI_USER_INTERRUPT;
 
 	*cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
-	if (intel_engine_has_semaphores(rq->engine))
+	if (intel_engine_has_semaphores(rq->engine) &&
+	    !intel_uc_uses_guc_submission(&rq->engine->gt->uc))
 		cs = gen12_emit_preempt_busywait(rq, cs);
 
 	rq->tail = intel_ring_offset(rq, cs);
Signature · 1 line
2.28.0
#22 lore raw

PATCH 12/51 drm/i915/guc: Ensure request ordering via completion fences

In reply to Matthew Brost

If two requests are on the same ring, they are explicitly ordered by the HW. So, a submission fence is sufficient to ensure ordering when using the new GuC submission interface. Conversely, if two requests share a timeline and are on the same physical engine but different context this doesn't ensure ordering on the new GuC submission interface. So, a completion fence needs to be used to ensure ordering.

---
 drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c |  1 -
 drivers/gpu/drm/i915/i915_request.c               | 12 ++++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -1
index 9dc1a256e185..4443cc6f5320 100644
@@ -933,7 +933,6 @@ static void guc_context_sched_disable(struct intel_context *ce)
 	 * a request before we set the 'context_pending_disable' flag here.
 	 */
 	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);
drivers/gpu/drm/i915/i915_request.c +10 -2
index b48c4905d3fc..2b2b63cba06c 100644
@@ -432,6 +432,7 @@ void i915_request_retire_upto(struct i915_request *rq)
 
 	do {
 		tmp = list_first_entry(&tl->requests, typeof(*tmp), link);
+		GEM_BUG_ON(!i915_request_completed(tmp));
 	} while (i915_request_retire(tmp) && tmp != rq);
 }
 
@@ -1380,6 +1381,9 @@ i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
 	return err;
 }
 
+static int
+i915_request_await_request(struct i915_request *to, struct i915_request *from);
+
 int
 i915_request_await_execution(struct i915_request *rq,
 			     struct dma_fence *fence)
@@ -1465,7 +1469,8 @@ i915_request_await_request(struct i915_request *to, struct i915_request *from)
 			return ret;
 	}
 
-	if (is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
+	if (!intel_engine_uses_guc(to->engine) &&
+	    is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
 		ret = await_request_submit(to, from);
 	else
 		ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
@@ -1626,6 +1631,8 @@ __i915_request_add_to_timeline(struct i915_request *rq)
 	prev = to_request(__i915_active_fence_set(&timeline->last_request,
 						  &rq->fence));
 	if (prev && !__i915_request_is_complete(prev)) {
+		bool uses_guc = intel_engine_uses_guc(rq->engine);
+
 		/*
 		 * The requests are supposed to be kept in order. However,
 		 * we need to be wary in case the timeline->last_request
@@ -1636,7 +1643,8 @@ __i915_request_add_to_timeline(struct i915_request *rq)
 			   i915_seqno_passed(prev->fence.seqno,
 					     rq->fence.seqno));
 
-		if (is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask))
+		if ((!uses_guc && is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask)) ||
+		    (uses_guc && prev->context == rq->context))
 			i915_sw_fence_await_sw_fence(&rq->submit,
 						     &prev->submit,
 						     &rq->submitq);
Signature · 6 lines
2.28.0

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

Re: [PATCH 12/51] drm/i915/guc: Ensure request ordering via completion fences

Quoted 19 lines · Matthew Brost

If two requests are on the same ring, they are explicitly ordered by the HW. So, a submission fence is sufficient to ensure ordering when using the new GuC submission interface. Conversely, if two requests share a timeline and are on the same physical engine but different context this doesn't ensure ordering on the new GuC submission interface. So, a completion fence needs to be used to ensure ordering.

---
  drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c |  1 -
  drivers/gpu/drm/i915/i915_request.c               | 12 ++++++++++--
  2 files changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
index 9dc1a256e185..4443cc6f5320 100644

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_context_sched_disable(struct intel_context *ce) · line 936

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -1
@@ -933,7 +933,6 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	 * a request before we set the 'context_pending_disable' flag here.
  	 */
  	if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
-		spin_unlock_irqrestore(&ce->guc_state.lock, flags);

incorrect spinlock drop is still here. Everything else looks ok (my suggestion to use an engine flag stands, but can be addressed as a follow up).

Daniele

Quoted 54 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
  		return;
  	}
  	guc_id = prep_context_pending_disable(ce);
drivers/gpu/drm/i915/i915_request.c +10 -2
index b48c4905d3fc..2b2b63cba06c 100644
@@ -432,6 +432,7 @@ void i915_request_retire_upto(struct i915_request *rq)
 
  	do {
  		tmp = list_first_entry(&tl->requests, typeof(*tmp), link);
+		GEM_BUG_ON(!i915_request_completed(tmp));
  	} while (i915_request_retire(tmp) && tmp != rq);
  }
 
@@ -1380,6 +1381,9 @@ i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
  	return err;
  }
 
+static int
+i915_request_await_request(struct i915_request *to, struct i915_request *from);
+
  int
  i915_request_await_execution(struct i915_request *rq,
  			     struct dma_fence *fence)
@@ -1465,7 +1469,8 @@ i915_request_await_request(struct i915_request *to, struct i915_request *from)
  			return ret;
  	}
 
-	if (is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
+	if (!intel_engine_uses_guc(to->engine) &&
+	    is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
  		ret = await_request_submit(to, from);
  	else
  		ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
@@ -1626,6 +1631,8 @@ __i915_request_add_to_timeline(struct i915_request *rq)
  	prev = to_request(__i915_active_fence_set(&timeline->last_request,
  						  &rq->fence));
  	if (prev && !__i915_request_is_complete(prev)) {
+		bool uses_guc = intel_engine_uses_guc(rq->engine);
+
  		/*
  		 * The requests are supposed to be kept in order. However,
  		 * we need to be wary in case the timeline->last_request
@@ -1636,7 +1643,8 @@ __i915_request_add_to_timeline(struct i915_request *rq)
  			   i915_seqno_passed(prev->fence.seqno,
  					     rq->fence.seqno));
 
-		if (is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask))
+		if ((!uses_guc && is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask)) ||
+		    (uses_guc && prev->context == rq->context))
  			i915_sw_fence_await_sw_fence(&rq->submit,
  						     &prev->submit,
  						     &rq->submitq);
#24 lore raw

Re: [PATCH 12/51] drm/i915/guc: Ensure request ordering via completion fences

Quoted 17 lines · Daniele Ceraolo Spurio
Quoted 14 lines · Matthew Brost

If two requests are on the same ring, they are explicitly ordered by the HW. So, a submission fence is sufficient to ensure ordering when using the new GuC submission interface. Conversely, if two requests share a timeline and are on the same physical engine but different context this doesn't ensure ordering on the new GuC submission interface. So, a completion fence needs to be used to ensure ordering.

---
  drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c |  1 -
  drivers/gpu/drm/i915/i915_request.c               | 12 ++++++++++--
  2 files changed, 10 insertions(+), 3 deletions(-)
Quoted 11 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_context_sched_disable(struct intel_context *ce) · line 936

Quoted 9 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -1
index 9dc1a256e185..4443cc6f5320 100644
@@ -933,7 +933,6 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	 * a request before we set the 'context_pending_disable' flag here.
  	 */
  	if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
-		spin_unlock_irqrestore(&ce->guc_state.lock, flags);

incorrect spinlock drop is still here. Everything else looks ok (my

No it isn't not. See the return directly below the drop of the spin lock. Matt

Quoted 3 lines

suggestion to use an engine flag stands, but can be addressed as a follow up).

Not sure I follow this one, but we can sync and address in a follow if needed.

Matt

Quoted 43 lines

Daniele

Quoted 41 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
  		return;
  	}
  	guc_id = prep_context_pending_disable(ce);
drivers/gpu/drm/i915/i915_request.c +8 -1
index b48c4905d3fc..2b2b63cba06c 100644
@@ -432,6 +432,7 @@ void i915_request_retire_upto(struct i915_request *rq)
  	do {
  		tmp = list_first_entry(&tl->requests, typeof(*tmp), link);
+		GEM_BUG_ON(!i915_request_completed(tmp));
  	} while (i915_request_retire(tmp) && tmp != rq);
  }
@@ -1380,6 +1381,9 @@ i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
  	return err;
  }
+static int
+i915_request_await_request(struct i915_request *to, struct i915_request *from);
+
  int
  i915_request_await_execution(struct i915_request *rq,
  			     struct dma_fence *fence)
@@ -1465,7 +1469,8 @@ i915_request_await_request(struct i915_request *to, struct i915_request *from)
  			return ret;
  	}
-	if (is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
+	if (!intel_engine_uses_guc(to->engine) &&
+	    is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
  		ret = await_request_submit(to, from);
  	else
  		ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
@@ -1626,6 +1631,8 @@ __i915_request_add_to_timeline(struct i915_request *rq)
  	prev = to_request(__i915_active_fence_set(&timeline->last_request,
  						  &rq->fence));
  	if (prev && !__i915_request_is_complete(prev)) {
+		bool uses_guc = intel_engine_uses_guc(rq->engine);
+
  		/*
  		 * The requests are supposed to be kept in order. However,
  		 * we need to be wary in case the timeline->last_request
@@ -1636,7 +1643,8 @@ __i915_request_add_to_timeline(struct i915_request *rq)
Quoted 9 lines

drivers/gpu/drm/i915/i915_request.c __i915_request_add_to_timeline(struct i915_request *rq) · line 1650

Quoted 8 lines
drivers/gpu/drm/i915/i915_request.c +2 -1
  			   i915_seqno_passed(prev->fence.seqno,
  					     rq->fence.seqno));
-		if (is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask))
+		if ((!uses_guc && is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask)) ||
+		    (uses_guc && prev->context == rq->context))
  			i915_sw_fence_await_sw_fence(&rq->submit,
  						     &prev->submit,
  						     &rq->submitq);

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

#25 lore raw

Re: [PATCH 12/51] drm/i915/guc: Ensure request ordering via completion fences

Quoted 30 lines · Matthew Brost
Quoted 17 lines · Daniele Ceraolo Spurio
Quoted 14 lines · Matthew Brost

If two requests are on the same ring, they are explicitly ordered by the HW. So, a submission fence is sufficient to ensure ordering when using the new GuC submission interface. Conversely, if two requests share a timeline and are on the same physical engine but different context this doesn't ensure ordering on the new GuC submission interface. So, a completion fence needs to be used to ensure ordering.

---
  drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c |  1 -
  drivers/gpu/drm/i915/i915_request.c               | 12 ++++++++++--
  2 files changed, 10 insertions(+), 3 deletions(-)
Quoted 11 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_context_sched_disable(struct intel_context *ce) · line 936

Quoted 9 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -1
index 9dc1a256e185..4443cc6f5320 100644
@@ -933,7 +933,6 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	 * a request before we set the 'context_pending_disable' flag here.
  	 */
  	if (unlikely(atomic_add_unless(&ce->pin_count, -2, 2))) {
-		spin_unlock_irqrestore(&ce->guc_state.lock, flags);

incorrect spinlock drop is still here. Everything else looks ok (my

Quoted 3 lines

No it isn't not. See the return directly below the drop of the spin lock. Matt

Ugh, misread this. Yes, this deletion shouldn't be here. Will fix.

Matt

Quoted 4 lines
Quoted 3 lines

suggestion to use an engine flag stands, but can be addressed as a follow up).

Quoted 57 lines

Not sure I follow this one, but we can sync and address in a follow if needed.

Matt

Quoted 52 lines

Daniele

Quoted 49 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
  		return;
  	}
  	guc_id = prep_context_pending_disable(ce);
drivers/gpu/drm/i915/i915_request.c +10 -2
index b48c4905d3fc..2b2b63cba06c 100644
@@ -432,6 +432,7 @@ void i915_request_retire_upto(struct i915_request *rq)
  	do {
  		tmp = list_first_entry(&tl->requests, typeof(*tmp), link);
+		GEM_BUG_ON(!i915_request_completed(tmp));
  	} while (i915_request_retire(tmp) && tmp != rq);
  }
@@ -1380,6 +1381,9 @@ i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
  	return err;
  }
+static int
+i915_request_await_request(struct i915_request *to, struct i915_request *from);
+
  int
  i915_request_await_execution(struct i915_request *rq,
  			     struct dma_fence *fence)
@@ -1465,7 +1469,8 @@ i915_request_await_request(struct i915_request *to, struct i915_request *from)
  			return ret;
  	}
-	if (is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
+	if (!intel_engine_uses_guc(to->engine) &&
+	    is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
  		ret = await_request_submit(to, from);
  	else
  		ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
@@ -1626,6 +1631,8 @@ __i915_request_add_to_timeline(struct i915_request *rq)
  	prev = to_request(__i915_active_fence_set(&timeline->last_request,
  						  &rq->fence));
  	if (prev && !__i915_request_is_complete(prev)) {
+		bool uses_guc = intel_engine_uses_guc(rq->engine);
+
  		/*
  		 * The requests are supposed to be kept in order. However,
  		 * we need to be wary in case the timeline->last_request
@@ -1636,7 +1643,8 @@ __i915_request_add_to_timeline(struct i915_request *rq)
  			   i915_seqno_passed(prev->fence.seqno,
  					     rq->fence.seqno));
-		if (is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask))
+		if ((!uses_guc && is_power_of_2(READ_ONCE(prev->engine)->mask | rq->engine->mask)) ||
+		    (uses_guc && prev->context == rq->context))
  			i915_sw_fence_await_sw_fence(&rq->submit,
  						     &prev->submit,
  						     &rq->submitq);

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

#26 lore raw

PATCH 13/51 drm/i915/guc: Disable semaphores when using GuC scheduling

In reply to Matthew Brost

Semaphores are an optimization and not required for basic GuC submission to work properly. Disable until we have time to do the implementation to enable semaphores and tune them for performance. Also long direction is just to delete semaphores from the i915 so another reason to not enable these for GuC submission.

This patch fixes an existing bug where I915_ENGINE_HAS_SEMAPHORES was not honored correctly.

v2: Reword commit message
v3:
(John H)
- Add text to commit indicating this also fixing an existing bug

---
 drivers/gpu/drm/i915/gem/i915_gem_context.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +4 -2
index 7d6f52d8a801..64659802d4df 100644
@@ -799,7 +799,8 @@ static int intel_context_set_gem(struct intel_context *ce,
 	}
 
 	if (ctx->sched.priority >= I915_PRIORITY_NORMAL &&
-	    intel_engine_has_timeslices(ce->engine))
+	    intel_engine_has_timeslices(ce->engine) &&
+	    intel_engine_has_semaphores(ce->engine))
 		__set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
 
 	if (IS_ACTIVE(CONFIG_DRM_I915_REQUEST_TIMEOUT) &&
@@ -1778,7 +1779,8 @@ static void __apply_priority(struct intel_context *ce, void *arg)
 	if (!intel_engine_has_timeslices(ce->engine))
 		return;
 
-	if (ctx->sched.priority >= I915_PRIORITY_NORMAL)
+	if (ctx->sched.priority >= I915_PRIORITY_NORMAL &&
+	    intel_engine_has_semaphores(ce->engine))
 		intel_context_set_use_semaphores(ce);
 	else
 		intel_context_clear_use_semaphores(ce);
Signature · 1 line
2.28.0
Reviewed-by on v1 13/51
#27 lore raw

Re: [PATCH 13/51] drm/i915/guc: Disable semaphores when using GuC scheduling

Quoted 8 lines · Matthew Brost

Semaphores are an optimization and not required for basic GuC submission to work properly. Disable until we have time to do the implementation to enable semaphores and tune them for performance. Also long direction is just to delete semaphores from the i915 so another reason to not enable these for GuC submission.

This patch fixes an existing bug where I915_ENGINE_HAS_SEMAPHORES was not honored correctly.

Bugs plural. Otherwise:

Quoted 27 lines

v2: Reword commit message
v3:
(John H)
- Add text to commit indicating this also fixing an existing bug

---
  drivers/gpu/drm/i915/gem/i915_gem_context.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -1
index 7d6f52d8a801..64659802d4df 100644
@@ -799,7 +799,8 @@ static int intel_context_set_gem(struct intel_context *ce,
  	}
 
  	if (ctx->sched.priority >= I915_PRIORITY_NORMAL &&
-	    intel_engine_has_timeslices(ce->engine))
+	    intel_engine_has_timeslices(ce->engine) &&
+	    intel_engine_has_semaphores(ce->engine))
  		__set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
 
  	if (IS_ACTIVE(CONFIG_DRM_I915_REQUEST_TIMEOUT) &&

drivers/gpu/drm/i915/gem/i915_gem_context.c static void __apply_priority(struct intel_context *ce, void *arg) · line 1786

Quoted 9 lines
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -1
@@ -1778,7 +1779,8 @@ static void __apply_priority(struct intel_context *ce, void *arg)
  	if (!intel_engine_has_timeslices(ce->engine))
  		return;
 
-	if (ctx->sched.priority >= I915_PRIORITY_NORMAL)
+	if (ctx->sched.priority >= I915_PRIORITY_NORMAL &&
+	    intel_engine_has_semaphores(ce->engine))
  		intel_context_set_use_semaphores(ce);
  	else
  		intel_context_clear_use_semaphores(ce);

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

#28 lore raw

PATCH 14/51 drm/i915/guc: Ensure G2H response has space in buffer

In reply to Matthew Brost

Ensure G2H response has space in the buffer before sending H2G CTB as the GuC can't handle any backpressure on the G2H interface.

v2:
(Matthew)
- s/INTEL_GUC_SEND/INTEL_GUC_CT_SEND
v3:
(Matthew)
- Add G2H credit accounting to blocking path, add g2h_release_space helper
(John H)
- CTB_G2H_BUFFER_SIZE / 4 == G2H_ROOM_BUFFER_SIZE

---
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  8 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     | 91 +++++++++++++++----
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |  9 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |  4 +
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 13 ++-
 5 files changed, 99 insertions(+), 26 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 03b7222b04a2..80b88bae5f24 100644
@@ -96,10 +96,11 @@ inline int intel_guc_send(struct intel_guc *guc, const u32 *action, u32 len)
 }
 
 static
-inline int intel_guc_send_nb(struct intel_guc *guc, const u32 *action, u32 len)
+inline int intel_guc_send_nb(struct intel_guc *guc, const u32 *action, u32 len,
+			     u32 g2h_len_dw)
 {
 	return intel_guc_ct_send(&guc->ct, action, len, NULL, 0,
-				 INTEL_GUC_CT_SEND_NB);
+				 MAKE_SEND_FLAGS(g2h_len_dw));
 }
 
 static inline int
@@ -113,6 +114,7 @@ intel_guc_send_and_receive(struct intel_guc *guc, const u32 *action, u32 len,
 static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
 					   const u32 *action,
 					   u32 len,
+					   u32 g2h_len_dw,
 					   bool loop)
 {
 	int err;
@@ -123,7 +125,7 @@ static inline int intel_guc_send_busy_loop(struct intel_guc* guc,
 	might_sleep_if(loop && not_atomic);
 
 retry:
-	err = intel_guc_send_nb(guc, action, len);
+	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
 	if (unlikely(err == -EBUSY && loop)) {
 		if (likely(not_atomic)) {
 			if (msleep_interruptible(sleep_period_ms))
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +74 -17

222 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h +8 -1
index edd1bba0445d..785dfc5c6efb 100644
@@ -33,6 +33,7 @@ struct intel_guc;
  * @desc: pointer to the buffer descriptor
  * @cmds: pointer to the commands buffer
  * @size: size of the commands buffer in dwords
+ * @resv_space: reserved space in buffer in dwords
  * @head: local shadow copy of head in dwords
  * @tail: local shadow copy of tail in dwords
  * @space: local shadow copy of space in dwords
@@ -43,9 +44,10 @@ struct intel_guc_ct_buffer {
 	struct guc_ct_buffer_desc *desc;
 	u32 *cmds;
 	u32 size;
+	u32 resv_space;
 	u32 tail;
 	u32 head;
-	u32 space;
+	atomic_t space;
 	bool broken;
 };
 
@@ -97,6 +99,11 @@ static inline bool intel_guc_ct_enabled(struct intel_guc_ct *ct)
 }
 
 #define INTEL_GUC_CT_SEND_NB		BIT(31)
+#define INTEL_GUC_CT_SEND_G2H_DW_SHIFT	0
+#define INTEL_GUC_CT_SEND_G2H_DW_MASK	(0xff << INTEL_GUC_CT_SEND_G2H_DW_SHIFT)
+#define MAKE_SEND_FLAGS(len) \
+	({GEM_BUG_ON(!FIELD_FIT(INTEL_GUC_CT_SEND_G2H_DW_MASK, len)); \
+	(FIELD_PREP(INTEL_GUC_CT_SEND_G2H_DW_MASK, len) | INTEL_GUC_CT_SEND_NB);})
 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);
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +4 -0
index 4e4edc368b77..94bb1ca6f889 100644
@@ -17,6 +17,10 @@
 #include "abi/guc_communication_ctb_abi.h"
 #include "abi/guc_messages_abi.h"
 
+/* Payload length only i.e. don't include G2H header length */
+#define G2H_LEN_DW_SCHED_CONTEXT_MODE_SET	2
+#define G2H_LEN_DW_DEREGISTER_CONTEXT		1
+
 #define GUC_CONTEXT_DISABLE		0
 #define GUC_CONTEXT_ENABLE		1
 
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +8 -5
index 4443cc6f5320..f7e34baa9506 100644
@@ -260,6 +260,7 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
 	struct intel_context *ce = rq->context;
 	u32 action[3];
 	int len = 0;
+	u32 g2h_len_dw = 0;
 	bool enabled = context_enabled(ce);
 
 	GEM_BUG_ON(!atomic_read(&ce->guc_id_ref));
@@ -271,13 +272,13 @@ static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
 		action[len++] = GUC_CONTEXT_ENABLE;
 		set_context_pending_enable(ce);
 		intel_context_get(ce);
+		g2h_len_dw = G2H_LEN_DW_SCHED_CONTEXT_MODE_SET;
 	} else {
 		action[len++] = INTEL_GUC_ACTION_SCHED_CONTEXT;
 		action[len++] = ce->guc_id;
 	}
 
-	err = intel_guc_send_nb(guc, action, len);
-
+	err = intel_guc_send_nb(guc, action, len, g2h_len_dw);
 	if (!enabled && !err) {
 		set_context_enabled(ce);
 	} else if (!enabled) {
@@ -730,7 +731,7 @@ static int __guc_action_register_context(struct intel_guc *guc,
 		offset,
 	};
 
-	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
 }
 
 static int register_context(struct intel_context *ce)
@@ -750,7 +751,8 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
 		guc_id,
 	};
 
-	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action),
+					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
 }
 
 static int deregister_context(struct intel_context *ce, u32 guc_id)
@@ -891,7 +893,8 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
 
 	intel_context_get(ce);
 
-	intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), true);
+	intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action),
+				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
 }
 
 static u16 prep_context_pending_disable(struct intel_context *ce)
Signature · 6 lines
2.28.0

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

PATCH 15/51 drm/i915/guc: Update intel_gt_wait_for_idle to work with GuC

In reply to Matthew Brost

When running the GuC the GPU can't be considered idle if the GuC still has contexts pinned. As such, a call has been added in intel_gt_wait_for_idle to idle the UC and in turn the GuC by waiting for the number of unpinned contexts to go to zero.

v2: rtimeout -> remaining_timeout
v3: Drop unnecessary includes, guc_submission_busy_loop -> guc_submission_send_busy_loop, drop negatie timeout trick, move a refactor of guc_context_unpin to earlier path (John H)

---
 drivers/gpu/drm/i915/gem/i915_gem_mman.c      |  3 +-
 drivers/gpu/drm/i915/gt/intel_gt.c            | 19 +++++
 drivers/gpu/drm/i915/gt/intel_gt.h            |  2 +
 drivers/gpu/drm/i915/gt/intel_gt_requests.c   | 21 ++---
 drivers/gpu/drm/i915/gt/intel_gt_requests.h   |  7 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  4 +
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |  1 +
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |  4 +
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 85 +++++++++++++++++--
 drivers/gpu/drm/i915/gt/uc/intel_uc.h         |  5 ++
 drivers/gpu/drm/i915/i915_gem_evict.c         |  1 +
 .../gpu/drm/i915/selftests/igt_live_test.c    |  2 +-
 .../gpu/drm/i915/selftests/mock_gem_device.c  |  3 +-
 13 files changed, 129 insertions(+), 28 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +2 -1
index a90f796e85c0..6fffd4d377c2 100644
@@ -645,7 +645,8 @@ mmap_offset_attach(struct drm_i915_gem_object *obj,
 		goto insert;
 
 	/* Attempt to reap some mmap space from dead objects */
-	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT);
+	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT,
+					       NULL);
 	if (err)
 		goto err;
 
drivers/gpu/drm/i915/gt/intel_gt.c +19 -0
index e714e21c0a4d..acfdd53b2678 100644
@@ -585,6 +585,25 @@ static void __intel_gt_disable(struct intel_gt *gt)
 	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
 }
 
+int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
+{
+	long remaining_timeout;
+
+	/* If the device is asleep, we have no requests outstanding */
+	if (!intel_gt_pm_is_awake(gt))
+		return 0;
+
+	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
+							   &remaining_timeout)) > 0) {
+		cond_resched();
+		if (signal_pending(current))
+			return -EINTR;
+	}
+
+	return timeout ? timeout : intel_uc_wait_for_idle(&gt->uc,
+							  remaining_timeout);
+}
+
 int intel_gt_init(struct intel_gt *gt)
 {
 	int err;
drivers/gpu/drm/i915/gt/intel_gt.h +2 -0
index e7aabe0cc5bf..74e771871a9b 100644
@@ -48,6 +48,8 @@ void intel_gt_driver_release(struct intel_gt *gt);
 
 void intel_gt_driver_late_release(struct intel_gt *gt);
 
+int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout);
+
 void intel_gt_check_and_clear_faults(struct intel_gt *gt);
 void intel_gt_clear_error_registers(struct intel_gt *gt,
 				    intel_engine_mask_t engine_mask);
drivers/gpu/drm/i915/gt/intel_gt_requests.c +5 -16
index 647eca9d867a..edb881d75630 100644
@@ -130,7 +130,8 @@ void intel_engine_fini_retire(struct intel_engine_cs *engine)
 	GEM_BUG_ON(engine->retire);
 }
 
-long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout)
+long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout,
+				      long *remaining_timeout)
 {
 	struct intel_gt_timelines *timelines = &gt->timelines;
 	struct intel_timeline *tl, *tn;
@@ -195,22 +196,10 @@ out_active:	spin_lock(&timelines->lock);
 	if (flush_submission(gt, timeout)) /* Wait, there's more! */
 		active_count++;
 
-	return active_count ? timeout : 0;
-}
-
-int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
-{
-	/* If the device is asleep, we have no requests outstanding */
-	if (!intel_gt_pm_is_awake(gt))
-		return 0;
-
-	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout)) > 0) {
-		cond_resched();
-		if (signal_pending(current))
-			return -EINTR;
-	}
+	if (remaining_timeout)
+		*remaining_timeout = timeout;
 
-	return timeout;
+	return active_count ? timeout : 0;
 }
 
 static void retire_work_handler(struct work_struct *work)
drivers/gpu/drm/i915/gt/intel_gt_requests.h +3 -4
index fcc30a6e4fe9..83ff5280c06e 100644
@@ -10,10 +10,11 @@ struct intel_engine_cs;
 struct intel_gt;
 struct intel_timeline;
 
-long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout);
+long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout,
+				      long *remaining_timeout);
 static inline void intel_gt_retire_requests(struct intel_gt *gt)
 {
-	intel_gt_retire_requests_timeout(gt, 0);
+	intel_gt_retire_requests_timeout(gt, 0, NULL);
 }
 
 void intel_engine_init_retire(struct intel_engine_cs *engine);
@@ -21,8 +22,6 @@ void intel_engine_add_retire(struct intel_engine_cs *engine,
 			     struct intel_timeline *tl);
 void intel_engine_fini_retire(struct intel_engine_cs *engine);
 
-int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout);
-
 void intel_gt_init_requests(struct intel_gt *gt);
 void intel_gt_park_requests(struct intel_gt *gt);
 void intel_gt_unpark_requests(struct intel_gt *gt);
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 80b88bae5f24..3cc566565224 100644
@@ -39,6 +39,8 @@ struct intel_guc {
 	spinlock_t irq_lock;
 	unsigned int msg_enabled_mask;
 
+	atomic_t outstanding_submission_g2h;
+
 	struct {
 		void (*reset)(struct intel_guc *guc);
 		void (*enable)(struct intel_guc *guc);
@@ -238,6 +240,8 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
 	spin_unlock_irq(&guc->irq_lock);
 }
 
+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);
 
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +1 -0
index c33906ec478d..f1cbed6b9f0a 100644
@@ -109,6 +109,7 @@ void intel_guc_ct_init_early(struct intel_guc_ct *ct)
 	INIT_LIST_HEAD(&ct->requests.incoming);
 	INIT_WORK(&ct->requests.worker, ct_incoming_request_worker_func);
 	tasklet_setup(&ct->receive_tasklet, ct_receive_tasklet_func);
+	init_waitqueue_head(&ct->wq);
 }
 
 static inline const char *guc_ct_buffer_type_to_str(u32 type)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h +4 -0
index 785dfc5c6efb..4b30a562ae63 100644
@@ -10,6 +10,7 @@
 #include <linux/spinlock.h>
 #include <linux/workqueue.h>
 #include <linux/ktime.h>
+#include <linux/wait.h>
 
 #include "intel_guc_fwif.h"
 
@@ -68,6 +69,9 @@ struct intel_guc_ct {
 
 	struct tasklet_struct receive_tasklet;
 
+	/** @wq: wait queue for g2h chanenl */
+	wait_queue_head_t wq;
+
 	struct {
 		u16 last_fence; /* last fence used to send request */
 
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +80 -5

133 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/intel_uc.h +5 -0
index 9c954c589edf..c4cef885e984 100644
@@ -81,6 +81,11 @@ uc_state_checkers(guc, guc_submission);
 #undef uc_state_checkers
 #undef __uc_state_checker
 
+static inline int intel_uc_wait_for_idle(struct intel_uc *uc, long timeout)
+{
+	return intel_guc_wait_for_idle(&uc->guc, timeout);
+}
+
 #define intel_uc_ops_function(_NAME, _OPS, _TYPE, _RET) \
 static inline _TYPE intel_uc_##_NAME(struct intel_uc *uc) \
 { \
drivers/gpu/drm/i915/i915_gem_evict.c +1 -0
index 4d2d59a9942b..2b73ddb11c66 100644
@@ -27,6 +27,7 @@
  */
 
 #include "gem/i915_gem_context.h"
+#include "gt/intel_gt.h"
 #include "gt/intel_gt_requests.h"
 
 #include "i915_drv.h"
drivers/gpu/drm/i915/selftests/igt_live_test.c +1 -1
index c130010a7033..1c721542e277 100644
@@ -5,7 +5,7 @@
  */
 
 #include "i915_drv.h"
-#include "gt/intel_gt_requests.h"
+#include "gt/intel_gt.h"
 
 #include "../i915_selftest.h"
 #include "igt_flush_test.h"
drivers/gpu/drm/i915/selftests/mock_gem_device.c +2 -1
index d189c4bd4bef..4f8180146888 100644
@@ -52,7 +52,8 @@ void mock_device_flush(struct drm_i915_private *i915)
 	do {
 		for_each_engine(engine, gt, id)
 			mock_engine_flush(engine);
-	} while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT));
+	} while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT,
+						  NULL));
 }
 
 static void mock_device_release(struct drm_device *dev)
Signature · 1 line
2.28.0
#30 lore raw

Re: [PATCH 15/51] drm/i915/guc: Update intel_gt_wait_for_idle to work with GuC

drivers/gpu/drm/i915/gt/intel_gt_requests.h

Quoted 129 lines · Matthew Brost

When running the GuC the GPU can't be considered idle if the GuC still has contexts pinned. As such, a call has been added in intel_gt_wait_for_idle to idle the UC and in turn the GuC by waiting for the number of unpinned contexts to go to zero.

v2: rtimeout -> remaining_timeout
v3: Drop unnecessary includes, guc_submission_busy_loop -> guc_submission_send_busy_loop, drop negatie timeout trick, move a refactor of guc_context_unpin to earlier path (John H)

---
  drivers/gpu/drm/i915/gem/i915_gem_mman.c      |  3 +-
  drivers/gpu/drm/i915/gt/intel_gt.c            | 19 +++++
  drivers/gpu/drm/i915/gt/intel_gt.h            |  2 +
  drivers/gpu/drm/i915/gt/intel_gt_requests.c   | 21 ++---
  drivers/gpu/drm/i915/gt/intel_gt_requests.h   |  7 +-
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  4 +
  drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |  1 +
  drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |  4 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 85 +++++++++++++++++--
  drivers/gpu/drm/i915/gt/uc/intel_uc.h         |  5 ++
  drivers/gpu/drm/i915/i915_gem_evict.c         |  1 +
  .../gpu/drm/i915/selftests/igt_live_test.c    |  2 +-
  .../gpu/drm/i915/selftests/mock_gem_device.c  |  3 +-
  13 files changed, 129 insertions(+), 28 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +2 -1
index a90f796e85c0..6fffd4d377c2 100644
@@ -645,7 +645,8 @@ mmap_offset_attach(struct drm_i915_gem_object *obj,
  		goto insert;
 
  	/* Attempt to reap some mmap space from dead objects */
-	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT);
+	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT,
+					       NULL);
  	if (err)
  		goto err;
 
drivers/gpu/drm/i915/gt/intel_gt.c +19 -0
index e714e21c0a4d..acfdd53b2678 100644
@@ -585,6 +585,25 @@ static void __intel_gt_disable(struct intel_gt *gt)
  	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
  }
 
+int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
+{
+	long remaining_timeout;
+
+	/* If the device is asleep, we have no requests outstanding */
+	if (!intel_gt_pm_is_awake(gt))
+		return 0;
+
+	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
+							   &remaining_timeout)) > 0) {
+		cond_resched();
+		if (signal_pending(current))
+			return -EINTR;
+	}
+
+	return timeout ? timeout : intel_uc_wait_for_idle(&gt->uc,
+							  remaining_timeout);
+}
+
  int intel_gt_init(struct intel_gt *gt)
  {
  	int err;
drivers/gpu/drm/i915/gt/intel_gt.h +2 -0
index e7aabe0cc5bf..74e771871a9b 100644
@@ -48,6 +48,8 @@ void intel_gt_driver_release(struct intel_gt *gt);
 
  void intel_gt_driver_late_release(struct intel_gt *gt);
 
+int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout);
+
  void intel_gt_check_and_clear_faults(struct intel_gt *gt);
  void intel_gt_clear_error_registers(struct intel_gt *gt,
  				    intel_engine_mask_t engine_mask);
drivers/gpu/drm/i915/gt/intel_gt_requests.c +5 -16
index 647eca9d867a..edb881d75630 100644
@@ -130,7 +130,8 @@ void intel_engine_fini_retire(struct intel_engine_cs *engine)
  	GEM_BUG_ON(engine->retire);
  }
 
-long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout)
+long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout,
+				      long *remaining_timeout)
  {
  	struct intel_gt_timelines *timelines = &gt->timelines;
  	struct intel_timeline *tl, *tn;
@@ -195,22 +196,10 @@ out_active:	spin_lock(&timelines->lock);
  	if (flush_submission(gt, timeout)) /* Wait, there's more! */
  		active_count++;
 
-	return active_count ? timeout : 0;
-}
-
-int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
-{
-	/* If the device is asleep, we have no requests outstanding */
-	if (!intel_gt_pm_is_awake(gt))
-		return 0;
-
-	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout)) > 0) {
-		cond_resched();
-		if (signal_pending(current))
-			return -EINTR;
-	}
+	if (remaining_timeout)
+		*remaining_timeout = timeout;
 
-	return timeout;
+	return active_count ? timeout : 0;
  }
 
  static void retire_work_handler(struct work_struct *work)
drivers/gpu/drm/i915/gt/intel_gt_requests.h +0 -0
index fcc30a6e4fe9..83ff5280c06e 100644

You were saying the the include of stddef is needed here?

Quoted 245 lines
drivers/gpu/drm/i915/gt/intel_gt_requests.h +3 -4
@@ -10,10 +10,11 @@ struct intel_engine_cs;
  struct intel_gt;
  struct intel_timeline;
 
-long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout);
+long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout,
+				      long *remaining_timeout);
  static inline void intel_gt_retire_requests(struct intel_gt *gt)
  {
-	intel_gt_retire_requests_timeout(gt, 0);
+	intel_gt_retire_requests_timeout(gt, 0, NULL);
  }
 
  void intel_engine_init_retire(struct intel_engine_cs *engine);
@@ -21,8 +22,6 @@ void intel_engine_add_retire(struct intel_engine_cs *engine,
  			     struct intel_timeline *tl);
  void intel_engine_fini_retire(struct intel_engine_cs *engine);
 
-int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout);
-
  void intel_gt_init_requests(struct intel_gt *gt);
  void intel_gt_park_requests(struct intel_gt *gt);
  void intel_gt_unpark_requests(struct intel_gt *gt);
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 80b88bae5f24..3cc566565224 100644
@@ -39,6 +39,8 @@ struct intel_guc {
  	spinlock_t irq_lock;
  	unsigned int msg_enabled_mask;
 
+	atomic_t outstanding_submission_g2h;
+
  	struct {
  		void (*reset)(struct intel_guc *guc);
  		void (*enable)(struct intel_guc *guc);
@@ -238,6 +240,8 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
  	spin_unlock_irq(&guc->irq_lock);
  }
 
+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);
 
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +1 -0
index c33906ec478d..f1cbed6b9f0a 100644
@@ -109,6 +109,7 @@ void intel_guc_ct_init_early(struct intel_guc_ct *ct)
  	INIT_LIST_HEAD(&ct->requests.incoming);
  	INIT_WORK(&ct->requests.worker, ct_incoming_request_worker_func);
  	tasklet_setup(&ct->receive_tasklet, ct_receive_tasklet_func);
+	init_waitqueue_head(&ct->wq);
  }
 
  static inline const char *guc_ct_buffer_type_to_str(u32 type)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h +4 -0
index 785dfc5c6efb..4b30a562ae63 100644
@@ -10,6 +10,7 @@
  #include <linux/spinlock.h>
  #include <linux/workqueue.h>
  #include <linux/ktime.h>
+#include <linux/wait.h>
 
  #include "intel_guc_fwif.h"
 
@@ -68,6 +69,9 @@ struct intel_guc_ct {
 
  	struct tasklet_struct receive_tasklet;
 
+	/** @wq: wait queue for g2h chanenl */
+	wait_queue_head_t wq;
+
  	struct {
  		u16 last_fence; /* last fence used to send request */
 
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +80 -5
index f7e34baa9506..088d11e2e497 100644
@@ -254,6 +254,69 @@ static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
  	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
  }
 
+static int guc_submission_send_busy_loop(struct intel_guc* guc,
+					 const u32 *action,
+					 u32 len,
+					 u32 g2h_len_dw,
+					 bool loop)
+{
+	int err;
+
+	err = intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop);
+
+	if (!err && g2h_len_dw)
+		atomic_inc(&guc->outstanding_submission_g2h);
+
+	return err;
+}
+
+static int guc_wait_for_pending_msg(struct intel_guc *guc,
+				    atomic_t *wait_var,
+				    bool interruptible,
+				    long timeout)
+{
+	const int state = interruptible ?
+		TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
+	DEFINE_WAIT(wait);
+
+	might_sleep();
+	GEM_BUG_ON(timeout < 0);
+
+	if (!atomic_read(wait_var))
+		return 0;
+
+	if (!timeout)
+		return -ETIME;
+
+	for (;;) {
+		prepare_to_wait(&guc->ct.wq, &wait, state);
+
+		if (!atomic_read(wait_var))
+			break;
+
+		if (signal_pending_state(state, current)) {
+			timeout = -EINTR;
+			break;
+		}
+
+		if (!timeout) {
+			timeout = -ETIME;
+			break;
+		}
+
+		timeout = io_schedule_timeout(timeout);
+	}
+	finish_wait(&guc->ct.wq, &wait);
+
+	return (timeout < 0) ? timeout : 0;
+}
+
+int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
+{
+	return guc_wait_for_pending_msg(guc, &guc->outstanding_submission_g2h,
+					true, timeout);
+}
+
  static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  {
  	int err;
@@ -280,6 +343,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) {
+		atomic_inc(&guc->outstanding_submission_g2h);
  		set_context_enabled(ce);
  	} else if (!enabled) {
  		clr_context_pending_enable(ce);
@@ -731,7 +795,8 @@ static int __guc_action_register_context(struct intel_guc *guc,
  		offset,
  	};
 
-	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
+	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+					     0, true);
  }
 
  static int register_context(struct intel_context *ce)
@@ -751,8 +816,9 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
  		guc_id,
  	};
 
-	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action),
-					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
+	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+					     G2H_LEN_DW_DEREGISTER_CONTEXT,
+					     true);
  }
 
  static int deregister_context(struct intel_context *ce, u32 guc_id)
@@ -893,8 +959,8 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
 
  	intel_context_get(ce);
 
-	intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action),
-				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
+	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+				      G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
  }
 
  static u16 prep_context_pending_disable(struct intel_context *ce)
@@ -1440,6 +1506,12 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  	return ce;
  }
 
+static void decr_outstanding_submission_g2h(struct intel_guc *guc)
+{
+	if (atomic_dec_and_test(&guc->outstanding_submission_g2h))
+		wake_up_all(&guc->ct.wq);
+}
+
  int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  					  const u32 *msg,
  					  u32 len)
@@ -1475,6 +1547,8 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		lrc_destroy(&ce->ref);
  	}
 
+	decr_outstanding_submission_g2h(guc);
+
  	return 0;
  }
 
@@ -1523,6 +1597,7 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
  		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
  	}
 
+	decr_outstanding_submission_g2h(guc);
  	intel_context_put(ce);
 
  	return 0;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +5 -0
index 9c954c589edf..c4cef885e984 100644
@@ -81,6 +81,11 @@ uc_state_checkers(guc, guc_submission);
  #undef uc_state_checkers
  #undef __uc_state_checker
 
+static inline int intel_uc_wait_for_idle(struct intel_uc *uc, long timeout)
+{
+	return intel_guc_wait_for_idle(&uc->guc, timeout);
+}
+
  #define intel_uc_ops_function(_NAME, _OPS, _TYPE, _RET) \
  static inline _TYPE intel_uc_##_NAME(struct intel_uc *uc) \
  { \
drivers/gpu/drm/i915/i915_gem_evict.c +0 -0
index 4d2d59a9942b..2b73ddb11c66 100644

drivers/gpu/drm/i915/i915_gem_evict.c line 30

Quoted 4 lines
drivers/gpu/drm/i915/i915_gem_evict.c +1 -0
@@ -27,6 +27,7 @@
   */
 
  #include "gem/i915_gem_context.h"
+#include "gt/intel_gt.h"

Still not seeing a need for this.

drivers/gpu/drm/i915/selftests/igt_live_test.c line 8

Quoted 13 lines
drivers/gpu/drm/i915/i915_gem_evict.c +0 -0
  #include "gt/intel_gt_requests.h"
 
  #include "i915_drv.h"
drivers/gpu/drm/i915/selftests/igt_live_test.c +1 -1
index c130010a7033..1c721542e277 100644
@@ -5,7 +5,7 @@
   */
 
  #include "i915_drv.h"
-#include "gt/intel_gt_requests.h"
+#include "gt/intel_gt.h"

Nor this.

John.

Quoted 17 lines
drivers/gpu/drm/i915/selftests/igt_live_test.c +0 -0
  #include "../i915_selftest.h"
  #include "igt_flush_test.h"
drivers/gpu/drm/i915/selftests/mock_gem_device.c +2 -1
index d189c4bd4bef..4f8180146888 100644
@@ -52,7 +52,8 @@ void mock_device_flush(struct drm_i915_private *i915)
  	do {
  		for_each_engine(engine, gt, id)
  			mock_engine_flush(engine);
-	} while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT));
+	} while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT,
+						  NULL));
  }
 
  static void mock_device_release(struct drm_device *dev)
#31 lore raw

Re: [PATCH 15/51] drm/i915/guc: Update intel_gt_wait_for_idle to work with GuC

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

When running the GuC the GPU can't be considered idle if the GuC still has contexts pinned. As such, a call has been added in intel_gt_wait_for_idle to idle the UC and in turn the GuC by waiting for the number of unpinned contexts to go to zero.

v2: rtimeout -> remaining_timeout
v3: Drop unnecessary includes, guc_submission_busy_loop -> guc_submission_send_busy_loop, drop negatie timeout trick, move a refactor of guc_context_unpin to earlier path (John H)

---
  drivers/gpu/drm/i915/gem/i915_gem_mman.c      |  3 +-
  drivers/gpu/drm/i915/gt/intel_gt.c            | 19 +++++
  drivers/gpu/drm/i915/gt/intel_gt.h            |  2 +
  drivers/gpu/drm/i915/gt/intel_gt_requests.c   | 21 ++---
  drivers/gpu/drm/i915/gt/intel_gt_requests.h   |  7 +-
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  4 +
  drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |  1 +
  drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |  4 +
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 85 +++++++++++++++++--
  drivers/gpu/drm/i915/gt/uc/intel_uc.h         |  5 ++
  drivers/gpu/drm/i915/i915_gem_evict.c         |  1 +
  .../gpu/drm/i915/selftests/igt_live_test.c    |  2 +-
  .../gpu/drm/i915/selftests/mock_gem_device.c  |  3 +-
  13 files changed, 129 insertions(+), 28 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +2 -1
index a90f796e85c0..6fffd4d377c2 100644
@@ -645,7 +645,8 @@ mmap_offset_attach(struct drm_i915_gem_object *obj,
  		goto insert;
  	/* Attempt to reap some mmap space from dead objects */
-	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT);
+	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT,
+					       NULL);
  	if (err)
  		goto err;
drivers/gpu/drm/i915/gt/intel_gt.c +19 -0
index e714e21c0a4d..acfdd53b2678 100644
@@ -585,6 +585,25 @@ static void __intel_gt_disable(struct intel_gt *gt)
  	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
  }
+int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
+{
+	long remaining_timeout;
+
+	/* If the device is asleep, we have no requests outstanding */
+	if (!intel_gt_pm_is_awake(gt))
+		return 0;
+
+	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
+							   &remaining_timeout)) > 0) {
+		cond_resched();
+		if (signal_pending(current))
+			return -EINTR;
+	}
+
+	return timeout ? timeout : intel_uc_wait_for_idle(&gt->uc,
+							  remaining_timeout);
+}
+
  int intel_gt_init(struct intel_gt *gt)
  {
  	int err;
drivers/gpu/drm/i915/gt/intel_gt.h +2 -0
index e7aabe0cc5bf..74e771871a9b 100644
@@ -48,6 +48,8 @@ void intel_gt_driver_release(struct intel_gt *gt);
  void intel_gt_driver_late_release(struct intel_gt *gt);
+int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout);
+
  void intel_gt_check_and_clear_faults(struct intel_gt *gt);
  void intel_gt_clear_error_registers(struct intel_gt *gt,
  				    intel_engine_mask_t engine_mask);
drivers/gpu/drm/i915/gt/intel_gt_requests.c +4 -16
index 647eca9d867a..edb881d75630 100644
@@ -130,7 +130,8 @@ void intel_engine_fini_retire(struct intel_engine_cs *engine)
  	GEM_BUG_ON(engine->retire);
  }
-long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout)
+long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout,
+				      long *remaining_timeout)
  {
  	struct intel_gt_timelines *timelines = &gt->timelines;
  	struct intel_timeline *tl, *tn;
@@ -195,22 +196,10 @@ out_active:	spin_lock(&timelines->lock);
  	if (flush_submission(gt, timeout)) /* Wait, there's more! */
  		active_count++;
-	return active_count ? timeout : 0;
-}
-
-int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
-{
-	/* If the device is asleep, we have no requests outstanding */
-	if (!intel_gt_pm_is_awake(gt))
-		return 0;
-
-	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout)) > 0) {
-		cond_resched();
-		if (signal_pending(current))
-			return -EINTR;
-	}
+	if (remaining_timeout)
+		*remaining_timeout = timeout;
-	return timeout;
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_gt_requests.h

Quoted 7 lines
drivers/gpu/drm/i915/gt/intel_gt_requests.c +1 -0
+	return active_count ? timeout : 0;
  }
  static void retire_work_handler(struct work_struct *work)
drivers/gpu/drm/i915/gt/intel_gt_requests.h +0 -0
index fcc30a6e4fe9..83ff5280c06e 100644

You were saying the the include of stddef is needed here?

Yes, HDRTEST [1] complains otherwise.

[1] https://patchwork.freedesktop.org/series/91840/#rev3

Quoted 223 lines
Quoted 218 lines
drivers/gpu/drm/i915/gt/intel_gt_requests.h +3 -4
@@ -10,10 +10,11 @@ struct intel_engine_cs;
  struct intel_gt;
  struct intel_timeline;
-long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout);
+long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout,
+				      long *remaining_timeout);
  static inline void intel_gt_retire_requests(struct intel_gt *gt)
  {
-	intel_gt_retire_requests_timeout(gt, 0);
+	intel_gt_retire_requests_timeout(gt, 0, NULL);
  }
  void intel_engine_init_retire(struct intel_engine_cs *engine);
@@ -21,8 +22,6 @@ void intel_engine_add_retire(struct intel_engine_cs *engine,
  			     struct intel_timeline *tl);
  void intel_engine_fini_retire(struct intel_engine_cs *engine);
-int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout);
-
  void intel_gt_init_requests(struct intel_gt *gt);
  void intel_gt_park_requests(struct intel_gt *gt);
  void intel_gt_unpark_requests(struct intel_gt *gt);
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 80b88bae5f24..3cc566565224 100644
@@ -39,6 +39,8 @@ struct intel_guc {
  	spinlock_t irq_lock;
  	unsigned int msg_enabled_mask;
+	atomic_t outstanding_submission_g2h;
+
  	struct {
  		void (*reset)(struct intel_guc *guc);
  		void (*enable)(struct intel_guc *guc);
@@ -238,6 +240,8 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
  	spin_unlock_irq(&guc->irq_lock);
  }
+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);
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +1 -0
index c33906ec478d..f1cbed6b9f0a 100644
@@ -109,6 +109,7 @@ void intel_guc_ct_init_early(struct intel_guc_ct *ct)
  	INIT_LIST_HEAD(&ct->requests.incoming);
  	INIT_WORK(&ct->requests.worker, ct_incoming_request_worker_func);
  	tasklet_setup(&ct->receive_tasklet, ct_receive_tasklet_func);
+	init_waitqueue_head(&ct->wq);
  }
  static inline const char *guc_ct_buffer_type_to_str(u32 type)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h +4 -0
index 785dfc5c6efb..4b30a562ae63 100644
@@ -10,6 +10,7 @@
  #include <linux/spinlock.h>
  #include <linux/workqueue.h>
  #include <linux/ktime.h>
+#include <linux/wait.h>
  #include "intel_guc_fwif.h"
@@ -68,6 +69,9 @@ struct intel_guc_ct {
  	struct tasklet_struct receive_tasklet;
+	/** @wq: wait queue for g2h chanenl */
+	wait_queue_head_t wq;
+
  	struct {
  		u16 last_fence; /* last fence used to send request */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +80 -5
index f7e34baa9506..088d11e2e497 100644
@@ -254,6 +254,69 @@ static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
  	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
  }
+static int guc_submission_send_busy_loop(struct intel_guc* guc,
+					 const u32 *action,
+					 u32 len,
+					 u32 g2h_len_dw,
+					 bool loop)
+{
+	int err;
+
+	err = intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop);
+
+	if (!err && g2h_len_dw)
+		atomic_inc(&guc->outstanding_submission_g2h);
+
+	return err;
+}
+
+static int guc_wait_for_pending_msg(struct intel_guc *guc,
+				    atomic_t *wait_var,
+				    bool interruptible,
+				    long timeout)
+{
+	const int state = interruptible ?
+		TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
+	DEFINE_WAIT(wait);
+
+	might_sleep();
+	GEM_BUG_ON(timeout < 0);
+
+	if (!atomic_read(wait_var))
+		return 0;
+
+	if (!timeout)
+		return -ETIME;
+
+	for (;;) {
+		prepare_to_wait(&guc->ct.wq, &wait, state);
+
+		if (!atomic_read(wait_var))
+			break;
+
+		if (signal_pending_state(state, current)) {
+			timeout = -EINTR;
+			break;
+		}
+
+		if (!timeout) {
+			timeout = -ETIME;
+			break;
+		}
+
+		timeout = io_schedule_timeout(timeout);
+	}
+	finish_wait(&guc->ct.wq, &wait);
+
+	return (timeout < 0) ? timeout : 0;
+}
+
+int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
+{
+	return guc_wait_for_pending_msg(guc, &guc->outstanding_submission_g2h,
+					true, timeout);
+}
+
  static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
  {
  	int err;
@@ -280,6 +343,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) {
+		atomic_inc(&guc->outstanding_submission_g2h);
  		set_context_enabled(ce);
  	} else if (!enabled) {
  		clr_context_pending_enable(ce);
@@ -731,7 +795,8 @@ static int __guc_action_register_context(struct intel_guc *guc,
  		offset,
  	};
-	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
+	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+					     0, true);
  }
  static int register_context(struct intel_context *ce)
@@ -751,8 +816,9 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
  		guc_id,
  	};
-	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action),
-					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
+	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+					     G2H_LEN_DW_DEREGISTER_CONTEXT,
+					     true);
  }
  static int deregister_context(struct intel_context *ce, u32 guc_id)
@@ -893,8 +959,8 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
  	intel_context_get(ce);
-	intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action),
-				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
+	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+				      G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
  }
  static u16 prep_context_pending_disable(struct intel_context *ce)
@@ -1440,6 +1506,12 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  	return ce;
  }
+static void decr_outstanding_submission_g2h(struct intel_guc *guc)
+{
+	if (atomic_dec_and_test(&guc->outstanding_submission_g2h))
+		wake_up_all(&guc->ct.wq);
+}
+
  int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  					  const u32 *msg,
  					  u32 len)
@@ -1475,6 +1547,8 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		lrc_destroy(&ce->ref);
  	}
+	decr_outstanding_submission_g2h(guc);
+
  	return 0;
  }
@@ -1523,6 +1597,7 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
  		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
  	}
+	decr_outstanding_submission_g2h(guc);
  	intel_context_put(ce);
  	return 0;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +5 -0
index 9c954c589edf..c4cef885e984 100644
@@ -81,6 +81,11 @@ uc_state_checkers(guc, guc_submission);
  #undef uc_state_checkers
  #undef __uc_state_checker
+static inline int intel_uc_wait_for_idle(struct intel_uc *uc, long timeout)
+{
+	return intel_guc_wait_for_idle(&uc->guc, timeout);
+}
+
  #define intel_uc_ops_function(_NAME, _OPS, _TYPE, _RET) \
  static inline _TYPE intel_uc_##_NAME(struct intel_uc *uc) \
  { \
drivers/gpu/drm/i915/i915_gem_evict.c +0 -0
index 4d2d59a9942b..2b73ddb11c66 100644

drivers/gpu/drm/i915/i915_gem_evict.c line 29

Quoted 3 lines
drivers/gpu/drm/i915/i915_gem_evict.c +1 -0
@@ -27,6 +27,7 @@
   */
  #include "gem/i915_gem_context.h"
+#include "gt/intel_gt.h"

Still not seeing a need for this.

Quoted 13 lines

drivers/gpu/drm/i915/selftests/igt_live_test.c line 7

Quoted 11 lines
drivers/gpu/drm/i915/i915_gem_evict.c +0 -0
  #include "gt/intel_gt_requests.h"
  #include "i915_drv.h"
drivers/gpu/drm/i915/selftests/igt_live_test.c +1 -1
index c130010a7033..1c721542e277 100644
@@ -5,7 +5,7 @@
   */
  #include "i915_drv.h"
-#include "gt/intel_gt_requests.h"
+#include "gt/intel_gt.h"

Nor this.

We need these because intel_gt_wait_for_idle which moved from "gt/intel_gt_requests.h" to "gt/intel_gt.h".

Matt

Quoted 18 lines

John.

Quoted 7 lines
drivers/gpu/drm/i915/selftests/igt_live_test.c +0 -0
  #include "../i915_selftest.h"
  #include "igt_flush_test.h"
drivers/gpu/drm/i915/selftests/mock_gem_device.c +0 -0
index d189c4bd4bef..4f8180146888 100644

drivers/gpu/drm/i915/selftests/mock_gem_device.c void mock_device_flush(struct drm_i915_private *i915) · line 58

Quoted 8 lines
drivers/gpu/drm/i915/selftests/mock_gem_device.c +2 -1
@@ -52,7 +52,8 @@ void mock_device_flush(struct drm_i915_private *i915)
  	do {
  		for_each_engine(engine, gt, id)
  			mock_engine_flush(engine);
-	} while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT));
+	} while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT,
+						  NULL));
  }
  static void mock_device_release(struct drm_device *dev)

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

Reviewed-by
#32 lore raw

Re: [PATCH 15/51] drm/i915/guc: Update intel_gt_wait_for_idle to work with GuC

Quoted 364 lines · Matthew Brost
Quoted 114 lines · John Harrison
Quoted 113 lines · Matthew Brost

When running the GuC the GPU can't be considered idle if the GuC still has contexts pinned. As such, a call has been added in intel_gt_wait_for_idle to idle the UC and in turn the GuC by waiting for the number of unpinned contexts to go to zero.

v2: rtimeout -> remaining_timeout
v3: Drop unnecessary includes, guc_submission_busy_loop -> guc_submission_send_busy_loop, drop negatie timeout trick, move a refactor of guc_context_unpin to earlier path (John H)

---
   drivers/gpu/drm/i915/gem/i915_gem_mman.c      |  3 +-
   drivers/gpu/drm/i915/gt/intel_gt.c            | 19 +++++
   drivers/gpu/drm/i915/gt/intel_gt.h            |  2 +
   drivers/gpu/drm/i915/gt/intel_gt_requests.c   | 21 ++---
   drivers/gpu/drm/i915/gt/intel_gt_requests.h   |  7 +-
   drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  4 +
   drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |  1 +
   drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h     |  4 +
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 85 +++++++++++++++++--
   drivers/gpu/drm/i915/gt/uc/intel_uc.h         |  5 ++
   drivers/gpu/drm/i915/i915_gem_evict.c         |  1 +
   .../gpu/drm/i915/selftests/igt_live_test.c    |  2 +-
   .../gpu/drm/i915/selftests/mock_gem_device.c  |  3 +-
   13 files changed, 129 insertions(+), 28 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +2 -1
index a90f796e85c0..6fffd4d377c2 100644
@@ -645,7 +645,8 @@ mmap_offset_attach(struct drm_i915_gem_object *obj,
   		goto insert;
   	/* Attempt to reap some mmap space from dead objects */
-	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT);
+	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT,
+					       NULL);
   	if (err)
   		goto err;
drivers/gpu/drm/i915/gt/intel_gt.c +19 -0
index e714e21c0a4d..acfdd53b2678 100644
@@ -585,6 +585,25 @@ static void __intel_gt_disable(struct intel_gt *gt)
   	GEM_BUG_ON(intel_gt_pm_is_awake(gt));
   }
+int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
+{
+	long remaining_timeout;
+
+	/* If the device is asleep, we have no requests outstanding */
+	if (!intel_gt_pm_is_awake(gt))
+		return 0;
+
+	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout,
+							   &remaining_timeout)) > 0) {
+		cond_resched();
+		if (signal_pending(current))
+			return -EINTR;
+	}
+
+	return timeout ? timeout : intel_uc_wait_for_idle(&gt->uc,
+							  remaining_timeout);
+}
+
   int intel_gt_init(struct intel_gt *gt)
   {
   	int err;
drivers/gpu/drm/i915/gt/intel_gt.h +2 -0
index e7aabe0cc5bf..74e771871a9b 100644
@@ -48,6 +48,8 @@ void intel_gt_driver_release(struct intel_gt *gt);
   void intel_gt_driver_late_release(struct intel_gt *gt);
+int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout);
+
   void intel_gt_check_and_clear_faults(struct intel_gt *gt);
   void intel_gt_clear_error_registers(struct intel_gt *gt,
   				    intel_engine_mask_t engine_mask);
drivers/gpu/drm/i915/gt/intel_gt_requests.c +4 -16
index 647eca9d867a..edb881d75630 100644
@@ -130,7 +130,8 @@ void intel_engine_fini_retire(struct intel_engine_cs *engine)
   	GEM_BUG_ON(engine->retire);
   }
-long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout)
+long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout,
+				      long *remaining_timeout)
   {
   	struct intel_gt_timelines *timelines = &gt->timelines;
   	struct intel_timeline *tl, *tn;
@@ -195,22 +196,10 @@ out_active:	spin_lock(&timelines->lock);
   	if (flush_submission(gt, timeout)) /* Wait, there's more! */
   		active_count++;
-	return active_count ? timeout : 0;
-}
-
-int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout)
-{
-	/* If the device is asleep, we have no requests outstanding */
-	if (!intel_gt_pm_is_awake(gt))
-		return 0;
-
-	while ((timeout = intel_gt_retire_requests_timeout(gt, timeout)) > 0) {
-		cond_resched();
-		if (signal_pending(current))
-			return -EINTR;
-	}
+	if (remaining_timeout)
+		*remaining_timeout = timeout;
-	return timeout;
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_gt_requests.h

Quoted 7 lines
drivers/gpu/drm/i915/gt/intel_gt_requests.c +1 -0
+	return active_count ? timeout : 0;
   }
   static void retire_work_handler(struct work_struct *work)
drivers/gpu/drm/i915/gt/intel_gt_requests.h +0 -0
index fcc30a6e4fe9..83ff5280c06e 100644

You were saying the the include of stddef is needed here?

Yes, HDRTEST [1] complains otherwise.

[1] https://patchwork.freedesktop.org/series/91840/#rev3

Quoted 223 lines
Quoted 218 lines
drivers/gpu/drm/i915/gt/intel_gt_requests.h +3 -4
@@ -10,10 +10,11 @@ struct intel_engine_cs;
   struct intel_gt;
   struct intel_timeline;
-long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout);
+long intel_gt_retire_requests_timeout(struct intel_gt *gt, long timeout,
+				      long *remaining_timeout);
   static inline void intel_gt_retire_requests(struct intel_gt *gt)
   {
-	intel_gt_retire_requests_timeout(gt, 0);
+	intel_gt_retire_requests_timeout(gt, 0, NULL);
   }
   void intel_engine_init_retire(struct intel_engine_cs *engine);
@@ -21,8 +22,6 @@ void intel_engine_add_retire(struct intel_engine_cs *engine,
   			     struct intel_timeline *tl);
   void intel_engine_fini_retire(struct intel_engine_cs *engine);
-int intel_gt_wait_for_idle(struct intel_gt *gt, long timeout);
-
   void intel_gt_init_requests(struct intel_gt *gt);
   void intel_gt_park_requests(struct intel_gt *gt);
   void intel_gt_unpark_requests(struct intel_gt *gt);
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 80b88bae5f24..3cc566565224 100644
@@ -39,6 +39,8 @@ struct intel_guc {
   	spinlock_t irq_lock;
   	unsigned int msg_enabled_mask;
+	atomic_t outstanding_submission_g2h;
+
   	struct {
   		void (*reset)(struct intel_guc *guc);
   		void (*enable)(struct intel_guc *guc);
@@ -238,6 +240,8 @@ static inline void intel_guc_disable_msg(struct intel_guc *guc, u32 mask)
   	spin_unlock_irq(&guc->irq_lock);
   }
+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);
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +1 -0
index c33906ec478d..f1cbed6b9f0a 100644
@@ -109,6 +109,7 @@ void intel_guc_ct_init_early(struct intel_guc_ct *ct)
   	INIT_LIST_HEAD(&ct->requests.incoming);
   	INIT_WORK(&ct->requests.worker, ct_incoming_request_worker_func);
   	tasklet_setup(&ct->receive_tasklet, ct_receive_tasklet_func);
+	init_waitqueue_head(&ct->wq);
   }
   static inline const char *guc_ct_buffer_type_to_str(u32 type)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.h +4 -0
index 785dfc5c6efb..4b30a562ae63 100644
@@ -10,6 +10,7 @@
   #include <linux/spinlock.h>
   #include <linux/workqueue.h>
   #include <linux/ktime.h>
+#include <linux/wait.h>
   #include "intel_guc_fwif.h"
@@ -68,6 +69,9 @@ struct intel_guc_ct {
   	struct tasklet_struct receive_tasklet;
+	/** @wq: wait queue for g2h chanenl */
+	wait_queue_head_t wq;
+
   	struct {
   		u16 last_fence; /* last fence used to send request */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +80 -5
index f7e34baa9506..088d11e2e497 100644
@@ -254,6 +254,69 @@ static inline void set_lrc_desc_registered(struct intel_guc *guc, u32 id,
   	xa_store_irq(&guc->context_lookup, id, ce, GFP_ATOMIC);
   }
+static int guc_submission_send_busy_loop(struct intel_guc* guc,
+					 const u32 *action,
+					 u32 len,
+					 u32 g2h_len_dw,
+					 bool loop)
+{
+	int err;
+
+	err = intel_guc_send_busy_loop(guc, action, len, g2h_len_dw, loop);
+
+	if (!err && g2h_len_dw)
+		atomic_inc(&guc->outstanding_submission_g2h);
+
+	return err;
+}
+
+static int guc_wait_for_pending_msg(struct intel_guc *guc,
+				    atomic_t *wait_var,
+				    bool interruptible,
+				    long timeout)
+{
+	const int state = interruptible ?
+		TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
+	DEFINE_WAIT(wait);
+
+	might_sleep();
+	GEM_BUG_ON(timeout < 0);
+
+	if (!atomic_read(wait_var))
+		return 0;
+
+	if (!timeout)
+		return -ETIME;
+
+	for (;;) {
+		prepare_to_wait(&guc->ct.wq, &wait, state);
+
+		if (!atomic_read(wait_var))
+			break;
+
+		if (signal_pending_state(state, current)) {
+			timeout = -EINTR;
+			break;
+		}
+
+		if (!timeout) {
+			timeout = -ETIME;
+			break;
+		}
+
+		timeout = io_schedule_timeout(timeout);
+	}
+	finish_wait(&guc->ct.wq, &wait);
+
+	return (timeout < 0) ? timeout : 0;
+}
+
+int intel_guc_wait_for_idle(struct intel_guc *guc, long timeout)
+{
+	return guc_wait_for_pending_msg(guc, &guc->outstanding_submission_g2h,
+					true, timeout);
+}
+
   static int guc_add_request(struct intel_guc *guc, struct i915_request *rq)
   {
   	int err;
@@ -280,6 +343,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) {
+		atomic_inc(&guc->outstanding_submission_g2h);
   		set_context_enabled(ce);
   	} else if (!enabled) {
   		clr_context_pending_enable(ce);
@@ -731,7 +795,8 @@ static int __guc_action_register_context(struct intel_guc *guc,
   		offset,
   	};
-	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action), 0, true);
+	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+					     0, true);
   }
   static int register_context(struct intel_context *ce)
@@ -751,8 +816,9 @@ static int __guc_action_deregister_context(struct intel_guc *guc,
   		guc_id,
   	};
-	return intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action),
-					G2H_LEN_DW_DEREGISTER_CONTEXT, true);
+	return guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+					     G2H_LEN_DW_DEREGISTER_CONTEXT,
+					     true);
   }
   static int deregister_context(struct intel_context *ce, u32 guc_id)
@@ -893,8 +959,8 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
   	intel_context_get(ce);
-	intel_guc_send_busy_loop(guc, action, ARRAY_SIZE(action),
-				 G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
+	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
+				      G2H_LEN_DW_SCHED_CONTEXT_MODE_SET, true);
   }
   static u16 prep_context_pending_disable(struct intel_context *ce)
@@ -1440,6 +1506,12 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
   	return ce;
   }
+static void decr_outstanding_submission_g2h(struct intel_guc *guc)
+{
+	if (atomic_dec_and_test(&guc->outstanding_submission_g2h))
+		wake_up_all(&guc->ct.wq);
+}
+
   int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
   					  const u32 *msg,
   					  u32 len)
@@ -1475,6 +1547,8 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
   		lrc_destroy(&ce->ref);
   	}
+	decr_outstanding_submission_g2h(guc);
+
   	return 0;
   }
@@ -1523,6 +1597,7 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
   		spin_unlock_irqrestore(&ce->guc_state.lock, flags);
   	}
+	decr_outstanding_submission_g2h(guc);
   	intel_context_put(ce);
   	return 0;
drivers/gpu/drm/i915/gt/uc/intel_uc.h +5 -0
index 9c954c589edf..c4cef885e984 100644
@@ -81,6 +81,11 @@ uc_state_checkers(guc, guc_submission);
   #undef uc_state_checkers
   #undef __uc_state_checker
+static inline int intel_uc_wait_for_idle(struct intel_uc *uc, long timeout)
+{
+	return intel_guc_wait_for_idle(&uc->guc, timeout);
+}
+
   #define intel_uc_ops_function(_NAME, _OPS, _TYPE, _RET) \
   static inline _TYPE intel_uc_##_NAME(struct intel_uc *uc) \
   { \
drivers/gpu/drm/i915/i915_gem_evict.c +0 -0
index 4d2d59a9942b..2b73ddb11c66 100644

drivers/gpu/drm/i915/i915_gem_evict.c line 29

Quoted 3 lines
drivers/gpu/drm/i915/i915_gem_evict.c +1 -0
@@ -27,6 +27,7 @@
    */
   #include "gem/i915_gem_context.h"
+#include "gt/intel_gt.h"

Still not seeing a need for this.

Quoted 13 lines

drivers/gpu/drm/i915/selftests/igt_live_test.c line 7

Quoted 11 lines
drivers/gpu/drm/i915/i915_gem_evict.c +0 -0
   #include "gt/intel_gt_requests.h"
   #include "i915_drv.h"
drivers/gpu/drm/i915/selftests/igt_live_test.c +1 -1
index c130010a7033..1c721542e277 100644
@@ -5,7 +5,7 @@
    */
   #include "i915_drv.h"
-#include "gt/intel_gt_requests.h"
+#include "gt/intel_gt.h"

Nor this.

Quoted 4 lines

We need these because intel_gt_wait_for_idle which moved from "gt/intel_gt_requests.h" to "gt/intel_gt.h".

Matt

Ah, okay. That makes sense.

With the return of stddef.h above...

Quoted 18 lines
Quoted 18 lines

John.

Quoted 7 lines
drivers/gpu/drm/i915/selftests/igt_live_test.c +0 -0
   #include "../i915_selftest.h"
   #include "igt_flush_test.h"
drivers/gpu/drm/i915/selftests/mock_gem_device.c +0 -0
index d189c4bd4bef..4f8180146888 100644

drivers/gpu/drm/i915/selftests/mock_gem_device.c void mock_device_flush(struct drm_i915_private *i915) · line 58

Quoted 8 lines
drivers/gpu/drm/i915/selftests/mock_gem_device.c +2 -1
@@ -52,7 +52,8 @@ void mock_device_flush(struct drm_i915_private *i915)
   	do {
   		for_each_engine(engine, gt, id)
   			mock_engine_flush(engine);
-	} while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT));
+	} while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT,
+						  NULL));
   }
   static void mock_device_release(struct drm_device *dev)

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

#33 lore raw

PATCH 16/51 drm/i915/guc: Update GuC debugfs to support new GuC

In reply to Matthew Brost

Update GuC debugfs to support the new GuC structures.

v2:
(John Harrison)
- Remove intel_lrc_reg.h include from i915_debugfs.c
(Michal)
- Rename GuC debugfs functions

---
 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 | 55 +++++++++++++++++++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.h |  5 ++
 5 files changed, 107 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +22 -0
index f1cbed6b9f0a..503a78517610 100644
@@ -1171,3 +1171,25 @@ void intel_guc_ct_event_handler(struct intel_guc_ct *ct)
 
 	ct_try_receive_message(ct);
 }
+
+void intel_guc_ct_print_info(struct intel_guc_ct *ct,
+			     struct drm_printer *p)
+{
+	drm_printf(p, "CT %s\n", enableddisabled(ct->enabled));
+
+	if (!ct->enabled)
+		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 4b30a562ae63..7b34026d264a 100644
@@ -16,6 +16,7 @@
 
 struct i915_vma;
 struct intel_guc;
+struct drm_printer;
 
 /**
  * DOC: Command Transport (CT).
@@ -112,4 +113,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_ct_print_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..7a454c91a736 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_ct_print_info(&guc->ct, &p);
+	intel_guc_submission_print_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_submission_print_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 +55 -0

58 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +5 -0
index 3f7005018939..2b9470c90558 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,10 @@ 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_submission_print_info(struct intel_guc *guc,
+				     struct drm_printer *p);
+void intel_guc_submission_print_context_info(struct intel_guc *guc,
+					     struct drm_printer *p);
 
 static inline bool intel_guc_submission_is_supported(struct intel_guc *guc)
 {
Signature · 6 lines
2.28.0

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

Re: [PATCH 16/51] drm/i915/guc: Update GuC debugfs to support new GuC

Quoted 10 lines · Matthew Brost

Update GuC debugfs to support the new GuC structures.

v2:
(John Harrison)
- Remove intel_lrc_reg.h include from i915_debugfs.c
(Michal)
- Rename GuC debugfs functions

Quoted 183 lines
---
  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 | 55 +++++++++++++++++++
  .../gpu/drm/i915/gt/uc/intel_guc_submission.h |  5 ++
  5 files changed, 107 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +22 -0
index f1cbed6b9f0a..503a78517610 100644
@@ -1171,3 +1171,25 @@ void intel_guc_ct_event_handler(struct intel_guc_ct *ct)
 
  	ct_try_receive_message(ct);
  }
+
+void intel_guc_ct_print_info(struct intel_guc_ct *ct,
+			     struct drm_printer *p)
+{
+	drm_printf(p, "CT %s\n", enableddisabled(ct->enabled));
+
+	if (!ct->enabled)
+		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 4b30a562ae63..7b34026d264a 100644
@@ -16,6 +16,7 @@
 
  struct i915_vma;
  struct intel_guc;
+struct drm_printer;
 
  /**
   * DOC: Command Transport (CT).
@@ -112,4 +113,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_ct_print_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..7a454c91a736 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_ct_print_info(&guc->ct, &p);
+	intel_guc_submission_print_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_submission_print_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 +55 -0
index 088d11e2e497..a2af7e17dcc2 100644
@@ -1602,3 +1602,58 @@ int intel_guc_sched_done_process_msg(struct intel_guc *guc,
 
  	return 0;
  }
+
+void intel_guc_submission_print_info(struct intel_guc *guc,
+				     struct drm_printer *p)
+{
+	struct i915_sched_engine *sched_engine = guc->sched_engine;
+	struct rb_node *rb;
+	unsigned long flags;
+
+	if (!sched_engine)
+		return;
+
+	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);
+	}
+	spin_unlock_irqrestore(&sched_engine->lock, flags);
+	drm_printf(p, "\n");
+}
+
+void intel_guc_submission_print_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 +1 -0
index 3f7005018939..2b9470c90558 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);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h void intel_guc_submission_fini(struct intel_guc *guc); · line 30

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +4 -0
@@ -20,6 +21,10 @@ 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_submission_print_info(struct intel_guc *guc,
+				     struct drm_printer *p);
+void intel_guc_submission_print_context_info(struct intel_guc *guc,
+					     struct drm_printer *p);
 
  static inline bool intel_guc_submission_is_supported(struct intel_guc *guc)
  {

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

#35 lore raw

PATCH 17/51 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.

v2: Fix white space alignment in i915_request_add trace point

---
 .../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             | 43 +++++++++++++++++--
 3 files changed, 45 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index a2af7e17dcc2..480fb2184ecf 100644
@@ -417,6 +417,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;
@@ -637,6 +638,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 2b2b63cba06c..01aa3d1ee2b1 100644
@@ -1319,6 +1319,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 +39 -4

75 lines not on this page · raw message

Signature · 6 lines
2.28.0

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

Re: [PATCH 17/51] 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,

Still has misplaced commas.

Also, Tvrtko has a bunch of comments/questions on the previous version that need to be addressed.

John.

Quoted 119 lines

and ring tail value.

v2: Fix white space alignment in i915_request_add trace point

---
  .../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             | 43 +++++++++++++++++--
  3 files changed, 45 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index a2af7e17dcc2..480fb2184ecf 100644
@@ -417,6 +417,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;
@@ -637,6 +638,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 2b2b63cba06c..01aa3d1ee2b1 100644
@@ -1319,6 +1319,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 +32 -4
index 6778ad2a14a4..ea41d069bf7d 100644
@@ -794,30 +794,50 @@ 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,
-	    TP_PROTO(struct i915_request *rq),
-	    TP_ARGS(rq)
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
  );
 
  #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)
+{

drivers/gpu/drm/i915/i915_trace.h TRACE_EVENT(i915_request_out, · line 927

Quoted 10 lines
drivers/gpu/drm/i915/i915_trace.h +7 -0
+}
+
+static inline void
+trace_i915_request_guc_submit(struct i915_request *rq)
+{
+}
+
  static inline void
  trace_i915_request_submit(struct i915_request *rq)
  {

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

#37 lore raw

Re: [PATCH 17/51] drm/i915/guc: Add several request trace points

Quoted 8 lines · John Harrison
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,

Still has misplaced commas.

Also, Tvrtko has a bunch of comments/questions on the previous version that need to be addressed.

Replied. Landed on just deleting the contentious, albiet very useful, trace points. Will revisit in the future.

Matt

Quoted 124 lines

John.

Quoted 121 lines

and ring tail value.

v2: Fix white space alignment in i915_request_add trace point

---
  .../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             | 43 +++++++++++++++++--
  3 files changed, 45 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
index a2af7e17dcc2..480fb2184ecf 100644
@@ -417,6 +417,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;
@@ -637,6 +638,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 2b2b63cba06c..01aa3d1ee2b1 100644
@@ -1319,6 +1319,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 +39 -4
index 6778ad2a14a4..ea41d069bf7d 100644
@@ -794,30 +794,50 @@ 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,
-	    TP_PROTO(struct i915_request *rq),
-	    TP_ARGS(rq)
+	     TP_PROTO(struct i915_request *rq),
+	     TP_ARGS(rq)
  );
  #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)
  {
#38 lore raw

PATCH 18/51 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             | 144 ++++++++++++++++++
 3 files changed, 164 insertions(+)
drivers/gpu/drm/i915/gt/intel_context.c +6 -0
index 91349d071e0e..251ff7eea22d 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 480fb2184ecf..05958260e849 100644
@@ -343,6 +343,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) {
@@ -808,6 +809,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);
 }
 
@@ -828,6 +831,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);
 }
 
@@ -902,6 +907,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);
 
@@ -960,6 +966,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_send_busy_loop(guc, action, ARRAY_SIZE(action),
@@ -1121,6 +1128,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);
 
@@ -1531,6 +1541,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;
@@ -1582,6 +1594,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 +144 -0

156 lines not on this page · raw message

Signature · 6 lines
2.28.0

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

PATCH 19/51 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.

v2: Update to work with proto-ctx

Cc Daniele Ceraolo Spurio <[email protected]>
---
 drivers/gpu/drm/i915/gem/i915_gem_context.c   |   8 +-
 drivers/gpu/drm/i915/gem/i915_gem_context.h   |   1 +
 drivers/gpu/drm/i915/gt/intel_context_types.h |   6 +
 drivers/gpu/drm/i915/gt/intel_engine.h        |  27 +-
 drivers/gpu/drm/i915/gt/intel_engine_cs.c     |  14 +
 .../drm/i915/gt/intel_execlists_submission.c  |  29 ++-
 .../drm/i915/gt/intel_execlists_submission.h  |   4 -
 drivers/gpu/drm/i915/gt/selftest_execlists.c  |  12 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 240 +++++++++++++++++-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.h |   2 +
 10 files changed, 308 insertions(+), 35 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -6
index 64659802d4df..edefe299bd76 100644
@@ -74,7 +74,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"
 
@@ -363,9 +362,6 @@ set_proto_ctx_engines_balance(struct i915_user_extension __user *base,
 	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;
 
@@ -950,8 +946,8 @@ static struct i915_gem_engines *user_engines(struct i915_gem_context *ctx,
 			break;
 
 		case I915_GEM_ENGINE_TYPE_BALANCED:
-			ce = intel_execlists_create_virtual(pe[n].siblings,
-							    pe[n].num_siblings);
+			ce = intel_engine_create_virtual(pe[n].siblings,
+							 pe[n].num_siblings);
 			break;
 
 		case I915_GEM_ENGINE_TYPE_INVALID:
drivers/gpu/drm/i915/gem/i915_gem_context.h +1 -0
index 20411db84914..2639c719a7a6 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 +6 -0
index 4a5518d295c2..542c98418771 100644
@@ -47,6 +47,12 @@ 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_engine_cs *(*get_sibling)(struct intel_engine_cs *engine,
+					       unsigned int sibling);
 };
 
 struct intel_context {
drivers/gpu/drm/i915/gt/intel_engine.h +26 -1
index f911c1224ab2..9fec0aca5f4b 100644
@@ -273,13 +273,38 @@ 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);
+
+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;
+}
+
 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_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 d561573ed98c..b7292d5cb7da 100644
@@ -1737,6 +1737,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 +20 -9
index 56e25090da67..28492cdce706 100644
@@ -193,6 +193,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,
@@ -2548,6 +2551,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)
@@ -3493,6 +3498,17 @@ 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 const struct intel_context_ops virtual_context_ops = {
 	.flags = COPS_HAS_INFLIGHT,
 
@@ -3507,6 +3523,8 @@ static const struct intel_context_ops virtual_context_ops = {
 	.exit = virtual_context_exit,
 
 	.destroy = virtual_context_destroy,
+
+	.get_sibling = virtual_get_sibling,
 };
 
 static intel_engine_mask_t virtual_submission_mask(struct virtual_engine *ve)
@@ -3655,20 +3673,13 @@ static void virtual_submit_request(struct i915_request *rq)
 	spin_unlock_irqrestore(&ve->base.sched_engine->lock, flags);
 }
 
-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);
drivers/gpu/drm/i915/gt/intel_execlists_submission.h +0 -4
index ad4f3e1a0fde..a1aa92c983a5 100644
@@ -32,10 +32,6 @@ 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);
-
 bool
 intel_engine_in_execlists_submission_mode(const struct intel_engine_cs *engine);
 
drivers/gpu/drm/i915/gt/selftest_execlists.c +6 -6
index 73ddc6e14730..59cf8afc6d6f 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;
@@ -4348,7 +4348,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 +231 -9

299 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +2 -0
index 2b9470c90558..5f263ac4f46a 100644
@@ -26,6 +26,8 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
 void intel_guc_submission_print_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
#40 lore raw

Re: [PATCH 19/51] drm/i915/guc: GuC virtual engines

Quoted 60 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.

v2: Update to work with proto-ctx

Cc Daniele Ceraolo Spurio <[email protected]>
---
  drivers/gpu/drm/i915/gem/i915_gem_context.c   |   8 +-
  drivers/gpu/drm/i915/gem/i915_gem_context.h   |   1 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |   6 +
  drivers/gpu/drm/i915/gt/intel_engine.h        |  27 +-
  drivers/gpu/drm/i915/gt/intel_engine_cs.c     |  14 +
  .../drm/i915/gt/intel_execlists_submission.c  |  29 ++-
  .../drm/i915/gt/intel_execlists_submission.h  |   4 -
  drivers/gpu/drm/i915/gt/selftest_execlists.c  |  12 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 240 +++++++++++++++++-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.h |   2 +
  10 files changed, 308 insertions(+), 35 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -6
index 64659802d4df..edefe299bd76 100644
@@ -74,7 +74,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"
 
@@ -363,9 +362,6 @@ set_proto_ctx_engines_balance(struct i915_user_extension __user *base,
  	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;
 
@@ -950,8 +946,8 @@ static struct i915_gem_engines *user_engines(struct i915_gem_context *ctx,
  			break;
 
  		case I915_GEM_ENGINE_TYPE_BALANCED:
-			ce = intel_execlists_create_virtual(pe[n].siblings,
-							    pe[n].num_siblings);
+			ce = intel_engine_create_virtual(pe[n].siblings,
+							 pe[n].num_siblings);
  			break;
 
  		case I915_GEM_ENGINE_TYPE_INVALID:
drivers/gpu/drm/i915/gem/i915_gem_context.h +0 -0
index 20411db84914..2639c719a7a6 100644

drivers/gpu/drm/i915/gem/i915_gem_context.h line 13

Quoted 4 lines
drivers/gpu/drm/i915/gem/i915_gem_context.h +1 -0
@@ -10,6 +10,7 @@
  #include "i915_gem_context_types.h"
 
  #include "gt/intel_context.h"
+#include "gt/intel_engine.h"

Apologies for the late question, but why do you need this include in this header? nothing else is changing within the file.

Quoted 29 lines
drivers/gpu/drm/i915/gem/i915_gem_context.h +0 -0
  #include "i915_drv.h"
  #include "i915_gem.h"
drivers/gpu/drm/i915/gt/intel_context_types.h +6 -0
index 4a5518d295c2..542c98418771 100644
@@ -47,6 +47,12 @@ 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_engine_cs *(*get_sibling)(struct intel_engine_cs *engine,
+					       unsigned int sibling);
  };
 
  struct intel_context {
drivers/gpu/drm/i915/gt/intel_engine.h +1 -0
index f911c1224ab2..9fec0aca5f4b 100644
@@ -273,13 +273,38 @@ intel_engine_has_preempt_reset(const struct intel_engine_cs *engine)
  	return intel_engine_has_preemption(engine);
  }
 
+struct intel_context *

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

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_engine.h +10 -0
+intel_engine_create_virtual(struct intel_engine_cs **siblings,
+			    unsigned int count);
+
+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");

I insist that this needs a better explanation, as I've commented on the previous rev. Given that this is a shared file, it is not immediately evident why this call shouldn't be called with the execlists backend.

Daniele

Quoted 513 lines
drivers/gpu/drm/i915/gt/intel_engine.h +15 -1
+
+	return false;
+}
+
  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_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 d561573ed98c..b7292d5cb7da 100644
@@ -1737,6 +1737,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 +20 -9
index 56e25090da67..28492cdce706 100644
@@ -193,6 +193,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,
@@ -2548,6 +2551,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)
@@ -3493,6 +3498,17 @@ 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 const struct intel_context_ops virtual_context_ops = {
  	.flags = COPS_HAS_INFLIGHT,
 
@@ -3507,6 +3523,8 @@ static const struct intel_context_ops virtual_context_ops = {
  	.exit = virtual_context_exit,
 
  	.destroy = virtual_context_destroy,
+
+	.get_sibling = virtual_get_sibling,
  };
 
  static intel_engine_mask_t virtual_submission_mask(struct virtual_engine *ve)
@@ -3655,20 +3673,13 @@ static void virtual_submit_request(struct i915_request *rq)
  	spin_unlock_irqrestore(&ve->base.sched_engine->lock, flags);
  }
 
-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);
drivers/gpu/drm/i915/gt/intel_execlists_submission.h +0 -4
index ad4f3e1a0fde..a1aa92c983a5 100644
@@ -32,10 +32,6 @@ 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);
-
  bool
  intel_engine_in_execlists_submission_mode(const struct intel_engine_cs *engine);
 
drivers/gpu/drm/i915/gt/selftest_execlists.c +6 -6
index 73ddc6e14730..59cf8afc6d6f 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;
@@ -4348,7 +4348,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 +231 -9
index 05958260e849..7b3e1c91e689 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 */
 
  /*
@@ -925,20 +934,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)
@@ -1043,6 +1067,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);
@@ -1059,7 +1098,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);
+		__guc_context_destroy(ce);
  		return;
  	}
 
@@ -1075,7 +1114,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;
  	}
 
@@ -1120,6 +1159,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)
@@ -1248,6 +1289,83 @@ 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 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,
+
+	.get_sibling = guc_virtual_get_sibling,
+};
+
  static void sanitize_hwsp(struct intel_engine_cs *engine)
  {
  	struct intel_timeline *tl;
@@ -1559,7 +1677,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);
@@ -1674,3 +1792,107 @@ void intel_guc_submission_print_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;
+
+	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;
+	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;
+			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;
+
+			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 +0 -0
index 2b9470c90558..5f263ac4f46a 100644

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h void intel_guc_submission_print_info(struct intel_guc *guc, · line 33

Quoted 8 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +2 -0
@@ -26,6 +26,8 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
  void intel_guc_submission_print_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 */

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

#41 lore raw

Re: [PATCH 19/51] drm/i915/guc: GuC virtual engines

Quoted 56 lines · Daniele Ceraolo Spurio
Quoted 53 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.

v2: Update to work with proto-ctx

Cc Daniele Ceraolo Spurio <[email protected]>
---
  drivers/gpu/drm/i915/gem/i915_gem_context.c   |   8 +-
  drivers/gpu/drm/i915/gem/i915_gem_context.h   |   1 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |   6 +
  drivers/gpu/drm/i915/gt/intel_engine.h        |  27 +-
  drivers/gpu/drm/i915/gt/intel_engine_cs.c     |  14 +
  .../drm/i915/gt/intel_execlists_submission.c  |  29 ++-
  .../drm/i915/gt/intel_execlists_submission.h  |   4 -
  drivers/gpu/drm/i915/gt/selftest_execlists.c  |  12 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 240 +++++++++++++++++-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.h |   2 +
  10 files changed, 308 insertions(+), 35 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -6
index 64659802d4df..edefe299bd76 100644
@@ -74,7 +74,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"
@@ -363,9 +362,6 @@ set_proto_ctx_engines_balance(struct i915_user_extension __user *base,
  	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;
@@ -950,8 +946,8 @@ static struct i915_gem_engines *user_engines(struct i915_gem_context *ctx,
  			break;
  		case I915_GEM_ENGINE_TYPE_BALANCED:
-			ce = intel_execlists_create_virtual(pe[n].siblings,
-							    pe[n].num_siblings);
+			ce = intel_engine_create_virtual(pe[n].siblings,
+							 pe[n].num_siblings);
  			break;
  		case I915_GEM_ENGINE_TYPE_INVALID:
drivers/gpu/drm/i915/gem/i915_gem_context.h +0 -1
index 20411db84914..2639c719a7a6 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_context.h
Quoted 9 lines

drivers/gpu/drm/i915/gem/i915_gem_context.h line 12

Quoted 5 lines

+++ b/drivers/gpu/drm/i915/gem/i915_gem_context.h

drivers/gpu/drm/i915/gem/i915_gem_context.h +1 -0
@@ -10,6 +10,7 @@
  #include "i915_gem_context_types.h"
  #include "gt/intel_context.h"
+#include "gt/intel_engine.h"

Apologies for the late question, but why do you need this include in this header? nothing else is changing within the file.

It likely doesn't need to be included. Let me see what the build / CI says if I remove it.

Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/i915/gem/i915_gem_context.h +0 -0
  #include "i915_drv.h"
  #include "i915_gem.h"
drivers/gpu/drm/i915/gt/intel_context_types.h +6 -0
index 4a5518d295c2..542c98418771 100644
@@ -47,6 +47,12 @@ 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_engine_cs *(*get_sibling)(struct intel_engine_cs *engine,
+					       unsigned int sibling);
  };
  struct intel_context {
drivers/gpu/drm/i915/gt/intel_engine.h +7 -0
index f911c1224ab2..9fec0aca5f4b 100644
@@ -273,13 +273,38 @@ 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);
+
+static inline bool
+intel_virtual_engine_has_heartbeat(const struct intel_engine_cs *engine)
+{
Quoted 9 lines

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

Quoted 4 lines
drivers/gpu/drm/i915/gt/intel_engine.h +4 -0
+	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");

I insist that this needs a better explanation, as I've commented on the previous rev. Given that this is a shared file, it is not immediately evident why this call shouldn't be called with the execlists backend.

Sure, can add a comment. Basically it comes down to it is always called an active request which in execlists mode is always a physical engine.

Matt

Quoted 481 lines

Daniele

Quoted 479 lines
drivers/gpu/drm/i915/gt/intel_engine.h +15 -1
+
+	return false;
+}
+
  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_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 d561573ed98c..b7292d5cb7da 100644
@@ -1737,6 +1737,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 +20 -9
index 56e25090da67..28492cdce706 100644
@@ -193,6 +193,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,
@@ -2548,6 +2551,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)
@@ -3493,6 +3498,17 @@ 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 const struct intel_context_ops virtual_context_ops = {
  	.flags = COPS_HAS_INFLIGHT,
@@ -3507,6 +3523,8 @@ static const struct intel_context_ops virtual_context_ops = {
  	.exit = virtual_context_exit,
  	.destroy = virtual_context_destroy,
+
+	.get_sibling = virtual_get_sibling,
  };
  static intel_engine_mask_t virtual_submission_mask(struct virtual_engine *ve)
@@ -3655,20 +3673,13 @@ static void virtual_submit_request(struct i915_request *rq)
  	spin_unlock_irqrestore(&ve->base.sched_engine->lock, flags);
  }
-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);
drivers/gpu/drm/i915/gt/intel_execlists_submission.h +0 -4
index ad4f3e1a0fde..a1aa92c983a5 100644
@@ -32,10 +32,6 @@ 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);
-
  bool
  intel_engine_in_execlists_submission_mode(const struct intel_engine_cs *engine);
drivers/gpu/drm/i915/gt/selftest_execlists.c +6 -6
index 73ddc6e14730..59cf8afc6d6f 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;
@@ -4348,7 +4348,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 +231 -9
index 05958260e849..7b3e1c91e689 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 */
  /*
@@ -925,20 +934,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)
@@ -1043,6 +1067,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);
@@ -1059,7 +1098,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);
+		__guc_context_destroy(ce);
  		return;
  	}
@@ -1075,7 +1114,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;
  	}
@@ -1120,6 +1159,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)
@@ -1248,6 +1289,83 @@ 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 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,
+
+	.get_sibling = guc_virtual_get_sibling,
+};
+
  static void sanitize_hwsp(struct intel_engine_cs *engine)
  {
  	struct intel_timeline *tl;
@@ -1559,7 +1677,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);
@@ -1674,3 +1792,107 @@ void intel_guc_submission_print_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;
+
+	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;
+	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;
+			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;
+
+			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 +0 -0
index 2b9470c90558..5f263ac4f46a 100644
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h void intel_guc_submission_print_info(struct intel_guc *guc, · line 32

Quoted 8 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.h +2 -0
@@ -26,6 +26,8 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
  void intel_guc_submission_print_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 */

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

#42 lore raw

Re: [PATCH 19/51] drm/i915/guc: GuC virtual engines

Quoted 106 lines · Matthew Brost
Quoted 54 lines · Daniele Ceraolo Spurio
Quoted 52 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.

v2: Update to work with proto-ctx

Cc Daniele Ceraolo Spurio <[email protected]>
---
   drivers/gpu/drm/i915/gem/i915_gem_context.c   |   8 +-
   drivers/gpu/drm/i915/gem/i915_gem_context.h   |   1 +
   drivers/gpu/drm/i915/gt/intel_context_types.h |   6 +
   drivers/gpu/drm/i915/gt/intel_engine.h        |  27 +-
   drivers/gpu/drm/i915/gt/intel_engine_cs.c     |  14 +
   .../drm/i915/gt/intel_execlists_submission.c  |  29 ++-
   .../drm/i915/gt/intel_execlists_submission.h  |   4 -
   drivers/gpu/drm/i915/gt/selftest_execlists.c  |  12 +-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 240 +++++++++++++++++-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.h |   2 +
   10 files changed, 308 insertions(+), 35 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -6
index 64659802d4df..edefe299bd76 100644
@@ -74,7 +74,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"
@@ -363,9 +362,6 @@ set_proto_ctx_engines_balance(struct i915_user_extension __user *base,
   	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;
@@ -950,8 +946,8 @@ static struct i915_gem_engines *user_engines(struct i915_gem_context *ctx,
   			break;
   		case I915_GEM_ENGINE_TYPE_BALANCED:
-			ce = intel_execlists_create_virtual(pe[n].siblings,
-							    pe[n].num_siblings);
+			ce = intel_engine_create_virtual(pe[n].siblings,
+							 pe[n].num_siblings);
   			break;
   		case I915_GEM_ENGINE_TYPE_INVALID:
drivers/gpu/drm/i915/gem/i915_gem_context.h +0 -0
index 20411db84914..2639c719a7a6 100644
Quoted 9 lines

drivers/gpu/drm/i915/gem/i915_gem_context.h line 12

Quoted 6 lines
drivers/gpu/drm/i915/gem/i915_gem_context.h +1 -0
@@ -10,6 +10,7 @@
   #include "i915_gem_context_types.h"
   #include "gt/intel_context.h"
+#include "gt/intel_engine.h"

Apologies for the late question, but why do you need this include in this header? nothing else is changing within the file.

It likely doesn't need to be included. Let me see what the build / CI says if I remove it.

Quoted 30 lines
Quoted 30 lines
drivers/gpu/drm/i915/gem/i915_gem_context.h +0 -0
   #include "i915_drv.h"
   #include "i915_gem.h"
drivers/gpu/drm/i915/gt/intel_context_types.h +6 -0
index 4a5518d295c2..542c98418771 100644
@@ -47,6 +47,12 @@ 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_engine_cs *(*get_sibling)(struct intel_engine_cs *engine,
+					       unsigned int sibling);
   };
   struct intel_context {
drivers/gpu/drm/i915/gt/intel_engine.h +6 -0
index f911c1224ab2..9fec0aca5f4b 100644
@@ -273,13 +273,38 @@ 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);
+
+static inline bool
+intel_virtual_engine_has_heartbeat(const struct intel_engine_cs *engine)
Quoted 9 lines

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

Quoted 5 lines
drivers/gpu/drm/i915/gt/intel_engine.h +5 -0
+{
+	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");

I insist that this needs a better explanation, as I've commented on the previous rev. Given that this is a shared file, it is not immediately evident why this call shouldn't be called with the execlists backend.

Quoted 2 lines

Sure, can add a comment. Basically it comes down to it is always called an active request which in execlists mode is always a physical engine.

I had added a sample comment in the previous review in case you want to copy it. I remember you explaining this to me a while back, but it always takes me a moment to recall it when I look at this code and I expect someone not familiar with the backend implementation would be even more confused.

Daniele

Quoted 492 lines

Matt

Quoted 489 lines

Daniele

Quoted 487 lines
drivers/gpu/drm/i915/gt/intel_engine.h +15 -1
+
+	return false;
+}
+
   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_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 d561573ed98c..b7292d5cb7da 100644
@@ -1737,6 +1737,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 +20 -9
index 56e25090da67..28492cdce706 100644
@@ -193,6 +193,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,
@@ -2548,6 +2551,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)
@@ -3493,6 +3498,17 @@ 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 const struct intel_context_ops virtual_context_ops = {
   	.flags = COPS_HAS_INFLIGHT,
@@ -3507,6 +3523,8 @@ static const struct intel_context_ops virtual_context_ops = {
   	.exit = virtual_context_exit,
   	.destroy = virtual_context_destroy,
+
+	.get_sibling = virtual_get_sibling,
   };
   static intel_engine_mask_t virtual_submission_mask(struct virtual_engine *ve)
@@ -3655,20 +3673,13 @@ static void virtual_submit_request(struct i915_request *rq)
   	spin_unlock_irqrestore(&ve->base.sched_engine->lock, flags);
   }
-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);
drivers/gpu/drm/i915/gt/intel_execlists_submission.h +0 -4
index ad4f3e1a0fde..a1aa92c983a5 100644
@@ -32,10 +32,6 @@ 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);
-
   bool
   intel_engine_in_execlists_submission_mode(const struct intel_engine_cs *engine);
drivers/gpu/drm/i915/gt/selftest_execlists.c +6 -6
index 73ddc6e14730..59cf8afc6d6f 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;
@@ -4348,7 +4348,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 +231 -9
index 05958260e849..7b3e1c91e689 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 */
   /*
@@ -925,20 +934,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)
@@ -1043,6 +1067,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);
@@ -1059,7 +1098,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);
+		__guc_context_destroy(ce);
   		return;
   	}
@@ -1075,7 +1114,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;
   	}
@@ -1120,6 +1159,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)
@@ -1248,6 +1289,83 @@ 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 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,
+
+	.get_sibling = guc_virtual_get_sibling,
+};
+
   static void sanitize_hwsp(struct intel_engine_cs *engine)
   {
   	struct intel_timeline *tl;
@@ -1559,7 +1677,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);
@@ -1674,3 +1792,107 @@ void intel_guc_submission_print_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;
+
+	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;
+	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;
+			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;
+
+			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 2b9470c90558..5f263ac4f46a 100644
@@ -26,6 +26,8 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
   void intel_guc_submission_print_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 */