Skip to contentSkip to conversation

Parallel submission aka multi-bb execbuf

PATCH In review Matthew Brost 5 participants 109 messages Aug 20, 2021 – Sep 30, 2021 Page 1 of 2

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

cover Parallel submission aka multi-bb execbuf

As discussed in [1] we are introducing a new parallel submission uAPI for the i915 which allows more than 1 BB to be submitted in an execbuf IOCTL. This is the implemenation for both GuC and execlists.

In addition to selftests in the series, an IGT is available implemented in the first 4 patches [2].

Media UMD changes to land soon.

First patch in the series in a squashed patch of [3] and does not need to be reviewed here.

The execbuf IOCTL changes have been done in a single large patch (#24) as all the changes flow together and I believe a single patch will be better if some one has to lookup this change in the future. Can split in a series of smaller patches if desired.

This code is available in a public [4] repo for UMD teams to test there code on.

v2: Drop complicated state machine to block in kernel if no guc_ids available, perma-pin parallel contexts, reworker execbuf IOCTL to be a series of loops inside the IOCTL rather than 1 large one on the outside, address Daniel Vetter's comments, rebase on [3]

[1] https://patchwork.freedesktop.org/series/92028/
[2] https://patchwork.freedesktop.org/series/93071/
[3] https://patchwork.freedesktop.org/series/93704/
[4] https://gitlab.freedesktop.org/mbrost/mbrost-drm-intel/-/tree/drm-intel-parallel

Matthew Brost (27):
drm/i915/guc: Squash Clean up GuC CI failures, simplify locking, and
kernel DOC
drm/i915/guc: Allow flexible number of context ids drm/i915/guc: Connect the number of guc_ids to debugfs drm/i915/guc: Take GT PM ref when deregistering context drm/i915: Add GT PM unpark worker
drm/i915/guc: Take engine PM when a context is pinned with GuC
submission
drm/i915/guc: Don't call switch_to_kernel_context with GuC submission drm/i915: Add logical engine mapping
drm/i915: Expose logical engine instance to user drm/i915/guc: Introduce context parent-child relationship drm/i915/guc: Implement parallel context pin / unpin functions drm/i915/guc: Add multi-lrc context registration drm/i915/guc: Ensure GuC schedule operations do not operate on child
contexts
drm/i915/guc: Assign contexts in parent-child relationship consecutive
guc_ids
drm/i915/guc: Implement multi-lrc submission
drm/i915/guc: Insert submit fences between requests in parent-child
relationship
drm/i915/guc: Implement multi-lrc reset
drm/i915/guc: Update debugfs for GuC multi-lrc
drm/i915: Fix bug in user proto-context creation that leaked contexts drm/i915/guc: Connect UAPI to GuC multi-lrc interface drm/i915/doc: Update parallel submit doc to point to i915_drm.h drm/i915/guc: Add basic GuC multi-lrc selftest drm/i915/guc: Implement no mid batch preemption for multi-lrc drm/i915: Multi-BB execbuf
drm/i915/guc: Handle errors in multi-lrc requests drm/i915: Enable multi-bb execbuf
drm/i915/execlists: Weak parallel submission support for execlists

 Documentation/gpu/rfc/i915_parallel_execbuf.h |  122 -
 Documentation/gpu/rfc/i915_scheduler.rst      |    4 +-
 drivers/gpu/drm/i915/Makefile                 |    1 +
 drivers/gpu/drm/i915/gem/i915_gem_context.c   |  222 +-
 .../gpu/drm/i915/gem/i915_gem_context_types.h |    6 +
 .../gpu/drm/i915/gem/i915_gem_execbuffer.c    |  765 ++++--
 drivers/gpu/drm/i915/gt/intel_context.c       |   58 +-
 drivers/gpu/drm/i915/gt/intel_context.h       |   52 +-
 drivers/gpu/drm/i915/gt/intel_context_types.h |  148 +-
 drivers/gpu/drm/i915/gt/intel_engine.h        |   12 +-
 drivers/gpu/drm/i915/gt/intel_engine_cs.c     |   66 +-
 drivers/gpu/drm/i915/gt/intel_engine_pm.c     |    9 +
 drivers/gpu/drm/i915/gt/intel_engine_pm.h     |   20 +
 drivers/gpu/drm/i915/gt/intel_engine_types.h  |    5 +
 .../drm/i915/gt/intel_execlists_submission.c  |   68 +-
 drivers/gpu/drm/i915/gt/intel_gt.c            |    3 +
 drivers/gpu/drm/i915/gt/intel_gt_pm.c         |    8 +
 drivers/gpu/drm/i915/gt/intel_gt_pm.h         |   23 +
 .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.c |   35 +
 .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.h |   40 +
 drivers/gpu/drm/i915/gt/intel_gt_types.h      |   10 +
 drivers/gpu/drm/i915/gt/intel_lrc.c           |    7 +
 drivers/gpu/drm/i915/gt/selftest_execlists.c  |   12 +-
 drivers/gpu/drm/i915/gt/selftest_hangcheck.c  |    6 +-
 drivers/gpu/drm/i915/gt/selftest_lrc.c        |   29 +-
 .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  |    1 +
 drivers/gpu/drm/i915/gt/uc/intel_guc.c        |   21 +
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   61 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |    2 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   30 +-
 .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    |   36 +
 drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |   10 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 2312 +++++++++++++----
 drivers/gpu/drm/i915/gt/uc/selftest_guc.c     |  126 +
 .../drm/i915/gt/uc/selftest_guc_multi_lrc.c   |  180 ++
 drivers/gpu/drm/i915/i915_gpu_error.c         |   39 +-
 drivers/gpu/drm/i915/i915_query.c             |    2 +
 drivers/gpu/drm/i915/i915_request.c           |  120 +-
 drivers/gpu/drm/i915/i915_request.h           |   40 +-
 drivers/gpu/drm/i915/i915_trace.h             |   12 +-
 drivers/gpu/drm/i915/i915_vma.c               |   21 +-
 drivers/gpu/drm/i915/i915_vma.h               |   13 +-
 drivers/gpu/drm/i915/intel_wakeref.c          |    5 +
 drivers/gpu/drm/i915/intel_wakeref.h          |   13 +
 .../drm/i915/selftests/i915_live_selftests.h  |    2 +
 drivers/gpu/drm/i915/selftests/i915_request.c |  100 +
 .../i915/selftests/intel_scheduler_helpers.c  |   12 +
 .../i915/selftests/intel_scheduler_helpers.h  |    2 +
 include/uapi/drm/i915_drm.h                   |  136 +-
 49 files changed, 3928 insertions(+), 1099 deletions(-)
 delete mode 100644 Documentation/gpu/rfc/i915_parallel_execbuf.h
 create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c
 create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h
 create mode 100644 drivers/gpu/drm/i915/gt/uc/selftest_guc.c
 create mode 100644 drivers/gpu/drm/i915/gt/uc/selftest_guc_multi_lrc.c
Signature · 1 line
2.32.0
#1 lore raw

PATCH 1/27 drm/i915/guc: Squash Clean up GuC CI failures, simplify locking, and kernel DOC

https://patchwork.freedesktop.org/series/93704/

---
 drivers/gpu/drm/i915/gt/intel_context.c       |  19 +-
 drivers/gpu/drm/i915/gt/intel_context_types.h |  81 +-
 .../drm/i915/gt/intel_execlists_submission.c  |   4 -
 drivers/gpu/drm/i915/gt/selftest_hangcheck.c  |   6 +-
 drivers/gpu/drm/i915/gt/selftest_lrc.c        |  29 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  19 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |   6 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 952 +++++++++++-------
 drivers/gpu/drm/i915/gt/uc/selftest_guc.c     | 126 +++
 drivers/gpu/drm/i915/i915_gpu_error.c         |  39 +-
 drivers/gpu/drm/i915/i915_request.h           |  23 +-
 drivers/gpu/drm/i915/i915_trace.h             |  12 +-
 .../drm/i915/selftests/i915_live_selftests.h  |   1 +
 drivers/gpu/drm/i915/selftests/i915_request.c | 100 ++
 .../i915/selftests/intel_scheduler_helpers.c  |  12 +
 .../i915/selftests/intel_scheduler_helpers.h  |   2 +
 16 files changed, 965 insertions(+), 466 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/gt/uc/selftest_guc.c
drivers/gpu/drm/i915/gt/intel_context.c +9 -10
index 745e84c72c90..adfe49b53b1b 100644
@@ -394,19 +394,18 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 
 	spin_lock_init(&ce->guc_state.lock);
 	INIT_LIST_HEAD(&ce->guc_state.fences);
+	INIT_LIST_HEAD(&ce->guc_state.requests);
 
-	spin_lock_init(&ce->guc_active.lock);
-	INIT_LIST_HEAD(&ce->guc_active.requests);
-
-	ce->guc_id = GUC_INVALID_LRC_ID;
-	INIT_LIST_HEAD(&ce->guc_id_link);
+	ce->guc_id.id = GUC_INVALID_LRC_ID;
+	INIT_LIST_HEAD(&ce->guc_id.link);
 
 	/*
 	 * Initialize fence to be complete as this is expected to be complete
 	 * unless there is a pending schedule disable outstanding.
 	 */
-	i915_sw_fence_init(&ce->guc_blocked, sw_fence_dummy_notify);
-	i915_sw_fence_commit(&ce->guc_blocked);
+	i915_sw_fence_init(&ce->guc_state.blocked_fence,
+			   sw_fence_dummy_notify);
+	i915_sw_fence_commit(&ce->guc_state.blocked_fence);
 
 	i915_active_init(&ce->active,
 			 __intel_context_active, __intel_context_retire, 0);
@@ -520,15 +519,15 @@ struct i915_request *intel_context_find_active_request(struct intel_context *ce)
 
 	GEM_BUG_ON(!intel_engine_uses_guc(ce->engine));
 
-	spin_lock_irqsave(&ce->guc_active.lock, flags);
-	list_for_each_entry_reverse(rq, &ce->guc_active.requests,
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	list_for_each_entry_reverse(rq, &ce->guc_state.requests,
 				    sched.link) {
 		if (i915_request_completed(rq))
 			break;
 
 		active = rq;
 	}
-	spin_unlock_irqrestore(&ce->guc_active.lock, flags);
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
 
 	return active;
 }
drivers/gpu/drm/i915/gt/intel_context_types.h +53 -28

108 lines not on this page · raw message

drivers/gpu/drm/i915/gt/intel_execlists_submission.c +0 -4
index de5f9c86b9a4..cafb0608ffb4 100644
@@ -2140,10 +2140,6 @@ static void __execlists_unhold(struct i915_request *rq)
 			if (p->flags & I915_DEPENDENCY_WEAK)
 				continue;
 
-			/* Propagate any change in error status */
-			if (rq->fence.error)
-				i915_request_set_error_once(w, rq->fence.error);
-
 			if (w->engine != rq->engine)
 				continue;
 
drivers/gpu/drm/i915/gt/selftest_hangcheck.c +3 -3
index 08f011f893b2..bf43bed905db 100644
@@ -789,7 +789,7 @@ static int __igt_reset_engine(struct intel_gt *gt, bool active)
 				if (err)
 					pr_err("[%s] Wait for request %lld:%lld [0x%04X] failed: %d!\n",
 					       engine->name, rq->fence.context,
-					       rq->fence.seqno, rq->context->guc_id, err);
+					       rq->fence.seqno, rq->context->guc_id.id, err);
 			}
 
 skip:
@@ -1098,7 +1098,7 @@ static int __igt_reset_engines(struct intel_gt *gt,
 				if (err)
 					pr_err("[%s] Wait for request %lld:%lld [0x%04X] failed: %d!\n",
 					       engine->name, rq->fence.context,
-					       rq->fence.seqno, rq->context->guc_id, err);
+					       rq->fence.seqno, rq->context->guc_id.id, err);
 			}
 
 			count++;
@@ -1108,7 +1108,7 @@ static int __igt_reset_engines(struct intel_gt *gt,
 					pr_err("i915_reset_engine(%s:%s): failed to reset request %lld:%lld [0x%04X]\n",
 					       engine->name, test_name,
 					       rq->fence.context,
-					       rq->fence.seqno, rq->context->guc_id);
+					       rq->fence.seqno, rq->context->guc_id.id);
 					i915_request_put(rq);
 
 					GEM_TRACE_DUMP();
drivers/gpu/drm/i915/gt/selftest_lrc.c +28 -1
index b0977a3b699b..cdc6ae48a1e1 100644
@@ -1074,6 +1074,32 @@ record_registers(struct intel_context *ce,
 	goto err_after;
 }
 
+static u32 safe_offset(u32 offset, u32 reg)
+{
+	/* XXX skip testing of watchdog - VLK-22772 */
+	if (offset == 0x178 || offset == 0x17c)
+		reg = 0;
+
+	return reg;
+}
+
+static int get_offset_mask(struct intel_engine_cs *engine)
+{
+	if (GRAPHICS_VER(engine->i915) < 12)
+		return 0xfff;
+
+	switch (engine->class) {
+	default:
+	case RENDER_CLASS:
+		return 0x07ff;
+	case COPY_ENGINE_CLASS:
+		return 0x0fff;
+	case VIDEO_DECODE_CLASS:
+	case VIDEO_ENHANCEMENT_CLASS:
+		return 0x3fff;
+	}
+}
+
 static struct i915_vma *load_context(struct intel_context *ce, u32 poison)
 {
 	struct i915_vma *batch;
@@ -1117,7 +1143,8 @@ static struct i915_vma *load_context(struct intel_context *ce, u32 poison)
 		len = (len + 1) / 2;
 		*cs++ = MI_LOAD_REGISTER_IMM(len);
 		while (len--) {
-			*cs++ = hw[dw];
+			*cs++ = safe_offset(hw[dw] & get_offset_mask(ce->engine),
+					    hw[dw]);
 			*cs++ = poison;
 			dw += 2;
 		}
drivers/gpu/drm/i915/gt/uc/intel_guc.h +15 -4
index 2e27fe59786b..112dd29a63fe 100644
@@ -41,6 +41,10 @@ struct intel_guc {
 	spinlock_t irq_lock;
 	unsigned int msg_enabled_mask;
 
+	/**
+	 * @outstanding_submission_g2h: number of outstanding G2H related to GuC
+	 * submission, used to determine if the GT is idle
+	 */
 	atomic_t outstanding_submission_g2h;
 
 	struct {
@@ -49,12 +53,16 @@ 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
+	/**
+	 * @contexts_lock: protects guc_ids, guc_id_list, ce->guc_id.id, and
+	 * ce->guc_id.ref when transitioning in and out of zero
 	 */
 	spinlock_t contexts_lock;
+	/** @guc_ids: used to allocate new guc_ids */
 	struct ida guc_ids;
+	/**
+	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
+	 */
 	struct list_head guc_id_list;
 
 	bool submission_supported;
@@ -70,7 +78,10 @@ struct intel_guc {
 	struct i915_vma *lrc_desc_pool;
 	void *lrc_desc_pool_vaddr;
 
-	/* guc_id to intel_context lookup */
+	/**
+	 * @context_lookup: used to resolve intel_context from guc_id, if a
+	 * context is present in this structure it is registered with the GuC
+	 */
 	struct xarray context_lookup;
 
 	/* Control params for fw initialization */
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +3 -3
index 22b4733b55e2..20c710a74498 100644
@@ -1042,9 +1042,9 @@ static void ct_incoming_request_worker_func(struct work_struct *w)
 		container_of(w, struct intel_guc_ct, requests.worker);
 	bool done;
 
-	done = ct_process_incoming_requests(ct);
-	if (!done)
-		queue_work(system_unbound_wq, &ct->requests.worker);
+	do {
+		done = ct_process_incoming_requests(ct);
+	} while (!done);
 }
 
 static int ct_handle_event(struct intel_guc_ct *ct, struct ct_incoming_msg *request)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +575 -377

1,854 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/selftest_guc.c new file +126 -0

126 lines not on this page · raw message

drivers/gpu/drm/i915/i915_gpu_error.c +19 -20
index 0f08bcfbe964..2819b69fbb72 100644
@@ -49,8 +49,7 @@
 #include "i915_memcpy.h"
 #include "i915_scatterlist.h"
 
-#define ALLOW_FAIL (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
-#define ATOMIC_MAYFAIL (GFP_ATOMIC | __GFP_NOWARN)
+#define ATOMIC_MAYFAIL (GFP_NOWAIT | __GFP_NOWARN)
 
 static void __sg_set_buf(struct scatterlist *sg,
 			 void *addr, unsigned int len, loff_t it)
@@ -79,7 +78,7 @@ static bool __i915_error_grow(struct drm_i915_error_state_buf *e, size_t len)
 	if (e->cur == e->end) {
 		struct scatterlist *sgl;
 
-		sgl = (typeof(sgl))__get_free_page(ALLOW_FAIL);
+		sgl = (typeof(sgl))__get_free_page(ATOMIC_MAYFAIL);
 		if (!sgl) {
 			e->err = -ENOMEM;
 			return false;
@@ -99,10 +98,10 @@ static bool __i915_error_grow(struct drm_i915_error_state_buf *e, size_t len)
 	}
 
 	e->size = ALIGN(len + 1, SZ_64K);
-	e->buf = kmalloc(e->size, ALLOW_FAIL);
+	e->buf = kmalloc(e->size, ATOMIC_MAYFAIL);
 	if (!e->buf) {
 		e->size = PAGE_ALIGN(len + 1);
-		e->buf = kmalloc(e->size, GFP_KERNEL);
+		e->buf = kmalloc(e->size, ATOMIC_MAYFAIL);
 	}
 	if (!e->buf) {
 		e->err = -ENOMEM;
@@ -243,12 +242,12 @@ static bool compress_init(struct i915_vma_compress *c)
 {
 	struct z_stream_s *zstream = &c->zstream;
 
-	if (pool_init(&c->pool, ALLOW_FAIL))
+	if (pool_init(&c->pool, ATOMIC_MAYFAIL))
 		return false;
 
 	zstream->workspace =
 		kmalloc(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
-			ALLOW_FAIL);
+			ATOMIC_MAYFAIL);
 	if (!zstream->workspace) {
 		pool_fini(&c->pool);
 		return false;
@@ -256,7 +255,7 @@ static bool compress_init(struct i915_vma_compress *c)
 
 	c->tmp = NULL;
 	if (i915_has_memcpy_from_wc())
-		c->tmp = pool_alloc(&c->pool, ALLOW_FAIL);
+		c->tmp = pool_alloc(&c->pool, ATOMIC_MAYFAIL);
 
 	return true;
 }
@@ -280,7 +279,7 @@ static void *compress_next_page(struct i915_vma_compress *c,
 	if (dst->page_count >= dst->num_pages)
 		return ERR_PTR(-ENOSPC);
 
-	page = pool_alloc(&c->pool, ALLOW_FAIL);
+	page = pool_alloc(&c->pool, ATOMIC_MAYFAIL);
 	if (!page)
 		return ERR_PTR(-ENOMEM);
 
@@ -376,7 +375,7 @@ struct i915_vma_compress {
 
 static bool compress_init(struct i915_vma_compress *c)
 {
-	return pool_init(&c->pool, ALLOW_FAIL) == 0;
+	return pool_init(&c->pool, ATOMIC_MAYFAIL) == 0;
 }
 
 static bool compress_start(struct i915_vma_compress *c)
@@ -391,7 +390,7 @@ static int compress_page(struct i915_vma_compress *c,
 {
 	void *ptr;
 
-	ptr = pool_alloc(&c->pool, ALLOW_FAIL);
+	ptr = pool_alloc(&c->pool, ATOMIC_MAYFAIL);
 	if (!ptr)
 		return -ENOMEM;
 
@@ -997,7 +996,7 @@ i915_vma_coredump_create(const struct intel_gt *gt,
 
 	num_pages = min_t(u64, vma->size, vma->obj->base.size) >> PAGE_SHIFT;
 	num_pages = DIV_ROUND_UP(10 * num_pages, 8); /* worstcase zlib growth */
-	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), ALLOW_FAIL);
+	dst = kmalloc(sizeof(*dst) + num_pages * sizeof(u32 *), ATOMIC_MAYFAIL);
 	if (!dst)
 		return NULL;
 
@@ -1433,7 +1432,7 @@ capture_engine(struct intel_engine_cs *engine,
 	struct i915_request *rq = NULL;
 	unsigned long flags;
 
-	ee = intel_engine_coredump_alloc(engine, GFP_KERNEL);
+	ee = intel_engine_coredump_alloc(engine, ATOMIC_MAYFAIL);
 	if (!ee)
 		return NULL;
 
@@ -1481,7 +1480,7 @@ gt_record_engines(struct intel_gt_coredump *gt,
 		struct intel_engine_coredump *ee;
 
 		/* Refill our page pool before entering atomic section */
-		pool_refill(&compress->pool, ALLOW_FAIL);
+		pool_refill(&compress->pool, ATOMIC_MAYFAIL);
 
 		ee = capture_engine(engine, compress);
 		if (!ee)
@@ -1507,7 +1506,7 @@ gt_record_uc(struct intel_gt_coredump *gt,
 	const struct intel_uc *uc = &gt->_gt->uc;
 	struct intel_uc_coredump *error_uc;
 
-	error_uc = kzalloc(sizeof(*error_uc), ALLOW_FAIL);
+	error_uc = kzalloc(sizeof(*error_uc), ATOMIC_MAYFAIL);
 	if (!error_uc)
 		return NULL;
 
@@ -1518,8 +1517,8 @@ gt_record_uc(struct intel_gt_coredump *gt,
 	 * As modparams are generally accesible from the userspace make
 	 * explicit copies of the firmware paths.
 	 */
-	error_uc->guc_fw.path = kstrdup(uc->guc.fw.path, ALLOW_FAIL);
-	error_uc->huc_fw.path = kstrdup(uc->huc.fw.path, ALLOW_FAIL);
+	error_uc->guc_fw.path = kstrdup(uc->guc.fw.path, ATOMIC_MAYFAIL);
+	error_uc->huc_fw.path = kstrdup(uc->huc.fw.path, ATOMIC_MAYFAIL);
 	error_uc->guc_log =
 		i915_vma_coredump_create(gt->_gt,
 					 uc->guc.log.vma, "GuC log buffer",
@@ -1778,7 +1777,7 @@ i915_vma_capture_prepare(struct intel_gt_coredump *gt)
 {
 	struct i915_vma_compress *compress;
 
-	compress = kmalloc(sizeof(*compress), ALLOW_FAIL);
+	compress = kmalloc(sizeof(*compress), ATOMIC_MAYFAIL);
 	if (!compress)
 		return NULL;
 
@@ -1811,11 +1810,11 @@ i915_gpu_coredump(struct intel_gt *gt, intel_engine_mask_t engine_mask)
 	if (IS_ERR(error))
 		return error;
 
-	error = i915_gpu_coredump_alloc(i915, ALLOW_FAIL);
+	error = i915_gpu_coredump_alloc(i915, ATOMIC_MAYFAIL);
 	if (!error)
 		return ERR_PTR(-ENOMEM);
 
-	error->gt = intel_gt_coredump_alloc(gt, ALLOW_FAIL);
+	error->gt = intel_gt_coredump_alloc(gt, ATOMIC_MAYFAIL);
 	if (error->gt) {
 		struct i915_vma_compress *compress;
 
drivers/gpu/drm/i915/i915_request.h +15 -8
index 1bc1349ba3c2..177eaf55adff 100644
@@ -218,6 +218,11 @@ struct i915_request {
 	};
 	struct llist_head execute_cb;
 	struct i915_sw_fence semaphore;
+	/**
+	 * @submit_work: complete submit fence from an IRQ if needed for
+	 * locking hierarchy reasons.
+	 */
+	struct irq_work submit_work;
 
 	/*
 	 * A list of everyone we wait upon, and everyone who waits upon us.
@@ -285,18 +290,20 @@ 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.
+	/**
+	 * @guc_fence_link: 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. Protected by
+	 * ce->guc_state.lock.
 	 */
 	struct list_head guc_fence_link;
 
 	/**
-	 * Priority level while the request is inflight. Differs from i915
-	 * scheduler priority. See comment above
-	 * I915_SCHEDULER_CAP_STATIC_PRIORITY_MAP for details.
+	 * @guc_prio: Priority level while the request is inflight. Differs from
+	 * i915 scheduler priority. See comment above
+	 * I915_SCHEDULER_CAP_STATIC_PRIORITY_MAP for details. Protected by
+	 * ce->guc_active.lock.
 	 */
 #define	GUC_PRIO_INIT	0xff
 #define	GUC_PRIO_FINI	0xfe
drivers/gpu/drm/i915/i915_trace.h +4 -8
index 806ad688274b..ec7fe12b94aa 100644
@@ -805,7 +805,7 @@ DECLARE_EVENT_CLASS(i915_request,
 			   __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->guc_id = rq->context->guc_id.id;
 			   __entry->ctx = rq->fence.context;
 			   __entry->seqno = rq->fence.seqno;
 			   __entry->tail = rq->tail;
@@ -903,23 +903,19 @@ DECLARE_EVENT_CLASS(intel_context,
 			     __field(u32, guc_id)
 			     __field(int, pin_count)
 			     __field(u32, sched_state)
-			     __field(u32, guc_sched_state_no_lock)
 			     __field(u8, guc_prio)
 			     ),
 
 		    TP_fast_assign(
-			   __entry->guc_id = ce->guc_id;
+			   __entry->guc_id = ce->guc_id.id;
 			   __entry->pin_count = atomic_read(&ce->pin_count);
 			   __entry->sched_state = ce->guc_state.sched_state;
-			   __entry->guc_sched_state_no_lock =
-			   atomic_read(&ce->guc_sched_state_no_lock);
-			   __entry->guc_prio = ce->guc_prio;
+			   __entry->guc_prio = ce->guc_state.prio;
 			   ),
 
-		    TP_printk("guc_id=%d, pin_count=%d sched_state=0x%x,0x%x, guc_prio=%u",
+		    TP_printk("guc_id=%d, pin_count=%d sched_state=0x%x, guc_prio=%u",
 			      __entry->guc_id, __entry->pin_count,
 			      __entry->sched_state,
-			      __entry->guc_sched_state_no_lock,
 			      __entry->guc_prio)
 );
 
drivers/gpu/drm/i915/selftests/i915_live_selftests.h +1 -0
index cfa5c4165a4f..3cf6758931f9 100644
@@ -47,5 +47,6 @@ selftest(execlists, intel_execlists_live_selftests)
 selftest(ring_submission, intel_ring_submission_live_selftests)
 selftest(perf, i915_perf_live_selftests)
 selftest(slpc, intel_slpc_live_selftests)
+selftest(guc, intel_guc_live_selftests)
 /* Here be dragons: keep last to run last! */
 selftest(late_gt_pm, intel_gt_pm_late_selftests)
drivers/gpu/drm/i915/selftests/i915_request.c +100 -0

112 lines not on this page · raw message

drivers/gpu/drm/i915/selftests/intel_scheduler_helpers.c +12 -0
index 4b328346b48a..310fb83c527e 100644
@@ -14,6 +14,18 @@
 #define REDUCED_PREEMPT		10
 #define WAIT_FOR_RESET_TIME	10000
 
+struct intel_engine_cs *intel_selftest_find_any_engine(struct intel_gt *gt)
+{
+	struct intel_engine_cs *engine;
+	enum intel_engine_id id;
+
+	for_each_engine(engine, gt, id)
+		return engine;
+
+	pr_err("No valid engine found!\n");
+	return NULL;
+}
+
 int intel_selftest_modify_policy(struct intel_engine_cs *engine,
 				 struct intel_selftest_saved_policy *saved,
 				 u32 modify_type)
drivers/gpu/drm/i915/selftests/intel_scheduler_helpers.h +2 -0
index 35c098601ac0..ae60bb507f45 100644
@@ -10,6 +10,7 @@
 
 struct i915_request;
 struct intel_engine_cs;
+struct intel_gt;
 
 struct intel_selftest_saved_policy {
 	u32 flags;
@@ -23,6 +24,7 @@ enum selftest_scheduler_modify {
 	SELFTEST_SCHEDULER_MODIFY_FAST_RESET,
 };
 
+struct intel_engine_cs *intel_selftest_find_any_engine(struct intel_gt *gt);
 int intel_selftest_modify_policy(struct intel_engine_cs *engine,
 				 struct intel_selftest_saved_policy *saved,
 				 enum selftest_scheduler_modify modify_type);
Signature · 1 line
2.32.0
#2 lore raw

PATCH 2/27 drm/i915/guc: Allow flexible number of context ids

In reply to Matthew Brost

Number of available GuC contexts ids might be limited. Stop referring in code to macro and use variable instead.

---
 drivers/gpu/drm/i915/gt/uc/intel_guc.h            |  4 ++++
 drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 112dd29a63fe..6fd2719d1b75 100644
@@ -60,6 +60,10 @@ struct intel_guc {
 	spinlock_t contexts_lock;
 	/** @guc_ids: used to allocate new guc_ids */
 	struct ida guc_ids;
+	/** @num_guc_ids: number of guc_ids that can be used */
+	u32 num_guc_ids;
+	/** @max_guc_ids: max number of guc_ids that can be used */
+	u32 max_guc_ids;
 	/**
 	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
 	 */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -6
index 46158d996bf6..8235e49bb347 100644
@@ -344,7 +344,7 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
 {
 	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
 
-	GEM_BUG_ON(index >= GUC_MAX_LRC_DESCRIPTORS);
+	GEM_BUG_ON(index >= guc->max_guc_ids);
 
 	return &base[index];
 }
@@ -353,7 +353,7 @@ static 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);
+	GEM_BUG_ON(id >= guc->max_guc_ids);
 
 	return ce;
 }
@@ -363,8 +363,7 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
 	u32 size;
 	int ret;
 
-	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
-			  GUC_MAX_LRC_DESCRIPTORS);
+	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) * guc->max_guc_ids);
 	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
 					     (void **)&guc->lrc_desc_pool_vaddr);
 	if (ret)
@@ -1193,7 +1192,7 @@ static void guc_submit_request(struct i915_request *rq)
 static int new_guc_id(struct intel_guc *guc)
 {
 	return ida_simple_get(&guc->guc_ids, 0,
-			      GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+			      guc->num_guc_ids, GFP_KERNEL |
 			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
 }
 
@@ -2704,6 +2703,8 @@ static bool __guc_submission_selected(struct intel_guc *guc)
 
 void intel_guc_submission_init_early(struct intel_guc *guc)
 {
+	guc->max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
 	guc->submission_supported = __guc_submission_supported(guc);
 	guc->submission_selected = __guc_submission_selected(guc);
 }
@@ -2713,7 +2714,7 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
 {
 	struct intel_context *ce;
 
-	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+	if (unlikely(desc_idx >= guc->max_guc_ids)) {
 		drm_err(&guc_to_gt(guc)->i915->drm,
 			"Invalid desc_idx %u", desc_idx);
 		return NULL;
@@ -3063,6 +3064,8 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
 
 	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
 		   atomic_read(&guc->outstanding_submission_g2h));
+	drm_printf(p, "GuC Number GuC IDs: %u\n", guc->num_guc_ids);
+	drm_printf(p, "GuC Max GuC IDs: %u\n", guc->max_guc_ids);
 	drm_printf(p, "GuC tasklet count: %u\n\n",
 		   atomic_read(&sched_engine->tasklet.count));
 
Signature · 1 line
2.32.0
#3 lore raw

Re: [Intel-gfx] [PATCH 02/27] drm/i915/guc: Allow flexible number of context ids

Quoted 15 lines · Matthew Brost

Number of available GuC contexts ids might be limited. Stop referring in code to macro and use variable instead.

---
  drivers/gpu/drm/i915/gt/uc/intel_guc.h            |  4 ++++
  drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++++++------
  2 files changed, 13 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +0 -0
index 112dd29a63fe..6fd2719d1b75 100644

drivers/gpu/drm/i915/gt/uc/intel_guc.h struct intel_guc { · line 66

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
@@ -60,6 +60,10 @@ struct intel_guc {
  	spinlock_t contexts_lock;
  	/** @guc_ids: used to allocate new guc_ids */
  	struct ida guc_ids;
+	/** @num_guc_ids: number of guc_ids that can be used */
+	u32 num_guc_ids;
+	/** @max_guc_ids: max number of guc_ids that can be used */
+	u32 max_guc_ids;

How do these differ? The description needs to say how or why 'num' might be less than 'max'. And surely 'max' is not the count 'that can be used'? Num is how many can be used, but max is how many are physically possible or something?

John.

Quoted 71 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +0 -0
  	/**
  	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
  	 */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -6
index 46158d996bf6..8235e49bb347 100644
@@ -344,7 +344,7 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
  {
  	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
 
-	GEM_BUG_ON(index >= GUC_MAX_LRC_DESCRIPTORS);
+	GEM_BUG_ON(index >= guc->max_guc_ids);
 
  	return &base[index];
  }
@@ -353,7 +353,7 @@ static 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);
+	GEM_BUG_ON(id >= guc->max_guc_ids);
 
  	return ce;
  }
@@ -363,8 +363,7 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
  	u32 size;
  	int ret;
 
-	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
-			  GUC_MAX_LRC_DESCRIPTORS);
+	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) * guc->max_guc_ids);
  	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
  					     (void **)&guc->lrc_desc_pool_vaddr);
  	if (ret)
@@ -1193,7 +1192,7 @@ static void guc_submit_request(struct i915_request *rq)
  static int new_guc_id(struct intel_guc *guc)
  {
  	return ida_simple_get(&guc->guc_ids, 0,
-			      GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+			      guc->num_guc_ids, GFP_KERNEL |
  			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  }
 
@@ -2704,6 +2703,8 @@ static bool __guc_submission_selected(struct intel_guc *guc)
 
  void intel_guc_submission_init_early(struct intel_guc *guc)
  {
+	guc->max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
  	guc->submission_supported = __guc_submission_supported(guc);
  	guc->submission_selected = __guc_submission_selected(guc);
  }
@@ -2713,7 +2714,7 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  {
  	struct intel_context *ce;
 
-	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+	if (unlikely(desc_idx >= guc->max_guc_ids)) {
  		drm_err(&guc_to_gt(guc)->i915->drm,
  			"Invalid desc_idx %u", desc_idx);
  		return NULL;
@@ -3063,6 +3064,8 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
 
  	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
  		   atomic_read(&guc->outstanding_submission_g2h));
+	drm_printf(p, "GuC Number GuC IDs: %u\n", guc->num_guc_ids);
+	drm_printf(p, "GuC Max GuC IDs: %u\n", guc->max_guc_ids);
  	drm_printf(p, "GuC tasklet count: %u\n\n",
  		   atomic_read(&sched_engine->tasklet.count));
 
#4 lore raw

Re: [Intel-gfx] [PATCH 02/27] drm/i915/guc: Allow flexible number of context ids

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

Number of available GuC contexts ids might be limited. Stop referring in code to macro and use variable instead.

---
  drivers/gpu/drm/i915/gt/uc/intel_guc.h            |  4 ++++
  drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++++++------
  2 files changed, 13 insertions(+), 6 deletions(-)
Quoted 17 lines

drivers/gpu/drm/i915/gt/uc/intel_guc.h struct intel_guc { · line 66

Quoted 12 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +4 -0
index 112dd29a63fe..6fd2719d1b75 100644
@@ -60,6 +60,10 @@ struct intel_guc {
  	spinlock_t contexts_lock;
  	/** @guc_ids: used to allocate new guc_ids */
  	struct ida guc_ids;
+	/** @num_guc_ids: number of guc_ids that can be used */
+	u32 num_guc_ids;
+	/** @max_guc_ids: max number of guc_ids that can be used */
+	u32 max_guc_ids;

How do these differ? The description needs to say how or why 'num' might be less than 'max'. And surely 'max' is not the count 'that can be used'? Num is how many can be used, but max is how many are physically possible or something?

Max is the possible per OS / PF / VF instance, while num is the current setting. Makes more sense once SRIOV lands / if this is connected to debugfs (e.g. only num can be set via debugfs).

Matt

Quoted 64 lines

John.

Quoted 61 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +0 -0
  	/**
  	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
  	 */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -6
index 46158d996bf6..8235e49bb347 100644
@@ -344,7 +344,7 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
  {
  	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
-	GEM_BUG_ON(index >= GUC_MAX_LRC_DESCRIPTORS);
+	GEM_BUG_ON(index >= guc->max_guc_ids);
  	return &base[index];
  }
@@ -353,7 +353,7 @@ static 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);
+	GEM_BUG_ON(id >= guc->max_guc_ids);
  	return ce;
  }
@@ -363,8 +363,7 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
  	u32 size;
  	int ret;
-	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
-			  GUC_MAX_LRC_DESCRIPTORS);
+	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) * guc->max_guc_ids);
  	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
  					     (void **)&guc->lrc_desc_pool_vaddr);
  	if (ret)
@@ -1193,7 +1192,7 @@ static void guc_submit_request(struct i915_request *rq)
  static int new_guc_id(struct intel_guc *guc)
  {
  	return ida_simple_get(&guc->guc_ids, 0,
-			      GUC_MAX_LRC_DESCRIPTORS, GFP_KERNEL |
+			      guc->num_guc_ids, GFP_KERNEL |
  			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  }
@@ -2704,6 +2703,8 @@ static bool __guc_submission_selected(struct intel_guc *guc)
  void intel_guc_submission_init_early(struct intel_guc *guc)
  {
+	guc->max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
  	guc->submission_supported = __guc_submission_supported(guc);
  	guc->submission_selected = __guc_submission_selected(guc);
  }
@@ -2713,7 +2714,7 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  {
  	struct intel_context *ce;
-	if (unlikely(desc_idx >= GUC_MAX_LRC_DESCRIPTORS)) {
+	if (unlikely(desc_idx >= guc->max_guc_ids)) {
  		drm_err(&guc_to_gt(guc)->i915->drm,
  			"Invalid desc_idx %u", desc_idx);
  		return NULL;
@@ -3063,6 +3064,8 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
  	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
  		   atomic_read(&guc->outstanding_submission_g2h));
+	drm_printf(p, "GuC Number GuC IDs: %u\n", guc->num_guc_ids);
+	drm_printf(p, "GuC Max GuC IDs: %u\n", guc->max_guc_ids);
  	drm_printf(p, "GuC tasklet count: %u\n\n",
  		   atomic_read(&sched_engine->tasklet.count));
#5 lore raw

PATCH 3/27 drm/i915/guc: Connect the number of guc_ids to debugfs

In reply to Matthew Brost

For testing purposes it may make sense to reduce the number of guc_ids available to be allocated. Add debugfs support for setting the number of guc_ids.

---
 .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    | 31 +++++++++++++++++++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  3 +-
 2 files changed, 33 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +31 -0
index 887c8c8f35db..b88d343ee432 100644
@@ -71,12 +71,43 @@ static bool intel_eval_slpc_support(void *data)
 	return intel_guc_slpc_is_used(guc);
 }
 
+static int guc_num_id_get(void *data, u64 *val)
+{
+	struct intel_guc *guc = data;
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	*val = guc->num_guc_ids;
+
+	return 0;
+}
+
+static int guc_num_id_set(void *data, u64 val)
+{
+	struct intel_guc *guc = data;
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	if (val > guc->max_guc_ids)
+		val = guc->max_guc_ids;
+	else if (val < 256)
+		val = 256;
+
+	guc->num_guc_ids = val;
+
+	return 0;
+}
+DEFINE_SIMPLE_ATTRIBUTE(guc_num_id_fops, guc_num_id_get, guc_num_id_set, "%lld\n");
+
 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 },
 		{ "guc_slpc_info", &guc_slpc_info_fops, &intel_eval_slpc_support},
+		{ "guc_num_id", &guc_num_id_fops, NULL },
 	};
 
 	if (!intel_guc_is_supported(guc))
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -1
index 8235e49bb347..68742b612692 100644
@@ -2716,7 +2716,8 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
 
 	if (unlikely(desc_idx >= guc->max_guc_ids)) {
 		drm_err(&guc_to_gt(guc)->i915->drm,
-			"Invalid desc_idx %u", desc_idx);
+			"Invalid desc_idx %u, max %u",
+			desc_idx, guc->max_guc_ids);
 		return NULL;
 	}
 
Signature · 1 line
2.32.0
#6 lore raw

Re: [Intel-gfx] [PATCH 03/27] drm/i915/guc: Connect the number of guc_ids to debugfs

Quoted 5 lines · Matthew Brost

For testing purposes it may make sense to reduce the number of guc_ids available to be allocated. Add debugfs support for setting the number of guc_ids.

It 'may make sense'? Is there an actual IGT/selftest test that uses this and needs to in order to not take several hours to run or something? Seems wrong to be adding a debugfs interface for something that would only be used for testing by internal developers who can quite easy just edit a define in the code to simplify their testing.

John.

Quoted 67 lines
---
  .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    | 31 +++++++++++++++++++
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  3 +-
  2 files changed, 33 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +31 -0
index 887c8c8f35db..b88d343ee432 100644
@@ -71,12 +71,43 @@ static bool intel_eval_slpc_support(void *data)
  	return intel_guc_slpc_is_used(guc);
  }
 
+static int guc_num_id_get(void *data, u64 *val)
+{
+	struct intel_guc *guc = data;
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	*val = guc->num_guc_ids;
+
+	return 0;
+}
+
+static int guc_num_id_set(void *data, u64 val)
+{
+	struct intel_guc *guc = data;
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	if (val > guc->max_guc_ids)
+		val = guc->max_guc_ids;
+	else if (val < 256)
+		val = 256;
+
+	guc->num_guc_ids = val;
+
+	return 0;
+}
+DEFINE_SIMPLE_ATTRIBUTE(guc_num_id_fops, guc_num_id_get, guc_num_id_set, "%lld\n");
+
  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 },
  		{ "guc_slpc_info", &guc_slpc_info_fops, &intel_eval_slpc_support},
+		{ "guc_num_id", &guc_num_id_fops, NULL },
  	};
 
  	if (!intel_guc_is_supported(guc))
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -1
index 8235e49bb347..68742b612692 100644
@@ -2716,7 +2716,8 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
 
  	if (unlikely(desc_idx >= guc->max_guc_ids)) {
  		drm_err(&guc_to_gt(guc)->i915->drm,
-			"Invalid desc_idx %u", desc_idx);
+			"Invalid desc_idx %u, max %u",
+			desc_idx, guc->max_guc_ids);
  		return NULL;
  	}
 
#7 lore raw

Re: [Intel-gfx] [PATCH 03/27] drm/i915/guc: Connect the number of guc_ids to debugfs

Quoted 5 lines · John Harrison
Quoted 4 lines · Matthew Brost

For testing purposes it may make sense to reduce the number of guc_ids available to be allocated. Add debugfs support for setting the number of guc_ids.

Quoted 7 lines
Quoted 1 line

It 'may make sense'? Is there an actual IGT/selftest test that uses this and needs to in order to not take several hours to run or something? Seems wrong to be adding a debugfs interface for something that would only be used for testing by internal developers who can quite easy just edit a define in the code to simplify their testing.

Had an IGT (gem_exec_parallel) to toggle this to hit certain conditions that are very hard to hit (guc_id exhaustion) but this was we punted this problem to the user. Now we likely don't need that test and it is probably ok to just drop this patch.

Matt

Quoted 67 lines

John.

Quoted 63 lines
---
  .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    | 31 +++++++++++++++++++
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c |  3 +-
  2 files changed, 33 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +31 -0
index 887c8c8f35db..b88d343ee432 100644
@@ -71,12 +71,43 @@ static bool intel_eval_slpc_support(void *data)
  	return intel_guc_slpc_is_used(guc);
  }
+static int guc_num_id_get(void *data, u64 *val)
+{
+	struct intel_guc *guc = data;
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	*val = guc->num_guc_ids;
+
+	return 0;
+}
+
+static int guc_num_id_set(void *data, u64 val)
+{
+	struct intel_guc *guc = data;
+
+	if (!intel_guc_submission_is_used(guc))
+		return -ENODEV;
+
+	if (val > guc->max_guc_ids)
+		val = guc->max_guc_ids;
+	else if (val < 256)
+		val = 256;
+
+	guc->num_guc_ids = val;
+
+	return 0;
+}
+DEFINE_SIMPLE_ATTRIBUTE(guc_num_id_fops, guc_num_id_get, guc_num_id_set, "%lld\n");
+
  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 },
  		{ "guc_slpc_info", &guc_slpc_info_fops, &intel_eval_slpc_support},
+		{ "guc_num_id", &guc_num_id_fops, NULL },
  	};
  	if (!intel_guc_is_supported(guc))
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -1
index 8235e49bb347..68742b612692 100644
@@ -2716,7 +2716,8 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  	if (unlikely(desc_idx >= guc->max_guc_ids)) {
  		drm_err(&guc_to_gt(guc)->i915->drm,
-			"Invalid desc_idx %u", desc_idx);
+			"Invalid desc_idx %u, max %u",
+			desc_idx, guc->max_guc_ids);
  		return NULL;
  	}
#8 lore raw

PATCH 4/27 drm/i915/guc: Take GT PM ref when deregistering context

In reply to Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a deregister context H2G is in flight.

FIXME: Move locking / structure changes into different patch

---
 drivers/gpu/drm/i915/gt/intel_context.c       |   2 +
 drivers/gpu/drm/i915/gt/intel_context_types.h |  13 +-
 drivers/gpu/drm/i915/gt/intel_engine_pm.h     |   5 +
 drivers/gpu/drm/i915/gt/intel_gt_pm.h         |  13 ++
 .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  |   1 +
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  46 ++--
 .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    |  13 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 212 +++++++++++-------
 8 files changed, 199 insertions(+), 106 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +2 -0
index adfe49b53b1b..c8595da64ad8 100644
@@ -399,6 +399,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 	ce->guc_id.id = GUC_INVALID_LRC_ID;
 	INIT_LIST_HEAD(&ce->guc_id.link);
 
+	INIT_LIST_HEAD(&ce->destroyed_link);
+
 	/*
 	 * Initialize fence to be complete as this is expected to be complete
 	 * unless there is a pending schedule disable outstanding.
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -3
index 80bbdc7810f6..fd338a30617e 100644
@@ -190,22 +190,29 @@ struct intel_context {
 		/**
 		 * @id: unique handle which is used to communicate information
 		 * with the GuC about this context, protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
 		 */
 		u16 id;
 		/**
 		 * @ref: the number of references to the guc_id, when
 		 * transitioning in and out of zero protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
 		 */
 		atomic_t ref;
 		/**
 		 * @link: in guc->guc_id_list when the guc_id has no refs but is
-		 * still valid, protected by guc->contexts_lock
+		 * still valid, protected by guc->submission_state.lock
 		 */
 		struct list_head link;
 	} guc_id;
 
+	/**
+	 * @destroyed_link: link in guc->submission_state.destroyed_contexts, in
+	 * list when context is pending to be destroyed (deregistered with the
+	 * GuC), protected by guc->submission_state.lock
+	 */
+	struct list_head destroyed_link;
+
 #ifdef CONFIG_DRM_I915_SELFTEST
 	/**
 	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
drivers/gpu/drm/i915/gt/intel_engine_pm.h +5 -0
index 70ea46d6cfb0..17a5028ea177 100644
@@ -16,6 +16,11 @@ intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
 	return intel_wakeref_is_active(&engine->wakeref);
 }
 
+static inline void __intel_engine_pm_get(struct intel_engine_cs *engine)
+{
+	__intel_wakeref_get(&engine->wakeref);
+}
+
 static inline void intel_engine_pm_get(struct intel_engine_cs *engine)
 {
 	intel_wakeref_get(&engine->wakeref);
drivers/gpu/drm/i915/gt/intel_gt_pm.h +13 -0
index d0588d8aaa44..a17bf0d4592b 100644
@@ -41,6 +41,19 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
 	intel_wakeref_put_async(&gt->wakeref);
 }
 
+#define with_intel_gt_pm(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_async(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake_async(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+
 static inline int intel_gt_pm_wait_for_idle(struct intel_gt *gt)
 {
 	return intel_wakeref_wait_for_idle(&gt->wakeref);
drivers/gpu/drm/i915/gt/uc/abi/guc_actions_abi.h +1 -0
index 8ff582222aff..ba10bd374cee 100644
@@ -142,6 +142,7 @@ enum intel_guc_action {
 	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_REGISTER_CONTEXT_MULTI_LRC = 0x4601,
 	INTEL_GUC_ACTION_RESET_CLIENT = 0x5507,
 	INTEL_GUC_ACTION_LIMIT
 };
drivers/gpu/drm/i915/gt/uc/intel_guc.h +31 -15

52 lines not on this page · raw message

drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +9 -4
index b88d343ee432..27655460ee84 100644
@@ -78,7 +78,7 @@ static int guc_num_id_get(void *data, u64 *val)
 	if (!intel_guc_submission_is_used(guc))
 		return -ENODEV;
 
-	*val = guc->num_guc_ids;
+	*val = guc->submission_state.num_guc_ids;
 
 	return 0;
 }
@@ -86,16 +86,21 @@ static int guc_num_id_get(void *data, u64 *val)
 static int guc_num_id_set(void *data, u64 val)
 {
 	struct intel_guc *guc = data;
+	unsigned long flags;
 
 	if (!intel_guc_submission_is_used(guc))
 		return -ENODEV;
 
-	if (val > guc->max_guc_ids)
-		val = guc->max_guc_ids;
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+
+	if (val > guc->submission_state.max_guc_ids)
+		val = guc->submission_state.max_guc_ids;
 	else if (val < 256)
 		val = 256;
 
-	guc->num_guc_ids = val;
+	guc->submission_state.num_guc_ids = val;
+
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
 
 	return 0;
 }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +128 -84

391 lines not on this page · raw message

Signature · 1 line
2.32.0
#9 lore raw

Re: [Intel-gfx] [PATCH 04/27] drm/i915/guc: Take GT PM ref when deregistering context

Quoted 4 lines · Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a deregister context H2G is in flight.

FIXME: Move locking / structure changes into different patch

This split needs to be done. It would also be helpful to have a more detailed explanation of what is going on and why the change is necessary. Is this a generic problem and the code currently in the tree is broken? Or is it something specific to parallel submission and isn't actually a problem until later in the series?

John.

Quoted 629 lines
---
  drivers/gpu/drm/i915/gt/intel_context.c       |   2 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  13 +-
  drivers/gpu/drm/i915/gt/intel_engine_pm.h     |   5 +
  drivers/gpu/drm/i915/gt/intel_gt_pm.h         |  13 ++
  .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  |   1 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  46 ++--
  .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    |  13 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 212 +++++++++++-------
  8 files changed, 199 insertions(+), 106 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +2 -0
index adfe49b53b1b..c8595da64ad8 100644
@@ -399,6 +399,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	ce->guc_id.id = GUC_INVALID_LRC_ID;
  	INIT_LIST_HEAD(&ce->guc_id.link);
 
+	INIT_LIST_HEAD(&ce->destroyed_link);
+
  	/*
  	 * Initialize fence to be complete as this is expected to be complete
  	 * unless there is a pending schedule disable outstanding.
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -3
index 80bbdc7810f6..fd338a30617e 100644
@@ -190,22 +190,29 @@ struct intel_context {
  		/**
  		 * @id: unique handle which is used to communicate information
  		 * with the GuC about this context, protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
  		 */
  		u16 id;
  		/**
  		 * @ref: the number of references to the guc_id, when
  		 * transitioning in and out of zero protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
  		 */
  		atomic_t ref;
  		/**
  		 * @link: in guc->guc_id_list when the guc_id has no refs but is
-		 * still valid, protected by guc->contexts_lock
+		 * still valid, protected by guc->submission_state.lock
  		 */
  		struct list_head link;
  	} guc_id;
 
+	/**
+	 * @destroyed_link: link in guc->submission_state.destroyed_contexts, in
+	 * list when context is pending to be destroyed (deregistered with the
+	 * GuC), protected by guc->submission_state.lock
+	 */
+	struct list_head destroyed_link;
+
  #ifdef CONFIG_DRM_I915_SELFTEST
  	/**
  	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
drivers/gpu/drm/i915/gt/intel_engine_pm.h +5 -0
index 70ea46d6cfb0..17a5028ea177 100644
@@ -16,6 +16,11 @@ intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
  	return intel_wakeref_is_active(&engine->wakeref);
  }
 
+static inline void __intel_engine_pm_get(struct intel_engine_cs *engine)
+{
+	__intel_wakeref_get(&engine->wakeref);
+}
+
  static inline void intel_engine_pm_get(struct intel_engine_cs *engine)
  {
  	intel_wakeref_get(&engine->wakeref);
drivers/gpu/drm/i915/gt/intel_gt_pm.h +13 -0
index d0588d8aaa44..a17bf0d4592b 100644
@@ -41,6 +41,19 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
  	intel_wakeref_put_async(&gt->wakeref);
  }
 
+#define with_intel_gt_pm(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_async(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake_async(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+
  static inline int intel_gt_pm_wait_for_idle(struct intel_gt *gt)
  {
  	return intel_wakeref_wait_for_idle(&gt->wakeref);
drivers/gpu/drm/i915/gt/uc/abi/guc_actions_abi.h +1 -0
index 8ff582222aff..ba10bd374cee 100644
@@ -142,6 +142,7 @@ enum intel_guc_action {
  	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_REGISTER_CONTEXT_MULTI_LRC = 0x4601,
  	INTEL_GUC_ACTION_RESET_CLIENT = 0x5507,
  	INTEL_GUC_ACTION_LIMIT
  };
drivers/gpu/drm/i915/gt/uc/intel_guc.h +31 -15
index 6fd2719d1b75..7358883f1540 100644
@@ -53,21 +53,37 @@ struct intel_guc {
  		void (*disable)(struct intel_guc *guc);
  	} interrupts;
 
-	/**
-	 * @contexts_lock: protects guc_ids, guc_id_list, ce->guc_id.id, and
-	 * ce->guc_id.ref when transitioning in and out of zero
-	 */
-	spinlock_t contexts_lock;
-	/** @guc_ids: used to allocate new guc_ids */
-	struct ida guc_ids;
-	/** @num_guc_ids: number of guc_ids that can be used */
-	u32 num_guc_ids;
-	/** @max_guc_ids: max number of guc_ids that can be used */
-	u32 max_guc_ids;
-	/**
-	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
-	 */
-	struct list_head guc_id_list;
+	struct {
+		/**
+		 * @lock: protects everything in submission_state, ce->guc_id,
+		 * and ce->destroyed_link
+		 */
+		spinlock_t lock;
+		/**
+		 * @guc_ids: used to allocate new guc_ids
+		 */
+		struct ida guc_ids;
+		/** @num_guc_ids: number of guc_ids that can be used */
+		u32 num_guc_ids;
+		/** @max_guc_ids: max number of guc_ids that can be used */
+		u32 max_guc_ids;
+		/**
+		 * @guc_id_list: list of intel_context with valid guc_ids but no
+		 * refs
+		 */
+		struct list_head guc_id_list;
+		/**
+		 * @destroyed_contexts: list of contexts waiting to be destroyed
+		 * (deregistered with the GuC)
+		 */
+		struct list_head destroyed_contexts;
+		/**
+		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * need to take a GT PM reference and can't from destroy
+		 * function as it might be in an atomic context (no sleeping)
+		 */
+		struct work_struct destroyed_worker;
+	} submission_state;
 
  	bool submission_supported;
  	bool submission_selected;
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +9 -4
index b88d343ee432..27655460ee84 100644
@@ -78,7 +78,7 @@ static int guc_num_id_get(void *data, u64 *val)
  	if (!intel_guc_submission_is_used(guc))
  		return -ENODEV;
 
-	*val = guc->num_guc_ids;
+	*val = guc->submission_state.num_guc_ids;
 
  	return 0;
  }
@@ -86,16 +86,21 @@ static int guc_num_id_get(void *data, u64 *val)
  static int guc_num_id_set(void *data, u64 val)
  {
  	struct intel_guc *guc = data;
+	unsigned long flags;
 
  	if (!intel_guc_submission_is_used(guc))
  		return -ENODEV;
 
-	if (val > guc->max_guc_ids)
-		val = guc->max_guc_ids;
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+
+	if (val > guc->submission_state.max_guc_ids)
+		val = guc->submission_state.max_guc_ids;
  	else if (val < 256)
  		val = 256;
 
-	guc->num_guc_ids = val;
+	guc->submission_state.num_guc_ids = val;
+
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
 
  	return 0;
  }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +128 -84
index 68742b612692..f835e06e5f9f 100644
@@ -86,9 +86,9 @@
   * submitting at a time. Currently only 1 sched_engine used for all of GuC
   * submission but that could change in the future.
   *
- * guc->contexts_lock
- * Protects guc_id allocation. Global lock i.e. Only 1 context that uses GuC
- * submission can hold this at a time.
+ * guc->submission_state.lock
+ * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
+ * list.
   *
   * ce->guc_state.lock
   * Protects everything under ce->guc_state. Ensures that a context is in the
@@ -100,7 +100,7 @@
   *
   * Lock ordering rules:
   * sched_engine->lock -> ce->guc_state.lock
- * guc->contexts_lock -> ce->guc_state.lock
+ * guc->submission_state.lock -> ce->guc_state.lock
   *
   * Reset races:
   * When a GPU full reset is triggered it is assumed that some G2H responses to
@@ -344,7 +344,7 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
  {
  	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
 
-	GEM_BUG_ON(index >= guc->max_guc_ids);
+	GEM_BUG_ON(index >= guc->submission_state.max_guc_ids);
 
  	return &base[index];
  }
@@ -353,7 +353,7 @@ static 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_guc_ids);
+	GEM_BUG_ON(id >= guc->submission_state.max_guc_ids);
 
  	return ce;
  }
@@ -363,7 +363,8 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
  	u32 size;
  	int ret;
 
-	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) * guc->max_guc_ids);
+	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
+			  guc->submission_state.max_guc_ids);
  	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
  					     (void **)&guc->lrc_desc_pool_vaddr);
  	if (ret)
@@ -711,6 +712,7 @@ static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
  			if (deregister)
  				guc_signal_context_fence(ce);
  			if (destroyed) {
+				intel_gt_pm_put_async(guc_to_gt(guc));
  				release_guc_id(guc, ce);
  				__guc_context_destroy(ce);
  			}
@@ -789,6 +791,8 @@ static void guc_flush_submissions(struct intel_guc *guc)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
 
+static void guc_flush_destroyed_contexts(struct intel_guc *guc);
+
  void intel_guc_submission_reset_prepare(struct intel_guc *guc)
  {
  	if (unlikely(!guc_submission_initialized(guc))) {
@@ -805,6 +809,7 @@ void intel_guc_submission_reset_prepare(struct intel_guc *guc)
  	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
 
  	flush_work(&guc->ct.requests.worker);
+	guc_flush_destroyed_contexts(guc);
 
  	scrub_guc_desc_for_outstanding_g2h(guc);
  }
@@ -1102,6 +1107,8 @@ void intel_guc_submission_reset_finish(struct intel_guc *guc)
  	intel_gt_unpark_heartbeats(guc_to_gt(guc));
  }
 
+static void destroyed_worker_func(struct work_struct *w);
+
  /*
   * Set up the memory resources to be shared with the GuC (via the GGTT)
   * at firmware loading time.
@@ -1124,9 +1131,11 @@ 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);
+	spin_lock_init(&guc->submission_state.lock);
+	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
+	ida_init(&guc->submission_state.guc_ids);
+	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
+	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
 
  	return 0;
  }
@@ -1137,6 +1146,7 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  		return;
 
  	guc_lrc_desc_pool_destroy(guc);
+	guc_flush_destroyed_contexts(guc);
  	i915_sched_engine_put(guc->sched_engine);
  }
 
@@ -1191,15 +1201,16 @@ static void guc_submit_request(struct i915_request *rq)
 
  static int new_guc_id(struct intel_guc *guc)
  {
-	return ida_simple_get(&guc->guc_ids, 0,
-			      guc->num_guc_ids, GFP_KERNEL |
+	return ida_simple_get(&guc->submission_state.guc_ids, 0,
+			      guc->submission_state.num_guc_ids, 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.id);
+		ida_simple_remove(&guc->submission_state.guc_ids,
+				  ce->guc_id.id);
  		reset_lrc_desc(guc, ce->guc_id.id);
  		set_context_guc_id_invalid(ce);
  	}
@@ -1211,9 +1222,9 @@ static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
  	unsigned long flags;
 
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
  	__release_guc_id(guc, ce);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
 
  static int steal_guc_id(struct intel_guc *guc)
@@ -1221,10 +1232,10 @@ static int steal_guc_id(struct intel_guc *guc)
  	struct intel_context *ce;
  	int guc_id;
 
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
 
-	if (!list_empty(&guc->guc_id_list)) {
-		ce = list_first_entry(&guc->guc_id_list,
+	if (!list_empty(&guc->submission_state.guc_id_list)) {
+		ce = list_first_entry(&guc->submission_state.guc_id_list,
  				      struct intel_context,
  				      guc_id.link);
 
@@ -1249,7 +1260,7 @@ static int assign_guc_id(struct intel_guc *guc, u16 *out)
  {
  	int ret;
 
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
 
  	ret = new_guc_id(guc);
  	if (unlikely(ret < 0)) {
@@ -1271,7 +1282,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
 
  try_again:
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
 
  	might_lock(&ce->guc_state.lock);
 
@@ -1286,7 +1297,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	atomic_inc(&ce->guc_id.ref);
 
  out_unlock:
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
 
  	/*
  	 * -EAGAIN indicates no guc_id are available, let's retire any
@@ -1322,11 +1333,12 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	if (unlikely(context_guc_id_invalid(ce)))
  		return;
 
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.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);
+		list_add_tail(&ce->guc_id.link,
+			      &guc->submission_state.guc_id_list);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
 
  static int __guc_action_register_context(struct intel_guc *guc,
@@ -1841,11 +1853,30 @@ static void guc_context_sched_disable(struct intel_context *ce)
  static void guc_lrc_desc_unpin(struct intel_context *ce)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
+	struct intel_gt *gt = guc_to_gt(guc);
+	unsigned long flags;
+	bool disabled;
 
+	GEM_BUG_ON(!intel_gt_pm_is_awake(gt));
  	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id.id));
  	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
  	GEM_BUG_ON(context_enabled(ce));
 
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled)) {
+		__intel_gt_pm_get(gt);
+		set_context_destroyed(ce);
+		clr_context_registered(ce);
+	}
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
  	deregister_context(ce, ce->guc_id.id, true);
  }
 
@@ -1873,78 +1904,88 @@ static void __guc_context_destroy(struct intel_context *ce)
  	}
  }
 
+static void guc_flush_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	GEM_BUG_ON(!submission_disabled(guc) &&
+		   guc_submission_initialized(guc));
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		__release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void deregister_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+		guc_lrc_desc_unpin(ce);
+		spin_lock_irqsave(&guc->submission_state.lock, flags);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void destroyed_worker_func(struct work_struct *w)
+{
+	struct intel_guc *guc = container_of(w, struct intel_guc,
+					     submission_state.destroyed_worker);
+	struct intel_gt *gt = guc_to_gt(guc);
+	int tmp;
+
+	with_intel_gt_pm(gt, tmp)
+		deregister_destroyed_contexts(guc);
+}
+
  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->uncore->rpm;
  	struct intel_guc *guc = ce_to_guc(ce);
-	intel_wakeref_t wakeref;
  	unsigned long flags;
-	bool disabled;
+	bool destroy;
 
  	/*
  	 * 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 or the GuC is in the middle of a reset.
  	 */
-	if (context_guc_id_invalid(ce)) {
-		__guc_context_destroy(ce);
-		return;
-	} else if (submission_disabled(guc) ||
-		   !lrc_desc_registered(guc, ce->guc_id.id)) {
-		release_guc_id(guc, ce);
-		__guc_context_destroy(ce);
-		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_id available to
-	 * steal 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_id.
-	 */
-	spin_lock_irqsave(&guc->contexts_lock, flags);
-	if (context_guc_id_invalid(ce)) {
-		spin_unlock_irqrestore(&guc->contexts_lock, flags);
-		__guc_context_destroy(ce);
-		return;
-	}
-
-	if (!list_empty(&ce->guc_id.link))
-		list_del_init(&ce->guc_id.link);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
-
-	/* Seal race with Reset */
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	disabled = submission_disabled(guc);
-	if (likely(!disabled)) {
-		set_context_destroyed(ce);
-		clr_context_registered(ce);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	destroy = submission_disabled(guc) || context_guc_id_invalid(ce) ||
+		!lrc_desc_registered(guc, ce->guc_id.id);
+	if (likely(!destroy)) {
+		if (!list_empty(&ce->guc_id.link))
+			list_del_init(&ce->guc_id.link);
+		list_add_tail(&ce->destroyed_link,
+			      &guc->submission_state.destroyed_contexts);
+	} else {
+		__release_guc_id(guc, ce);
  	}
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-	if (unlikely(disabled)) {
-		release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+	if (unlikely(destroy)) {
  		__guc_context_destroy(ce);
  		return;
  	}
 
  	/*
-	 * We defer GuC context deregistration until the context is destroyed
-	 * in order to save on CTBs. With this optimization ideally we only need
-	 * 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 runtime wakeref is not 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.
+	 * We use a worker to issue the H2G to deregister the context as we can
+	 * take the GT PM for the first time which isn't allowed from an atomic
+	 * context.
  	 */
-	with_intel_runtime_pm(runtime_pm, wakeref)
-		guc_lrc_desc_unpin(ce);
+	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
  }
 
  static int guc_context_alloc(struct intel_context *ce)
@@ -2703,8 +2744,8 @@ static bool __guc_submission_selected(struct intel_guc *guc)
 
  void intel_guc_submission_init_early(struct intel_guc *guc)
  {
-	guc->max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
-	guc->num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
  	guc->submission_supported = __guc_submission_supported(guc);
  	guc->submission_selected = __guc_submission_selected(guc);
  }
@@ -2714,10 +2755,10 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  {
  	struct intel_context *ce;
 
-	if (unlikely(desc_idx >= guc->max_guc_ids)) {
+	if (unlikely(desc_idx >= guc->submission_state.max_guc_ids)) {
  		drm_err(&guc_to_gt(guc)->i915->drm,
  			"Invalid desc_idx %u, max %u",
-			desc_idx, guc->max_guc_ids);
+			desc_idx, guc->submission_state.max_guc_ids);
  		return NULL;
  	}
 
@@ -2771,6 +2812,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		intel_context_put(ce);
  	} else if (context_destroyed(ce)) {
  		/* Context has been destroyed */
+		intel_gt_pm_put_async(guc_to_gt(guc));
  		release_guc_id(guc, ce);
  		__guc_context_destroy(ce);
  	}
@@ -3065,8 +3107,10 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
 
  	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
  		   atomic_read(&guc->outstanding_submission_g2h));
-	drm_printf(p, "GuC Number GuC IDs: %u\n", guc->num_guc_ids);
-	drm_printf(p, "GuC Max GuC IDs: %u\n", guc->max_guc_ids);
+	drm_printf(p, "GuC Number GuC IDs: %u\n",
+		   guc->submission_state.num_guc_ids);
+	drm_printf(p, "GuC Max GuC IDs: %u\n",
+		   guc->submission_state.max_guc_ids);
  	drm_printf(p, "GuC tasklet count: %u\n\n",
  		   atomic_read(&sched_engine->tasklet.count));
 
#10 lore raw

Re: [Intel-gfx] [PATCH 04/27] drm/i915/guc: Take GT PM ref when deregistering context

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

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a deregister context H2G is in flight.

FIXME: Move locking / structure changes into different patch

This split needs to be done. It would also be helpful to have a more

Can do the split in the next rev.

Quoted 5 lines

detailed explanation of what is going on and why the change is necessary. Is this a generic problem and the code currently in the tree is broken? Or is it something specific to parallel submission and isn't actually a problem until later in the series?

This a generic problem - we should always have PM reference when a user context has submission enable (a follow on patch) or any G2H is in flight to avoid reporting the GPU falsely idle.

Matt

Quoted 578 lines

John.

Quoted 575 lines
---
  drivers/gpu/drm/i915/gt/intel_context.c       |   2 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  13 +-
  drivers/gpu/drm/i915/gt/intel_engine_pm.h     |   5 +
  drivers/gpu/drm/i915/gt/intel_gt_pm.h         |  13 ++
  .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  |   1 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  46 ++--
  .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    |  13 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 212 +++++++++++-------
  8 files changed, 199 insertions(+), 106 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +2 -0
index adfe49b53b1b..c8595da64ad8 100644
@@ -399,6 +399,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	ce->guc_id.id = GUC_INVALID_LRC_ID;
  	INIT_LIST_HEAD(&ce->guc_id.link);
+	INIT_LIST_HEAD(&ce->destroyed_link);
+
  	/*
  	 * Initialize fence to be complete as this is expected to be complete
  	 * unless there is a pending schedule disable outstanding.
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -3
index 80bbdc7810f6..fd338a30617e 100644
@@ -190,22 +190,29 @@ struct intel_context {
  		/**
  		 * @id: unique handle which is used to communicate information
  		 * with the GuC about this context, protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
  		 */
  		u16 id;
  		/**
  		 * @ref: the number of references to the guc_id, when
  		 * transitioning in and out of zero protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
  		 */
  		atomic_t ref;
  		/**
  		 * @link: in guc->guc_id_list when the guc_id has no refs but is
-		 * still valid, protected by guc->contexts_lock
+		 * still valid, protected by guc->submission_state.lock
  		 */
  		struct list_head link;
  	} guc_id;
+	/**
+	 * @destroyed_link: link in guc->submission_state.destroyed_contexts, in
+	 * list when context is pending to be destroyed (deregistered with the
+	 * GuC), protected by guc->submission_state.lock
+	 */
+	struct list_head destroyed_link;
+
  #ifdef CONFIG_DRM_I915_SELFTEST
  	/**
  	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
drivers/gpu/drm/i915/gt/intel_engine_pm.h +5 -0
index 70ea46d6cfb0..17a5028ea177 100644
@@ -16,6 +16,11 @@ intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
  	return intel_wakeref_is_active(&engine->wakeref);
  }
+static inline void __intel_engine_pm_get(struct intel_engine_cs *engine)
+{
+	__intel_wakeref_get(&engine->wakeref);
+}
+
  static inline void intel_engine_pm_get(struct intel_engine_cs *engine)
  {
  	intel_wakeref_get(&engine->wakeref);
drivers/gpu/drm/i915/gt/intel_gt_pm.h +13 -0
index d0588d8aaa44..a17bf0d4592b 100644
@@ -41,6 +41,19 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
  	intel_wakeref_put_async(&gt->wakeref);
  }
+#define with_intel_gt_pm(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_async(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake_async(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+
  static inline int intel_gt_pm_wait_for_idle(struct intel_gt *gt)
  {
  	return intel_wakeref_wait_for_idle(&gt->wakeref);
drivers/gpu/drm/i915/gt/uc/abi/guc_actions_abi.h +1 -0
index 8ff582222aff..ba10bd374cee 100644
@@ -142,6 +142,7 @@ enum intel_guc_action {
  	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_REGISTER_CONTEXT_MULTI_LRC = 0x4601,
  	INTEL_GUC_ACTION_RESET_CLIENT = 0x5507,
  	INTEL_GUC_ACTION_LIMIT
  };
drivers/gpu/drm/i915/gt/uc/intel_guc.h +31 -15
index 6fd2719d1b75..7358883f1540 100644
@@ -53,21 +53,37 @@ struct intel_guc {
  		void (*disable)(struct intel_guc *guc);
  	} interrupts;
-	/**
-	 * @contexts_lock: protects guc_ids, guc_id_list, ce->guc_id.id, and
-	 * ce->guc_id.ref when transitioning in and out of zero
-	 */
-	spinlock_t contexts_lock;
-	/** @guc_ids: used to allocate new guc_ids */
-	struct ida guc_ids;
-	/** @num_guc_ids: number of guc_ids that can be used */
-	u32 num_guc_ids;
-	/** @max_guc_ids: max number of guc_ids that can be used */
-	u32 max_guc_ids;
-	/**
-	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
-	 */
-	struct list_head guc_id_list;
+	struct {
+		/**
+		 * @lock: protects everything in submission_state, ce->guc_id,
+		 * and ce->destroyed_link
+		 */
+		spinlock_t lock;
+		/**
+		 * @guc_ids: used to allocate new guc_ids
+		 */
+		struct ida guc_ids;
+		/** @num_guc_ids: number of guc_ids that can be used */
+		u32 num_guc_ids;
+		/** @max_guc_ids: max number of guc_ids that can be used */
+		u32 max_guc_ids;
+		/**
+		 * @guc_id_list: list of intel_context with valid guc_ids but no
+		 * refs
+		 */
+		struct list_head guc_id_list;
+		/**
+		 * @destroyed_contexts: list of contexts waiting to be destroyed
+		 * (deregistered with the GuC)
+		 */
+		struct list_head destroyed_contexts;
+		/**
+		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * need to take a GT PM reference and can't from destroy
+		 * function as it might be in an atomic context (no sleeping)
+		 */
+		struct work_struct destroyed_worker;
+	} submission_state;
  	bool submission_supported;
  	bool submission_selected;
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +9 -4
index b88d343ee432..27655460ee84 100644
@@ -78,7 +78,7 @@ static int guc_num_id_get(void *data, u64 *val)
  	if (!intel_guc_submission_is_used(guc))
  		return -ENODEV;
-	*val = guc->num_guc_ids;
+	*val = guc->submission_state.num_guc_ids;
  	return 0;
  }
@@ -86,16 +86,21 @@ static int guc_num_id_get(void *data, u64 *val)
  static int guc_num_id_set(void *data, u64 val)
  {
  	struct intel_guc *guc = data;
+	unsigned long flags;
  	if (!intel_guc_submission_is_used(guc))
  		return -ENODEV;
-	if (val > guc->max_guc_ids)
-		val = guc->max_guc_ids;
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+
+	if (val > guc->submission_state.max_guc_ids)
+		val = guc->submission_state.max_guc_ids;
  	else if (val < 256)
  		val = 256;
-	guc->num_guc_ids = val;
+	guc->submission_state.num_guc_ids = val;
+
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  	return 0;
  }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +128 -84
index 68742b612692..f835e06e5f9f 100644
@@ -86,9 +86,9 @@
   * submitting at a time. Currently only 1 sched_engine used for all of GuC
   * submission but that could change in the future.
   *
- * guc->contexts_lock
- * Protects guc_id allocation. Global lock i.e. Only 1 context that uses GuC
- * submission can hold this at a time.
+ * guc->submission_state.lock
+ * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
+ * list.
   *
   * ce->guc_state.lock
   * Protects everything under ce->guc_state. Ensures that a context is in the
@@ -100,7 +100,7 @@
   *
   * Lock ordering rules:
   * sched_engine->lock -> ce->guc_state.lock
- * guc->contexts_lock -> ce->guc_state.lock
+ * guc->submission_state.lock -> ce->guc_state.lock
   *
   * Reset races:
   * When a GPU full reset is triggered it is assumed that some G2H responses to
@@ -344,7 +344,7 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
  {
  	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
-	GEM_BUG_ON(index >= guc->max_guc_ids);
+	GEM_BUG_ON(index >= guc->submission_state.max_guc_ids);
  	return &base[index];
  }
@@ -353,7 +353,7 @@ static 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_guc_ids);
+	GEM_BUG_ON(id >= guc->submission_state.max_guc_ids);
  	return ce;
  }
@@ -363,7 +363,8 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
  	u32 size;
  	int ret;
-	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) * guc->max_guc_ids);
+	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
+			  guc->submission_state.max_guc_ids);
  	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
  					     (void **)&guc->lrc_desc_pool_vaddr);
  	if (ret)
@@ -711,6 +712,7 @@ static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
  			if (deregister)
  				guc_signal_context_fence(ce);
  			if (destroyed) {
+				intel_gt_pm_put_async(guc_to_gt(guc));
  				release_guc_id(guc, ce);
  				__guc_context_destroy(ce);
  			}
@@ -789,6 +791,8 @@ static void guc_flush_submissions(struct intel_guc *guc)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
+static void guc_flush_destroyed_contexts(struct intel_guc *guc);
+
  void intel_guc_submission_reset_prepare(struct intel_guc *guc)
  {
  	if (unlikely(!guc_submission_initialized(guc))) {
@@ -805,6 +809,7 @@ void intel_guc_submission_reset_prepare(struct intel_guc *guc)
  	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
  	flush_work(&guc->ct.requests.worker);
+	guc_flush_destroyed_contexts(guc);
  	scrub_guc_desc_for_outstanding_g2h(guc);
  }
@@ -1102,6 +1107,8 @@ void intel_guc_submission_reset_finish(struct intel_guc *guc)
  	intel_gt_unpark_heartbeats(guc_to_gt(guc));
  }
+static void destroyed_worker_func(struct work_struct *w);
+
  /*
   * Set up the memory resources to be shared with the GuC (via the GGTT)
   * at firmware loading time.
@@ -1124,9 +1131,11 @@ 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);
+	spin_lock_init(&guc->submission_state.lock);
+	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
+	ida_init(&guc->submission_state.guc_ids);
+	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
+	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
  	return 0;
  }
@@ -1137,6 +1146,7 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  		return;
  	guc_lrc_desc_pool_destroy(guc);
+	guc_flush_destroyed_contexts(guc);
  	i915_sched_engine_put(guc->sched_engine);
  }
@@ -1191,15 +1201,16 @@ static void guc_submit_request(struct i915_request *rq)
  static int new_guc_id(struct intel_guc *guc)
  {
-	return ida_simple_get(&guc->guc_ids, 0,
-			      guc->num_guc_ids, GFP_KERNEL |
+	return ida_simple_get(&guc->submission_state.guc_ids, 0,
+			      guc->submission_state.num_guc_ids, 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.id);
+		ida_simple_remove(&guc->submission_state.guc_ids,
+				  ce->guc_id.id);
  		reset_lrc_desc(guc, ce->guc_id.id);
  		set_context_guc_id_invalid(ce);
  	}
@@ -1211,9 +1222,9 @@ static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
  	unsigned long flags;
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
  	__release_guc_id(guc, ce);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
  static int steal_guc_id(struct intel_guc *guc)
@@ -1221,10 +1232,10 @@ static int steal_guc_id(struct intel_guc *guc)
  	struct intel_context *ce;
  	int guc_id;
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
-	if (!list_empty(&guc->guc_id_list)) {
-		ce = list_first_entry(&guc->guc_id_list,
+	if (!list_empty(&guc->submission_state.guc_id_list)) {
+		ce = list_first_entry(&guc->submission_state.guc_id_list,
  				      struct intel_context,
  				      guc_id.link);
@@ -1249,7 +1260,7 @@ static int assign_guc_id(struct intel_guc *guc, u16 *out)
  {
  	int ret;
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
  	ret = new_guc_id(guc);
  	if (unlikely(ret < 0)) {
@@ -1271,7 +1282,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
  try_again:
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
  	might_lock(&ce->guc_state.lock);
@@ -1286,7 +1297,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	atomic_inc(&ce->guc_id.ref);
  out_unlock:
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  	/*
  	 * -EAGAIN indicates no guc_id are available, let's retire any
@@ -1322,11 +1333,12 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	if (unlikely(context_guc_id_invalid(ce)))
  		return;
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.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);
+		list_add_tail(&ce->guc_id.link,
+			      &guc->submission_state.guc_id_list);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
  static int __guc_action_register_context(struct intel_guc *guc,
@@ -1841,11 +1853,30 @@ static void guc_context_sched_disable(struct intel_context *ce)
  static void guc_lrc_desc_unpin(struct intel_context *ce)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
+	struct intel_gt *gt = guc_to_gt(guc);
+	unsigned long flags;
+	bool disabled;
+	GEM_BUG_ON(!intel_gt_pm_is_awake(gt));
  	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id.id));
  	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
  	GEM_BUG_ON(context_enabled(ce));
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled)) {
+		__intel_gt_pm_get(gt);
+		set_context_destroyed(ce);
+		clr_context_registered(ce);
+	}
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
  	deregister_context(ce, ce->guc_id.id, true);
  }
@@ -1873,78 +1904,88 @@ static void __guc_context_destroy(struct intel_context *ce)
  	}
  }
+static void guc_flush_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	GEM_BUG_ON(!submission_disabled(guc) &&
+		   guc_submission_initialized(guc));
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		__release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void deregister_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+		guc_lrc_desc_unpin(ce);
+		spin_lock_irqsave(&guc->submission_state.lock, flags);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void destroyed_worker_func(struct work_struct *w)
+{
+	struct intel_guc *guc = container_of(w, struct intel_guc,
+					     submission_state.destroyed_worker);
+	struct intel_gt *gt = guc_to_gt(guc);
+	int tmp;
+
+	with_intel_gt_pm(gt, tmp)
+		deregister_destroyed_contexts(guc);
+}
+
  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->uncore->rpm;
  	struct intel_guc *guc = ce_to_guc(ce);
-	intel_wakeref_t wakeref;
  	unsigned long flags;
-	bool disabled;
+	bool destroy;
  	/*
  	 * 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 or the GuC is in the middle of a reset.
  	 */
-	if (context_guc_id_invalid(ce)) {
-		__guc_context_destroy(ce);
-		return;
-	} else if (submission_disabled(guc) ||
-		   !lrc_desc_registered(guc, ce->guc_id.id)) {
-		release_guc_id(guc, ce);
-		__guc_context_destroy(ce);
-		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_id available to
-	 * steal 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_id.
-	 */
-	spin_lock_irqsave(&guc->contexts_lock, flags);
-	if (context_guc_id_invalid(ce)) {
-		spin_unlock_irqrestore(&guc->contexts_lock, flags);
-		__guc_context_destroy(ce);
-		return;
-	}
-
-	if (!list_empty(&ce->guc_id.link))
-		list_del_init(&ce->guc_id.link);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
-
-	/* Seal race with Reset */
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	disabled = submission_disabled(guc);
-	if (likely(!disabled)) {
-		set_context_destroyed(ce);
-		clr_context_registered(ce);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	destroy = submission_disabled(guc) || context_guc_id_invalid(ce) ||
+		!lrc_desc_registered(guc, ce->guc_id.id);
+	if (likely(!destroy)) {
+		if (!list_empty(&ce->guc_id.link))
+			list_del_init(&ce->guc_id.link);
+		list_add_tail(&ce->destroyed_link,
+			      &guc->submission_state.destroyed_contexts);
+	} else {
+		__release_guc_id(guc, ce);
  	}
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-	if (unlikely(disabled)) {
-		release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+	if (unlikely(destroy)) {
  		__guc_context_destroy(ce);
  		return;
  	}
  	/*
-	 * We defer GuC context deregistration until the context is destroyed
-	 * in order to save on CTBs. With this optimization ideally we only need
-	 * 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 runtime wakeref is not 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.
+	 * We use a worker to issue the H2G to deregister the context as we can
+	 * take the GT PM for the first time which isn't allowed from an atomic
+	 * context.
  	 */
-	with_intel_runtime_pm(runtime_pm, wakeref)
-		guc_lrc_desc_unpin(ce);
+	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
  }
  static int guc_context_alloc(struct intel_context *ce)
@@ -2703,8 +2744,8 @@ static bool __guc_submission_selected(struct intel_guc *guc)
  void intel_guc_submission_init_early(struct intel_guc *guc)
  {
-	guc->max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
-	guc->num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
  	guc->submission_supported = __guc_submission_supported(guc);
  	guc->submission_selected = __guc_submission_selected(guc);
  }
@@ -2714,10 +2755,10 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  {
  	struct intel_context *ce;
-	if (unlikely(desc_idx >= guc->max_guc_ids)) {
+	if (unlikely(desc_idx >= guc->submission_state.max_guc_ids)) {
  		drm_err(&guc_to_gt(guc)->i915->drm,
  			"Invalid desc_idx %u, max %u",
-			desc_idx, guc->max_guc_ids);
+			desc_idx, guc->submission_state.max_guc_ids);
  		return NULL;
  	}
@@ -2771,6 +2812,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		intel_context_put(ce);
  	} else if (context_destroyed(ce)) {
  		/* Context has been destroyed */
+		intel_gt_pm_put_async(guc_to_gt(guc));
  		release_guc_id(guc, ce);
  		__guc_context_destroy(ce);
  	}
@@ -3065,8 +3107,10 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
  	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
  		   atomic_read(&guc->outstanding_submission_g2h));
-	drm_printf(p, "GuC Number GuC IDs: %u\n", guc->num_guc_ids);
-	drm_printf(p, "GuC Max GuC IDs: %u\n", guc->max_guc_ids);
+	drm_printf(p, "GuC Number GuC IDs: %u\n",
+		   guc->submission_state.num_guc_ids);
+	drm_printf(p, "GuC Max GuC IDs: %u\n",
+		   guc->submission_state.max_guc_ids);
  	drm_printf(p, "GuC tasklet count: %u\n\n",
  		   atomic_read(&sched_engine->tasklet.count));
#11 lore raw

Re: [Intel-gfx] [PATCH 04/27] drm/i915/guc: Take GT PM ref when deregistering context

In reply to Matthew Brost
Quoted 469 lines · Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a deregister context H2G is in flight.

FIXME: Move locking / structure changes into different patch

---
  drivers/gpu/drm/i915/gt/intel_context.c       |   2 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  13 +-
  drivers/gpu/drm/i915/gt/intel_engine_pm.h     |   5 +
  drivers/gpu/drm/i915/gt/intel_gt_pm.h         |  13 ++
  .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  |   1 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  46 ++--
  .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    |  13 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 212 +++++++++++-------
  8 files changed, 199 insertions(+), 106 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +2 -0
index adfe49b53b1b..c8595da64ad8 100644
@@ -399,6 +399,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	ce->guc_id.id = GUC_INVALID_LRC_ID;
  	INIT_LIST_HEAD(&ce->guc_id.link);
 
+	INIT_LIST_HEAD(&ce->destroyed_link);
+
  	/*
  	 * Initialize fence to be complete as this is expected to be complete
  	 * unless there is a pending schedule disable outstanding.
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -3
index 80bbdc7810f6..fd338a30617e 100644
@@ -190,22 +190,29 @@ struct intel_context {
  		/**
  		 * @id: unique handle which is used to communicate information
  		 * with the GuC about this context, protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
  		 */
  		u16 id;
  		/**
  		 * @ref: the number of references to the guc_id, when
  		 * transitioning in and out of zero protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
  		 */
  		atomic_t ref;
  		/**
  		 * @link: in guc->guc_id_list when the guc_id has no refs but is
-		 * still valid, protected by guc->contexts_lock
+		 * still valid, protected by guc->submission_state.lock
  		 */
  		struct list_head link;
  	} guc_id;
 
+	/**
+	 * @destroyed_link: link in guc->submission_state.destroyed_contexts, in
+	 * list when context is pending to be destroyed (deregistered with the
+	 * GuC), protected by guc->submission_state.lock
+	 */
+	struct list_head destroyed_link;
+
  #ifdef CONFIG_DRM_I915_SELFTEST
  	/**
  	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
drivers/gpu/drm/i915/gt/intel_engine_pm.h +5 -0
index 70ea46d6cfb0..17a5028ea177 100644
@@ -16,6 +16,11 @@ intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
  	return intel_wakeref_is_active(&engine->wakeref);
  }
 
+static inline void __intel_engine_pm_get(struct intel_engine_cs *engine)
+{
+	__intel_wakeref_get(&engine->wakeref);
+}
+
  static inline void intel_engine_pm_get(struct intel_engine_cs *engine)
  {
  	intel_wakeref_get(&engine->wakeref);
drivers/gpu/drm/i915/gt/intel_gt_pm.h +13 -0
index d0588d8aaa44..a17bf0d4592b 100644
@@ -41,6 +41,19 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
  	intel_wakeref_put_async(&gt->wakeref);
  }
 
+#define with_intel_gt_pm(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_async(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake_async(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+
  static inline int intel_gt_pm_wait_for_idle(struct intel_gt *gt)
  {
  	return intel_wakeref_wait_for_idle(&gt->wakeref);
drivers/gpu/drm/i915/gt/uc/abi/guc_actions_abi.h +1 -0
index 8ff582222aff..ba10bd374cee 100644
@@ -142,6 +142,7 @@ enum intel_guc_action {
  	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_REGISTER_CONTEXT_MULTI_LRC = 0x4601,
  	INTEL_GUC_ACTION_RESET_CLIENT = 0x5507,
  	INTEL_GUC_ACTION_LIMIT
  };
drivers/gpu/drm/i915/gt/uc/intel_guc.h +31 -15
index 6fd2719d1b75..7358883f1540 100644
@@ -53,21 +53,37 @@ struct intel_guc {
  		void (*disable)(struct intel_guc *guc);
  	} interrupts;
 
-	/**
-	 * @contexts_lock: protects guc_ids, guc_id_list, ce->guc_id.id, and
-	 * ce->guc_id.ref when transitioning in and out of zero
-	 */
-	spinlock_t contexts_lock;
-	/** @guc_ids: used to allocate new guc_ids */
-	struct ida guc_ids;
-	/** @num_guc_ids: number of guc_ids that can be used */
-	u32 num_guc_ids;
-	/** @max_guc_ids: max number of guc_ids that can be used */
-	u32 max_guc_ids;
-	/**
-	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
-	 */
-	struct list_head guc_id_list;
+	struct {
+		/**
+		 * @lock: protects everything in submission_state, ce->guc_id,
+		 * and ce->destroyed_link
+		 */
+		spinlock_t lock;
+		/**
+		 * @guc_ids: used to allocate new guc_ids
+		 */
+		struct ida guc_ids;
+		/** @num_guc_ids: number of guc_ids that can be used */
+		u32 num_guc_ids;
+		/** @max_guc_ids: max number of guc_ids that can be used */
+		u32 max_guc_ids;
+		/**
+		 * @guc_id_list: list of intel_context with valid guc_ids but no
+		 * refs
+		 */
+		struct list_head guc_id_list;
+		/**
+		 * @destroyed_contexts: list of contexts waiting to be destroyed
+		 * (deregistered with the GuC)
+		 */
+		struct list_head destroyed_contexts;
+		/**
+		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * need to take a GT PM reference and can't from destroy
+		 * function as it might be in an atomic context (no sleeping)
+		 */
+		struct work_struct destroyed_worker;
+	} submission_state;
 
  	bool submission_supported;
  	bool submission_selected;
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +9 -4
index b88d343ee432..27655460ee84 100644
@@ -78,7 +78,7 @@ static int guc_num_id_get(void *data, u64 *val)
  	if (!intel_guc_submission_is_used(guc))
  		return -ENODEV;
 
-	*val = guc->num_guc_ids;
+	*val = guc->submission_state.num_guc_ids;
 
  	return 0;
  }
@@ -86,16 +86,21 @@ static int guc_num_id_get(void *data, u64 *val)
  static int guc_num_id_set(void *data, u64 val)
  {
  	struct intel_guc *guc = data;
+	unsigned long flags;
 
  	if (!intel_guc_submission_is_used(guc))
  		return -ENODEV;
 
-	if (val > guc->max_guc_ids)
-		val = guc->max_guc_ids;
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+
+	if (val > guc->submission_state.max_guc_ids)
+		val = guc->submission_state.max_guc_ids;
  	else if (val < 256)
  		val = 256;
 
-	guc->num_guc_ids = val;
+	guc->submission_state.num_guc_ids = val;
+
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
 
  	return 0;
  }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +75 -24
index 68742b612692..f835e06e5f9f 100644
@@ -86,9 +86,9 @@
   * submitting at a time. Currently only 1 sched_engine used for all of GuC
   * submission but that could change in the future.
   *
- * guc->contexts_lock
- * Protects guc_id allocation. Global lock i.e. Only 1 context that uses GuC
- * submission can hold this at a time.
+ * guc->submission_state.lock
+ * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
+ * list.
   *
   * ce->guc_state.lock
   * Protects everything under ce->guc_state. Ensures that a context is in the
@@ -100,7 +100,7 @@
   *
   * Lock ordering rules:
   * sched_engine->lock -> ce->guc_state.lock
- * guc->contexts_lock -> ce->guc_state.lock
+ * guc->submission_state.lock -> ce->guc_state.lock
   *
   * Reset races:
   * When a GPU full reset is triggered it is assumed that some G2H responses to
@@ -344,7 +344,7 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
  {
  	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
 
-	GEM_BUG_ON(index >= guc->max_guc_ids);
+	GEM_BUG_ON(index >= guc->submission_state.max_guc_ids);
 
  	return &base[index];
  }
@@ -353,7 +353,7 @@ static 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_guc_ids);
+	GEM_BUG_ON(id >= guc->submission_state.max_guc_ids);
 
  	return ce;
  }
@@ -363,7 +363,8 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
  	u32 size;
  	int ret;
 
-	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) * guc->max_guc_ids);
+	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
+			  guc->submission_state.max_guc_ids);
  	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
  					     (void **)&guc->lrc_desc_pool_vaddr);
  	if (ret)
@@ -711,6 +712,7 @@ static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
  			if (deregister)
  				guc_signal_context_fence(ce);
  			if (destroyed) {
+				intel_gt_pm_put_async(guc_to_gt(guc));
  				release_guc_id(guc, ce);
  				__guc_context_destroy(ce);
  			}
@@ -789,6 +791,8 @@ static void guc_flush_submissions(struct intel_guc *guc)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
 
+static void guc_flush_destroyed_contexts(struct intel_guc *guc);
+
  void intel_guc_submission_reset_prepare(struct intel_guc *guc)
  {
  	if (unlikely(!guc_submission_initialized(guc))) {
@@ -805,6 +809,7 @@ void intel_guc_submission_reset_prepare(struct intel_guc *guc)
  	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
 
  	flush_work(&guc->ct.requests.worker);
+	guc_flush_destroyed_contexts(guc);
 
  	scrub_guc_desc_for_outstanding_g2h(guc);
  }
@@ -1102,6 +1107,8 @@ void intel_guc_submission_reset_finish(struct intel_guc *guc)
  	intel_gt_unpark_heartbeats(guc_to_gt(guc));
  }
 
+static void destroyed_worker_func(struct work_struct *w);
+
  /*
   * Set up the memory resources to be shared with the GuC (via the GGTT)
   * at firmware loading time.
@@ -1124,9 +1131,11 @@ 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);
+	spin_lock_init(&guc->submission_state.lock);
+	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
+	ida_init(&guc->submission_state.guc_ids);
+	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
+	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
 
  	return 0;
  }
@@ -1137,6 +1146,7 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  		return;
 
  	guc_lrc_desc_pool_destroy(guc);
+	guc_flush_destroyed_contexts(guc);
  	i915_sched_engine_put(guc->sched_engine);
  }
 
@@ -1191,15 +1201,16 @@ static void guc_submit_request(struct i915_request *rq)
 
  static int new_guc_id(struct intel_guc *guc)
  {
-	return ida_simple_get(&guc->guc_ids, 0,
-			      guc->num_guc_ids, GFP_KERNEL |
+	return ida_simple_get(&guc->submission_state.guc_ids, 0,
+			      guc->submission_state.num_guc_ids, 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.id);
+		ida_simple_remove(&guc->submission_state.guc_ids,
+				  ce->guc_id.id);
  		reset_lrc_desc(guc, ce->guc_id.id);
  		set_context_guc_id_invalid(ce);
  	}
@@ -1211,9 +1222,9 @@ static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
  	unsigned long flags;
 
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
  	__release_guc_id(guc, ce);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
 
  static int steal_guc_id(struct intel_guc *guc)
@@ -1221,10 +1232,10 @@ static int steal_guc_id(struct intel_guc *guc)
  	struct intel_context *ce;
  	int guc_id;
 
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
 
-	if (!list_empty(&guc->guc_id_list)) {
-		ce = list_first_entry(&guc->guc_id_list,
+	if (!list_empty(&guc->submission_state.guc_id_list)) {
+		ce = list_first_entry(&guc->submission_state.guc_id_list,
  				      struct intel_context,
  				      guc_id.link);
 
@@ -1249,7 +1260,7 @@ static int assign_guc_id(struct intel_guc *guc, u16 *out)
  {
  	int ret;
 
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
 
  	ret = new_guc_id(guc);
  	if (unlikely(ret < 0)) {
@@ -1271,7 +1282,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
 
  try_again:
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
 
  	might_lock(&ce->guc_state.lock);
 
@@ -1286,7 +1297,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	atomic_inc(&ce->guc_id.ref);
 
  out_unlock:
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
 
  	/*
  	 * -EAGAIN indicates no guc_id are available, let's retire any
@@ -1322,11 +1333,12 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	if (unlikely(context_guc_id_invalid(ce)))
  		return;
 
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.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);
+		list_add_tail(&ce->guc_id.link,
+			      &guc->submission_state.guc_id_list);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
 
  static int __guc_action_register_context(struct intel_guc *guc,
@@ -1841,11 +1853,30 @@ static void guc_context_sched_disable(struct intel_context *ce)
  static void guc_lrc_desc_unpin(struct intel_context *ce)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
+	struct intel_gt *gt = guc_to_gt(guc);
+	unsigned long flags;
+	bool disabled;
 
+	GEM_BUG_ON(!intel_gt_pm_is_awake(gt));
  	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id.id));
  	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
  	GEM_BUG_ON(context_enabled(ce));
 
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled)) {
+		__intel_gt_pm_get(gt);
+		set_context_destroyed(ce);
+		clr_context_registered(ce);
+	}
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
  	deregister_context(ce, ce->guc_id.id, true);
  }
 
@@ -1873,78 +1904,88 @@ static void __guc_context_destroy(struct intel_context *ce)
  	}
  }
 
+static void guc_flush_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	GEM_BUG_ON(!submission_disabled(guc) &&
+		   guc_submission_initialized(guc));
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		__release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void deregister_destroyed_contexts(struct intel_guc *guc)

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

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		spin_unlock_irqrestore(&guc->submission_state.lock, flags);

Lock drop and re-acquire is not safe - list_for_each_entry_safe only deal with the list_del_init above. It does not help if someone else modifies the list in the window while the lock is not held, in which case things will go BOOM!

Not sure in what differnt ways you use this list, but perhaps you could employ a lockless single linked list for this. In which case you could atomically unlink all elements. Or just change this to not do lock dropping, if you can safely use the same list_head to put on a local list or something.

Regards,

Tvrtko

Quoted 155 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +43 -60
+		guc_lrc_desc_unpin(ce);
+		spin_lock_irqsave(&guc->submission_state.lock, flags);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void destroyed_worker_func(struct work_struct *w)
+{
+	struct intel_guc *guc = container_of(w, struct intel_guc,
+					     submission_state.destroyed_worker);
+	struct intel_gt *gt = guc_to_gt(guc);
+	int tmp;
+
+	with_intel_gt_pm(gt, tmp)
+		deregister_destroyed_contexts(guc);
+}
+
  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->uncore->rpm;
  	struct intel_guc *guc = ce_to_guc(ce);
-	intel_wakeref_t wakeref;
  	unsigned long flags;
-	bool disabled;
+	bool destroy;
 
  	/*
  	 * 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 or the GuC is in the middle of a reset.
  	 */
-	if (context_guc_id_invalid(ce)) {
-		__guc_context_destroy(ce);
-		return;
-	} else if (submission_disabled(guc) ||
-		   !lrc_desc_registered(guc, ce->guc_id.id)) {
-		release_guc_id(guc, ce);
-		__guc_context_destroy(ce);
-		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_id available to
-	 * steal 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_id.
-	 */
-	spin_lock_irqsave(&guc->contexts_lock, flags);
-	if (context_guc_id_invalid(ce)) {
-		spin_unlock_irqrestore(&guc->contexts_lock, flags);
-		__guc_context_destroy(ce);
-		return;
-	}
-
-	if (!list_empty(&ce->guc_id.link))
-		list_del_init(&ce->guc_id.link);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
-
-	/* Seal race with Reset */
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	disabled = submission_disabled(guc);
-	if (likely(!disabled)) {
-		set_context_destroyed(ce);
-		clr_context_registered(ce);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	destroy = submission_disabled(guc) || context_guc_id_invalid(ce) ||
+		!lrc_desc_registered(guc, ce->guc_id.id);
+	if (likely(!destroy)) {
+		if (!list_empty(&ce->guc_id.link))
+			list_del_init(&ce->guc_id.link);
+		list_add_tail(&ce->destroyed_link,
+			      &guc->submission_state.destroyed_contexts);
+	} else {
+		__release_guc_id(guc, ce);
  	}
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-	if (unlikely(disabled)) {
-		release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+	if (unlikely(destroy)) {
  		__guc_context_destroy(ce);
  		return;
  	}
 
  	/*
-	 * We defer GuC context deregistration until the context is destroyed
-	 * in order to save on CTBs. With this optimization ideally we only need
-	 * 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 runtime wakeref is not 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.
+	 * We use a worker to issue the H2G to deregister the context as we can
+	 * take the GT PM for the first time which isn't allowed from an atomic
+	 * context.
  	 */
-	with_intel_runtime_pm(runtime_pm, wakeref)
-		guc_lrc_desc_unpin(ce);
+	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
  }
 
  static int guc_context_alloc(struct intel_context *ce)
@@ -2703,8 +2744,8 @@ static bool __guc_submission_selected(struct intel_guc *guc)
 
  void intel_guc_submission_init_early(struct intel_guc *guc)
  {
-	guc->max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
-	guc->num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
  	guc->submission_supported = __guc_submission_supported(guc);
  	guc->submission_selected = __guc_submission_selected(guc);
  }
@@ -2714,10 +2755,10 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  {
  	struct intel_context *ce;
 
-	if (unlikely(desc_idx >= guc->max_guc_ids)) {
+	if (unlikely(desc_idx >= guc->submission_state.max_guc_ids)) {
  		drm_err(&guc_to_gt(guc)->i915->drm,
  			"Invalid desc_idx %u, max %u",
-			desc_idx, guc->max_guc_ids);
+			desc_idx, guc->submission_state.max_guc_ids);
  		return NULL;
  	}
 
@@ -2771,6 +2812,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		intel_context_put(ce);
  	} else if (context_destroyed(ce)) {
  		/* Context has been destroyed */
+		intel_gt_pm_put_async(guc_to_gt(guc));
  		release_guc_id(guc, ce);
  		__guc_context_destroy(ce);
  	}
@@ -3065,8 +3107,10 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
 
  	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
  		   atomic_read(&guc->outstanding_submission_g2h));
-	drm_printf(p, "GuC Number GuC IDs: %u\n", guc->num_guc_ids);
-	drm_printf(p, "GuC Max GuC IDs: %u\n", guc->max_guc_ids);
+	drm_printf(p, "GuC Number GuC IDs: %u\n",
+		   guc->submission_state.num_guc_ids);
+	drm_printf(p, "GuC Max GuC IDs: %u\n",
+		   guc->submission_state.max_guc_ids);
  	drm_printf(p, "GuC tasklet count: %u\n\n",
  		   atomic_read(&sched_engine->tasklet.count));
 
#12 lore raw

Re: [Intel-gfx] [PATCH 04/27] drm/i915/guc: Take GT PM ref when deregistering context

Quoted 432 lines · Tvrtko Ursulin
Quoted 430 lines · Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a deregister context H2G is in flight.

FIXME: Move locking / structure changes into different patch

---
  drivers/gpu/drm/i915/gt/intel_context.c       |   2 +
  drivers/gpu/drm/i915/gt/intel_context_types.h |  13 +-
  drivers/gpu/drm/i915/gt/intel_engine_pm.h     |   5 +
  drivers/gpu/drm/i915/gt/intel_gt_pm.h         |  13 ++
  .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  |   1 +
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  46 ++--
  .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    |  13 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 212 +++++++++++-------
  8 files changed, 199 insertions(+), 106 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +2 -0
index adfe49b53b1b..c8595da64ad8 100644
@@ -399,6 +399,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	ce->guc_id.id = GUC_INVALID_LRC_ID;
  	INIT_LIST_HEAD(&ce->guc_id.link);
+	INIT_LIST_HEAD(&ce->destroyed_link);
+
  	/*
  	 * Initialize fence to be complete as this is expected to be complete
  	 * unless there is a pending schedule disable outstanding.
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -3
index 80bbdc7810f6..fd338a30617e 100644
@@ -190,22 +190,29 @@ struct intel_context {
  		/**
  		 * @id: unique handle which is used to communicate information
  		 * with the GuC about this context, protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
  		 */
  		u16 id;
  		/**
  		 * @ref: the number of references to the guc_id, when
  		 * transitioning in and out of zero protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
  		 */
  		atomic_t ref;
  		/**
  		 * @link: in guc->guc_id_list when the guc_id has no refs but is
-		 * still valid, protected by guc->contexts_lock
+		 * still valid, protected by guc->submission_state.lock
  		 */
  		struct list_head link;
  	} guc_id;
+	/**
+	 * @destroyed_link: link in guc->submission_state.destroyed_contexts, in
+	 * list when context is pending to be destroyed (deregistered with the
+	 * GuC), protected by guc->submission_state.lock
+	 */
+	struct list_head destroyed_link;
+
  #ifdef CONFIG_DRM_I915_SELFTEST
  	/**
  	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
drivers/gpu/drm/i915/gt/intel_engine_pm.h +5 -0
index 70ea46d6cfb0..17a5028ea177 100644
@@ -16,6 +16,11 @@ intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
  	return intel_wakeref_is_active(&engine->wakeref);
  }
+static inline void __intel_engine_pm_get(struct intel_engine_cs *engine)
+{
+	__intel_wakeref_get(&engine->wakeref);
+}
+
  static inline void intel_engine_pm_get(struct intel_engine_cs *engine)
  {
  	intel_wakeref_get(&engine->wakeref);
drivers/gpu/drm/i915/gt/intel_gt_pm.h +13 -0
index d0588d8aaa44..a17bf0d4592b 100644
@@ -41,6 +41,19 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
  	intel_wakeref_put_async(&gt->wakeref);
  }
+#define with_intel_gt_pm(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_async(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake_async(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+
  static inline int intel_gt_pm_wait_for_idle(struct intel_gt *gt)
  {
  	return intel_wakeref_wait_for_idle(&gt->wakeref);
drivers/gpu/drm/i915/gt/uc/abi/guc_actions_abi.h +1 -0
index 8ff582222aff..ba10bd374cee 100644
@@ -142,6 +142,7 @@ enum intel_guc_action {
  	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_REGISTER_CONTEXT_MULTI_LRC = 0x4601,
  	INTEL_GUC_ACTION_RESET_CLIENT = 0x5507,
  	INTEL_GUC_ACTION_LIMIT
  };
drivers/gpu/drm/i915/gt/uc/intel_guc.h +31 -15
index 6fd2719d1b75..7358883f1540 100644
@@ -53,21 +53,37 @@ struct intel_guc {
  		void (*disable)(struct intel_guc *guc);
  	} interrupts;
-	/**
-	 * @contexts_lock: protects guc_ids, guc_id_list, ce->guc_id.id, and
-	 * ce->guc_id.ref when transitioning in and out of zero
-	 */
-	spinlock_t contexts_lock;
-	/** @guc_ids: used to allocate new guc_ids */
-	struct ida guc_ids;
-	/** @num_guc_ids: number of guc_ids that can be used */
-	u32 num_guc_ids;
-	/** @max_guc_ids: max number of guc_ids that can be used */
-	u32 max_guc_ids;
-	/**
-	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
-	 */
-	struct list_head guc_id_list;
+	struct {
+		/**
+		 * @lock: protects everything in submission_state, ce->guc_id,
+		 * and ce->destroyed_link
+		 */
+		spinlock_t lock;
+		/**
+		 * @guc_ids: used to allocate new guc_ids
+		 */
+		struct ida guc_ids;
+		/** @num_guc_ids: number of guc_ids that can be used */
+		u32 num_guc_ids;
+		/** @max_guc_ids: max number of guc_ids that can be used */
+		u32 max_guc_ids;
+		/**
+		 * @guc_id_list: list of intel_context with valid guc_ids but no
+		 * refs
+		 */
+		struct list_head guc_id_list;
+		/**
+		 * @destroyed_contexts: list of contexts waiting to be destroyed
+		 * (deregistered with the GuC)
+		 */
+		struct list_head destroyed_contexts;
+		/**
+		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * need to take a GT PM reference and can't from destroy
+		 * function as it might be in an atomic context (no sleeping)
+		 */
+		struct work_struct destroyed_worker;
+	} submission_state;
  	bool submission_supported;
  	bool submission_selected;
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +9 -4
index b88d343ee432..27655460ee84 100644
@@ -78,7 +78,7 @@ static int guc_num_id_get(void *data, u64 *val)
  	if (!intel_guc_submission_is_used(guc))
  		return -ENODEV;
-	*val = guc->num_guc_ids;
+	*val = guc->submission_state.num_guc_ids;
  	return 0;
  }
@@ -86,16 +86,21 @@ static int guc_num_id_get(void *data, u64 *val)
  static int guc_num_id_set(void *data, u64 val)
  {
  	struct intel_guc *guc = data;
+	unsigned long flags;
  	if (!intel_guc_submission_is_used(guc))
  		return -ENODEV;
-	if (val > guc->max_guc_ids)
-		val = guc->max_guc_ids;
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+
+	if (val > guc->submission_state.max_guc_ids)
+		val = guc->submission_state.max_guc_ids;
  	else if (val < 256)
  		val = 256;
-	guc->num_guc_ids = val;
+	guc->submission_state.num_guc_ids = val;
+
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  	return 0;
  }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +82 -24
index 68742b612692..f835e06e5f9f 100644
@@ -86,9 +86,9 @@
   * submitting at a time. Currently only 1 sched_engine used for all of GuC
   * submission but that could change in the future.
   *
- * guc->contexts_lock
- * Protects guc_id allocation. Global lock i.e. Only 1 context that uses GuC
- * submission can hold this at a time.
+ * guc->submission_state.lock
+ * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
+ * list.
   *
   * ce->guc_state.lock
   * Protects everything under ce->guc_state. Ensures that a context is in the
@@ -100,7 +100,7 @@
   *
   * Lock ordering rules:
   * sched_engine->lock -> ce->guc_state.lock
- * guc->contexts_lock -> ce->guc_state.lock
+ * guc->submission_state.lock -> ce->guc_state.lock
   *
   * Reset races:
   * When a GPU full reset is triggered it is assumed that some G2H responses to
@@ -344,7 +344,7 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
  {
  	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
-	GEM_BUG_ON(index >= guc->max_guc_ids);
+	GEM_BUG_ON(index >= guc->submission_state.max_guc_ids);
  	return &base[index];
  }
@@ -353,7 +353,7 @@ static 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_guc_ids);
+	GEM_BUG_ON(id >= guc->submission_state.max_guc_ids);
  	return ce;
  }
@@ -363,7 +363,8 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
  	u32 size;
  	int ret;
-	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) * guc->max_guc_ids);
+	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
+			  guc->submission_state.max_guc_ids);
  	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
  					     (void **)&guc->lrc_desc_pool_vaddr);
  	if (ret)
@@ -711,6 +712,7 @@ static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
  			if (deregister)
  				guc_signal_context_fence(ce);
  			if (destroyed) {
+				intel_gt_pm_put_async(guc_to_gt(guc));
  				release_guc_id(guc, ce);
  				__guc_context_destroy(ce);
  			}
@@ -789,6 +791,8 @@ static void guc_flush_submissions(struct intel_guc *guc)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
+static void guc_flush_destroyed_contexts(struct intel_guc *guc);
+
  void intel_guc_submission_reset_prepare(struct intel_guc *guc)
  {
  	if (unlikely(!guc_submission_initialized(guc))) {
@@ -805,6 +809,7 @@ void intel_guc_submission_reset_prepare(struct intel_guc *guc)
  	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
  	flush_work(&guc->ct.requests.worker);
+	guc_flush_destroyed_contexts(guc);
  	scrub_guc_desc_for_outstanding_g2h(guc);
  }
@@ -1102,6 +1107,8 @@ void intel_guc_submission_reset_finish(struct intel_guc *guc)
  	intel_gt_unpark_heartbeats(guc_to_gt(guc));
  }
+static void destroyed_worker_func(struct work_struct *w);
+
  /*
   * Set up the memory resources to be shared with the GuC (via the GGTT)
   * at firmware loading time.
@@ -1124,9 +1131,11 @@ 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);
+	spin_lock_init(&guc->submission_state.lock);
+	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
+	ida_init(&guc->submission_state.guc_ids);
+	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
+	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
  	return 0;
  }
@@ -1137,6 +1146,7 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  		return;
  	guc_lrc_desc_pool_destroy(guc);
+	guc_flush_destroyed_contexts(guc);
  	i915_sched_engine_put(guc->sched_engine);
  }
@@ -1191,15 +1201,16 @@ static void guc_submit_request(struct i915_request *rq)
  static int new_guc_id(struct intel_guc *guc)
  {
-	return ida_simple_get(&guc->guc_ids, 0,
-			      guc->num_guc_ids, GFP_KERNEL |
+	return ida_simple_get(&guc->submission_state.guc_ids, 0,
+			      guc->submission_state.num_guc_ids, 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.id);
+		ida_simple_remove(&guc->submission_state.guc_ids,
+				  ce->guc_id.id);
  		reset_lrc_desc(guc, ce->guc_id.id);
  		set_context_guc_id_invalid(ce);
  	}
@@ -1211,9 +1222,9 @@ static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
  	unsigned long flags;
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
  	__release_guc_id(guc, ce);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
  static int steal_guc_id(struct intel_guc *guc)
@@ -1221,10 +1232,10 @@ static int steal_guc_id(struct intel_guc *guc)
  	struct intel_context *ce;
  	int guc_id;
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
-	if (!list_empty(&guc->guc_id_list)) {
-		ce = list_first_entry(&guc->guc_id_list,
+	if (!list_empty(&guc->submission_state.guc_id_list)) {
+		ce = list_first_entry(&guc->submission_state.guc_id_list,
  				      struct intel_context,
  				      guc_id.link);
@@ -1249,7 +1260,7 @@ static int assign_guc_id(struct intel_guc *guc, u16 *out)
  {
  	int ret;
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
  	ret = new_guc_id(guc);
  	if (unlikely(ret < 0)) {
@@ -1271,7 +1282,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
  try_again:
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
  	might_lock(&ce->guc_state.lock);
@@ -1286,7 +1297,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	atomic_inc(&ce->guc_id.ref);
  out_unlock:
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  	/*
  	 * -EAGAIN indicates no guc_id are available, let's retire any
@@ -1322,11 +1333,12 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	if (unlikely(context_guc_id_invalid(ce)))
  		return;
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.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);
+		list_add_tail(&ce->guc_id.link,
+			      &guc->submission_state.guc_id_list);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
  static int __guc_action_register_context(struct intel_guc *guc,
@@ -1841,11 +1853,30 @@ static void guc_context_sched_disable(struct intel_context *ce)
  static void guc_lrc_desc_unpin(struct intel_context *ce)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
+	struct intel_gt *gt = guc_to_gt(guc);
+	unsigned long flags;
+	bool disabled;
+	GEM_BUG_ON(!intel_gt_pm_is_awake(gt));
  	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id.id));
  	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
  	GEM_BUG_ON(context_enabled(ce));
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled)) {
+		__intel_gt_pm_get(gt);
+		set_context_destroyed(ce);
+		clr_context_registered(ce);
+	}
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
  	deregister_context(ce, ce->guc_id.id, true);
  }
@@ -1873,78 +1904,88 @@ static void __guc_context_destroy(struct intel_context *ce)
  	}
  }
+static void guc_flush_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	GEM_BUG_ON(!submission_disabled(guc) &&
+		   guc_submission_initialized(guc));
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		__release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void deregister_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
Quoted 9 lines

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

Quoted 3 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		spin_unlock_irqrestore(&guc->submission_state.lock, flags);

Lock drop and re-acquire is not safe - list_for_each_entry_safe only deal with the list_del_init above. It does not help if someone else modifies the list in the window while the lock is not held, in which case things will go BOOM!

Good catch, but is this true? We always add via list_add_tail which if I'm reading the list_for_each_entry_safe macro correctly I think is safe to do as 'n = list_next_entry(n, member)' will just evaluate to the new tail on each iteration, right?

Reference to macro below:

#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_first_entry(head, typeof(*pos), member),	\
		n = list_next_entry(pos, member);			\
	     &pos->member != (head); 					\
	     pos = n, n = list_next_entry(n, member))

Regardless probably safer to just not drop the lock.

Quoted 6 lines

Not sure in what differnt ways you use this list, but perhaps you could employ a lockless single linked list for this. In which case you could atomically unlink all elements. Or just change this to not do lock dropping, if you can safely use the same list_head to put on a local list or something.

I think both options work - not sure why I drop the lock here. I'll play around with this and fix in the next rev.

Matt

Quoted 151 lines

Regards,

Tvrtko

Quoted 147 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +43 -60
+		guc_lrc_desc_unpin(ce);
+		spin_lock_irqsave(&guc->submission_state.lock, flags);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void destroyed_worker_func(struct work_struct *w)
+{
+	struct intel_guc *guc = container_of(w, struct intel_guc,
+					     submission_state.destroyed_worker);
+	struct intel_gt *gt = guc_to_gt(guc);
+	int tmp;
+
+	with_intel_gt_pm(gt, tmp)
+		deregister_destroyed_contexts(guc);
+}
+
  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->uncore->rpm;
  	struct intel_guc *guc = ce_to_guc(ce);
-	intel_wakeref_t wakeref;
  	unsigned long flags;
-	bool disabled;
+	bool destroy;
  	/*
  	 * 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 or the GuC is in the middle of a reset.
  	 */
-	if (context_guc_id_invalid(ce)) {
-		__guc_context_destroy(ce);
-		return;
-	} else if (submission_disabled(guc) ||
-		   !lrc_desc_registered(guc, ce->guc_id.id)) {
-		release_guc_id(guc, ce);
-		__guc_context_destroy(ce);
-		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_id available to
-	 * steal 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_id.
-	 */
-	spin_lock_irqsave(&guc->contexts_lock, flags);
-	if (context_guc_id_invalid(ce)) {
-		spin_unlock_irqrestore(&guc->contexts_lock, flags);
-		__guc_context_destroy(ce);
-		return;
-	}
-
-	if (!list_empty(&ce->guc_id.link))
-		list_del_init(&ce->guc_id.link);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
-
-	/* Seal race with Reset */
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	disabled = submission_disabled(guc);
-	if (likely(!disabled)) {
-		set_context_destroyed(ce);
-		clr_context_registered(ce);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	destroy = submission_disabled(guc) || context_guc_id_invalid(ce) ||
+		!lrc_desc_registered(guc, ce->guc_id.id);
+	if (likely(!destroy)) {
+		if (!list_empty(&ce->guc_id.link))
+			list_del_init(&ce->guc_id.link);
+		list_add_tail(&ce->destroyed_link,
+			      &guc->submission_state.destroyed_contexts);
+	} else {
+		__release_guc_id(guc, ce);
  	}
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-	if (unlikely(disabled)) {
-		release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+	if (unlikely(destroy)) {
  		__guc_context_destroy(ce);
  		return;
  	}
  	/*
-	 * We defer GuC context deregistration until the context is destroyed
-	 * in order to save on CTBs. With this optimization ideally we only need
-	 * 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 runtime wakeref is not 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.
+	 * We use a worker to issue the H2G to deregister the context as we can
+	 * take the GT PM for the first time which isn't allowed from an atomic
+	 * context.
  	 */
-	with_intel_runtime_pm(runtime_pm, wakeref)
-		guc_lrc_desc_unpin(ce);
+	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
  }
  static int guc_context_alloc(struct intel_context *ce)
@@ -2703,8 +2744,8 @@ static bool __guc_submission_selected(struct intel_guc *guc)
  void intel_guc_submission_init_early(struct intel_guc *guc)
  {
-	guc->max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
-	guc->num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
  	guc->submission_supported = __guc_submission_supported(guc);
  	guc->submission_selected = __guc_submission_selected(guc);
  }
@@ -2714,10 +2755,10 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  {
  	struct intel_context *ce;
-	if (unlikely(desc_idx >= guc->max_guc_ids)) {
+	if (unlikely(desc_idx >= guc->submission_state.max_guc_ids)) {
  		drm_err(&guc_to_gt(guc)->i915->drm,
  			"Invalid desc_idx %u, max %u",
-			desc_idx, guc->max_guc_ids);
+			desc_idx, guc->submission_state.max_guc_ids);
  		return NULL;
  	}
@@ -2771,6 +2812,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
  		intel_context_put(ce);
  	} else if (context_destroyed(ce)) {
  		/* Context has been destroyed */
+		intel_gt_pm_put_async(guc_to_gt(guc));
  		release_guc_id(guc, ce);
  		__guc_context_destroy(ce);
  	}
@@ -3065,8 +3107,10 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
  	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
  		   atomic_read(&guc->outstanding_submission_g2h));
-	drm_printf(p, "GuC Number GuC IDs: %u\n", guc->num_guc_ids);
-	drm_printf(p, "GuC Max GuC IDs: %u\n", guc->max_guc_ids);
+	drm_printf(p, "GuC Number GuC IDs: %u\n",
+		   guc->submission_state.num_guc_ids);
+	drm_printf(p, "GuC Max GuC IDs: %u\n",
+		   guc->submission_state.max_guc_ids);
  	drm_printf(p, "GuC tasklet count: %u\n\n",
  		   atomic_read(&sched_engine->tasklet.count));
 
#13 lore raw

Re: [Intel-gfx] [PATCH 04/27] drm/i915/guc: Take GT PM ref when deregistering context

Quoted 448 lines · Matthew Brost
Quoted 432 lines · Tvrtko Ursulin
Quoted 430 lines · Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a deregister context H2G is in flight.

FIXME: Move locking / structure changes into different patch

---
   drivers/gpu/drm/i915/gt/intel_context.c       |   2 +
   drivers/gpu/drm/i915/gt/intel_context_types.h |  13 +-
   drivers/gpu/drm/i915/gt/intel_engine_pm.h     |   5 +
   drivers/gpu/drm/i915/gt/intel_gt_pm.h         |  13 ++
   .../gpu/drm/i915/gt/uc/abi/guc_actions_abi.h  |   1 +
   drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  46 ++--
   .../gpu/drm/i915/gt/uc/intel_guc_debugfs.c    |  13 +-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 212 +++++++++++-------
   8 files changed, 199 insertions(+), 106 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +2 -0
index adfe49b53b1b..c8595da64ad8 100644
@@ -399,6 +399,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
   	ce->guc_id.id = GUC_INVALID_LRC_ID;
   	INIT_LIST_HEAD(&ce->guc_id.link);
+	INIT_LIST_HEAD(&ce->destroyed_link);
+
   	/*
   	 * Initialize fence to be complete as this is expected to be complete
   	 * unless there is a pending schedule disable outstanding.
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -3
index 80bbdc7810f6..fd338a30617e 100644
@@ -190,22 +190,29 @@ struct intel_context {
   		/**
   		 * @id: unique handle which is used to communicate information
   		 * with the GuC about this context, protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
   		 */
   		u16 id;
   		/**
   		 * @ref: the number of references to the guc_id, when
   		 * transitioning in and out of zero protected by
-		 * guc->contexts_lock
+		 * guc->submission_state.lock
   		 */
   		atomic_t ref;
   		/**
   		 * @link: in guc->guc_id_list when the guc_id has no refs but is
-		 * still valid, protected by guc->contexts_lock
+		 * still valid, protected by guc->submission_state.lock
   		 */
   		struct list_head link;
   	} guc_id;
+	/**
+	 * @destroyed_link: link in guc->submission_state.destroyed_contexts, in
+	 * list when context is pending to be destroyed (deregistered with the
+	 * GuC), protected by guc->submission_state.lock
+	 */
+	struct list_head destroyed_link;
+
   #ifdef CONFIG_DRM_I915_SELFTEST
   	/**
   	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
drivers/gpu/drm/i915/gt/intel_engine_pm.h +5 -0
index 70ea46d6cfb0..17a5028ea177 100644
@@ -16,6 +16,11 @@ intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
   	return intel_wakeref_is_active(&engine->wakeref);
   }
+static inline void __intel_engine_pm_get(struct intel_engine_cs *engine)
+{
+	__intel_wakeref_get(&engine->wakeref);
+}
+
   static inline void intel_engine_pm_get(struct intel_engine_cs *engine)
   {
   	intel_wakeref_get(&engine->wakeref);
drivers/gpu/drm/i915/gt/intel_gt_pm.h +13 -0
index d0588d8aaa44..a17bf0d4592b 100644
@@ -41,6 +41,19 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
   	intel_wakeref_put_async(&gt->wakeref);
   }
+#define with_intel_gt_pm(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_async(gt, tmp) \
+	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put(gt), tmp = 0)
+#define with_intel_gt_pm_if_awake_async(gt, tmp) \
+	for (tmp = intel_gt_pm_get_if_awake(gt); tmp; \
+	     intel_gt_pm_put_async(gt), tmp = 0)
+
   static inline int intel_gt_pm_wait_for_idle(struct intel_gt *gt)
   {
   	return intel_wakeref_wait_for_idle(&gt->wakeref);
drivers/gpu/drm/i915/gt/uc/abi/guc_actions_abi.h +1 -0
index 8ff582222aff..ba10bd374cee 100644
@@ -142,6 +142,7 @@ enum intel_guc_action {
   	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_REGISTER_CONTEXT_MULTI_LRC = 0x4601,
   	INTEL_GUC_ACTION_RESET_CLIENT = 0x5507,
   	INTEL_GUC_ACTION_LIMIT
   };
drivers/gpu/drm/i915/gt/uc/intel_guc.h +31 -15
index 6fd2719d1b75..7358883f1540 100644
@@ -53,21 +53,37 @@ struct intel_guc {
   		void (*disable)(struct intel_guc *guc);
   	} interrupts;
-	/**
-	 * @contexts_lock: protects guc_ids, guc_id_list, ce->guc_id.id, and
-	 * ce->guc_id.ref when transitioning in and out of zero
-	 */
-	spinlock_t contexts_lock;
-	/** @guc_ids: used to allocate new guc_ids */
-	struct ida guc_ids;
-	/** @num_guc_ids: number of guc_ids that can be used */
-	u32 num_guc_ids;
-	/** @max_guc_ids: max number of guc_ids that can be used */
-	u32 max_guc_ids;
-	/**
-	 * @guc_id_list: list of intel_context with valid guc_ids but no refs
-	 */
-	struct list_head guc_id_list;
+	struct {
+		/**
+		 * @lock: protects everything in submission_state, ce->guc_id,
+		 * and ce->destroyed_link
+		 */
+		spinlock_t lock;
+		/**
+		 * @guc_ids: used to allocate new guc_ids
+		 */
+		struct ida guc_ids;
+		/** @num_guc_ids: number of guc_ids that can be used */
+		u32 num_guc_ids;
+		/** @max_guc_ids: max number of guc_ids that can be used */
+		u32 max_guc_ids;
+		/**
+		 * @guc_id_list: list of intel_context with valid guc_ids but no
+		 * refs
+		 */
+		struct list_head guc_id_list;
+		/**
+		 * @destroyed_contexts: list of contexts waiting to be destroyed
+		 * (deregistered with the GuC)
+		 */
+		struct list_head destroyed_contexts;
+		/**
+		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * need to take a GT PM reference and can't from destroy
+		 * function as it might be in an atomic context (no sleeping)
+		 */
+		struct work_struct destroyed_worker;
+	} submission_state;
   	bool submission_supported;
   	bool submission_selected;
drivers/gpu/drm/i915/gt/uc/intel_guc_debugfs.c +9 -4
index b88d343ee432..27655460ee84 100644
@@ -78,7 +78,7 @@ static int guc_num_id_get(void *data, u64 *val)
   	if (!intel_guc_submission_is_used(guc))
   		return -ENODEV;
-	*val = guc->num_guc_ids;
+	*val = guc->submission_state.num_guc_ids;
   	return 0;
   }
@@ -86,16 +86,21 @@ static int guc_num_id_get(void *data, u64 *val)
   static int guc_num_id_set(void *data, u64 val)
   {
   	struct intel_guc *guc = data;
+	unsigned long flags;
   	if (!intel_guc_submission_is_used(guc))
   		return -ENODEV;
-	if (val > guc->max_guc_ids)
-		val = guc->max_guc_ids;
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+
+	if (val > guc->submission_state.max_guc_ids)
+		val = guc->submission_state.max_guc_ids;
   	else if (val < 256)
   		val = 256;
-	guc->num_guc_ids = val;
+	guc->submission_state.num_guc_ids = val;
+
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
   	return 0;
   }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +82 -24
index 68742b612692..f835e06e5f9f 100644
@@ -86,9 +86,9 @@
    * submitting at a time. Currently only 1 sched_engine used for all of GuC
    * submission but that could change in the future.
    *
- * guc->contexts_lock
- * Protects guc_id allocation. Global lock i.e. Only 1 context that uses GuC
- * submission can hold this at a time.
+ * guc->submission_state.lock
+ * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
+ * list.
    *
    * ce->guc_state.lock
    * Protects everything under ce->guc_state. Ensures that a context is in the
@@ -100,7 +100,7 @@
    *
    * Lock ordering rules:
    * sched_engine->lock -> ce->guc_state.lock
- * guc->contexts_lock -> ce->guc_state.lock
+ * guc->submission_state.lock -> ce->guc_state.lock
    *
    * Reset races:
    * When a GPU full reset is triggered it is assumed that some G2H responses to
@@ -344,7 +344,7 @@ static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
   {
   	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
-	GEM_BUG_ON(index >= guc->max_guc_ids);
+	GEM_BUG_ON(index >= guc->submission_state.max_guc_ids);
   	return &base[index];
   }
@@ -353,7 +353,7 @@ static 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_guc_ids);
+	GEM_BUG_ON(id >= guc->submission_state.max_guc_ids);
   	return ce;
   }
@@ -363,7 +363,8 @@ static int guc_lrc_desc_pool_create(struct intel_guc *guc)
   	u32 size;
   	int ret;
-	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) * guc->max_guc_ids);
+	size = PAGE_ALIGN(sizeof(struct guc_lrc_desc) *
+			  guc->submission_state.max_guc_ids);
   	ret = intel_guc_allocate_and_map_vma(guc, size, &guc->lrc_desc_pool,
   					     (void **)&guc->lrc_desc_pool_vaddr);
   	if (ret)
@@ -711,6 +712,7 @@ static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
   			if (deregister)
   				guc_signal_context_fence(ce);
   			if (destroyed) {
+				intel_gt_pm_put_async(guc_to_gt(guc));
   				release_guc_id(guc, ce);
   				__guc_context_destroy(ce);
   			}
@@ -789,6 +791,8 @@ static void guc_flush_submissions(struct intel_guc *guc)
   	spin_unlock_irqrestore(&sched_engine->lock, flags);
   }
+static void guc_flush_destroyed_contexts(struct intel_guc *guc);
+
   void intel_guc_submission_reset_prepare(struct intel_guc *guc)
   {
   	if (unlikely(!guc_submission_initialized(guc))) {
@@ -805,6 +809,7 @@ void intel_guc_submission_reset_prepare(struct intel_guc *guc)
   	spin_unlock_irq(&guc_to_gt(guc)->irq_lock);
   	flush_work(&guc->ct.requests.worker);
+	guc_flush_destroyed_contexts(guc);
   	scrub_guc_desc_for_outstanding_g2h(guc);
   }
@@ -1102,6 +1107,8 @@ void intel_guc_submission_reset_finish(struct intel_guc *guc)
   	intel_gt_unpark_heartbeats(guc_to_gt(guc));
   }
+static void destroyed_worker_func(struct work_struct *w);
+
   /*
    * Set up the memory resources to be shared with the GuC (via the GGTT)
    * at firmware loading time.
@@ -1124,9 +1131,11 @@ 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);
+	spin_lock_init(&guc->submission_state.lock);
+	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
+	ida_init(&guc->submission_state.guc_ids);
+	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
+	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
   	return 0;
   }
@@ -1137,6 +1146,7 @@ void intel_guc_submission_fini(struct intel_guc *guc)
   		return;
   	guc_lrc_desc_pool_destroy(guc);
+	guc_flush_destroyed_contexts(guc);
   	i915_sched_engine_put(guc->sched_engine);
   }
@@ -1191,15 +1201,16 @@ static void guc_submit_request(struct i915_request *rq)
   static int new_guc_id(struct intel_guc *guc)
   {
-	return ida_simple_get(&guc->guc_ids, 0,
-			      guc->num_guc_ids, GFP_KERNEL |
+	return ida_simple_get(&guc->submission_state.guc_ids, 0,
+			      guc->submission_state.num_guc_ids, 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.id);
+		ida_simple_remove(&guc->submission_state.guc_ids,
+				  ce->guc_id.id);
   		reset_lrc_desc(guc, ce->guc_id.id);
   		set_context_guc_id_invalid(ce);
   	}
@@ -1211,9 +1222,9 @@ static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
   {
   	unsigned long flags;
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
   	__release_guc_id(guc, ce);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
   }
   static int steal_guc_id(struct intel_guc *guc)
@@ -1221,10 +1232,10 @@ static int steal_guc_id(struct intel_guc *guc)
   	struct intel_context *ce;
   	int guc_id;
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
-	if (!list_empty(&guc->guc_id_list)) {
-		ce = list_first_entry(&guc->guc_id_list,
+	if (!list_empty(&guc->submission_state.guc_id_list)) {
+		ce = list_first_entry(&guc->submission_state.guc_id_list,
   				      struct intel_context,
   				      guc_id.link);
@@ -1249,7 +1260,7 @@ static int assign_guc_id(struct intel_guc *guc, u16 *out)
   {
   	int ret;
-	lockdep_assert_held(&guc->contexts_lock);
+	lockdep_assert_held(&guc->submission_state.lock);
   	ret = new_guc_id(guc);
   	if (unlikely(ret < 0)) {
@@ -1271,7 +1282,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
   	GEM_BUG_ON(atomic_read(&ce->guc_id.ref));
   try_again:
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
   	might_lock(&ce->guc_state.lock);
@@ -1286,7 +1297,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
   	atomic_inc(&ce->guc_id.ref);
   out_unlock:
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
   	/*
   	 * -EAGAIN indicates no guc_id are available, let's retire any
@@ -1322,11 +1333,12 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
   	if (unlikely(context_guc_id_invalid(ce)))
   		return;
-	spin_lock_irqsave(&guc->contexts_lock, flags);
+	spin_lock_irqsave(&guc->submission_state.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);
+		list_add_tail(&ce->guc_id.link,
+			      &guc->submission_state.guc_id_list);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
   }
   static int __guc_action_register_context(struct intel_guc *guc,
@@ -1841,11 +1853,30 @@ static void guc_context_sched_disable(struct intel_context *ce)
   static void guc_lrc_desc_unpin(struct intel_context *ce)
   {
   	struct intel_guc *guc = ce_to_guc(ce);
+	struct intel_gt *gt = guc_to_gt(guc);
+	unsigned long flags;
+	bool disabled;
+	GEM_BUG_ON(!intel_gt_pm_is_awake(gt));
   	GEM_BUG_ON(!lrc_desc_registered(guc, ce->guc_id.id));
   	GEM_BUG_ON(ce != __get_context(guc, ce->guc_id.id));
   	GEM_BUG_ON(context_enabled(ce));
+	/* Seal race with Reset */
+	spin_lock_irqsave(&ce->guc_state.lock, flags);
+	disabled = submission_disabled(guc);
+	if (likely(!disabled)) {
+		__intel_gt_pm_get(gt);
+		set_context_destroyed(ce);
+		clr_context_registered(ce);
+	}
+	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
+	if (unlikely(disabled)) {
+		release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+		return;
+	}
+
   	deregister_context(ce, ce->guc_id.id, true);
   }
@@ -1873,78 +1904,88 @@ static void __guc_context_destroy(struct intel_context *ce)
   	}
   }
+static void guc_flush_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	GEM_BUG_ON(!submission_disabled(guc) &&
+		   guc_submission_initialized(guc));
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		__release_guc_id(guc, ce);
+		__guc_context_destroy(ce);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void deregister_destroyed_contexts(struct intel_guc *guc)
+{
+	struct intel_context *ce, *cn;
+	unsigned long flags;
+
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	list_for_each_entry_safe(ce, cn,
+				 &guc->submission_state.destroyed_contexts,
Quoted 9 lines

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

Quoted 3 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+				 destroyed_link) {
+		list_del_init(&ce->destroyed_link);
+		spin_unlock_irqrestore(&guc->submission_state.lock, flags);

Lock drop and re-acquire is not safe - list_for_each_entry_safe only deal with the list_del_init above. It does not help if someone else modifies the list in the window while the lock is not held, in which case things will go BOOM!

Good catch, but is this true? We always add via list_add_tail which if I'm reading the list_for_each_entry_safe macro correctly I think is safe to do as 'n = list_next_entry(n, member)' will just evaluate to the new tail on each iteration, right?

Quoted 8 lines

Reference to macro below:

#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_first_entry(head, typeof(*pos), member),	\
		n = list_next_entry(pos, member);			\
	     &pos->member != (head); 					\
	     pos = n, n = list_next_entry(n, member))

Regardless probably safer to just not drop the lock.

Yes, otherwise a comment is needed to explain, but best to avoid such smarts.

Or just use a different list if it fits better. As mentioned if it is a very simple producer/consumer relationship which always unlinks all items, and list head is not re-used for something else, then you can consider llist_add/llist_del_all.

Regards,

Tvrtko

Quoted 164 lines
Quoted 7 lines

Not sure in what differnt ways you use this list, but perhaps you could employ a lockless single linked list for this. In which case you could atomically unlink all elements. Or just change this to not do lock dropping, if you can safely use the same list_head to put on a local list or something.

I think both options work - not sure why I drop the lock here. I'll play around with this and fix in the next rev.

Matt

Quoted 151 lines

Regards,

Tvrtko

Quoted 147 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +43 -60
+		guc_lrc_desc_unpin(ce);
+		spin_lock_irqsave(&guc->submission_state.lock, flags);
+	}
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+}
+
+static void destroyed_worker_func(struct work_struct *w)
+{
+	struct intel_guc *guc = container_of(w, struct intel_guc,
+					     submission_state.destroyed_worker);
+	struct intel_gt *gt = guc_to_gt(guc);
+	int tmp;
+
+	with_intel_gt_pm(gt, tmp)
+		deregister_destroyed_contexts(guc);
+}
+
   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->uncore->rpm;
   	struct intel_guc *guc = ce_to_guc(ce);
-	intel_wakeref_t wakeref;
   	unsigned long flags;
-	bool disabled;
+	bool destroy;
   	/*
   	 * 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 or the GuC is in the middle of a reset.
   	 */
-	if (context_guc_id_invalid(ce)) {
-		__guc_context_destroy(ce);
-		return;
-	} else if (submission_disabled(guc) ||
-		   !lrc_desc_registered(guc, ce->guc_id.id)) {
-		release_guc_id(guc, ce);
-		__guc_context_destroy(ce);
-		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_id available to
-	 * steal 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_id.
-	 */
-	spin_lock_irqsave(&guc->contexts_lock, flags);
-	if (context_guc_id_invalid(ce)) {
-		spin_unlock_irqrestore(&guc->contexts_lock, flags);
-		__guc_context_destroy(ce);
-		return;
-	}
-
-	if (!list_empty(&ce->guc_id.link))
-		list_del_init(&ce->guc_id.link);
-	spin_unlock_irqrestore(&guc->contexts_lock, flags);
-
-	/* Seal race with Reset */
-	spin_lock_irqsave(&ce->guc_state.lock, flags);
-	disabled = submission_disabled(guc);
-	if (likely(!disabled)) {
-		set_context_destroyed(ce);
-		clr_context_registered(ce);
+	spin_lock_irqsave(&guc->submission_state.lock, flags);
+	destroy = submission_disabled(guc) || context_guc_id_invalid(ce) ||
+		!lrc_desc_registered(guc, ce->guc_id.id);
+	if (likely(!destroy)) {
+		if (!list_empty(&ce->guc_id.link))
+			list_del_init(&ce->guc_id.link);
+		list_add_tail(&ce->destroyed_link,
+			      &guc->submission_state.destroyed_contexts);
+	} else {
+		__release_guc_id(guc, ce);
   	}
-	spin_unlock_irqrestore(&ce->guc_state.lock, flags);
-	if (unlikely(disabled)) {
-		release_guc_id(guc, ce);
+	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
+	if (unlikely(destroy)) {
   		__guc_context_destroy(ce);
   		return;
   	}
   	/*
-	 * We defer GuC context deregistration until the context is destroyed
-	 * in order to save on CTBs. With this optimization ideally we only need
-	 * 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 runtime wakeref is not 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.
+	 * We use a worker to issue the H2G to deregister the context as we can
+	 * take the GT PM for the first time which isn't allowed from an atomic
+	 * context.
   	 */
-	with_intel_runtime_pm(runtime_pm, wakeref)
-		guc_lrc_desc_unpin(ce);
+	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
   }
   static int guc_context_alloc(struct intel_context *ce)
@@ -2703,8 +2744,8 @@ static bool __guc_submission_selected(struct intel_guc *guc)
   void intel_guc_submission_init_early(struct intel_guc *guc)
   {
-	guc->max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
-	guc->num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.max_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
+	guc->submission_state.num_guc_ids = GUC_MAX_LRC_DESCRIPTORS;
   	guc->submission_supported = __guc_submission_supported(guc);
   	guc->submission_selected = __guc_submission_selected(guc);
   }
@@ -2714,10 +2755,10 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
   {
   	struct intel_context *ce;
-	if (unlikely(desc_idx >= guc->max_guc_ids)) {
+	if (unlikely(desc_idx >= guc->submission_state.max_guc_ids)) {
   		drm_err(&guc_to_gt(guc)->i915->drm,
   			"Invalid desc_idx %u, max %u",
-			desc_idx, guc->max_guc_ids);
+			desc_idx, guc->submission_state.max_guc_ids);
   		return NULL;
   	}
@@ -2771,6 +2812,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
   		intel_context_put(ce);
   	} else if (context_destroyed(ce)) {
   		/* Context has been destroyed */
+		intel_gt_pm_put_async(guc_to_gt(guc));
   		release_guc_id(guc, ce);
   		__guc_context_destroy(ce);
   	}
@@ -3065,8 +3107,10 @@ void intel_guc_submission_print_info(struct intel_guc *guc,
   	drm_printf(p, "GuC Number Outstanding Submission G2H: %u\n",
   		   atomic_read(&guc->outstanding_submission_g2h));
-	drm_printf(p, "GuC Number GuC IDs: %u\n", guc->num_guc_ids);
-	drm_printf(p, "GuC Max GuC IDs: %u\n", guc->max_guc_ids);
+	drm_printf(p, "GuC Number GuC IDs: %u\n",
+		   guc->submission_state.num_guc_ids);
+	drm_printf(p, "GuC Max GuC IDs: %u\n",
+		   guc->submission_state.max_guc_ids);
   	drm_printf(p, "GuC tasklet count: %u\n\n",
   		   atomic_read(&sched_engine->tasklet.count));
 
#14 lore raw

PATCH 5/27 drm/i915: Add GT PM unpark worker

In reply to Matthew Brost

Sometimes it is desirable to queue work up for later if the GT PM isn't held and run that work on next GT PM unpark.

Implemented with a list in the GT of all pending work, workqueues in the list, a callback to add a workqueue to the list, and finally a wakeref post_get callback that iterates / drains the list + queues the workqueues.

First user of this is deregistration of GuC contexts.

---
 drivers/gpu/drm/i915/Makefile                 |  1 +
 drivers/gpu/drm/i915/gt/intel_gt.c            |  3 ++
 drivers/gpu/drm/i915/gt/intel_gt_pm.c         |  8 ++++
 .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.c | 35 ++++++++++++++++
 .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.h | 40 +++++++++++++++++++
 drivers/gpu/drm/i915/gt/intel_gt_types.h      | 10 +++++
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  8 ++--
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++--
 drivers/gpu/drm/i915/intel_wakeref.c          |  5 +++
 drivers/gpu/drm/i915/intel_wakeref.h          |  1 +
 10 files changed, 119 insertions(+), 7 deletions(-)
 create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c
 create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h
drivers/gpu/drm/i915/Makefile +1 -0
index 642a5b5a1b81..579bdc069f25 100644
@@ -103,6 +103,7 @@ gt-y += \
 	gt/intel_gt_clock_utils.o \
 	gt/intel_gt_irq.o \
 	gt/intel_gt_pm.o \
+	gt/intel_gt_pm_unpark_work.o \
 	gt/intel_gt_pm_irq.o \
 	gt/intel_gt_requests.o \
 	gt/intel_gtt.o \
drivers/gpu/drm/i915/gt/intel_gt.c +3 -0
index 62d40c986642..7e690e74baa2 100644
@@ -29,6 +29,9 @@ void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
 
 	spin_lock_init(&gt->irq_lock);
 
+	spin_lock_init(&gt->pm_unpark_work_lock);
+	INIT_LIST_HEAD(&gt->pm_unpark_work_list);
+
 	INIT_LIST_HEAD(&gt->closed_vma);
 	spin_lock_init(&gt->closed_lock);
 
drivers/gpu/drm/i915/gt/intel_gt_pm.c +8 -0
index dea8e2479897..564c11a3748b 100644
@@ -90,6 +90,13 @@ static int __gt_unpark(struct intel_wakeref *wf)
 	return 0;
 }
 
+static void __gt_unpark_work_queue(struct intel_wakeref *wf)
+{
+	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
+
+	intel_gt_pm_unpark_work_queue(gt);
+}
+
 static int __gt_park(struct intel_wakeref *wf)
 {
 	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
@@ -118,6 +125,7 @@ static int __gt_park(struct intel_wakeref *wf)
 
 static const struct intel_wakeref_ops wf_ops = {
 	.get = __gt_unpark,
+	.post_get = __gt_unpark_work_queue,
 	.put = __gt_park,
 };
 
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +35 -0
new file mode 100644
index 000000000000..23162dbd0c35
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#include "i915_drv.h"
+#include "intel_runtime_pm.h"
+#include "intel_gt_pm.h"
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt)
+{
+	struct intel_gt_pm_unpark_work *work, *next;
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	list_for_each_entry_safe(work, next,
+				 &gt->pm_unpark_work_list, link) {
+		list_del_init(&work->link);
+		queue_work(system_unbound_wq, &work->worker);
+	}
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	if (intel_gt_pm_is_awake(gt))
+		queue_work(system_unbound_wq, &work->worker);
+	else if (list_empty(&work->link))
+		list_add_tail(&work->link, &gt->pm_unpark_work_list);
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h new file +40 -0
new file mode 100644
index 000000000000..eaf1dc313aa2
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#ifndef INTEL_GT_PM_UNPARK_WORK_H
+#define INTEL_GT_PM_UNPARK_WORK_H
+
+#include <linux/list.h>
+#include <linux/workqueue.h>
+
+struct intel_gt;
+
+/**
+ * struct intel_gt_pm_unpark_work - work to be scheduled when GT unparked
+ */
+struct intel_gt_pm_unpark_work {
+	/**
+	 * @link: link into gt->pm_unpark_work_list of workers that need to be
+	 * scheduled when GT is unpark, protected by gt->pm_unpark_work_lock
+	 */
+	struct list_head link;
+	/** @worker: will be scheduled when GT unparked */
+	struct work_struct worker;
+};
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt);
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work);
+
+static inline void
+intel_gt_pm_unpark_work_init(struct intel_gt_pm_unpark_work *work,
+			     work_func_t fn)
+{
+	INIT_LIST_HEAD(&work->link);
+	INIT_WORK(&work->worker, fn);
+}
+
+#endif /* INTEL_GT_PM_UNPARK_WORK_H */
drivers/gpu/drm/i915/gt/intel_gt_types.h +10 -0
index a81e21bf1bd1..4480312f0add 100644
@@ -96,6 +96,16 @@ struct intel_gt {
 	struct intel_wakeref wakeref;
 	atomic_t user_wakeref;
 
+	/**
+	 * @pm_unpark_work_list: list of delayed work to scheduled which GT is
+	 * unparked, protected by pm_unpark_work_lock
+	 */
+	struct list_head pm_unpark_work_list;
+	/**
+	 * @pm_unpark_work_lock: protects pm_unpark_work_list
+	 */
+	spinlock_t pm_unpark_work_lock;
+
 	struct list_head closed_vma;
 	spinlock_t closed_lock; /* guards the list of closed_vma */
 
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 7358883f1540..023953e77553 100644
@@ -19,6 +19,7 @@
 #include "intel_uc_fw.h"
 #include "i915_utils.h"
 #include "i915_vma.h"
+#include "gt/intel_gt_pm_unpark_work.h"
 
 struct __guc_ads_blob;
 
@@ -78,11 +79,12 @@ struct intel_guc {
 		 */
 		struct list_head destroyed_contexts;
 		/**
-		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * @destroyed_worker: Worker to deregister contexts, need as we
 		 * need to take a GT PM reference and can't from destroy
-		 * function as it might be in an atomic context (no sleeping)
+		 * function as it might be in an atomic context (no sleeping).
+		 * Worker only issues deregister when GT is unparked.
 		 */
-		struct work_struct destroyed_worker;
+		struct intel_gt_pm_unpark_work destroyed_worker;
 	} submission_state;
 
 	bool submission_supported;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -4
index f835e06e5f9f..dbf919801de2 100644
@@ -1135,7 +1135,8 @@ int intel_guc_submission_init(struct intel_guc *guc)
 	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
 	ida_init(&guc->submission_state.guc_ids);
 	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
-	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
+	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
+				     destroyed_worker_func);
 
 	return 0;
 }
@@ -1942,13 +1943,18 @@ static void deregister_destroyed_contexts(struct intel_guc *guc)
 
 static void destroyed_worker_func(struct work_struct *w)
 {
-	struct intel_guc *guc = container_of(w, struct intel_guc,
+	struct intel_gt_pm_unpark_work *destroyed_worker =
+		container_of(w, struct intel_gt_pm_unpark_work, worker);
+	struct intel_guc *guc = container_of(destroyed_worker, struct intel_guc,
 					     submission_state.destroyed_worker);
 	struct intel_gt *gt = guc_to_gt(guc);
 	int tmp;
 
-	with_intel_gt_pm(gt, tmp)
+	with_intel_gt_pm_if_awake(gt, tmp)
 		deregister_destroyed_contexts(guc);
+
+	if (!list_empty(&guc->submission_state.destroyed_contexts))
+		intel_gt_pm_unpark_work_add(gt, destroyed_worker);
 }
 
 static void guc_context_destroy(struct kref *kref)
@@ -1985,7 +1991,8 @@ static void guc_context_destroy(struct kref *kref)
 	 * take the GT PM for the first time which isn't allowed from an atomic
 	 * context.
 	 */
-	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
+	intel_gt_pm_unpark_work_add(guc_to_gt(guc),
+				    &guc->submission_state.destroyed_worker);
 }
 
 static int guc_context_alloc(struct intel_context *ce)
drivers/gpu/drm/i915/intel_wakeref.c +5 -0
index dfd87d082218..282fc4f312e3 100644
@@ -24,6 +24,8 @@ static void rpm_put(struct intel_wakeref *wf)
 
 int __intel_wakeref_get_first(struct intel_wakeref *wf)
 {
+	bool do_post = false;
+
 	/*
 	 * Treat get/put as different subclasses, as we may need to run
 	 * the put callback from under the shrinker and do not want to
@@ -44,8 +46,11 @@ int __intel_wakeref_get_first(struct intel_wakeref *wf)
 		}
 
 		smp_mb__before_atomic(); /* release wf->count */
+		do_post = true;
 	}
 	atomic_inc(&wf->count);
+	if (do_post && wf->ops->post_get)
+		wf->ops->post_get(wf);
 	mutex_unlock(&wf->mutex);
 
 	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
drivers/gpu/drm/i915/intel_wakeref.h +1 -0
index 545c8f277c46..ef7e6a698e8a 100644
@@ -30,6 +30,7 @@ typedef depot_stack_handle_t intel_wakeref_t;
 
 struct intel_wakeref_ops {
 	int (*get)(struct intel_wakeref *wf);
+	void (*post_get)(struct intel_wakeref *wf);
 	int (*put)(struct intel_wakeref *wf);
 };
 
Signature · 1 line
2.32.0
#15 lore raw

Re: [Intel-gfx] [PATCH 05/27] drm/i915: Add GT PM unpark worker

Quoted 2 lines · Matthew Brost

Sometimes it is desirable to queue work up for later if the GT PM isn't held and run that work on next GT PM unpark.

What is the reason for doing this? Why is it important? Why not just take the GT PM at the time the work is requested?

Quoted 7 lines

Implemented with a list in the GT of all pending work, workqueues in the list, a callback to add a workqueue to the list, and finally a wakeref post_get callback that iterates / drains the list + queues the workqueues.

First user of this is deregistration of GuC contexts.

This statement should be in the first paragraph but needs to be more detailed - why is it necessary to add all this extra complexity rather than just taking the GT PM?

Quoted 190 lines
---
  drivers/gpu/drm/i915/Makefile                 |  1 +
  drivers/gpu/drm/i915/gt/intel_gt.c            |  3 ++
  drivers/gpu/drm/i915/gt/intel_gt_pm.c         |  8 ++++
  .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.c | 35 ++++++++++++++++
  .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.h | 40 +++++++++++++++++++
  drivers/gpu/drm/i915/gt/intel_gt_types.h      | 10 +++++
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  8 ++--
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++--
  drivers/gpu/drm/i915/intel_wakeref.c          |  5 +++
  drivers/gpu/drm/i915/intel_wakeref.h          |  1 +
  10 files changed, 119 insertions(+), 7 deletions(-)

create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h

drivers/gpu/drm/i915/Makefile +1 -0
index 642a5b5a1b81..579bdc069f25 100644
@@ -103,6 +103,7 @@ gt-y += \
  	gt/intel_gt_clock_utils.o \
  	gt/intel_gt_irq.o \
  	gt/intel_gt_pm.o \
+	gt/intel_gt_pm_unpark_work.o \
  	gt/intel_gt_pm_irq.o \
  	gt/intel_gt_requests.o \
  	gt/intel_gtt.o \
drivers/gpu/drm/i915/gt/intel_gt.c +3 -0
index 62d40c986642..7e690e74baa2 100644
@@ -29,6 +29,9 @@ void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
 
  	spin_lock_init(&gt->irq_lock);
 
+	spin_lock_init(&gt->pm_unpark_work_lock);
+	INIT_LIST_HEAD(&gt->pm_unpark_work_list);
+
  	INIT_LIST_HEAD(&gt->closed_vma);
  	spin_lock_init(&gt->closed_lock);
 
drivers/gpu/drm/i915/gt/intel_gt_pm.c +8 -0
index dea8e2479897..564c11a3748b 100644
@@ -90,6 +90,13 @@ static int __gt_unpark(struct intel_wakeref *wf)
  	return 0;
  }
 
+static void __gt_unpark_work_queue(struct intel_wakeref *wf)
+{
+	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
+
+	intel_gt_pm_unpark_work_queue(gt);
+}
+
  static int __gt_park(struct intel_wakeref *wf)
  {
  	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
@@ -118,6 +125,7 @@ static int __gt_park(struct intel_wakeref *wf)
 
  static const struct intel_wakeref_ops wf_ops = {
  	.get = __gt_unpark,
+	.post_get = __gt_unpark_work_queue,
  	.put = __gt_park,
  };
 
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +35 -0
new file mode 100644
index 000000000000..23162dbd0c35
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#include "i915_drv.h"
+#include "intel_runtime_pm.h"
+#include "intel_gt_pm.h"
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt)
+{
+	struct intel_gt_pm_unpark_work *work, *next;
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	list_for_each_entry_safe(work, next,
+				 &gt->pm_unpark_work_list, link) {
+		list_del_init(&work->link);
+		queue_work(system_unbound_wq, &work->worker);
+	}
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	if (intel_gt_pm_is_awake(gt))
+		queue_work(system_unbound_wq, &work->worker);
+	else if (list_empty(&work->link))
+		list_add_tail(&work->link, &gt->pm_unpark_work_list);
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h new file +40 -0
new file mode 100644
index 000000000000..eaf1dc313aa2
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#ifndef INTEL_GT_PM_UNPARK_WORK_H
+#define INTEL_GT_PM_UNPARK_WORK_H
+
+#include <linux/list.h>
+#include <linux/workqueue.h>
+
+struct intel_gt;
+
+/**
+ * struct intel_gt_pm_unpark_work - work to be scheduled when GT unparked
+ */
+struct intel_gt_pm_unpark_work {
+	/**
+	 * @link: link into gt->pm_unpark_work_list of workers that need to be
+	 * scheduled when GT is unpark, protected by gt->pm_unpark_work_lock
+	 */
+	struct list_head link;
+	/** @worker: will be scheduled when GT unparked */
+	struct work_struct worker;
+};
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt);
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work);
+
+static inline void
+intel_gt_pm_unpark_work_init(struct intel_gt_pm_unpark_work *work,
+			     work_func_t fn)
+{
+	INIT_LIST_HEAD(&work->link);
+	INIT_WORK(&work->worker, fn);
+}
+
+#endif /* INTEL_GT_PM_UNPARK_WORK_H */
drivers/gpu/drm/i915/gt/intel_gt_types.h +10 -0
index a81e21bf1bd1..4480312f0add 100644
@@ -96,6 +96,16 @@ struct intel_gt {
  	struct intel_wakeref wakeref;
  	atomic_t user_wakeref;
 
+	/**
+	 * @pm_unpark_work_list: list of delayed work to scheduled which GT is
+	 * unparked, protected by pm_unpark_work_lock
+	 */
+	struct list_head pm_unpark_work_list;
+	/**
+	 * @pm_unpark_work_lock: protects pm_unpark_work_list
+	 */
+	spinlock_t pm_unpark_work_lock;
+
  	struct list_head closed_vma;
  	spinlock_t closed_lock; /* guards the list of closed_vma */
 
drivers/gpu/drm/i915/gt/uc/intel_guc.h +1 -0
index 7358883f1540..023953e77553 100644
@@ -19,6 +19,7 @@
  #include "intel_uc_fw.h"
  #include "i915_utils.h"
  #include "i915_vma.h"
+#include "gt/intel_gt_pm_unpark_work.h"
 
  struct __guc_ads_blob;
 

drivers/gpu/drm/i915/gt/uc/intel_guc.h struct intel_guc { · line 84

Quoted 8 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +2 -2
@@ -78,11 +79,12 @@ struct intel_guc {
  		 */
  		struct list_head destroyed_contexts;
  		/**
-		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * @destroyed_worker: Worker to deregister contexts, need as we
  		 * need to take a GT PM reference and can't from destroy
-		 * function as it might be in an atomic context (no sleeping)
+		 * function as it might be in an atomic context (no sleeping).

These corrections should be squashed into the previous patch that added these comments in the first place.

John.

Quoted 89 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +2 -1
+		 * Worker only issues deregister when GT is unparked.
  		 */
-		struct work_struct destroyed_worker;
+		struct intel_gt_pm_unpark_work destroyed_worker;
  	} submission_state;
 
  	bool submission_supported;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -4
index f835e06e5f9f..dbf919801de2 100644
@@ -1135,7 +1135,8 @@ int intel_guc_submission_init(struct intel_guc *guc)
  	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
  	ida_init(&guc->submission_state.guc_ids);
  	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
-	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
+	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
+				     destroyed_worker_func);
 
  	return 0;
  }
@@ -1942,13 +1943,18 @@ static void deregister_destroyed_contexts(struct intel_guc *guc)
 
  static void destroyed_worker_func(struct work_struct *w)
  {
-	struct intel_guc *guc = container_of(w, struct intel_guc,
+	struct intel_gt_pm_unpark_work *destroyed_worker =
+		container_of(w, struct intel_gt_pm_unpark_work, worker);
+	struct intel_guc *guc = container_of(destroyed_worker, struct intel_guc,
  					     submission_state.destroyed_worker);
  	struct intel_gt *gt = guc_to_gt(guc);
  	int tmp;
 
-	with_intel_gt_pm(gt, tmp)
+	with_intel_gt_pm_if_awake(gt, tmp)
  		deregister_destroyed_contexts(guc);
+
+	if (!list_empty(&guc->submission_state.destroyed_contexts))
+		intel_gt_pm_unpark_work_add(gt, destroyed_worker);
  }
 
  static void guc_context_destroy(struct kref *kref)
@@ -1985,7 +1991,8 @@ static void guc_context_destroy(struct kref *kref)
  	 * take the GT PM for the first time which isn't allowed from an atomic
  	 * context.
  	 */
-	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
+	intel_gt_pm_unpark_work_add(guc_to_gt(guc),
+				    &guc->submission_state.destroyed_worker);
  }
 
  static int guc_context_alloc(struct intel_context *ce)
drivers/gpu/drm/i915/intel_wakeref.c +5 -0
index dfd87d082218..282fc4f312e3 100644
@@ -24,6 +24,8 @@ static void rpm_put(struct intel_wakeref *wf)
 
  int __intel_wakeref_get_first(struct intel_wakeref *wf)
  {
+	bool do_post = false;
+
  	/*
  	 * Treat get/put as different subclasses, as we may need to run
  	 * the put callback from under the shrinker and do not want to
@@ -44,8 +46,11 @@ int __intel_wakeref_get_first(struct intel_wakeref *wf)
  		}
 
  		smp_mb__before_atomic(); /* release wf->count */
+		do_post = true;
  	}
  	atomic_inc(&wf->count);
+	if (do_post && wf->ops->post_get)
+		wf->ops->post_get(wf);
  	mutex_unlock(&wf->mutex);
 
  	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
drivers/gpu/drm/i915/intel_wakeref.h +1 -0
index 545c8f277c46..ef7e6a698e8a 100644
@@ -30,6 +30,7 @@ typedef depot_stack_handle_t intel_wakeref_t;
 
  struct intel_wakeref_ops {
  	int (*get)(struct intel_wakeref *wf);
+	void (*post_get)(struct intel_wakeref *wf);
  	int (*put)(struct intel_wakeref *wf);
  };
 
#16 lore raw

Re: [Intel-gfx] [PATCH 05/27] drm/i915: Add GT PM unpark worker

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

Sometimes it is desirable to queue work up for later if the GT PM isn't held and run that work on next GT PM unpark.

What is the reason for doing this? Why is it important? Why not just take the GT PM at the time the work is requested?

This was suggestion from a long time back, don't take GT PM (waking the GPU) if is is idle. I believe Daniele suggested this a couple years ago / we still have this FIXME comment in DII.

Quoted 6 lines
Quoted 6 lines

Implemented with a list in the GT of all pending work, workqueues in the list, a callback to add a workqueue to the list, and finally a wakeref post_get callback that iterates / drains the list + queues the workqueues.

Quoted 6 lines
Quoted 1 line

First user of this is deregistration of GuC contexts.

This statement should be in the first paragraph but needs to be more detailed - why is it necessary to add all this extra complexity rather than just taking the GT PM?

This is just an optimization - don't take the GT PM when deregistering a context if the GT is idle. If you think it is too complex we can can delete this but IMO it really isn't any more complex than the previous patch as either way we need a list + worker. Also once we have this framework in place we might be able to find other users of this.

Quoted 182 lines
Quoted 182 lines
---
  drivers/gpu/drm/i915/Makefile                 |  1 +
  drivers/gpu/drm/i915/gt/intel_gt.c            |  3 ++
  drivers/gpu/drm/i915/gt/intel_gt_pm.c         |  8 ++++
  .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.c | 35 ++++++++++++++++
  .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.h | 40 +++++++++++++++++++
  drivers/gpu/drm/i915/gt/intel_gt_types.h      | 10 +++++
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  8 ++--
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++--
  drivers/gpu/drm/i915/intel_wakeref.c          |  5 +++
  drivers/gpu/drm/i915/intel_wakeref.h          |  1 +
  10 files changed, 119 insertions(+), 7 deletions(-)

create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h

drivers/gpu/drm/i915/Makefile +1 -0
index 642a5b5a1b81..579bdc069f25 100644
@@ -103,6 +103,7 @@ gt-y += \
  	gt/intel_gt_clock_utils.o \
  	gt/intel_gt_irq.o \
  	gt/intel_gt_pm.o \
+	gt/intel_gt_pm_unpark_work.o \
  	gt/intel_gt_pm_irq.o \
  	gt/intel_gt_requests.o \
  	gt/intel_gtt.o \
drivers/gpu/drm/i915/gt/intel_gt.c +3 -0
index 62d40c986642..7e690e74baa2 100644
@@ -29,6 +29,9 @@ void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
  	spin_lock_init(&gt->irq_lock);
+	spin_lock_init(&gt->pm_unpark_work_lock);
+	INIT_LIST_HEAD(&gt->pm_unpark_work_list);
+
  	INIT_LIST_HEAD(&gt->closed_vma);
  	spin_lock_init(&gt->closed_lock);
drivers/gpu/drm/i915/gt/intel_gt_pm.c +8 -0
index dea8e2479897..564c11a3748b 100644
@@ -90,6 +90,13 @@ static int __gt_unpark(struct intel_wakeref *wf)
  	return 0;
  }
+static void __gt_unpark_work_queue(struct intel_wakeref *wf)
+{
+	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
+
+	intel_gt_pm_unpark_work_queue(gt);
+}
+
  static int __gt_park(struct intel_wakeref *wf)
  {
  	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
@@ -118,6 +125,7 @@ static int __gt_park(struct intel_wakeref *wf)
  static const struct intel_wakeref_ops wf_ops = {
  	.get = __gt_unpark,
+	.post_get = __gt_unpark_work_queue,
  	.put = __gt_park,
  };
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +35 -0
new file mode 100644
index 000000000000..23162dbd0c35
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#include "i915_drv.h"
+#include "intel_runtime_pm.h"
+#include "intel_gt_pm.h"
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt)
+{
+	struct intel_gt_pm_unpark_work *work, *next;
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	list_for_each_entry_safe(work, next,
+				 &gt->pm_unpark_work_list, link) {
+		list_del_init(&work->link);
+		queue_work(system_unbound_wq, &work->worker);
+	}
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	if (intel_gt_pm_is_awake(gt))
+		queue_work(system_unbound_wq, &work->worker);
+	else if (list_empty(&work->link))
+		list_add_tail(&work->link, &gt->pm_unpark_work_list);
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h new file +40 -0
new file mode 100644
index 000000000000..eaf1dc313aa2
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#ifndef INTEL_GT_PM_UNPARK_WORK_H
+#define INTEL_GT_PM_UNPARK_WORK_H
+
+#include <linux/list.h>
+#include <linux/workqueue.h>
+
+struct intel_gt;
+
+/**
+ * struct intel_gt_pm_unpark_work - work to be scheduled when GT unparked
+ */
+struct intel_gt_pm_unpark_work {
+	/**
+	 * @link: link into gt->pm_unpark_work_list of workers that need to be
+	 * scheduled when GT is unpark, protected by gt->pm_unpark_work_lock
+	 */
+	struct list_head link;
+	/** @worker: will be scheduled when GT unparked */
+	struct work_struct worker;
+};
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt);
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work);
+
+static inline void
+intel_gt_pm_unpark_work_init(struct intel_gt_pm_unpark_work *work,
+			     work_func_t fn)
+{
+	INIT_LIST_HEAD(&work->link);
+	INIT_WORK(&work->worker, fn);
+}
+
+#endif /* INTEL_GT_PM_UNPARK_WORK_H */
drivers/gpu/drm/i915/gt/intel_gt_types.h +10 -0
index a81e21bf1bd1..4480312f0add 100644
@@ -96,6 +96,16 @@ struct intel_gt {
  	struct intel_wakeref wakeref;
  	atomic_t user_wakeref;
+	/**
+	 * @pm_unpark_work_list: list of delayed work to scheduled which GT is
+	 * unparked, protected by pm_unpark_work_lock
+	 */
+	struct list_head pm_unpark_work_list;
+	/**
+	 * @pm_unpark_work_lock: protects pm_unpark_work_list
+	 */
+	spinlock_t pm_unpark_work_lock;
+
  	struct list_head closed_vma;
  	spinlock_t closed_lock; /* guards the list of closed_vma */
drivers/gpu/drm/i915/gt/uc/intel_guc.h +1 -0
index 7358883f1540..023953e77553 100644
@@ -19,6 +19,7 @@
  #include "intel_uc_fw.h"
  #include "i915_utils.h"
  #include "i915_vma.h"
+#include "gt/intel_gt_pm_unpark_work.h"
  struct __guc_ads_blob;
@@ -78,11 +79,12 @@ struct intel_guc {
  		 */
  		struct list_head destroyed_contexts;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc.h struct intel_guc { · line 84

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +2 -2
  		/**
-		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * @destroyed_worker: Worker to deregister contexts, need as we
  		 * need to take a GT PM reference and can't from destroy
-		 * function as it might be in an atomic context (no sleeping)
+		 * function as it might be in an atomic context (no sleeping).

These corrections should be squashed into the previous patch that added these comments in the first place.

Yep.

Matt

Quoted 82 lines

John.

Quoted 78 lines
drivers/gpu/drm/i915/gt/uc/intel_guc.h +2 -1
+		 * Worker only issues deregister when GT is unparked.
  		 */
-		struct work_struct destroyed_worker;
+		struct intel_gt_pm_unpark_work destroyed_worker;
  	} submission_state;
  	bool submission_supported;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +11 -4
index f835e06e5f9f..dbf919801de2 100644
@@ -1135,7 +1135,8 @@ int intel_guc_submission_init(struct intel_guc *guc)
  	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
  	ida_init(&guc->submission_state.guc_ids);
  	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
-	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
+	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
+				     destroyed_worker_func);
  	return 0;
  }
@@ -1942,13 +1943,18 @@ static void deregister_destroyed_contexts(struct intel_guc *guc)
  static void destroyed_worker_func(struct work_struct *w)
  {
-	struct intel_guc *guc = container_of(w, struct intel_guc,
+	struct intel_gt_pm_unpark_work *destroyed_worker =
+		container_of(w, struct intel_gt_pm_unpark_work, worker);
+	struct intel_guc *guc = container_of(destroyed_worker, struct intel_guc,
  					     submission_state.destroyed_worker);
  	struct intel_gt *gt = guc_to_gt(guc);
  	int tmp;
-	with_intel_gt_pm(gt, tmp)
+	with_intel_gt_pm_if_awake(gt, tmp)
  		deregister_destroyed_contexts(guc);
+
+	if (!list_empty(&guc->submission_state.destroyed_contexts))
+		intel_gt_pm_unpark_work_add(gt, destroyed_worker);
  }
  static void guc_context_destroy(struct kref *kref)
@@ -1985,7 +1991,8 @@ static void guc_context_destroy(struct kref *kref)
  	 * take the GT PM for the first time which isn't allowed from an atomic
  	 * context.
  	 */
-	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
+	intel_gt_pm_unpark_work_add(guc_to_gt(guc),
+				    &guc->submission_state.destroyed_worker);
  }
  static int guc_context_alloc(struct intel_context *ce)
drivers/gpu/drm/i915/intel_wakeref.c +5 -0
index dfd87d082218..282fc4f312e3 100644
@@ -24,6 +24,8 @@ static void rpm_put(struct intel_wakeref *wf)
  int __intel_wakeref_get_first(struct intel_wakeref *wf)
  {
+	bool do_post = false;
+
  	/*
  	 * Treat get/put as different subclasses, as we may need to run
  	 * the put callback from under the shrinker and do not want to
@@ -44,8 +46,11 @@ int __intel_wakeref_get_first(struct intel_wakeref *wf)
  		}
  		smp_mb__before_atomic(); /* release wf->count */
+		do_post = true;
  	}
  	atomic_inc(&wf->count);
+	if (do_post && wf->ops->post_get)
+		wf->ops->post_get(wf);
  	mutex_unlock(&wf->mutex);
  	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
drivers/gpu/drm/i915/intel_wakeref.h +1 -0
index 545c8f277c46..ef7e6a698e8a 100644
@@ -30,6 +30,7 @@ typedef depot_stack_handle_t intel_wakeref_t;
  struct intel_wakeref_ops {
  	int (*get)(struct intel_wakeref *wf);
+	void (*post_get)(struct intel_wakeref *wf);
  	int (*put)(struct intel_wakeref *wf);
  };
#17 lore raw

Re: [Intel-gfx] [PATCH 05/27] drm/i915: Add GT PM unpark worker

In reply to Matthew Brost
Quoted 2 lines · Matthew Brost

Sometimes it is desirable to queue work up for later if the GT PM isn't held and run that work on next GT PM unpark.

Sounds maybe plausible, but it depends how much work can happen on unpark and whether it can have too much of a negative impact on latency for interactive loads? Or from a reverse angle, why the work wouldn't be done on parking?

Also what kind of mechanism for dealing with too much stuff being put on this list you have? Can there be pressure which triggers (or would need to trigger) these deregistrations to happen at runtime (no park/unpark transitions)?

Quoted 6 lines

Implemented with a list in the GT of all pending work, workqueues in the list, a callback to add a workqueue to the list, and finally a wakeref post_get callback that iterates / drains the list + queues the workqueues.

First user of this is deregistration of GuC contexts.

Does first imply there are more incoming?

Quoted 96 lines
---
  drivers/gpu/drm/i915/Makefile                 |  1 +
  drivers/gpu/drm/i915/gt/intel_gt.c            |  3 ++
  drivers/gpu/drm/i915/gt/intel_gt_pm.c         |  8 ++++
  .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.c | 35 ++++++++++++++++
  .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.h | 40 +++++++++++++++++++
  drivers/gpu/drm/i915/gt/intel_gt_types.h      | 10 +++++
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  8 ++--
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++--
  drivers/gpu/drm/i915/intel_wakeref.c          |  5 +++
  drivers/gpu/drm/i915/intel_wakeref.h          |  1 +
  10 files changed, 119 insertions(+), 7 deletions(-)

create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h

drivers/gpu/drm/i915/Makefile +1 -0
index 642a5b5a1b81..579bdc069f25 100644
@@ -103,6 +103,7 @@ gt-y += \
  	gt/intel_gt_clock_utils.o \
  	gt/intel_gt_irq.o \
  	gt/intel_gt_pm.o \
+	gt/intel_gt_pm_unpark_work.o \
  	gt/intel_gt_pm_irq.o \
  	gt/intel_gt_requests.o \
  	gt/intel_gtt.o \
drivers/gpu/drm/i915/gt/intel_gt.c +3 -0
index 62d40c986642..7e690e74baa2 100644
@@ -29,6 +29,9 @@ void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
 
  	spin_lock_init(&gt->irq_lock);
 
+	spin_lock_init(&gt->pm_unpark_work_lock);
+	INIT_LIST_HEAD(&gt->pm_unpark_work_list);
+
  	INIT_LIST_HEAD(&gt->closed_vma);
  	spin_lock_init(&gt->closed_lock);
 
drivers/gpu/drm/i915/gt/intel_gt_pm.c +8 -0
index dea8e2479897..564c11a3748b 100644
@@ -90,6 +90,13 @@ static int __gt_unpark(struct intel_wakeref *wf)
  	return 0;
  }
 
+static void __gt_unpark_work_queue(struct intel_wakeref *wf)
+{
+	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
+
+	intel_gt_pm_unpark_work_queue(gt);
+}
+
  static int __gt_park(struct intel_wakeref *wf)
  {
  	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
@@ -118,6 +125,7 @@ static int __gt_park(struct intel_wakeref *wf)
 
  static const struct intel_wakeref_ops wf_ops = {
  	.get = __gt_unpark,
+	.post_get = __gt_unpark_work_queue,
  	.put = __gt_park,
  };
 
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +22 -0
new file mode 100644
index 000000000000..23162dbd0c35
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#include "i915_drv.h"
+#include "intel_runtime_pm.h"
+#include "intel_gt_pm.h"
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt)
+{
+	struct intel_gt_pm_unpark_work *work, *next;
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	list_for_each_entry_safe(work, next,
+				 &gt->pm_unpark_work_list, link) {
+		list_del_init(&work->link);
+		queue_work(system_unbound_wq, &work->worker);
+	}
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}

drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c line 32

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +10 -0
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	if (intel_gt_pm_is_awake(gt))
+		queue_work(system_unbound_wq, &work->worker);
+	else if (list_empty(&work->link))

What's the list_empty check for, something can race by design?

Quoted 120 lines
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +3 -0
+		list_add_tail(&work->link, &gt->pm_unpark_work_list);
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h new file +40 -0
new file mode 100644
index 000000000000..eaf1dc313aa2
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#ifndef INTEL_GT_PM_UNPARK_WORK_H
+#define INTEL_GT_PM_UNPARK_WORK_H
+
+#include <linux/list.h>
+#include <linux/workqueue.h>
+
+struct intel_gt;
+
+/**
+ * struct intel_gt_pm_unpark_work - work to be scheduled when GT unparked
+ */
+struct intel_gt_pm_unpark_work {
+	/**
+	 * @link: link into gt->pm_unpark_work_list of workers that need to be
+	 * scheduled when GT is unpark, protected by gt->pm_unpark_work_lock
+	 */
+	struct list_head link;
+	/** @worker: will be scheduled when GT unparked */
+	struct work_struct worker;
+};
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt);
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work);
+
+static inline void
+intel_gt_pm_unpark_work_init(struct intel_gt_pm_unpark_work *work,
+			     work_func_t fn)
+{
+	INIT_LIST_HEAD(&work->link);
+	INIT_WORK(&work->worker, fn);
+}
+
+#endif /* INTEL_GT_PM_UNPARK_WORK_H */
drivers/gpu/drm/i915/gt/intel_gt_types.h +10 -0
index a81e21bf1bd1..4480312f0add 100644
@@ -96,6 +96,16 @@ struct intel_gt {
  	struct intel_wakeref wakeref;
  	atomic_t user_wakeref;
 
+	/**
+	 * @pm_unpark_work_list: list of delayed work to scheduled which GT is
+	 * unparked, protected by pm_unpark_work_lock
+	 */
+	struct list_head pm_unpark_work_list;
+	/**
+	 * @pm_unpark_work_lock: protects pm_unpark_work_list
+	 */
+	spinlock_t pm_unpark_work_lock;
+
  	struct list_head closed_vma;
  	spinlock_t closed_lock; /* guards the list of closed_vma */
 
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 7358883f1540..023953e77553 100644
@@ -19,6 +19,7 @@
  #include "intel_uc_fw.h"
  #include "i915_utils.h"
  #include "i915_vma.h"
+#include "gt/intel_gt_pm_unpark_work.h"
 
  struct __guc_ads_blob;
 
@@ -78,11 +79,12 @@ struct intel_guc {
  		 */
  		struct list_head destroyed_contexts;
  		/**
-		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * @destroyed_worker: Worker to deregister contexts, need as we
  		 * need to take a GT PM reference and can't from destroy
-		 * function as it might be in an atomic context (no sleeping)
+		 * function as it might be in an atomic context (no sleeping).
+		 * Worker only issues deregister when GT is unparked.
  		 */
-		struct work_struct destroyed_worker;
+		struct intel_gt_pm_unpark_work destroyed_worker;
  	} submission_state;
 
  	bool submission_supported;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -2
index f835e06e5f9f..dbf919801de2 100644
@@ -1135,7 +1135,8 @@ int intel_guc_submission_init(struct intel_guc *guc)
  	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
  	ida_init(&guc->submission_state.guc_ids);
  	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
-	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
+	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
+				     destroyed_worker_func);
 
  	return 0;
  }
@@ -1942,13 +1943,18 @@ static void deregister_destroyed_contexts(struct intel_guc *guc)
 
  static void destroyed_worker_func(struct work_struct *w)
  {
-	struct intel_guc *guc = container_of(w, struct intel_guc,
+	struct intel_gt_pm_unpark_work *destroyed_worker =
+		container_of(w, struct intel_gt_pm_unpark_work, worker);
+	struct intel_guc *guc = container_of(destroyed_worker, struct intel_guc,

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void deregister_destroyed_contexts(struct intel_guc *guc) · line 1957

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -1
  					     submission_state.destroyed_worker);
  	struct intel_gt *gt = guc_to_gt(guc);
  	int tmp;
 
-	with_intel_gt_pm(gt, tmp)
+	with_intel_gt_pm_if_awake(gt, tmp)
  		deregister_destroyed_contexts(guc);
+
+	if (!list_empty(&guc->submission_state.destroyed_contexts))
+		intel_gt_pm_unpark_work_add(gt, destroyed_worker);

This is the worker itself, right?

There's a "if awake" here followed by another "if awake" inside intel_gt_pm_unpark_work_add which raises questions.

Second question is what's the list_empty for - why is the state of the list itself relevant to a single worker deciding whether to re-add itself to it or not? And is there a lock protecting this list?

On the overall it feels questionable to have unpark work which apparently can race with subsequent parking. Presumably you cannot have it run sync on unpark due execution context issues?

Quoted 27 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -1
  }
 
  static void guc_context_destroy(struct kref *kref)
@@ -1985,7 +1991,8 @@ static void guc_context_destroy(struct kref *kref)
  	 * take the GT PM for the first time which isn't allowed from an atomic
  	 * context.
  	 */
-	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
+	intel_gt_pm_unpark_work_add(guc_to_gt(guc),
+				    &guc->submission_state.destroyed_worker);
  }
 
  static int guc_context_alloc(struct intel_context *ce)
drivers/gpu/drm/i915/intel_wakeref.c +2 -0
index dfd87d082218..282fc4f312e3 100644
@@ -24,6 +24,8 @@ static void rpm_put(struct intel_wakeref *wf)
 
  int __intel_wakeref_get_first(struct intel_wakeref *wf)
  {
+	bool do_post = false;
+
  	/*
  	 * Treat get/put as different subclasses, as we may need to run
  	 * the put callback from under the shrinker and do not want to

drivers/gpu/drm/i915/intel_wakeref.c int __intel_wakeref_get_first(struct intel_wakeref *wf) · line 53

Quoted 8 lines
drivers/gpu/drm/i915/intel_wakeref.c +3 -0
@@ -44,8 +46,11 @@ int __intel_wakeref_get_first(struct intel_wakeref *wf)
  		}
 
  		smp_mb__before_atomic(); /* release wf->count */
+		do_post = true;
  	}
  	atomic_inc(&wf->count);
+	if (do_post && wf->ops->post_get)
+		wf->ops->post_get(wf);

You want this hook under the wf->mutex and why?

Regards,

Tvrtko

Quoted 16 lines
drivers/gpu/drm/i915/intel_wakeref.c +0 -0
  	mutex_unlock(&wf->mutex);
 
  	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
drivers/gpu/drm/i915/intel_wakeref.h +1 -0
index 545c8f277c46..ef7e6a698e8a 100644
@@ -30,6 +30,7 @@ typedef depot_stack_handle_t intel_wakeref_t;
 
  struct intel_wakeref_ops {
  	int (*get)(struct intel_wakeref *wf);
+	void (*post_get)(struct intel_wakeref *wf);
  	int (*put)(struct intel_wakeref *wf);
  };
 
#18 lore raw

Re: [Intel-gfx] [PATCH 05/27] drm/i915: Add GT PM unpark worker

Quoted 8 lines · Tvrtko Ursulin
Quoted 2 lines · Matthew Brost

Sometimes it is desirable to queue work up for later if the GT PM isn't held and run that work on next GT PM unpark.

Sounds maybe plausible, but it depends how much work can happen on unpark and whether it can have too much of a negative impact on latency for interactive loads? Or from a reverse angle, why the work wouldn't be done on

All it is does is add an interface to kick a work queue on unpark. i.e. All the actually work is done async in the work queue so it shouldn't add any latency.

Quoted 4 lines

parking?

Also what kind of mechanism for dealing with too much stuff being put on this list you have? Can there be pressure which triggers (or would need to

No limits on pressure. See above, I don't think this is a concern.

Quoted 12 lines

trigger) these deregistrations to happen at runtime (no park/unpark transitions)?

Quoted 6 lines

Implemented with a list in the GT of all pending work, workqueues in the list, a callback to add a workqueue to the list, and finally a wakeref post_get callback that iterates / drains the list + queues the workqueues.

First user of this is deregistration of GuC contexts.

Does first imply there are more incoming?

Haven't found another user yet but this is generic mechanism so we can add more in the future if other use cases arrise.

Quoted 94 lines
Quoted 94 lines
---
  drivers/gpu/drm/i915/Makefile                 |  1 +
  drivers/gpu/drm/i915/gt/intel_gt.c            |  3 ++
  drivers/gpu/drm/i915/gt/intel_gt_pm.c         |  8 ++++
  .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.c | 35 ++++++++++++++++
  .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.h | 40 +++++++++++++++++++
  drivers/gpu/drm/i915/gt/intel_gt_types.h      | 10 +++++
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  8 ++--
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++--
  drivers/gpu/drm/i915/intel_wakeref.c          |  5 +++
  drivers/gpu/drm/i915/intel_wakeref.h          |  1 +
  10 files changed, 119 insertions(+), 7 deletions(-)

create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h

drivers/gpu/drm/i915/Makefile +1 -0
index 642a5b5a1b81..579bdc069f25 100644
@@ -103,6 +103,7 @@ gt-y += \
  	gt/intel_gt_clock_utils.o \
  	gt/intel_gt_irq.o \
  	gt/intel_gt_pm.o \
+	gt/intel_gt_pm_unpark_work.o \
  	gt/intel_gt_pm_irq.o \
  	gt/intel_gt_requests.o \
  	gt/intel_gtt.o \
drivers/gpu/drm/i915/gt/intel_gt.c +3 -0
index 62d40c986642..7e690e74baa2 100644
@@ -29,6 +29,9 @@ void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
  	spin_lock_init(&gt->irq_lock);
+	spin_lock_init(&gt->pm_unpark_work_lock);
+	INIT_LIST_HEAD(&gt->pm_unpark_work_list);
+
  	INIT_LIST_HEAD(&gt->closed_vma);
  	spin_lock_init(&gt->closed_lock);
drivers/gpu/drm/i915/gt/intel_gt_pm.c +8 -0
index dea8e2479897..564c11a3748b 100644
@@ -90,6 +90,13 @@ static int __gt_unpark(struct intel_wakeref *wf)
  	return 0;
  }
+static void __gt_unpark_work_queue(struct intel_wakeref *wf)
+{
+	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
+
+	intel_gt_pm_unpark_work_queue(gt);
+}
+
  static int __gt_park(struct intel_wakeref *wf)
  {
  	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
@@ -118,6 +125,7 @@ static int __gt_park(struct intel_wakeref *wf)
  static const struct intel_wakeref_ops wf_ops = {
  	.get = __gt_unpark,
+	.post_get = __gt_unpark_work_queue,
  	.put = __gt_park,
  };
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +26 -0
new file mode 100644
index 000000000000..23162dbd0c35
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#include "i915_drv.h"
+#include "intel_runtime_pm.h"
+#include "intel_gt_pm.h"
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt)
+{
+	struct intel_gt_pm_unpark_work *work, *next;
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	list_for_each_entry_safe(work, next,
+				 &gt->pm_unpark_work_list, link) {
+		list_del_init(&work->link);
+		queue_work(system_unbound_wq, &work->worker);
+	}
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work)
+{
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c line 32

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +6 -0
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	if (intel_gt_pm_is_awake(gt))
+		queue_work(system_unbound_wq, &work->worker);
+	else if (list_empty(&work->link))

What's the list_empty check for, something can race by design?

This function is allowed to be called twice, e.g. Two contexts can be tried to be deregistered while the GT PM is ideal but only first context results in the worker being added to the list of workers to be kicked on unpark.

Quoted 116 lines
Quoted 116 lines
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +3 -0
+		list_add_tail(&work->link, &gt->pm_unpark_work_list);
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h new file +40 -0
new file mode 100644
index 000000000000..eaf1dc313aa2
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#ifndef INTEL_GT_PM_UNPARK_WORK_H
+#define INTEL_GT_PM_UNPARK_WORK_H
+
+#include <linux/list.h>
+#include <linux/workqueue.h>
+
+struct intel_gt;
+
+/**
+ * struct intel_gt_pm_unpark_work - work to be scheduled when GT unparked
+ */
+struct intel_gt_pm_unpark_work {
+	/**
+	 * @link: link into gt->pm_unpark_work_list of workers that need to be
+	 * scheduled when GT is unpark, protected by gt->pm_unpark_work_lock
+	 */
+	struct list_head link;
+	/** @worker: will be scheduled when GT unparked */
+	struct work_struct worker;
+};
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt);
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work);
+
+static inline void
+intel_gt_pm_unpark_work_init(struct intel_gt_pm_unpark_work *work,
+			     work_func_t fn)
+{
+	INIT_LIST_HEAD(&work->link);
+	INIT_WORK(&work->worker, fn);
+}
+
+#endif /* INTEL_GT_PM_UNPARK_WORK_H */
drivers/gpu/drm/i915/gt/intel_gt_types.h +10 -0
index a81e21bf1bd1..4480312f0add 100644
@@ -96,6 +96,16 @@ struct intel_gt {
  	struct intel_wakeref wakeref;
  	atomic_t user_wakeref;
+	/**
+	 * @pm_unpark_work_list: list of delayed work to scheduled which GT is
+	 * unparked, protected by pm_unpark_work_lock
+	 */
+	struct list_head pm_unpark_work_list;
+	/**
+	 * @pm_unpark_work_lock: protects pm_unpark_work_list
+	 */
+	spinlock_t pm_unpark_work_lock;
+
  	struct list_head closed_vma;
  	spinlock_t closed_lock; /* guards the list of closed_vma */
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 7358883f1540..023953e77553 100644
@@ -19,6 +19,7 @@
  #include "intel_uc_fw.h"
  #include "i915_utils.h"
  #include "i915_vma.h"
+#include "gt/intel_gt_pm_unpark_work.h"
  struct __guc_ads_blob;
@@ -78,11 +79,12 @@ struct intel_guc {
  		 */
  		struct list_head destroyed_contexts;
  		/**
-		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * @destroyed_worker: Worker to deregister contexts, need as we
  		 * need to take a GT PM reference and can't from destroy
-		 * function as it might be in an atomic context (no sleeping)
+		 * function as it might be in an atomic context (no sleeping).
+		 * Worker only issues deregister when GT is unparked.
  		 */
-		struct work_struct destroyed_worker;
+		struct intel_gt_pm_unpark_work destroyed_worker;
  	} submission_state;
  	bool submission_supported;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -2
index f835e06e5f9f..dbf919801de2 100644
@@ -1135,7 +1135,8 @@ int intel_guc_submission_init(struct intel_guc *guc)
  	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
  	ida_init(&guc->submission_state.guc_ids);
  	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
-	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
+	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
+				     destroyed_worker_func);
  	return 0;
  }
@@ -1942,13 +1943,18 @@ static void deregister_destroyed_contexts(struct intel_guc *guc)
  static void destroyed_worker_func(struct work_struct *w)
  {
-	struct intel_guc *guc = container_of(w, struct intel_guc,
+	struct intel_gt_pm_unpark_work *destroyed_worker =
+		container_of(w, struct intel_gt_pm_unpark_work, worker);
+	struct intel_guc *guc = container_of(destroyed_worker, struct intel_guc,
  					     submission_state.destroyed_worker);
  	struct intel_gt *gt = guc_to_gt(guc);
  	int tmp;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void deregister_destroyed_contexts(struct intel_guc *guc) · line 1957

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -1
-	with_intel_gt_pm(gt, tmp)
+	with_intel_gt_pm_if_awake(gt, tmp)
  		deregister_destroyed_contexts(guc);
+
+	if (!list_empty(&guc->submission_state.destroyed_contexts))
+		intel_gt_pm_unpark_work_add(gt, destroyed_worker);

This is the worker itself, right?

Yes.

Quoted 3 lines

There's a "if awake" here followed by another "if awake" inside intel_gt_pm_unpark_work_add which raises questions.

Even in the worker we only deregister the context if the GT is awake.

Quoted 4 lines

Second question is what's the list_empty for - why is the state of the list itself relevant to a single worker deciding whether to re-add itself to it or not? And is there a lock protecting this list?

Yes we have locking - pm_unpark_work_lock. It is basically all 2 threads to call intel_gt_pm_unpark_work_add while the GT is idle - in that case only the first call adds the worker to the list of workers to kick when unparked (same explaination as above).

Quoted 4 lines

On the overall it feels questionable to have unpark work which apparently can race with subsequent parking. Presumably you cannot have it run sync on unpark due execution context issues?

No race, just allowed to be called twice.

Quoted 25 lines
Quoted 25 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -1
  }
  static void guc_context_destroy(struct kref *kref)
@@ -1985,7 +1991,8 @@ static void guc_context_destroy(struct kref *kref)
  	 * take the GT PM for the first time which isn't allowed from an atomic
  	 * context.
  	 */
-	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
+	intel_gt_pm_unpark_work_add(guc_to_gt(guc),
+				    &guc->submission_state.destroyed_worker);
  }
  static int guc_context_alloc(struct intel_context *ce)
drivers/gpu/drm/i915/intel_wakeref.c +2 -0
index dfd87d082218..282fc4f312e3 100644
@@ -24,6 +24,8 @@ static void rpm_put(struct intel_wakeref *wf)
  int __intel_wakeref_get_first(struct intel_wakeref *wf)
  {
+	bool do_post = false;
+
  	/*
  	 * Treat get/put as different subclasses, as we may need to run
  	 * the put callback from under the shrinker and do not want to
@@ -44,8 +46,11 @@ int __intel_wakeref_get_first(struct intel_wakeref *wf)
  		}
Quoted 9 lines

drivers/gpu/drm/i915/intel_wakeref.c int __intel_wakeref_get_first(struct intel_wakeref *wf) · line 53

Quoted 6 lines
drivers/gpu/drm/i915/intel_wakeref.c +3 -0
  		smp_mb__before_atomic(); /* release wf->count */
+		do_post = true;
  	}
  	atomic_inc(&wf->count);
+	if (do_post && wf->ops->post_get)
+		wf->ops->post_get(wf);

You want this hook under the wf->mutex and why?

I didn't really think about this but everything else in under the mutex so I included this under the mutex too. In this case this post_get op could likely be outside the mutex but I think it harmless to keep it under the mutex. For future safety, I think it should stay under the mutex.

Matt

Quoted 17 lines

Regards,

Tvrtko

Quoted 13 lines
drivers/gpu/drm/i915/intel_wakeref.c +0 -0
  	mutex_unlock(&wf->mutex);
  	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
drivers/gpu/drm/i915/intel_wakeref.h +1 -0
index 545c8f277c46..ef7e6a698e8a 100644
@@ -30,6 +30,7 @@ typedef depot_stack_handle_t intel_wakeref_t;
  struct intel_wakeref_ops {
  	int (*get)(struct intel_wakeref *wf);
+	void (*post_get)(struct intel_wakeref *wf);
  	int (*put)(struct intel_wakeref *wf);
  };
 
#19 lore raw

Re: [Intel-gfx] [PATCH 05/27] drm/i915: Add GT PM unpark worker

Quoted 14 lines · Matthew Brost
Quoted 8 lines · Tvrtko Ursulin
Quoted 2 lines · Matthew Brost

Sometimes it is desirable to queue work up for later if the GT PM isn't held and run that work on next GT PM unpark.

Sounds maybe plausible, but it depends how much work can happen on unpark and whether it can have too much of a negative impact on latency for interactive loads? Or from a reverse angle, why the work wouldn't be done on

All it is does is add an interface to kick a work queue on unpark. i.e. All the actually work is done async in the work queue so it shouldn't add any latency.

Quoted 6 lines
Quoted 4 lines

parking?

Also what kind of mechanism for dealing with too much stuff being put on this list you have? Can there be pressure which triggers (or would need to

No limits on pressure. See above, I don't think this is a concern.

On unpark it has the potential to send an unbound amount of actions for the GuC to process. Which will be competing, in GuC internal processing power, with the user action which caused the unpark. That logically does feel like can have effect on initial latency. Why you think it cannot?

Why the work wouldn't be done on parking?

With this scheme couldn't we end up with a situation that the worker keeps missing the GT unparked state and so keeps piling items on the deregistration list? Can you run of some ids like that (which is related to my question of how is pressure handled here).

Unpark
Register context
Submit work
Retire
Schedule context deregister
Park

Worker runs
GT parked
Work put on a list

Unpark
Schedule deregistration worker
Register new context
Submit work
Retire
Schedule contect deregister
Park

Worker runs (lets say there was CPU pressure) GT already parked
-> deregistration queue now has two contexts on it

... repeat until disaster ...

Unless I have misunderstood the logic.

Quoted 8 lines
Quoted 8 lines

trigger) these deregistrations to happen at runtime (no park/unpark transitions)?

Quoted 5 lines

Implemented with a list in the GT of all pending work, workqueues in the list, a callback to add a workqueue to the list, and finally a wakeref post_get callback that iterates / drains the list + queues the workqueues.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

First user of this is deregistration of GuC contexts.

Does first imply there are more incoming?

Haven't found another user yet but this is generic mechanism so we can add more in the future if other use cases arrise.

My feeling is it would be best to leave it for later.

Quoted 105 lines
Quoted 95 lines
Quoted 95 lines
---
   drivers/gpu/drm/i915/Makefile                 |  1 +
   drivers/gpu/drm/i915/gt/intel_gt.c            |  3 ++
   drivers/gpu/drm/i915/gt/intel_gt_pm.c         |  8 ++++
   .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.c | 35 ++++++++++++++++
   .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.h | 40 +++++++++++++++++++
   drivers/gpu/drm/i915/gt/intel_gt_types.h      | 10 +++++
   drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  8 ++--
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++--
   drivers/gpu/drm/i915/intel_wakeref.c          |  5 +++
   drivers/gpu/drm/i915/intel_wakeref.h          |  1 +
   10 files changed, 119 insertions(+), 7 deletions(-)

create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h

drivers/gpu/drm/i915/Makefile +1 -0
index 642a5b5a1b81..579bdc069f25 100644
@@ -103,6 +103,7 @@ gt-y += \
   	gt/intel_gt_clock_utils.o \
   	gt/intel_gt_irq.o \
   	gt/intel_gt_pm.o \
+	gt/intel_gt_pm_unpark_work.o \
   	gt/intel_gt_pm_irq.o \
   	gt/intel_gt_requests.o \
   	gt/intel_gtt.o \
drivers/gpu/drm/i915/gt/intel_gt.c +3 -0
index 62d40c986642..7e690e74baa2 100644
@@ -29,6 +29,9 @@ void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
   	spin_lock_init(&gt->irq_lock);
+	spin_lock_init(&gt->pm_unpark_work_lock);
+	INIT_LIST_HEAD(&gt->pm_unpark_work_list);
+
   	INIT_LIST_HEAD(&gt->closed_vma);
   	spin_lock_init(&gt->closed_lock);
drivers/gpu/drm/i915/gt/intel_gt_pm.c +8 -0
index dea8e2479897..564c11a3748b 100644
@@ -90,6 +90,13 @@ static int __gt_unpark(struct intel_wakeref *wf)
   	return 0;
   }
+static void __gt_unpark_work_queue(struct intel_wakeref *wf)
+{
+	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
+
+	intel_gt_pm_unpark_work_queue(gt);
+}
+
   static int __gt_park(struct intel_wakeref *wf)
   {
   	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
@@ -118,6 +125,7 @@ static int __gt_park(struct intel_wakeref *wf)
   static const struct intel_wakeref_ops wf_ops = {
   	.get = __gt_unpark,
+	.post_get = __gt_unpark_work_queue,
   	.put = __gt_park,
   };
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +26 -0
new file mode 100644
index 000000000000..23162dbd0c35
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#include "i915_drv.h"
+#include "intel_runtime_pm.h"
+#include "intel_gt_pm.h"
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt)
+{
+	struct intel_gt_pm_unpark_work *work, *next;
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	list_for_each_entry_safe(work, next,
+				 &gt->pm_unpark_work_list, link) {
+		list_del_init(&work->link);
+		queue_work(system_unbound_wq, &work->worker);
+	}
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work)
+{
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c line 32

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +6 -0
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	if (intel_gt_pm_is_awake(gt))
+		queue_work(system_unbound_wq, &work->worker);
+	else if (list_empty(&work->link))

What's the list_empty check for, something can race by design?

Quoted 4 lines

This function is allowed to be called twice, e.g. Two contexts can be tried to be deregistered while the GT PM is ideal but only first context results in the worker being added to the list of workers to be kicked on unpark.

Hmm okay.

Quoted 127 lines
Quoted 117 lines
Quoted 117 lines
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +3 -0
+		list_add_tail(&work->link, &gt->pm_unpark_work_list);
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h new file +40 -0
new file mode 100644
index 000000000000..eaf1dc313aa2
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#ifndef INTEL_GT_PM_UNPARK_WORK_H
+#define INTEL_GT_PM_UNPARK_WORK_H
+
+#include <linux/list.h>
+#include <linux/workqueue.h>
+
+struct intel_gt;
+
+/**
+ * struct intel_gt_pm_unpark_work - work to be scheduled when GT unparked
+ */
+struct intel_gt_pm_unpark_work {
+	/**
+	 * @link: link into gt->pm_unpark_work_list of workers that need to be
+	 * scheduled when GT is unpark, protected by gt->pm_unpark_work_lock
+	 */
+	struct list_head link;
+	/** @worker: will be scheduled when GT unparked */
+	struct work_struct worker;
+};
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt);
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work);
+
+static inline void
+intel_gt_pm_unpark_work_init(struct intel_gt_pm_unpark_work *work,
+			     work_func_t fn)
+{
+	INIT_LIST_HEAD(&work->link);
+	INIT_WORK(&work->worker, fn);
+}
+
+#endif /* INTEL_GT_PM_UNPARK_WORK_H */
drivers/gpu/drm/i915/gt/intel_gt_types.h +10 -0
index a81e21bf1bd1..4480312f0add 100644
@@ -96,6 +96,16 @@ struct intel_gt {
   	struct intel_wakeref wakeref;
   	atomic_t user_wakeref;
+	/**
+	 * @pm_unpark_work_list: list of delayed work to scheduled which GT is
+	 * unparked, protected by pm_unpark_work_lock
+	 */
+	struct list_head pm_unpark_work_list;
+	/**
+	 * @pm_unpark_work_lock: protects pm_unpark_work_list
+	 */
+	spinlock_t pm_unpark_work_lock;
+
   	struct list_head closed_vma;
   	spinlock_t closed_lock; /* guards the list of closed_vma */
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 7358883f1540..023953e77553 100644
@@ -19,6 +19,7 @@
   #include "intel_uc_fw.h"
   #include "i915_utils.h"
   #include "i915_vma.h"
+#include "gt/intel_gt_pm_unpark_work.h"
   struct __guc_ads_blob;
@@ -78,11 +79,12 @@ struct intel_guc {
   		 */
   		struct list_head destroyed_contexts;
   		/**
-		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * @destroyed_worker: Worker to deregister contexts, need as we
   		 * need to take a GT PM reference and can't from destroy
-		 * function as it might be in an atomic context (no sleeping)
+		 * function as it might be in an atomic context (no sleeping).
+		 * Worker only issues deregister when GT is unparked.
   		 */
-		struct work_struct destroyed_worker;
+		struct intel_gt_pm_unpark_work destroyed_worker;
   	} submission_state;
   	bool submission_supported;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -2
index f835e06e5f9f..dbf919801de2 100644
@@ -1135,7 +1135,8 @@ int intel_guc_submission_init(struct intel_guc *guc)
   	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
   	ida_init(&guc->submission_state.guc_ids);
   	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
-	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
+	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
+				     destroyed_worker_func);
   	return 0;
   }
@@ -1942,13 +1943,18 @@ static void deregister_destroyed_contexts(struct intel_guc *guc)
   static void destroyed_worker_func(struct work_struct *w)
   {
-	struct intel_guc *guc = container_of(w, struct intel_guc,
+	struct intel_gt_pm_unpark_work *destroyed_worker =
+		container_of(w, struct intel_gt_pm_unpark_work, worker);
+	struct intel_guc *guc = container_of(destroyed_worker, struct intel_guc,
   					     submission_state.destroyed_worker);
   	struct intel_gt *gt = guc_to_gt(guc);
   	int tmp;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void deregister_destroyed_contexts(struct intel_guc *guc) · line 1957

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -1
-	with_intel_gt_pm(gt, tmp)
+	with_intel_gt_pm_if_awake(gt, tmp)
   		deregister_destroyed_contexts(guc);
+
+	if (!list_empty(&guc->submission_state.destroyed_contexts))
+		intel_gt_pm_unpark_work_add(gt, destroyed_worker);

This is the worker itself, right?

Quoted 7 lines

Yes.

Quoted 3 lines

There's a "if awake" here followed by another "if awake" inside intel_gt_pm_unpark_work_add which raises questions.

Even in the worker we only deregister the context if the GT is awake.

Yeah but the two "if awake" checks followed by one another is the confusing part since it is not atomic. Coupled with the list empty check I mean. So two external async condition can influence the flow, one of which is even checked twice.

The worker can decide not to deregister, but by the time intel_gt_pm_unpark_work_add runs GT may have became unparked so worker gets queued. When it runs GT is parked again and so it can bounce in a loop forever.

Quoted 11 lines
Quoted 5 lines

Second question is what's the list_empty for - why is the state of the list itself relevant to a single worker deciding whether to re-add itself to it or not? And is there a lock protecting this list?

Yes we have locking - pm_unpark_work_lock. It is basically all 2 threads to call intel_gt_pm_unpark_work_add while the GT is idle - in that case only the first call adds the worker to the list of workers to kick when unparked (same explaination as above).

Quoted 6 lines
Quoted 4 lines

On the overall it feels questionable to have unpark work which apparently can race with subsequent parking. Presumably you cannot have it run sync on unpark due execution context issues?

No race, just allowed to be called twice.

But list_empty check is not under any locks so I don't follow.

Quoted 36 lines
Quoted 26 lines
Quoted 26 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -1
   }
   static void guc_context_destroy(struct kref *kref)
@@ -1985,7 +1991,8 @@ static void guc_context_destroy(struct kref *kref)
   	 * take the GT PM for the first time which isn't allowed from an atomic
   	 * context.
   	 */
-	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
+	intel_gt_pm_unpark_work_add(guc_to_gt(guc),
+				    &guc->submission_state.destroyed_worker);
   }
   static int guc_context_alloc(struct intel_context *ce)
drivers/gpu/drm/i915/intel_wakeref.c +2 -0
index dfd87d082218..282fc4f312e3 100644
@@ -24,6 +24,8 @@ static void rpm_put(struct intel_wakeref *wf)
   int __intel_wakeref_get_first(struct intel_wakeref *wf)
   {
+	bool do_post = false;
+
   	/*
   	 * Treat get/put as different subclasses, as we may need to run
   	 * the put callback from under the shrinker and do not want to
@@ -44,8 +46,11 @@ int __intel_wakeref_get_first(struct intel_wakeref *wf)
   		}
Quoted 9 lines

drivers/gpu/drm/i915/intel_wakeref.c int __intel_wakeref_get_first(struct intel_wakeref *wf) · line 53

Quoted 6 lines
drivers/gpu/drm/i915/intel_wakeref.c +3 -0
   		smp_mb__before_atomic(); /* release wf->count */
+		do_post = true;
   	}
   	atomic_inc(&wf->count);
+	if (do_post && wf->ops->post_get)
+		wf->ops->post_get(wf);

You want this hook under the wf->mutex and why?

Quoted 5 lines

I didn't really think about this but everything else in under the mutex so I included this under the mutex too. In this case this post_get op could likely be outside the mutex but I think it harmless to keep it under the mutex. For future safety, I think it should stay under the mutex.

If it is under the mutex then what is the point of two hooks? The only difference is get sees wf->count == 0, while post_get sees it as one.

No wait.. you have put post_get outside the 0->1 transition check so potentially called more than once. You probably do not want this.. But could you just do this from the existing hook is the main question.

Regards,

Tvrtko

Quoted 20 lines

Matt

Quoted 17 lines

Regards,

Tvrtko

Quoted 13 lines
drivers/gpu/drm/i915/intel_wakeref.c +0 -0
   	mutex_unlock(&wf->mutex);
   	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
drivers/gpu/drm/i915/intel_wakeref.h +1 -0
index 545c8f277c46..ef7e6a698e8a 100644
@@ -30,6 +30,7 @@ typedef depot_stack_handle_t intel_wakeref_t;
   struct intel_wakeref_ops {
   	int (*get)(struct intel_wakeref *wf);
+	void (*post_get)(struct intel_wakeref *wf);
   	int (*put)(struct intel_wakeref *wf);
   };
 
#20 lore raw

Re: [Intel-gfx] [PATCH 05/27] drm/i915: Add GT PM unpark worker

Quoted 21 lines · Tvrtko Ursulin
Quoted 19 lines · Matthew Brost
Quoted 8 lines · Tvrtko Ursulin
Quoted 2 lines · Matthew Brost

Sometimes it is desirable to queue work up for later if the GT PM isn't held and run that work on next GT PM unpark.

Sounds maybe plausible, but it depends how much work can happen on unpark and whether it can have too much of a negative impact on latency for interactive loads? Or from a reverse angle, why the work wouldn't be done on

All it is does is add an interface to kick a work queue on unpark. i.e. All the actually work is done async in the work queue so it shouldn't add any latency.

Quoted 4 lines

parking?

Also what kind of mechanism for dealing with too much stuff being put on this list you have? Can there be pressure which triggers (or would need to

Quoted 7 lines
Quoted 1 line

No limits on pressure. See above, I don't think this is a concern.

On unpark it has the potential to send an unbound amount of actions for the GuC to process. Which will be competing, in GuC internal processing power, with the user action which caused the unpark. That logically does feel like can have effect on initial latency. Why you think it cannot?

Ah, I see what you mean. Yes, bunch of deregisters could be sent before a submission adding latency. Maybe I just drop this whole idea / patch for now. Not going to respond to individual comments because this will be dropped.

Matt

Quoted 399 lines

Why the work wouldn't be done on parking?

With this scheme couldn't we end up with a situation that the worker keeps missing the GT unparked state and so keeps piling items on the deregistration list? Can you run of some ids like that (which is related to my question of how is pressure handled here).

Unpark
Register context
Submit work
Retire
Schedule context deregister
Park

Worker runs
GT parked
Work put on a list

Unpark
Schedule deregistration worker
Register new context
Submit work
Retire
Schedule contect deregister
Park

Worker runs (lets say there was CPU pressure) GT already parked
-> deregistration queue now has two contexts on it

... repeat until disaster ...

Unless I have misunderstood the logic.

Quoted 8 lines
Quoted 8 lines

trigger) these deregistrations to happen at runtime (no park/unpark transitions)?

Quoted 5 lines

Implemented with a list in the GT of all pending work, workqueues in the list, a callback to add a workqueue to the list, and finally a wakeref post_get callback that iterates / drains the list + queues the workqueues.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

First user of this is deregistration of GuC contexts.

Does first imply there are more incoming?

Haven't found another user yet but this is generic mechanism so we can add more in the future if other use cases arrise.

My feeling is it would be best to leave it for later.

Quoted 104 lines
Quoted 94 lines
Quoted 94 lines
---
   drivers/gpu/drm/i915/Makefile                 |  1 +
   drivers/gpu/drm/i915/gt/intel_gt.c            |  3 ++
   drivers/gpu/drm/i915/gt/intel_gt_pm.c         |  8 ++++
   .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.c | 35 ++++++++++++++++
   .../gpu/drm/i915/gt/intel_gt_pm_unpark_work.h | 40 +++++++++++++++++++
   drivers/gpu/drm/i915/gt/intel_gt_types.h      | 10 +++++
   drivers/gpu/drm/i915/gt/uc/intel_guc.h        |  8 ++--
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 15 +++++--
   drivers/gpu/drm/i915/intel_wakeref.c          |  5 +++
   drivers/gpu/drm/i915/intel_wakeref.h          |  1 +
   10 files changed, 119 insertions(+), 7 deletions(-)

create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c create mode 100644 drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h

drivers/gpu/drm/i915/Makefile +1 -0
index 642a5b5a1b81..579bdc069f25 100644
@@ -103,6 +103,7 @@ gt-y += \
   	gt/intel_gt_clock_utils.o \
   	gt/intel_gt_irq.o \
   	gt/intel_gt_pm.o \
+	gt/intel_gt_pm_unpark_work.o \
   	gt/intel_gt_pm_irq.o \
   	gt/intel_gt_requests.o \
   	gt/intel_gtt.o \
drivers/gpu/drm/i915/gt/intel_gt.c +3 -0
index 62d40c986642..7e690e74baa2 100644
@@ -29,6 +29,9 @@ void intel_gt_init_early(struct intel_gt *gt, struct drm_i915_private *i915)
   	spin_lock_init(&gt->irq_lock);
+	spin_lock_init(&gt->pm_unpark_work_lock);
+	INIT_LIST_HEAD(&gt->pm_unpark_work_list);
+
   	INIT_LIST_HEAD(&gt->closed_vma);
   	spin_lock_init(&gt->closed_lock);
drivers/gpu/drm/i915/gt/intel_gt_pm.c +8 -0
index dea8e2479897..564c11a3748b 100644
@@ -90,6 +90,13 @@ static int __gt_unpark(struct intel_wakeref *wf)
   	return 0;
   }
+static void __gt_unpark_work_queue(struct intel_wakeref *wf)
+{
+	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
+
+	intel_gt_pm_unpark_work_queue(gt);
+}
+
   static int __gt_park(struct intel_wakeref *wf)
   {
   	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
@@ -118,6 +125,7 @@ static int __gt_park(struct intel_wakeref *wf)
   static const struct intel_wakeref_ops wf_ops = {
   	.get = __gt_unpark,
+	.post_get = __gt_unpark_work_queue,
   	.put = __gt_park,
   };
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +26 -0
new file mode 100644
index 000000000000..23162dbd0c35
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#include "i915_drv.h"
+#include "intel_runtime_pm.h"
+#include "intel_gt_pm.h"
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt)
+{
+	struct intel_gt_pm_unpark_work *work, *next;
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	list_for_each_entry_safe(work, next,
+				 &gt->pm_unpark_work_list, link) {
+		list_del_init(&work->link);
+		queue_work(system_unbound_wq, &work->worker);
+	}
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work)
+{
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c line 32

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +6 -0
+	unsigned long flags;
+
+	spin_lock_irqsave(&gt->pm_unpark_work_lock, flags);
+	if (intel_gt_pm_is_awake(gt))
+		queue_work(system_unbound_wq, &work->worker);
+	else if (list_empty(&work->link))

What's the list_empty check for, something can race by design?

Quoted 4 lines

This function is allowed to be called twice, e.g. Two contexts can be tried to be deregistered while the GT PM is ideal but only first context results in the worker being added to the list of workers to be kicked on unpark.

Hmm okay.

Quoted 127 lines
Quoted 117 lines
Quoted 117 lines
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.c new file +3 -0
+		list_add_tail(&work->link, &gt->pm_unpark_work_list);
+	spin_unlock_irqrestore(&gt->pm_unpark_work_lock, flags);
+}
drivers/gpu/drm/i915/gt/intel_gt_pm_unpark_work.h new file +40 -0
new file mode 100644
index 000000000000..eaf1dc313aa2
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2021 Intel Corporation
+ */
+
+#ifndef INTEL_GT_PM_UNPARK_WORK_H
+#define INTEL_GT_PM_UNPARK_WORK_H
+
+#include <linux/list.h>
+#include <linux/workqueue.h>
+
+struct intel_gt;
+
+/**
+ * struct intel_gt_pm_unpark_work - work to be scheduled when GT unparked
+ */
+struct intel_gt_pm_unpark_work {
+	/**
+	 * @link: link into gt->pm_unpark_work_list of workers that need to be
+	 * scheduled when GT is unpark, protected by gt->pm_unpark_work_lock
+	 */
+	struct list_head link;
+	/** @worker: will be scheduled when GT unparked */
+	struct work_struct worker;
+};
+
+void intel_gt_pm_unpark_work_queue(struct intel_gt *gt);
+
+void intel_gt_pm_unpark_work_add(struct intel_gt *gt,
+				 struct intel_gt_pm_unpark_work *work);
+
+static inline void
+intel_gt_pm_unpark_work_init(struct intel_gt_pm_unpark_work *work,
+			     work_func_t fn)
+{
+	INIT_LIST_HEAD(&work->link);
+	INIT_WORK(&work->worker, fn);
+}
+
+#endif /* INTEL_GT_PM_UNPARK_WORK_H */
drivers/gpu/drm/i915/gt/intel_gt_types.h +10 -0
index a81e21bf1bd1..4480312f0add 100644
@@ -96,6 +96,16 @@ struct intel_gt {
   	struct intel_wakeref wakeref;
   	atomic_t user_wakeref;
+	/**
+	 * @pm_unpark_work_list: list of delayed work to scheduled which GT is
+	 * unparked, protected by pm_unpark_work_lock
+	 */
+	struct list_head pm_unpark_work_list;
+	/**
+	 * @pm_unpark_work_lock: protects pm_unpark_work_list
+	 */
+	spinlock_t pm_unpark_work_lock;
+
   	struct list_head closed_vma;
   	spinlock_t closed_lock; /* guards the list of closed_vma */
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -3
index 7358883f1540..023953e77553 100644
@@ -19,6 +19,7 @@
   #include "intel_uc_fw.h"
   #include "i915_utils.h"
   #include "i915_vma.h"
+#include "gt/intel_gt_pm_unpark_work.h"
   struct __guc_ads_blob;
@@ -78,11 +79,12 @@ struct intel_guc {
   		 */
   		struct list_head destroyed_contexts;
   		/**
-		 * @destroyed_worker: worker to deregister contexts, need as we
+		 * @destroyed_worker: Worker to deregister contexts, need as we
   		 * need to take a GT PM reference and can't from destroy
-		 * function as it might be in an atomic context (no sleeping)
+		 * function as it might be in an atomic context (no sleeping).
+		 * Worker only issues deregister when GT is unparked.
   		 */
-		struct work_struct destroyed_worker;
+		struct intel_gt_pm_unpark_work destroyed_worker;
   	} submission_state;
   	bool submission_supported;
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -2
index f835e06e5f9f..dbf919801de2 100644
@@ -1135,7 +1135,8 @@ int intel_guc_submission_init(struct intel_guc *guc)
   	INIT_LIST_HEAD(&guc->submission_state.guc_id_list);
   	ida_init(&guc->submission_state.guc_ids);
   	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
-	INIT_WORK(&guc->submission_state.destroyed_worker, destroyed_worker_func);
+	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
+				     destroyed_worker_func);
   	return 0;
   }
@@ -1942,13 +1943,18 @@ static void deregister_destroyed_contexts(struct intel_guc *guc)
   static void destroyed_worker_func(struct work_struct *w)
   {
-	struct intel_guc *guc = container_of(w, struct intel_guc,
+	struct intel_gt_pm_unpark_work *destroyed_worker =
+		container_of(w, struct intel_gt_pm_unpark_work, worker);
+	struct intel_guc *guc = container_of(destroyed_worker, struct intel_guc,
   					     submission_state.destroyed_worker);
   	struct intel_gt *gt = guc_to_gt(guc);
   	int tmp;
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void deregister_destroyed_contexts(struct intel_guc *guc) · line 1957

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -1
-	with_intel_gt_pm(gt, tmp)
+	with_intel_gt_pm_if_awake(gt, tmp)
   		deregister_destroyed_contexts(guc);
+
+	if (!list_empty(&guc->submission_state.destroyed_contexts))
+		intel_gt_pm_unpark_work_add(gt, destroyed_worker);

This is the worker itself, right?

Quoted 7 lines

Yes.

Quoted 3 lines

There's a "if awake" here followed by another "if awake" inside intel_gt_pm_unpark_work_add which raises questions.

Even in the worker we only deregister the context if the GT is awake.

Yeah but the two "if awake" checks followed by one another is the confusing part since it is not atomic. Coupled with the list empty check I mean. So two external async condition can influence the flow, one of which is even checked twice.

The worker can decide not to deregister, but by the time intel_gt_pm_unpark_work_add runs GT may have became unparked so worker gets queued. When it runs GT is parked again and so it can bounce in a loop forever.

Quoted 9 lines
Quoted 4 lines

Second question is what's the list_empty for - why is the state of the list itself relevant to a single worker deciding whether to re-add itself to it or not? And is there a lock protecting this list?

Yes we have locking - pm_unpark_work_lock. It is basically all 2 threads to call intel_gt_pm_unpark_work_add while the GT is idle - in that case only the first call adds the worker to the list of workers to kick when unparked (same explaination as above).

Quoted 6 lines
Quoted 4 lines

On the overall it feels questionable to have unpark work which apparently can race with subsequent parking. Presumably you cannot have it run sync on unpark due execution context issues?

No race, just allowed to be called twice.

But list_empty check is not under any locks so I don't follow.

Quoted 36 lines
Quoted 26 lines
Quoted 26 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -1
   }
   static void guc_context_destroy(struct kref *kref)
@@ -1985,7 +1991,8 @@ static void guc_context_destroy(struct kref *kref)
   	 * take the GT PM for the first time which isn't allowed from an atomic
   	 * context.
   	 */
-	queue_work(system_unbound_wq, &guc->submission_state.destroyed_worker);
+	intel_gt_pm_unpark_work_add(guc_to_gt(guc),
+				    &guc->submission_state.destroyed_worker);
   }
   static int guc_context_alloc(struct intel_context *ce)
drivers/gpu/drm/i915/intel_wakeref.c +2 -0
index dfd87d082218..282fc4f312e3 100644
@@ -24,6 +24,8 @@ static void rpm_put(struct intel_wakeref *wf)
   int __intel_wakeref_get_first(struct intel_wakeref *wf)
   {
+	bool do_post = false;
+
   	/*
   	 * Treat get/put as different subclasses, as we may need to run
   	 * the put callback from under the shrinker and do not want to
@@ -44,8 +46,11 @@ int __intel_wakeref_get_first(struct intel_wakeref *wf)
   		}
Quoted 9 lines

drivers/gpu/drm/i915/intel_wakeref.c int __intel_wakeref_get_first(struct intel_wakeref *wf) · line 53

Quoted 6 lines
drivers/gpu/drm/i915/intel_wakeref.c +3 -0
   		smp_mb__before_atomic(); /* release wf->count */
+		do_post = true;
   	}
   	atomic_inc(&wf->count);
+	if (do_post && wf->ops->post_get)
+		wf->ops->post_get(wf);

You want this hook under the wf->mutex and why?

Quoted 5 lines

I didn't really think about this but everything else in under the mutex so I included this under the mutex too. In this case this post_get op could likely be outside the mutex but I think it harmless to keep it under the mutex. For future safety, I think it should stay under the mutex.

If it is under the mutex then what is the point of two hooks? The only difference is get sees wf->count == 0, while post_get sees it as one.

No wait.. you have put post_get outside the 0->1 transition check so potentially called more than once. You probably do not want this.. But could you just do this from the existing hook is the main question.

Regards,

Tvrtko

Quoted 20 lines

Matt

Quoted 17 lines

Regards,

Tvrtko

Quoted 13 lines
drivers/gpu/drm/i915/intel_wakeref.c +0 -0
   	mutex_unlock(&wf->mutex);
   	INTEL_WAKEREF_BUG_ON(atomic_read(&wf->count) <= 0);
drivers/gpu/drm/i915/intel_wakeref.h +1 -0
index 545c8f277c46..ef7e6a698e8a 100644
@@ -30,6 +30,7 @@ typedef depot_stack_handle_t intel_wakeref_t;
   struct intel_wakeref_ops {
   	int (*get)(struct intel_wakeref *wf);
+	void (*post_get)(struct intel_wakeref *wf);
   	int (*put)(struct intel_wakeref *wf);
   };
 
#21 lore raw

PATCH 6/27 drm/i915/guc: Take engine PM when a context is pinned with GuC submission

In reply to Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a scheduling of user context could be enabled.

v2:
(Daniel Vetter)
- Add might_lock annotations to pin / unpin function

---
 drivers/gpu/drm/i915/gt/intel_context.c       |  3 ++
 drivers/gpu/drm/i915/gt/intel_engine_pm.h     | 15 ++++++++
 drivers/gpu/drm/i915/gt/intel_gt_pm.h         | 10 ++++++
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 36 +++++++++++++++++--
 drivers/gpu/drm/i915/intel_wakeref.h          | 12 +++++++
 5 files changed, 73 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index c8595da64ad8..508cfe5770c0 100644
@@ -240,6 +240,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
 	if (err)
 		goto err_post_unpin;
 
+	intel_engine_pm_might_get(ce->engine);
+
 	if (unlikely(intel_context_is_closed(ce))) {
 		err = -ENOENT;
 		goto err_unlock;
@@ -313,6 +315,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
 		return;
 
 	CE_TRACE(ce, "unpin\n");
+	intel_engine_pm_might_put(ce->engine);
 	ce->ops->unpin(ce);
 	ce->ops->post_unpin(ce);
 
drivers/gpu/drm/i915/gt/intel_engine_pm.h +15 -0
index 17a5028ea177..3fe2ae1bcc26 100644
@@ -9,6 +9,7 @@
 #include "i915_request.h"
 #include "intel_engine_types.h"
 #include "intel_wakeref.h"
+#include "intel_gt_pm.h"
 
 static inline bool
 intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
@@ -31,6 +32,13 @@ static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine)
 	return intel_wakeref_get_if_active(&engine->wakeref);
 }
 
+static inline void intel_engine_pm_might_get(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_get(&engine->wakeref);
+	intel_gt_pm_might_get(engine->gt);
+}
+
 static inline void intel_engine_pm_put(struct intel_engine_cs *engine)
 {
 	intel_wakeref_put(&engine->wakeref);
@@ -52,6 +60,13 @@ static inline void intel_engine_pm_flush(struct intel_engine_cs *engine)
 	intel_wakeref_unlock_wait(&engine->wakeref);
 }
 
+static inline void intel_engine_pm_might_put(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_put(&engine->wakeref);
+	intel_gt_pm_might_put(engine->gt);
+}
+
 static inline struct i915_request *
 intel_engine_create_kernel_request(struct intel_engine_cs *engine)
 {
drivers/gpu/drm/i915/gt/intel_gt_pm.h +10 -0
index a17bf0d4592b..3c173033ce23 100644
@@ -31,6 +31,11 @@ static inline bool intel_gt_pm_get_if_awake(struct intel_gt *gt)
 	return intel_wakeref_get_if_active(&gt->wakeref);
 }
 
+static inline void intel_gt_pm_might_get(struct intel_gt *gt)
+{
+	intel_wakeref_might_get(&gt->wakeref);
+}
+
 static inline void intel_gt_pm_put(struct intel_gt *gt)
 {
 	intel_wakeref_put(&gt->wakeref);
@@ -41,6 +46,11 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
 	intel_wakeref_put_async(&gt->wakeref);
 }
 
+static inline void intel_gt_pm_might_put(struct intel_gt *gt)
+{
+	intel_wakeref_might_put(&gt->wakeref);
+}
+
 #define with_intel_gt_pm(gt, tmp) \
 	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
 	     intel_gt_pm_put(gt), tmp = 0)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +33 -3
index dbf919801de2..e0eed70f9b92 100644
@@ -1550,7 +1550,12 @@ static int guc_context_pre_pin(struct intel_context *ce,
 
 static int guc_context_pin(struct intel_context *ce, void *vaddr)
 {
-	return __guc_context_pin(ce, ce->engine, vaddr);
+	int ret = __guc_context_pin(ce, ce->engine, vaddr);
+
+	if (likely(!ret && !intel_context_is_barrier(ce)))
+		intel_engine_pm_get(ce->engine);
+
+	return ret;
 }
 
 static void guc_context_unpin(struct intel_context *ce)
@@ -1559,6 +1564,9 @@ static void guc_context_unpin(struct intel_context *ce)
 
 	unpin_guc_id(guc, ce);
 	lrc_unpin(ce);
+
+	if (likely(!intel_context_is_barrier(ce)))
+		intel_engine_pm_put_async(ce->engine);
 }
 
 static void guc_context_post_unpin(struct intel_context *ce)
@@ -2328,8 +2336,30 @@ static int guc_virtual_context_pre_pin(struct intel_context *ce,
 static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
 {
 	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+	int ret = __guc_context_pin(ce, engine, vaddr);
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+
+	if (likely(!ret))
+		for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+			intel_engine_pm_get(engine);
 
-	return __guc_context_pin(ce, engine, vaddr);
+	return ret;
+}
+
+static void guc_virtual_context_unpin(struct intel_context *ce)
+{
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+	struct intel_engine_cs *engine;
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	GEM_BUG_ON(context_enabled(ce));
+	GEM_BUG_ON(intel_context_is_barrier(ce));
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+
+	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+		intel_engine_pm_put_async(engine);
 }
 
 static void guc_virtual_context_enter(struct intel_context *ce)
@@ -2366,7 +2396,7 @@ static const struct intel_context_ops virtual_guc_context_ops = {
 
 	.pre_pin = guc_virtual_context_pre_pin,
 	.pin = guc_virtual_context_pin,
-	.unpin = guc_context_unpin,
+	.unpin = guc_virtual_context_unpin,
 	.post_unpin = guc_context_post_unpin,
 
 	.ban = guc_context_ban,
drivers/gpu/drm/i915/intel_wakeref.h +12 -0
index ef7e6a698e8a..dd530ae028e0 100644
@@ -124,6 +124,12 @@ enum {
 	__INTEL_WAKEREF_PUT_LAST_BIT__
 };
 
+static inline void
+intel_wakeref_might_get(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
 /**
  * intel_wakeref_put_flags: Release the wakeref
  * @wf: the wakeref
@@ -171,6 +177,12 @@ intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
 			    FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
 }
 
+static inline void
+intel_wakeref_might_put(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
 /**
  * intel_wakeref_lock: Lock the wakeref (mutex)
  * @wf: the wakeref
Signature · 1 line
2.32.0
#22 lore raw

Re: [Intel-gfx] [PATCH 06/27] drm/i915/guc: Take engine PM when a context is pinned with GuC submission

Quoted 2 lines · Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a scheduling of user context could be enabled.

As with earlier PM patch, needs more explanation of what the problem is and why it is only now a problem.

Quoted 48 lines

v2:
(Daniel Vetter)
- Add might_lock annotations to pin / unpin function

---
  drivers/gpu/drm/i915/gt/intel_context.c       |  3 ++
  drivers/gpu/drm/i915/gt/intel_engine_pm.h     | 15 ++++++++
  drivers/gpu/drm/i915/gt/intel_gt_pm.h         | 10 ++++++
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 36 +++++++++++++++++--
  drivers/gpu/drm/i915/intel_wakeref.h          | 12 +++++++
  5 files changed, 73 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index c8595da64ad8..508cfe5770c0 100644
@@ -240,6 +240,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
  	if (err)
  		goto err_post_unpin;
 
+	intel_engine_pm_might_get(ce->engine);
+
  	if (unlikely(intel_context_is_closed(ce))) {
  		err = -ENOENT;
  		goto err_unlock;
@@ -313,6 +315,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
  		return;
 
  	CE_TRACE(ce, "unpin\n");
+	intel_engine_pm_might_put(ce->engine);
  	ce->ops->unpin(ce);
  	ce->ops->post_unpin(ce);
 
drivers/gpu/drm/i915/gt/intel_engine_pm.h +1 -0
index 17a5028ea177..3fe2ae1bcc26 100644
@@ -9,6 +9,7 @@
  #include "i915_request.h"
  #include "intel_engine_types.h"
  #include "intel_wakeref.h"
+#include "intel_gt_pm.h"
 
  static inline bool
  intel_engine_pm_is_awake(const struct intel_engine_cs *engine)

drivers/gpu/drm/i915/gt/intel_engine_pm.h static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine) · line 38

Quoted 7 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.h +4 -0
@@ -31,6 +32,13 @@ static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine)
  	return intel_wakeref_get_if_active(&engine->wakeref);
  }
 
+static inline void intel_engine_pm_might_get(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_get(&engine->wakeref);

Why doesn't this need to iterate through the physical engines of the virtual engine?

John.

Quoted 147 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.h +10 -0
+	intel_gt_pm_might_get(engine->gt);
+}
+
  static inline void intel_engine_pm_put(struct intel_engine_cs *engine)
  {
  	intel_wakeref_put(&engine->wakeref);
@@ -52,6 +60,13 @@ static inline void intel_engine_pm_flush(struct intel_engine_cs *engine)
  	intel_wakeref_unlock_wait(&engine->wakeref);
  }
 
+static inline void intel_engine_pm_might_put(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_put(&engine->wakeref);
+	intel_gt_pm_might_put(engine->gt);
+}
+
  static inline struct i915_request *
  intel_engine_create_kernel_request(struct intel_engine_cs *engine)
  {
drivers/gpu/drm/i915/gt/intel_gt_pm.h +10 -0
index a17bf0d4592b..3c173033ce23 100644
@@ -31,6 +31,11 @@ static inline bool intel_gt_pm_get_if_awake(struct intel_gt *gt)
  	return intel_wakeref_get_if_active(&gt->wakeref);
  }
 
+static inline void intel_gt_pm_might_get(struct intel_gt *gt)
+{
+	intel_wakeref_might_get(&gt->wakeref);
+}
+
  static inline void intel_gt_pm_put(struct intel_gt *gt)
  {
  	intel_wakeref_put(&gt->wakeref);
@@ -41,6 +46,11 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
  	intel_wakeref_put_async(&gt->wakeref);
  }
 
+static inline void intel_gt_pm_might_put(struct intel_gt *gt)
+{
+	intel_wakeref_might_put(&gt->wakeref);
+}
+
  #define with_intel_gt_pm(gt, tmp) \
  	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
  	     intel_gt_pm_put(gt), tmp = 0)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +33 -3
index dbf919801de2..e0eed70f9b92 100644
@@ -1550,7 +1550,12 @@ static int guc_context_pre_pin(struct intel_context *ce,
 
  static int guc_context_pin(struct intel_context *ce, void *vaddr)
  {
-	return __guc_context_pin(ce, ce->engine, vaddr);
+	int ret = __guc_context_pin(ce, ce->engine, vaddr);
+
+	if (likely(!ret && !intel_context_is_barrier(ce)))
+		intel_engine_pm_get(ce->engine);
+
+	return ret;
  }
 
  static void guc_context_unpin(struct intel_context *ce)
@@ -1559,6 +1564,9 @@ static void guc_context_unpin(struct intel_context *ce)
 
  	unpin_guc_id(guc, ce);
  	lrc_unpin(ce);
+
+	if (likely(!intel_context_is_barrier(ce)))
+		intel_engine_pm_put_async(ce->engine);
  }
 
  static void guc_context_post_unpin(struct intel_context *ce)
@@ -2328,8 +2336,30 @@ static int guc_virtual_context_pre_pin(struct intel_context *ce,
  static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
  {
  	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+	int ret = __guc_context_pin(ce, engine, vaddr);
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+
+	if (likely(!ret))
+		for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+			intel_engine_pm_get(engine);
 
-	return __guc_context_pin(ce, engine, vaddr);
+	return ret;
+}
+
+static void guc_virtual_context_unpin(struct intel_context *ce)
+{
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+	struct intel_engine_cs *engine;
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	GEM_BUG_ON(context_enabled(ce));
+	GEM_BUG_ON(intel_context_is_barrier(ce));
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+
+	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+		intel_engine_pm_put_async(engine);
  }
 
  static void guc_virtual_context_enter(struct intel_context *ce)
@@ -2366,7 +2396,7 @@ static const struct intel_context_ops virtual_guc_context_ops = {
 
  	.pre_pin = guc_virtual_context_pre_pin,
  	.pin = guc_virtual_context_pin,
-	.unpin = guc_context_unpin,
+	.unpin = guc_virtual_context_unpin,
  	.post_unpin = guc_context_post_unpin,
 
  	.ban = guc_context_ban,
drivers/gpu/drm/i915/intel_wakeref.h +12 -0
index ef7e6a698e8a..dd530ae028e0 100644
@@ -124,6 +124,12 @@ enum {
  	__INTEL_WAKEREF_PUT_LAST_BIT__
  };
 
+static inline void
+intel_wakeref_might_get(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
  /**
   * intel_wakeref_put_flags: Release the wakeref
   * @wf: the wakeref
@@ -171,6 +177,12 @@ intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
  			    FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
  }
 
+static inline void
+intel_wakeref_might_put(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
  /**
   * intel_wakeref_lock: Lock the wakeref (mutex)
   * @wf: the wakeref
#23 lore raw

Re: [Intel-gfx] [PATCH 06/27] drm/i915/guc: Take engine PM when a context is pinned with GuC submission

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

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a scheduling of user context could be enabled.

As with earlier PM patch, needs more explanation of what the problem is and why it is only now a problem.

Same explaination, will add here.

Quoted 44 lines
Quoted 44 lines

v2:
(Daniel Vetter)
- Add might_lock annotations to pin / unpin function

---
  drivers/gpu/drm/i915/gt/intel_context.c       |  3 ++
  drivers/gpu/drm/i915/gt/intel_engine_pm.h     | 15 ++++++++
  drivers/gpu/drm/i915/gt/intel_gt_pm.h         | 10 ++++++
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 36 +++++++++++++++++--
  drivers/gpu/drm/i915/intel_wakeref.h          | 12 +++++++
  5 files changed, 73 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index c8595da64ad8..508cfe5770c0 100644
@@ -240,6 +240,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
  	if (err)
  		goto err_post_unpin;
+	intel_engine_pm_might_get(ce->engine);
+
  	if (unlikely(intel_context_is_closed(ce))) {
  		err = -ENOENT;
  		goto err_unlock;
@@ -313,6 +315,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
  		return;
  	CE_TRACE(ce, "unpin\n");
+	intel_engine_pm_might_put(ce->engine);
  	ce->ops->unpin(ce);
  	ce->ops->post_unpin(ce);
drivers/gpu/drm/i915/gt/intel_engine_pm.h +1 -0
index 17a5028ea177..3fe2ae1bcc26 100644
@@ -9,6 +9,7 @@
  #include "i915_request.h"
  #include "intel_engine_types.h"
  #include "intel_wakeref.h"
+#include "intel_gt_pm.h"
  static inline bool
  intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
@@ -31,6 +32,13 @@ static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine)
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_pm.h static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine) · line 38

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.h +4 -0
  	return intel_wakeref_get_if_active(&engine->wakeref);
  }
+static inline void intel_engine_pm_might_get(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_get(&engine->wakeref);

Why doesn't this need to iterate through the physical engines of the virtual engine?

Yea, technically it should. This is just an annotation though to check if we do something horribly wrong in our code. If we use any physical engine in our stack this annotation should pop and we can fix it. I just don't see what making this 100% correct for virtual engines buys us. If you want I can fix this but thinking the more complex we make this annotation the less likely it just gets compiled out with lockdep off which is what we are aiming for.

Matt

Quoted 137 lines

John.

Quoted 134 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.h +10 -0
+	intel_gt_pm_might_get(engine->gt);
+}
+
  static inline void intel_engine_pm_put(struct intel_engine_cs *engine)
  {
  	intel_wakeref_put(&engine->wakeref);
@@ -52,6 +60,13 @@ static inline void intel_engine_pm_flush(struct intel_engine_cs *engine)
  	intel_wakeref_unlock_wait(&engine->wakeref);
  }
+static inline void intel_engine_pm_might_put(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_put(&engine->wakeref);
+	intel_gt_pm_might_put(engine->gt);
+}
+
  static inline struct i915_request *
  intel_engine_create_kernel_request(struct intel_engine_cs *engine)
  {
drivers/gpu/drm/i915/gt/intel_gt_pm.h +10 -0
index a17bf0d4592b..3c173033ce23 100644
@@ -31,6 +31,11 @@ static inline bool intel_gt_pm_get_if_awake(struct intel_gt *gt)
  	return intel_wakeref_get_if_active(&gt->wakeref);
  }
+static inline void intel_gt_pm_might_get(struct intel_gt *gt)
+{
+	intel_wakeref_might_get(&gt->wakeref);
+}
+
  static inline void intel_gt_pm_put(struct intel_gt *gt)
  {
  	intel_wakeref_put(&gt->wakeref);
@@ -41,6 +46,11 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
  	intel_wakeref_put_async(&gt->wakeref);
  }
+static inline void intel_gt_pm_might_put(struct intel_gt *gt)
+{
+	intel_wakeref_might_put(&gt->wakeref);
+}
+
  #define with_intel_gt_pm(gt, tmp) \
  	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
  	     intel_gt_pm_put(gt), tmp = 0)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +33 -3
index dbf919801de2..e0eed70f9b92 100644
@@ -1550,7 +1550,12 @@ static int guc_context_pre_pin(struct intel_context *ce,
  static int guc_context_pin(struct intel_context *ce, void *vaddr)
  {
-	return __guc_context_pin(ce, ce->engine, vaddr);
+	int ret = __guc_context_pin(ce, ce->engine, vaddr);
+
+	if (likely(!ret && !intel_context_is_barrier(ce)))
+		intel_engine_pm_get(ce->engine);
+
+	return ret;
  }
  static void guc_context_unpin(struct intel_context *ce)
@@ -1559,6 +1564,9 @@ static void guc_context_unpin(struct intel_context *ce)
  	unpin_guc_id(guc, ce);
  	lrc_unpin(ce);
+
+	if (likely(!intel_context_is_barrier(ce)))
+		intel_engine_pm_put_async(ce->engine);
  }
  static void guc_context_post_unpin(struct intel_context *ce)
@@ -2328,8 +2336,30 @@ static int guc_virtual_context_pre_pin(struct intel_context *ce,
  static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
  {
  	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+	int ret = __guc_context_pin(ce, engine, vaddr);
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+
+	if (likely(!ret))
+		for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+			intel_engine_pm_get(engine);
-	return __guc_context_pin(ce, engine, vaddr);
+	return ret;
+}
+
+static void guc_virtual_context_unpin(struct intel_context *ce)
+{
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+	struct intel_engine_cs *engine;
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	GEM_BUG_ON(context_enabled(ce));
+	GEM_BUG_ON(intel_context_is_barrier(ce));
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+
+	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+		intel_engine_pm_put_async(engine);
  }
  static void guc_virtual_context_enter(struct intel_context *ce)
@@ -2366,7 +2396,7 @@ static const struct intel_context_ops virtual_guc_context_ops = {
  	.pre_pin = guc_virtual_context_pre_pin,
  	.pin = guc_virtual_context_pin,
-	.unpin = guc_context_unpin,
+	.unpin = guc_virtual_context_unpin,
  	.post_unpin = guc_context_post_unpin,
  	.ban = guc_context_ban,
drivers/gpu/drm/i915/intel_wakeref.h +12 -0
index ef7e6a698e8a..dd530ae028e0 100644
@@ -124,6 +124,12 @@ enum {
  	__INTEL_WAKEREF_PUT_LAST_BIT__
  };
+static inline void
+intel_wakeref_might_get(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
  /**
   * intel_wakeref_put_flags: Release the wakeref
   * @wf: the wakeref
@@ -171,6 +177,12 @@ intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
  			    FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
  }
+static inline void
+intel_wakeref_might_put(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
  /**
   * intel_wakeref_lock: Lock the wakeref (mutex)
   * @wf: the wakeref
#24 lore raw

Re: [Intel-gfx] [PATCH 06/27] drm/i915/guc: Take engine PM when a context is pinned with GuC submission

Quoted 62 lines · Matthew Brost
Quoted 7 lines · John Harrison
Quoted 2 lines · Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a scheduling of user context could be enabled.

As with earlier PM patch, needs more explanation of what the problem is and why it is only now a problem.

Same explaination, will add here.

Quoted 43 lines
Quoted 43 lines

v2:
(Daniel Vetter)
- Add might_lock annotations to pin / unpin function

---
   drivers/gpu/drm/i915/gt/intel_context.c       |  3 ++
   drivers/gpu/drm/i915/gt/intel_engine_pm.h     | 15 ++++++++
   drivers/gpu/drm/i915/gt/intel_gt_pm.h         | 10 ++++++
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 36 +++++++++++++++++--
   drivers/gpu/drm/i915/intel_wakeref.h          | 12 +++++++
   5 files changed, 73 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index c8595da64ad8..508cfe5770c0 100644
@@ -240,6 +240,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
   	if (err)
   		goto err_post_unpin;
+	intel_engine_pm_might_get(ce->engine);
+
   	if (unlikely(intel_context_is_closed(ce))) {
   		err = -ENOENT;
   		goto err_unlock;
@@ -313,6 +315,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
   		return;
   	CE_TRACE(ce, "unpin\n");
+	intel_engine_pm_might_put(ce->engine);
   	ce->ops->unpin(ce);
   	ce->ops->post_unpin(ce);
drivers/gpu/drm/i915/gt/intel_engine_pm.h +1 -0
index 17a5028ea177..3fe2ae1bcc26 100644
@@ -9,6 +9,7 @@
   #include "i915_request.h"
   #include "intel_engine_types.h"
   #include "intel_wakeref.h"
+#include "intel_gt_pm.h"
   static inline bool
   intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
@@ -31,6 +32,13 @@ static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine)
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_pm.h static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine) · line 38

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.h +4 -0
   	return intel_wakeref_get_if_active(&engine->wakeref);
   }
+static inline void intel_engine_pm_might_get(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_get(&engine->wakeref);

Why doesn't this need to iterate through the physical engines of the virtual engine?

Quoted 7 lines

Yea, technically it should. This is just an annotation though to check if we do something horribly wrong in our code. If we use any physical engine in our stack this annotation should pop and we can fix it. I just don't see what making this 100% correct for virtual engines buys us. If you want I can fix this but thinking the more complex we make this annotation the less likely it just gets compiled out with lockdep off which is what we are aiming for.

But if the annotation is missing a fundamental lock then it is surely not actually going to do any good? Not sure if you need to iterate over all child engines + parent but it seems like you should be calling might_lock() on at least one engine's mutex to feed the lockdep annotation.

John.

Quoted 138 lines

Matt

Quoted 136 lines

John.

Quoted 134 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.h +10 -0
+	intel_gt_pm_might_get(engine->gt);
+}
+
   static inline void intel_engine_pm_put(struct intel_engine_cs *engine)
   {
   	intel_wakeref_put(&engine->wakeref);
@@ -52,6 +60,13 @@ static inline void intel_engine_pm_flush(struct intel_engine_cs *engine)
   	intel_wakeref_unlock_wait(&engine->wakeref);
   }
+static inline void intel_engine_pm_might_put(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_put(&engine->wakeref);
+	intel_gt_pm_might_put(engine->gt);
+}
+
   static inline struct i915_request *
   intel_engine_create_kernel_request(struct intel_engine_cs *engine)
   {
drivers/gpu/drm/i915/gt/intel_gt_pm.h +10 -0
index a17bf0d4592b..3c173033ce23 100644
@@ -31,6 +31,11 @@ static inline bool intel_gt_pm_get_if_awake(struct intel_gt *gt)
   	return intel_wakeref_get_if_active(&gt->wakeref);
   }
+static inline void intel_gt_pm_might_get(struct intel_gt *gt)
+{
+	intel_wakeref_might_get(&gt->wakeref);
+}
+
   static inline void intel_gt_pm_put(struct intel_gt *gt)
   {
   	intel_wakeref_put(&gt->wakeref);
@@ -41,6 +46,11 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
   	intel_wakeref_put_async(&gt->wakeref);
   }
+static inline void intel_gt_pm_might_put(struct intel_gt *gt)
+{
+	intel_wakeref_might_put(&gt->wakeref);
+}
+
   #define with_intel_gt_pm(gt, tmp) \
   	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
   	     intel_gt_pm_put(gt), tmp = 0)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +33 -3
index dbf919801de2..e0eed70f9b92 100644
@@ -1550,7 +1550,12 @@ static int guc_context_pre_pin(struct intel_context *ce,
   static int guc_context_pin(struct intel_context *ce, void *vaddr)
   {
-	return __guc_context_pin(ce, ce->engine, vaddr);
+	int ret = __guc_context_pin(ce, ce->engine, vaddr);
+
+	if (likely(!ret && !intel_context_is_barrier(ce)))
+		intel_engine_pm_get(ce->engine);
+
+	return ret;
   }
   static void guc_context_unpin(struct intel_context *ce)
@@ -1559,6 +1564,9 @@ static void guc_context_unpin(struct intel_context *ce)
   	unpin_guc_id(guc, ce);
   	lrc_unpin(ce);
+
+	if (likely(!intel_context_is_barrier(ce)))
+		intel_engine_pm_put_async(ce->engine);
   }
   static void guc_context_post_unpin(struct intel_context *ce)
@@ -2328,8 +2336,30 @@ static int guc_virtual_context_pre_pin(struct intel_context *ce,
   static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
   {
   	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+	int ret = __guc_context_pin(ce, engine, vaddr);
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+
+	if (likely(!ret))
+		for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+			intel_engine_pm_get(engine);
-	return __guc_context_pin(ce, engine, vaddr);
+	return ret;
+}
+
+static void guc_virtual_context_unpin(struct intel_context *ce)
+{
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+	struct intel_engine_cs *engine;
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	GEM_BUG_ON(context_enabled(ce));
+	GEM_BUG_ON(intel_context_is_barrier(ce));
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+
+	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+		intel_engine_pm_put_async(engine);
   }
   static void guc_virtual_context_enter(struct intel_context *ce)
@@ -2366,7 +2396,7 @@ static const struct intel_context_ops virtual_guc_context_ops = {
   	.pre_pin = guc_virtual_context_pre_pin,
   	.pin = guc_virtual_context_pin,
-	.unpin = guc_context_unpin,
+	.unpin = guc_virtual_context_unpin,
   	.post_unpin = guc_context_post_unpin,
   	.ban = guc_context_ban,
drivers/gpu/drm/i915/intel_wakeref.h +12 -0
index ef7e6a698e8a..dd530ae028e0 100644
@@ -124,6 +124,12 @@ enum {
   	__INTEL_WAKEREF_PUT_LAST_BIT__
   };
+static inline void
+intel_wakeref_might_get(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
   /**
    * intel_wakeref_put_flags: Release the wakeref
    * @wf: the wakeref
@@ -171,6 +177,12 @@ intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
   			    FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
   }
+static inline void
+intel_wakeref_might_put(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
   /**
    * intel_wakeref_lock: Lock the wakeref (mutex)
    * @wf: the wakeref
#25 lore raw

Re: [Intel-gfx] [PATCH 06/27] drm/i915/guc: Take engine PM when a context is pinned with GuC submission

Quoted 70 lines · John Harrison
Quoted 62 lines · Matthew Brost
Quoted 7 lines · John Harrison
Quoted 2 lines · Matthew Brost

Taking a PM reference to prevent intel_gt_wait_for_idle from short circuiting while a scheduling of user context could be enabled.

As with earlier PM patch, needs more explanation of what the problem is and why it is only now a problem.

Same explaination, will add here.

Quoted 43 lines
Quoted 43 lines

v2:
(Daniel Vetter)
- Add might_lock annotations to pin / unpin function

---
   drivers/gpu/drm/i915/gt/intel_context.c       |  3 ++
   drivers/gpu/drm/i915/gt/intel_engine_pm.h     | 15 ++++++++
   drivers/gpu/drm/i915/gt/intel_gt_pm.h         | 10 ++++++
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 36 +++++++++++++++++--
   drivers/gpu/drm/i915/intel_wakeref.h          | 12 +++++++
   5 files changed, 73 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +3 -0
index c8595da64ad8..508cfe5770c0 100644
@@ -240,6 +240,8 @@ int __intel_context_do_pin_ww(struct intel_context *ce,
   	if (err)
   		goto err_post_unpin;
+	intel_engine_pm_might_get(ce->engine);
+
   	if (unlikely(intel_context_is_closed(ce))) {
   		err = -ENOENT;
   		goto err_unlock;
@@ -313,6 +315,7 @@ void __intel_context_do_unpin(struct intel_context *ce, int sub)
   		return;
   	CE_TRACE(ce, "unpin\n");
+	intel_engine_pm_might_put(ce->engine);
   	ce->ops->unpin(ce);
   	ce->ops->post_unpin(ce);
drivers/gpu/drm/i915/gt/intel_engine_pm.h +1 -0
index 17a5028ea177..3fe2ae1bcc26 100644
@@ -9,6 +9,7 @@
   #include "i915_request.h"
   #include "intel_engine_types.h"
   #include "intel_wakeref.h"
+#include "intel_gt_pm.h"
   static inline bool
   intel_engine_pm_is_awake(const struct intel_engine_cs *engine)
@@ -31,6 +32,13 @@ static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine)
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_pm.h static inline bool intel_engine_pm_get_if_awake(struct intel_engine_cs *engine) · line 38

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.h +4 -0
   	return intel_wakeref_get_if_active(&engine->wakeref);
   }
+static inline void intel_engine_pm_might_get(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_get(&engine->wakeref);

Why doesn't this need to iterate through the physical engines of the virtual engine?

Quoted 7 lines

Yea, technically it should. This is just an annotation though to check if we do something horribly wrong in our code. If we use any physical engine in our stack this annotation should pop and we can fix it. I just don't see what making this 100% correct for virtual engines buys us. If you want I can fix this but thinking the more complex we make this annotation the less likely it just gets compiled out with lockdep off which is what we are aiming for.

Quoted 5 lines

But if the annotation is missing a fundamental lock then it is surely not actually going to do any good? Not sure if you need to iterate over all child engines + parent but it seems like you should be calling might_lock() on at least one engine's mutex to feed the lockdep annotation.

These are all inline functions so the compiler to be able to compile this out if lockdep is disabled. I'll fix this in the next rev.

Matt

Quoted 141 lines

John.

Quoted 138 lines

Matt

Quoted 136 lines

John.

Quoted 134 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.h +10 -0
+	intel_gt_pm_might_get(engine->gt);
+}
+
   static inline void intel_engine_pm_put(struct intel_engine_cs *engine)
   {
   	intel_wakeref_put(&engine->wakeref);
@@ -52,6 +60,13 @@ static inline void intel_engine_pm_flush(struct intel_engine_cs *engine)
   	intel_wakeref_unlock_wait(&engine->wakeref);
   }
+static inline void intel_engine_pm_might_put(struct intel_engine_cs *engine)
+{
+	if (!intel_engine_is_virtual(engine))
+		intel_wakeref_might_put(&engine->wakeref);
+	intel_gt_pm_might_put(engine->gt);
+}
+
   static inline struct i915_request *
   intel_engine_create_kernel_request(struct intel_engine_cs *engine)
   {
drivers/gpu/drm/i915/gt/intel_gt_pm.h +10 -0
index a17bf0d4592b..3c173033ce23 100644
@@ -31,6 +31,11 @@ static inline bool intel_gt_pm_get_if_awake(struct intel_gt *gt)
   	return intel_wakeref_get_if_active(&gt->wakeref);
   }
+static inline void intel_gt_pm_might_get(struct intel_gt *gt)
+{
+	intel_wakeref_might_get(&gt->wakeref);
+}
+
   static inline void intel_gt_pm_put(struct intel_gt *gt)
   {
   	intel_wakeref_put(&gt->wakeref);
@@ -41,6 +46,11 @@ static inline void intel_gt_pm_put_async(struct intel_gt *gt)
   	intel_wakeref_put_async(&gt->wakeref);
   }
+static inline void intel_gt_pm_might_put(struct intel_gt *gt)
+{
+	intel_wakeref_might_put(&gt->wakeref);
+}
+
   #define with_intel_gt_pm(gt, tmp) \
   	for (tmp = 1, intel_gt_pm_get(gt); tmp; \
   	     intel_gt_pm_put(gt), tmp = 0)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +33 -3
index dbf919801de2..e0eed70f9b92 100644
@@ -1550,7 +1550,12 @@ static int guc_context_pre_pin(struct intel_context *ce,
   static int guc_context_pin(struct intel_context *ce, void *vaddr)
   {
-	return __guc_context_pin(ce, ce->engine, vaddr);
+	int ret = __guc_context_pin(ce, ce->engine, vaddr);
+
+	if (likely(!ret && !intel_context_is_barrier(ce)))
+		intel_engine_pm_get(ce->engine);
+
+	return ret;
   }
   static void guc_context_unpin(struct intel_context *ce)
@@ -1559,6 +1564,9 @@ static void guc_context_unpin(struct intel_context *ce)
   	unpin_guc_id(guc, ce);
   	lrc_unpin(ce);
+
+	if (likely(!intel_context_is_barrier(ce)))
+		intel_engine_pm_put_async(ce->engine);
   }
   static void guc_context_post_unpin(struct intel_context *ce)
@@ -2328,8 +2336,30 @@ static int guc_virtual_context_pre_pin(struct intel_context *ce,
   static int guc_virtual_context_pin(struct intel_context *ce, void *vaddr)
   {
   	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+	int ret = __guc_context_pin(ce, engine, vaddr);
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+
+	if (likely(!ret))
+		for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+			intel_engine_pm_get(engine);
-	return __guc_context_pin(ce, engine, vaddr);
+	return ret;
+}
+
+static void guc_virtual_context_unpin(struct intel_context *ce)
+{
+	intel_engine_mask_t tmp, mask = ce->engine->mask;
+	struct intel_engine_cs *engine;
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	GEM_BUG_ON(context_enabled(ce));
+	GEM_BUG_ON(intel_context_is_barrier(ce));
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+
+	for_each_engine_masked(engine, ce->engine->gt, mask, tmp)
+		intel_engine_pm_put_async(engine);
   }
   static void guc_virtual_context_enter(struct intel_context *ce)
@@ -2366,7 +2396,7 @@ static const struct intel_context_ops virtual_guc_context_ops = {
   	.pre_pin = guc_virtual_context_pre_pin,
   	.pin = guc_virtual_context_pin,
-	.unpin = guc_context_unpin,
+	.unpin = guc_virtual_context_unpin,
   	.post_unpin = guc_context_post_unpin,
   	.ban = guc_context_ban,
drivers/gpu/drm/i915/intel_wakeref.h +12 -0
index ef7e6a698e8a..dd530ae028e0 100644
@@ -124,6 +124,12 @@ enum {
   	__INTEL_WAKEREF_PUT_LAST_BIT__
   };
+static inline void
+intel_wakeref_might_get(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
   /**
    * intel_wakeref_put_flags: Release the wakeref
    * @wf: the wakeref
@@ -171,6 +177,12 @@ intel_wakeref_put_delay(struct intel_wakeref *wf, unsigned long delay)
   			    FIELD_PREP(INTEL_WAKEREF_PUT_DELAY, delay));
   }
+static inline void
+intel_wakeref_might_put(struct intel_wakeref *wf)
+{
+	might_lock(&wf->mutex);
+}
+
   /**
    * intel_wakeref_lock: Lock the wakeref (mutex)
    * @wf: the wakeref
#26 lore raw

PATCH 7/27 drm/i915/guc: Don't call switch_to_kernel_context with GuC submission

In reply to Matthew Brost

Calling switch_to_kernel_context isn't needed if the engine PM reference is taken while all contexts are pinned. By not calling switch_to_kernel_context we save on issuing a request to the engine.

v2:
(Daniel Vetter)
- Add FIXME comment about pushing switch_to_kernel_context to backend

---
 drivers/gpu/drm/i915/gt/intel_engine_pm.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/gpu/drm/i915/gt/intel_engine_pm.c +9 -0
index 1f07ac4e0672..11fee66daf60 100644
@@ -162,6 +162,15 @@ static bool switch_to_kernel_context(struct intel_engine_cs *engine)
 	unsigned long flags;
 	bool result = true;
 
+	/*
+	 * No need to switch_to_kernel_context if GuC submission
+	 *
+	 * FIXME: This execlists specific backend behavior in generic code, this
+	 * should be pushed to the backend.
+	 */
+	if (intel_engine_uses_guc(engine))
+		return true;
+
 	/* GPU is pointing to the void, as good as in the kernel context. */
 	if (intel_gt_is_wedged(engine->gt))
 		return true;
Signature · 1 line
2.32.0
#27 lore raw

Re: [Intel-gfx] [PATCH 07/27] drm/i915/guc: Don't call switch_to_kernel_context with GuC submission

Quoted 3 lines · Matthew Brost

Calling switch_to_kernel_context isn't needed if the engine PM reference is taken while all contexts are pinned. By not calling switch_to_kernel_context we save on issuing a request to the engine.

I thought the intention of the switch_to_kernel was to ensure that the GPU is not touching any user context and is basically idle. That is not a valid assumption with an external scheduler such as GuC. So why is the description above only mentioning PM references? What is the connection between the PM ref and the switch_to_kernel?

Also, the comment in the code does not mention anything about PM references, it just says 'not necessary with GuC' but no explanation at all.

Quoted 15 lines

v2:
(Daniel Vetter)
- Add FIXME comment about pushing switch_to_kernel_context to backend

---
  drivers/gpu/drm/i915/gt/intel_engine_pm.c | 9 +++++++++
  1 file changed, 9 insertions(+)
drivers/gpu/drm/i915/gt/intel_engine_pm.c +0 -0
index 1f07ac4e0672..11fee66daf60 100644

drivers/gpu/drm/i915/gt/intel_engine_pm.c static bool switch_to_kernel_context(struct intel_engine_cs *engine) · line 168

Quoted 7 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.c +4 -0
@@ -162,6 +162,15 @@ static bool switch_to_kernel_context(struct intel_engine_cs *engine)
  	unsigned long flags;
  	bool result = true;
 
+	/*
+	 * No need to switch_to_kernel_context if GuC submission
+	 *
+	 * FIXME: This execlists specific backend behavior in generic code, this

"This execlists" -> "This is execlist"

"this should be" -> "it should be"

John.

Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.c +5 -0
+	 * should be pushed to the backend.
+	 */
+	if (intel_engine_uses_guc(engine))
+		return true;
+
  	/* GPU is pointing to the void, as good as in the kernel context. */
  	if (intel_gt_is_wedged(engine->gt))
  		return true;
#28 lore raw

Re: [Intel-gfx] [PATCH 07/27] drm/i915/guc: Don't call switch_to_kernel_context with GuC submission

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

Calling switch_to_kernel_context isn't needed if the engine PM reference is taken while all contexts are pinned. By not calling switch_to_kernel_context we save on issuing a request to the engine.

Quoted 9 lines

I thought the intention of the switch_to_kernel was to ensure that the GPU is not touching any user context and is basically idle. That is not a valid assumption with an external scheduler such as GuC. So why is the description above only mentioning PM references? What is the connection between the PM ref and the switch_to_kernel?

Also, the comment in the code does not mention anything about PM references, it just says 'not necessary with GuC' but no explanation at all.

Yea, this need to be explained better. How about this?

Calling switch_to_kernel_context isn't needed if the engine PM reference is take while all user contexts have scheduling enabled. Once scheduling is disabled on all user contexts the GuC is guaranteed to not touch any user context state which is effectively the same pointing to a kernel context.

Matt

Quoted 37 lines
Quoted 16 lines

v2:
(Daniel Vetter)
- Add FIXME comment about pushing switch_to_kernel_context to backend

---
  drivers/gpu/drm/i915/gt/intel_engine_pm.c | 9 +++++++++
  1 file changed, 9 insertions(+)
drivers/gpu/drm/i915/gt/intel_engine_pm.c +0 -0
index 1f07ac4e0672..11fee66daf60 100644

drivers/gpu/drm/i915/gt/intel_engine_pm.c static bool switch_to_kernel_context(struct intel_engine_cs *engine) · line 167

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.c +4 -0
@@ -162,6 +162,15 @@ static bool switch_to_kernel_context(struct intel_engine_cs *engine)
  	unsigned long flags;
  	bool result = true;
+	/*
+	 * No need to switch_to_kernel_context if GuC submission
+	 *
+	 * FIXME: This execlists specific backend behavior in generic code, this

"This execlists" -> "This is execlist"

"this should be" -> "it should be"

John.

Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.c +5 -0
+	 * should be pushed to the backend.
+	 */
+	if (intel_engine_uses_guc(engine))
+		return true;
+
  	/* GPU is pointing to the void, as good as in the kernel context. */
  	if (intel_gt_is_wedged(engine->gt))
  		return true;
#29 lore raw

Re: [Intel-gfx] [PATCH 07/27] drm/i915/guc: Don't call switch_to_kernel_context with GuC submission

Quoted 16 lines · Matthew Brost
Quoted 4 lines · John Harrison
Quoted 3 lines · Matthew Brost

Calling switch_to_kernel_context isn't needed if the engine PM reference is taken while all contexts are pinned. By not calling switch_to_kernel_context we save on issuing a request to the engine.

Quoted 9 lines

I thought the intention of the switch_to_kernel was to ensure that the GPU is not touching any user context and is basically idle. That is not a valid assumption with an external scheduler such as GuC. So why is the description above only mentioning PM references? What is the connection between the PM ref and the switch_to_kernel?

Also, the comment in the code does not mention anything about PM references, it just says 'not necessary with GuC' but no explanation at all.

Yea, this need to be explained better. How about this?

Quoted 7 lines

Calling switch_to_kernel_context isn't needed if the engine PM reference is take while all user contexts have scheduling enabled. Once scheduling is disabled on all user contexts the GuC is guaranteed to not touch any user context state which is effectively the same pointing to a kernel context.

Matt

I'm still not seeing how the PM reference is involved?

Also, IMHO the focus is wrong in the above text. The fundamental requirement is the ensure the hardware is idle. Execlist achieves this by switching to a safe context. GuC achieves it by disabling scheduling. Indeed, switching to a 'safe' context really has no effect with GuC submission. So 'effectively the same as pointing to a kernel context' is an incorrect description. I would go with something like:

"This is execlist specific behaviour intended to ensure the GPU is idle by switching to a known 'safe' context. With GuC submission, the same idle guarantee is achieved by other means (disabling scheduling). Further, switching to a 'safe' context has no effect with GuC submission as the scheduler can just switch back again.
FIXME: Move this backend scheduler specific behaviour into the scheduler backend."

John.

Quoted 36 lines
Quoted 36 lines
Quoted 16 lines

v2:
(Daniel Vetter)
- Add FIXME comment about pushing switch_to_kernel_context to backend

---
   drivers/gpu/drm/i915/gt/intel_engine_pm.c | 9 +++++++++
   1 file changed, 9 insertions(+)
drivers/gpu/drm/i915/gt/intel_engine_pm.c +0 -0
index 1f07ac4e0672..11fee66daf60 100644

drivers/gpu/drm/i915/gt/intel_engine_pm.c static bool switch_to_kernel_context(struct intel_engine_cs *engine) · line 167

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.c +4 -0
@@ -162,6 +162,15 @@ static bool switch_to_kernel_context(struct intel_engine_cs *engine)
   	unsigned long flags;
   	bool result = true;
+	/*
+	 * No need to switch_to_kernel_context if GuC submission
+	 *
+	 * FIXME: This execlists specific backend behavior in generic code, this

"This execlists" -> "This is execlist"

"this should be" -> "it should be"

John.

Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.c +5 -0
+	 * should be pushed to the backend.
+	 */
+	if (intel_engine_uses_guc(engine))
+		return true;
+
   	/* GPU is pointing to the void, as good as in the kernel context. */
   	if (intel_gt_is_wedged(engine->gt))
   		return true;
#30 lore raw

Re: [Intel-gfx] [PATCH 07/27] drm/i915/guc: Don't call switch_to_kernel_context with GuC submission

Quoted 28 lines · John Harrison

On 9/13/2021 09:54, Matthew Brost wrote:

On Thu, Sep 09, 2021 at 03:51:27PM -0700, John Harrison wrote:

On 8/20/2021 15:44, Matthew Brost wrote:

Calling switch_to_kernel_context isn't needed if the engine PM reference is taken while all contexts are pinned. By not calling switch_to_kernel_context we save on issuing a request to the engine.

I thought the intention of the switch_to_kernel was to ensure that the GPU is not touching any user context and is basically idle. That is not a valid assumption with an external scheduler such as GuC. So why is the description above only mentioning PM references? What is the connection between the PM ref and the switch_to_kernel?

Also, the comment in the code does not mention anything about PM references, it just says 'not necessary with GuC' but no explanation at all.

Yea, this need to be explained better. How about this?

Calling switch_to_kernel_context isn't needed if the engine PM reference is take while all user contexts have scheduling enabled. Once scheduling is disabled on all user contexts the GuC is guaranteed to not touch any user context state which is effectively the same pointing to a kernel context.

Quoted 4 lines

Matt

I'm still not seeing how the PM reference is involved?

We shouldn't trap into the GT PM park code while a user context has scheduling enabled as the GT PM park code may have side affects we don't to execute if a user context still has scheduling enabled. I guess that isn't explained very well.

Quoted 7 lines

Also, IMHO the focus is wrong in the above text. The fundamental requirement is the ensure the hardware is idle. Execlist achieves this by switching to a safe context. GuC achieves it by disabling scheduling. Indeed, switching to a 'safe' context really has no effect with GuC submission. So 'effectively the same as pointing to a kernel context' is an incorrect description. I would go with something like:

Quoted 8 lines

"This is execlist specific behaviour intended to ensure the GPU is idle by switching to a known 'safe' context. With GuC submission, the same idle guarantee is achieved by other means (disabling scheduling). Further, switching to a 'safe' context has no effect with GuC submission as the scheduler can just switch back again.
FIXME: Move this backend scheduler specific behaviour into the scheduler backend."

That is worded better. Will pull into the next rev.

Matt

Quoted 47 lines

John.

            v2:
              (Daniel Vetter)
               - Add FIXME comment about pushing switch_to_kernel_context to backend

            Signed-off-by: Matthew Brost <[email protected]>
            Reviewed-by: Daniel Vetter <[email protected]>
            ---
              drivers/gpu/drm/i915/gt/intel_engine_pm.c | 9 +++++++++
              1 file changed, 9 insertions(+)

            diff --git a/drivers/gpu/drm/i915/gt/intel_engine_pm.c b/drivers/gpu/drm/i915/gt/intel_engine_pm.c
            index 1f07ac4e0672..11fee66daf60 100644
            --- a/drivers/gpu/drm/i915/gt/intel_engine_pm.c
            +++ b/drivers/gpu/drm/i915/gt/intel_engine_pm.c
            @@ -162,6 +162,15 @@ static bool switch_to_kernel_context(struct intel_engine_cs *engine)
                    unsigned long flags;
                    bool result = true;
            +       /*
            +        * No need to switch_to_kernel_context if GuC submission
            +        *
            +        * FIXME: This execlists specific backend behavior in generic code, this

        "This execlists" -> "This is execlist"

        "this should be" -> "it should be"

        John.


            +        * should be pushed to the backend.
            +        */
            +       if (intel_engine_uses_guc(engine))
            +               return true;
            +
                    /* GPU is pointing to the void, as good as in the kernel context. */
                    if (intel_gt_is_wedged(engine->gt))
                            return true;

SECURITY NOTE: file ~/.netrc must not be accessible by others

#31 lore raw

Re: [Intel-gfx] [PATCH 07/27] drm/i915/guc: Don't call switch_to_kernel_context with GuC submission

In reply to John Harrison
Quoted 37 lines · John Harrison
Quoted 3 lines · Matthew Brost

Calling switch_to_kernel_context isn't needed if the engine PM reference is taken while all contexts are pinned. By not calling switch_to_kernel_context we save on issuing a request to the engine.

I thought the intention of the switch_to_kernel was to ensure that the GPU is not touching any user context and is basically idle. That is not a valid assumption with an external scheduler such as GuC. So why is the description above only mentioning PM references? What is the connection between the PM ref and the switch_to_kernel?

Also, the comment in the code does not mention anything about PM references, it just says 'not necessary with GuC' but no explanation at all.

Quoted 15 lines

v2:
(Daniel Vetter)
- Add FIXME comment about pushing switch_to_kernel_context to backend

---
  drivers/gpu/drm/i915/gt/intel_engine_pm.c | 9 +++++++++
  1 file changed, 9 insertions(+)
drivers/gpu/drm/i915/gt/intel_engine_pm.c +0 -0
index 1f07ac4e0672..11fee66daf60 100644

drivers/gpu/drm/i915/gt/intel_engine_pm.c static bool switch_to_kernel_context(struct intel_engine_cs *engine) · line 167

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.c +4 -0
@@ -162,6 +162,15 @@ static bool switch_to_kernel_context(struct intel_engine_cs *engine)
  	unsigned long flags;
  	bool result = true;
+	/*
+	 * No need to switch_to_kernel_context if GuC submission
+	 *
+	 * FIXME: This execlists specific backend behavior in generic code, this

"This execlists" -> "This is execlist"

Quoted 2 lines

"this should be" -> "it should be"

Missed this. Will fix in next rev.

Matt

Quoted 11 lines

John.

Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_engine_pm.c +5 -0
+	 * should be pushed to the backend.
+	 */
+	if (intel_engine_uses_guc(engine))
+		return true;
+
  	/* GPU is pointing to the void, as good as in the kernel context. */
  	if (intel_gt_is_wedged(engine->gt))
  		return true;
#32 lore raw

PATCH 8/27 drm/i915: Add logical engine mapping

In reply to Matthew Brost

Add logical engine mapping. This is required for split-frame, as workloads need to be placed on engines in a logically contiguous manner.

v2:
(Daniel Vetter)
- Add kernel doc for new fields

---
 drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 60 ++++++++++++++++---
 drivers/gpu/drm/i915/gt/intel_engine_types.h  |  5 ++
 .../drm/i915/gt/intel_execlists_submission.c  |  1 +
 drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |  2 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 21 +------
 5 files changed, 60 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_cs.c +51 -9
index 0d9105a31d84..4d790f9a65dd 100644
@@ -290,7 +290,8 @@ static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
 	GEM_DEBUG_WARN_ON(iir);
 }
 
-static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
+static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
+			      u8 logical_instance)
 {
 	const struct engine_info *info = &intel_engines[id];
 	struct drm_i915_private *i915 = gt->i915;
@@ -334,6 +335,7 @@ static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
 
 	engine->class = info->class;
 	engine->instance = info->instance;
+	engine->logical_mask = BIT(logical_instance);
 	__sprint_engine_name(engine);
 
 	engine->props.heartbeat_interval_ms =
@@ -572,6 +574,37 @@ static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
 	return info->engine_mask;
 }
 
+static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
+				 u8 class, const u8 *map, u8 num_instances)
+{
+	int i, j;
+	u8 current_logical_id = 0;
+
+	for (j = 0; j < num_instances; ++j) {
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			if (!HAS_ENGINE(gt, i) ||
+			    intel_engines[i].class != class)
+				continue;
+
+			if (intel_engines[i].instance == map[j]) {
+				logical_ids[intel_engines[i].instance] =
+					current_logical_id++;
+				break;
+			}
+		}
+	}
+}
+
+static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
+{
+	int i;
+	u8 map[MAX_ENGINE_INSTANCE + 1];
+
+	for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
+		map[i] = i;
+	populate_logical_ids(gt, logical_ids, class, map, ARRAY_SIZE(map));
+}
+
 /**
  * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
  * @gt: pointer to struct intel_gt
@@ -583,7 +616,8 @@ int intel_engines_init_mmio(struct intel_gt *gt)
 	struct drm_i915_private *i915 = gt->i915;
 	const unsigned int engine_mask = init_engine_mask(gt);
 	unsigned int mask = 0;
-	unsigned int i;
+	unsigned int i, class;
+	u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
 	int err;
 
 	drm_WARN_ON(&i915->drm, engine_mask == 0);
@@ -593,15 +627,23 @@ int intel_engines_init_mmio(struct intel_gt *gt)
 	if (i915_inject_probe_failure(i915))
 		return -ENODEV;
 
-	for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
-		if (!HAS_ENGINE(gt, i))
-			continue;
+	for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
+		setup_logical_ids(gt, logical_ids, class);
 
-		err = intel_engine_setup(gt, i);
-		if (err)
-			goto cleanup;
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			u8 instance = intel_engines[i].instance;
+
+			if (intel_engines[i].class != class ||
+			    !HAS_ENGINE(gt, i))
+				continue;
 
-		mask |= BIT(i);
+			err = intel_engine_setup(gt, i,
+						 logical_ids[instance]);
+			if (err)
+				goto cleanup;
+
+			mask |= BIT(i);
+		}
 	}
 
 	/*
drivers/gpu/drm/i915/gt/intel_engine_types.h +5 -0
index ed91bcff20eb..fddf35546b58 100644
@@ -266,6 +266,11 @@ struct intel_engine_cs {
 	unsigned int guc_id;
 
 	intel_engine_mask_t mask;
+	/**
+	 * @logical_mask: logical mask of engine, reported to user space via
+	 * query IOCTL and used to communicate with the GuC in logical space
+	 */
+	intel_engine_mask_t logical_mask;
 
 	u8 class;
 	u8 instance;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -0
index cafb0608ffb4..813a6de01382 100644
@@ -3875,6 +3875,7 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
 
 		ve->siblings[ve->num_siblings++] = sibling;
 		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
 
 		/*
 		 * All physical engines must be compatible for their emission
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c +1 -1
index 6926919bcac6..9f5f43a16182 100644
@@ -176,7 +176,7 @@ static void guc_mapping_table_init(struct intel_gt *gt,
 	for_each_engine(engine, gt, id) {
 		u8 guc_class = engine_class_to_guc_class(engine->class);
 
-		system_info->mapping_table[guc_class][engine->instance] =
+		system_info->mapping_table[guc_class][ilog2(engine->logical_mask)] =
 			engine->instance;
 	}
 }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -19
index e0eed70f9b92..ffafbac7335e 100644
@@ -1401,23 +1401,6 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
 	return __guc_action_deregister_context(guc, guc_id, loop);
 }
 
-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:
-		MISSING_CASE(class);
-		return 0;
-	}
-}
-
 static void guc_context_policy_init(struct intel_engine_cs *engine,
 				    struct guc_lrc_desc *desc)
 {
@@ -1459,8 +1442,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
 
 	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->engine_submit_mask = engine->logical_mask;
 	desc->hw_context_desc = ce->lrc.lrca;
 	desc->priority = ce->guc_state.prio;
 	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
@@ -3260,6 +3242,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
 		}
 
 		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
 
 		if (n != 0 && ve->base.class != sibling->class) {
 			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
Signature · 1 line
2.32.0
#33 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 60 lines · Matthew Brost

Add logical engine mapping. This is required for split-frame, as workloads need to be placed on engines in a logically contiguous manner.

v2:
(Daniel Vetter)
- Add kernel doc for new fields

---
  drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 60 ++++++++++++++++---
  drivers/gpu/drm/i915/gt/intel_engine_types.h  |  5 ++
  .../drm/i915/gt/intel_execlists_submission.c  |  1 +
  drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |  2 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 21 +------
  5 files changed, 60 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_cs.c +21 -1
index 0d9105a31d84..4d790f9a65dd 100644
@@ -290,7 +290,8 @@ static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
  	GEM_DEBUG_WARN_ON(iir);
  }
 
-static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
+static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
+			      u8 logical_instance)
  {
  	const struct engine_info *info = &intel_engines[id];
  	struct drm_i915_private *i915 = gt->i915;
@@ -334,6 +335,7 @@ static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
 
  	engine->class = info->class;
  	engine->instance = info->instance;
+	engine->logical_mask = BIT(logical_instance);
  	__sprint_engine_name(engine);
 
  	engine->props.heartbeat_interval_ms =
@@ -572,6 +574,37 @@ static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
  	return info->engine_mask;
  }
 
+static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
+				 u8 class, const u8 *map, u8 num_instances)
+{
+	int i, j;
+	u8 current_logical_id = 0;
+
+	for (j = 0; j < num_instances; ++j) {
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			if (!HAS_ENGINE(gt, i) ||
+			    intel_engines[i].class != class)
+				continue;
+
+			if (intel_engines[i].instance == map[j]) {
+				logical_ids[intel_engines[i].instance] =
+					current_logical_id++;
+				break;
+			}
+		}

drivers/gpu/drm/i915/gt/intel_engine_cs.c static intel_engine_mask_t init_engine_mask(struct intel_gt *gt) · line 604

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +10 -0
+	}
+}
+
+static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
+{
+	int i;
+	u8 map[MAX_ENGINE_INSTANCE + 1];
+
+	for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
+		map[i] = i;

What's the point of the map array since it is 1:1 with instance?

Quoted 33 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +11 -7
+	populate_logical_ids(gt, logical_ids, class, map, ARRAY_SIZE(map));
+}
+
  /**
   * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
   * @gt: pointer to struct intel_gt
@@ -583,7 +616,8 @@ int intel_engines_init_mmio(struct intel_gt *gt)
  	struct drm_i915_private *i915 = gt->i915;
  	const unsigned int engine_mask = init_engine_mask(gt);
  	unsigned int mask = 0;
-	unsigned int i;
+	unsigned int i, class;
+	u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
  	int err;
 
  	drm_WARN_ON(&i915->drm, engine_mask == 0);
@@ -593,15 +627,23 @@ int intel_engines_init_mmio(struct intel_gt *gt)
  	if (i915_inject_probe_failure(i915))
  		return -ENODEV;
 
-	for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
-		if (!HAS_ENGINE(gt, i))
-			continue;
+	for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
+		setup_logical_ids(gt, logical_ids, class);
 
-		err = intel_engine_setup(gt, i);
-		if (err)
-			goto cleanup;
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			u8 instance = intel_engines[i].instance;
+
+			if (intel_engines[i].class != class ||

drivers/gpu/drm/i915/gt/intel_engine_cs.c int intel_engines_init_mmio(struct intel_gt *gt) · line 645

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +8 -1
+			    !HAS_ENGINE(gt, i))
+				continue;
 
-		mask |= BIT(i);
+			err = intel_engine_setup(gt, i,
+						 logical_ids[instance]);
+			if (err)
+				goto cleanup;
+
+			mask |= BIT(i);

I still this there is a less clunky way to set this up in less code and more readable at the same time. Like do it in two passes so you can iterate gt->engine_class[] array instead of having to implement a skip condition (both on class and HAS_ENGINE at two places) and also avoid walking the flat intel_engines array recursively.

Quoted 9 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +1 -0
+		}
  	}
 
  	/*
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
index ed91bcff20eb..fddf35546b58 100644

drivers/gpu/drm/i915/gt/intel_engine_types.h struct intel_engine_cs { · line 273

Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +5 -0
@@ -266,6 +266,11 @@ struct intel_engine_cs {
  	unsigned int guc_id;
 
  	intel_engine_mask_t mask;
+	/**
+	 * @logical_mask: logical mask of engine, reported to user space via
+	 * query IOCTL and used to communicate with the GuC in logical space
+	 */
+	intel_engine_mask_t logical_mask;

You could prefix the new field with uabi_ to match the existing scheme and to signify to anyone who might be touching it in the future it should not be changed.

Also, I think comment should explain what is logical space ie. how the numbering works.

Not sure the part about GuC needs to be in the comment since uapi is supposed to be backend agnostic.

Regards,

Tvrtko

Quoted 75 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
  	u8 class;
  	u8 instance;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -0
index cafb0608ffb4..813a6de01382 100644
@@ -3875,6 +3875,7 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
 
  		ve->siblings[ve->num_siblings++] = sibling;
  		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
 
  		/*
  		 * All physical engines must be compatible for their emission
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c +1 -1
index 6926919bcac6..9f5f43a16182 100644
@@ -176,7 +176,7 @@ static void guc_mapping_table_init(struct intel_gt *gt,
  	for_each_engine(engine, gt, id) {
  		u8 guc_class = engine_class_to_guc_class(engine->class);
 
-		system_info->mapping_table[guc_class][engine->instance] =
+		system_info->mapping_table[guc_class][ilog2(engine->logical_mask)] =
  			engine->instance;
  	}
  }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -19
index e0eed70f9b92..ffafbac7335e 100644
@@ -1401,23 +1401,6 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
  	return __guc_action_deregister_context(guc, guc_id, loop);
  }
 
-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:
-		MISSING_CASE(class);
-		return 0;
-	}
-}
-
  static void guc_context_policy_init(struct intel_engine_cs *engine,
  				    struct guc_lrc_desc *desc)
  {
@@ -1459,8 +1442,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
 
  	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->engine_submit_mask = engine->logical_mask;
  	desc->hw_context_desc = ce->lrc.lrca;
  	desc->priority = ce->guc_state.prio;
  	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
@@ -3260,6 +3242,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  		}
 
  		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
 
  		if (n != 0 && ve->base.class != sibling->class) {
  			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
#34 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 62 lines · Tvrtko Ursulin
Quoted 60 lines · Matthew Brost

Add logical engine mapping. This is required for split-frame, as workloads need to be placed on engines in a logically contiguous manner.

v2:
(Daniel Vetter)
- Add kernel doc for new fields

---
  drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 60 ++++++++++++++++---
  drivers/gpu/drm/i915/gt/intel_engine_types.h  |  5 ++
  .../drm/i915/gt/intel_execlists_submission.c  |  1 +
  drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |  2 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 21 +------
  5 files changed, 60 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_cs.c +25 -1
index 0d9105a31d84..4d790f9a65dd 100644
@@ -290,7 +290,8 @@ static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
  	GEM_DEBUG_WARN_ON(iir);
  }
-static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
+static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
+			      u8 logical_instance)
  {
  	const struct engine_info *info = &intel_engines[id];
  	struct drm_i915_private *i915 = gt->i915;
@@ -334,6 +335,7 @@ static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
  	engine->class = info->class;
  	engine->instance = info->instance;
+	engine->logical_mask = BIT(logical_instance);
  	__sprint_engine_name(engine);
  	engine->props.heartbeat_interval_ms =
@@ -572,6 +574,37 @@ static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
  	return info->engine_mask;
  }
+static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
+				 u8 class, const u8 *map, u8 num_instances)
+{
+	int i, j;
+	u8 current_logical_id = 0;
+
+	for (j = 0; j < num_instances; ++j) {
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			if (!HAS_ENGINE(gt, i) ||
+			    intel_engines[i].class != class)
+				continue;
+
+			if (intel_engines[i].instance == map[j]) {
+				logical_ids[intel_engines[i].instance] =
+					current_logical_id++;
+				break;
+			}
+		}
+	}
+}
+
+static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c static intel_engine_mask_t init_engine_mask(struct intel_gt *gt) · line 604

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +6 -0
+{
+	int i;
+	u8 map[MAX_ENGINE_INSTANCE + 1];
+
+	for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
+		map[i] = i;

What's the point of the map array since it is 1:1 with instance?

Future products do not have a 1 to 1 mapping and that mapping can change based on fusing, e.g. XeHP SDV.

Also technically ICL / TGL / ADL physical instance 2 maps to logical instance 1.

Quoted 37 lines
Quoted 37 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +17 -8
+	populate_logical_ids(gt, logical_ids, class, map, ARRAY_SIZE(map));
+}
+
  /**
   * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
   * @gt: pointer to struct intel_gt
@@ -583,7 +616,8 @@ int intel_engines_init_mmio(struct intel_gt *gt)
  	struct drm_i915_private *i915 = gt->i915;
  	const unsigned int engine_mask = init_engine_mask(gt);
  	unsigned int mask = 0;
-	unsigned int i;
+	unsigned int i, class;
+	u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
  	int err;
  	drm_WARN_ON(&i915->drm, engine_mask == 0);
@@ -593,15 +627,23 @@ int intel_engines_init_mmio(struct intel_gt *gt)
  	if (i915_inject_probe_failure(i915))
  		return -ENODEV;
-	for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
-		if (!HAS_ENGINE(gt, i))
-			continue;
+	for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
+		setup_logical_ids(gt, logical_ids, class);
-		err = intel_engine_setup(gt, i);
-		if (err)
-			goto cleanup;
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			u8 instance = intel_engines[i].instance;
+
+			if (intel_engines[i].class != class ||
+			    !HAS_ENGINE(gt, i))
+				continue;
-		mask |= BIT(i);
+			err = intel_engine_setup(gt, i,
+						 logical_ids[instance]);
+			if (err)
+				goto cleanup;
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c int intel_engines_init_mmio(struct intel_gt *gt) · line 642

Quoted 2 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +2 -0
+
+			mask |= BIT(i);

I still this there is a less clunky way to set this up in less code and more readable at the same time. Like do it in two passes so you can iterate gt->engine_class[] array instead of having to implement a skip condition (both on class and HAS_ENGINE at two places) and also avoid walking the flat intel_engines array recursively.

Kinda a bikeshed arguing about a pretty simple loop structure, don't you think? I personally like the way it laid out.

Pseudo code for your suggestion?

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +1 -0
+		}
  	}
  	/*
drivers/gpu/drm/i915/gt/intel_engine_types.h +1 -0
index ed91bcff20eb..fddf35546b58 100644
@@ -266,6 +266,11 @@ struct intel_engine_cs {
  	unsigned int guc_id;
  	intel_engine_mask_t mask;
+	/**
Quoted 8 lines

drivers/gpu/drm/i915/gt/intel_engine_types.h struct intel_engine_cs { · line 273

Quoted 4 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +4 -0
+	 * @logical_mask: logical mask of engine, reported to user space via
+	 * query IOCTL and used to communicate with the GuC in logical space
+	 */
+	intel_engine_mask_t logical_mask;

You could prefix the new field with uabi_ to match the existing scheme and to signify to anyone who might be touching it in the future it should not be changed.

This is kinda uabi, but also kinda isn't. We do report a logical instance via IOCTL but it also really is tied the GuC backend as we only can communicate with the GuC in logical space. IMO we should leave as is.

Quoted 4 lines

Also, I think comment should explain what is logical space ie. how the numbering works.

Don't I already do that? I suppose I could add something like:

The logical mask within engine class must be contiguous across all instances.

Quoted 3 lines

Not sure the part about GuC needs to be in the comment since uapi is supposed to be backend agnostic.

Communicating with the GuC in logical space is a pretty key point here. The communication with the GuC in logical space is backend specific but how our hardware works (e.g. split frame workloads must be placed logical contiguous) is not. Mentioning the GuC requirement here makes sense to me for completeness.

Matt

Quoted 71 lines

Regards,

Tvrtko

Quoted 67 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
  	u8 class;
  	u8 instance;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -0
index cafb0608ffb4..813a6de01382 100644
@@ -3875,6 +3875,7 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  		ve->siblings[ve->num_siblings++] = sibling;
  		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
  		/*
  		 * All physical engines must be compatible for their emission
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c +1 -1
index 6926919bcac6..9f5f43a16182 100644
@@ -176,7 +176,7 @@ static void guc_mapping_table_init(struct intel_gt *gt,
  	for_each_engine(engine, gt, id) {
  		u8 guc_class = engine_class_to_guc_class(engine->class);
-		system_info->mapping_table[guc_class][engine->instance] =
+		system_info->mapping_table[guc_class][ilog2(engine->logical_mask)] =
  			engine->instance;
  	}
  }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -19
index e0eed70f9b92..ffafbac7335e 100644
@@ -1401,23 +1401,6 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
  	return __guc_action_deregister_context(guc, guc_id, loop);
  }
-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:
-		MISSING_CASE(class);
-		return 0;
-	}
-}
-
  static void guc_context_policy_init(struct intel_engine_cs *engine,
  				    struct guc_lrc_desc *desc)
  {
@@ -1459,8 +1442,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  	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->engine_submit_mask = engine->logical_mask;
  	desc->hw_context_desc = ce->lrc.lrca;
  	desc->priority = ce->guc_state.prio;
  	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
@@ -3260,6 +3242,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
  		}
  		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
  		if (n != 0 && ve->base.class != sibling->class) {
  			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
 
#35 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 73 lines · Matthew Brost
Quoted 62 lines · Tvrtko Ursulin
Quoted 60 lines · Matthew Brost

Add logical engine mapping. This is required for split-frame, as workloads need to be placed on engines in a logically contiguous manner.

v2:
(Daniel Vetter)
- Add kernel doc for new fields

---
   drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 60 ++++++++++++++++---
   drivers/gpu/drm/i915/gt/intel_engine_types.h  |  5 ++
   .../drm/i915/gt/intel_execlists_submission.c  |  1 +
   drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |  2 +-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 21 +------
   5 files changed, 60 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_cs.c +25 -1
index 0d9105a31d84..4d790f9a65dd 100644
@@ -290,7 +290,8 @@ static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
   	GEM_DEBUG_WARN_ON(iir);
   }
-static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
+static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
+			      u8 logical_instance)
   {
   	const struct engine_info *info = &intel_engines[id];
   	struct drm_i915_private *i915 = gt->i915;
@@ -334,6 +335,7 @@ static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
   	engine->class = info->class;
   	engine->instance = info->instance;
+	engine->logical_mask = BIT(logical_instance);
   	__sprint_engine_name(engine);
   	engine->props.heartbeat_interval_ms =
@@ -572,6 +574,37 @@ static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
   	return info->engine_mask;
   }
+static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
+				 u8 class, const u8 *map, u8 num_instances)
+{
+	int i, j;
+	u8 current_logical_id = 0;
+
+	for (j = 0; j < num_instances; ++j) {
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			if (!HAS_ENGINE(gt, i) ||
+			    intel_engines[i].class != class)
+				continue;
+
+			if (intel_engines[i].instance == map[j]) {
+				logical_ids[intel_engines[i].instance] =
+					current_logical_id++;
+				break;
+			}
+		}
+	}
+}
+
+static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c static intel_engine_mask_t init_engine_mask(struct intel_gt *gt) · line 604

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +6 -0
+{
+	int i;
+	u8 map[MAX_ENGINE_INSTANCE + 1];
+
+	for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
+		map[i] = i;

What's the point of the map array since it is 1:1 with instance?

Quoted 5 lines

Future products do not have a 1 to 1 mapping and that mapping can change based on fusing, e.g. XeHP SDV.

Also technically ICL / TGL / ADL physical instance 2 maps to logical instance 1.

I don't follow the argument. All I can see is that "map[i] = i" always in the proposed code, which is then used to check "instance == map[instance]". So I'd suggest to remove this array from the code until there is a need for it.

Quoted 47 lines
Quoted 37 lines
Quoted 37 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +17 -8
+	populate_logical_ids(gt, logical_ids, class, map, ARRAY_SIZE(map));
+}
+
   /**
    * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
    * @gt: pointer to struct intel_gt
@@ -583,7 +616,8 @@ int intel_engines_init_mmio(struct intel_gt *gt)
   	struct drm_i915_private *i915 = gt->i915;
   	const unsigned int engine_mask = init_engine_mask(gt);
   	unsigned int mask = 0;
-	unsigned int i;
+	unsigned int i, class;
+	u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
   	int err;
   	drm_WARN_ON(&i915->drm, engine_mask == 0);
@@ -593,15 +627,23 @@ int intel_engines_init_mmio(struct intel_gt *gt)
   	if (i915_inject_probe_failure(i915))
   		return -ENODEV;
-	for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
-		if (!HAS_ENGINE(gt, i))
-			continue;
+	for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
+		setup_logical_ids(gt, logical_ids, class);
-		err = intel_engine_setup(gt, i);
-		if (err)
-			goto cleanup;
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			u8 instance = intel_engines[i].instance;
+
+			if (intel_engines[i].class != class ||
+			    !HAS_ENGINE(gt, i))
+				continue;
-		mask |= BIT(i);
+			err = intel_engine_setup(gt, i,
+						 logical_ids[instance]);
+			if (err)
+				goto cleanup;
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c int intel_engines_init_mmio(struct intel_gt *gt) · line 642

Quoted 2 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +2 -0
+
+			mask |= BIT(i);

I still this there is a less clunky way to set this up in less code and more readable at the same time. Like do it in two passes so you can iterate gt->engine_class[] array instead of having to implement a skip condition (both on class and HAS_ENGINE at two places) and also avoid walking the flat intel_engines array recursively.

Quoted 4 lines

Kinda a bikeshed arguing about a pretty simple loop structure, don't you think? I personally like the way it laid out.

Pseudo code for your suggestion?

Leave the existing setup loop as is and add an additional "for engine class" walk after it. That way you can walk already setup gt->engine_class[] array so wouldn't need to skip wrong classes and have HAS_ENGINE checks when walking the flat intel_engines[] array several times. It also applies to the helper which counts logical instances per class.

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +1 -0
+		}
   	}
   	/*
drivers/gpu/drm/i915/gt/intel_engine_types.h +1 -0
index ed91bcff20eb..fddf35546b58 100644
@@ -266,6 +266,11 @@ struct intel_engine_cs {
   	unsigned int guc_id;
   	intel_engine_mask_t mask;
+	/**
Quoted 8 lines

drivers/gpu/drm/i915/gt/intel_engine_types.h struct intel_engine_cs { · line 273

Quoted 4 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +4 -0
+	 * @logical_mask: logical mask of engine, reported to user space via
+	 * query IOCTL and used to communicate with the GuC in logical space
+	 */
+	intel_engine_mask_t logical_mask;

You could prefix the new field with uabi_ to match the existing scheme and to signify to anyone who might be touching it in the future it should not be changed.

Quoted 4 lines

This is kinda uabi, but also kinda isn't. We do report a logical instance via IOCTL but it also really is tied the GuC backend as we only can communicate with the GuC in logical space. IMO we should leave as is.

Perhaps it would be best to call the new field uabi_logical_instance so it's clear it is reported in the query directly and do the BIT() transformation in the GuC backend?

Quoted 7 lines
Quoted 5 lines

Also, I think comment should explain what is logical space ie. how the numbering works.

Don't I already do that? I suppose I could add something like:

Where is it? Can't see it in the uapi kerneldoc AFAICS (for the new query) or here.

Quoted 3 lines

The logical mask within engine class must be contiguous across all instances.

Best not to start mentioning the mask for the first time. Just explain what logical numbering is in terms of how engines are enumerated in order of physical instances but skipping the fused off ones. In the kerneldoc for the new query is I think the right place.

Quoted 9 lines
Quoted 3 lines

Not sure the part about GuC needs to be in the comment since uapi is supposed to be backend agnostic.

Communicating with the GuC in logical space is a pretty key point here. The communication with the GuC in logical space is backend specific but how our hardware works (e.g. split frame workloads must be placed logical contiguous) is not. Mentioning the GuC requirement here makes sense to me for completeness.

Yeah might be, I was thinking more about the new query. Query definitely is backend agnostic but yes it is fine to say in the comment here the new field is used both for the query and for communicating with GuC.

Regards,

Tvrtko

Quoted 74 lines

Matt

Quoted 71 lines

Regards,

Tvrtko

Quoted 67 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
   	u8 class;
   	u8 instance;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -0
index cafb0608ffb4..813a6de01382 100644
@@ -3875,6 +3875,7 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
   		ve->siblings[ve->num_siblings++] = sibling;
   		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
   		/*
   		 * All physical engines must be compatible for their emission
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c +1 -1
index 6926919bcac6..9f5f43a16182 100644
@@ -176,7 +176,7 @@ static void guc_mapping_table_init(struct intel_gt *gt,
   	for_each_engine(engine, gt, id) {
   		u8 guc_class = engine_class_to_guc_class(engine->class);
-		system_info->mapping_table[guc_class][engine->instance] =
+		system_info->mapping_table[guc_class][ilog2(engine->logical_mask)] =
   			engine->instance;
   	}
   }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -19
index e0eed70f9b92..ffafbac7335e 100644
@@ -1401,23 +1401,6 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
   	return __guc_action_deregister_context(guc, guc_id, loop);
   }
-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:
-		MISSING_CASE(class);
-		return 0;
-	}
-}
-
   static void guc_context_policy_init(struct intel_engine_cs *engine,
   				    struct guc_lrc_desc *desc)
   {
@@ -1459,8 +1442,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
   	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->engine_submit_mask = engine->logical_mask;
   	desc->hw_context_desc = ce->lrc.lrca;
   	desc->priority = ce->guc_state.prio;
   	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
@@ -3260,6 +3242,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
   		}
   		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
   		if (n != 0 && ve->base.class != sibling->class) {
   			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
 
#36 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 78 lines · Tvrtko Ursulin
Quoted 76 lines · Matthew Brost
Quoted 62 lines · Tvrtko Ursulin
Quoted 60 lines · Matthew Brost

Add logical engine mapping. This is required for split-frame, as workloads need to be placed on engines in a logically contiguous manner.

v2:
(Daniel Vetter)
- Add kernel doc for new fields

---
   drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 60 ++++++++++++++++---
   drivers/gpu/drm/i915/gt/intel_engine_types.h  |  5 ++
   .../drm/i915/gt/intel_execlists_submission.c  |  1 +
   drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |  2 +-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 21 +------
   5 files changed, 60 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_cs.c +25 -1
index 0d9105a31d84..4d790f9a65dd 100644
@@ -290,7 +290,8 @@ static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
   	GEM_DEBUG_WARN_ON(iir);
   }
-static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
+static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
+			      u8 logical_instance)
   {
   	const struct engine_info *info = &intel_engines[id];
   	struct drm_i915_private *i915 = gt->i915;
@@ -334,6 +335,7 @@ static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
   	engine->class = info->class;
   	engine->instance = info->instance;
+	engine->logical_mask = BIT(logical_instance);
   	__sprint_engine_name(engine);
   	engine->props.heartbeat_interval_ms =
@@ -572,6 +574,37 @@ static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
   	return info->engine_mask;
   }
+static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
+				 u8 class, const u8 *map, u8 num_instances)
+{
+	int i, j;
+	u8 current_logical_id = 0;
+
+	for (j = 0; j < num_instances; ++j) {
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			if (!HAS_ENGINE(gt, i) ||
+			    intel_engines[i].class != class)
+				continue;
+
+			if (intel_engines[i].instance == map[j]) {
+				logical_ids[intel_engines[i].instance] =
+					current_logical_id++;
+				break;
+			}
+		}
+	}
+}
+
+static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c static intel_engine_mask_t init_engine_mask(struct intel_gt *gt) · line 604

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +6 -0
+{
+	int i;
+	u8 map[MAX_ENGINE_INSTANCE + 1];
+
+	for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
+		map[i] = i;

What's the point of the map array since it is 1:1 with instance?

Future products do not have a 1 to 1 mapping and that mapping can change based on fusing, e.g. XeHP SDV.

Quoted 8 lines
Quoted 2 lines

Also technically ICL / TGL / ADL physical instance 2 maps to logical instance 1.

I don't follow the argument. All I can see is that "map[i] = i" always in the proposed code, which is then used to check "instance == map[instance]". So I'd suggest to remove this array from the code until there is a need for it.

Ok, this logic is slightly confusing and makes more sense once we have non-standard mappings. Yes, map is setup in a 1 to 1 mapping by default with the value in map[i] being a physical instance. Populate_logical_ids searches the map finding all physical instances present in the map assigning each found instance a new logical id increasing by 1 each time.

e.g. If the map is setup 0-N and only physical instance 0 / 2 are present they will get logical mapping 0 / 1 respectfully.

This algorithm works for non-standard mappings too /w fused parts. e.g. on XeHP SDV the map is: { 0, 2, 4, 6, 1, 3, 5, 7 } and if any of the physical instances can't be found due to fusing the logical mapping is still correct per the bspec.

This array is absolutely needed for multi-lrc submission to work, even on ICL / TGL / ADL as the GuC only supports logically contiguous engine instances.

Quoted 50 lines
Quoted 50 lines
Quoted 37 lines
Quoted 37 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +17 -8
+	populate_logical_ids(gt, logical_ids, class, map, ARRAY_SIZE(map));
+}
+
   /**
    * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
    * @gt: pointer to struct intel_gt
@@ -583,7 +616,8 @@ int intel_engines_init_mmio(struct intel_gt *gt)
   	struct drm_i915_private *i915 = gt->i915;
   	const unsigned int engine_mask = init_engine_mask(gt);
   	unsigned int mask = 0;
-	unsigned int i;
+	unsigned int i, class;
+	u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
   	int err;
   	drm_WARN_ON(&i915->drm, engine_mask == 0);
@@ -593,15 +627,23 @@ int intel_engines_init_mmio(struct intel_gt *gt)
   	if (i915_inject_probe_failure(i915))
   		return -ENODEV;
-	for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
-		if (!HAS_ENGINE(gt, i))
-			continue;
+	for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
+		setup_logical_ids(gt, logical_ids, class);
-		err = intel_engine_setup(gt, i);
-		if (err)
-			goto cleanup;
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			u8 instance = intel_engines[i].instance;
+
+			if (intel_engines[i].class != class ||
+			    !HAS_ENGINE(gt, i))
+				continue;
-		mask |= BIT(i);
+			err = intel_engine_setup(gt, i,
+						 logical_ids[instance]);
+			if (err)
+				goto cleanup;
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c int intel_engines_init_mmio(struct intel_gt *gt) · line 642

Quoted 2 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +2 -0
+
+			mask |= BIT(i);

I still this there is a less clunky way to set this up in less code and more readable at the same time. Like do it in two passes so you can iterate gt->engine_class[] array instead of having to implement a skip condition (both on class and HAS_ENGINE at two places) and also avoid walking the flat intel_engines array recursively.

Kinda a bikeshed arguing about a pretty simple loop structure, don't you think? I personally like the way it laid out.

Quoted 8 lines
Quoted 1 line

Pseudo code for your suggestion?

Leave the existing setup loop as is and add an additional "for engine class" walk after it. That way you can walk already setup gt->engine_class[] array so wouldn't need to skip wrong classes and have HAS_ENGINE checks when walking the flat intel_engines[] array several times. It also applies to the helper which counts logical instances per class.

Ok, I think I see what you are getting at. Again IMO this is a total bikeshed as this is 1 time setup step that we really should only care if the loop works or not rather than it being optimized / looks a way a certain person wants. I can change this if you really insist but again IMO disucssing this is a total waste of energy.

Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +1 -0
+		}
   	}
   	/*
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
index ed91bcff20eb..fddf35546b58 100644

drivers/gpu/drm/i915/gt/intel_engine_types.h struct intel_engine_cs { · line 272

Quoted 7 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +5 -0
@@ -266,6 +266,11 @@ struct intel_engine_cs {
   	unsigned int guc_id;
   	intel_engine_mask_t mask;
+	/**
+	 * @logical_mask: logical mask of engine, reported to user space via
+	 * query IOCTL and used to communicate with the GuC in logical space
+	 */
+	intel_engine_mask_t logical_mask;

You could prefix the new field with uabi_ to match the existing scheme and to signify to anyone who might be touching it in the future it should not be changed.

Quoted 9 lines
Quoted 4 lines

This is kinda uabi, but also kinda isn't. We do report a logical instance via IOCTL but it also really is tied the GuC backend as we only can communicate with the GuC in logical space. IMO we should leave as is.

Perhaps it would be best to call the new field uabi_logical_instance so it's clear it is reported in the query directly and do the BIT() transformation in the GuC backend?

Virtual engines can have a multiple bits set in this mask, so this is used for both the query on physical engines via UABI and submission in the GuC backend.

Quoted 11 lines
Quoted 7 lines
Quoted 5 lines

Also, I think comment should explain what is logical space ie. how the numbering works.

Don't I already do that? I suppose I could add something like:

Where is it? Can't see it in the uapi kerneldoc AFAICS (for the new query) or here.

	/**
	 * @logical_mask: logical mask of engine, reported to user space via
	 * query IOCTL and used to communicate with the GuC in logical space
	 */
Quoted 9 lines
Quoted 3 lines

The logical mask within engine class must be contiguous across all instances.

Best not to start mentioning the mask for the first time. Just explain what logical numbering is in terms of how engines are enumerated in order of physical instances but skipping the fused off ones. In the kerneldoc for the new query is I think the right place.

Maybe I can add:

The logical mapping is defined on per part basis in the bspec and can very based the parts fusing.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

Not sure the part about GuC needs to be in the comment since uapi is supposed to be backend agnostic.

Communicating with the GuC in logical space is a pretty key point here. The communication with the GuC in logical space is backend specific but how our hardware works (e.g. split frame workloads must be placed logical contiguous) is not. Mentioning the GuC requirement here makes sense to me for completeness.

Quoted 4 lines

Yeah might be, I was thinking more about the new query. Query definitely is backend agnostic but yes it is fine to say in the comment here the new field is used both for the query and for communicating with GuC.

Sounds good, will make it clear it used for the query and from communicating with the GuC.

Matt

Quoted 78 lines

Regards,

Tvrtko

Quoted 74 lines

Matt

Quoted 71 lines

Regards,

Tvrtko

Quoted 67 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
   	u8 class;
   	u8 instance;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -0
index cafb0608ffb4..813a6de01382 100644
@@ -3875,6 +3875,7 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
   		ve->siblings[ve->num_siblings++] = sibling;
   		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
   		/*
   		 * All physical engines must be compatible for their emission
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c +1 -1
index 6926919bcac6..9f5f43a16182 100644
@@ -176,7 +176,7 @@ static void guc_mapping_table_init(struct intel_gt *gt,
   	for_each_engine(engine, gt, id) {
   		u8 guc_class = engine_class_to_guc_class(engine->class);
-		system_info->mapping_table[guc_class][engine->instance] =
+		system_info->mapping_table[guc_class][ilog2(engine->logical_mask)] =
   			engine->instance;
   	}
   }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -19
index e0eed70f9b92..ffafbac7335e 100644
@@ -1401,23 +1401,6 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
   	return __guc_action_deregister_context(guc, guc_id, loop);
   }
-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:
-		MISSING_CASE(class);
-		return 0;
-	}
-}
-
   static void guc_context_policy_init(struct intel_engine_cs *engine,
   				    struct guc_lrc_desc *desc)
   {
@@ -1459,8 +1442,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
   	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->engine_submit_mask = engine->logical_mask;
   	desc->hw_context_desc = ce->lrc.lrca;
   	desc->priority = ce->guc_state.prio;
   	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
@@ -3260,6 +3242,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
   		}
   		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
   		if (n != 0 && ve->base.class != sibling->class) {
   			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
 
#37 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 98 lines · Matthew Brost
Quoted 78 lines · Tvrtko Ursulin
Quoted 76 lines · Matthew Brost
Quoted 62 lines · Tvrtko Ursulin
Quoted 60 lines · Matthew Brost

Add logical engine mapping. This is required for split-frame, as workloads need to be placed on engines in a logically contiguous manner.

v2:
(Daniel Vetter)
- Add kernel doc for new fields

---
    drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 60 ++++++++++++++++---
    drivers/gpu/drm/i915/gt/intel_engine_types.h  |  5 ++
    .../drm/i915/gt/intel_execlists_submission.c  |  1 +
    drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |  2 +-
    .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 21 +------
    5 files changed, 60 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_cs.c +25 -1
index 0d9105a31d84..4d790f9a65dd 100644
@@ -290,7 +290,8 @@ static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
    	GEM_DEBUG_WARN_ON(iir);
    }
-static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
+static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
+			      u8 logical_instance)
    {
    	const struct engine_info *info = &intel_engines[id];
    	struct drm_i915_private *i915 = gt->i915;
@@ -334,6 +335,7 @@ static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
    	engine->class = info->class;
    	engine->instance = info->instance;
+	engine->logical_mask = BIT(logical_instance);
    	__sprint_engine_name(engine);
    	engine->props.heartbeat_interval_ms =
@@ -572,6 +574,37 @@ static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
    	return info->engine_mask;
    }
+static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
+				 u8 class, const u8 *map, u8 num_instances)
+{
+	int i, j;
+	u8 current_logical_id = 0;
+
+	for (j = 0; j < num_instances; ++j) {
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			if (!HAS_ENGINE(gt, i) ||
+			    intel_engines[i].class != class)
+				continue;
+
+			if (intel_engines[i].instance == map[j]) {
+				logical_ids[intel_engines[i].instance] =
+					current_logical_id++;
+				break;
+			}
+		}
+	}
+}
+
+static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c static intel_engine_mask_t init_engine_mask(struct intel_gt *gt) · line 604

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +6 -0
+{
+	int i;
+	u8 map[MAX_ENGINE_INSTANCE + 1];
+
+	for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
+		map[i] = i;

What's the point of the map array since it is 1:1 with instance?

Future products do not have a 1 to 1 mapping and that mapping can change based on fusing, e.g. XeHP SDV.

Quoted 8 lines
Quoted 2 lines

Also technically ICL / TGL / ADL physical instance 2 maps to logical instance 1.

I don't follow the argument. All I can see is that "map[i] = i" always in the proposed code, which is then used to check "instance == map[instance]". So I'd suggest to remove this array from the code until there is a need for it.

Ok, this logic is slightly confusing and makes more sense once we have non-standard mappings. Yes, map is setup in a 1 to 1 mapping by default with the value in map[i] being a physical instance. Populate_logical_ids searches the map finding all physical instances present in the map assigning each found instance a new logical id increasing by 1 each time.

e.g. If the map is setup 0-N and only physical instance 0 / 2 are present they will get logical mapping 0 / 1 respectfully.

Quoted 8 lines

This algorithm works for non-standard mappings too /w fused parts. e.g. on XeHP SDV the map is: { 0, 2, 4, 6, 1, 3, 5, 7 } and if any of the physical instances can't be found due to fusing the logical mapping is still correct per the bspec.

This array is absolutely needed for multi-lrc submission to work, even on ICL / TGL / ADL as the GuC only supports logically contiguous engine instances.

No idea how can an array fixed at "map[i] = i" be absolutely needed when you can just write it like "i". Sometimes it is okay to lay some ground work for future platforms but in this case to me it's just obfuscation which should be added later, when it is required.

Quoted 59 lines
Quoted 50 lines
Quoted 50 lines
Quoted 37 lines
Quoted 37 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +17 -8
+	populate_logical_ids(gt, logical_ids, class, map, ARRAY_SIZE(map));
+}
+
    /**
     * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
     * @gt: pointer to struct intel_gt
@@ -583,7 +616,8 @@ int intel_engines_init_mmio(struct intel_gt *gt)
    	struct drm_i915_private *i915 = gt->i915;
    	const unsigned int engine_mask = init_engine_mask(gt);
    	unsigned int mask = 0;
-	unsigned int i;
+	unsigned int i, class;
+	u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
    	int err;
    	drm_WARN_ON(&i915->drm, engine_mask == 0);
@@ -593,15 +627,23 @@ int intel_engines_init_mmio(struct intel_gt *gt)
    	if (i915_inject_probe_failure(i915))
    		return -ENODEV;
-	for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
-		if (!HAS_ENGINE(gt, i))
-			continue;
+	for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
+		setup_logical_ids(gt, logical_ids, class);
-		err = intel_engine_setup(gt, i);
-		if (err)
-			goto cleanup;
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			u8 instance = intel_engines[i].instance;
+
+			if (intel_engines[i].class != class ||
+			    !HAS_ENGINE(gt, i))
+				continue;
-		mask |= BIT(i);
+			err = intel_engine_setup(gt, i,
+						 logical_ids[instance]);
+			if (err)
+				goto cleanup;
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c int intel_engines_init_mmio(struct intel_gt *gt) · line 642

Quoted 2 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +2 -0
+
+			mask |= BIT(i);

I still this there is a less clunky way to set this up in less code and more readable at the same time. Like do it in two passes so you can iterate gt->engine_class[] array instead of having to implement a skip condition (both on class and HAS_ENGINE at two places) and also avoid walking the flat intel_engines array recursively.

Kinda a bikeshed arguing about a pretty simple loop structure, don't you think? I personally like the way it laid out.

Quoted 8 lines
Quoted 1 line

Pseudo code for your suggestion?

Leave the existing setup loop as is and add an additional "for engine class" walk after it. That way you can walk already setup gt->engine_class[] array so wouldn't need to skip wrong classes and have HAS_ENGINE checks when walking the flat intel_engines[] array several times. It also applies to the helper which counts logical instances per class.

Quoted 5 lines

Ok, I think I see what you are getting at. Again IMO this is a total bikeshed as this is 1 time setup step that we really should only care if the loop works or not rather than it being optimized / looks a way a certain person wants. I can change this if you really insist but again IMO disucssing this is a total waste of energy.

It should be such a no brainer to go with simpler and less invasive change that I honestly don't understand where is the big deal. Here is my pseudo code one more time and that will be the last from me on the topic.

Today we have:

for_each intel_engines: // intel_engines is a flat list of all engines
intel_engine_setup()

You propose to change it to:

for_each engine_class:
for 0..max_global_engine_instance:
for_each intel_engines:
skip engine not present
skip class not matching

count logical instance

for_each intel_engines:
skip engine not present
skip wrong class

intel_engine_setup()

I propose:

// Leave as is:

for_each intel_engines:
intel_engine_setup()

// Add:

for_each engine_class:
logical = 0
for_each gt->engine_class[class]:
skip engine not present

engine->logical_instance = logical++

When code which actually needs a preturbed "map" arrives you add that in to this second loop.

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +1 -0
+		}
    	}
    	/*
drivers/gpu/drm/i915/gt/intel_engine_types.h +1 -0
index ed91bcff20eb..fddf35546b58 100644
@@ -266,6 +266,11 @@ struct intel_engine_cs {
    	unsigned int guc_id;
    	intel_engine_mask_t mask;
+	/**
Quoted 8 lines

drivers/gpu/drm/i915/gt/intel_engine_types.h struct intel_engine_cs { · line 273

Quoted 4 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +4 -0
+	 * @logical_mask: logical mask of engine, reported to user space via
+	 * query IOCTL and used to communicate with the GuC in logical space
+	 */
+	intel_engine_mask_t logical_mask;

You could prefix the new field with uabi_ to match the existing scheme and to signify to anyone who might be touching it in the future it should not be changed.

This is kinda uabi, but also kinda isn't. We do report a logical instance via IOCTL but it also really is tied the GuC backend as we only can communicate with the GuC in logical space. IMO we should leave as is.

Quoted 8 lines
Quoted 4 lines

Perhaps it would be best to call the new field uabi_logical_instance so it's clear it is reported in the query directly and do the BIT() transformation in the GuC backend?

Virtual engines can have a multiple bits set in this mask, so this is used for both the query on physical engines via UABI and submission in the GuC backend.

You could add both fields if that would help. I just think it is preferrable to keep the existing convetion of uabi_ prefix for the fields which i915 refactors must not change willy-nilly.

Quoted 28 lines
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines

Also, I think comment should explain what is logical space ie. how the numbering works.

Don't I already do that? I suppose I could add something like:

Where is it? Can't see it in the uapi kerneldoc AFAICS (for the new query) or here.

	/**
	 * @logical_mask: logical mask of engine, reported to user space via
	 * query IOCTL and used to communicate with the GuC in logical space
	 */
Quoted 9 lines
Quoted 3 lines

The logical mask within engine class must be contiguous across all instances.

Best not to start mentioning the mask for the first time. Just explain what logical numbering is in terms of how engines are enumerated in order of physical instances but skipping the fused off ones. In the kerneldoc for the new query is I think the right place.

Quoted 4 lines

Maybe I can add:

The logical mapping is defined on per part basis in the bspec and can very based the parts fusing.

Sounds good, I think that would be useful. But in the uapi kerneldoc.

Perhaps a cross-link to/from the kernel doc which talks about frame split to explain consecutive logical instances have to be used. That would tie the new query with that uapi in the narrative.

Regards,

Tvrtko

Quoted 98 lines
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

Not sure the part about GuC needs to be in the comment since uapi is supposed to be backend agnostic.

Communicating with the GuC in logical space is a pretty key point here. The communication with the GuC in logical space is backend specific but how our hardware works (e.g. split frame workloads must be placed logical contiguous) is not. Mentioning the GuC requirement here makes sense to me for completeness.

Quoted 4 lines

Yeah might be, I was thinking more about the new query. Query definitely is backend agnostic but yes it is fine to say in the comment here the new field is used both for the query and for communicating with GuC.

Sounds good, will make it clear it used for the query and from communicating with the GuC.

Matt

Quoted 78 lines

Regards,

Tvrtko

Quoted 74 lines

Matt

Quoted 71 lines

Regards,

Tvrtko

Quoted 67 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
    	u8 class;
    	u8 instance;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -0
index cafb0608ffb4..813a6de01382 100644
@@ -3875,6 +3875,7 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
    		ve->siblings[ve->num_siblings++] = sibling;
    		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
    		/*
    		 * All physical engines must be compatible for their emission
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c +1 -1
index 6926919bcac6..9f5f43a16182 100644
@@ -176,7 +176,7 @@ static void guc_mapping_table_init(struct intel_gt *gt,
    	for_each_engine(engine, gt, id) {
    		u8 guc_class = engine_class_to_guc_class(engine->class);
-		system_info->mapping_table[guc_class][engine->instance] =
+		system_info->mapping_table[guc_class][ilog2(engine->logical_mask)] =
    			engine->instance;
    	}
    }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -19
index e0eed70f9b92..ffafbac7335e 100644
@@ -1401,23 +1401,6 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
    	return __guc_action_deregister_context(guc, guc_id, loop);
    }
-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:
-		MISSING_CASE(class);
-		return 0;
-	}
-}
-
    static void guc_context_policy_init(struct intel_engine_cs *engine,
    				    struct guc_lrc_desc *desc)
    {
@@ -1459,8 +1442,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
    	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->engine_submit_mask = engine->logical_mask;
    	desc->hw_context_desc = ce->lrc.lrca;
    	desc->priority = ce->guc_state.prio;
    	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
@@ -3260,6 +3242,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
    		}
    		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
    		if (n != 0 && ve->base.class != sibling->class) {
    			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
 
#38 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 105 lines · Tvrtko Ursulin
Quoted 103 lines · Matthew Brost
Quoted 78 lines · Tvrtko Ursulin
Quoted 76 lines · Matthew Brost
Quoted 62 lines · Tvrtko Ursulin
Quoted 60 lines · Matthew Brost

Add logical engine mapping. This is required for split-frame, as workloads need to be placed on engines in a logically contiguous manner.

v2:
(Daniel Vetter)
- Add kernel doc for new fields

---
    drivers/gpu/drm/i915/gt/intel_engine_cs.c     | 60 ++++++++++++++++---
    drivers/gpu/drm/i915/gt/intel_engine_types.h  |  5 ++
    .../drm/i915/gt/intel_execlists_submission.c  |  1 +
    drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c    |  2 +-
    .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 21 +------
    5 files changed, 60 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gt/intel_engine_cs.c +25 -1
index 0d9105a31d84..4d790f9a65dd 100644
@@ -290,7 +290,8 @@ static void nop_irq_handler(struct intel_engine_cs *engine, u16 iir)
    	GEM_DEBUG_WARN_ON(iir);
    }
-static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
+static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id,
+			      u8 logical_instance)
    {
    	const struct engine_info *info = &intel_engines[id];
    	struct drm_i915_private *i915 = gt->i915;
@@ -334,6 +335,7 @@ static int intel_engine_setup(struct intel_gt *gt, enum intel_engine_id id)
    	engine->class = info->class;
    	engine->instance = info->instance;
+	engine->logical_mask = BIT(logical_instance);
    	__sprint_engine_name(engine);
    	engine->props.heartbeat_interval_ms =
@@ -572,6 +574,37 @@ static intel_engine_mask_t init_engine_mask(struct intel_gt *gt)
    	return info->engine_mask;
    }
+static void populate_logical_ids(struct intel_gt *gt, u8 *logical_ids,
+				 u8 class, const u8 *map, u8 num_instances)
+{
+	int i, j;
+	u8 current_logical_id = 0;
+
+	for (j = 0; j < num_instances; ++j) {
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			if (!HAS_ENGINE(gt, i) ||
+			    intel_engines[i].class != class)
+				continue;
+
+			if (intel_engines[i].instance == map[j]) {
+				logical_ids[intel_engines[i].instance] =
+					current_logical_id++;
+				break;
+			}
+		}
+	}
+}
+
+static void setup_logical_ids(struct intel_gt *gt, u8 *logical_ids, u8 class)
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c static intel_engine_mask_t init_engine_mask(struct intel_gt *gt) · line 604

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +6 -0
+{
+	int i;
+	u8 map[MAX_ENGINE_INSTANCE + 1];
+
+	for (i = 0; i < MAX_ENGINE_INSTANCE + 1; ++i)
+		map[i] = i;

What's the point of the map array since it is 1:1 with instance?

Future products do not have a 1 to 1 mapping and that mapping can change based on fusing, e.g. XeHP SDV.

Quoted 8 lines
Quoted 2 lines

Also technically ICL / TGL / ADL physical instance 2 maps to logical instance 1.

I don't follow the argument. All I can see is that "map[i] = i" always in the proposed code, which is then used to check "instance == map[instance]". So I'd suggest to remove this array from the code until there is a need for it.

Ok, this logic is slightly confusing and makes more sense once we have non-standard mappings. Yes, map is setup in a 1 to 1 mapping by default with the value in map[i] being a physical instance. Populate_logical_ids searches the map finding all physical instances present in the map assigning each found instance a new logical id increasing by 1 each time.

e.g. If the map is setup 0-N and only physical instance 0 / 2 are present they will get logical mapping 0 / 1 respectfully.

This algorithm works for non-standard mappings too /w fused parts. e.g. on XeHP SDV the map is: { 0, 2, 4, 6, 1, 3, 5, 7 } and if any of the physical instances can't be found due to fusing the logical mapping is still correct per the bspec.

Quoted 6 lines
Quoted 3 lines

This array is absolutely needed for multi-lrc submission to work, even on ICL / TGL / ADL as the GuC only supports logically contiguous engine instances.

No idea how can an array fixed at "map[i] = i" be absolutely needed when you can just write it like "i". Sometimes it is okay to lay some ground work for

You can't write "i", that is the point. The map is a search array saying if entry is present assign + increase the logical id. That is how a map of 0, 1, 2 with physical instancnes 0, 2 present result in a logical mapping of 0, 1. This is the algorithm we use for all parts, albeit some parts have different maps (e.g. XeHP SDV, PVC, etc...) compared to here where we use the default map of sequential numbers.

Quoted 2 lines

future platforms but in this case to me it's just obfuscation which should be added later, when it is required.

Same algorithm, see above. This should land is as it doesn't make sense to hack a different algorithm only to replace it with the correct algorithm later.

Quoted 106 lines
Quoted 60 lines
Quoted 51 lines
Quoted 51 lines
Quoted 38 lines
Quoted 38 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +17 -8
+	populate_logical_ids(gt, logical_ids, class, map, ARRAY_SIZE(map));
+}
+
    /**
     * intel_engines_init_mmio() - allocate and prepare the Engine Command Streamers
     * @gt: pointer to struct intel_gt
@@ -583,7 +616,8 @@ int intel_engines_init_mmio(struct intel_gt *gt)
    	struct drm_i915_private *i915 = gt->i915;
    	const unsigned int engine_mask = init_engine_mask(gt);
    	unsigned int mask = 0;
-	unsigned int i;
+	unsigned int i, class;
+	u8 logical_ids[MAX_ENGINE_INSTANCE + 1];
    	int err;
    	drm_WARN_ON(&i915->drm, engine_mask == 0);
@@ -593,15 +627,23 @@ int intel_engines_init_mmio(struct intel_gt *gt)
    	if (i915_inject_probe_failure(i915))
    		return -ENODEV;
-	for (i = 0; i < ARRAY_SIZE(intel_engines); i++) {
-		if (!HAS_ENGINE(gt, i))
-			continue;
+	for (class = 0; class < MAX_ENGINE_CLASS + 1; ++class) {
+		setup_logical_ids(gt, logical_ids, class);
-		err = intel_engine_setup(gt, i);
-		if (err)
-			goto cleanup;
+		for (i = 0; i < ARRAY_SIZE(intel_engines); ++i) {
+			u8 instance = intel_engines[i].instance;
+
+			if (intel_engines[i].class != class ||
+			    !HAS_ENGINE(gt, i))
+				continue;
-		mask |= BIT(i);
+			err = intel_engine_setup(gt, i,
+						 logical_ids[instance]);
+			if (err)
+				goto cleanup;
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_engine_cs.c int intel_engines_init_mmio(struct intel_gt *gt) · line 642

Quoted 2 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +2 -0
+
+			mask |= BIT(i);

I still this there is a less clunky way to set this up in less code and more readable at the same time. Like do it in two passes so you can iterate gt->engine_class[] array instead of having to implement a skip condition (both on class and HAS_ENGINE at two places) and also avoid walking the flat intel_engines array recursively.

Kinda a bikeshed arguing about a pretty simple loop structure, don't you think? I personally like the way it laid out.

Quoted 8 lines
Quoted 1 line

Pseudo code for your suggestion?

Leave the existing setup loop as is and add an additional "for engine class" walk after it. That way you can walk already setup gt->engine_class[] array so wouldn't need to skip wrong classes and have HAS_ENGINE checks when walking the flat intel_engines[] array several times. It also applies to the helper which counts logical instances per class.

Quoted 5 lines

Ok, I think I see what you are getting at. Again IMO this is a total bikeshed as this is 1 time setup step that we really should only care if the loop works or not rather than it being optimized / looks a way a certain person wants. I can change this if you really insist but again IMO disucssing this is a total waste of energy.

It should be such a no brainer to go with simpler and less invasive change that I honestly don't understand where is the big deal. Here is my pseudo code one more time and that will be the last from me on the topic.

Today we have:

for_each intel_engines: // intel_engines is a flat list of all engines
intel_engine_setup()

You propose to change it to:

for_each engine_class:
for 0..max_global_engine_instance:
for_each intel_engines:
skip engine not present
skip class not matching

count logical instance

for_each intel_engines:
skip engine not present
skip wrong class

intel_engine_setup()

I propose:

// Leave as is:

for_each intel_engines:
intel_engine_setup()

// Add:

for_each engine_class:
logical = 0
for_each gt->engine_class[class]:
skip engine not present

Quoted 6 lines

engine->logical_instance = logical++

When code which actually needs a preturbed "map" arrives you add that in to this second loop.

See above, why introduce an algorithm that doesn't work for future parts
+ future patches are land imminently? It makes zero sense whatsoever. With your proposal we would literally land code to just throw it away a couple of months from now + break patches we intend to land soon. This algorithm works and has no reason whatsoever to be optimal as it a one time setup call. I really don't understand why we are still talking about this paint color.

Quoted 30 lines
Quoted 30 lines
Quoted 29 lines
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/i915/gt/intel_engine_cs.c +1 -0
+		}
    	}
    	/*
drivers/gpu/drm/i915/gt/intel_engine_types.h +1 -0
index ed91bcff20eb..fddf35546b58 100644
@@ -266,6 +266,11 @@ struct intel_engine_cs {
    	unsigned int guc_id;
    	intel_engine_mask_t mask;
+	/**
Quoted 8 lines

drivers/gpu/drm/i915/gt/intel_engine_types.h struct intel_engine_cs { · line 273

Quoted 4 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +4 -0
+	 * @logical_mask: logical mask of engine, reported to user space via
+	 * query IOCTL and used to communicate with the GuC in logical space
+	 */
+	intel_engine_mask_t logical_mask;

You could prefix the new field with uabi_ to match the existing scheme and to signify to anyone who might be touching it in the future it should not be changed.

Quoted 4 lines

This is kinda uabi, but also kinda isn't. We do report a logical instance via IOCTL but it also really is tied the GuC backend as we only can communicate with the GuC in logical space. IMO we should leave as is.

Perhaps it would be best to call the new field uabi_logical_instance so it's clear it is reported in the query directly and do the BIT() transformation in the GuC backend?

Quoted 8 lines
Quoted 3 lines

Virtual engines can have a multiple bits set in this mask, so this is used for both the query on physical engines via UABI and submission in the GuC backend.

You could add both fields if that would help. I just think it is preferrable to keep the existing convetion of uabi_ prefix for the fields which i915 refactors must not change willy-nilly.

Sure can add another field to make the separation clear.

Quoted 29 lines
Quoted 29 lines
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines

Also, I think comment should explain what is logical space ie. how the numbering works.

Don't I already do that? I suppose I could add something like:

Where is it? Can't see it in the uapi kerneldoc AFAICS (for the new query) or here.

	/**
	 * @logical_mask: logical mask of engine, reported to user space via
	 * query IOCTL and used to communicate with the GuC in logical space
	 */
Quoted 9 lines
Quoted 3 lines

The logical mask within engine class must be contiguous across all instances.

Best not to start mentioning the mask for the first time. Just explain what logical numbering is in terms of how engines are enumerated in order of physical instances but skipping the fused off ones. In the kerneldoc for the new query is I think the right place.

Maybe I can add:

Quoted 9 lines
Quoted 2 lines

The logical mapping is defined on per part basis in the bspec and can very based the parts fusing.

Sounds good, I think that would be useful. But in the uapi kerneldoc.

Perhaps a cross-link to/from the kernel doc which talks about frame split to explain consecutive logical instances have to be used. That would tie the new query with that uapi in the narrative.

Sure, let me see what I can do here.

Matt

Quoted 101 lines

Regards,

Tvrtko

Quoted 97 lines
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

Not sure the part about GuC needs to be in the comment since uapi is supposed to be backend agnostic.

Communicating with the GuC in logical space is a pretty key point here. The communication with the GuC in logical space is backend specific but how our hardware works (e.g. split frame workloads must be placed logical contiguous) is not. Mentioning the GuC requirement here makes sense to me for completeness.

Quoted 4 lines

Yeah might be, I was thinking more about the new query. Query definitely is backend agnostic but yes it is fine to say in the comment here the new field is used both for the query and for communicating with GuC.

Sounds good, will make it clear it used for the query and from communicating with the GuC.

Matt

Quoted 78 lines

Regards,

Tvrtko

Quoted 74 lines

Matt

Quoted 71 lines

Regards,

Tvrtko

Quoted 67 lines
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -0
    	u8 class;
    	u8 instance;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +1 -0
index cafb0608ffb4..813a6de01382 100644
@@ -3875,6 +3875,7 @@ execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
    		ve->siblings[ve->num_siblings++] = sibling;
    		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
    		/*
    		 * All physical engines must be compatible for their emission
drivers/gpu/drm/i915/gt/uc/intel_guc_ads.c +1 -1
index 6926919bcac6..9f5f43a16182 100644
@@ -176,7 +176,7 @@ static void guc_mapping_table_init(struct intel_gt *gt,
    	for_each_engine(engine, gt, id) {
    		u8 guc_class = engine_class_to_guc_class(engine->class);
-		system_info->mapping_table[guc_class][engine->instance] =
+		system_info->mapping_table[guc_class][ilog2(engine->logical_mask)] =
    			engine->instance;
    	}
    }
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -19
index e0eed70f9b92..ffafbac7335e 100644
@@ -1401,23 +1401,6 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
    	return __guc_action_deregister_context(guc, guc_id, loop);
    }
-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:
-		MISSING_CASE(class);
-		return 0;
-	}
-}
-
    static void guc_context_policy_init(struct intel_engine_cs *engine,
    				    struct guc_lrc_desc *desc)
    {
@@ -1459,8 +1442,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
    	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->engine_submit_mask = engine->logical_mask;
    	desc->hw_context_desc = ce->lrc.lrca;
    	desc->priority = ce->guc_state.prio;
    	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
@@ -3260,6 +3242,7 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count)
    		}
    		ve->base.mask |= sibling->mask;
+		ve->base.logical_mask |= sibling->logical_mask;
    		if (n != 0 && ve->base.class != sibling->class) {
    			DRM_DEBUG("invalid mixing of engine class, sibling %d, already %d\n",
 
#39 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 2 lines · Matthew Brost
Quoted 1 line · Tvrtko Ursulin

8<

Quoted 39 lines
Quoted 39 lines

Today we have:

for_each intel_engines: // intel_engines is a flat list of all engines
intel_engine_setup()

You propose to change it to:

for_each engine_class:
for 0..max_global_engine_instance:
for_each intel_engines:
skip engine not present
skip class not matching

count logical instance

for_each intel_engines:
skip engine not present
skip wrong class

intel_engine_setup()

I propose:

// Leave as is:

for_each intel_engines:
intel_engine_setup()

// Add:

for_each engine_class:
logical = 0
for_each gt->engine_class[class]:
skip engine not present

engine->logical_instance = logical++

Quoted 8 lines
Quoted 3 lines

When code which actually needs a preturbed "map" arrives you add that in to this second loop.

See above, why introduce an algorithm that doesn't work for future parts
+ future patches are land imminently? It makes zero sense whatsoever. With your proposal we would literally land code to just throw it away a couple of months from now + break patches we intend to land soon. This

It sure works, it just walks the per class list instead of walking the flat list skipping one class at the time.

Just add the map based transformation to the second pass later, when it becomes required.

Quoted 3 lines

algorithm works and has no reason whatsoever to be optimal as it a one time setup call. I really don't understand why we are still talking about this paint color.

I don't think bike shedding is not an appropriate term when complaint is how proposed algorithm is needlessly complicated.

Regards,

Tvrtko

#40 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 55 lines · Tvrtko Ursulin
Quoted 2 lines · Matthew Brost
Quoted 1 line · Tvrtko Ursulin

8<

Quoted 39 lines
Quoted 39 lines

Today we have:

for_each intel_engines: // intel_engines is a flat list of all engines
intel_engine_setup()

You propose to change it to:

for_each engine_class:
for 0..max_global_engine_instance:
for_each intel_engines:
skip engine not present
skip class not matching

count logical instance

for_each intel_engines:
skip engine not present
skip wrong class

intel_engine_setup()

I propose:

// Leave as is:

for_each intel_engines:
intel_engine_setup()

// Add:

for_each engine_class:
logical = 0
for_each gt->engine_class[class]:
skip engine not present

engine->logical_instance = logical++

Quoted 8 lines
Quoted 3 lines

When code which actually needs a preturbed "map" arrives you add that in to this second loop.

See above, why introduce an algorithm that doesn't work for future parts
+ future patches are land imminently? It makes zero sense whatsoever. With your proposal we would literally land code to just throw it away a couple of months from now + break patches we intend to land soon. This

Quoted 6 lines

It sure works, it just walks the per class list instead of walking the flat list skipping one class at the time.

Just add the map based transformation to the second pass later, when it becomes required.

I can flatten the algorithm if that helps alleviate your concerns but with that being said, I've played around this locally and IMO makes the code way more ugly. Sure it eliminates some iterations of the loop but who really cares about that in a one time setup function?

Quoted 7 lines
Quoted 3 lines

algorithm works and has no reason whatsoever to be optimal as it a one time setup call. I really don't understand why we are still talking about this paint color.

I don't think bike shedding is not an appropriate term when complaint is how proposed algorithm is needlessly complicated.

Are you just ignoring the fact that the algorithm (map) is needed in pending patches? IMO it is more complicated to write throw away code when the proper algorithm is already written. If the logical mapping was straight forward on all platforms as the ones currently upstream I would 100% agree with your suggestion, but it isn't on unembargoed platforms eminently going upstream. The algorithm I have works for the current platforms + the pending platforms. IMO is 100% acceptable to merge something looking towards a known future.

Matt

Quoted 3 lines

Regards,

Tvrtko

#41 lore raw

Re: [Intel-gfx] [PATCH 08/27] drm/i915: Add logical engine mapping

Quoted 76 lines · Matthew Brost
Quoted 55 lines · Tvrtko Ursulin
Quoted 2 lines · Matthew Brost
Quoted 1 line · Tvrtko Ursulin

8<

Quoted 39 lines
Quoted 39 lines

Today we have:

for_each intel_engines: // intel_engines is a flat list of all engines
intel_engine_setup()

You propose to change it to:

for_each engine_class:
for 0..max_global_engine_instance:
for_each intel_engines:
skip engine not present
skip class not matching

count logical instance

for_each intel_engines:
skip engine not present
skip wrong class

intel_engine_setup()

I propose:

// Leave as is:

for_each intel_engines:
intel_engine_setup()

// Add:

for_each engine_class:
logical = 0
for_each gt->engine_class[class]:
skip engine not present

engine->logical_instance = logical++

Quoted 8 lines
Quoted 3 lines

When code which actually needs a preturbed "map" arrives you add that in to this second loop.

See above, why introduce an algorithm that doesn't work for future parts
+ future patches are land imminently? It makes zero sense whatsoever. With your proposal we would literally land code to just throw it away a couple of months from now + break patches we intend to land soon. This

Quoted 6 lines

It sure works, it just walks the per class list instead of walking the flat list skipping one class at the time.

Just add the map based transformation to the second pass later, when it becomes required.

I can flatten the algorithm if that helps alleviate your concerns but with that being said, I've played around this locally and IMO makes the code way more ugly. Sure it eliminates some iterations of the loop but who really cares about that in a one time setup function?

Quoted 7 lines
Quoted 3 lines

algorithm works and has no reason whatsoever to be optimal as it a one time setup call. I really don't understand why we are still talking about this paint color.

I don't think bike shedding is not an appropriate term when complaint is how proposed algorithm is needlessly complicated.

Quoted 8 lines

Are you just ignoring the fact that the algorithm (map) is needed in pending patches? IMO it is more complicated to write throw away code when the proper algorithm is already written. If the logical mapping was straight forward on all platforms as the ones currently upstream I would 100% agree with your suggestion, but it isn't on unembargoed platforms eminently going upstream. The algorithm I have works for the current platforms + the pending platforms. IMO is 100% acceptable to merge something looking towards a known future.

FWIW my 2c is that unused bits detract from review. And my gut feeling still is that code can be written in a simpler way and that the map transform can still plug in easily on top in a later series.

I said FWIW since even if I am right you can still view my comments as external/community inputs at this point and proceed however you wish.

Regards,

Tvrtko

#42 lore raw

PATCH 9/27 drm/i915: Expose logical engine instance to user

In reply to Matthew Brost

Expose logical engine instance to user via query engine info IOCTL. This is required for split-frame workloads as these needs to be placed on engines in a logically contiguous order. The logical mapping can change based on fusing. Rather than having user have knowledge of the fusing we simply just expose the logical mapping with the existing query engine info IOCTL.

IGT: https://patchwork.freedesktop.org/patch/445637/?series=92854&rev=1
media UMD: link coming soon

v2:
(Daniel Vetter)
- Add IGT link, placeholder for media UMD

---
 drivers/gpu/drm/i915/i915_query.c | 2 ++
 include/uapi/drm/i915_drm.h       | 8 +++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_query.c +2 -0
index e49da36c62fb..8a72923fbdba 100644
@@ -124,7 +124,9 @@ query_engine_info(struct drm_i915_private *i915,
 	for_each_uabi_engine(engine, i915) {
 		info.engine.engine_class = engine->uabi_class;
 		info.engine.engine_instance = engine->uabi_instance;
+		info.flags = I915_ENGINE_INFO_HAS_LOGICAL_INSTANCE;
 		info.capabilities = engine->uabi_capabilities;
+		info.logical_instance = ilog2(engine->logical_mask);
 
 		if (copy_to_user(info_ptr, &info, sizeof(info)))
 			return -EFAULT;
include/uapi/drm/i915_drm.h +7 -1
index bde5860b3686..b1248a67b4f8 100644
@@ -2726,14 +2726,20 @@ struct drm_i915_engine_info {
 
 	/** @flags: Engine flags. */
 	__u64 flags;
+#define I915_ENGINE_INFO_HAS_LOGICAL_INSTANCE		(1 << 0)
 
 	/** @capabilities: Capabilities of this engine. */
 	__u64 capabilities;
 #define I915_VIDEO_CLASS_CAPABILITY_HEVC		(1 << 0)
 #define I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC	(1 << 1)
 
+	/** @logical_instance: Logical instance of engine */
+	__u16 logical_instance;
+
 	/** @rsvd1: Reserved fields. */
-	__u64 rsvd1[4];
+	__u16 rsvd1[3];
+	/** @rsvd2: Reserved fields. */
+	__u64 rsvd2[3];
 };
 
 /**
Signature · 1 line
2.32.0
Reviewed-by on v1 9/27
#43 lore raw

Re: [Intel-gfx] [PATCH 09/27] drm/i915: Expose logical engine instance to user

Quoted 51 lines · Matthew Brost

Expose logical engine instance to user via query engine info IOCTL. This is required for split-frame workloads as these needs to be placed on engines in a logically contiguous order. The logical mapping can change based on fusing. Rather than having user have knowledge of the fusing we simply just expose the logical mapping with the existing query engine info IOCTL.

IGT: https://patchwork.freedesktop.org/patch/445637/?series=92854&rev=1
media UMD: link coming soon

v2:
(Daniel Vetter)
- Add IGT link, placeholder for media UMD

---
  drivers/gpu/drm/i915/i915_query.c | 2 ++
  include/uapi/drm/i915_drm.h       | 8 +++++++-
  2 files changed, 9 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_query.c +2 -0
index e49da36c62fb..8a72923fbdba 100644
@@ -124,7 +124,9 @@ query_engine_info(struct drm_i915_private *i915,
  	for_each_uabi_engine(engine, i915) {
  		info.engine.engine_class = engine->uabi_class;
  		info.engine.engine_instance = engine->uabi_instance;
+		info.flags = I915_ENGINE_INFO_HAS_LOGICAL_INSTANCE;
  		info.capabilities = engine->uabi_capabilities;
+		info.logical_instance = ilog2(engine->logical_mask);
 
  		if (copy_to_user(info_ptr, &info, sizeof(info)))
  			return -EFAULT;
include/uapi/drm/i915_drm.h +2 -0
index bde5860b3686..b1248a67b4f8 100644
@@ -2726,14 +2726,20 @@ struct drm_i915_engine_info {
 
  	/** @flags: Engine flags. */
  	__u64 flags;
+#define I915_ENGINE_INFO_HAS_LOGICAL_INSTANCE		(1 << 0)
 
  	/** @capabilities: Capabilities of this engine. */
  	__u64 capabilities;
  #define I915_VIDEO_CLASS_CAPABILITY_HEVC		(1 << 0)
  #define I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC	(1 << 1)
 
+	/** @logical_instance: Logical instance of engine */

include/uapi/drm/i915_drm.h struct drm_i915_engine_info { · line 2745

Quoted 10 lines
include/uapi/drm/i915_drm.h +5 -1
+	__u16 logical_instance;
+
  	/** @rsvd1: Reserved fields. */
-	__u64 rsvd1[4];
+	__u16 rsvd1[3];
+	/** @rsvd2: Reserved fields. */
+	__u64 rsvd2[3];
  };
 
  /**

Any idea why the padding? Would be useful if the comment said 'this structure must be at least/exactly X bytes in size / a multiple of X bytes in size because ...' or whatever.

However, not really anything to do with this patch as such, so either way:

#44 lore raw

Re: [Intel-gfx] [PATCH 09/27] drm/i915: Expose logical engine instance to user

Quoted 52 lines · John Harrison
Quoted 51 lines · Matthew Brost

Expose logical engine instance to user via query engine info IOCTL. This is required for split-frame workloads as these needs to be placed on engines in a logically contiguous order. The logical mapping can change based on fusing. Rather than having user have knowledge of the fusing we simply just expose the logical mapping with the existing query engine info IOCTL.

IGT: https://patchwork.freedesktop.org/patch/445637/?series=92854&rev=1
media UMD: link coming soon

v2:
(Daniel Vetter)
- Add IGT link, placeholder for media UMD

---
  drivers/gpu/drm/i915/i915_query.c | 2 ++
  include/uapi/drm/i915_drm.h       | 8 +++++++-
  2 files changed, 9 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_query.c +2 -0
index e49da36c62fb..8a72923fbdba 100644
@@ -124,7 +124,9 @@ query_engine_info(struct drm_i915_private *i915,
  	for_each_uabi_engine(engine, i915) {
  		info.engine.engine_class = engine->uabi_class;
  		info.engine.engine_instance = engine->uabi_instance;
+		info.flags = I915_ENGINE_INFO_HAS_LOGICAL_INSTANCE;
  		info.capabilities = engine->uabi_capabilities;
+		info.logical_instance = ilog2(engine->logical_mask);
  		if (copy_to_user(info_ptr, &info, sizeof(info)))
  			return -EFAULT;
include/uapi/drm/i915_drm.h +4 -1
index bde5860b3686..b1248a67b4f8 100644
@@ -2726,14 +2726,20 @@ struct drm_i915_engine_info {
  	/** @flags: Engine flags. */
  	__u64 flags;
+#define I915_ENGINE_INFO_HAS_LOGICAL_INSTANCE		(1 << 0)
  	/** @capabilities: Capabilities of this engine. */
  	__u64 capabilities;
  #define I915_VIDEO_CLASS_CAPABILITY_HEVC		(1 << 0)
  #define I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC	(1 << 1)
+	/** @logical_instance: Logical instance of engine */
+	__u16 logical_instance;
+
  	/** @rsvd1: Reserved fields. */
-	__u64 rsvd1[4];
Quoted 9 lines

include/uapi/drm/i915_drm.h struct drm_i915_engine_info { · line 2745

Quoted 5 lines
include/uapi/drm/i915_drm.h +3 -0
+	__u16 rsvd1[3];
+	/** @rsvd2: Reserved fields. */
+	__u64 rsvd2[3];
  };
  /**

Any idea why the padding? Would be useful if the comment said 'this structure must be at least/exactly X bytes in size / a multiple of X bytes in size because ...' or whatever.

I think this is pretty standard in UABI interface - add a bunch of padding bits in case you need them in the future (i.e. this patch is an example of that).

Matt

Quoted 3 lines

However, not really anything to do with this patch as such, so either way:

#45 lore raw

PATCH 10/27 drm/i915/guc: Introduce context parent-child relationship

In reply to Matthew Brost

Introduce context parent-child relationship. Once this relationship is created all pinning / unpinning operations are directed to the parent context. The parent context is responsible for pinning all of its' children and itself.

This is a precursor to the full GuC multi-lrc implementation but aligns to how GuC mutli-lrc interface is defined - a single H2G is used register / deregister all of the contexts simultaneously.

Subsequent patches in the series will implement the pinning / unpinning operations for parent / child contexts.

v2:
(Daniel Vetter)
- Add kernel doc, add wrapper to access parent to ensure safety

---
 drivers/gpu/drm/i915/gt/intel_context.c       | 29 ++++++++++++++
 drivers/gpu/drm/i915/gt/intel_context.h       | 39 +++++++++++++++++++
 drivers/gpu/drm/i915/gt/intel_context_types.h | 23 +++++++++++
 3 files changed, 91 insertions(+)
drivers/gpu/drm/i915/gt/intel_context.c +29 -0
index 508cfe5770c0..00d1aee6d199 100644
@@ -404,6 +404,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 
 	INIT_LIST_HEAD(&ce->destroyed_link);
 
+	INIT_LIST_HEAD(&ce->guc_child_list);
+
 	/*
 	 * Initialize fence to be complete as this is expected to be complete
 	 * unless there is a pending schedule disable outstanding.
@@ -418,10 +420,17 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 
 void intel_context_fini(struct intel_context *ce)
 {
+	struct intel_context *child, *next;
+
 	if (ce->timeline)
 		intel_timeline_put(ce->timeline);
 	i915_vm_put(ce->vm);
 
+	/* Need to put the creation ref for the children */
+	if (intel_context_is_parent(ce))
+		for_each_child_safe(ce, child, next)
+			intel_context_put(child);
+
 	mutex_destroy(&ce->pin_mutex);
 	i915_active_fini(&ce->active);
 }
@@ -537,6 +546,26 @@ struct i915_request *intel_context_find_active_request(struct intel_context *ce)
 	return active;
 }
 
+void intel_context_bind_parent_child(struct intel_context *parent,
+				     struct intel_context *child)
+{
+	/*
+	 * Callers responsibility to validate that this function is used
+	 * correctly but we use GEM_BUG_ON here ensure that they do.
+	 */
+	GEM_BUG_ON(!intel_engine_uses_guc(parent->engine));
+	GEM_BUG_ON(intel_context_is_pinned(parent));
+	GEM_BUG_ON(intel_context_is_child(parent));
+	GEM_BUG_ON(intel_context_is_pinned(child));
+	GEM_BUG_ON(intel_context_is_child(child));
+	GEM_BUG_ON(intel_context_is_parent(child));
+
+	parent->guc_number_children++;
+	list_add_tail(&child->guc_child_link,
+		      &parent->guc_child_list);
+	child->parent = parent;
+}
+
 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
 #include "selftest_context.c"
 #endif
drivers/gpu/drm/i915/gt/intel_context.h +39 -0
index c41098950746..c2985822ab74 100644
@@ -44,6 +44,45 @@ void intel_context_free(struct intel_context *ce);
 int intel_context_reconfigure_sseu(struct intel_context *ce,
 				   const struct intel_sseu sseu);
 
+static inline bool intel_context_is_child(struct intel_context *ce)
+{
+	return !!ce->parent;
+}
+
+static inline bool intel_context_is_parent(struct intel_context *ce)
+{
+	return !!ce->guc_number_children;
+}
+
+static inline bool intel_context_is_pinned(struct intel_context *ce);
+
+static inline struct intel_context *
+intel_context_to_parent(struct intel_context *ce)
+{
+        if (intel_context_is_child(ce)) {
+		/*
+		 * The parent holds ref count to the child so it is always safe
+		 * for the parent to access the child, but the child has pointer
+		 * to the parent without a ref. To ensure this is safe the child
+		 * should only access the parent pointer while the parent is
+		 * pinned.
+		 */
+                GEM_BUG_ON(!intel_context_is_pinned(ce->parent));
+
+                return ce->parent;
+        } else {
+                return ce;
+        }
+}
+
+void intel_context_bind_parent_child(struct intel_context *parent,
+				     struct intel_context *child);
+
+#define for_each_child(parent, ce)\
+	list_for_each_entry(ce, &(parent)->guc_child_list, guc_child_link)
+#define for_each_child_safe(parent, ce, cn)\
+	list_for_each_entry_safe(ce, cn, &(parent)->guc_child_list, guc_child_link)
+
 /**
  * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
  * @ce - the context
drivers/gpu/drm/i915/gt/intel_context_types.h +23 -0
index fd338a30617e..0fafc178cf2c 100644
@@ -213,6 +213,29 @@ struct intel_context {
 	 */
 	struct list_head destroyed_link;
 
+	/** anonymous struct for parent / children only members */
+	struct {
+		union {
+			/**
+			 * @guc_child_list: parent's list of of children
+			 * contexts, no protection as immutable after context
+			 * creation
+			 */
+			struct list_head guc_child_list;
+			/**
+			 * @guc_child_link: child's link into parent's list of
+			 * children
+			 */
+			struct list_head guc_child_link;
+		};
+
+		/** @parent: pointer to parent if child */
+		struct intel_context *parent;
+
+		/** @guc_number_children: number of children if parent */
+		u8 guc_number_children;
+	};
+
 #ifdef CONFIG_DRM_I915_SELFTEST
 	/**
 	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
Signature · 1 line
2.32.0
#46 lore raw

Re: [Intel-gfx] [PATCH 10/27] drm/i915/guc: Introduce context parent-child relationship

Quoted 28 lines · Matthew Brost

Introduce context parent-child relationship. Once this relationship is created all pinning / unpinning operations are directed to the parent context. The parent context is responsible for pinning all of its' children and itself.

This is a precursor to the full GuC multi-lrc implementation but aligns to how GuC mutli-lrc interface is defined - a single H2G is used register / deregister all of the contexts simultaneously.

Subsequent patches in the series will implement the pinning / unpinning operations for parent / child contexts.

v2:
(Daniel Vetter)
- Add kernel doc, add wrapper to access parent to ensure safety

---
  drivers/gpu/drm/i915/gt/intel_context.c       | 29 ++++++++++++++
  drivers/gpu/drm/i915/gt/intel_context.h       | 39 +++++++++++++++++++
  drivers/gpu/drm/i915/gt/intel_context_types.h | 23 +++++++++++
  3 files changed, 91 insertions(+)
drivers/gpu/drm/i915/gt/intel_context.c +0 -0
index 508cfe5770c0..00d1aee6d199 100644

drivers/gpu/drm/i915/gt/intel_context.c intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine) · line 406

Quoted 3 lines
drivers/gpu/drm/i915/gt/intel_context.c +0 -0
@@ -404,6 +404,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 
  	INIT_LIST_HEAD(&ce->destroyed_link);
 

No need for this blank line?

Quoted 59 lines
drivers/gpu/drm/i915/gt/intel_context.c +29 -0
+	INIT_LIST_HEAD(&ce->guc_child_list);
+
  	/*
  	 * Initialize fence to be complete as this is expected to be complete
  	 * unless there is a pending schedule disable outstanding.
@@ -418,10 +420,17 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
 
  void intel_context_fini(struct intel_context *ce)
  {
+	struct intel_context *child, *next;
+
  	if (ce->timeline)
  		intel_timeline_put(ce->timeline);
  	i915_vm_put(ce->vm);
 
+	/* Need to put the creation ref for the children */
+	if (intel_context_is_parent(ce))
+		for_each_child_safe(ce, child, next)
+			intel_context_put(child);
+
  	mutex_destroy(&ce->pin_mutex);
  	i915_active_fini(&ce->active);
  }
@@ -537,6 +546,26 @@ struct i915_request *intel_context_find_active_request(struct intel_context *ce)
  	return active;
  }
 
+void intel_context_bind_parent_child(struct intel_context *parent,
+				     struct intel_context *child)
+{
+	/*
+	 * Callers responsibility to validate that this function is used
+	 * correctly but we use GEM_BUG_ON here ensure that they do.
+	 */
+	GEM_BUG_ON(!intel_engine_uses_guc(parent->engine));
+	GEM_BUG_ON(intel_context_is_pinned(parent));
+	GEM_BUG_ON(intel_context_is_child(parent));
+	GEM_BUG_ON(intel_context_is_pinned(child));
+	GEM_BUG_ON(intel_context_is_child(child));
+	GEM_BUG_ON(intel_context_is_parent(child));
+
+	parent->guc_number_children++;
+	list_add_tail(&child->guc_child_link,
+		      &parent->guc_child_list);
+	child->parent = parent;
+}
+
  #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  #include "selftest_context.c"
  #endif
drivers/gpu/drm/i915/gt/intel_context.h +1 -0
index c41098950746..c2985822ab74 100644
@@ -44,6 +44,45 @@ void intel_context_free(struct intel_context *ce);
  int intel_context_reconfigure_sseu(struct intel_context *ce,
  				   const struct intel_sseu sseu);
 
+static inline bool intel_context_is_child(struct intel_context *ce)

drivers/gpu/drm/i915/gt/intel_context.h void intel_context_free(struct intel_context *ce); · line 57

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_context.h +10 -0
+{
+	return !!ce->parent;
+}
+
+static inline bool intel_context_is_parent(struct intel_context *ce)
+{
+	return !!ce->guc_number_children;
+}
+
+static inline bool intel_context_is_pinned(struct intel_context *ce);

No point declaring 'static inline' if there is no function body?

drivers/gpu/drm/i915/gt/intel_context.h void intel_context_free(struct intel_context *ce); · line 65

Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_context.h +8 -0
+
+static inline struct intel_context *
+intel_context_to_parent(struct intel_context *ce)
+{
+        if (intel_context_is_child(ce)) {
+		/*
+		 * The parent holds ref count to the child so it is always safe
+		 * for the parent to access the child, but the child has pointer

has pointer -> has a pointer

Quoted 9 lines
drivers/gpu/drm/i915/gt/intel_context.h +9 -0
+		 * to the parent without a ref. To ensure this is safe the child
+		 * should only access the parent pointer while the parent is
+		 * pinned.
+		 */
+                GEM_BUG_ON(!intel_context_is_pinned(ce->parent));
+
+                return ce->parent;
+        } else {
+                return ce;

drivers/gpu/drm/i915/gt/intel_context.h void intel_context_free(struct intel_context *ce); · line 84

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_context.h +10 -0
+        }
+}
+
+void intel_context_bind_parent_child(struct intel_context *parent,
+				     struct intel_context *child);
+
+#define for_each_child(parent, ce)\
+	list_for_each_entry(ce, &(parent)->guc_child_list, guc_child_link)
+#define for_each_child_safe(parent, ce, cn)\
+	list_for_each_entry_safe(ce, cn, &(parent)->guc_child_list, guc_child_link)

Do these macros not need some kind of intel_context prefix? Or at least be 'for_each_guc_child' given the naming of the list/link fields? But maybe not if the guc_ is dropped from the variable names - see below.

Quoted 23 lines
drivers/gpu/drm/i915/gt/intel_context.h +1 -0
+
  /**
   * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
   * @ce - the context
drivers/gpu/drm/i915/gt/intel_context_types.h +11 -0
index fd338a30617e..0fafc178cf2c 100644
@@ -213,6 +213,29 @@ struct intel_context {
  	 */
  	struct list_head destroyed_link;
 
+	/** anonymous struct for parent / children only members */
+	struct {
+		union {
+			/**
+			 * @guc_child_list: parent's list of of children
+			 * contexts, no protection as immutable after context
+			 * creation
+			 */
+			struct list_head guc_child_list;
+			/**
+			 * @guc_child_link: child's link into parent's list of

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 236

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -0
+			 * children
+			 */
+			struct list_head guc_child_link;
+		};
+
+		/** @parent: pointer to parent if child */
+		struct intel_context *parent;
+
+		/** @guc_number_children: number of children if parent */
+		u8 guc_number_children;

These are not really a GuC specific fields? The parent/child thing might only be necessary for GuC submission (although can you say it won't be required by any future backend, such as the DRM scheduler?) but it is a context level concept. None of the files changed in this patch are GuC specific. So no need for 'guc_' prefix? Alternatively, if it all really is completely GuC specific then the 'parent' field should also have the prefix? Or even just name the outer struct 'guc_family' or something and drop the prefixes from all the inner members.

John.

Quoted 5 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +2 -0
+	};
+
  #ifdef CONFIG_DRM_I915_SELFTEST
  	/**
  	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
#47 lore raw

Re: [Intel-gfx] [PATCH 10/27] drm/i915/guc: Introduce context parent-child relationship

Quoted 24 lines · John Harrison
Quoted 23 lines · Matthew Brost

Introduce context parent-child relationship. Once this relationship is created all pinning / unpinning operations are directed to the parent context. The parent context is responsible for pinning all of its' children and itself.

This is a precursor to the full GuC multi-lrc implementation but aligns to how GuC mutli-lrc interface is defined - a single H2G is used register / deregister all of the contexts simultaneously.

Subsequent patches in the series will implement the pinning / unpinning operations for parent / child contexts.

v2:
(Daniel Vetter)
- Add kernel doc, add wrapper to access parent to ensure safety

---
  drivers/gpu/drm/i915/gt/intel_context.c       | 29 ++++++++++++++
  drivers/gpu/drm/i915/gt/intel_context.h       | 39 +++++++++++++++++++
  drivers/gpu/drm/i915/gt/intel_context_types.h | 23 +++++++++++
  3 files changed, 91 insertions(+)
Quoted 8 lines

drivers/gpu/drm/i915/gt/intel_context.c intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine) · line 404

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_context.c +0 -0
index 508cfe5770c0..00d1aee6d199 100644
@@ -404,6 +404,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  	INIT_LIST_HEAD(&ce->destroyed_link);

No need for this blank line?

I guess but typically I try to put blank lines between each different set of variables (e.g. a lock and list would next to each other if the lock protects the list, two different lists would have a blank line like this case here).

Quoted 58 lines
Quoted 58 lines
drivers/gpu/drm/i915/gt/intel_context.c +29 -0
+	INIT_LIST_HEAD(&ce->guc_child_list);
+
  	/*
  	 * Initialize fence to be complete as this is expected to be complete
  	 * unless there is a pending schedule disable outstanding.
@@ -418,10 +420,17 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine)
  void intel_context_fini(struct intel_context *ce)
  {
+	struct intel_context *child, *next;
+
  	if (ce->timeline)
  		intel_timeline_put(ce->timeline);
  	i915_vm_put(ce->vm);
+	/* Need to put the creation ref for the children */
+	if (intel_context_is_parent(ce))
+		for_each_child_safe(ce, child, next)
+			intel_context_put(child);
+
  	mutex_destroy(&ce->pin_mutex);
  	i915_active_fini(&ce->active);
  }
@@ -537,6 +546,26 @@ struct i915_request *intel_context_find_active_request(struct intel_context *ce)
  	return active;
  }
+void intel_context_bind_parent_child(struct intel_context *parent,
+				     struct intel_context *child)
+{
+	/*
+	 * Callers responsibility to validate that this function is used
+	 * correctly but we use GEM_BUG_ON here ensure that they do.
+	 */
+	GEM_BUG_ON(!intel_engine_uses_guc(parent->engine));
+	GEM_BUG_ON(intel_context_is_pinned(parent));
+	GEM_BUG_ON(intel_context_is_child(parent));
+	GEM_BUG_ON(intel_context_is_pinned(child));
+	GEM_BUG_ON(intel_context_is_child(child));
+	GEM_BUG_ON(intel_context_is_parent(child));
+
+	parent->guc_number_children++;
+	list_add_tail(&child->guc_child_link,
+		      &parent->guc_child_list);
+	child->parent = parent;
+}
+
  #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  #include "selftest_context.c"
  #endif
drivers/gpu/drm/i915/gt/intel_context.h +4 -0
index c41098950746..c2985822ab74 100644
@@ -44,6 +44,45 @@ void intel_context_free(struct intel_context *ce);
  int intel_context_reconfigure_sseu(struct intel_context *ce,
  				   const struct intel_sseu sseu);
+static inline bool intel_context_is_child(struct intel_context *ce)
+{
+	return !!ce->parent;
+}
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_context.h void intel_context_free(struct intel_context *ce); · line 57

Quoted 7 lines
drivers/gpu/drm/i915/gt/intel_context.h +7 -0
+
+static inline bool intel_context_is_parent(struct intel_context *ce)
+{
+	return !!ce->guc_number_children;
+}
+
+static inline bool intel_context_is_pinned(struct intel_context *ce);

No point declaring 'static inline' if there is no function body?

Forward delc for the below function.

Quoted 10 lines

drivers/gpu/drm/i915/gt/intel_context.h void intel_context_free(struct intel_context *ce); · line 65

Quoted 8 lines
drivers/gpu/drm/i915/gt/intel_context.h +8 -0
+
+static inline struct intel_context *
+intel_context_to_parent(struct intel_context *ce)
+{
+        if (intel_context_is_child(ce)) {
+		/*
+		 * The parent holds ref count to the child so it is always safe
+		 * for the parent to access the child, but the child has pointer

has pointer -> has a pointer

Yep.

Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/i915/gt/intel_context.h +14 -0
+		 * to the parent without a ref. To ensure this is safe the child
+		 * should only access the parent pointer while the parent is
+		 * pinned.
+		 */
+                GEM_BUG_ON(!intel_context_is_pinned(ce->parent));
+
+                return ce->parent;
+        } else {
+                return ce;
+        }
+}
+
+void intel_context_bind_parent_child(struct intel_context *parent,
+				     struct intel_context *child);
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_context.h void intel_context_free(struct intel_context *ce); · line 84

Quoted 5 lines
drivers/gpu/drm/i915/gt/intel_context.h +5 -0
+
+#define for_each_child(parent, ce)\
+	list_for_each_entry(ce, &(parent)->guc_child_list, guc_child_link)
+#define for_each_child_safe(parent, ce, cn)\
+	list_for_each_entry_safe(ce, cn, &(parent)->guc_child_list, guc_child_link)

Do these macros not need some kind of intel_context prefix? Or at least be 'for_each_guc_child' given the naming of the list/link fields? But maybe not if the guc_ is dropped from the variable names - see below.

I like the names. Yes, the guc_* prefix should be dropped because these are used in execlists too. I can drop those prefixes in the next rev.

Quoted 32 lines
Quoted 22 lines
drivers/gpu/drm/i915/gt/intel_context.h +1 -0
+
  /**
   * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
   * @ce - the context
drivers/gpu/drm/i915/gt/intel_context_types.h +11 -0
index fd338a30617e..0fafc178cf2c 100644
@@ -213,6 +213,29 @@ struct intel_context {
  	 */
  	struct list_head destroyed_link;
+	/** anonymous struct for parent / children only members */
+	struct {
+		union {
+			/**
+			 * @guc_child_list: parent's list of of children
+			 * contexts, no protection as immutable after context
+			 * creation
+			 */
+			struct list_head guc_child_list;
+			/**
+			 * @guc_child_link: child's link into parent's list of

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 235

Quoted 10 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +10 -0
+			 * children
+			 */
+			struct list_head guc_child_link;
+		};
+
+		/** @parent: pointer to parent if child */
+		struct intel_context *parent;
+
+		/** @guc_number_children: number of children if parent */
+		u8 guc_number_children;
Quoted 9 lines

These are not really a GuC specific fields? The parent/child thing might only be necessary for GuC submission (although can you say it won't be required by any future backend, such as the DRM scheduler?) but it is a context level concept. None of the files changed in this patch are GuC specific. So no need for 'guc_' prefix? Alternatively, if it all really is completely GuC specific then the 'parent' field should also have the prefix? Or even just name the outer struct 'guc_family' or something and drop the prefixes from all the inner members.

Yep, will drop. Originally only used for GuC submission but now a generic concept.

Matt

Quoted 8 lines

John.

Quoted 5 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +2 -0
+	};
+
  #ifdef CONFIG_DRM_I915_SELFTEST
  	/**
  	 * @drop_schedule_enable: Force drop of schedule enable G2H for selftest
#48 lore raw

PATCH 11/27 drm/i915/guc: Implement parallel context pin / unpin functions

In reply to Matthew Brost

Parallel contexts are perma-pinned by the upper layers which makes the backend implementation rather simple. The parent pins the guc_id and children increment the parent's pin count on pin to ensure all the contexts are unpinned before we disable scheduling with the GuC / or deregister the context.

v2:
(Daniel Vetter)
- Perma-pin parallel contexts

---
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 70 +++++++++++++++++++
 1 file changed, 70 insertions(+)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +70 -0
index ffafbac7335e..14b24298cdd7 100644
@@ -2395,6 +2395,76 @@ static const struct intel_context_ops virtual_guc_context_ops = {
 	.get_sibling = guc_virtual_get_sibling,
 };
 
+/* Future patches will use this function */
+__maybe_unused
+static int guc_parent_context_pin(struct intel_context *ce, void *vaddr)
+{
+	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+	struct intel_guc *guc = ce_to_guc(ce);
+	int ret;
+
+	GEM_BUG_ON(!intel_context_is_parent(ce));
+	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
+
+	ret = pin_guc_id(guc, ce);
+	if (unlikely(ret < 0))
+		return ret;
+
+	return __guc_context_pin(ce, engine, vaddr);
+}
+
+/* Future patches will use this function */
+__maybe_unused
+static int guc_child_context_pin(struct intel_context *ce, void *vaddr)
+{
+	struct intel_engine_cs *engine = guc_virtual_get_sibling(ce->engine, 0);
+
+	GEM_BUG_ON(!intel_context_is_child(ce));
+	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
+
+	__intel_context_pin(ce->parent);
+	return __guc_context_pin(ce, engine, vaddr);
+}
+
+/* Future patches will use this function */
+__maybe_unused
+static void guc_parent_context_unpin(struct intel_context *ce)
+{
+	struct intel_guc *guc = ce_to_guc(ce);
+
+	GEM_BUG_ON(context_enabled(ce));
+	GEM_BUG_ON(intel_context_is_barrier(ce));
+	GEM_BUG_ON(!intel_context_is_parent(ce));
+	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
+
+	unpin_guc_id(guc, ce);
+	lrc_unpin(ce);
+}
+
+/* Future patches will use this function */
+__maybe_unused
+static void guc_child_context_unpin(struct intel_context *ce)
+{
+	GEM_BUG_ON(context_enabled(ce));
+	GEM_BUG_ON(intel_context_is_barrier(ce));
+	GEM_BUG_ON(!intel_context_is_child(ce));
+	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
+
+	lrc_unpin(ce);
+}
+
+/* Future patches will use this function */
+__maybe_unused
+static void guc_child_context_post_unpin(struct intel_context *ce)
+{
+	GEM_BUG_ON(!intel_context_is_child(ce));
+	GEM_BUG_ON(!intel_context_is_pinned(ce->parent));
+	GEM_BUG_ON(!intel_engine_is_virtual(ce->engine));
+
+	lrc_post_unpin(ce);
+	intel_context_unpin(ce->parent);
+}
+
 static bool
 guc_irq_enable_breadcrumbs(struct intel_breadcrumbs *b)
 {
Signature · 1 line
2.32.0
#49 lore raw

PATCH 12/27 drm/i915/guc: Add multi-lrc context registration

In reply to Matthew Brost

Add multi-lrc context registration H2G. In addition a workqueue and process descriptor are setup during multi-lrc context registration as these data structures are needed for multi-lrc submission.

---
 drivers/gpu/drm/i915/gt/intel_context_types.h |  12 ++
 drivers/gpu/drm/i915/gt/intel_lrc.c           |   5 +
 drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |   2 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 109 +++++++++++++++++-
 4 files changed, 126 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_context_types.h +12 -0
index 0fafc178cf2c..6f567ebeb039 100644
@@ -232,8 +232,20 @@ struct intel_context {
 		/** @parent: pointer to parent if child */
 		struct intel_context *parent;
 
+
+		/** @guc_wqi_head: head pointer in work queue */
+		u16 guc_wqi_head;
+		/** @guc_wqi_tail: tail pointer in work queue */
+		u16 guc_wqi_tail;
+
 		/** @guc_number_children: number of children if parent */
 		u8 guc_number_children;
+
+		/**
+		 * @parent_page: page in context used by parent for work queue,
+		 * work queue descriptor
+		 */
+		u8 parent_page;
 	};
 
 #ifdef CONFIG_DRM_I915_SELFTEST
drivers/gpu/drm/i915/gt/intel_lrc.c +5 -0
index bb4af4977920..0ddbad4e062a 100644
@@ -861,6 +861,11 @@ __lrc_alloc_state(struct intel_context *ce, struct intel_engine_cs *engine)
 		context_size += PAGE_SIZE;
 	}
 
+	if (intel_context_is_parent(ce)) {
+		ce->parent_page = context_size / PAGE_SIZE;
+		context_size += PAGE_SIZE;
+	}
+
 	obj = i915_gem_object_create_lmem(engine->i915, context_size, 0);
 	if (IS_ERR(obj))
 		obj = i915_gem_object_create_shmem(engine->i915, context_size);
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +1 -1
index fa4be13c8854..0e600a3b8f1e 100644
@@ -52,7 +52,7 @@
 
 #define GUC_DOORBELL_INVALID		256
 
-#define GUC_WQ_SIZE			(PAGE_SIZE * 2)
+#define GUC_WQ_SIZE			(PAGE_SIZE / 2)
 
 /* Work queue item header definitions */
 #define WQ_STATUS_ACTIVE		1
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +108 -1

153 lines not on this page · raw message

Signature · 1 line
2.32.0
#50 lore raw

Re: [Intel-gfx] [PATCH 12/27] drm/i915/guc: Add multi-lrc context registration

Quoted 17 lines · Matthew Brost

Add multi-lrc context registration H2G. In addition a workqueue and process descriptor are setup during multi-lrc context registration as these data structures are needed for multi-lrc submission.

---
  drivers/gpu/drm/i915/gt/intel_context_types.h |  12 ++
  drivers/gpu/drm/i915/gt/intel_lrc.c           |   5 +
  drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |   2 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 109 +++++++++++++++++-
  4 files changed, 126 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_context_types.h +0 -0
index 0fafc178cf2c..6f567ebeb039 100644

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 240

Quoted 9 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +6 -0
@@ -232,8 +232,20 @@ struct intel_context {
  		/** @parent: pointer to parent if child */
  		struct intel_context *parent;
 
+
+		/** @guc_wqi_head: head pointer in work queue */
+		u16 guc_wqi_head;
+		/** @guc_wqi_tail: tail pointer in work queue */
+		u16 guc_wqi_tail;
+

These should be in the 'guc_state' sub-struct? Would be good to keep all GuC specific content in one self-contained struct. Especially given the other child/parent fields are no going to be guc_ prefixed any more.

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 245

Quoted 5 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +3 -0
  		/** @guc_number_children: number of children if parent */
  		u8 guc_number_children;
+
+		/**
+		 * @parent_page: page in context used by parent for work queue,

Maybe 'page in context record'? Otherwise, exactly what 'context' is meant here? It isn't the 'struct intel_context'. The contetx record is saved as 'ce->state' / 'ce->lrc_reg_state', yes? Is it possible to link to either of those field? Probably not given that they don't appear to have any kerneldoc description :(. Maybe add that in too :).

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 246

Quoted 1 line
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -0
+		 * work queue descriptor

Later on, it is described as 'process descriptor and work queue'. It would be good to be consistent.

Quoted 26 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +2 -0
+		 */
+		u8 parent_page;
  	};
 
  #ifdef CONFIG_DRM_I915_SELFTEST
drivers/gpu/drm/i915/gt/intel_lrc.c +5 -0
index bb4af4977920..0ddbad4e062a 100644
@@ -861,6 +861,11 @@ __lrc_alloc_state(struct intel_context *ce, struct intel_engine_cs *engine)
  		context_size += PAGE_SIZE;
  	}
 
+	if (intel_context_is_parent(ce)) {
+		ce->parent_page = context_size / PAGE_SIZE;
+		context_size += PAGE_SIZE;
+	}
+
  	obj = i915_gem_object_create_lmem(engine->i915, context_size, 0);
  	if (IS_ERR(obj))
  		obj = i915_gem_object_create_shmem(engine->i915, context_size);
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -0
index fa4be13c8854..0e600a3b8f1e 100644

drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h line 55

Quoted 5 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +1 -1
@@ -52,7 +52,7 @@
 
  #define GUC_DOORBELL_INVALID		256
 
-#define GUC_WQ_SIZE			(PAGE_SIZE * 2)
+#define GUC_WQ_SIZE			(PAGE_SIZE / 2)

Is this size actually dictated by the GuC API? Or is it just a driver level decision? If the latter, shouldn't this be below instead?

Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -0
  /* Work queue item header definitions */
  #define WQ_STATUS_ACTIVE		1
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -0
index 14b24298cdd7..dbcb9ab28a9a 100644
@@ -340,6 +340,39 @@ static struct i915_priolist *to_priolist(struct rb_node *rb)
  	return rb_entry(rb, struct i915_priolist, node);
  }
 
+/*
+ * When using multi-lrc submission an extra page in the context state is

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static struct i915_priolist *to_priolist(struct rb_node *rb) · line 354

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+ * reserved for the process descriptor and work queue.
+ *
+ * The layout of this page is below:
+ * 0						guc_process_desc
+ * ...						unused
+ * PAGE_SIZE / 2				work queue start
+ * ...						work queue
+ * PAGE_SIZE - 1				work queue end
+ */
+#define WQ_OFFSET	(PAGE_SIZE / 2)

Can this not be derived from GUC_WQ_SIZE given that the two are fundamentally linked? E.g. '#define WQ_OFFSET (PAGE_SIZE - GUC_WQ_SIZE)'? And maybe have a '#define WQ_TOTAL_SIZE PAGE_SIZE' and use that in all of WQ_OFFSET, GUC_WQ_SIZE and the allocation itself in intel_lrc.c?

Also, the process descriptor is actually an array of descriptors sized by the number of children? Or am I misunderstanding the code below? In so, shouldn't there be a 'COMPILE_BUG_ON((MAX_ENGINE_INSTANCE * sizeof(descriptor)) < (WQ_DESC_SIZE)' where WQ_DESC_SIZE is WQ_TOTAL_SIZE - WQ_SIZE?

Quoted 9 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -0
+static u32 __get_process_desc_offset(struct intel_context *ce)
+{
+	GEM_BUG_ON(!ce->parent_page);
+
+	return ce->parent_page * PAGE_SIZE;
+}
+
+static u32 __get_wq_offset(struct intel_context *ce)
+{

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static struct i915_priolist *to_priolist(struct rb_node *rb) · line 373

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+	return __get_process_desc_offset(ce) + WQ_OFFSET;
+}
+
+static struct guc_process_desc *
+__get_process_desc(struct intel_context *ce)
+{
+	return (struct guc_process_desc *)
+		(ce->lrc_reg_state +
+		 ((__get_process_desc_offset(ce) -
+		   LRC_STATE_OFFSET) / sizeof(u32)));

Where did the LRC_STATE_OFFSET come from? Is that built in to the lrg_reg_state pointer itself? That needs to be documented somewhere.

Quoted 70 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +35 -1
+}
+
  static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
  {
  	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
@@ -1342,6 +1375,30 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
 
+static int __guc_action_register_multi_lrc(struct intel_guc *guc,
+					   struct intel_context *ce,
+					   u32 guc_id,
+					   u32 offset,
+					   bool loop)
+{
+	struct intel_context *child;
+	u32 action[4 + MAX_ENGINE_INSTANCE];
+	int len = 0;
+
+	GEM_BUG_ON(ce->guc_number_children > MAX_ENGINE_INSTANCE);
+
+	action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
+	action[len++] = guc_id;
+	action[len++] = ce->guc_number_children + 1;
+	action[len++] = offset;
+	for_each_child(ce, child) {
+		offset += sizeof(struct guc_lrc_desc);
+		action[len++] = offset;
+	}
+
+	return guc_submission_send_busy_loop(guc, action, len, 0, loop);
+}
+
  static int __guc_action_register_context(struct intel_guc *guc,
  					 u32 guc_id,
  					 u32 offset,
@@ -1364,9 +1421,15 @@ static int register_context(struct intel_context *ce, bool loop)
  		ce->guc_id.id * sizeof(struct guc_lrc_desc);
  	int ret;
 
+	GEM_BUG_ON(intel_context_is_child(ce));
  	trace_intel_context_register(ce);
 
-	ret = __guc_action_register_context(guc, ce->guc_id.id, offset, loop);
+	if (intel_context_is_parent(ce))
+		ret = __guc_action_register_multi_lrc(guc, ce, ce->guc_id.id,
+						      offset, loop);
+	else
+		ret = __guc_action_register_context(guc, ce->guc_id.id, offset,
+						    loop);
  	if (likely(!ret)) {
  		unsigned long flags;
 
@@ -1396,6 +1459,7 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
 
+	GEM_BUG_ON(intel_context_is_child(ce));
  	trace_intel_context_deregister(ce);
 
  	return __guc_action_deregister_context(guc, guc_id, loop);
@@ -1423,6 +1487,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  	struct guc_lrc_desc *desc;
  	bool context_registered;
  	intel_wakeref_t wakeref;
+	struct intel_context *child;
  	int ret = 0;
 
  	GEM_BUG_ON(!engine->mask);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_lrc_desc_pin(struct intel_context *ce, bool loop) · line 1518

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
@@ -1448,6 +1513,42 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
  	guc_context_policy_init(engine, desc);
 
+	/*
+	 * Context is a parent, we need to register a process descriptor
+	 * describing a work queue and register all child contexts.

Technically, this should say 'If the context is a parent'. Or just move it to be inside the if block.

Quoted 9 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +9 -0
+	 */
+	if (intel_context_is_parent(ce)) {
+		struct guc_process_desc *pdesc;
+
+		ce->guc_wqi_tail = 0;
+		ce->guc_wqi_head = 0;
+
+		desc->process_desc = i915_ggtt_offset(ce->state) +
+			__get_process_desc_offset(ce);

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_lrc_desc_pin(struct intel_context *ce, bool loop) · line 1537

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +10 -0
+		desc->wq_addr = i915_ggtt_offset(ce->state) +
+			__get_wq_offset(ce);
+		desc->wq_size = GUC_WQ_SIZE;
+
+		pdesc = __get_process_desc(ce);
+		memset(pdesc, 0, sizeof(*(pdesc)));
+		pdesc->stage_id = ce->guc_id.id;
+		pdesc->wq_base_addr = desc->wq_addr;
+		pdesc->wq_size_bytes = desc->wq_size;
+		pdesc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;

Should this not be inherited from the ce? And same below. Or are we not using this priority in that way?

John.

Quoted 30 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+		pdesc->wq_status = WQ_STATUS_ACTIVE;
+
+		for_each_child(ce, child) {
+			desc = __get_lrc_desc(guc, child->guc_id.id);
+
+			desc->engine_class =
+				engine_class_to_guc_class(engine->class);
+			desc->hw_context_desc = child->lrc.lrca;
+			desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+			desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+			guc_context_policy_init(engine, desc);
+		}
+	}
+
  	/*
  	 * The context_lookup xarray is used to determine if the hardware
  	 * context is currently registered. There are two cases in which it
@@ -2858,6 +2959,12 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  		return NULL;
  	}
 
+	if (unlikely(intel_context_is_child(ce))) {
+		drm_err(&guc_to_gt(guc)->i915->drm,
+			"Context is child, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
  	return ce;
  }
 
#51 lore raw

Re: [Intel-gfx] [PATCH 12/27] drm/i915/guc: Add multi-lrc context registration

Quoted 13 lines · John Harrison
Quoted 12 lines · Matthew Brost

Add multi-lrc context registration H2G. In addition a workqueue and process descriptor are setup during multi-lrc context registration as these data structures are needed for multi-lrc submission.

---
  drivers/gpu/drm/i915/gt/intel_context_types.h |  12 ++
  drivers/gpu/drm/i915/gt/intel_lrc.c           |   5 +
  drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |   2 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 109 +++++++++++++++++-
  4 files changed, 126 insertions(+), 2 deletions(-)
Quoted 17 lines

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 239

Quoted 13 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +6 -0
index 0fafc178cf2c..6f567ebeb039 100644
@@ -232,8 +232,20 @@ struct intel_context {
  		/** @parent: pointer to parent if child */
  		struct intel_context *parent;
+
+		/** @guc_wqi_head: head pointer in work queue */
+		u16 guc_wqi_head;
+		/** @guc_wqi_tail: tail pointer in work queue */
+		u16 guc_wqi_tail;
+

These should be in the 'guc_state' sub-struct? Would be good to keep all GuC specific content in one self-contained struct. Especially given the other child/parent fields are no going to be guc_ prefixed any more.

Right now I have everything in guc_state protected by guc_state.lock, these fields are not protected by this lock. IMO it is better to use a different sub-structure for the parallel fields (even if anonymous).

Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 245

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +3 -0
  		/** @guc_number_children: number of children if parent */
  		u8 guc_number_children;
+
+		/**
+		 * @parent_page: page in context used by parent for work queue,

Maybe 'page in context record'? Otherwise, exactly what 'context' is meant here? It isn't the 'struct intel_context'. The contetx record is saved as 'ce->state' / 'ce->lrc_reg_state', yes? Is it possible to link to either of

It is the page in ce->state / page minus LRC reg offset in ce->lrg_reg_state. Will update the commit to make that clear.

Quoted 7 lines

those field? Probably not given that they don't appear to have any kerneldoc description :(. Maybe add that in too :).

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 246

Quoted 1 line
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -0
+		 * work queue descriptor

Later on, it is described as 'process descriptor and work queue'. It would be good to be consistent.

Yep. Will fix.

Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +2 -0
+		 */
+		u8 parent_page;
  	};
  #ifdef CONFIG_DRM_I915_SELFTEST
drivers/gpu/drm/i915/gt/intel_lrc.c +5 -0
index bb4af4977920..0ddbad4e062a 100644
@@ -861,6 +861,11 @@ __lrc_alloc_state(struct intel_context *ce, struct intel_engine_cs *engine)
  		context_size += PAGE_SIZE;
  	}
+	if (intel_context_is_parent(ce)) {
+		ce->parent_page = context_size / PAGE_SIZE;
+		context_size += PAGE_SIZE;
+	}
+
  	obj = i915_gem_object_create_lmem(engine->i915, context_size, 0);
  	if (IS_ERR(obj))
  		obj = i915_gem_object_create_shmem(engine->i915, context_size);
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -0
index fa4be13c8854..0e600a3b8f1e 100644
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h line 53

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +1 -1
@@ -52,7 +52,7 @@
  #define GUC_DOORBELL_INVALID		256
-#define GUC_WQ_SIZE			(PAGE_SIZE * 2)
+#define GUC_WQ_SIZE			(PAGE_SIZE / 2)

Is this size actually dictated by the GuC API? Or is it just a driver level decision? If the latter, shouldn't this be below instead?

Driver level decision. What exactly do you mean by below?

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -0
  /* Work queue item header definitions */
  #define WQ_STATUS_ACTIVE		1
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
index 14b24298cdd7..dbcb9ab28a9a 100644
@@ -340,6 +340,39 @@ static struct i915_priolist *to_priolist(struct rb_node *rb)
  	return rb_entry(rb, struct i915_priolist, node);
  }
+/*
+ * When using multi-lrc submission an extra page in the context state is
+ * reserved for the process descriptor and work queue.
+ *
+ * The layout of this page is below:
+ * 0						guc_process_desc
Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static struct i915_priolist *to_priolist(struct rb_node *rb) · line 354

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+ * ...						unused
+ * PAGE_SIZE / 2				work queue start
+ * ...						work queue
+ * PAGE_SIZE - 1				work queue end
+ */
+#define WQ_OFFSET	(PAGE_SIZE / 2)

Can this not be derived from GUC_WQ_SIZE given that the two are fundamentally linked? E.g. '#define WQ_OFFSET (PAGE_SIZE - GUC_WQ_SIZE)'?

Yes. I like 'define WQ_OFFSET (PAGE_SIZE - GUC_WQ_SIZE)'. Will change.

Quoted 5 lines

And maybe have a '#define WQ_TOTAL_SIZE PAGE_SIZE' and use that in all of WQ_OFFSET, GUC_WQ_SIZE and the allocation itself in intel_lrc.c?

Also, the process descriptor is actually an array of descriptors sized by the number of children? Or am I misunderstanding the code below? In so,

No, it is fixed size descriptor.

A later patch in the series uses the space of above the process descriptor for insertation of preeemption points handshake. That does depend on the number of children. I will add a COMPILE_BUG_ON for that to ensure everything fits in the memory layout.

Quoted 17 lines

shouldn't there be a 'COMPILE_BUG_ON((MAX_ENGINE_INSTANCE * sizeof(descriptor)) < (WQ_DESC_SIZE)' where WQ_DESC_SIZE is WQ_TOTAL_SIZE - WQ_SIZE?

Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+static u32 __get_process_desc_offset(struct intel_context *ce)
+{
+	GEM_BUG_ON(!ce->parent_page);
+
+	return ce->parent_page * PAGE_SIZE;
+}
+
+static u32 __get_wq_offset(struct intel_context *ce)
+{
+	return __get_process_desc_offset(ce) + WQ_OFFSET;
+}
+
+static struct guc_process_desc *
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static struct i915_priolist *to_priolist(struct rb_node *rb) · line 373

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+__get_process_desc(struct intel_context *ce)
+{
+	return (struct guc_process_desc *)
+		(ce->lrc_reg_state +
+		 ((__get_process_desc_offset(ce) -
+		   LRC_STATE_OFFSET) / sizeof(u32)));

Where did the LRC_STATE_OFFSET come from? Is that built in to the lrg_reg_state pointer itself? That needs to be documented somewhere.

In gt/intel_lrc.c (lrc_pin) ce->lrc_reg_state is assigned to mmap(ce->state) + LRC_STATE_OFFSET. I can update the kerneldoc for that field in this patch.

Quoted 62 lines
Quoted 62 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +35 -1
+}
+
  static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
  {
  	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
@@ -1342,6 +1375,30 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
+static int __guc_action_register_multi_lrc(struct intel_guc *guc,
+					   struct intel_context *ce,
+					   u32 guc_id,
+					   u32 offset,
+					   bool loop)
+{
+	struct intel_context *child;
+	u32 action[4 + MAX_ENGINE_INSTANCE];
+	int len = 0;
+
+	GEM_BUG_ON(ce->guc_number_children > MAX_ENGINE_INSTANCE);
+
+	action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
+	action[len++] = guc_id;
+	action[len++] = ce->guc_number_children + 1;
+	action[len++] = offset;
+	for_each_child(ce, child) {
+		offset += sizeof(struct guc_lrc_desc);
+		action[len++] = offset;
+	}
+
+	return guc_submission_send_busy_loop(guc, action, len, 0, loop);
+}
+
  static int __guc_action_register_context(struct intel_guc *guc,
  					 u32 guc_id,
  					 u32 offset,
@@ -1364,9 +1421,15 @@ static int register_context(struct intel_context *ce, bool loop)
  		ce->guc_id.id * sizeof(struct guc_lrc_desc);
  	int ret;
+	GEM_BUG_ON(intel_context_is_child(ce));
  	trace_intel_context_register(ce);
-	ret = __guc_action_register_context(guc, ce->guc_id.id, offset, loop);
+	if (intel_context_is_parent(ce))
+		ret = __guc_action_register_multi_lrc(guc, ce, ce->guc_id.id,
+						      offset, loop);
+	else
+		ret = __guc_action_register_context(guc, ce->guc_id.id, offset,
+						    loop);
  	if (likely(!ret)) {
  		unsigned long flags;
@@ -1396,6 +1459,7 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
  {
  	struct intel_guc *guc = ce_to_guc(ce);
+	GEM_BUG_ON(intel_context_is_child(ce));
  	trace_intel_context_deregister(ce);
  	return __guc_action_deregister_context(guc, guc_id, loop);
@@ -1423,6 +1487,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  	struct guc_lrc_desc *desc;
  	bool context_registered;
  	intel_wakeref_t wakeref;
+	struct intel_context *child;
  	int ret = 0;
  	GEM_BUG_ON(!engine->mask);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_lrc_desc_pin(struct intel_context *ce, bool loop) · line 1517

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
@@ -1448,6 +1513,42 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
  	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
  	guc_context_policy_init(engine, desc);
+	/*
+	 * Context is a parent, we need to register a process descriptor
+	 * describing a work queue and register all child contexts.

Technically, this should say 'If the context is a parent'. Or just move it to be inside the if block.

I will add the "If the".

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+	 */
+	if (intel_context_is_parent(ce)) {
+		struct guc_process_desc *pdesc;
+
+		ce->guc_wqi_tail = 0;
+		ce->guc_wqi_head = 0;
+
+		desc->process_desc = i915_ggtt_offset(ce->state) +
+			__get_process_desc_offset(ce);
+		desc->wq_addr = i915_ggtt_offset(ce->state) +
+			__get_wq_offset(ce);
+		desc->wq_size = GUC_WQ_SIZE;
+
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_lrc_desc_pin(struct intel_context *ce, bool loop) · line 1537

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+		pdesc = __get_process_desc(ce);
+		memset(pdesc, 0, sizeof(*(pdesc)));
+		pdesc->stage_id = ce->guc_id.id;
+		pdesc->wq_base_addr = desc->wq_addr;
+		pdesc->wq_size_bytes = desc->wq_size;
+		pdesc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;

Should this not be inherited from the ce? And same below. Or are we not using this priority in that way?

Honestly I don't think this field is used or maybe doesn't even exist anymore. I'll check the GuC code and likely delete this or if it is still present I'll inherited this from the ce.

Matt

Quoted 31 lines

John.

Quoted 28 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+		pdesc->wq_status = WQ_STATUS_ACTIVE;
+
+		for_each_child(ce, child) {
+			desc = __get_lrc_desc(guc, child->guc_id.id);
+
+			desc->engine_class =
+				engine_class_to_guc_class(engine->class);
+			desc->hw_context_desc = child->lrc.lrca;
+			desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+			desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+			guc_context_policy_init(engine, desc);
+		}
+	}
+
  	/*
  	 * The context_lookup xarray is used to determine if the hardware
  	 * context is currently registered. There are two cases in which it
@@ -2858,6 +2959,12 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
  		return NULL;
  	}
+	if (unlikely(intel_context_is_child(ce))) {
+		drm_err(&guc_to_gt(guc)->i915->drm,
+			"Context is child, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
  	return ce;
  }
#52 lore raw

Re: [Intel-gfx] [PATCH 12/27] drm/i915/guc: Add multi-lrc context registration

Quoted 31 lines · Matthew Brost
Quoted 13 lines · John Harrison
Quoted 12 lines · Matthew Brost

Add multi-lrc context registration H2G. In addition a workqueue and process descriptor are setup during multi-lrc context registration as these data structures are needed for multi-lrc submission.

---
   drivers/gpu/drm/i915/gt/intel_context_types.h |  12 ++
   drivers/gpu/drm/i915/gt/intel_lrc.c           |   5 +
   drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |   2 +-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 109 +++++++++++++++++-
   4 files changed, 126 insertions(+), 2 deletions(-)
Quoted 17 lines

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 239

Quoted 13 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +6 -0
index 0fafc178cf2c..6f567ebeb039 100644
@@ -232,8 +232,20 @@ struct intel_context {
   		/** @parent: pointer to parent if child */
   		struct intel_context *parent;
+
+		/** @guc_wqi_head: head pointer in work queue */
+		u16 guc_wqi_head;
+		/** @guc_wqi_tail: tail pointer in work queue */
+		u16 guc_wqi_tail;
+

These should be in the 'guc_state' sub-struct? Would be good to keep all GuC specific content in one self-contained struct. Especially given the other child/parent fields are no going to be guc_ prefixed any more.

Quoted 3 lines

Right now I have everything in guc_state protected by guc_state.lock, these fields are not protected by this lock. IMO it is better to use a different sub-structure for the parallel fields (even if anonymous).

Hmm, I still think it is bad to be scattering back-end specific fields amongst regular fields. The GuC patches include a whole bunch of complaints about execlist back-end specific stuff leaking through to the higher levels, we really shouldn't be guilty of doing the same with GuC if at all possible. At the very least, the GuC specific fields should be grouped together at the end of the struct rather than inter-mingled.

Quoted 51 lines
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 245

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +3 -0
   		/** @guc_number_children: number of children if parent */
   		u8 guc_number_children;
+
+		/**
+		 * @parent_page: page in context used by parent for work queue,

Maybe 'page in context record'? Otherwise, exactly what 'context' is meant here? It isn't the 'struct intel_context'. The contetx record is saved as 'ce->state' / 'ce->lrc_reg_state', yes? Is it possible to link to either of

It is the page in ce->state / page minus LRC reg offset in ce->lrg_reg_state. Will update the commit to make that clear.

Quoted 7 lines

those field? Probably not given that they don't appear to have any kerneldoc description :(. Maybe add that in too :).

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 246

Quoted 1 line
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -0
+		 * work queue descriptor

Later on, it is described as 'process descriptor and work queue'. It would be good to be consistent.

Yep. Will fix.

Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +2 -0
+		 */
+		u8 parent_page;
   	};
   #ifdef CONFIG_DRM_I915_SELFTEST
drivers/gpu/drm/i915/gt/intel_lrc.c +5 -0
index bb4af4977920..0ddbad4e062a 100644
@@ -861,6 +861,11 @@ __lrc_alloc_state(struct intel_context *ce, struct intel_engine_cs *engine)
   		context_size += PAGE_SIZE;
   	}
+	if (intel_context_is_parent(ce)) {
+		ce->parent_page = context_size / PAGE_SIZE;
+		context_size += PAGE_SIZE;
+	}
+
   	obj = i915_gem_object_create_lmem(engine->i915, context_size, 0);
   	if (IS_ERR(obj))
   		obj = i915_gem_object_create_shmem(engine->i915, context_size);
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -0
index fa4be13c8854..0e600a3b8f1e 100644
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h line 53

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +1 -1
@@ -52,7 +52,7 @@
   #define GUC_DOORBELL_INVALID		256
-#define GUC_WQ_SIZE			(PAGE_SIZE * 2)
+#define GUC_WQ_SIZE			(PAGE_SIZE / 2)

Is this size actually dictated by the GuC API? Or is it just a driver level decision? If the latter, shouldn't this be below instead?

Quoted 1 line

Driver level decision. What exactly do you mean by below?

The next chunk of the patch - where WQ_OFFSET is defined and the whole caboodle is described.

Quoted 24 lines
Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -0
   /* Work queue item header definitions */
   #define WQ_STATUS_ACTIVE		1
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
index 14b24298cdd7..dbcb9ab28a9a 100644
@@ -340,6 +340,39 @@ static struct i915_priolist *to_priolist(struct rb_node *rb)
   	return rb_entry(rb, struct i915_priolist, node);
   }
+/*
+ * When using multi-lrc submission an extra page in the context state is
+ * reserved for the process descriptor and work queue.
+ *
+ * The layout of this page is below:
+ * 0						guc_process_desc
Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static struct i915_priolist *to_priolist(struct rb_node *rb) · line 354

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+ * ...						unused
+ * PAGE_SIZE / 2				work queue start
+ * ...						work queue
+ * PAGE_SIZE - 1				work queue end
+ */
+#define WQ_OFFSET	(PAGE_SIZE / 2)

Can this not be derived from GUC_WQ_SIZE given that the two are fundamentally linked? E.g. '#define WQ_OFFSET (PAGE_SIZE - GUC_WQ_SIZE)'?

Quoted 8 lines

Yes. I like 'define WQ_OFFSET (PAGE_SIZE - GUC_WQ_SIZE)'. Will change.

Quoted 5 lines

And maybe have a '#define WQ_TOTAL_SIZE PAGE_SIZE' and use that in all of WQ_OFFSET, GUC_WQ_SIZE and the allocation itself in intel_lrc.c?

Also, the process descriptor is actually an array of descriptors sized by the number of children? Or am I misunderstanding the code below? In so,

No, it is fixed size descriptor.

Yeah, I think I was getting confused between pdesc and desc in the code below.

I still think it would be a good idea to have everything explicitly named and the only mention of PAGE_SIZE is in the 'total size' definition.

John.

Quoted 167 lines

A later patch in the series uses the space of above the process descriptor for insertation of preeemption points handshake. That does depend on the number of children. I will add a COMPILE_BUG_ON for that to ensure everything fits in the memory layout.

Quoted 17 lines

shouldn't there be a 'COMPILE_BUG_ON((MAX_ENGINE_INSTANCE * sizeof(descriptor)) < (WQ_DESC_SIZE)' where WQ_DESC_SIZE is WQ_TOTAL_SIZE - WQ_SIZE?

Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+static u32 __get_process_desc_offset(struct intel_context *ce)
+{
+	GEM_BUG_ON(!ce->parent_page);
+
+	return ce->parent_page * PAGE_SIZE;
+}
+
+static u32 __get_wq_offset(struct intel_context *ce)
+{
+	return __get_process_desc_offset(ce) + WQ_OFFSET;
+}
+
+static struct guc_process_desc *
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static struct i915_priolist *to_priolist(struct rb_node *rb) · line 373

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+__get_process_desc(struct intel_context *ce)
+{
+	return (struct guc_process_desc *)
+		(ce->lrc_reg_state +
+		 ((__get_process_desc_offset(ce) -
+		   LRC_STATE_OFFSET) / sizeof(u32)));

Where did the LRC_STATE_OFFSET come from? Is that built in to the lrg_reg_state pointer itself? That needs to be documented somewhere.

In gt/intel_lrc.c (lrc_pin) ce->lrc_reg_state is assigned to mmap(ce->state) + LRC_STATE_OFFSET. I can update the kerneldoc for that field in this patch.

Quoted 62 lines
Quoted 62 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +35 -1
+}
+
   static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
   {
   	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
@@ -1342,6 +1375,30 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
   	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
   }
+static int __guc_action_register_multi_lrc(struct intel_guc *guc,
+					   struct intel_context *ce,
+					   u32 guc_id,
+					   u32 offset,
+					   bool loop)
+{
+	struct intel_context *child;
+	u32 action[4 + MAX_ENGINE_INSTANCE];
+	int len = 0;
+
+	GEM_BUG_ON(ce->guc_number_children > MAX_ENGINE_INSTANCE);
+
+	action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
+	action[len++] = guc_id;
+	action[len++] = ce->guc_number_children + 1;
+	action[len++] = offset;
+	for_each_child(ce, child) {
+		offset += sizeof(struct guc_lrc_desc);
+		action[len++] = offset;
+	}
+
+	return guc_submission_send_busy_loop(guc, action, len, 0, loop);
+}
+
   static int __guc_action_register_context(struct intel_guc *guc,
   					 u32 guc_id,
   					 u32 offset,
@@ -1364,9 +1421,15 @@ static int register_context(struct intel_context *ce, bool loop)
   		ce->guc_id.id * sizeof(struct guc_lrc_desc);
   	int ret;
+	GEM_BUG_ON(intel_context_is_child(ce));
   	trace_intel_context_register(ce);
-	ret = __guc_action_register_context(guc, ce->guc_id.id, offset, loop);
+	if (intel_context_is_parent(ce))
+		ret = __guc_action_register_multi_lrc(guc, ce, ce->guc_id.id,
+						      offset, loop);
+	else
+		ret = __guc_action_register_context(guc, ce->guc_id.id, offset,
+						    loop);
   	if (likely(!ret)) {
   		unsigned long flags;
@@ -1396,6 +1459,7 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
   {
   	struct intel_guc *guc = ce_to_guc(ce);
+	GEM_BUG_ON(intel_context_is_child(ce));
   	trace_intel_context_deregister(ce);
   	return __guc_action_deregister_context(guc, guc_id, loop);
@@ -1423,6 +1487,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
   	struct guc_lrc_desc *desc;
   	bool context_registered;
   	intel_wakeref_t wakeref;
+	struct intel_context *child;
   	int ret = 0;
   	GEM_BUG_ON(!engine->mask);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_lrc_desc_pin(struct intel_context *ce, bool loop) · line 1517

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
@@ -1448,6 +1513,42 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
   	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
   	guc_context_policy_init(engine, desc);
+	/*
+	 * Context is a parent, we need to register a process descriptor
+	 * describing a work queue and register all child contexts.

Technically, this should say 'If the context is a parent'. Or just move it to be inside the if block.

I will add the "If the".

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+	 */
+	if (intel_context_is_parent(ce)) {
+		struct guc_process_desc *pdesc;
+
+		ce->guc_wqi_tail = 0;
+		ce->guc_wqi_head = 0;
+
+		desc->process_desc = i915_ggtt_offset(ce->state) +
+			__get_process_desc_offset(ce);
+		desc->wq_addr = i915_ggtt_offset(ce->state) +
+			__get_wq_offset(ce);
+		desc->wq_size = GUC_WQ_SIZE;
+
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_lrc_desc_pin(struct intel_context *ce, bool loop) · line 1537

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+		pdesc = __get_process_desc(ce);
+		memset(pdesc, 0, sizeof(*(pdesc)));
+		pdesc->stage_id = ce->guc_id.id;
+		pdesc->wq_base_addr = desc->wq_addr;
+		pdesc->wq_size_bytes = desc->wq_size;
+		pdesc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;

Should this not be inherited from the ce? And same below. Or are we not using this priority in that way?

Honestly I don't think this field is used or maybe doesn't even exist anymore. I'll check the GuC code and likely delete this or if it is still present I'll inherited this from the ce.

Matt

Quoted 30 lines

John.

Quoted 28 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+		pdesc->wq_status = WQ_STATUS_ACTIVE;
+
+		for_each_child(ce, child) {
+			desc = __get_lrc_desc(guc, child->guc_id.id);
+
+			desc->engine_class =
+				engine_class_to_guc_class(engine->class);
+			desc->hw_context_desc = child->lrc.lrca;
+			desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+			desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+			guc_context_policy_init(engine, desc);
+		}
+	}
+
   	/*
   	 * The context_lookup xarray is used to determine if the hardware
   	 * context is currently registered. There are two cases in which it
@@ -2858,6 +2959,12 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
   		return NULL;
   	}
+	if (unlikely(intel_context_is_child(ce))) {
+		drm_err(&guc_to_gt(guc)->i915->drm,
+			"Context is child, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
   	return ce;
   }
#53 lore raw

Re: [Intel-gfx] [PATCH 12/27] drm/i915/guc: Add multi-lrc context registration

Quoted 35 lines · John Harrison
Quoted 31 lines · Matthew Brost
Quoted 13 lines · John Harrison
Quoted 12 lines · Matthew Brost

Add multi-lrc context registration H2G. In addition a workqueue and process descriptor are setup during multi-lrc context registration as these data structures are needed for multi-lrc submission.

---
   drivers/gpu/drm/i915/gt/intel_context_types.h |  12 ++
   drivers/gpu/drm/i915/gt/intel_lrc.c           |   5 +
   drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |   2 +-
   .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 109 +++++++++++++++++-
   4 files changed, 126 insertions(+), 2 deletions(-)
Quoted 17 lines

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 239

Quoted 13 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +6 -0
index 0fafc178cf2c..6f567ebeb039 100644
@@ -232,8 +232,20 @@ struct intel_context {
   		/** @parent: pointer to parent if child */
   		struct intel_context *parent;
+
+		/** @guc_wqi_head: head pointer in work queue */
+		u16 guc_wqi_head;
+		/** @guc_wqi_tail: tail pointer in work queue */
+		u16 guc_wqi_tail;
+

These should be in the 'guc_state' sub-struct? Would be good to keep all GuC specific content in one self-contained struct. Especially given the other child/parent fields are no going to be guc_ prefixed any more.

Quoted 3 lines

Right now I have everything in guc_state protected by guc_state.lock, these fields are not protected by this lock. IMO it is better to use a different sub-structure for the parallel fields (even if anonymous).

Quoted 7 lines

Hmm, I still think it is bad to be scattering back-end specific fields amongst regular fields. The GuC patches include a whole bunch of complaints about execlist back-end specific stuff leaking through to the higher levels, we really shouldn't be guilty of doing the same with GuC if at all possible. At the very least, the GuC specific fields should be grouped together at the end of the struct rather than inter-mingled.

How 2 different sub-structures - parallel (shared) & guc_parallel (guc specific)?

Quoted 83 lines
Quoted 51 lines
Quoted 9 lines

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 245

Quoted 6 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +3 -0
   		/** @guc_number_children: number of children if parent */
   		u8 guc_number_children;
+
+		/**
+		 * @parent_page: page in context used by parent for work queue,

Maybe 'page in context record'? Otherwise, exactly what 'context' is meant here? It isn't the 'struct intel_context'. The contetx record is saved as 'ce->state' / 'ce->lrc_reg_state', yes? Is it possible to link to either of

It is the page in ce->state / page minus LRC reg offset in ce->lrg_reg_state. Will update the commit to make that clear.

Quoted 7 lines

those field? Probably not given that they don't appear to have any kerneldoc description :(. Maybe add that in too :).

drivers/gpu/drm/i915/gt/intel_context_types.h struct intel_context { · line 246

Quoted 1 line
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -0
+		 * work queue descriptor

Later on, it is described as 'process descriptor and work queue'. It would be good to be consistent.

Yep. Will fix.

Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/i915/gt/intel_context_types.h +2 -0
+		 */
+		u8 parent_page;
   	};
   #ifdef CONFIG_DRM_I915_SELFTEST
drivers/gpu/drm/i915/gt/intel_lrc.c +5 -0
index bb4af4977920..0ddbad4e062a 100644
@@ -861,6 +861,11 @@ __lrc_alloc_state(struct intel_context *ce, struct intel_engine_cs *engine)
   		context_size += PAGE_SIZE;
   	}
+	if (intel_context_is_parent(ce)) {
+		ce->parent_page = context_size / PAGE_SIZE;
+		context_size += PAGE_SIZE;
+	}
+
   	obj = i915_gem_object_create_lmem(engine->i915, context_size, 0);
   	if (IS_ERR(obj))
   		obj = i915_gem_object_create_shmem(engine->i915, context_size);
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -0
index fa4be13c8854..0e600a3b8f1e 100644
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h line 53

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +1 -1
@@ -52,7 +52,7 @@
   #define GUC_DOORBELL_INVALID		256
-#define GUC_WQ_SIZE			(PAGE_SIZE * 2)
+#define GUC_WQ_SIZE			(PAGE_SIZE / 2)

Is this size actually dictated by the GuC API? Or is it just a driver level decision? If the latter, shouldn't this be below instead?

Quoted 1 line

Driver level decision. What exactly do you mean by below?

The next chunk of the patch - where WQ_OFFSET is defined and the whole caboodle is described.

Quoted 28 lines
Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +0 -0
   /* Work queue item header definitions */
   #define WQ_STATUS_ACTIVE		1
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
index 14b24298cdd7..dbcb9ab28a9a 100644
@@ -340,6 +340,39 @@ static struct i915_priolist *to_priolist(struct rb_node *rb)
   	return rb_entry(rb, struct i915_priolist, node);
   }
+/*
+ * When using multi-lrc submission an extra page in the context state is
+ * reserved for the process descriptor and work queue.
+ *
+ * The layout of this page is below:
+ * 0						guc_process_desc
Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static struct i915_priolist *to_priolist(struct rb_node *rb) · line 354

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+ * ...						unused
+ * PAGE_SIZE / 2				work queue start
+ * ...						work queue
+ * PAGE_SIZE - 1				work queue end
+ */
+#define WQ_OFFSET	(PAGE_SIZE / 2)

Can this not be derived from GUC_WQ_SIZE given that the two are fundamentally linked? E.g. '#define WQ_OFFSET (PAGE_SIZE - GUC_WQ_SIZE)'?

Yes. I like 'define WQ_OFFSET (PAGE_SIZE - GUC_WQ_SIZE)'. Will change.

Quoted 3 lines

And maybe have a '#define WQ_TOTAL_SIZE PAGE_SIZE' and use that in all of WQ_OFFSET, GUC_WQ_SIZE and the allocation itself in intel_lrc.c?

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

Also, the process descriptor is actually an array of descriptors sized by the number of children? Or am I misunderstanding the code below? In so,

No, it is fixed size descriptor.

Yeah, I think I was getting confused between pdesc and desc in the code below.

I still think it would be a good idea to have everything explicitly named and the only mention of PAGE_SIZE is in the 'total size' definition.

#define PARENT_SCRATCH_SIZE PAGE_SIZE?

Matt

Quoted 171 lines

John.

Quoted 167 lines

A later patch in the series uses the space of above the process descriptor for insertation of preeemption points handshake. That does depend on the number of children. I will add a COMPILE_BUG_ON for that to ensure everything fits in the memory layout.

Quoted 17 lines

shouldn't there be a 'COMPILE_BUG_ON((MAX_ENGINE_INSTANCE * sizeof(descriptor)) < (WQ_DESC_SIZE)' where WQ_DESC_SIZE is WQ_TOTAL_SIZE - WQ_SIZE?

Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+static u32 __get_process_desc_offset(struct intel_context *ce)
+{
+	GEM_BUG_ON(!ce->parent_page);
+
+	return ce->parent_page * PAGE_SIZE;
+}
+
+static u32 __get_wq_offset(struct intel_context *ce)
+{
+	return __get_process_desc_offset(ce) + WQ_OFFSET;
+}
+
+static struct guc_process_desc *
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static struct i915_priolist *to_priolist(struct rb_node *rb) · line 373

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+__get_process_desc(struct intel_context *ce)
+{
+	return (struct guc_process_desc *)
+		(ce->lrc_reg_state +
+		 ((__get_process_desc_offset(ce) -
+		   LRC_STATE_OFFSET) / sizeof(u32)));

Where did the LRC_STATE_OFFSET come from? Is that built in to the lrg_reg_state pointer itself? That needs to be documented somewhere.

In gt/intel_lrc.c (lrc_pin) ce->lrc_reg_state is assigned to mmap(ce->state) + LRC_STATE_OFFSET. I can update the kerneldoc for that field in this patch.

Quoted 62 lines
Quoted 62 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +35 -1
+}
+
   static struct guc_lrc_desc *__get_lrc_desc(struct intel_guc *guc, u32 index)
   {
   	struct guc_lrc_desc *base = guc->lrc_desc_pool_vaddr;
@@ -1342,6 +1375,30 @@ static void unpin_guc_id(struct intel_guc *guc, struct intel_context *ce)
   	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
   }
+static int __guc_action_register_multi_lrc(struct intel_guc *guc,
+					   struct intel_context *ce,
+					   u32 guc_id,
+					   u32 offset,
+					   bool loop)
+{
+	struct intel_context *child;
+	u32 action[4 + MAX_ENGINE_INSTANCE];
+	int len = 0;
+
+	GEM_BUG_ON(ce->guc_number_children > MAX_ENGINE_INSTANCE);
+
+	action[len++] = INTEL_GUC_ACTION_REGISTER_CONTEXT_MULTI_LRC;
+	action[len++] = guc_id;
+	action[len++] = ce->guc_number_children + 1;
+	action[len++] = offset;
+	for_each_child(ce, child) {
+		offset += sizeof(struct guc_lrc_desc);
+		action[len++] = offset;
+	}
+
+	return guc_submission_send_busy_loop(guc, action, len, 0, loop);
+}
+
   static int __guc_action_register_context(struct intel_guc *guc,
   					 u32 guc_id,
   					 u32 offset,
@@ -1364,9 +1421,15 @@ static int register_context(struct intel_context *ce, bool loop)
   		ce->guc_id.id * sizeof(struct guc_lrc_desc);
   	int ret;
+	GEM_BUG_ON(intel_context_is_child(ce));
   	trace_intel_context_register(ce);
-	ret = __guc_action_register_context(guc, ce->guc_id.id, offset, loop);
+	if (intel_context_is_parent(ce))
+		ret = __guc_action_register_multi_lrc(guc, ce, ce->guc_id.id,
+						      offset, loop);
+	else
+		ret = __guc_action_register_context(guc, ce->guc_id.id, offset,
+						    loop);
   	if (likely(!ret)) {
   		unsigned long flags;
@@ -1396,6 +1459,7 @@ static int deregister_context(struct intel_context *ce, u32 guc_id, bool loop)
   {
   	struct intel_guc *guc = ce_to_guc(ce);
+	GEM_BUG_ON(intel_context_is_child(ce));
   	trace_intel_context_deregister(ce);
   	return __guc_action_deregister_context(guc, guc_id, loop);
@@ -1423,6 +1487,7 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
   	struct guc_lrc_desc *desc;
   	bool context_registered;
   	intel_wakeref_t wakeref;
+	struct intel_context *child;
   	int ret = 0;
   	GEM_BUG_ON(!engine->mask);
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_lrc_desc_pin(struct intel_context *ce, bool loop) · line 1517

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
@@ -1448,6 +1513,42 @@ static int guc_lrc_desc_pin(struct intel_context *ce, bool loop)
   	desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
   	guc_context_policy_init(engine, desc);
+	/*
+	 * Context is a parent, we need to register a process descriptor
+	 * describing a work queue and register all child contexts.

Technically, this should say 'If the context is a parent'. Or just move it to be inside the if block.

I will add the "If the".

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +13 -0
+	 */
+	if (intel_context_is_parent(ce)) {
+		struct guc_process_desc *pdesc;
+
+		ce->guc_wqi_tail = 0;
+		ce->guc_wqi_head = 0;
+
+		desc->process_desc = i915_ggtt_offset(ce->state) +
+			__get_process_desc_offset(ce);
+		desc->wq_addr = i915_ggtt_offset(ce->state) +
+			__get_wq_offset(ce);
+		desc->wq_size = GUC_WQ_SIZE;
+
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static int guc_lrc_desc_pin(struct intel_context *ce, bool loop) · line 1537

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +6 -0
+		pdesc = __get_process_desc(ce);
+		memset(pdesc, 0, sizeof(*(pdesc)));
+		pdesc->stage_id = ce->guc_id.id;
+		pdesc->wq_base_addr = desc->wq_addr;
+		pdesc->wq_size_bytes = desc->wq_size;
+		pdesc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;

Should this not be inherited from the ce? And same below. Or are we not using this priority in that way?

Honestly I don't think this field is used or maybe doesn't even exist anymore. I'll check the GuC code and likely delete this or if it is still present I'll inherited this from the ce.

Matt

Quoted 30 lines

John.

Quoted 28 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +20 -0
+		pdesc->wq_status = WQ_STATUS_ACTIVE;
+
+		for_each_child(ce, child) {
+			desc = __get_lrc_desc(guc, child->guc_id.id);
+
+			desc->engine_class =
+				engine_class_to_guc_class(engine->class);
+			desc->hw_context_desc = child->lrc.lrca;
+			desc->priority = GUC_CLIENT_PRIORITY_KMD_NORMAL;
+			desc->context_flags = CONTEXT_REGISTRATION_FLAG_KMD;
+			guc_context_policy_init(engine, desc);
+		}
+	}
+
   	/*
   	 * The context_lookup xarray is used to determine if the hardware
   	 * context is currently registered. There are two cases in which it
@@ -2858,6 +2959,12 @@ g2h_context_lookup(struct intel_guc *guc, u32 desc_idx)
   		return NULL;
   	}
+	if (unlikely(intel_context_is_child(ce))) {
+		drm_err(&guc_to_gt(guc)->i915->drm,
+			"Context is child, desc_idx %u", desc_idx);
+		return NULL;
+	}
+
   	return ce;
   }
#54 lore raw

PATCH 13/27 drm/i915/guc: Ensure GuC schedule operations do not operate on child contexts

In reply to Matthew Brost

In GuC parent-child contexts the parent context controls the scheduling, ensure only the parent does the scheduling operations.

---
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 24 ++++++++++++++-----
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +18 -6
index dbcb9ab28a9a..00d54bb00bfb 100644
@@ -320,6 +320,12 @@ static void decr_context_committed_requests(struct intel_context *ce)
 	GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
 }
 
+static struct intel_context *
+request_to_scheduling_context(struct i915_request *rq)
+{
+	return intel_context_to_parent(rq->context);
+}
+
 static bool context_guc_id_invalid(struct intel_context *ce)
 {
 	return ce->guc_id.id == GUC_INVALID_LRC_ID;
@@ -1684,6 +1690,7 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
 
 	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
 
+	GEM_BUG_ON(intel_context_is_child(ce));
 	trace_intel_context_sched_disable(ce);
 
 	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
@@ -1898,6 +1905,8 @@ static void guc_context_sched_disable(struct intel_context *ce)
 	u16 guc_id;
 	bool enabled;
 
+	GEM_BUG_ON(intel_context_is_child(ce));
+
 	if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
 	    !lrc_desc_registered(guc, ce->guc_id.id)) {
 		spin_lock_irqsave(&ce->guc_state.lock, flags);
@@ -2286,6 +2295,8 @@ static void guc_signal_context_fence(struct intel_context *ce)
 {
 	unsigned long flags;
 
+	GEM_BUG_ON(intel_context_is_child(ce));
+
 	spin_lock_irqsave(&ce->guc_state.lock, flags);
 	clr_context_wait_for_deregister_to_register(ce);
 	__guc_signal_context_fence(ce);
@@ -2315,7 +2326,7 @@ static void guc_context_init(struct intel_context *ce)
 
 static int guc_request_alloc(struct i915_request *rq)
 {
-	struct intel_context *ce = rq->context;
+	struct intel_context *ce = request_to_scheduling_context(rq);
 	struct intel_guc *guc = ce_to_guc(ce);
 	unsigned long flags;
 	int ret;
@@ -2358,11 +2369,12 @@ static int guc_request_alloc(struct i915_request *rq)
 	 * 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.
+	 * There is no need for a lock here as the timeline mutex (or
+	 * parallel_submit mutex in the case of multi-lrc) 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))
 		goto out;
Signature · 1 line
2.32.0
#55 lore raw

Re: [Intel-gfx] [PATCH 13/27] drm/i915/guc: Ensure GuC schedule operations do not operate on child contexts

Quoted 62 lines · Matthew Brost

In GuC parent-child contexts the parent context controls the scheduling, ensure only the parent does the scheduling operations.

---
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 24 ++++++++++++++-----
  1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +12 -1
index dbcb9ab28a9a..00d54bb00bfb 100644
@@ -320,6 +320,12 @@ static void decr_context_committed_requests(struct intel_context *ce)
  	GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
  }
 
+static struct intel_context *
+request_to_scheduling_context(struct i915_request *rq)
+{
+	return intel_context_to_parent(rq->context);
+}
+
  static bool context_guc_id_invalid(struct intel_context *ce)
  {
  	return ce->guc_id.id == GUC_INVALID_LRC_ID;
@@ -1684,6 +1690,7 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
 
  	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
 
+	GEM_BUG_ON(intel_context_is_child(ce));
  	trace_intel_context_sched_disable(ce);
 
  	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
@@ -1898,6 +1905,8 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	u16 guc_id;
  	bool enabled;
 
+	GEM_BUG_ON(intel_context_is_child(ce));
+
  	if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id.id)) {
  		spin_lock_irqsave(&ce->guc_state.lock, flags);
@@ -2286,6 +2295,8 @@ static void guc_signal_context_fence(struct intel_context *ce)
  {
  	unsigned long flags;
 
+	GEM_BUG_ON(intel_context_is_child(ce));
+
  	spin_lock_irqsave(&ce->guc_state.lock, flags);
  	clr_context_wait_for_deregister_to_register(ce);
  	__guc_signal_context_fence(ce);
@@ -2315,7 +2326,7 @@ static void guc_context_init(struct intel_context *ce)
 
  static int guc_request_alloc(struct i915_request *rq)
  {
-	struct intel_context *ce = rq->context;
+	struct intel_context *ce = request_to_scheduling_context(rq);
  	struct intel_guc *guc = ce_to_guc(ce);
  	unsigned long flags;
  	int ret;
@@ -2358,11 +2369,12 @@ static int guc_request_alloc(struct i915_request *rq)
  	 * exhausted and return -EAGAIN to the user indicating that they can try

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

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -5
  	 * 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.
+	 * There is no need for a lock here as the timeline mutex (or
+	 * parallel_submit mutex in the case of multi-lrc) ensures at most one
+	 * context can be executing this code path at once. The guc_id_ref is

Isn't that now two? One uni-LRC holding the timeline mutex and one multi-LRC holding the parallel submit mutex?

John.

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+	 * 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))
  		goto out;
#56 lore raw

Re: [Intel-gfx] [PATCH 13/27] drm/i915/guc: Ensure GuC schedule operations do not operate on child contexts

Quoted 60 lines · John Harrison
Quoted 59 lines · Matthew Brost

In GuC parent-child contexts the parent context controls the scheduling, ensure only the parent does the scheduling operations.

---
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 24 ++++++++++++++-----
  1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +12 -3
index dbcb9ab28a9a..00d54bb00bfb 100644
@@ -320,6 +320,12 @@ static void decr_context_committed_requests(struct intel_context *ce)
  	GEM_BUG_ON(ce->guc_state.number_committed_requests < 0);
  }
+static struct intel_context *
+request_to_scheduling_context(struct i915_request *rq)
+{
+	return intel_context_to_parent(rq->context);
+}
+
  static bool context_guc_id_invalid(struct intel_context *ce)
  {
  	return ce->guc_id.id == GUC_INVALID_LRC_ID;
@@ -1684,6 +1690,7 @@ static void __guc_context_sched_disable(struct intel_guc *guc,
  	GEM_BUG_ON(guc_id == GUC_INVALID_LRC_ID);
+	GEM_BUG_ON(intel_context_is_child(ce));
  	trace_intel_context_sched_disable(ce);
  	guc_submission_send_busy_loop(guc, action, ARRAY_SIZE(action),
@@ -1898,6 +1905,8 @@ static void guc_context_sched_disable(struct intel_context *ce)
  	u16 guc_id;
  	bool enabled;
+	GEM_BUG_ON(intel_context_is_child(ce));
+
  	if (submission_disabled(guc) || context_guc_id_invalid(ce) ||
  	    !lrc_desc_registered(guc, ce->guc_id.id)) {
  		spin_lock_irqsave(&ce->guc_state.lock, flags);
@@ -2286,6 +2295,8 @@ static void guc_signal_context_fence(struct intel_context *ce)
  {
  	unsigned long flags;
+	GEM_BUG_ON(intel_context_is_child(ce));
+
  	spin_lock_irqsave(&ce->guc_state.lock, flags);
  	clr_context_wait_for_deregister_to_register(ce);
  	__guc_signal_context_fence(ce);
@@ -2315,7 +2326,7 @@ static void guc_context_init(struct intel_context *ce)
  static int guc_request_alloc(struct i915_request *rq)
  {
-	struct intel_context *ce = rq->context;
+	struct intel_context *ce = request_to_scheduling_context(rq);
  	struct intel_guc *guc = ce_to_guc(ce);
  	unsigned long flags;
  	int ret;
@@ -2358,11 +2369,12 @@ static int guc_request_alloc(struct i915_request *rq)
  	 * 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
Quoted 9 lines

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

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -3
-	 * 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.
+	 * There is no need for a lock here as the timeline mutex (or
+	 * parallel_submit mutex in the case of multi-lrc) ensures at most one
+	 * context can be executing this code path at once. The guc_id_ref is

Isn't that now two? One uni-LRC holding the timeline mutex and one multi-LRC holding the parallel submit mutex?

This is actually a stale comment and need scrub this. The parallel_submit mutex is gone, now we grab the ce->timeline locks starting at the parent and then all children in a loop. I think the original comment is sufficient.

Matt

Quoted 9 lines

John.

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+	 * 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))
  		goto out;
#57 lore raw

PATCH 14/27 drm/i915/guc: Assign contexts in parent-child relationship consecutive guc_ids

In reply to Matthew Brost

Assign contexts in parent-child relationship consecutive guc_ids. This is accomplished by partitioning guc_id space between ones that need to be consecutive (1/16 available guc_ids) and ones that do not (15/16 of available guc_ids). The consecutive search is implemented via the bitmap API.

This is a precursor to the full GuC multi-lrc implementation but aligns to how GuC mutli-lrc interface is defined - guc_ids must be consecutive when using the GuC multi-lrc interface.

v2:
(Daniel Vetter)
- Explictly state why we assign consecutive guc_ids

---
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   6 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 107 +++++++++++++-----
 2 files changed, 86 insertions(+), 27 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -1
index 023953e77553..3f95b1b4f15c 100644
@@ -61,9 +61,13 @@ struct intel_guc {
 		 */
 		spinlock_t lock;
 		/**
-		 * @guc_ids: used to allocate new guc_ids
+		 * @guc_ids: used to allocate new guc_ids, single-lrc
 		 */
 		struct ida guc_ids;
+		/**
+		 * @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc
+		 */
+		unsigned long *guc_ids_bitmap;
 		/** @num_guc_ids: number of guc_ids that can be used */
 		u32 num_guc_ids;
 		/** @max_guc_ids: max number of guc_ids that can be used */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +81 -26

181 lines not on this page · raw message

Signature · 1 line
2.32.0
#58 lore raw

Re: [Intel-gfx] [PATCH 14/27] drm/i915/guc: Assign contexts in parent-child relationship consecutive guc_ids

Quoted 44 lines · Matthew Brost

Assign contexts in parent-child relationship consecutive guc_ids. This is accomplished by partitioning guc_id space between ones that need to be consecutive (1/16 available guc_ids) and ones that do not (15/16 of available guc_ids). The consecutive search is implemented via the bitmap API.

This is a precursor to the full GuC multi-lrc implementation but aligns to how GuC mutli-lrc interface is defined - guc_ids must be consecutive when using the GuC multi-lrc interface.

v2:
(Daniel Vetter)
- Explictly state why we assign consecutive guc_ids

---
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   6 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 107 +++++++++++++-----
  2 files changed, 86 insertions(+), 27 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -1
index 023953e77553..3f95b1b4f15c 100644
@@ -61,9 +61,13 @@ struct intel_guc {
  		 */
  		spinlock_t lock;
  		/**
-		 * @guc_ids: used to allocate new guc_ids
+		 * @guc_ids: used to allocate new guc_ids, single-lrc
  		 */
  		struct ida guc_ids;
+		/**
+		 * @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc
+		 */
+		unsigned long *guc_ids_bitmap;
  		/** @num_guc_ids: number of guc_ids that can be used */
  		u32 num_guc_ids;
  		/** @max_guc_ids: max number of guc_ids that can be used */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
index 00d54bb00bfb..e9dfd43d29a0 100644

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

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
@@ -125,6 +125,18 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count);
 
  #define GUC_REQUEST_SIZE 64 /* bytes */
 
+/*
+ * We reserve 1/16 of the guc_ids for multi-lrc as these need to be contiguous
+ * per the GuC submission interface. A different allocation algorithm is used
+ * (bitmap vs. ida) between multi-lrc and single-lrc hence the reason to

The 'hence' clause seems to be attached to the wrong reason. The id space is partition because of the contiguous vs random requirements of multi vs single LRC, not because a different allocator is used in one partion vs the other.

Quoted 64 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +34 -4
+ * partition the guc_id space. We believe the number of multi-lrc contexts in
+ * use should be low and 1/16 should be sufficient. Minimum of 32 guc_ids for
+ * multi-lrc.
+ */
+#define NUMBER_MULTI_LRC_GUC_ID(guc) \
+	((guc)->submission_state.num_guc_ids / 16 > 32 ? \
+	 (guc)->submission_state.num_guc_ids / 16 : 32)
+
  /*
   * Below is a set of functions which control the GuC scheduling state which
   * require a lock.
@@ -1176,6 +1188,10 @@ int intel_guc_submission_init(struct intel_guc *guc)
  	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
  	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
  				     destroyed_worker_func);
+	guc->submission_state.guc_ids_bitmap =
+		bitmap_zalloc(NUMBER_MULTI_LRC_GUC_ID(guc), GFP_KERNEL);
+	if (!guc->submission_state.guc_ids_bitmap)
+		return -ENOMEM;
 
  	return 0;
  }
@@ -1188,6 +1204,7 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  	guc_lrc_desc_pool_destroy(guc);
  	guc_flush_destroyed_contexts(guc);
  	i915_sched_engine_put(guc->sched_engine);
+	bitmap_free(guc->submission_state.guc_ids_bitmap);
  }
 
  static void queue_request(struct i915_sched_engine *sched_engine,
@@ -1239,18 +1256,43 @@ static void guc_submit_request(struct i915_request *rq)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
 
-static int new_guc_id(struct intel_guc *guc)
+static int new_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
-	return ida_simple_get(&guc->submission_state.guc_ids, 0,
-			      guc->submission_state.num_guc_ids, GFP_KERNEL |
-			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+	int ret;
+
+	GEM_BUG_ON(intel_context_is_child(ce));
+
+	if (intel_context_is_parent(ce))
+		ret = bitmap_find_free_region(guc->submission_state.guc_ids_bitmap,
+					      NUMBER_MULTI_LRC_GUC_ID(guc),
+					      order_base_2(ce->guc_number_children
+							   + 1));
+	else
+		ret = ida_simple_get(&guc->submission_state.guc_ids,
+				     NUMBER_MULTI_LRC_GUC_ID(guc),
+				     guc->submission_state.num_guc_ids,
+				     GFP_KERNEL | __GFP_RETRY_MAYFAIL |
+				     __GFP_NOWARN);
+	if (unlikely(ret < 0))
+		return ret;
+
+	ce->guc_id.id = ret;
+	return 0;
  }
 
  static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_submit_request(struct i915_request *rq) · line 1292

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +7 -2
+	GEM_BUG_ON(intel_context_is_child(ce));
+
  	if (!context_guc_id_invalid(ce)) {
-		ida_simple_remove(&guc->submission_state.guc_ids,
-				  ce->guc_id.id);
+		if (intel_context_is_parent(ce))
+			bitmap_release_region(guc->submission_state.guc_ids_bitmap,
+					      ce->guc_id.id,
+					      order_base_2(ce->guc_number_children
+							   + 1));

Is there any check against adding/removing children when the guc_ids are allocated? Presumably it shouldn't ever happen but if it did then the bitmap_release would not match the allocation. Maybe add BUG_ON(ce->guc_id) to the parent/child link functions (if it's not there already?).

Quoted 7 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+		else
+			ida_simple_remove(&guc->submission_state.guc_ids,
+					  ce->guc_id.id);
  		reset_lrc_desc(guc, ce->guc_id.id);
  		set_context_guc_id_invalid(ce);
  	}

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void release_guc_id(struct intel_guc *guc, struct intel_context *ce) · line 1314

Quoted 9 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -3
@@ -1267,49 +1309,60 @@ static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
 
-static int steal_guc_id(struct intel_guc *guc)
+static int steal_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
-	struct intel_context *ce;
-	int guc_id;
+	struct intel_context *cn;

Leaving this as 'ce' and calling the input parameter 'ce_in' would have made for significantly easier to read diffs!

Quoted 12 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -2
  	lockdep_assert_held(&guc->submission_state.lock);
+	GEM_BUG_ON(intel_context_is_child(ce));
+	GEM_BUG_ON(intel_context_is_parent(ce));
 
  	if (!list_empty(&guc->submission_state.guc_id_list)) {
-		ce = list_first_entry(&guc->submission_state.guc_id_list,
+		cn = list_first_entry(&guc->submission_state.guc_id_list,
  				      struct intel_context,
  				      guc_id.link);
 
-		GEM_BUG_ON(atomic_read(&ce->guc_id.ref));

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void release_guc_id(struct intel_guc *guc, struct intel_context *ce) · line 1288

Quoted 10 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -5
-		GEM_BUG_ON(context_guc_id_invalid(ce));
-
-		list_del_init(&ce->guc_id.link);
-		guc_id = ce->guc_id.id;
+		GEM_BUG_ON(atomic_read(&cn->guc_id.ref));
+		GEM_BUG_ON(context_guc_id_invalid(cn));
+		GEM_BUG_ON(intel_context_is_child(cn));
+		GEM_BUG_ON(intel_context_is_parent(cn));
 
-		spin_lock(&ce->guc_state.lock);

As far as I can tell, the only actual change to this function (beyond 'ce_in->id = id' vs 'return id' and adding anti-family asserts) is that this spinlock was dropped. However, I'm not seeing any replacement for it or any comment about why the spinlock is no longer necessary.

John.

Quoted 68 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +24 -10
-		clr_context_registered(ce);
-		spin_unlock(&ce->guc_state.lock);
+		list_del_init(&cn->guc_id.link);
+		ce->guc_id = cn->guc_id;
+		clr_context_registered(cn);
+		set_context_guc_id_invalid(cn);
 
-		set_context_guc_id_invalid(ce);
-		return guc_id;
+		return 0;
  	} else {
  		return -EAGAIN;
  	}
  }
 
-static int assign_guc_id(struct intel_guc *guc, u16 *out)
+static int assign_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
  	int ret;
 
  	lockdep_assert_held(&guc->submission_state.lock);
+	GEM_BUG_ON(intel_context_is_child(ce));
 
-	ret = new_guc_id(guc);
+	ret = new_guc_id(guc, ce);
  	if (unlikely(ret < 0)) {
-		ret = steal_guc_id(guc);
+		if (intel_context_is_parent(ce))
+			return -ENOSPC;
+
+		ret = steal_guc_id(guc, ce);
  		if (ret < 0)
  			return ret;
  	}
 
-	*out = ret;
+	if (intel_context_is_parent(ce)) {
+		struct intel_context *child;
+		int i = 1;
+
+		for_each_child(ce, child)
+			child->guc_id.id = ce->guc_id.id + i++;
+	}
+
  	return 0;
  }
 
@@ -1327,7 +1380,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	might_lock(&ce->guc_state.lock);
 
  	if (context_guc_id_invalid(ce)) {
-		ret = assign_guc_id(guc, &ce->guc_id.id);
+		ret = assign_guc_id(guc, ce);
  		if (ret)
  			goto out_unlock;
  		ret = 1;	/* Indidcates newly assigned guc_id */
@@ -1369,8 +1422,10 @@ 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);
+	GEM_BUG_ON(intel_context_is_child(ce));
 
-	if (unlikely(context_guc_id_invalid(ce)))
+	if (unlikely(context_guc_id_invalid(ce) ||
+		     intel_context_is_parent(ce)))
  		return;
 
  	spin_lock_irqsave(&guc->submission_state.lock, flags);
#59 lore raw

Re: [Intel-gfx] [PATCH 14/27] drm/i915/guc: Assign contexts in parent-child relationship consecutive guc_ids

Quoted 46 lines · John Harrison
Quoted 45 lines · Matthew Brost

Assign contexts in parent-child relationship consecutive guc_ids. This is accomplished by partitioning guc_id space between ones that need to be consecutive (1/16 available guc_ids) and ones that do not (15/16 of available guc_ids). The consecutive search is implemented via the bitmap API.

This is a precursor to the full GuC multi-lrc implementation but aligns to how GuC mutli-lrc interface is defined - guc_ids must be consecutive when using the GuC multi-lrc interface.

v2:
(Daniel Vetter)
- Explictly state why we assign consecutive guc_ids

---
  drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   6 +-
  .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 107 +++++++++++++-----
  2 files changed, 86 insertions(+), 27 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.h +5 -1
index 023953e77553..3f95b1b4f15c 100644
@@ -61,9 +61,13 @@ struct intel_guc {
  		 */
  		spinlock_t lock;
  		/**
-		 * @guc_ids: used to allocate new guc_ids
+		 * @guc_ids: used to allocate new guc_ids, single-lrc
  		 */
  		struct ida guc_ids;
+		/**
+		 * @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc
+		 */
+		unsigned long *guc_ids_bitmap;
  		/** @num_guc_ids: number of guc_ids that can be used */
  		u32 num_guc_ids;
  		/** @max_guc_ids: max number of guc_ids that can be used */
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +0 -0
index 00d54bb00bfb..e9dfd43d29a0 100644
@@ -125,6 +125,18 @@ guc_create_virtual(struct intel_engine_cs **siblings, unsigned int count);
  #define GUC_REQUEST_SIZE 64 /* bytes */
Quoted 9 lines

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

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -0
+/*
+ * We reserve 1/16 of the guc_ids for multi-lrc as these need to be contiguous
+ * per the GuC submission interface. A different allocation algorithm is used
+ * (bitmap vs. ida) between multi-lrc and single-lrc hence the reason to

The 'hence' clause seems to be attached to the wrong reason. The id space is partition because of the contiguous vs random requirements of multi vs single LRC, not because a different allocator is used in one partion vs the other.

Kinda? The reason I partitioned it because to algorithms are different, we could a unified space with a single algorithm, right? It was just easier split the space and use 2 already existing data structures rather cook up an algorithm in a unified space. There isn't a requirement from the GuC that the space is partitioned, the only requirement is multi-lrc IDs are contiguous. All this being said, I think comment is correct.

Quoted 64 lines
Quoted 64 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +36 -5
+ * partition the guc_id space. We believe the number of multi-lrc contexts in
+ * use should be low and 1/16 should be sufficient. Minimum of 32 guc_ids for
+ * multi-lrc.
+ */
+#define NUMBER_MULTI_LRC_GUC_ID(guc) \
+	((guc)->submission_state.num_guc_ids / 16 > 32 ? \
+	 (guc)->submission_state.num_guc_ids / 16 : 32)
+
  /*
   * Below is a set of functions which control the GuC scheduling state which
   * require a lock.
@@ -1176,6 +1188,10 @@ int intel_guc_submission_init(struct intel_guc *guc)
  	INIT_LIST_HEAD(&guc->submission_state.destroyed_contexts);
  	intel_gt_pm_unpark_work_init(&guc->submission_state.destroyed_worker,
  				     destroyed_worker_func);
+	guc->submission_state.guc_ids_bitmap =
+		bitmap_zalloc(NUMBER_MULTI_LRC_GUC_ID(guc), GFP_KERNEL);
+	if (!guc->submission_state.guc_ids_bitmap)
+		return -ENOMEM;
  	return 0;
  }
@@ -1188,6 +1204,7 @@ void intel_guc_submission_fini(struct intel_guc *guc)
  	guc_lrc_desc_pool_destroy(guc);
  	guc_flush_destroyed_contexts(guc);
  	i915_sched_engine_put(guc->sched_engine);
+	bitmap_free(guc->submission_state.guc_ids_bitmap);
  }
  static void queue_request(struct i915_sched_engine *sched_engine,
@@ -1239,18 +1256,43 @@ static void guc_submit_request(struct i915_request *rq)
  	spin_unlock_irqrestore(&sched_engine->lock, flags);
  }
-static int new_guc_id(struct intel_guc *guc)
+static int new_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
-	return ida_simple_get(&guc->submission_state.guc_ids, 0,
-			      guc->submission_state.num_guc_ids, GFP_KERNEL |
-			      __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
+	int ret;
+
+	GEM_BUG_ON(intel_context_is_child(ce));
+
+	if (intel_context_is_parent(ce))
+		ret = bitmap_find_free_region(guc->submission_state.guc_ids_bitmap,
+					      NUMBER_MULTI_LRC_GUC_ID(guc),
+					      order_base_2(ce->guc_number_children
+							   + 1));
+	else
+		ret = ida_simple_get(&guc->submission_state.guc_ids,
+				     NUMBER_MULTI_LRC_GUC_ID(guc),
+				     guc->submission_state.num_guc_ids,
+				     GFP_KERNEL | __GFP_RETRY_MAYFAIL |
+				     __GFP_NOWARN);
+	if (unlikely(ret < 0))
+		return ret;
+
+	ce->guc_id.id = ret;
+	return 0;
  }
  static void __release_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
+	GEM_BUG_ON(intel_context_is_child(ce));
+
  	if (!context_guc_id_invalid(ce)) {
-		ida_simple_remove(&guc->submission_state.guc_ids,
Quoted 8 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void guc_submit_request(struct i915_request *rq) · line 1292

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +5 -1
-				  ce->guc_id.id);
+		if (intel_context_is_parent(ce))
+			bitmap_release_region(guc->submission_state.guc_ids_bitmap,
+					      ce->guc_id.id,
+					      order_base_2(ce->guc_number_children
+							   + 1));

Is there any check against adding/removing children when the guc_ids are allocated? Presumably it shouldn't ever happen but if it did then the

I don't have any protection for that but adding something like this isn't bad idea.

Quoted 3 lines

bitmap_release would not match the allocation. Maybe add BUG_ON(ce->guc_id) to the parent/child link functions (if it's not there already?).

Do you something like below in this function?

GEM_BUG_ON(guc_id_not_is_use());

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -0
+		else
+			ida_simple_remove(&guc->submission_state.guc_ids,
+					  ce->guc_id.id);
  		reset_lrc_desc(guc, ce->guc_id.id);
  		set_context_guc_id_invalid(ce);
  	}
@@ -1267,49 +1309,60 @@ static void release_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	spin_unlock_irqrestore(&guc->submission_state.lock, flags);
  }
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void release_guc_id(struct intel_guc *guc, struct intel_context *ce) · line 1314

Quoted 6 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +2 -3
-static int steal_guc_id(struct intel_guc *guc)
+static int steal_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
-	struct intel_context *ce;
-	int guc_id;
+	struct intel_context *cn;

Leaving this as 'ce' and calling the input parameter 'ce_in' would have made for significantly easier to read diffs!

Yea probably but I think we should change the style to make diff easier to read.

Quoted 14 lines
Quoted 14 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +4 -6
  	lockdep_assert_held(&guc->submission_state.lock);
+	GEM_BUG_ON(intel_context_is_child(ce));
+	GEM_BUG_ON(intel_context_is_parent(ce));
  	if (!list_empty(&guc->submission_state.guc_id_list)) {
-		ce = list_first_entry(&guc->submission_state.guc_id_list,
+		cn = list_first_entry(&guc->submission_state.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.id;
+		GEM_BUG_ON(atomic_read(&cn->guc_id.ref));
Quoted 9 lines

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c static void release_guc_id(struct intel_guc *guc, struct intel_context *ce) · line 1288

Quoted 4 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +3 -1
+		GEM_BUG_ON(context_guc_id_invalid(cn));
+		GEM_BUG_ON(intel_context_is_child(cn));
+		GEM_BUG_ON(intel_context_is_parent(cn));
-		spin_lock(&ce->guc_state.lock);

As far as I can tell, the only actual change to this function (beyond 'ce_in->id = id' vs 'return id' and adding anti-family asserts) is that this spinlock was dropped. However, I'm not seeing any replacement for it or any comment about why the spinlock is no longer necessary.

Good catch, the lock shouldn't be dropped.

Matt

Quoted 62 lines

John.

Quoted 58 lines
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +24 -10
-		clr_context_registered(ce);
-		spin_unlock(&ce->guc_state.lock);
+		list_del_init(&cn->guc_id.link);
+		ce->guc_id = cn->guc_id;
+		clr_context_registered(cn);
+		set_context_guc_id_invalid(cn);
-		set_context_guc_id_invalid(ce);
-		return guc_id;
+		return 0;
  	} else {
  		return -EAGAIN;
  	}
  }
-static int assign_guc_id(struct intel_guc *guc, u16 *out)
+static int assign_guc_id(struct intel_guc *guc, struct intel_context *ce)
  {
  	int ret;
  	lockdep_assert_held(&guc->submission_state.lock);
+	GEM_BUG_ON(intel_context_is_child(ce));
-	ret = new_guc_id(guc);
+	ret = new_guc_id(guc, ce);
  	if (unlikely(ret < 0)) {
-		ret = steal_guc_id(guc);
+		if (intel_context_is_parent(ce))
+			return -ENOSPC;
+
+		ret = steal_guc_id(guc, ce);
  		if (ret < 0)
  			return ret;
  	}
-	*out = ret;
+	if (intel_context_is_parent(ce)) {
+		struct intel_context *child;
+		int i = 1;
+
+		for_each_child(ce, child)
+			child->guc_id.id = ce->guc_id.id + i++;
+	}
+
  	return 0;
  }
@@ -1327,7 +1380,7 @@ static int pin_guc_id(struct intel_guc *guc, struct intel_context *ce)
  	might_lock(&ce->guc_state.lock);
  	if (context_guc_id_invalid(ce)) {
-		ret = assign_guc_id(guc, &ce->guc_id.id);
+		ret = assign_guc_id(guc, ce);
  		if (ret)
  			goto out_unlock;
  		ret = 1;	/* Indidcates newly assigned guc_id */
@@ -1369,8 +1422,10 @@ 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);
+	GEM_BUG_ON(intel_context_is_child(ce));
-	if (unlikely(context_guc_id_invalid(ce)))
+	if (unlikely(context_guc_id_invalid(ce) ||
+		     intel_context_is_parent(ce)))
  		return;
  	spin_lock_irqsave(&guc->submission_state.lock, flags);
#60 lore raw

PATCH 15/27 drm/i915/guc: Implement multi-lrc submission

In reply to Matthew Brost

Implement multi-lrc submission via a single workqueue entry and single H2G. The workqueue entry contains an updated tail value for each request, of all the contexts in the multi-lrc submission, and updates these values simultaneously. As such, the tasklet and bypass path have been updated to coalesce requests into a single submission.

---
 drivers/gpu/drm/i915/gt/uc/intel_guc.c        |  21 ++
 drivers/gpu/drm/i915/gt/uc/intel_guc.h        |   8 +
 drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c     |  24 +-
 drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h   |   6 +-
 .../gpu/drm/i915/gt/uc/intel_guc_submission.c | 312 +++++++++++++++---
 drivers/gpu/drm/i915/i915_request.h           |   8 +
 6 files changed, 317 insertions(+), 62 deletions(-)
drivers/gpu/drm/i915/gt/uc/intel_guc.c +21 -0
index fbfcae727d7f..879aef662b2e 100644
@@ -748,3 +748,24 @@ void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p)
 		}
 	}
 }
+
+void intel_guc_write_barrier(struct intel_guc *guc)
+{
+	struct intel_gt *gt = guc_to_gt(guc);
+
+	if (i915_gem_object_is_lmem(guc->ct.vma->obj)) {
+		GEM_BUG_ON(guc->send_regs.fw_domains);
+		/*
+		 * This register is used by the i915 and GuC for MMIO based
+		 * communication. Once we are in this code CTBs are the only
+		 * method the i915 uses to communicate with the GuC so it is
+		 * safe to write to this register (a value of 0 is NOP for MMIO
+		 * communication). If we ever start mixing CTBs and MMIOs a new
+		 * register will have to be chosen.
+		 */
+		intel_uncore_write_fw(gt->uncore, GEN11_SOFT_SCRATCH(0), 0);
+	} else {
+		/* wmb() sufficient for a barrier if in smem */
+		wmb();
+	}
+}
drivers/gpu/drm/i915/gt/uc/intel_guc.h +8 -0
index 3f95b1b4f15c..0ead2406d03c 100644
@@ -37,6 +37,12 @@ struct intel_guc {
 	/* Global engine used to submit requests to GuC */
 	struct i915_sched_engine *sched_engine;
 	struct i915_request *stalled_request;
+	enum {
+		STALL_NONE,
+		STALL_REGISTER_CONTEXT,
+		STALL_MOVE_LRC_TAIL,
+		STALL_ADD_REQUEST,
+	} submission_stall_reason;
 
 	/* intel_guc_recv interrupt related state */
 	spinlock_t irq_lock;
@@ -332,4 +338,6 @@ void intel_guc_submission_cancel_requests(struct intel_guc *guc);
 
 void intel_guc_load_status(struct intel_guc *guc, struct drm_printer *p);
 
+void intel_guc_write_barrier(struct intel_guc *guc);
+
 #endif
drivers/gpu/drm/i915/gt/uc/intel_guc_ct.c +1 -23
index 20c710a74498..10d1878d2826 100644
@@ -377,28 +377,6 @@ static u32 ct_get_next_fence(struct intel_guc_ct *ct)
 	return ++ct->requests.last_fence;
 }
 
-static void write_barrier(struct intel_guc_ct *ct)
-{
-	struct intel_guc *guc = ct_to_guc(ct);
-	struct intel_gt *gt = guc_to_gt(guc);
-
-	if (i915_gem_object_is_lmem(guc->ct.vma->obj)) {
-		GEM_BUG_ON(guc->send_regs.fw_domains);
-		/*
-		 * This register is used by the i915 and GuC for MMIO based
-		 * communication. Once we are in this code CTBs are the only
-		 * method the i915 uses to communicate with the GuC so it is
-		 * safe to write to this register (a value of 0 is NOP for MMIO
-		 * communication). If we ever start mixing CTBs and MMIOs a new
-		 * register will have to be chosen.
-		 */
-		intel_uncore_write_fw(gt->uncore, GEN11_SOFT_SCRATCH(0), 0);
-	} else {
-		/* wmb() sufficient for a barrier if in smem */
-		wmb();
-	}
-}
-
 static int ct_write(struct intel_guc_ct *ct,
 		    const u32 *action,
 		    u32 len /* in dwords */,
@@ -468,7 +446,7 @@ static int ct_write(struct intel_guc_ct *ct,
 	 * make sure H2G buffer update and LRC tail update (if this triggering a
 	 * submission) are visible before updating the descriptor tail
 	 */
-	write_barrier(ct);
+	intel_guc_write_barrier(ct_to_guc(ct));
 
 	/* update local copies */
 	ctb->tail = tail;
drivers/gpu/drm/i915/gt/uc/intel_guc_fwif.h +4 -2
index 0e600a3b8f1e..6cd26dc060d1 100644
@@ -65,12 +65,14 @@
 #define   WQ_TYPE_PSEUDO		(0x2 << WQ_TYPE_SHIFT)
 #define   WQ_TYPE_INORDER		(0x3 << WQ_TYPE_SHIFT)
 #define   WQ_TYPE_NOOP			(0x4 << WQ_TYPE_SHIFT)
-#define WQ_TARGET_SHIFT			10
+#define   WQ_TYPE_MULTI_LRC		(0x5 << WQ_TYPE_SHIFT)
+#define WQ_TARGET_SHIFT			8
 #define WQ_LEN_SHIFT			16
 #define WQ_NO_WCFLUSH_WAIT		(1 << 27)
 #define WQ_PRESENT_WORKLOAD		(1 << 28)
 
-#define WQ_RING_TAIL_SHIFT		20
+#define WQ_GUC_ID_SHIFT			0
+#define WQ_RING_TAIL_SHIFT		18
 #define WQ_RING_TAIL_MAX		0x7FF	/* 2^11 QWords */
 #define WQ_RING_TAIL_MASK		(WQ_RING_TAIL_MAX << WQ_RING_TAIL_SHIFT)
 
drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c +275 -37

437 lines not on this page · raw message

drivers/gpu/drm/i915/i915_request.h +8 -0
index 177eaf55adff..8f0073e19079 100644
@@ -139,6 +139,14 @@ enum {
 	 * the GPU. Here we track such boost requests on a per-request basis.
 	 */
 	I915_FENCE_FLAG_BOOST,
+
+	/*
+	 * I915_FENCE_FLAG_SUBMIT_PARALLEL - request with a context in a
+	 * parent-child relationship (parallel submission, multi-lrc) should
+	 * trigger a submission to the GuC rather than just moving the context
+	 * tail.
+	 */
+	I915_FENCE_FLAG_SUBMIT_PARALLEL,
 };
 
 /**
Signature · 1 line
2.32.0
#61 lore raw

Re: [PATCH 15/27] drm/i915/guc: Implement multi-lrc submission

Thank you for the patch! Perhaps something to improve:

Hi Matthew,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on drm-intel/for-linux-next]
[also build test WARNING on drm-tip/drm-tip next-20210820]
[cannot apply to drm-exynos/exynos-drm-next tegra-drm/drm/tegra/for-next linus/master drm/drm-next v5.14-rc6] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Matthew-Brost/Parallel-submission-aka-multi-bb-execbuf/20210821-065348
base:   git://anongit.freedesktop.org/drm-intel for-linux-next
config: x86_64-buildonly-randconfig-r002-20210821 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 9e9d70591e72fc6762b4b9a226b68ed1307419bf)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/55917f6ee7575ffb033e6b19a9eb38c3210e14db
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Matthew-Brost/Parallel-submission-aka-multi-bb-execbuf/20210821-065348 git checkout 55917f6ee7575ffb033e6b19a9eb38c3210e14db
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c:1467:6: warning: no previous prototype for function 'need_tasklet' [-Wmissing-prototypes]
   bool need_tasklet(struct intel_guc *guc, struct i915_request *rq)
        ^

drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c:1467:1: note: declare 'static' if the function is not intended to be used outside of this translation unit bool need_tasklet(struct intel_guc *guc, struct i915_request *rq)
^
static
1 warning generated.

vim +/need_tasklet +1467 drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c

1466

> 1467	bool need_tasklet(struct intel_guc *guc, struct i915_request *rq)

1468 {
1469 struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
1470 struct intel_context *ce = request_to_scheduling_context(rq);
1471
1472 return submission_disabled(guc) || guc->stalled_request ||
1473 !i915_sched_engine_is_empty(sched_engine) ||
1474 !lrc_desc_registered(guc, ce->guc_id.id);
1475 }
1476

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#62 lore raw

Re: [PATCH 15/27] drm/i915/guc: Implement multi-lrc submission

In reply to Matthew Brost
Thank you for the patch! Perhaps something to improve:

Hi Matthew,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on drm-intel/for-linux-next]
[also build test WARNING on drm-tip/drm-tip drm-exynos/exynos-drm-next next-20210820] [cannot apply to tegra-drm/drm/tegra/for-next linus/master drm/drm-next v5.14-rc6] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Matthew-Brost/Parallel-submission-aka-multi-bb-execbuf/20210821-065348
base:   git://anongit.freedesktop.org/drm-intel for-linux-next
config: x86_64-randconfig-a011-20210821 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/55917f6ee7575ffb033e6b19a9eb38c3210e14db
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Matthew-Brost/Parallel-submission-aka-multi-bb-execbuf/20210821-065348
        git checkout 55917f6ee7575ffb033e6b19a9eb38c3210e14db
        # save the attached .config to linux build tree
        make W=1 ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c:1467:6: warning: no previous prototype for 'need_tasklet' [-Wmissing-prototypes]

1467 | bool need_tasklet(struct intel_guc *guc, struct i915_request *rq)
| ^~~~~~~~~~~~

vim +/need_tasklet +1467 drivers/gpu/drm/i915/gt/uc/intel_guc_submission.c

1466

> 1467	bool need_tasklet(struct intel_guc *guc, struct i915_request *rq)

1468 {
1469 struct i915_sched_engine *sched_engine = rq->engine->sched_engine;
1470 struct intel_context *ce = request_to_scheduling_context(rq);
1471
1472 return submission_disabled(guc) || guc->stalled_request ||
1473 !i915_sched_engine_is_empty(sched_engine) ||
1474 !lrc_desc_registered(guc, ce->guc_id.id);
1475 }
1476

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org