Skip to contentSkip to conversation

Upstreaming prep / all of mbrosts patches

PATCH In review Matthew Brost 8 participants 126 messages May 2, 2023 – Jun 12, 2023 Page 1 of 2

Subsystems (16)
lore
Share & tools
lore

Matthew Brost

author maintainer
#0 lore raw

v2 cover Upstreaming prep / all of mbrosts patches

Series includes:

- DRM scheduler changes for firmware backends (1 to 1 entity to scheduler)
- LR workload story
- VM LRU handling
- GuC doorbell submission
- Basic GPUVA
- Sparse binding support
- GPUVA + extobj + drm exec (collaboration with dakr + Francois Dugast)
- GPUVA + userptr (minimal, more can be once Nouveua has userptr)
- Fix fencing rules for compute / fault mode
- Remove async worker for VM + error handling updates
- Kernel doc for VM bind

Series is not fully ready for upstream and some of these things need to get merged upstream first but overall it is largely correct and certainly step in the right direction. Based on its size and the fact it took me 8 hours to rebase this today I'd say let's get this tree and fixup everything else in place.

Minor uAPI breakage, IGT series:
https://patchwork.freedesktop.org/series/117177/

gitlab link:
https://gitlab.freedesktop.org/drm/xe/kernel/-/merge_requests/344

Christian König (1):
drm: execution context for GEM buffers v3

Danilo Krummrich (2):
maple_tree: split up MA_STATE() macro
drm: manager to keep track of GPUs VA mappings

Matthew Brost (28):
drm/sched: Add run_wq argument to drm_sched_init drm/sched: Move schedule policy to scheduler drm/sched: Add DRM_SCHED_POLICY_SINGLE_ENTITY scheduling policy drm/xe: Use DRM_SCHED_POLICY_SINGLE_ENTITY mode drm/xe: Long running job update
drm/xe: Ensure LR engines are not persistent
drm/xe: Only try to lock external BOs in VM bind drm/xe: VM LRU bulk move
drm/xe/guc: Read HXG fields from DW1 of G2H response drm/xe/guc: Return the lower part of blocking H2G message drm/xe/guc: Use doorbells for submission if possible drm/xe/guc: Print doorbell ID in GuC engine debugfs entry
maple_tree: Export mas_preallocate
drm/xe: Port Xe to GPUVA
drm/xe: NULL binding implementation
drm/xe: Avoid doing rebinds
drm/xe: Reduce the number list links in xe_vma drm/xe: Optimize size of xe_vma allocation
drm/gpuva: Add drm device to GPUVA manager
drm/gpuva: Move dma-resv to GPUVA manager
drm/gpuva: Add support for extobj
drm/xe: Userptr refactor
drm/exec: Always compile drm_exec
drm/xe: Use drm_exec for locking rather than TTM exec helpers drm/xe: Allow dma-fences as in-syncs for compute / faulting VM drm/xe: Allow compute VMs to output dma-fences on binds drm/xe: remove async worker, sync binds, new error handling drm/xe/uapi: Add some VM bind kernel doc

 Documentation/gpu/drm-mm.rst                 |   43 +
 drivers/gpu/drm/Kconfig                      |    6 +
 drivers/gpu/drm/Makefile                     |    4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c   |    3 +-
 drivers/gpu/drm/drm_debugfs.c                |   41 +
 drivers/gpu/drm/drm_exec.c                   |  248 ++
 drivers/gpu/drm/drm_gem.c                    |    3 +
 drivers/gpu/drm/drm_gpuva_mgr.c              | 1779 ++++++++++++
 drivers/gpu/drm/etnaviv/etnaviv_sched.c      |    5 +-
 drivers/gpu/drm/i915/display/intel_display.c |    6 +-
 drivers/gpu/drm/lima/lima_sched.c            |    5 +-
 drivers/gpu/drm/msm/msm_ringbuffer.c         |    5 +-
 drivers/gpu/drm/panfrost/panfrost_job.c      |    5 +-
 drivers/gpu/drm/scheduler/sched_entity.c     |   84 +-
 drivers/gpu/drm/scheduler/sched_fence.c      |    2 +-
 drivers/gpu/drm/scheduler/sched_main.c       |   88 +-
 drivers/gpu/drm/v3d/v3d_sched.c              |   25 +-
 drivers/gpu/drm/xe/Kconfig                   |    1 +
 drivers/gpu/drm/xe/regs/xe_guc_regs.h        |    1 +
 drivers/gpu/drm/xe/tests/xe_bo.c             |   26 +-
 drivers/gpu/drm/xe/tests/xe_migrate.c        |    6 +-
 drivers/gpu/drm/xe/xe_bo.c                   |  100 +-
 drivers/gpu/drm/xe/xe_bo.h                   |   13 +-
 drivers/gpu/drm/xe/xe_bo_evict.c             |   24 +-
 drivers/gpu/drm/xe/xe_bo_types.h             |    1 -
 drivers/gpu/drm/xe/xe_device.c               |    2 +-
 drivers/gpu/drm/xe/xe_dma_buf.c              |    2 +-
 drivers/gpu/drm/xe/xe_engine.c               |   50 +-
 drivers/gpu/drm/xe/xe_engine.h               |    4 +
 drivers/gpu/drm/xe/xe_engine_types.h         |    1 +
 drivers/gpu/drm/xe/xe_exec.c                 |  117 +-
 drivers/gpu/drm/xe/xe_execlist.c             |    3 +-
 drivers/gpu/drm/xe/xe_gt_pagefault.c         |   84 +-
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c  |   14 +-
 drivers/gpu/drm/xe/xe_guc.c                  |    6 +
 drivers/gpu/drm/xe/xe_guc_ct.c               |   12 +-
 drivers/gpu/drm/xe/xe_guc_engine_types.h     |    9 +
 drivers/gpu/drm/xe/xe_guc_pc.c               |    6 +-
 drivers/gpu/drm/xe/xe_guc_submit.c           |  398 ++-
 drivers/gpu/drm/xe/xe_guc_submit.h           |    1 +
 drivers/gpu/drm/xe/xe_guc_types.h            |    4 +
 drivers/gpu/drm/xe/xe_huc.c                  |    2 +-
 drivers/gpu/drm/xe/xe_lrc.c                  |    8 +-
 drivers/gpu/drm/xe/xe_migrate.c              |   31 +-
 drivers/gpu/drm/xe/xe_pt.c                   |  198 +-
 drivers/gpu/drm/xe/xe_sync.c                 |   26 +-
 drivers/gpu/drm/xe/xe_sync.h                 |    2 +-
 drivers/gpu/drm/xe/xe_trace.h                |   20 +-
 drivers/gpu/drm/xe/xe_vm.c                   | 2567 +++++++-----------
 drivers/gpu/drm/xe/xe_vm.h                   |  135 +-
 drivers/gpu/drm/xe/xe_vm_madvise.c           |  125 +-
 drivers/gpu/drm/xe/xe_vm_types.h             |  324 ++-
 drivers/gpu/drm/xe/xe_wait_user_fence.c      |   43 +-
 include/drm/drm_debugfs.h                    |   24 +
 include/drm/drm_drv.h                        |    7 +
 include/drm/drm_exec.h                       |  115 +
 include/drm/drm_gem.h                        |   75 +
 include/drm/drm_gpuva_mgr.h                  |  759 ++++++
 include/drm/gpu_scheduler.h                  |   29 +-
 include/linux/maple_tree.h                   |    7 +-
 include/uapi/drm/xe_drm.h                    |  128 +-
 lib/maple_tree.c                             |    1 +
 62 files changed, 5543 insertions(+), 2320 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_exec.c
 create mode 100644 drivers/gpu/drm/drm_gpuva_mgr.c
 create mode 100644 include/drm/drm_exec.h
 create mode 100644 include/drm/drm_gpuva_mgr.h
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v2 1/31 drm/sched: Add run_wq argument to drm_sched_init

We will have this argument upstream, lets pull into the Xe repo.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  2 +-
 drivers/gpu/drm/etnaviv/etnaviv_sched.c    |  2 +-
 drivers/gpu/drm/lima/lima_sched.c          |  2 +-
 drivers/gpu/drm/msm/msm_ringbuffer.c       |  2 +-
 drivers/gpu/drm/panfrost/panfrost_job.c    |  2 +-
 drivers/gpu/drm/scheduler/sched_main.c     |  4 +++-
 drivers/gpu/drm/v3d/v3d_sched.c            | 10 +++++-----
 drivers/gpu/drm/xe/xe_execlist.c           |  2 +-
 drivers/gpu/drm/xe/xe_guc_submit.c         |  2 +-
 include/drm/gpu_scheduler.h                |  1 +
 10 files changed, 16 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -1
index 902f9b5ff82c..fe28f6b71fe3 100644
@@ -2364,7 +2364,7 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
 			break;
 		}
 
-		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
+		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops, NULL,
 				   ring->num_hw_submission, amdgpu_job_hang_limit,
 				   timeout, adev->reset_domain->wq,
 				   ring->sched_score, ring->name,
drivers/gpu/drm/etnaviv/etnaviv_sched.c +1 -1
index 1ae87dfd19c4..8486a2923f1b 100644
@@ -133,7 +133,7 @@ int etnaviv_sched_init(struct etnaviv_gpu *gpu)
 {
 	int ret;
 
-	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops,
+	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops, NULL,
 			     etnaviv_hw_jobs_limit, etnaviv_job_hang_limit,
 			     msecs_to_jiffies(500), NULL, NULL,
 			     dev_name(gpu->dev), gpu->dev);
drivers/gpu/drm/lima/lima_sched.c +1 -1
index ff003403fbbc..54f53bece27c 100644
@@ -488,7 +488,7 @@ int lima_sched_pipe_init(struct lima_sched_pipe *pipe, const char *name)
 
 	INIT_WORK(&pipe->recover_work, lima_sched_recover_work);
 
-	return drm_sched_init(&pipe->base, &lima_sched_ops, 1,
+	return drm_sched_init(&pipe->base, &lima_sched_ops, NULL, 1,
 			      lima_job_hang_limit,
 			      msecs_to_jiffies(timeout), NULL,
 			      NULL, name, pipe->ldev->dev);
drivers/gpu/drm/msm/msm_ringbuffer.c +1 -1
index 57a8e9564540..5879fc262047 100644
@@ -95,7 +95,7 @@ struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
 	 /* currently managing hangcheck ourselves: */
 	sched_timeout = MAX_SCHEDULE_TIMEOUT;
 
-	ret = drm_sched_init(&ring->sched, &msm_sched_ops,
+	ret = drm_sched_init(&ring->sched, &msm_sched_ops, NULL,
 			num_hw_submissions, 0, sched_timeout,
 			NULL, NULL, to_msm_bo(ring->bo)->name, gpu->dev->dev);
 	if (ret) {
drivers/gpu/drm/panfrost/panfrost_job.c +1 -1
index dbc597ab46fb..f48b07056a16 100644
@@ -815,7 +815,7 @@ int panfrost_job_init(struct panfrost_device *pfdev)
 		js->queue[j].fence_context = dma_fence_context_alloc(1);
 
 		ret = drm_sched_init(&js->queue[j].sched,
-				     &panfrost_sched_ops,
+				     &panfrost_sched_ops, NULL,
 				     nentries, 0,
 				     msecs_to_jiffies(JOB_TIMEOUT_MS),
 				     pfdev->reset.wq,
drivers/gpu/drm/scheduler/sched_main.c +3 -1
index cfd8a838e283..e79b9c760efe 100644
@@ -1182,6 +1182,7 @@ static void drm_sched_main(struct work_struct *w)
  *
  * @sched: scheduler instance
  * @ops: backend operations for this scheduler
+ * @run_wq: workqueue to use for run work. If NULL, the system_wq is used
  * @hw_submission: number of hw submissions that can be in flight
  * @hang_limit: number of times to allow a job to hang before dropping it
  * @timeout: timeout value in jiffies for the scheduler
@@ -1195,6 +1196,7 @@ static void drm_sched_main(struct work_struct *w)
  */
 int drm_sched_init(struct drm_gpu_scheduler *sched,
 		   const struct drm_sched_backend_ops *ops,
+		   struct workqueue_struct *run_wq,
 		   unsigned hw_submission, unsigned hang_limit,
 		   long timeout, struct workqueue_struct *timeout_wq,
 		   atomic_t *score, const char *name, struct device *dev)
@@ -1203,9 +1205,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
 	sched->ops = ops;
 	sched->hw_submission_limit = hw_submission;
 	sched->name = name;
+	sched->run_wq = run_wq ? : system_wq;
 	sched->timeout = timeout;
 	sched->timeout_wq = timeout_wq ? : system_wq;
-	sched->run_wq = system_wq;	/* FIXME: Let user pass this in */
 	sched->hang_limit = hang_limit;
 	sched->score = score ? score : &sched->_score;
 	sched->dev = dev;
drivers/gpu/drm/v3d/v3d_sched.c +5 -5
index 06238e6d7f5c..38e092ea41e6 100644
@@ -388,7 +388,7 @@ v3d_sched_init(struct v3d_dev *v3d)
 	int ret;
 
 	ret = drm_sched_init(&v3d->queue[V3D_BIN].sched,
-			     &v3d_bin_sched_ops,
+			     &v3d_bin_sched_ops, NULL,
 			     hw_jobs_limit, job_hang_limit,
 			     msecs_to_jiffies(hang_limit_ms), NULL,
 			     NULL, "v3d_bin", v3d->drm.dev);
@@ -396,7 +396,7 @@ v3d_sched_init(struct v3d_dev *v3d)
 		return ret;
 
 	ret = drm_sched_init(&v3d->queue[V3D_RENDER].sched,
-			     &v3d_render_sched_ops,
+			     &v3d_render_sched_ops, NULL,
 			     hw_jobs_limit, job_hang_limit,
 			     msecs_to_jiffies(hang_limit_ms), NULL,
 			     NULL, "v3d_render", v3d->drm.dev);
@@ -404,7 +404,7 @@ v3d_sched_init(struct v3d_dev *v3d)
 		goto fail;
 
 	ret = drm_sched_init(&v3d->queue[V3D_TFU].sched,
-			     &v3d_tfu_sched_ops,
+			     &v3d_tfu_sched_ops, NULL,
 			     hw_jobs_limit, job_hang_limit,
 			     msecs_to_jiffies(hang_limit_ms), NULL,
 			     NULL, "v3d_tfu", v3d->drm.dev);
@@ -413,7 +413,7 @@ v3d_sched_init(struct v3d_dev *v3d)
 
 	if (v3d_has_csd(v3d)) {
 		ret = drm_sched_init(&v3d->queue[V3D_CSD].sched,
-				     &v3d_csd_sched_ops,
+				     &v3d_csd_sched_ops, NULL,
 				     hw_jobs_limit, job_hang_limit,
 				     msecs_to_jiffies(hang_limit_ms), NULL,
 				     NULL, "v3d_csd", v3d->drm.dev);
@@ -421,7 +421,7 @@ v3d_sched_init(struct v3d_dev *v3d)
 			goto fail;
 
 		ret = drm_sched_init(&v3d->queue[V3D_CACHE_CLEAN].sched,
-				     &v3d_cache_clean_sched_ops,
+				     &v3d_cache_clean_sched_ops, NULL,
 				     hw_jobs_limit, job_hang_limit,
 				     msecs_to_jiffies(hang_limit_ms), NULL,
 				     NULL, "v3d_cache_clean", v3d->drm.dev);
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index de4f0044b211..d6d60ebf3d5f 100644
@@ -336,7 +336,7 @@ static int execlist_engine_init(struct xe_engine *e)
 
 	exl->engine = e;
 
-	err = drm_sched_init(&exl->sched, &drm_sched_ops,
+	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
 			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
 			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
 			     NULL, NULL, e->hwe->name,
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
index e857013070b9..735f31257f3a 100644
@@ -1081,7 +1081,7 @@ static int guc_engine_init(struct xe_engine *e)
 	init_waitqueue_head(&ge->suspend_wait);
 
 	timeout = xe_vm_no_dma_fences(e->vm) ? MAX_SCHEDULE_TIMEOUT : HZ * 5;
-	err = drm_sched_init(&ge->sched, &drm_sched_ops,
+	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
 			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
 			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
 			     e->name, gt_to_xe(e->gt)->drm.dev);
include/drm/gpu_scheduler.h +1 -0
index cf85f93218fc..09bc39840dc8 100644
@@ -539,6 +539,7 @@ struct drm_gpu_scheduler {
 
 int drm_sched_init(struct drm_gpu_scheduler *sched,
 		   const struct drm_sched_backend_ops *ops,
+		   struct workqueue_struct *run_wq,
 		   uint32_t hw_submission, unsigned hang_limit,
 		   long timeout, struct workqueue_struct *timeout_wq,
 		   atomic_t *score, const char *name, struct device *dev);
Signature · 1 line
2.34.1
#2 lore raw

Re: [Intel-xe] [PATCH v2 01/31] drm/sched: Add run_wq argument to drm_sched_init

Hi,

Quoted 1 line · Matthew Brost

We will have this argument upstream, lets pull into the Xe repo.

Please rephrase the commit message. Add explanation, imperative wording and remove mentions of the Xe repo (nobody cares about that once it goes upstream)

/Thomas

Quoted 198 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  2 +-
  drivers/gpu/drm/etnaviv/etnaviv_sched.c    |  2 +-
  drivers/gpu/drm/lima/lima_sched.c          |  2 +-
  drivers/gpu/drm/msm/msm_ringbuffer.c       |  2 +-
  drivers/gpu/drm/panfrost/panfrost_job.c    |  2 +-
  drivers/gpu/drm/scheduler/sched_main.c     |  4 +++-
  drivers/gpu/drm/v3d/v3d_sched.c            | 10 +++++-----
  drivers/gpu/drm/xe/xe_execlist.c           |  2 +-
  drivers/gpu/drm/xe/xe_guc_submit.c         |  2 +-
  include/drm/gpu_scheduler.h                |  1 +
  10 files changed, 16 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -1
index 902f9b5ff82c..fe28f6b71fe3 100644
@@ -2364,7 +2364,7 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
  			break;
  		}
 
-		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
+		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops, NULL,
  				   ring->num_hw_submission, amdgpu_job_hang_limit,
  				   timeout, adev->reset_domain->wq,
  				   ring->sched_score, ring->name,
drivers/gpu/drm/etnaviv/etnaviv_sched.c +1 -1
index 1ae87dfd19c4..8486a2923f1b 100644
@@ -133,7 +133,7 @@ int etnaviv_sched_init(struct etnaviv_gpu *gpu)
  {
  	int ret;
 
-	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops,
+	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops, NULL,
  			     etnaviv_hw_jobs_limit, etnaviv_job_hang_limit,
  			     msecs_to_jiffies(500), NULL, NULL,
  			     dev_name(gpu->dev), gpu->dev);
drivers/gpu/drm/lima/lima_sched.c +1 -1
index ff003403fbbc..54f53bece27c 100644
@@ -488,7 +488,7 @@ int lima_sched_pipe_init(struct lima_sched_pipe *pipe, const char *name)
 
  	INIT_WORK(&pipe->recover_work, lima_sched_recover_work);
 
-	return drm_sched_init(&pipe->base, &lima_sched_ops, 1,
+	return drm_sched_init(&pipe->base, &lima_sched_ops, NULL, 1,
  			      lima_job_hang_limit,
  			      msecs_to_jiffies(timeout), NULL,
  			      NULL, name, pipe->ldev->dev);
drivers/gpu/drm/msm/msm_ringbuffer.c +1 -1
index 57a8e9564540..5879fc262047 100644
@@ -95,7 +95,7 @@ struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
  	 /* currently managing hangcheck ourselves: */
  	sched_timeout = MAX_SCHEDULE_TIMEOUT;
 
-	ret = drm_sched_init(&ring->sched, &msm_sched_ops,
+	ret = drm_sched_init(&ring->sched, &msm_sched_ops, NULL,
  			num_hw_submissions, 0, sched_timeout,
  			NULL, NULL, to_msm_bo(ring->bo)->name, gpu->dev->dev);
  	if (ret) {
drivers/gpu/drm/panfrost/panfrost_job.c +1 -1
index dbc597ab46fb..f48b07056a16 100644
@@ -815,7 +815,7 @@ int panfrost_job_init(struct panfrost_device *pfdev)
  		js->queue[j].fence_context = dma_fence_context_alloc(1);
 
  		ret = drm_sched_init(&js->queue[j].sched,
-				     &panfrost_sched_ops,
+				     &panfrost_sched_ops, NULL,
  				     nentries, 0,
  				     msecs_to_jiffies(JOB_TIMEOUT_MS),
  				     pfdev->reset.wq,
drivers/gpu/drm/scheduler/sched_main.c +3 -1
index cfd8a838e283..e79b9c760efe 100644
@@ -1182,6 +1182,7 @@ static void drm_sched_main(struct work_struct *w)
   *
   * @sched: scheduler instance
   * @ops: backend operations for this scheduler
+ * @run_wq: workqueue to use for run work. If NULL, the system_wq is used
   * @hw_submission: number of hw submissions that can be in flight
   * @hang_limit: number of times to allow a job to hang before dropping it
   * @timeout: timeout value in jiffies for the scheduler
@@ -1195,6 +1196,7 @@ static void drm_sched_main(struct work_struct *w)
   */
  int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   const struct drm_sched_backend_ops *ops,
+		   struct workqueue_struct *run_wq,
  		   unsigned hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
  		   atomic_t *score, const char *name, struct device *dev)
@@ -1203,9 +1205,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  	sched->ops = ops;
  	sched->hw_submission_limit = hw_submission;
  	sched->name = name;
+	sched->run_wq = run_wq ? : system_wq;
  	sched->timeout = timeout;
  	sched->timeout_wq = timeout_wq ? : system_wq;
-	sched->run_wq = system_wq;	/* FIXME: Let user pass this in */
  	sched->hang_limit = hang_limit;
  	sched->score = score ? score : &sched->_score;
  	sched->dev = dev;
drivers/gpu/drm/v3d/v3d_sched.c +5 -5
index 06238e6d7f5c..38e092ea41e6 100644
@@ -388,7 +388,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  	int ret;
 
  	ret = drm_sched_init(&v3d->queue[V3D_BIN].sched,
-			     &v3d_bin_sched_ops,
+			     &v3d_bin_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_bin", v3d->drm.dev);
@@ -396,7 +396,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  		return ret;
 
  	ret = drm_sched_init(&v3d->queue[V3D_RENDER].sched,
-			     &v3d_render_sched_ops,
+			     &v3d_render_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_render", v3d->drm.dev);
@@ -404,7 +404,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  		goto fail;
 
  	ret = drm_sched_init(&v3d->queue[V3D_TFU].sched,
-			     &v3d_tfu_sched_ops,
+			     &v3d_tfu_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_tfu", v3d->drm.dev);
@@ -413,7 +413,7 @@ v3d_sched_init(struct v3d_dev *v3d)
 
  	if (v3d_has_csd(v3d)) {
  		ret = drm_sched_init(&v3d->queue[V3D_CSD].sched,
-				     &v3d_csd_sched_ops,
+				     &v3d_csd_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
  				     NULL, "v3d_csd", v3d->drm.dev);
@@ -421,7 +421,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  			goto fail;
 
  		ret = drm_sched_init(&v3d->queue[V3D_CACHE_CLEAN].sched,
-				     &v3d_cache_clean_sched_ops,
+				     &v3d_cache_clean_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
  				     NULL, "v3d_cache_clean", v3d->drm.dev);
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index de4f0044b211..d6d60ebf3d5f 100644
@@ -336,7 +336,7 @@ static int execlist_engine_init(struct xe_engine *e)
 
  	exl->engine = e;
 
-	err = drm_sched_init(&exl->sched, &drm_sched_ops,
+	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
  			     NULL, NULL, e->hwe->name,
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
index e857013070b9..735f31257f3a 100644
@@ -1081,7 +1081,7 @@ static int guc_engine_init(struct xe_engine *e)
  	init_waitqueue_head(&ge->suspend_wait);
 
  	timeout = xe_vm_no_dma_fences(e->vm) ? MAX_SCHEDULE_TIMEOUT : HZ * 5;
-	err = drm_sched_init(&ge->sched, &drm_sched_ops,
+	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
  			     e->name, gt_to_xe(e->gt)->drm.dev);
include/drm/gpu_scheduler.h +1 -0
index cf85f93218fc..09bc39840dc8 100644
@@ -539,6 +539,7 @@ struct drm_gpu_scheduler {
 
  int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   const struct drm_sched_backend_ops *ops,
+		   struct workqueue_struct *run_wq,
  		   uint32_t hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
  		   atomic_t *score, const char *name, struct device *dev);
author maintainer
#3 lore raw

Re: [Intel-xe] [PATCH v2 01/31] drm/sched: Add run_wq argument to drm_sched_init

Quoted 10 lines · Thomas Hellström

Hi,

Quoted 1 line · Matthew Brost

We will have this argument upstream, lets pull into the Xe repo.

Please rephrase the commit message. Add explanation, imperative wording and remove mentions of the Xe repo (nobody cares about that once it goes upstream)

/Thomas

This patch gets squashed into one eariler in our repo into t upstream version:
https://patchwork.freedesktop.org/patch/530648/?series=116054&rev=1

Can reword if you think it is a big deal but this is just a placeholder.

Matt

Quoted 184 lines
Quoted 184 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  2 +-
  drivers/gpu/drm/etnaviv/etnaviv_sched.c    |  2 +-
  drivers/gpu/drm/lima/lima_sched.c          |  2 +-
  drivers/gpu/drm/msm/msm_ringbuffer.c       |  2 +-
  drivers/gpu/drm/panfrost/panfrost_job.c    |  2 +-
  drivers/gpu/drm/scheduler/sched_main.c     |  4 +++-
  drivers/gpu/drm/v3d/v3d_sched.c            | 10 +++++-----
  drivers/gpu/drm/xe/xe_execlist.c           |  2 +-
  drivers/gpu/drm/xe/xe_guc_submit.c         |  2 +-
  include/drm/gpu_scheduler.h                |  1 +
  10 files changed, 16 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -1
index 902f9b5ff82c..fe28f6b71fe3 100644
@@ -2364,7 +2364,7 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
  			break;
  		}
-		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
+		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops, NULL,
  				   ring->num_hw_submission, amdgpu_job_hang_limit,
  				   timeout, adev->reset_domain->wq,
  				   ring->sched_score, ring->name,
drivers/gpu/drm/etnaviv/etnaviv_sched.c +1 -1
index 1ae87dfd19c4..8486a2923f1b 100644
@@ -133,7 +133,7 @@ int etnaviv_sched_init(struct etnaviv_gpu *gpu)
  {
  	int ret;
-	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops,
+	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops, NULL,
  			     etnaviv_hw_jobs_limit, etnaviv_job_hang_limit,
  			     msecs_to_jiffies(500), NULL, NULL,
  			     dev_name(gpu->dev), gpu->dev);
drivers/gpu/drm/lima/lima_sched.c +1 -1
index ff003403fbbc..54f53bece27c 100644
@@ -488,7 +488,7 @@ int lima_sched_pipe_init(struct lima_sched_pipe *pipe, const char *name)
  	INIT_WORK(&pipe->recover_work, lima_sched_recover_work);
-	return drm_sched_init(&pipe->base, &lima_sched_ops, 1,
+	return drm_sched_init(&pipe->base, &lima_sched_ops, NULL, 1,
  			      lima_job_hang_limit,
  			      msecs_to_jiffies(timeout), NULL,
  			      NULL, name, pipe->ldev->dev);
drivers/gpu/drm/msm/msm_ringbuffer.c +1 -1
index 57a8e9564540..5879fc262047 100644
@@ -95,7 +95,7 @@ struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
  	 /* currently managing hangcheck ourselves: */
  	sched_timeout = MAX_SCHEDULE_TIMEOUT;
-	ret = drm_sched_init(&ring->sched, &msm_sched_ops,
+	ret = drm_sched_init(&ring->sched, &msm_sched_ops, NULL,
  			num_hw_submissions, 0, sched_timeout,
  			NULL, NULL, to_msm_bo(ring->bo)->name, gpu->dev->dev);
  	if (ret) {
drivers/gpu/drm/panfrost/panfrost_job.c +1 -1
index dbc597ab46fb..f48b07056a16 100644
@@ -815,7 +815,7 @@ int panfrost_job_init(struct panfrost_device *pfdev)
  		js->queue[j].fence_context = dma_fence_context_alloc(1);
  		ret = drm_sched_init(&js->queue[j].sched,
-				     &panfrost_sched_ops,
+				     &panfrost_sched_ops, NULL,
  				     nentries, 0,
  				     msecs_to_jiffies(JOB_TIMEOUT_MS),
  				     pfdev->reset.wq,
drivers/gpu/drm/scheduler/sched_main.c +3 -1
index cfd8a838e283..e79b9c760efe 100644
@@ -1182,6 +1182,7 @@ static void drm_sched_main(struct work_struct *w)
   *
   * @sched: scheduler instance
   * @ops: backend operations for this scheduler
+ * @run_wq: workqueue to use for run work. If NULL, the system_wq is used
   * @hw_submission: number of hw submissions that can be in flight
   * @hang_limit: number of times to allow a job to hang before dropping it
   * @timeout: timeout value in jiffies for the scheduler
@@ -1195,6 +1196,7 @@ static void drm_sched_main(struct work_struct *w)
   */
  int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   const struct drm_sched_backend_ops *ops,
+		   struct workqueue_struct *run_wq,
  		   unsigned hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
  		   atomic_t *score, const char *name, struct device *dev)
@@ -1203,9 +1205,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  	sched->ops = ops;
  	sched->hw_submission_limit = hw_submission;
  	sched->name = name;
+	sched->run_wq = run_wq ? : system_wq;
  	sched->timeout = timeout;
  	sched->timeout_wq = timeout_wq ? : system_wq;
-	sched->run_wq = system_wq;	/* FIXME: Let user pass this in */
  	sched->hang_limit = hang_limit;
  	sched->score = score ? score : &sched->_score;
  	sched->dev = dev;
drivers/gpu/drm/v3d/v3d_sched.c +5 -5
index 06238e6d7f5c..38e092ea41e6 100644
@@ -388,7 +388,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  	int ret;
  	ret = drm_sched_init(&v3d->queue[V3D_BIN].sched,
-			     &v3d_bin_sched_ops,
+			     &v3d_bin_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_bin", v3d->drm.dev);
@@ -396,7 +396,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  		return ret;
  	ret = drm_sched_init(&v3d->queue[V3D_RENDER].sched,
-			     &v3d_render_sched_ops,
+			     &v3d_render_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_render", v3d->drm.dev);
@@ -404,7 +404,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  		goto fail;
  	ret = drm_sched_init(&v3d->queue[V3D_TFU].sched,
-			     &v3d_tfu_sched_ops,
+			     &v3d_tfu_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_tfu", v3d->drm.dev);
@@ -413,7 +413,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  	if (v3d_has_csd(v3d)) {
  		ret = drm_sched_init(&v3d->queue[V3D_CSD].sched,
-				     &v3d_csd_sched_ops,
+				     &v3d_csd_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
  				     NULL, "v3d_csd", v3d->drm.dev);
@@ -421,7 +421,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  			goto fail;
  		ret = drm_sched_init(&v3d->queue[V3D_CACHE_CLEAN].sched,
-				     &v3d_cache_clean_sched_ops,
+				     &v3d_cache_clean_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
  				     NULL, "v3d_cache_clean", v3d->drm.dev);
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index de4f0044b211..d6d60ebf3d5f 100644
@@ -336,7 +336,7 @@ static int execlist_engine_init(struct xe_engine *e)
  	exl->engine = e;
-	err = drm_sched_init(&exl->sched, &drm_sched_ops,
+	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
  			     NULL, NULL, e->hwe->name,
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
index e857013070b9..735f31257f3a 100644
@@ -1081,7 +1081,7 @@ static int guc_engine_init(struct xe_engine *e)
  	init_waitqueue_head(&ge->suspend_wait);
  	timeout = xe_vm_no_dma_fences(e->vm) ? MAX_SCHEDULE_TIMEOUT : HZ * 5;
-	err = drm_sched_init(&ge->sched, &drm_sched_ops,
+	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
  			     e->name, gt_to_xe(e->gt)->drm.dev);
include/drm/gpu_scheduler.h +1 -0
index cf85f93218fc..09bc39840dc8 100644
@@ -539,6 +539,7 @@ struct drm_gpu_scheduler {
  int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   const struct drm_sched_backend_ops *ops,
+		   struct workqueue_struct *run_wq,
  		   uint32_t hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
  		   atomic_t *score, const char *name, struct device *dev);
#4 lore raw

Re: [Intel-xe] [PATCH v2 01/31] drm/sched: Add run_wq argument to drm_sched_init

Quoted 10 lines · Matthew Brost
Quoted 9 lines · Thomas Hellström

Hi,

Quoted 1 line · Matthew Brost

We will have this argument upstream, lets pull into the Xe repo.

Please rephrase the commit message. Add explanation, imperative wording and remove mentions of the Xe repo (nobody cares about that once it goes upstream)

Quoted 5 lines
Quoted 1 line

/Thomas

This patch gets squashed into one eariler in our repo into t upstream version:
https://patchwork.freedesktop.org/patch/530648/?series=116054&rev=1

if the patch is to be squashed in an existent one please create it with git commit --fixup=<hash of commit to be changed>

so we push this to the top of the branch and during the cleanup rebases that I'm running it will get automatically squashed...
(git rebase --autosquash)

without the fixup! annotation I don't have visibility to know which patches should be squashed.

Quoted 190 lines

Can reword if you think it is a big deal but this is just a placeholder.

Matt

Quoted 184 lines
Quoted 184 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  2 +-
  drivers/gpu/drm/etnaviv/etnaviv_sched.c    |  2 +-
  drivers/gpu/drm/lima/lima_sched.c          |  2 +-
  drivers/gpu/drm/msm/msm_ringbuffer.c       |  2 +-
  drivers/gpu/drm/panfrost/panfrost_job.c    |  2 +-
  drivers/gpu/drm/scheduler/sched_main.c     |  4 +++-
  drivers/gpu/drm/v3d/v3d_sched.c            | 10 +++++-----
  drivers/gpu/drm/xe/xe_execlist.c           |  2 +-
  drivers/gpu/drm/xe/xe_guc_submit.c         |  2 +-
  include/drm/gpu_scheduler.h                |  1 +
  10 files changed, 16 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -1
index 902f9b5ff82c..fe28f6b71fe3 100644
@@ -2364,7 +2364,7 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
  			break;
  		}
-		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
+		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops, NULL,
  				   ring->num_hw_submission, amdgpu_job_hang_limit,
  				   timeout, adev->reset_domain->wq,
  				   ring->sched_score, ring->name,
drivers/gpu/drm/etnaviv/etnaviv_sched.c +1 -1
index 1ae87dfd19c4..8486a2923f1b 100644
@@ -133,7 +133,7 @@ int etnaviv_sched_init(struct etnaviv_gpu *gpu)
  {
  	int ret;
-	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops,
+	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops, NULL,
  			     etnaviv_hw_jobs_limit, etnaviv_job_hang_limit,
  			     msecs_to_jiffies(500), NULL, NULL,
  			     dev_name(gpu->dev), gpu->dev);
drivers/gpu/drm/lima/lima_sched.c +1 -1
index ff003403fbbc..54f53bece27c 100644
@@ -488,7 +488,7 @@ int lima_sched_pipe_init(struct lima_sched_pipe *pipe, const char *name)
  	INIT_WORK(&pipe->recover_work, lima_sched_recover_work);
-	return drm_sched_init(&pipe->base, &lima_sched_ops, 1,
+	return drm_sched_init(&pipe->base, &lima_sched_ops, NULL, 1,
  			      lima_job_hang_limit,
  			      msecs_to_jiffies(timeout), NULL,
  			      NULL, name, pipe->ldev->dev);
drivers/gpu/drm/msm/msm_ringbuffer.c +1 -1
index 57a8e9564540..5879fc262047 100644
@@ -95,7 +95,7 @@ struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
  	 /* currently managing hangcheck ourselves: */
  	sched_timeout = MAX_SCHEDULE_TIMEOUT;
-	ret = drm_sched_init(&ring->sched, &msm_sched_ops,
+	ret = drm_sched_init(&ring->sched, &msm_sched_ops, NULL,
  			num_hw_submissions, 0, sched_timeout,
  			NULL, NULL, to_msm_bo(ring->bo)->name, gpu->dev->dev);
  	if (ret) {
drivers/gpu/drm/panfrost/panfrost_job.c +1 -1
index dbc597ab46fb..f48b07056a16 100644
@@ -815,7 +815,7 @@ int panfrost_job_init(struct panfrost_device *pfdev)
  		js->queue[j].fence_context = dma_fence_context_alloc(1);
  		ret = drm_sched_init(&js->queue[j].sched,
-				     &panfrost_sched_ops,
+				     &panfrost_sched_ops, NULL,
  				     nentries, 0,
  				     msecs_to_jiffies(JOB_TIMEOUT_MS),
  				     pfdev->reset.wq,
drivers/gpu/drm/scheduler/sched_main.c +3 -1
index cfd8a838e283..e79b9c760efe 100644
@@ -1182,6 +1182,7 @@ static void drm_sched_main(struct work_struct *w)
   *
   * @sched: scheduler instance
   * @ops: backend operations for this scheduler
+ * @run_wq: workqueue to use for run work. If NULL, the system_wq is used
   * @hw_submission: number of hw submissions that can be in flight
   * @hang_limit: number of times to allow a job to hang before dropping it
   * @timeout: timeout value in jiffies for the scheduler
@@ -1195,6 +1196,7 @@ static void drm_sched_main(struct work_struct *w)
   */
  int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   const struct drm_sched_backend_ops *ops,
+		   struct workqueue_struct *run_wq,
  		   unsigned hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
  		   atomic_t *score, const char *name, struct device *dev)
@@ -1203,9 +1205,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  	sched->ops = ops;
  	sched->hw_submission_limit = hw_submission;
  	sched->name = name;
+	sched->run_wq = run_wq ? : system_wq;
  	sched->timeout = timeout;
  	sched->timeout_wq = timeout_wq ? : system_wq;
-	sched->run_wq = system_wq;	/* FIXME: Let user pass this in */
  	sched->hang_limit = hang_limit;
  	sched->score = score ? score : &sched->_score;
  	sched->dev = dev;
drivers/gpu/drm/v3d/v3d_sched.c +5 -5
index 06238e6d7f5c..38e092ea41e6 100644
@@ -388,7 +388,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  	int ret;
  	ret = drm_sched_init(&v3d->queue[V3D_BIN].sched,
-			     &v3d_bin_sched_ops,
+			     &v3d_bin_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_bin", v3d->drm.dev);
@@ -396,7 +396,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  		return ret;
  	ret = drm_sched_init(&v3d->queue[V3D_RENDER].sched,
-			     &v3d_render_sched_ops,
+			     &v3d_render_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_render", v3d->drm.dev);
@@ -404,7 +404,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  		goto fail;
  	ret = drm_sched_init(&v3d->queue[V3D_TFU].sched,
-			     &v3d_tfu_sched_ops,
+			     &v3d_tfu_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
  			     NULL, "v3d_tfu", v3d->drm.dev);
@@ -413,7 +413,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  	if (v3d_has_csd(v3d)) {
  		ret = drm_sched_init(&v3d->queue[V3D_CSD].sched,
-				     &v3d_csd_sched_ops,
+				     &v3d_csd_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
  				     NULL, "v3d_csd", v3d->drm.dev);
@@ -421,7 +421,7 @@ v3d_sched_init(struct v3d_dev *v3d)
  			goto fail;
  		ret = drm_sched_init(&v3d->queue[V3D_CACHE_CLEAN].sched,
-				     &v3d_cache_clean_sched_ops,
+				     &v3d_cache_clean_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
  				     NULL, "v3d_cache_clean", v3d->drm.dev);
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index de4f0044b211..d6d60ebf3d5f 100644
@@ -336,7 +336,7 @@ static int execlist_engine_init(struct xe_engine *e)
  	exl->engine = e;
-	err = drm_sched_init(&exl->sched, &drm_sched_ops,
+	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
  			     NULL, NULL, e->hwe->name,
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
index e857013070b9..735f31257f3a 100644
@@ -1081,7 +1081,7 @@ static int guc_engine_init(struct xe_engine *e)
  	init_waitqueue_head(&ge->suspend_wait);
  	timeout = xe_vm_no_dma_fences(e->vm) ? MAX_SCHEDULE_TIMEOUT : HZ * 5;
-	err = drm_sched_init(&ge->sched, &drm_sched_ops,
+	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
  			     e->name, gt_to_xe(e->gt)->drm.dev);
include/drm/gpu_scheduler.h +1 -0
index cf85f93218fc..09bc39840dc8 100644
@@ -539,6 +539,7 @@ struct drm_gpu_scheduler {
  int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   const struct drm_sched_backend_ops *ops,
+		   struct workqueue_struct *run_wq,
  		   uint32_t hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
  		   atomic_t *score, const char *name, struct device *dev);
author maintainer
#5 lore raw

PATCH v2 2/31 drm/sched: Move schedule policy to scheduler

In reply to Matthew Brost

Rather than a global modparam for scheduling policy, move the scheduling policy to scheduler so user can control each scheduler policy.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  1 +
 drivers/gpu/drm/etnaviv/etnaviv_sched.c    |  3 ++-
 drivers/gpu/drm/lima/lima_sched.c          |  3 ++-
 drivers/gpu/drm/msm/msm_ringbuffer.c       |  3 ++-
 drivers/gpu/drm/panfrost/panfrost_job.c    |  3 ++-
 drivers/gpu/drm/scheduler/sched_entity.c   | 24 ++++++++++++++++++----
 drivers/gpu/drm/scheduler/sched_main.c     | 21 ++++++++++++++-----
 drivers/gpu/drm/v3d/v3d_sched.c            | 15 +++++++++-----
 drivers/gpu/drm/xe/xe_execlist.c           |  2 +-
 drivers/gpu/drm/xe/xe_guc_submit.c         |  3 ++-
 include/drm/gpu_scheduler.h                | 20 ++++++++++++------
 11 files changed, 72 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index fe28f6b71fe3..577ea5b98cd5 100644
@@ -2368,6 +2368,7 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
 				   ring->num_hw_submission, amdgpu_job_hang_limit,
 				   timeout, adev->reset_domain->wq,
 				   ring->sched_score, ring->name,
+				   DRM_SCHED_POLICY_DEFAULT,
 				   adev->dev);
 		if (r) {
 			DRM_ERROR("Failed to create scheduler on ring %s.\n",
drivers/gpu/drm/etnaviv/etnaviv_sched.c +2 -1
index 8486a2923f1b..61204a3f8b0b 100644
@@ -136,7 +136,8 @@ int etnaviv_sched_init(struct etnaviv_gpu *gpu)
 	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops, NULL,
 			     etnaviv_hw_jobs_limit, etnaviv_job_hang_limit,
 			     msecs_to_jiffies(500), NULL, NULL,
-			     dev_name(gpu->dev), gpu->dev);
+			     dev_name(gpu->dev), DRM_SCHED_POLICY_DEFAULT,
+			     gpu->dev);
 	if (ret)
 		return ret;
 
drivers/gpu/drm/lima/lima_sched.c +2 -1
index 54f53bece27c..33042ba6ae93 100644
@@ -491,7 +491,8 @@ int lima_sched_pipe_init(struct lima_sched_pipe *pipe, const char *name)
 	return drm_sched_init(&pipe->base, &lima_sched_ops, NULL, 1,
 			      lima_job_hang_limit,
 			      msecs_to_jiffies(timeout), NULL,
-			      NULL, name, pipe->ldev->dev);
+			      NULL, name, DRM_SCHED_POLICY_DEFAULT,
+			      pipe->ldev->dev);
 }
 
 void lima_sched_pipe_fini(struct lima_sched_pipe *pipe)
drivers/gpu/drm/msm/msm_ringbuffer.c +2 -1
index 5879fc262047..f408a9097315 100644
@@ -97,7 +97,8 @@ struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
 
 	ret = drm_sched_init(&ring->sched, &msm_sched_ops, NULL,
 			num_hw_submissions, 0, sched_timeout,
-			NULL, NULL, to_msm_bo(ring->bo)->name, gpu->dev->dev);
+			NULL, NULL, to_msm_bo(ring->bo)->name,
+			DRM_SCHED_POLICY_DEFAULT, gpu->dev->dev);
 	if (ret) {
 		goto fail;
 	}
drivers/gpu/drm/panfrost/panfrost_job.c +2 -1
index f48b07056a16..effa48b33dce 100644
@@ -819,7 +819,8 @@ int panfrost_job_init(struct panfrost_device *pfdev)
 				     nentries, 0,
 				     msecs_to_jiffies(JOB_TIMEOUT_MS),
 				     pfdev->reset.wq,
-				     NULL, "pan_js", pfdev->dev);
+				     NULL, "pan_js", DRM_SCHED_POLICY_DEFAULT,
+				     pfdev->dev);
 		if (ret) {
 			dev_err(pfdev->dev, "Failed to create scheduler: %d.", ret);
 			goto err_sched;
drivers/gpu/drm/scheduler/sched_entity.c +20 -4
index 15d04a0ec623..2300b2fc06ab 100644
@@ -33,6 +33,20 @@
 #define to_drm_sched_job(sched_job)		\
 		container_of((sched_job), struct drm_sched_job, queue_node)
 
+static bool bad_policies(struct drm_gpu_scheduler **sched_list,
+			 unsigned int num_sched_list)
+{
+	enum drm_sched_policy sched_policy = sched_list[0]->sched_policy;
+	unsigned int i;
+
+	/* All scdedule policies must match */
+	for (i = 1; i < num_sched_list; ++i)
+		if (sched_policy != sched_list[i]->sched_policy)
+			return true;
+
+	return false;
+}
+
 /**
  * drm_sched_entity_init - Init a context entity used by scheduler when
  * submit to HW ring.
@@ -62,7 +76,8 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 			  unsigned int num_sched_list,
 			  atomic_t *guilty)
 {
-	if (!(entity && sched_list && (num_sched_list == 0 || sched_list[0])))
+	if (!(entity && sched_list && (num_sched_list == 0 || sched_list[0])) ||
+	    bad_policies(sched_list, num_sched_list))
 		return -EINVAL;
 
 	memset(entity, 0, sizeof(struct drm_sched_entity));
@@ -75,8 +90,9 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 	entity->last_scheduled = NULL;
 	RB_CLEAR_NODE(&entity->rb_tree_node);
 
-	if(num_sched_list)
+	if(num_sched_list) {
 		entity->rq = &sched_list[0]->sched_rq[entity->priority];
+	}
 
 	init_completion(&entity->entity_idle);
 
@@ -440,7 +456,7 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
 	 * Update the entity's location in the min heap according to
 	 * the timestamp of the next job, if any.
 	 */
-	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO) {
+	if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO) {
 		struct drm_sched_job *next;
 
 		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
@@ -528,7 +544,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 		drm_sched_rq_add_entity(entity->rq, entity);
 		spin_unlock(&entity->rq_lock);
 
-		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
+		if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
 			drm_sched_rq_update_fifo(entity, sched_job->submit_ts);
 
 		drm_sched_wakeup(entity->rq->sched);
drivers/gpu/drm/scheduler/sched_main.c +16 -5
index e79b9c760efe..6777a2db554f 100644
@@ -62,14 +62,14 @@
 #define to_drm_sched_job(sched_job)		\
 		container_of((sched_job), struct drm_sched_job, queue_node)
 
-int drm_sched_policy = DRM_SCHED_POLICY_FIFO;
+int default_drm_sched_policy = DRM_SCHED_POLICY_FIFO;
 
 /**
  * DOC: sched_policy (int)
  * Used to override default entities scheduling policy in a run queue.
  */
 MODULE_PARM_DESC(sched_policy, "Specify the scheduling policy for entities on a run-queue, " __stringify(DRM_SCHED_POLICY_RR) " = Round Robin, " __stringify(DRM_SCHED_POLICY_FIFO) " = FIFO (default).");
-module_param_named(sched_policy, drm_sched_policy, int, 0444);
+module_param_named(sched_policy, default_drm_sched_policy, int, 0444);
 
 static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a,
 							    const struct rb_node *b)
@@ -173,7 +173,7 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
 	if (rq->current_entity == entity)
 		rq->current_entity = NULL;
 
-	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
+	if (rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
 		drm_sched_rq_remove_fifo_locked(entity);
 
 	spin_unlock(&rq->lock);
@@ -956,7 +956,7 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
 
 	/* Kernel run queue has higher priority than normal run queue*/
 	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
-		entity = drm_sched_policy == DRM_SCHED_POLICY_FIFO ?
+		entity = sched->sched_policy == DRM_SCHED_POLICY_FIFO ?
 			drm_sched_rq_select_entity_fifo(&sched->sched_rq[i]) :
 			drm_sched_rq_select_entity_rr(&sched->sched_rq[i]);
 		if (entity)
@@ -1190,6 +1190,7 @@ static void drm_sched_main(struct work_struct *w)
  *		used
  * @score: optional score atomic shared with other schedulers
  * @name: name used for debugging
+ * @sched_policy: schedule policy
  * @dev: target &struct device
  *
  * Return 0 on success, otherwise error code.
@@ -1199,9 +1200,15 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
 		   struct workqueue_struct *run_wq,
 		   unsigned hw_submission, unsigned hang_limit,
 		   long timeout, struct workqueue_struct *timeout_wq,
-		   atomic_t *score, const char *name, struct device *dev)
+		   atomic_t *score, const char *name,
+		   enum drm_sched_policy sched_policy,
+		   struct device *dev)
 {
 	int i;
+
+	if (sched_policy >= DRM_SCHED_POLICY_COUNT)
+		return -EINVAL;
+
 	sched->ops = ops;
 	sched->hw_submission_limit = hw_submission;
 	sched->name = name;
@@ -1211,6 +1218,10 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
 	sched->hang_limit = hang_limit;
 	sched->score = score ? score : &sched->_score;
 	sched->dev = dev;
+	if (sched_policy == DRM_SCHED_POLICY_DEFAULT)
+		sched->sched_policy = default_drm_sched_policy;
+	else
+		sched->sched_policy = sched_policy;
 	for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_COUNT; i++)
 		drm_sched_rq_init(sched, &sched->sched_rq[i]);
 
drivers/gpu/drm/v3d/v3d_sched.c +10 -5
index 38e092ea41e6..5e3fe77fa991 100644
@@ -391,7 +391,8 @@ v3d_sched_init(struct v3d_dev *v3d)
 			     &v3d_bin_sched_ops, NULL,
 			     hw_jobs_limit, job_hang_limit,
 			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_bin", v3d->drm.dev);
+			     NULL, "v3d_bin", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
 	if (ret)
 		return ret;
 
@@ -399,7 +400,8 @@ v3d_sched_init(struct v3d_dev *v3d)
 			     &v3d_render_sched_ops, NULL,
 			     hw_jobs_limit, job_hang_limit,
 			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_render", v3d->drm.dev);
+			     ULL, "v3d_render", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
 	if (ret)
 		goto fail;
 
@@ -407,7 +409,8 @@ v3d_sched_init(struct v3d_dev *v3d)
 			     &v3d_tfu_sched_ops, NULL,
 			     hw_jobs_limit, job_hang_limit,
 			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_tfu", v3d->drm.dev);
+			     NULL, "v3d_tfu", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
 	if (ret)
 		goto fail;
 
@@ -416,7 +419,8 @@ v3d_sched_init(struct v3d_dev *v3d)
 				     &v3d_csd_sched_ops, NULL,
 				     hw_jobs_limit, job_hang_limit,
 				     msecs_to_jiffies(hang_limit_ms), NULL,
-				     NULL, "v3d_csd", v3d->drm.dev);
+				     NULL, "v3d_csd", DRM_SCHED_POLICY_DEFAULT,
+				     v3d->drm.dev);
 		if (ret)
 			goto fail;
 
@@ -424,7 +428,8 @@ v3d_sched_init(struct v3d_dev *v3d)
 				     &v3d_cache_clean_sched_ops, NULL,
 				     hw_jobs_limit, job_hang_limit,
 				     msecs_to_jiffies(hang_limit_ms), NULL,
-				     NULL, "v3d_cache_clean", v3d->drm.dev);
+				     NULL, "v3d_cache_clean",
+				     DRM_SCHED_POLICY_DEFAULT, v3d->drm.dev);
 		if (ret)
 			goto fail;
 	}
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index d6d60ebf3d5f..48060d14547a 100644
@@ -339,7 +339,7 @@ static int execlist_engine_init(struct xe_engine *e)
 	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
 			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
 			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
-			     NULL, NULL, e->hwe->name,
+			     NULL, NULL, e->hwe->name, DRM_SCHED_POLICY_DEFAULT,
 			     gt_to_xe(e->gt)->drm.dev);
 	if (err)
 		goto err_free;
drivers/gpu/drm/xe/xe_guc_submit.c +2 -1
index 735f31257f3a..9d3fadca43be 100644
@@ -1084,7 +1084,8 @@ static int guc_engine_init(struct xe_engine *e)
 	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
 			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
 			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
-			     e->name, gt_to_xe(e->gt)->drm.dev);
+			     e->name, DRM_SCHED_POLICY_DEFAULT,
+			     gt_to_xe(e->gt)->drm.dev);
 	if (err)
 		goto err_free;
 
include/drm/gpu_scheduler.h +14 -6
index 09bc39840dc8..3df801401028 100644
@@ -63,11 +63,15 @@ enum drm_sched_priority {
 	DRM_SCHED_PRIORITY_UNSET = -2
 };
 
-/* Used to chose between FIFO and RR jobs scheduling */
-extern int drm_sched_policy;
-
-#define DRM_SCHED_POLICY_RR    0
-#define DRM_SCHED_POLICY_FIFO  1
+/* Used to chose default scheduling policy*/
+extern int default_drm_sched_policy;
+
+enum drm_sched_policy {
+	DRM_SCHED_POLICY_DEFAULT,
+	DRM_SCHED_POLICY_RR,
+	DRM_SCHED_POLICY_FIFO,
+	DRM_SCHED_POLICY_COUNT,
+};
 
 /**
  * struct drm_sched_entity - A wrapper around a job queue (typically
@@ -505,6 +509,7 @@ struct drm_sched_backend_ops {
  *              guilty and it will no longer be considered for scheduling.
  * @score: score to help loadbalancer pick a idle sched
  * @_score: score used when the driver doesn't provide one
+ * @sched_policy: Schedule policy for scheduler
  * @ready: marks if the underlying HW is ready to work
  * @free_guilty: A hit to time out handler to free the guilty job.
  * @pause_run_wq: pause queuing of @work_run on @run_wq
@@ -531,6 +536,7 @@ struct drm_gpu_scheduler {
 	int				hang_limit;
 	atomic_t                        *score;
 	atomic_t                        _score;
+	enum drm_sched_policy		sched_policy;
 	bool				ready;
 	bool				free_guilty;
 	bool				pause_run_wq;
@@ -542,7 +548,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
 		   struct workqueue_struct *run_wq,
 		   uint32_t hw_submission, unsigned hang_limit,
 		   long timeout, struct workqueue_struct *timeout_wq,
-		   atomic_t *score, const char *name, struct device *dev);
+		   atomic_t *score, const char *name,
+		   enum drm_sched_policy sched_policy,
+		   struct device *dev);
 
 void drm_sched_fini(struct drm_gpu_scheduler *sched);
 int drm_sched_job_init(struct drm_sched_job *job,
Signature · 1 line
2.34.1
#6 lore raw

Re: [Intel-xe] [PATCH v2 02/31] drm/sched: Move schedule policy to scheduler

Quoted 2 lines · Matthew Brost

Rather than a global modparam for scheduling policy, move the scheduling policy to scheduler so user can control each scheduler policy.

Could you add some more info here, about why this is done and how the scheduler policy is supposed to be controlled? Should it say "driver can control" rather than "user can control" at this stage+

Quoted 89 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  1 +
  drivers/gpu/drm/etnaviv/etnaviv_sched.c    |  3 ++-
  drivers/gpu/drm/lima/lima_sched.c          |  3 ++-
  drivers/gpu/drm/msm/msm_ringbuffer.c       |  3 ++-
  drivers/gpu/drm/panfrost/panfrost_job.c    |  3 ++-
  drivers/gpu/drm/scheduler/sched_entity.c   | 24 ++++++++++++++++++----
  drivers/gpu/drm/scheduler/sched_main.c     | 21 ++++++++++++++-----
  drivers/gpu/drm/v3d/v3d_sched.c            | 15 +++++++++-----
  drivers/gpu/drm/xe/xe_execlist.c           |  2 +-
  drivers/gpu/drm/xe/xe_guc_submit.c         |  3 ++-
  include/drm/gpu_scheduler.h                | 20 ++++++++++++------
  11 files changed, 72 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index fe28f6b71fe3..577ea5b98cd5 100644
@@ -2368,6 +2368,7 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
  				   ring->num_hw_submission, amdgpu_job_hang_limit,
  				   timeout, adev->reset_domain->wq,
  				   ring->sched_score, ring->name,
+				   DRM_SCHED_POLICY_DEFAULT,
  				   adev->dev);
  		if (r) {
  			DRM_ERROR("Failed to create scheduler on ring %s.\n",
drivers/gpu/drm/etnaviv/etnaviv_sched.c +2 -1
index 8486a2923f1b..61204a3f8b0b 100644
@@ -136,7 +136,8 @@ int etnaviv_sched_init(struct etnaviv_gpu *gpu)
  	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops, NULL,
  			     etnaviv_hw_jobs_limit, etnaviv_job_hang_limit,
  			     msecs_to_jiffies(500), NULL, NULL,
-			     dev_name(gpu->dev), gpu->dev);
+			     dev_name(gpu->dev), DRM_SCHED_POLICY_DEFAULT,
+			     gpu->dev);
  	if (ret)
  		return ret;
 
drivers/gpu/drm/lima/lima_sched.c +2 -1
index 54f53bece27c..33042ba6ae93 100644
@@ -491,7 +491,8 @@ int lima_sched_pipe_init(struct lima_sched_pipe *pipe, const char *name)
  	return drm_sched_init(&pipe->base, &lima_sched_ops, NULL, 1,
  			      lima_job_hang_limit,
  			      msecs_to_jiffies(timeout), NULL,
-			      NULL, name, pipe->ldev->dev);
+			      NULL, name, DRM_SCHED_POLICY_DEFAULT,
+			      pipe->ldev->dev);
  }
 
  void lima_sched_pipe_fini(struct lima_sched_pipe *pipe)
drivers/gpu/drm/msm/msm_ringbuffer.c +2 -1
index 5879fc262047..f408a9097315 100644
@@ -97,7 +97,8 @@ struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
 
  	ret = drm_sched_init(&ring->sched, &msm_sched_ops, NULL,
  			num_hw_submissions, 0, sched_timeout,
-			NULL, NULL, to_msm_bo(ring->bo)->name, gpu->dev->dev);
+			NULL, NULL, to_msm_bo(ring->bo)->name,
+			DRM_SCHED_POLICY_DEFAULT, gpu->dev->dev);
  	if (ret) {
  		goto fail;
  	}
drivers/gpu/drm/panfrost/panfrost_job.c +2 -1
index f48b07056a16..effa48b33dce 100644
@@ -819,7 +819,8 @@ int panfrost_job_init(struct panfrost_device *pfdev)
  				     nentries, 0,
  				     msecs_to_jiffies(JOB_TIMEOUT_MS),
  				     pfdev->reset.wq,
-				     NULL, "pan_js", pfdev->dev);
+				     NULL, "pan_js", DRM_SCHED_POLICY_DEFAULT,
+				     pfdev->dev);
  		if (ret) {
  			dev_err(pfdev->dev, "Failed to create scheduler: %d.", ret);
  			goto err_sched;
drivers/gpu/drm/scheduler/sched_entity.c +0 -0
index 15d04a0ec623..2300b2fc06ab 100644

drivers/gpu/drm/scheduler/sched_entity.c line 42

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_entity.c +7 -0
@@ -33,6 +33,20 @@
  #define to_drm_sched_job(sched_job)		\
  		container_of((sched_job), struct drm_sched_job, queue_node)
 
+static bool bad_policies(struct drm_gpu_scheduler **sched_list,
+			 unsigned int num_sched_list)
+{
+	enum drm_sched_policy sched_policy = sched_list[0]->sched_policy;
+	unsigned int i;
+
+	/* All scdedule policies must match */

s/scdedule/schedule/

Quoted 21 lines
drivers/gpu/drm/scheduler/sched_entity.c +9 -1
+	for (i = 1; i < num_sched_list; ++i)
+		if (sched_policy != sched_list[i]->sched_policy)
+			return true;
+
+	return false;
+}
+
  /**
   * drm_sched_entity_init - Init a context entity used by scheduler when
   * submit to HW ring.
@@ -62,7 +76,8 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  			  unsigned int num_sched_list,
  			  atomic_t *guilty)
  {
-	if (!(entity && sched_list && (num_sched_list == 0 || sched_list[0])))
+	if (!(entity && sched_list && (num_sched_list == 0 || sched_list[0])) ||
+	    bad_policies(sched_list, num_sched_list))
  		return -EINVAL;
 
  	memset(entity, 0, sizeof(struct drm_sched_entity));

drivers/gpu/drm/scheduler/sched_entity.c int drm_sched_entity_init(struct drm_sched_entity *entity, · line 95

Quoted 7 lines
drivers/gpu/drm/scheduler/sched_entity.c +2 -1
@@ -75,8 +90,9 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  	entity->last_scheduled = NULL;
  	RB_CLEAR_NODE(&entity->rb_tree_node);
 
-	if(num_sched_list)
+	if(num_sched_list) {
  		entity->rq = &sched_list[0]->sched_rq[entity->priority];
+	}

Why are brackets added here?

Quoted 229 lines
drivers/gpu/drm/scheduler/sched_entity.c +2 -2
  	init_completion(&entity->entity_idle);
 
@@ -440,7 +456,7 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
  	 * Update the entity's location in the min heap according to
  	 * the timestamp of the next job, if any.
  	 */
-	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO) {
+	if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO) {
  		struct drm_sched_job *next;
 
  		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
@@ -528,7 +544,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  		drm_sched_rq_add_entity(entity->rq, entity);
  		spin_unlock(&entity->rq_lock);
 
-		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
+		if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
  			drm_sched_rq_update_fifo(entity, sched_job->submit_ts);
 
  		drm_sched_wakeup(entity->rq->sched);
drivers/gpu/drm/scheduler/sched_main.c +16 -5
index e79b9c760efe..6777a2db554f 100644
@@ -62,14 +62,14 @@
  #define to_drm_sched_job(sched_job)		\
  		container_of((sched_job), struct drm_sched_job, queue_node)
 
-int drm_sched_policy = DRM_SCHED_POLICY_FIFO;
+int default_drm_sched_policy = DRM_SCHED_POLICY_FIFO;
 
  /**
   * DOC: sched_policy (int)
   * Used to override default entities scheduling policy in a run queue.
   */
  MODULE_PARM_DESC(sched_policy, "Specify the scheduling policy for entities on a run-queue, " __stringify(DRM_SCHED_POLICY_RR) " = Round Robin, " __stringify(DRM_SCHED_POLICY_FIFO) " = FIFO (default).");
-module_param_named(sched_policy, drm_sched_policy, int, 0444);
+module_param_named(sched_policy, default_drm_sched_policy, int, 0444);
 
  static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a,
  							    const struct rb_node *b)
@@ -173,7 +173,7 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  	if (rq->current_entity == entity)
  		rq->current_entity = NULL;
 
-	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
+	if (rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
  		drm_sched_rq_remove_fifo_locked(entity);
 
  	spin_unlock(&rq->lock);
@@ -956,7 +956,7 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
 
  	/* Kernel run queue has higher priority than normal run queue*/
  	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
-		entity = drm_sched_policy == DRM_SCHED_POLICY_FIFO ?
+		entity = sched->sched_policy == DRM_SCHED_POLICY_FIFO ?
  			drm_sched_rq_select_entity_fifo(&sched->sched_rq[i]) :
  			drm_sched_rq_select_entity_rr(&sched->sched_rq[i]);
  		if (entity)
@@ -1190,6 +1190,7 @@ static void drm_sched_main(struct work_struct *w)
   *		used
   * @score: optional score atomic shared with other schedulers
   * @name: name used for debugging
+ * @sched_policy: schedule policy
   * @dev: target &struct device
   *
   * Return 0 on success, otherwise error code.
@@ -1199,9 +1200,15 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   struct workqueue_struct *run_wq,
  		   unsigned hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
-		   atomic_t *score, const char *name, struct device *dev)
+		   atomic_t *score, const char *name,
+		   enum drm_sched_policy sched_policy,
+		   struct device *dev)
  {
  	int i;
+
+	if (sched_policy >= DRM_SCHED_POLICY_COUNT)
+		return -EINVAL;
+
  	sched->ops = ops;
  	sched->hw_submission_limit = hw_submission;
  	sched->name = name;
@@ -1211,6 +1218,10 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  	sched->hang_limit = hang_limit;
  	sched->score = score ? score : &sched->_score;
  	sched->dev = dev;
+	if (sched_policy == DRM_SCHED_POLICY_DEFAULT)
+		sched->sched_policy = default_drm_sched_policy;
+	else
+		sched->sched_policy = sched_policy;
  	for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_COUNT; i++)
  		drm_sched_rq_init(sched, &sched->sched_rq[i]);
 
drivers/gpu/drm/v3d/v3d_sched.c +10 -5
index 38e092ea41e6..5e3fe77fa991 100644
@@ -391,7 +391,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  			     &v3d_bin_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_bin", v3d->drm.dev);
+			     NULL, "v3d_bin", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
  	if (ret)
  		return ret;
 
@@ -399,7 +400,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  			     &v3d_render_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_render", v3d->drm.dev);
+			     ULL, "v3d_render", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
  	if (ret)
  		goto fail;
 
@@ -407,7 +409,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  			     &v3d_tfu_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_tfu", v3d->drm.dev);
+			     NULL, "v3d_tfu", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
  	if (ret)
  		goto fail;
 
@@ -416,7 +419,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  				     &v3d_csd_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
-				     NULL, "v3d_csd", v3d->drm.dev);
+				     NULL, "v3d_csd", DRM_SCHED_POLICY_DEFAULT,
+				     v3d->drm.dev);
  		if (ret)
  			goto fail;
 
@@ -424,7 +428,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  				     &v3d_cache_clean_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
-				     NULL, "v3d_cache_clean", v3d->drm.dev);
+				     NULL, "v3d_cache_clean",
+				     DRM_SCHED_POLICY_DEFAULT, v3d->drm.dev);
  		if (ret)
  			goto fail;
  	}
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index d6d60ebf3d5f..48060d14547a 100644
@@ -339,7 +339,7 @@ static int execlist_engine_init(struct xe_engine *e)
  	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
-			     NULL, NULL, e->hwe->name,
+			     NULL, NULL, e->hwe->name, DRM_SCHED_POLICY_DEFAULT,
  			     gt_to_xe(e->gt)->drm.dev);
  	if (err)
  		goto err_free;
drivers/gpu/drm/xe/xe_guc_submit.c +2 -1
index 735f31257f3a..9d3fadca43be 100644
@@ -1084,7 +1084,8 @@ static int guc_engine_init(struct xe_engine *e)
  	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
-			     e->name, gt_to_xe(e->gt)->drm.dev);
+			     e->name, DRM_SCHED_POLICY_DEFAULT,
+			     gt_to_xe(e->gt)->drm.dev);
  	if (err)
  		goto err_free;
 
include/drm/gpu_scheduler.h +14 -6
index 09bc39840dc8..3df801401028 100644
@@ -63,11 +63,15 @@ enum drm_sched_priority {
  	DRM_SCHED_PRIORITY_UNSET = -2
  };
 
-/* Used to chose between FIFO and RR jobs scheduling */
-extern int drm_sched_policy;
-
-#define DRM_SCHED_POLICY_RR    0
-#define DRM_SCHED_POLICY_FIFO  1
+/* Used to chose default scheduling policy*/
+extern int default_drm_sched_policy;
+
+enum drm_sched_policy {
+	DRM_SCHED_POLICY_DEFAULT,
+	DRM_SCHED_POLICY_RR,
+	DRM_SCHED_POLICY_FIFO,
+	DRM_SCHED_POLICY_COUNT,
+};
 
  /**
   * struct drm_sched_entity - A wrapper around a job queue (typically
@@ -505,6 +509,7 @@ struct drm_sched_backend_ops {
   *              guilty and it will no longer be considered for scheduling.
   * @score: score to help loadbalancer pick a idle sched
   * @_score: score used when the driver doesn't provide one
+ * @sched_policy: Schedule policy for scheduler
   * @ready: marks if the underlying HW is ready to work
   * @free_guilty: A hit to time out handler to free the guilty job.
   * @pause_run_wq: pause queuing of @work_run on @run_wq
@@ -531,6 +536,7 @@ struct drm_gpu_scheduler {
  	int				hang_limit;
  	atomic_t                        *score;
  	atomic_t                        _score;
+	enum drm_sched_policy		sched_policy;
  	bool				ready;
  	bool				free_guilty;
  	bool				pause_run_wq;
@@ -542,7 +548,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   struct workqueue_struct *run_wq,
  		   uint32_t hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
-		   atomic_t *score, const char *name, struct device *dev);
+		   atomic_t *score, const char *name,
+		   enum drm_sched_policy sched_policy,
+		   struct device *dev);
 
  void drm_sched_fini(struct drm_gpu_scheduler *sched);
  int drm_sched_job_init(struct drm_sched_job *job,
author maintainer
#7 lore raw

Re: [Intel-xe] [PATCH v2 02/31] drm/sched: Move schedule policy to scheduler

Quoted 9 lines · Thomas Hellström
Quoted 2 lines · Matthew Brost

Rather than a global modparam for scheduling policy, move the scheduling policy to scheduler so user can control each scheduler policy.

Could you add some more info here, about why this is done and how the scheduler policy is supposed to be controlled? Should it say "driver can control" rather than "user can control" at this stage+

Rather than a global modparam for scheduling policy, move the scheduling policy to scheduler so driver can control each scheduler policy. This is required as it is possible in certain drivers certain scheduling polices are not allow, thus the driver must set the policy rather than the modparam.

^^^ How about that?

Again this is a placeholder patch as it should land upstream first.

Matt

Quoted 333 lines
Quoted 87 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  1 +
  drivers/gpu/drm/etnaviv/etnaviv_sched.c    |  3 ++-
  drivers/gpu/drm/lima/lima_sched.c          |  3 ++-
  drivers/gpu/drm/msm/msm_ringbuffer.c       |  3 ++-
  drivers/gpu/drm/panfrost/panfrost_job.c    |  3 ++-
  drivers/gpu/drm/scheduler/sched_entity.c   | 24 ++++++++++++++++++----
  drivers/gpu/drm/scheduler/sched_main.c     | 21 ++++++++++++++-----
  drivers/gpu/drm/v3d/v3d_sched.c            | 15 +++++++++-----
  drivers/gpu/drm/xe/xe_execlist.c           |  2 +-
  drivers/gpu/drm/xe/xe_guc_submit.c         |  3 ++-
  include/drm/gpu_scheduler.h                | 20 ++++++++++++------
  11 files changed, 72 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
index fe28f6b71fe3..577ea5b98cd5 100644
@@ -2368,6 +2368,7 @@ static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
  				   ring->num_hw_submission, amdgpu_job_hang_limit,
  				   timeout, adev->reset_domain->wq,
  				   ring->sched_score, ring->name,
+				   DRM_SCHED_POLICY_DEFAULT,
  				   adev->dev);
  		if (r) {
  			DRM_ERROR("Failed to create scheduler on ring %s.\n",
drivers/gpu/drm/etnaviv/etnaviv_sched.c +2 -1
index 8486a2923f1b..61204a3f8b0b 100644
@@ -136,7 +136,8 @@ int etnaviv_sched_init(struct etnaviv_gpu *gpu)
  	ret = drm_sched_init(&gpu->sched, &etnaviv_sched_ops, NULL,
  			     etnaviv_hw_jobs_limit, etnaviv_job_hang_limit,
  			     msecs_to_jiffies(500), NULL, NULL,
-			     dev_name(gpu->dev), gpu->dev);
+			     dev_name(gpu->dev), DRM_SCHED_POLICY_DEFAULT,
+			     gpu->dev);
  	if (ret)
  		return ret;
drivers/gpu/drm/lima/lima_sched.c +2 -1
index 54f53bece27c..33042ba6ae93 100644
@@ -491,7 +491,8 @@ int lima_sched_pipe_init(struct lima_sched_pipe *pipe, const char *name)
  	return drm_sched_init(&pipe->base, &lima_sched_ops, NULL, 1,
  			      lima_job_hang_limit,
  			      msecs_to_jiffies(timeout), NULL,
-			      NULL, name, pipe->ldev->dev);
+			      NULL, name, DRM_SCHED_POLICY_DEFAULT,
+			      pipe->ldev->dev);
  }
  void lima_sched_pipe_fini(struct lima_sched_pipe *pipe)
drivers/gpu/drm/msm/msm_ringbuffer.c +2 -1
index 5879fc262047..f408a9097315 100644
@@ -97,7 +97,8 @@ struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
  	ret = drm_sched_init(&ring->sched, &msm_sched_ops, NULL,
  			num_hw_submissions, 0, sched_timeout,
-			NULL, NULL, to_msm_bo(ring->bo)->name, gpu->dev->dev);
+			NULL, NULL, to_msm_bo(ring->bo)->name,
+			DRM_SCHED_POLICY_DEFAULT, gpu->dev->dev);
  	if (ret) {
  		goto fail;
  	}
drivers/gpu/drm/panfrost/panfrost_job.c +2 -1
index f48b07056a16..effa48b33dce 100644
@@ -819,7 +819,8 @@ int panfrost_job_init(struct panfrost_device *pfdev)
  				     nentries, 0,
  				     msecs_to_jiffies(JOB_TIMEOUT_MS),
  				     pfdev->reset.wq,
-				     NULL, "pan_js", pfdev->dev);
+				     NULL, "pan_js", DRM_SCHED_POLICY_DEFAULT,
+				     pfdev->dev);
  		if (ret) {
  			dev_err(pfdev->dev, "Failed to create scheduler: %d.", ret);
  			goto err_sched;
drivers/gpu/drm/scheduler/sched_entity.c +0 -0
index 15d04a0ec623..2300b2fc06ab 100644

drivers/gpu/drm/scheduler/sched_entity.c line 41

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_entity.c +7 -0
@@ -33,6 +33,20 @@
  #define to_drm_sched_job(sched_job)		\
  		container_of((sched_job), struct drm_sched_job, queue_node)
+static bool bad_policies(struct drm_gpu_scheduler **sched_list,
+			 unsigned int num_sched_list)
+{
+	enum drm_sched_policy sched_policy = sched_list[0]->sched_policy;
+	unsigned int i;
+
+	/* All scdedule policies must match */

s/scdedule/schedule/

Quoted 20 lines
drivers/gpu/drm/scheduler/sched_entity.c +9 -1
+	for (i = 1; i < num_sched_list; ++i)
+		if (sched_policy != sched_list[i]->sched_policy)
+			return true;
+
+	return false;
+}
+
  /**
   * drm_sched_entity_init - Init a context entity used by scheduler when
   * submit to HW ring.
@@ -62,7 +76,8 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  			  unsigned int num_sched_list,
  			  atomic_t *guilty)
  {
-	if (!(entity && sched_list && (num_sched_list == 0 || sched_list[0])))
+	if (!(entity && sched_list && (num_sched_list == 0 || sched_list[0])) ||
+	    bad_policies(sched_list, num_sched_list))
  		return -EINVAL;
  	memset(entity, 0, sizeof(struct drm_sched_entity));

drivers/gpu/drm/scheduler/sched_entity.c int drm_sched_entity_init(struct drm_sched_entity *entity, · line 94

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_entity.c +2 -1
@@ -75,8 +90,9 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  	entity->last_scheduled = NULL;
  	RB_CLEAR_NODE(&entity->rb_tree_node);
-	if(num_sched_list)
+	if(num_sched_list) {
  		entity->rq = &sched_list[0]->sched_rq[entity->priority];
+	}

Why are brackets added here?

Quoted 209 lines
drivers/gpu/drm/scheduler/sched_entity.c +2 -2
  	init_completion(&entity->entity_idle);
@@ -440,7 +456,7 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
  	 * Update the entity's location in the min heap according to
  	 * the timestamp of the next job, if any.
  	 */
-	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO) {
+	if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO) {
  		struct drm_sched_job *next;
  		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
@@ -528,7 +544,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  		drm_sched_rq_add_entity(entity->rq, entity);
  		spin_unlock(&entity->rq_lock);
-		if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
+		if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
  			drm_sched_rq_update_fifo(entity, sched_job->submit_ts);
  		drm_sched_wakeup(entity->rq->sched);
drivers/gpu/drm/scheduler/sched_main.c +16 -5
index e79b9c760efe..6777a2db554f 100644
@@ -62,14 +62,14 @@
  #define to_drm_sched_job(sched_job)		\
  		container_of((sched_job), struct drm_sched_job, queue_node)
-int drm_sched_policy = DRM_SCHED_POLICY_FIFO;
+int default_drm_sched_policy = DRM_SCHED_POLICY_FIFO;
  /**
   * DOC: sched_policy (int)
   * Used to override default entities scheduling policy in a run queue.
   */
  MODULE_PARM_DESC(sched_policy, "Specify the scheduling policy for entities on a run-queue, " __stringify(DRM_SCHED_POLICY_RR) " = Round Robin, " __stringify(DRM_SCHED_POLICY_FIFO) " = FIFO (default).");
-module_param_named(sched_policy, drm_sched_policy, int, 0444);
+module_param_named(sched_policy, default_drm_sched_policy, int, 0444);
  static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a,
  							    const struct rb_node *b)
@@ -173,7 +173,7 @@ void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  	if (rq->current_entity == entity)
  		rq->current_entity = NULL;
-	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
+	if (rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
  		drm_sched_rq_remove_fifo_locked(entity);
  	spin_unlock(&rq->lock);
@@ -956,7 +956,7 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
  	/* Kernel run queue has higher priority than normal run queue*/
  	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
-		entity = drm_sched_policy == DRM_SCHED_POLICY_FIFO ?
+		entity = sched->sched_policy == DRM_SCHED_POLICY_FIFO ?
  			drm_sched_rq_select_entity_fifo(&sched->sched_rq[i]) :
  			drm_sched_rq_select_entity_rr(&sched->sched_rq[i]);
  		if (entity)
@@ -1190,6 +1190,7 @@ static void drm_sched_main(struct work_struct *w)
   *		used
   * @score: optional score atomic shared with other schedulers
   * @name: name used for debugging
+ * @sched_policy: schedule policy
   * @dev: target &struct device
   *
   * Return 0 on success, otherwise error code.
@@ -1199,9 +1200,15 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   struct workqueue_struct *run_wq,
  		   unsigned hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
-		   atomic_t *score, const char *name, struct device *dev)
+		   atomic_t *score, const char *name,
+		   enum drm_sched_policy sched_policy,
+		   struct device *dev)
  {
  	int i;
+
+	if (sched_policy >= DRM_SCHED_POLICY_COUNT)
+		return -EINVAL;
+
  	sched->ops = ops;
  	sched->hw_submission_limit = hw_submission;
  	sched->name = name;
@@ -1211,6 +1218,10 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  	sched->hang_limit = hang_limit;
  	sched->score = score ? score : &sched->_score;
  	sched->dev = dev;
+	if (sched_policy == DRM_SCHED_POLICY_DEFAULT)
+		sched->sched_policy = default_drm_sched_policy;
+	else
+		sched->sched_policy = sched_policy;
  	for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_COUNT; i++)
  		drm_sched_rq_init(sched, &sched->sched_rq[i]);
drivers/gpu/drm/v3d/v3d_sched.c +10 -5
index 38e092ea41e6..5e3fe77fa991 100644
@@ -391,7 +391,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  			     &v3d_bin_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_bin", v3d->drm.dev);
+			     NULL, "v3d_bin", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
  	if (ret)
  		return ret;
@@ -399,7 +400,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  			     &v3d_render_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_render", v3d->drm.dev);
+			     ULL, "v3d_render", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
  	if (ret)
  		goto fail;
@@ -407,7 +409,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  			     &v3d_tfu_sched_ops, NULL,
  			     hw_jobs_limit, job_hang_limit,
  			     msecs_to_jiffies(hang_limit_ms), NULL,
-			     NULL, "v3d_tfu", v3d->drm.dev);
+			     NULL, "v3d_tfu", DRM_SCHED_POLICY_DEFAULT,
+			     v3d->drm.dev);
  	if (ret)
  		goto fail;
@@ -416,7 +419,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  				     &v3d_csd_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
-				     NULL, "v3d_csd", v3d->drm.dev);
+				     NULL, "v3d_csd", DRM_SCHED_POLICY_DEFAULT,
+				     v3d->drm.dev);
  		if (ret)
  			goto fail;
@@ -424,7 +428,8 @@ v3d_sched_init(struct v3d_dev *v3d)
  				     &v3d_cache_clean_sched_ops, NULL,
  				     hw_jobs_limit, job_hang_limit,
  				     msecs_to_jiffies(hang_limit_ms), NULL,
-				     NULL, "v3d_cache_clean", v3d->drm.dev);
+				     NULL, "v3d_cache_clean",
+				     DRM_SCHED_POLICY_DEFAULT, v3d->drm.dev);
  		if (ret)
  			goto fail;
  	}
drivers/gpu/drm/xe/xe_execlist.c +1 -1
index d6d60ebf3d5f..48060d14547a 100644
@@ -339,7 +339,7 @@ static int execlist_engine_init(struct xe_engine *e)
  	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
-			     NULL, NULL, e->hwe->name,
+			     NULL, NULL, e->hwe->name, DRM_SCHED_POLICY_DEFAULT,
  			     gt_to_xe(e->gt)->drm.dev);
  	if (err)
  		goto err_free;
drivers/gpu/drm/xe/xe_guc_submit.c +2 -1
index 735f31257f3a..9d3fadca43be 100644
@@ -1084,7 +1084,8 @@ static int guc_engine_init(struct xe_engine *e)
  	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
-			     e->name, gt_to_xe(e->gt)->drm.dev);
+			     e->name, DRM_SCHED_POLICY_DEFAULT,
+			     gt_to_xe(e->gt)->drm.dev);
  	if (err)
  		goto err_free;
include/drm/gpu_scheduler.h +14 -6
index 09bc39840dc8..3df801401028 100644
@@ -63,11 +63,15 @@ enum drm_sched_priority {
  	DRM_SCHED_PRIORITY_UNSET = -2
  };
-/* Used to chose between FIFO and RR jobs scheduling */
-extern int drm_sched_policy;
-
-#define DRM_SCHED_POLICY_RR    0
-#define DRM_SCHED_POLICY_FIFO  1
+/* Used to chose default scheduling policy*/
+extern int default_drm_sched_policy;
+
+enum drm_sched_policy {
+	DRM_SCHED_POLICY_DEFAULT,
+	DRM_SCHED_POLICY_RR,
+	DRM_SCHED_POLICY_FIFO,
+	DRM_SCHED_POLICY_COUNT,
+};
  /**
   * struct drm_sched_entity - A wrapper around a job queue (typically
@@ -505,6 +509,7 @@ struct drm_sched_backend_ops {
   *              guilty and it will no longer be considered for scheduling.
   * @score: score to help loadbalancer pick a idle sched
   * @_score: score used when the driver doesn't provide one
+ * @sched_policy: Schedule policy for scheduler
   * @ready: marks if the underlying HW is ready to work
   * @free_guilty: A hit to time out handler to free the guilty job.
   * @pause_run_wq: pause queuing of @work_run on @run_wq
@@ -531,6 +536,7 @@ struct drm_gpu_scheduler {
  	int				hang_limit;
  	atomic_t                        *score;
  	atomic_t                        _score;
+	enum drm_sched_policy		sched_policy;
  	bool				ready;
  	bool				free_guilty;
  	bool				pause_run_wq;
@@ -542,7 +548,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  		   struct workqueue_struct *run_wq,
  		   uint32_t hw_submission, unsigned hang_limit,
  		   long timeout, struct workqueue_struct *timeout_wq,
-		   atomic_t *score, const char *name, struct device *dev);
+		   atomic_t *score, const char *name,
+		   enum drm_sched_policy sched_policy,
+		   struct device *dev);
  void drm_sched_fini(struct drm_gpu_scheduler *sched);
  int drm_sched_job_init(struct drm_sched_job *job,
author maintainer
#8 lore raw

PATCH v2 3/31 drm/sched: Add DRM_SCHED_POLICY_SINGLE_ENTITY scheduling policy

In reply to Matthew Brost

DRM_SCHED_POLICY_SINGLE_ENTITY creates a 1 to 1 relationship between scheduler and entity. No priorities or run queue used in this mode. Intended for devices with firmware schedulers.

---
 drivers/gpu/drm/scheduler/sched_entity.c | 64 +++++++++++++++++++-----
 drivers/gpu/drm/scheduler/sched_fence.c  |  2 +-
 drivers/gpu/drm/scheduler/sched_main.c   | 63 ++++++++++++++++++++---
 include/drm/gpu_scheduler.h              |  8 +++
 4 files changed, 115 insertions(+), 22 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +51 -13
index 2300b2fc06ab..8b70900c54cc 100644
@@ -83,6 +83,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 	memset(entity, 0, sizeof(struct drm_sched_entity));
 	INIT_LIST_HEAD(&entity->list);
 	entity->rq = NULL;
+	entity->single_sched = NULL;
 	entity->guilty = guilty;
 	entity->num_sched_list = num_sched_list;
 	entity->priority = priority;
@@ -91,7 +92,15 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 	RB_CLEAR_NODE(&entity->rb_tree_node);
 
 	if(num_sched_list) {
-		entity->rq = &sched_list[0]->sched_rq[entity->priority];
+		if (sched_list[0]->sched_policy !=
+		    DRM_SCHED_POLICY_SINGLE_ENTITY) {
+			entity->rq = &sched_list[0]->sched_rq[entity->priority];
+		} else {
+			if (num_sched_list != 1 || sched_list[0]->single_entity)
+				return -EINVAL;
+			sched_list[0]->single_entity = entity;
+			entity->single_sched = sched_list[0];
+		}
 	}
 
 	init_completion(&entity->entity_idle);
@@ -125,7 +134,8 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
 				    struct drm_gpu_scheduler **sched_list,
 				    unsigned int num_sched_list)
 {
-	WARN_ON(!num_sched_list || !sched_list);
+	WARN_ON(!num_sched_list || !sched_list ||
+		!!entity->single_sched);
 
 	entity->sched_list = sched_list;
 	entity->num_sched_list = num_sched_list;
@@ -195,13 +205,15 @@ static void drm_sched_entity_kill(struct drm_sched_entity *entity)
 {
 	struct drm_sched_job *job;
 	struct dma_fence *prev;
+	bool single_entity = !!entity->single_sched;
 
-	if (!entity->rq)
+	if (!entity->rq && !single_entity)
 		return;
 
 	spin_lock(&entity->rq_lock);
 	entity->stopped = true;
-	drm_sched_rq_remove_entity(entity->rq, entity);
+	if (!single_entity)
+		drm_sched_rq_remove_entity(entity->rq, entity);
 	spin_unlock(&entity->rq_lock);
 
 	/* Make sure this entity is not used by the scheduler at the moment */
@@ -223,6 +235,20 @@ static void drm_sched_entity_kill(struct drm_sched_entity *entity)
 	dma_fence_put(prev);
 }
 
+/**
+ * drm_sched_entity_to_scheduler - Schedule entity to GPU scheduler
+ * @entity: scheduler entity
+ *
+ * Returns GPU scheduler for the entity
+ */
+struct drm_gpu_scheduler *
+drm_sched_entity_to_scheduler(struct drm_sched_entity *entity)
+{
+	bool single_entity = !!entity->single_sched;
+
+	return single_entity ? entity->single_sched : entity->rq->sched;
+}
+
 /**
  * drm_sched_entity_flush - Flush a context entity
  *
@@ -240,11 +266,12 @@ long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)
 	struct drm_gpu_scheduler *sched;
 	struct task_struct *last_user;
 	long ret = timeout;
+	bool single_entity = !!entity->single_sched;
 
-	if (!entity->rq)
+	if (!entity->rq && !single_entity)
 		return 0;
 
-	sched = entity->rq->sched;
+	sched = drm_sched_entity_to_scheduler(entity);
 	/**
 	 * The client will not queue more IBs during this fini, consume existing
 	 * queued IBs or discard them on SIGKILL
@@ -337,7 +364,7 @@ static void drm_sched_entity_wakeup(struct dma_fence *f,
 		container_of(cb, struct drm_sched_entity, cb);
 
 	drm_sched_entity_clear_dep(f, cb);
-	drm_sched_wakeup(entity->rq->sched);
+	drm_sched_wakeup(drm_sched_entity_to_scheduler(entity));
 }
 
 /**
@@ -351,6 +378,8 @@ static void drm_sched_entity_wakeup(struct dma_fence *f,
 void drm_sched_entity_set_priority(struct drm_sched_entity *entity,
 				   enum drm_sched_priority priority)
 {
+	WARN_ON(!!entity->single_sched);
+
 	spin_lock(&entity->rq_lock);
 	entity->priority = priority;
 	spin_unlock(&entity->rq_lock);
@@ -363,7 +392,7 @@ EXPORT_SYMBOL(drm_sched_entity_set_priority);
  */
 static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity)
 {
-	struct drm_gpu_scheduler *sched = entity->rq->sched;
+	struct drm_gpu_scheduler *sched = drm_sched_entity_to_scheduler(entity);
 	struct dma_fence *fence = entity->dependency;
 	struct drm_sched_fence *s_fence;
 
@@ -456,7 +485,8 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
 	 * Update the entity's location in the min heap according to
 	 * the timestamp of the next job, if any.
 	 */
-	if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO) {
+	if (drm_sched_entity_to_scheduler(entity)->sched_policy ==
+	    DRM_SCHED_POLICY_FIFO) {
 		struct drm_sched_job *next;
 
 		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
@@ -473,6 +503,8 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 	struct drm_gpu_scheduler *sched;
 	struct drm_sched_rq *rq;
 
+	WARN_ON(!!entity->single_sched);
+
 	/* single possible engine and already selected */
 	if (!entity->sched_list)
 		return;
@@ -522,16 +554,21 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 {
 	struct drm_sched_entity *entity = sched_job->entity;
+	bool single_entity = !!entity->single_sched;
 	bool first;
 
 	trace_drm_sched_job(sched_job, entity);
-	atomic_inc(entity->rq->sched->score);
+	if (!single_entity)
+		atomic_inc(entity->rq->sched->score);
 	WRITE_ONCE(entity->last_user, current->group_leader);
 	first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
 	sched_job->submit_ts = ktime_get();
 
 	/* first job wakes up scheduler */
 	if (first) {
+		struct drm_gpu_scheduler *sched =
+			drm_sched_entity_to_scheduler(entity);
+
 		/* Add the entity to the run queue */
 		spin_lock(&entity->rq_lock);
 		if (entity->stopped) {
@@ -541,13 +578,14 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
 			return;
 		}
 
-		drm_sched_rq_add_entity(entity->rq, entity);
+		if (!single_entity)
+			drm_sched_rq_add_entity(entity->rq, entity);
 		spin_unlock(&entity->rq_lock);
 
-		if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
+		if (sched->sched_policy == DRM_SCHED_POLICY_FIFO)
 			drm_sched_rq_update_fifo(entity, sched_job->submit_ts);
 
-		drm_sched_wakeup(entity->rq->sched);
+		drm_sched_wakeup(sched);
 	}
 }
 EXPORT_SYMBOL(drm_sched_entity_push_job);
drivers/gpu/drm/scheduler/sched_fence.c +1 -1
index 7fd869520ef2..1ba5056851dd 100644
@@ -167,7 +167,7 @@ void drm_sched_fence_init(struct drm_sched_fence *fence,
 {
 	unsigned seq;
 
-	fence->sched = entity->rq->sched;
+	fence->sched = drm_sched_entity_to_scheduler(entity);
 	seq = atomic_inc_return(&entity->fence_seq);
 	dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
 		       &fence->lock, entity->fence_context, seq);
drivers/gpu/drm/scheduler/sched_main.c +55 -8

145 lines not on this page · raw message

include/drm/gpu_scheduler.h +8 -0
index 3df801401028..669d6520cd3a 100644
@@ -70,6 +70,7 @@ enum drm_sched_policy {
 	DRM_SCHED_POLICY_DEFAULT,
 	DRM_SCHED_POLICY_RR,
 	DRM_SCHED_POLICY_FIFO,
+	DRM_SCHED_POLICY_SINGLE_ENTITY,
 	DRM_SCHED_POLICY_COUNT,
 };
 
@@ -103,6 +104,9 @@ struct drm_sched_entity {
 	 */
 	struct drm_sched_rq		*rq;
 
+	/** @single_sched: Single scheduler */
+	struct drm_gpu_scheduler	*single_sched;
+
 	/**
 	 * @sched_list:
 	 *
@@ -488,6 +492,7 @@ struct drm_sched_backend_ops {
  * struct drm_gpu_scheduler - scheduler instance-specific data
  *
  * @ops: backend operations provided by the driver.
+ * @single_entity: Single entity for the scheduler
  * @hw_submission_limit: the max size of the hardware queue.
  * @timeout: the time after which a job is removed from the scheduler.
  * @name: name of the ring for which this scheduler is being used.
@@ -519,6 +524,7 @@ struct drm_sched_backend_ops {
  */
 struct drm_gpu_scheduler {
 	const struct drm_sched_backend_ops	*ops;
+	struct drm_sched_entity		*single_entity;
 	uint32_t			hw_submission_limit;
 	long				timeout;
 	const char			*name;
@@ -604,6 +610,8 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 			  struct drm_gpu_scheduler **sched_list,
 			  unsigned int num_sched_list,
 			  atomic_t *guilty);
+struct drm_gpu_scheduler *
+drm_sched_entity_to_scheduler(struct drm_sched_entity *entity);
 long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout);
 void drm_sched_entity_fini(struct drm_sched_entity *entity);
 void drm_sched_entity_destroy(struct drm_sched_entity *entity);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 3/31
#9 lore raw

Re: [Intel-xe] [PATCH v2 03/31] drm/sched: Add DRM_SCHED_POLICY_SINGLE_ENTITY scheduling policy

An question below, with that addressed (possibly without change)

although I'm not a scheduler expert and we should ideally have additional reviewers,

Quoted 42 lines · Matthew Brost

DRM_SCHED_POLICY_SINGLE_ENTITY creates a 1 to 1 relationship between scheduler and entity. No priorities or run queue used in this mode. Intended for devices with firmware schedulers.

---
  drivers/gpu/drm/scheduler/sched_entity.c | 64 +++++++++++++++++++-----
  drivers/gpu/drm/scheduler/sched_fence.c  |  2 +-
  drivers/gpu/drm/scheduler/sched_main.c   | 63 ++++++++++++++++++++---
  include/drm/gpu_scheduler.h              |  8 +++
  4 files changed, 115 insertions(+), 22 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +10 -1
index 2300b2fc06ab..8b70900c54cc 100644
@@ -83,6 +83,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  	memset(entity, 0, sizeof(struct drm_sched_entity));
  	INIT_LIST_HEAD(&entity->list);
  	entity->rq = NULL;
+	entity->single_sched = NULL;
  	entity->guilty = guilty;
  	entity->num_sched_list = num_sched_list;
  	entity->priority = priority;
@@ -91,7 +92,15 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  	RB_CLEAR_NODE(&entity->rb_tree_node);
 
  	if(num_sched_list) {
-		entity->rq = &sched_list[0]->sched_rq[entity->priority];
+		if (sched_list[0]->sched_policy !=
+		    DRM_SCHED_POLICY_SINGLE_ENTITY) {
+			entity->rq = &sched_list[0]->sched_rq[entity->priority];
+		} else {
+			if (num_sched_list != 1 || sched_list[0]->single_entity)
+				return -EINVAL;
+			sched_list[0]->single_entity = entity;
+			entity->single_sched = sched_list[0];
+		}
  	}
 
  	init_completion(&entity->entity_idle);

drivers/gpu/drm/scheduler/sched_entity.c void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, · line 128

Quoted 4 lines
drivers/gpu/drm/scheduler/sched_entity.c +0 -1
@@ -125,7 +134,8 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
  				    struct drm_gpu_scheduler **sched_list,
  				    unsigned int num_sched_list)
  {
-	WARN_ON(!num_sched_list || !sched_list);

Is there a way to get to the drm device so we can use drm_WARN_ON() here and below? I figure not?

Thanks,

Thomas

Quoted 368 lines
drivers/gpu/drm/scheduler/sched_entity.c +41 -11
+	WARN_ON(!num_sched_list || !sched_list ||
+		!!entity->single_sched);
 
  	entity->sched_list = sched_list;
  	entity->num_sched_list = num_sched_list;
@@ -195,13 +205,15 @@ static void drm_sched_entity_kill(struct drm_sched_entity *entity)
  {
  	struct drm_sched_job *job;
  	struct dma_fence *prev;
+	bool single_entity = !!entity->single_sched;
 
-	if (!entity->rq)
+	if (!entity->rq && !single_entity)
  		return;
 
  	spin_lock(&entity->rq_lock);
  	entity->stopped = true;
-	drm_sched_rq_remove_entity(entity->rq, entity);
+	if (!single_entity)
+		drm_sched_rq_remove_entity(entity->rq, entity);
  	spin_unlock(&entity->rq_lock);
 
  	/* Make sure this entity is not used by the scheduler at the moment */
@@ -223,6 +235,20 @@ static void drm_sched_entity_kill(struct drm_sched_entity *entity)
  	dma_fence_put(prev);
  }
 
+/**
+ * drm_sched_entity_to_scheduler - Schedule entity to GPU scheduler
+ * @entity: scheduler entity
+ *
+ * Returns GPU scheduler for the entity
+ */
+struct drm_gpu_scheduler *
+drm_sched_entity_to_scheduler(struct drm_sched_entity *entity)
+{
+	bool single_entity = !!entity->single_sched;
+
+	return single_entity ? entity->single_sched : entity->rq->sched;
+}
+
  /**
   * drm_sched_entity_flush - Flush a context entity
   *
@@ -240,11 +266,12 @@ long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)
  	struct drm_gpu_scheduler *sched;
  	struct task_struct *last_user;
  	long ret = timeout;
+	bool single_entity = !!entity->single_sched;
 
-	if (!entity->rq)
+	if (!entity->rq && !single_entity)
  		return 0;
 
-	sched = entity->rq->sched;
+	sched = drm_sched_entity_to_scheduler(entity);
  	/**
  	 * The client will not queue more IBs during this fini, consume existing
  	 * queued IBs or discard them on SIGKILL
@@ -337,7 +364,7 @@ static void drm_sched_entity_wakeup(struct dma_fence *f,
  		container_of(cb, struct drm_sched_entity, cb);
 
  	drm_sched_entity_clear_dep(f, cb);
-	drm_sched_wakeup(entity->rq->sched);
+	drm_sched_wakeup(drm_sched_entity_to_scheduler(entity));
  }
 
  /**
@@ -351,6 +378,8 @@ static void drm_sched_entity_wakeup(struct dma_fence *f,
  void drm_sched_entity_set_priority(struct drm_sched_entity *entity,
  				   enum drm_sched_priority priority)
  {
+	WARN_ON(!!entity->single_sched);
+
  	spin_lock(&entity->rq_lock);
  	entity->priority = priority;
  	spin_unlock(&entity->rq_lock);
@@ -363,7 +392,7 @@ EXPORT_SYMBOL(drm_sched_entity_set_priority);
   */
  static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity)
  {
-	struct drm_gpu_scheduler *sched = entity->rq->sched;
+	struct drm_gpu_scheduler *sched = drm_sched_entity_to_scheduler(entity);
  	struct dma_fence *fence = entity->dependency;
  	struct drm_sched_fence *s_fence;
 
@@ -456,7 +485,8 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
  	 * Update the entity's location in the min heap according to
  	 * the timestamp of the next job, if any.
  	 */
-	if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO) {
+	if (drm_sched_entity_to_scheduler(entity)->sched_policy ==
+	    DRM_SCHED_POLICY_FIFO) {
  		struct drm_sched_job *next;
 
  		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
@@ -473,6 +503,8 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
  	struct drm_gpu_scheduler *sched;
  	struct drm_sched_rq *rq;
 
+	WARN_ON(!!entity->single_sched);
+
  	/* single possible engine and already selected */
  	if (!entity->sched_list)
  		return;
@@ -522,16 +554,21 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
  void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  {
  	struct drm_sched_entity *entity = sched_job->entity;
+	bool single_entity = !!entity->single_sched;
  	bool first;
 
  	trace_drm_sched_job(sched_job, entity);
-	atomic_inc(entity->rq->sched->score);
+	if (!single_entity)
+		atomic_inc(entity->rq->sched->score);
  	WRITE_ONCE(entity->last_user, current->group_leader);
  	first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
  	sched_job->submit_ts = ktime_get();
 
  	/* first job wakes up scheduler */
  	if (first) {
+		struct drm_gpu_scheduler *sched =
+			drm_sched_entity_to_scheduler(entity);
+
  		/* Add the entity to the run queue */
  		spin_lock(&entity->rq_lock);
  		if (entity->stopped) {
@@ -541,13 +578,14 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  			return;
  		}
 
-		drm_sched_rq_add_entity(entity->rq, entity);
+		if (!single_entity)
+			drm_sched_rq_add_entity(entity->rq, entity);
  		spin_unlock(&entity->rq_lock);
 
-		if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
+		if (sched->sched_policy == DRM_SCHED_POLICY_FIFO)
  			drm_sched_rq_update_fifo(entity, sched_job->submit_ts);
 
-		drm_sched_wakeup(entity->rq->sched);
+		drm_sched_wakeup(sched);
  	}
  }
  EXPORT_SYMBOL(drm_sched_entity_push_job);
drivers/gpu/drm/scheduler/sched_fence.c +1 -1
index 7fd869520ef2..1ba5056851dd 100644
@@ -167,7 +167,7 @@ void drm_sched_fence_init(struct drm_sched_fence *fence,
  {
  	unsigned seq;
 
-	fence->sched = entity->rq->sched;
+	fence->sched = drm_sched_entity_to_scheduler(entity);
  	seq = atomic_inc_return(&entity->fence_seq);
  	dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
  		       &fence->lock, entity->fence_context, seq);
drivers/gpu/drm/scheduler/sched_main.c +55 -8
index 6777a2db554f..870568d94f1f 100644
@@ -32,7 +32,8 @@
   * backend operations to the scheduler like submitting a job to hardware run queue,
   * returning the dependencies of a job etc.
   *
- * The organisation of the scheduler is the following:
+ * The organisation of the scheduler is the following for scheduling policies
+ * DRM_SCHED_POLICY_RR and DRM_SCHED_POLICY_FIFO:
   *
   * 1. Each hw run queue has one scheduler
   * 2. Each scheduler has multiple run queues with different priorities
@@ -41,7 +42,22 @@
   * 4. Entities themselves maintain a queue of jobs that will be scheduled on
   *    the hardware.
   *
- * The jobs in a entity are always scheduled in the order that they were pushed.
+ * The organisation of the scheduler is the following for scheduling policy
+ * DRM_SCHED_POLICY_SINGLE_ENTITY:
+ *
+ * 1. One to one relationship between scheduler and entity
+ * 2. No priorities implemented per scheduler (single job queue)
+ * 3. No run queues in scheduler rather jobs are directly dequeued from entity
+ * 4. The entity maintains a queue of jobs that will be scheduled on the
+ * hardware
+ *
+ * The jobs in a entity are always scheduled in the order that they were pushed
+ * regardless of scheduling policy.
+ *
+ * A policy of DRM_SCHED_POLICY_RR or DRM_SCHED_POLICY_FIFO is expected to used
+ * when the KMD is scheduling directly on the hardware while a scheduling policy
+ * of DRM_SCHED_POLICY_SINGLE_ENTITY is expected to be used when there is a
+ * firmare scheduler.
   */
 
  #include <linux/wait.h>
@@ -92,6 +108,8 @@ static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *enti
 
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
  {
+	WARN_ON(!!entity->single_sched);
+
  	/*
  	 * Both locks need to be grabbed, one to protect from entity->rq change
  	 * for entity from within concurrent drm_sched_entity_select_rq and the
@@ -122,6 +140,8 @@ void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
  static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
  			      struct drm_sched_rq *rq)
  {
+	WARN_ON(sched->sched_policy == DRM_SCHED_POLICY_SINGLE_ENTITY);
+
  	spin_lock_init(&rq->lock);
  	INIT_LIST_HEAD(&rq->entities);
  	rq->rb_tree_root = RB_ROOT_CACHED;
@@ -140,6 +160,8 @@ static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
  void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  			     struct drm_sched_entity *entity)
  {
+	WARN_ON(!!entity->single_sched);
+
  	if (!list_empty(&entity->list))
  		return;
 
@@ -162,6 +184,8 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity)
  {
+	WARN_ON(!!entity->single_sched);
+
  	if (list_empty(&entity->list))
  		return;
 
@@ -691,7 +715,7 @@ int drm_sched_job_init(struct drm_sched_job *job,
  		       struct drm_sched_entity *entity,
  		       void *owner)
  {
-	if (!entity->rq)
+	if (!entity->rq && !entity->single_sched)
  		return -ENOENT;
 
  	job->entity = entity;
@@ -724,13 +748,16 @@ void drm_sched_job_arm(struct drm_sched_job *job)
  {
  	struct drm_gpu_scheduler *sched;
  	struct drm_sched_entity *entity = job->entity;
+	bool single_entity = !!entity->single_sched;
 
  	BUG_ON(!entity);
-	drm_sched_entity_select_rq(entity);
-	sched = entity->rq->sched;
+	if (!single_entity)
+		drm_sched_entity_select_rq(entity);
+	sched = drm_sched_entity_to_scheduler(entity);
 
  	job->sched = sched;
-	job->s_priority = entity->rq - sched->sched_rq;
+	if (!single_entity)
+		job->s_priority = entity->rq - sched->sched_rq;
  	job->id = atomic64_inc_return(&sched->job_id_count);
 
  	drm_sched_fence_init(job->s_fence, job->entity);
@@ -954,6 +981,13 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
  	if (!drm_sched_ready(sched))
  		return NULL;
 
+	if (sched->single_entity) {
+		if (drm_sched_entity_is_ready(sched->single_entity))
+			return sched->single_entity;
+
+		return NULL;
+	}
+
  	/* Kernel run queue has higher priority than normal run queue*/
  	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
  		entity = sched->sched_policy == DRM_SCHED_POLICY_FIFO ?
@@ -1210,6 +1244,7 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  		return -EINVAL;
 
  	sched->ops = ops;
+	sched->single_entity = NULL;
  	sched->hw_submission_limit = hw_submission;
  	sched->name = name;
  	sched->run_wq = run_wq ? : system_wq;
@@ -1222,7 +1257,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  		sched->sched_policy = default_drm_sched_policy;
  	else
  		sched->sched_policy = sched_policy;
-	for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_COUNT; i++)
+	for (i = DRM_SCHED_PRIORITY_MIN; sched_policy !=
+	     DRM_SCHED_POLICY_SINGLE_ENTITY && i < DRM_SCHED_PRIORITY_COUNT;
+	     i++)
  		drm_sched_rq_init(sched, &sched->sched_rq[i]);
 
  	init_waitqueue_head(&sched->job_scheduled);
@@ -1255,7 +1292,15 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
 
  	drm_sched_run_wq_stop(sched);
 
-	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
+	if (sched->single_entity) {
+		spin_lock(&sched->single_entity->rq_lock);
+		sched->single_entity->stopped = true;
+		spin_unlock(&sched->single_entity->rq_lock);
+	}
+
+	for (i = DRM_SCHED_PRIORITY_COUNT - 1; sched->sched_policy !=
+	     DRM_SCHED_POLICY_SINGLE_ENTITY && i >= DRM_SCHED_PRIORITY_MIN;
+	     i--) {
  		struct drm_sched_rq *rq = &sched->sched_rq[i];
 
  		if (!rq)
@@ -1299,6 +1344,8 @@ void drm_sched_increase_karma(struct drm_sched_job *bad)
  	struct drm_sched_entity *entity;
  	struct drm_gpu_scheduler *sched = bad->sched;
 
+	WARN_ON(sched->sched_policy == DRM_SCHED_POLICY_SINGLE_ENTITY);
+
  	/* don't change @bad's karma if it's from KERNEL RQ,
  	 * because sometimes GPU hang would cause kernel jobs (like VM updating jobs)
  	 * corrupt but keep in mind that kernel jobs always considered good.
include/drm/gpu_scheduler.h +8 -0
index 3df801401028..669d6520cd3a 100644
@@ -70,6 +70,7 @@ enum drm_sched_policy {
  	DRM_SCHED_POLICY_DEFAULT,
  	DRM_SCHED_POLICY_RR,
  	DRM_SCHED_POLICY_FIFO,
+	DRM_SCHED_POLICY_SINGLE_ENTITY,
  	DRM_SCHED_POLICY_COUNT,
  };
 
@@ -103,6 +104,9 @@ struct drm_sched_entity {
  	 */
  	struct drm_sched_rq		*rq;
 
+	/** @single_sched: Single scheduler */
+	struct drm_gpu_scheduler	*single_sched;
+
  	/**
  	 * @sched_list:
  	 *
@@ -488,6 +492,7 @@ struct drm_sched_backend_ops {
   * struct drm_gpu_scheduler - scheduler instance-specific data
   *
   * @ops: backend operations provided by the driver.
+ * @single_entity: Single entity for the scheduler
   * @hw_submission_limit: the max size of the hardware queue.
   * @timeout: the time after which a job is removed from the scheduler.
   * @name: name of the ring for which this scheduler is being used.
@@ -519,6 +524,7 @@ struct drm_sched_backend_ops {
   */
  struct drm_gpu_scheduler {
  	const struct drm_sched_backend_ops	*ops;
+	struct drm_sched_entity		*single_entity;
  	uint32_t			hw_submission_limit;
  	long				timeout;
  	const char			*name;
@@ -604,6 +610,8 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  			  struct drm_gpu_scheduler **sched_list,
  			  unsigned int num_sched_list,
  			  atomic_t *guilty);
+struct drm_gpu_scheduler *
+drm_sched_entity_to_scheduler(struct drm_sched_entity *entity);
  long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout);
  void drm_sched_entity_fini(struct drm_sched_entity *entity);
  void drm_sched_entity_destroy(struct drm_sched_entity *entity);
author maintainer
#10 lore raw

Re: [Intel-xe] [PATCH v2 03/31] drm/sched: Add DRM_SCHED_POLICY_SINGLE_ENTITY scheduling policy

Quoted 47 lines · Thomas Hellström

An question below, with that addressed (possibly without change)

although I'm not a scheduler expert and we should ideally have additional reviewers,

Quoted 39 lines · Matthew Brost

DRM_SCHED_POLICY_SINGLE_ENTITY creates a 1 to 1 relationship between scheduler and entity. No priorities or run queue used in this mode. Intended for devices with firmware schedulers.

---
  drivers/gpu/drm/scheduler/sched_entity.c | 64 +++++++++++++++++++-----
  drivers/gpu/drm/scheduler/sched_fence.c  |  2 +-
  drivers/gpu/drm/scheduler/sched_main.c   | 63 ++++++++++++++++++++---
  include/drm/gpu_scheduler.h              |  8 +++
  4 files changed, 115 insertions(+), 22 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +10 -1
index 2300b2fc06ab..8b70900c54cc 100644
@@ -83,6 +83,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  	memset(entity, 0, sizeof(struct drm_sched_entity));
  	INIT_LIST_HEAD(&entity->list);
  	entity->rq = NULL;
+	entity->single_sched = NULL;
  	entity->guilty = guilty;
  	entity->num_sched_list = num_sched_list;
  	entity->priority = priority;
@@ -91,7 +92,15 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  	RB_CLEAR_NODE(&entity->rb_tree_node);
  	if(num_sched_list) {
-		entity->rq = &sched_list[0]->sched_rq[entity->priority];
+		if (sched_list[0]->sched_policy !=
+		    DRM_SCHED_POLICY_SINGLE_ENTITY) {
+			entity->rq = &sched_list[0]->sched_rq[entity->priority];
+		} else {
+			if (num_sched_list != 1 || sched_list[0]->single_entity)
+				return -EINVAL;
+			sched_list[0]->single_entity = entity;
+			entity->single_sched = sched_list[0];
+		}
  	}
  	init_completion(&entity->entity_idle);
Quoted 9 lines

drivers/gpu/drm/scheduler/sched_entity.c void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, · line 128

Quoted 5 lines
drivers/gpu/drm/scheduler/sched_entity.c +0 -1
@@ -125,7 +134,8 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
  				    struct drm_gpu_scheduler **sched_list,
  				    unsigned int num_sched_list)
  {
-	WARN_ON(!num_sched_list || !sched_list);

Is there a way to get to the drm device so we can use drm_WARN_ON() here and below? I figure not?

Correct, no way to resolve the DRM device.

Matt

Quoted 338 lines

Thanks,

Thomas

Quoted 333 lines
drivers/gpu/drm/scheduler/sched_entity.c +41 -11
+	WARN_ON(!num_sched_list || !sched_list ||
+		!!entity->single_sched);
  	entity->sched_list = sched_list;
  	entity->num_sched_list = num_sched_list;
@@ -195,13 +205,15 @@ static void drm_sched_entity_kill(struct drm_sched_entity *entity)
  {
  	struct drm_sched_job *job;
  	struct dma_fence *prev;
+	bool single_entity = !!entity->single_sched;
-	if (!entity->rq)
+	if (!entity->rq && !single_entity)
  		return;
  	spin_lock(&entity->rq_lock);
  	entity->stopped = true;
-	drm_sched_rq_remove_entity(entity->rq, entity);
+	if (!single_entity)
+		drm_sched_rq_remove_entity(entity->rq, entity);
  	spin_unlock(&entity->rq_lock);
  	/* Make sure this entity is not used by the scheduler at the moment */
@@ -223,6 +235,20 @@ static void drm_sched_entity_kill(struct drm_sched_entity *entity)
  	dma_fence_put(prev);
  }
+/**
+ * drm_sched_entity_to_scheduler - Schedule entity to GPU scheduler
+ * @entity: scheduler entity
+ *
+ * Returns GPU scheduler for the entity
+ */
+struct drm_gpu_scheduler *
+drm_sched_entity_to_scheduler(struct drm_sched_entity *entity)
+{
+	bool single_entity = !!entity->single_sched;
+
+	return single_entity ? entity->single_sched : entity->rq->sched;
+}
+
  /**
   * drm_sched_entity_flush - Flush a context entity
   *
@@ -240,11 +266,12 @@ long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)
  	struct drm_gpu_scheduler *sched;
  	struct task_struct *last_user;
  	long ret = timeout;
+	bool single_entity = !!entity->single_sched;
-	if (!entity->rq)
+	if (!entity->rq && !single_entity)
  		return 0;
-	sched = entity->rq->sched;
+	sched = drm_sched_entity_to_scheduler(entity);
  	/**
  	 * The client will not queue more IBs during this fini, consume existing
  	 * queued IBs or discard them on SIGKILL
@@ -337,7 +364,7 @@ static void drm_sched_entity_wakeup(struct dma_fence *f,
  		container_of(cb, struct drm_sched_entity, cb);
  	drm_sched_entity_clear_dep(f, cb);
-	drm_sched_wakeup(entity->rq->sched);
+	drm_sched_wakeup(drm_sched_entity_to_scheduler(entity));
  }
  /**
@@ -351,6 +378,8 @@ static void drm_sched_entity_wakeup(struct dma_fence *f,
  void drm_sched_entity_set_priority(struct drm_sched_entity *entity,
  				   enum drm_sched_priority priority)
  {
+	WARN_ON(!!entity->single_sched);
+
  	spin_lock(&entity->rq_lock);
  	entity->priority = priority;
  	spin_unlock(&entity->rq_lock);
@@ -363,7 +392,7 @@ EXPORT_SYMBOL(drm_sched_entity_set_priority);
   */
  static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity)
  {
-	struct drm_gpu_scheduler *sched = entity->rq->sched;
+	struct drm_gpu_scheduler *sched = drm_sched_entity_to_scheduler(entity);
  	struct dma_fence *fence = entity->dependency;
  	struct drm_sched_fence *s_fence;
@@ -456,7 +485,8 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
  	 * Update the entity's location in the min heap according to
  	 * the timestamp of the next job, if any.
  	 */
-	if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO) {
+	if (drm_sched_entity_to_scheduler(entity)->sched_policy ==
+	    DRM_SCHED_POLICY_FIFO) {
  		struct drm_sched_job *next;
  		next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue));
@@ -473,6 +503,8 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
  	struct drm_gpu_scheduler *sched;
  	struct drm_sched_rq *rq;
+	WARN_ON(!!entity->single_sched);
+
  	/* single possible engine and already selected */
  	if (!entity->sched_list)
  		return;
@@ -522,16 +554,21 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
  void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  {
  	struct drm_sched_entity *entity = sched_job->entity;
+	bool single_entity = !!entity->single_sched;
  	bool first;
  	trace_drm_sched_job(sched_job, entity);
-	atomic_inc(entity->rq->sched->score);
+	if (!single_entity)
+		atomic_inc(entity->rq->sched->score);
  	WRITE_ONCE(entity->last_user, current->group_leader);
  	first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
  	sched_job->submit_ts = ktime_get();
  	/* first job wakes up scheduler */
  	if (first) {
+		struct drm_gpu_scheduler *sched =
+			drm_sched_entity_to_scheduler(entity);
+
  		/* Add the entity to the run queue */
  		spin_lock(&entity->rq_lock);
  		if (entity->stopped) {
@@ -541,13 +578,14 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
  			return;
  		}
-		drm_sched_rq_add_entity(entity->rq, entity);
+		if (!single_entity)
+			drm_sched_rq_add_entity(entity->rq, entity);
  		spin_unlock(&entity->rq_lock);
-		if (entity->rq->sched->sched_policy == DRM_SCHED_POLICY_FIFO)
+		if (sched->sched_policy == DRM_SCHED_POLICY_FIFO)
  			drm_sched_rq_update_fifo(entity, sched_job->submit_ts);
-		drm_sched_wakeup(entity->rq->sched);
+		drm_sched_wakeup(sched);
  	}
  }
  EXPORT_SYMBOL(drm_sched_entity_push_job);
drivers/gpu/drm/scheduler/sched_fence.c +1 -1
index 7fd869520ef2..1ba5056851dd 100644
@@ -167,7 +167,7 @@ void drm_sched_fence_init(struct drm_sched_fence *fence,
  {
  	unsigned seq;
-	fence->sched = entity->rq->sched;
+	fence->sched = drm_sched_entity_to_scheduler(entity);
  	seq = atomic_inc_return(&entity->fence_seq);
  	dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
  		       &fence->lock, entity->fence_context, seq);
drivers/gpu/drm/scheduler/sched_main.c +55 -8
index 6777a2db554f..870568d94f1f 100644
@@ -32,7 +32,8 @@
   * backend operations to the scheduler like submitting a job to hardware run queue,
   * returning the dependencies of a job etc.
   *
- * The organisation of the scheduler is the following:
+ * The organisation of the scheduler is the following for scheduling policies
+ * DRM_SCHED_POLICY_RR and DRM_SCHED_POLICY_FIFO:
   *
   * 1. Each hw run queue has one scheduler
   * 2. Each scheduler has multiple run queues with different priorities
@@ -41,7 +42,22 @@
   * 4. Entities themselves maintain a queue of jobs that will be scheduled on
   *    the hardware.
   *
- * The jobs in a entity are always scheduled in the order that they were pushed.
+ * The organisation of the scheduler is the following for scheduling policy
+ * DRM_SCHED_POLICY_SINGLE_ENTITY:
+ *
+ * 1. One to one relationship between scheduler and entity
+ * 2. No priorities implemented per scheduler (single job queue)
+ * 3. No run queues in scheduler rather jobs are directly dequeued from entity
+ * 4. The entity maintains a queue of jobs that will be scheduled on the
+ * hardware
+ *
+ * The jobs in a entity are always scheduled in the order that they were pushed
+ * regardless of scheduling policy.
+ *
+ * A policy of DRM_SCHED_POLICY_RR or DRM_SCHED_POLICY_FIFO is expected to used
+ * when the KMD is scheduling directly on the hardware while a scheduling policy
+ * of DRM_SCHED_POLICY_SINGLE_ENTITY is expected to be used when there is a
+ * firmare scheduler.
   */
  #include <linux/wait.h>
@@ -92,6 +108,8 @@ static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *enti
  void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
  {
+	WARN_ON(!!entity->single_sched);
+
  	/*
  	 * Both locks need to be grabbed, one to protect from entity->rq change
  	 * for entity from within concurrent drm_sched_entity_select_rq and the
@@ -122,6 +140,8 @@ void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
  static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
  			      struct drm_sched_rq *rq)
  {
+	WARN_ON(sched->sched_policy == DRM_SCHED_POLICY_SINGLE_ENTITY);
+
  	spin_lock_init(&rq->lock);
  	INIT_LIST_HEAD(&rq->entities);
  	rq->rb_tree_root = RB_ROOT_CACHED;
@@ -140,6 +160,8 @@ static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
  void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  			     struct drm_sched_entity *entity)
  {
+	WARN_ON(!!entity->single_sched);
+
  	if (!list_empty(&entity->list))
  		return;
@@ -162,6 +184,8 @@ void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
  void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
  				struct drm_sched_entity *entity)
  {
+	WARN_ON(!!entity->single_sched);
+
  	if (list_empty(&entity->list))
  		return;
@@ -691,7 +715,7 @@ int drm_sched_job_init(struct drm_sched_job *job,
  		       struct drm_sched_entity *entity,
  		       void *owner)
  {
-	if (!entity->rq)
+	if (!entity->rq && !entity->single_sched)
  		return -ENOENT;
  	job->entity = entity;
@@ -724,13 +748,16 @@ void drm_sched_job_arm(struct drm_sched_job *job)
  {
  	struct drm_gpu_scheduler *sched;
  	struct drm_sched_entity *entity = job->entity;
+	bool single_entity = !!entity->single_sched;
  	BUG_ON(!entity);
-	drm_sched_entity_select_rq(entity);
-	sched = entity->rq->sched;
+	if (!single_entity)
+		drm_sched_entity_select_rq(entity);
+	sched = drm_sched_entity_to_scheduler(entity);
  	job->sched = sched;
-	job->s_priority = entity->rq - sched->sched_rq;
+	if (!single_entity)
+		job->s_priority = entity->rq - sched->sched_rq;
  	job->id = atomic64_inc_return(&sched->job_id_count);
  	drm_sched_fence_init(job->s_fence, job->entity);
@@ -954,6 +981,13 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
  	if (!drm_sched_ready(sched))
  		return NULL;
+	if (sched->single_entity) {
+		if (drm_sched_entity_is_ready(sched->single_entity))
+			return sched->single_entity;
+
+		return NULL;
+	}
+
  	/* Kernel run queue has higher priority than normal run queue*/
  	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
  		entity = sched->sched_policy == DRM_SCHED_POLICY_FIFO ?
@@ -1210,6 +1244,7 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  		return -EINVAL;
  	sched->ops = ops;
+	sched->single_entity = NULL;
  	sched->hw_submission_limit = hw_submission;
  	sched->name = name;
  	sched->run_wq = run_wq ? : system_wq;
@@ -1222,7 +1257,9 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
  		sched->sched_policy = default_drm_sched_policy;
  	else
  		sched->sched_policy = sched_policy;
-	for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_COUNT; i++)
+	for (i = DRM_SCHED_PRIORITY_MIN; sched_policy !=
+	     DRM_SCHED_POLICY_SINGLE_ENTITY && i < DRM_SCHED_PRIORITY_COUNT;
+	     i++)
  		drm_sched_rq_init(sched, &sched->sched_rq[i]);
  	init_waitqueue_head(&sched->job_scheduled);
@@ -1255,7 +1292,15 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
  	drm_sched_run_wq_stop(sched);
-	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
+	if (sched->single_entity) {
+		spin_lock(&sched->single_entity->rq_lock);
+		sched->single_entity->stopped = true;
+		spin_unlock(&sched->single_entity->rq_lock);
+	}
+
+	for (i = DRM_SCHED_PRIORITY_COUNT - 1; sched->sched_policy !=
+	     DRM_SCHED_POLICY_SINGLE_ENTITY && i >= DRM_SCHED_PRIORITY_MIN;
+	     i--) {
  		struct drm_sched_rq *rq = &sched->sched_rq[i];
  		if (!rq)
@@ -1299,6 +1344,8 @@ void drm_sched_increase_karma(struct drm_sched_job *bad)
  	struct drm_sched_entity *entity;
  	struct drm_gpu_scheduler *sched = bad->sched;
+	WARN_ON(sched->sched_policy == DRM_SCHED_POLICY_SINGLE_ENTITY);
+
  	/* don't change @bad's karma if it's from KERNEL RQ,
  	 * because sometimes GPU hang would cause kernel jobs (like VM updating jobs)
  	 * corrupt but keep in mind that kernel jobs always considered good.
include/drm/gpu_scheduler.h +8 -0
index 3df801401028..669d6520cd3a 100644
@@ -70,6 +70,7 @@ enum drm_sched_policy {
  	DRM_SCHED_POLICY_DEFAULT,
  	DRM_SCHED_POLICY_RR,
  	DRM_SCHED_POLICY_FIFO,
+	DRM_SCHED_POLICY_SINGLE_ENTITY,
  	DRM_SCHED_POLICY_COUNT,
  };
@@ -103,6 +104,9 @@ struct drm_sched_entity {
  	 */
  	struct drm_sched_rq		*rq;
+	/** @single_sched: Single scheduler */
+	struct drm_gpu_scheduler	*single_sched;
+
  	/**
  	 * @sched_list:
  	 *
@@ -488,6 +492,7 @@ struct drm_sched_backend_ops {
   * struct drm_gpu_scheduler - scheduler instance-specific data
   *
   * @ops: backend operations provided by the driver.
+ * @single_entity: Single entity for the scheduler
   * @hw_submission_limit: the max size of the hardware queue.
   * @timeout: the time after which a job is removed from the scheduler.
   * @name: name of the ring for which this scheduler is being used.
@@ -519,6 +524,7 @@ struct drm_sched_backend_ops {
   */
  struct drm_gpu_scheduler {
  	const struct drm_sched_backend_ops	*ops;
+	struct drm_sched_entity		*single_entity;
  	uint32_t			hw_submission_limit;
  	long				timeout;
  	const char			*name;
@@ -604,6 +610,8 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
  			  struct drm_gpu_scheduler **sched_list,
  			  unsigned int num_sched_list,
  			  atomic_t *guilty);
+struct drm_gpu_scheduler *
+drm_sched_entity_to_scheduler(struct drm_sched_entity *entity);
  long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout);
  void drm_sched_entity_fini(struct drm_sched_entity *entity);
  void drm_sched_entity_destroy(struct drm_sched_entity *entity);
author maintainer
#11 lore raw

PATCH v2 4/31 drm/xe: Use DRM_SCHED_POLICY_SINGLE_ENTITY mode

In reply to Matthew Brost

We create 1 GPU scheduler per entity in Xe, use DRM_SCHED_POLICY_SINGLE_ENTITY scheduling which is designed for that paradigm.

---
 drivers/gpu/drm/xe/xe_execlist.c   | 3 ++-
 drivers/gpu/drm/xe/xe_guc_submit.c | 3 +--
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_execlist.c +2 -1
index 48060d14547a..79fb951c2965 100644
@@ -339,7 +339,8 @@ static int execlist_engine_init(struct xe_engine *e)
 	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
 			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
 			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
-			     NULL, NULL, e->hwe->name, DRM_SCHED_POLICY_DEFAULT,
+			     NULL, NULL, e->hwe->name,
+			     DRM_SCHED_POLICY_SINGLE_ENTITY,
 			     gt_to_xe(e->gt)->drm.dev);
 	if (err)
 		goto err_free;
drivers/gpu/drm/xe/xe_guc_submit.c +1 -2
index 9d3fadca43be..68d09e7a4cc0 100644
@@ -1084,7 +1084,7 @@ static int guc_engine_init(struct xe_engine *e)
 	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
 			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
 			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
-			     e->name, DRM_SCHED_POLICY_DEFAULT,
+			     e->name, DRM_SCHED_POLICY_SINGLE_ENTITY,
 			     gt_to_xe(e->gt)->drm.dev);
 	if (err)
 		goto err_free;
@@ -1185,7 +1185,6 @@ static int guc_engine_set_priority(struct xe_engine *e,
 	if (!msg)
 		return -ENOMEM;
 
-	drm_sched_entity_set_priority(e->entity, priority);
 	guc_engine_add_msg(e, msg, SET_SCHED_PROPS);
 
 	return 0;
Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 4/31
#12 lore raw

Re: [Intel-xe] [PATCH v2 04/31] drm/xe: Use DRM_SCHED_POLICY_SINGLE_ENTITY mode

Quoted 38 lines · Matthew Brost

We create 1 GPU scheduler per entity in Xe, use DRM_SCHED_POLICY_SINGLE_ENTITY scheduling which is designed for that paradigm.

---
  drivers/gpu/drm/xe/xe_execlist.c   | 3 ++-
  drivers/gpu/drm/xe/xe_guc_submit.c | 3 +--
  2 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_execlist.c +2 -1
index 48060d14547a..79fb951c2965 100644
@@ -339,7 +339,8 @@ static int execlist_engine_init(struct xe_engine *e)
  	err = drm_sched_init(&exl->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     XE_SCHED_HANG_LIMIT, XE_SCHED_JOB_TIMEOUT,
-			     NULL, NULL, e->hwe->name, DRM_SCHED_POLICY_DEFAULT,
+			     NULL, NULL, e->hwe->name,
+			     DRM_SCHED_POLICY_SINGLE_ENTITY,
  			     gt_to_xe(e->gt)->drm.dev);
  	if (err)
  		goto err_free;
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
index 9d3fadca43be..68d09e7a4cc0 100644
@@ -1084,7 +1084,7 @@ static int guc_engine_init(struct xe_engine *e)
  	err = drm_sched_init(&ge->sched, &drm_sched_ops, NULL,
  			     e->lrc[0].ring.size / MAX_JOB_SIZE_BYTES,
  			     64, timeout, guc_to_gt(guc)->ordered_wq, NULL,
-			     e->name, DRM_SCHED_POLICY_DEFAULT,
+			     e->name, DRM_SCHED_POLICY_SINGLE_ENTITY,
  			     gt_to_xe(e->gt)->drm.dev);
  	if (err)
  		goto err_free;

drivers/gpu/drm/xe/xe_guc_submit.c static int guc_engine_set_priority(struct xe_engine *e, · line 1190

Quoted 7 lines
drivers/gpu/drm/xe/xe_guc_submit.c +0 -1
@@ -1185,7 +1185,6 @@ static int guc_engine_set_priority(struct xe_engine *e,
  	if (!msg)
  		return -ENOMEM;
 
-	drm_sched_entity_set_priority(e->entity, priority);
  	guc_engine_add_msg(e, msg, SET_SCHED_PROPS);
 
  	return 0;
author maintainer
#13 lore raw

PATCH v2 5/31 drm/xe: Long running job update

In reply to Matthew Brost

Flow control + write ring in exec, return NULL in run_job, siganl xe_hw_fence immediately, and override TDR for LR jobs.

---
 drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
 drivers/gpu/drm/xe/xe_engine.h           |  4 +
 drivers/gpu/drm/xe/xe_exec.c             |  8 ++
 drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_trace.h            |  5 ++
 6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +32 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
 #include "xe_macros.h"
 #include "xe_migrate.h"
 #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
 	up_write(&e->vm->lock);
 }
 
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
 /**
  * xe_engine_is_idle() - Whether an engine is idle.
  * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
 	return engine->width > 1;
 }
 
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
 bool xe_engine_is_idle(struct xe_engine *engine);
 
 void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
 #include "xe_device.h"
 #include "xe_engine.h"
 #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
 #include "xe_sched_job.h"
 #include "xe_sync.h"
 #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		goto err_engine_end;
 	}
 
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
 	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
 				  addresses : &args->address);
 	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		xe_sync_entry_signal(&syncs[i], job,
 				     &job->drm.s_fence->finished);
 
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
 	xe_sched_job_push(job);
 	xe_vm_reactivate_rebind(vm);
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
 	 */
 #define MAX_STATIC_MSG_TYPE	3
 	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
 	/** @fini_async: do final fini async from this worker */
 	struct work_struct fini_async;
 	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +86 -9

190 lines not on this page · raw message

drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
 	     TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
 DECLARE_EVENT_CLASS(xe_sched_job,
 		    TP_PROTO(struct xe_sched_job *job),
 		    TP_ARGS(job),
Signature · 1 line
2.34.1
#14 lore raw

Re: [Intel-xe] [PATCH v2 05/31] drm/xe: Long running job update

Quoted 1 line · Matthew Brost

Flow control + write ring in exec, return NULL in run_job, siganl

typo: s/siganl/signal

Quoted 1 line

xe_hw_fence immediately, and override TDR for LR jobs.

So, this would likely be the recommendation on how to deal with the lack of completion fence right?! Could you please put a more descriptive text that we could convert to a documentation later?

Quoted 27 lines
---
 drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
 drivers/gpu/drm/xe/xe_engine.h           |  4 +
 drivers/gpu/drm/xe/xe_exec.c             |  8 ++
 drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_trace.h            |  5 ++
 6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +1 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
 #include "xe_macros.h"
 #include "xe_migrate.h"
 #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
 	up_write(&e->vm->lock);
 }
 

drivers/gpu/drm/xe/xe_engine.c static void engine_kill_compute(struct xe_engine *e) · line 686

Quoted 10 lines
drivers/gpu/drm/xe/xe_engine.c +10 -0
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);

Why do we have this ENGINE_FLAG_VM here?

Quoted 312 lines
drivers/gpu/drm/xe/xe_engine.c +21 -0
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
 /**
  * xe_engine_is_idle() - Whether an engine is idle.
  * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
 	return engine->width > 1;
 }
 
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
 bool xe_engine_is_idle(struct xe_engine *engine);
 
 void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
 #include "xe_device.h"
 #include "xe_engine.h"
 #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
 #include "xe_sched_job.h"
 #include "xe_sync.h"
 #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		goto err_engine_end;
 	}
 
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
 	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
 				  addresses : &args->address);
 	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		xe_sync_entry_signal(&syncs[i], job,
 				     &job->drm.s_fence->finished);
 
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
 	xe_sched_job_push(job);
 	xe_vm_reactivate_rebind(vm);
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
 	 */
 #define MAX_STATIC_MSG_TYPE	3
 	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
 	/** @fini_async: do final fini async from this worker */
 	struct work_struct fini_async;
 	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +86 -9
index 68d09e7a4cc0..0a41f5d04f6d 100644
@@ -500,6 +500,14 @@ static void register_engine(struct xe_engine *e)
 		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
 	}
 
+	/*
+	 * We must keep a reference for LR engines if engine is registered with
+	 * the GuC as jobs signal immediately and can't destroy an engine if the
+	 * GuC has a reference to it.
+	 */
+	if (xe_engine_is_lr(e))
+		xe_engine_get(e);
+
 	set_engine_registered(e);
 	trace_xe_engine_register(e);
 	if (xe_engine_is_parallel(e))
@@ -662,6 +670,7 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
 {
 	struct xe_sched_job *job = to_xe_sched_job(drm_job);
 	struct xe_engine *e = job->engine;
+	bool lr = xe_engine_is_lr(e);
 
 	XE_BUG_ON((engine_destroyed(e) || engine_pending_disable(e)) &&
 		  !engine_banned(e) && !engine_suspended(e));
@@ -671,14 +680,19 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
 	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
 		if (!engine_registered(e))
 			register_engine(e);
-		e->ring_ops->emit_job(job);
+		if (!lr)	/* Written in IOCTL */
+			e->ring_ops->emit_job(job);
 		submit_engine(e);
 	}
 
-	if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags))
+	if (lr) {
+		xe_sched_job_set_error(job, -ENOTSUPP);
+		return NULL;
+	} else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
 		return job->fence;
-	else
+	} else {
 		return dma_fence_get(job->fence);
+	}
 }
 
 static void guc_engine_free_job(struct drm_sched_job *drm_job)
@@ -782,6 +796,57 @@ static void simple_error_capture(struct xe_engine *e)
 }
 #endif
 
+static void xe_guc_engine_trigger_cleanup(struct xe_engine *e)
+{
+	struct xe_guc *guc = engine_to_guc(e);
+
+	if (xe_engine_is_lr(e))
+		queue_work(guc_to_gt(guc)->ordered_wq, &e->guc->lr_tdr);
+	else
+		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+}
+
+static void xe_guc_engine_lr_cleanup(struct work_struct *w)
+{
+	struct xe_guc_engine *ge =
+		container_of(w, struct xe_guc_engine, lr_tdr);
+	struct xe_engine *e = ge->engine;
+	struct drm_gpu_scheduler *sched = &ge->sched;
+
+	XE_BUG_ON(!xe_engine_is_lr(e));
+	trace_xe_engine_lr_cleanup(e);
+
+	/* Kill the run_job / process_msg entry points */
+	drm_sched_run_wq_stop(sched);
+
+	/* Engine state now stable, disable scheduling / deregister if needed */
+	if (engine_registered(e)) {
+		struct xe_guc *guc = engine_to_guc(e);
+		int ret;
+
+		set_engine_banned(e);
+		xe_engine_get(e);
+		disable_scheduling_deregister(guc, e);
+
+		/*
+		 * Must wait for scheduling to be disabled before signalling
+		 * any fences, if GT broken the GT reset code should signal us.
+		 */
+		smp_rmb();
+		ret = wait_event_timeout(guc->ct.wq,
+					 !engine_pending_disable(e) ||
+					 guc_read_stopped(guc), HZ * 5);
+		if (!ret) {
+			XE_WARN_ON("Schedule disable failed to respond");
+			drm_sched_run_wq_start(sched);
+			xe_gt_reset_async(e->gt);
+			return;
+		}
+	}
+
+	drm_sched_run_wq_start(sched);
+}
+
 static enum drm_gpu_sched_stat
 guc_engine_timedout_job(struct drm_sched_job *drm_job)
 {
@@ -832,7 +897,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
 			err = -EIO;
 		set_engine_banned(e);
 		xe_engine_get(e);
-		disable_scheduling_deregister(engine_to_guc(e), e);
+		disable_scheduling_deregister(guc, e);
 
 		/*
 		 * Must wait for scheduling to be disabled before signalling
@@ -865,7 +930,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
 	 */
 	list_add(&drm_job->list, &sched->pending_list);
 	drm_sched_run_wq_start(sched);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
 
 	/* Mark all outstanding jobs as bad, thus completing them */
 	spin_lock(&sched->job_list_lock);
@@ -889,6 +954,8 @@ static void __guc_engine_fini_async(struct work_struct *w)
 
 	trace_xe_engine_destroy(e);
 
+	if (xe_engine_is_lr(e))
+		cancel_work_sync(&ge->lr_tdr);
 	if (e->flags & ENGINE_FLAG_PERSISTENT)
 		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
 	release_guc_id(guc, e);
@@ -906,7 +973,7 @@ static void guc_engine_fini_async(struct xe_engine *e)
 	bool kernel = e->flags & ENGINE_FLAG_KERNEL;
 
 	INIT_WORK(&e->guc->fini_async, __guc_engine_fini_async);
-	queue_work(system_unbound_wq, &e->guc->fini_async);
+	queue_work(system_wq, &e->guc->fini_async);
 
 	/* We must block on kernel engines so slabs are empty on driver unload */
 	if (kernel) {
@@ -1089,12 +1156,16 @@ static int guc_engine_init(struct xe_engine *e)
 	if (err)
 		goto err_free;
 
+
 	sched = &ge->sched;
 	err = drm_sched_entity_init(&ge->entity, DRM_SCHED_PRIORITY_NORMAL,
 				    &sched, 1, NULL);
 	if (err)
 		goto err_sched;
 
+	if (xe_engine_is_lr(e))
+		INIT_WORK(&e->guc->lr_tdr, xe_guc_engine_lr_cleanup);
+
 	mutex_lock(&guc->submission_state.lock);
 
 	err = alloc_guc_id(guc, e);
@@ -1146,7 +1217,7 @@ static void guc_engine_kill(struct xe_engine *e)
 {
 	trace_xe_engine_kill(e);
 	set_engine_killed(e);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
 }
 
 static void guc_engine_add_msg(struct xe_engine *e, struct drm_sched_msg *msg,
@@ -1296,6 +1367,9 @@ static void guc_engine_stop(struct xe_guc *guc, struct xe_engine *e)
 	/* Stop scheduling + flush any DRM scheduler operations */
 	drm_sched_run_wq_stop(sched);
 
+	if (engine_registered(e) && xe_engine_is_lr(e))
+		xe_engine_put(e);
+
 	/* Clean up lost G2H + reset engine state */
 	if (engine_destroyed(e) && engine_registered(e)) {
 		if (engine_banned(e))
@@ -1520,6 +1594,9 @@ int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
 	trace_xe_engine_deregister_done(e);
 
 	clear_engine_registered(e);
+	if (xe_engine_is_lr(e))
+		xe_engine_put(e);
+
 	if (engine_banned(e))
 		xe_engine_put(e);
 	else
@@ -1557,7 +1634,7 @@ int xe_guc_engine_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
 	 */
 	set_engine_reset(e);
 	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
 
 	return 0;
 }
@@ -1584,7 +1661,7 @@ int xe_guc_engine_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
 	/* Treat the same as engine reset */
 	set_engine_reset(e);
 	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
 
 	return 0;
 }
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
 	     TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
 DECLARE_EVENT_CLASS(xe_sched_job,
 		    TP_PROTO(struct xe_sched_job *job),
 		    TP_ARGS(job),
Signature · 1 line
2.34.1
author maintainer
#15 lore raw

Re: [Intel-xe] [PATCH v2 05/31] drm/xe: Long running job update

Quoted 5 lines · Rodrigo Vivi
Quoted 1 line · Matthew Brost

Flow control + write ring in exec, return NULL in run_job, siganl

typo: s/siganl/signal

Yep.

Quoted 6 lines
Quoted 1 line

xe_hw_fence immediately, and override TDR for LR jobs.

So, this would likely be the recommendation on how to deal with the lack of completion fence right?! Could you please put a more descriptive text that we could convert to a documentation later?

Sure, let write something a bit longer here. In my next drm scheduler series upstream I'll likely need a bit of DOC in drm scheduler anyways.

Matt

Quoted 352 lines
Quoted 27 lines
---
 drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
 drivers/gpu/drm/xe/xe_engine.h           |  4 +
 drivers/gpu/drm/xe/xe_exec.c             |  8 ++
 drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
 drivers/gpu/drm/xe/xe_trace.h            |  5 ++
 6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +1 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
 #include "xe_macros.h"
 #include "xe_migrate.h"
 #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
 	up_write(&e->vm->lock);
 }
 

drivers/gpu/drm/xe/xe_engine.c static void engine_kill_compute(struct xe_engine *e) · line 686

Quoted 10 lines
drivers/gpu/drm/xe/xe_engine.c +10 -0
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);

Why do we have this ENGINE_FLAG_VM here?

Quoted 312 lines
drivers/gpu/drm/xe/xe_engine.c +21 -0
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
 /**
  * xe_engine_is_idle() - Whether an engine is idle.
  * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
 	return engine->width > 1;
 }
 
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
 bool xe_engine_is_idle(struct xe_engine *engine);
 
 void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
 #include "xe_device.h"
 #include "xe_engine.h"
 #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
 #include "xe_sched_job.h"
 #include "xe_sync.h"
 #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		goto err_engine_end;
 	}
 
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
 	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
 				  addresses : &args->address);
 	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		xe_sync_entry_signal(&syncs[i], job,
 				     &job->drm.s_fence->finished);
 
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
 	xe_sched_job_push(job);
 	xe_vm_reactivate_rebind(vm);
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
 	 */
 #define MAX_STATIC_MSG_TYPE	3
 	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
 	/** @fini_async: do final fini async from this worker */
 	struct work_struct fini_async;
 	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +86 -9
index 68d09e7a4cc0..0a41f5d04f6d 100644
@@ -500,6 +500,14 @@ static void register_engine(struct xe_engine *e)
 		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
 	}
 
+	/*
+	 * We must keep a reference for LR engines if engine is registered with
+	 * the GuC as jobs signal immediately and can't destroy an engine if the
+	 * GuC has a reference to it.
+	 */
+	if (xe_engine_is_lr(e))
+		xe_engine_get(e);
+
 	set_engine_registered(e);
 	trace_xe_engine_register(e);
 	if (xe_engine_is_parallel(e))
@@ -662,6 +670,7 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
 {
 	struct xe_sched_job *job = to_xe_sched_job(drm_job);
 	struct xe_engine *e = job->engine;
+	bool lr = xe_engine_is_lr(e);
 
 	XE_BUG_ON((engine_destroyed(e) || engine_pending_disable(e)) &&
 		  !engine_banned(e) && !engine_suspended(e));
@@ -671,14 +680,19 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
 	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
 		if (!engine_registered(e))
 			register_engine(e);
-		e->ring_ops->emit_job(job);
+		if (!lr)	/* Written in IOCTL */
+			e->ring_ops->emit_job(job);
 		submit_engine(e);
 	}
 
-	if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags))
+	if (lr) {
+		xe_sched_job_set_error(job, -ENOTSUPP);
+		return NULL;
+	} else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
 		return job->fence;
-	else
+	} else {
 		return dma_fence_get(job->fence);
+	}
 }
 
 static void guc_engine_free_job(struct drm_sched_job *drm_job)
@@ -782,6 +796,57 @@ static void simple_error_capture(struct xe_engine *e)
 }
 #endif
 
+static void xe_guc_engine_trigger_cleanup(struct xe_engine *e)
+{
+	struct xe_guc *guc = engine_to_guc(e);
+
+	if (xe_engine_is_lr(e))
+		queue_work(guc_to_gt(guc)->ordered_wq, &e->guc->lr_tdr);
+	else
+		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+}
+
+static void xe_guc_engine_lr_cleanup(struct work_struct *w)
+{
+	struct xe_guc_engine *ge =
+		container_of(w, struct xe_guc_engine, lr_tdr);
+	struct xe_engine *e = ge->engine;
+	struct drm_gpu_scheduler *sched = &ge->sched;
+
+	XE_BUG_ON(!xe_engine_is_lr(e));
+	trace_xe_engine_lr_cleanup(e);
+
+	/* Kill the run_job / process_msg entry points */
+	drm_sched_run_wq_stop(sched);
+
+	/* Engine state now stable, disable scheduling / deregister if needed */
+	if (engine_registered(e)) {
+		struct xe_guc *guc = engine_to_guc(e);
+		int ret;
+
+		set_engine_banned(e);
+		xe_engine_get(e);
+		disable_scheduling_deregister(guc, e);
+
+		/*
+		 * Must wait for scheduling to be disabled before signalling
+		 * any fences, if GT broken the GT reset code should signal us.
+		 */
+		smp_rmb();
+		ret = wait_event_timeout(guc->ct.wq,
+					 !engine_pending_disable(e) ||
+					 guc_read_stopped(guc), HZ * 5);
+		if (!ret) {
+			XE_WARN_ON("Schedule disable failed to respond");
+			drm_sched_run_wq_start(sched);
+			xe_gt_reset_async(e->gt);
+			return;
+		}
+	}
+
+	drm_sched_run_wq_start(sched);
+}
+
 static enum drm_gpu_sched_stat
 guc_engine_timedout_job(struct drm_sched_job *drm_job)
 {
@@ -832,7 +897,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
 			err = -EIO;
 		set_engine_banned(e);
 		xe_engine_get(e);
-		disable_scheduling_deregister(engine_to_guc(e), e);
+		disable_scheduling_deregister(guc, e);
 
 		/*
 		 * Must wait for scheduling to be disabled before signalling
@@ -865,7 +930,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
 	 */
 	list_add(&drm_job->list, &sched->pending_list);
 	drm_sched_run_wq_start(sched);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
 
 	/* Mark all outstanding jobs as bad, thus completing them */
 	spin_lock(&sched->job_list_lock);
@@ -889,6 +954,8 @@ static void __guc_engine_fini_async(struct work_struct *w)
 
 	trace_xe_engine_destroy(e);
 
+	if (xe_engine_is_lr(e))
+		cancel_work_sync(&ge->lr_tdr);
 	if (e->flags & ENGINE_FLAG_PERSISTENT)
 		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
 	release_guc_id(guc, e);
@@ -906,7 +973,7 @@ static void guc_engine_fini_async(struct xe_engine *e)
 	bool kernel = e->flags & ENGINE_FLAG_KERNEL;
 
 	INIT_WORK(&e->guc->fini_async, __guc_engine_fini_async);
-	queue_work(system_unbound_wq, &e->guc->fini_async);
+	queue_work(system_wq, &e->guc->fini_async);
 
 	/* We must block on kernel engines so slabs are empty on driver unload */
 	if (kernel) {
@@ -1089,12 +1156,16 @@ static int guc_engine_init(struct xe_engine *e)
 	if (err)
 		goto err_free;
 
+
 	sched = &ge->sched;
 	err = drm_sched_entity_init(&ge->entity, DRM_SCHED_PRIORITY_NORMAL,
 				    &sched, 1, NULL);
 	if (err)
 		goto err_sched;
 
+	if (xe_engine_is_lr(e))
+		INIT_WORK(&e->guc->lr_tdr, xe_guc_engine_lr_cleanup);
+
 	mutex_lock(&guc->submission_state.lock);
 
 	err = alloc_guc_id(guc, e);
@@ -1146,7 +1217,7 @@ static void guc_engine_kill(struct xe_engine *e)
 {
 	trace_xe_engine_kill(e);
 	set_engine_killed(e);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
 }
 
 static void guc_engine_add_msg(struct xe_engine *e, struct drm_sched_msg *msg,
@@ -1296,6 +1367,9 @@ static void guc_engine_stop(struct xe_guc *guc, struct xe_engine *e)
 	/* Stop scheduling + flush any DRM scheduler operations */
 	drm_sched_run_wq_stop(sched);
 
+	if (engine_registered(e) && xe_engine_is_lr(e))
+		xe_engine_put(e);
+
 	/* Clean up lost G2H + reset engine state */
 	if (engine_destroyed(e) && engine_registered(e)) {
 		if (engine_banned(e))
@@ -1520,6 +1594,9 @@ int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
 	trace_xe_engine_deregister_done(e);
 
 	clear_engine_registered(e);
+	if (xe_engine_is_lr(e))
+		xe_engine_put(e);
+
 	if (engine_banned(e))
 		xe_engine_put(e);
 	else
@@ -1557,7 +1634,7 @@ int xe_guc_engine_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
 	 */
 	set_engine_reset(e);
 	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
 
 	return 0;
 }
@@ -1584,7 +1661,7 @@ int xe_guc_engine_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
 	/* Treat the same as engine reset */
 	set_engine_reset(e);
 	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
 
 	return 0;
 }
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
 	     TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
 DECLARE_EVENT_CLASS(xe_sched_job,
 		    TP_PROTO(struct xe_sched_job *job),
 		    TP_ARGS(job),
Signature · 1 line
2.34.1
#16 lore raw

Re: [Intel-xe] [PATCH v2 05/31] drm/xe: Long running job update

In reply to Matthew Brost

Hi, Matthew

In addition to Rodrigo's comments:

Quoted 152 lines · Matthew Brost

Flow control + write ring in exec, return NULL in run_job, siganl xe_hw_fence immediately, and override TDR for LR jobs.

---
  drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
  drivers/gpu/drm/xe/xe_engine.h           |  4 +
  drivers/gpu/drm/xe/xe_exec.c             |  8 ++
  drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
  drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
  drivers/gpu/drm/xe/xe_trace.h            |  5 ++
  6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +32 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
  #include "xe_macros.h"
  #include "xe_migrate.h"
  #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
  #include "xe_trace.h"
  #include "xe_vm.h"
 
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
  	up_write(&e->vm->lock);
  }
 
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
  /**
   * xe_engine_is_idle() - Whether an engine is idle.
   * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
  	return engine->width > 1;
  }
 
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
  bool xe_engine_is_idle(struct xe_engine *engine);
 
  void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
  #include "xe_device.h"
  #include "xe_engine.h"
  #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
  #include "xe_sched_job.h"
  #include "xe_sync.h"
  #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  		goto err_engine_end;
  	}
 
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
  	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
  				  addresses : &args->address);
  	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  		xe_sync_entry_signal(&syncs[i], job,
  				     &job->drm.s_fence->finished);
 
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
  	xe_sched_job_push(job);
  	xe_vm_reactivate_rebind(vm);
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
  	 */
  #define MAX_STATIC_MSG_TYPE	3
  	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
  	/** @fini_async: do final fini async from this worker */
  	struct work_struct fini_async;
  	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +9 -0
index 68d09e7a4cc0..0a41f5d04f6d 100644
@@ -500,6 +500,14 @@ static void register_engine(struct xe_engine *e)
  		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
  	}
 
+	/*
+	 * We must keep a reference for LR engines if engine is registered with
+	 * the GuC as jobs signal immediately and can't destroy an engine if the
+	 * GuC has a reference to it.
+	 */
+	if (xe_engine_is_lr(e))
+		xe_engine_get(e);
+
  	set_engine_registered(e);
  	trace_xe_engine_register(e);
  	if (xe_engine_is_parallel(e))
@@ -662,6 +670,7 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
  {
  	struct xe_sched_job *job = to_xe_sched_job(drm_job);
  	struct xe_engine *e = job->engine;
+	bool lr = xe_engine_is_lr(e);
 
  	XE_BUG_ON((engine_destroyed(e) || engine_pending_disable(e)) &&
  		  !engine_banned(e) && !engine_suspended(e));

drivers/gpu/drm/xe/xe_guc_submit.c guc_engine_run_job(struct drm_sched_job *drm_job) · line 683

Quoted 5 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
@@ -671,14 +680,19 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
  	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
  		if (!engine_registered(e))
  			register_engine(e);
-		e->ring_ops->emit_job(job);
+		if (!lr)	/* Written in IOCTL */

Hmm? What does "Written in IOCTL mean?" Could you rephrase to something more descriptive?

Quoted 48 lines
drivers/gpu/drm/xe/xe_guc_submit.c +34 -2
+			e->ring_ops->emit_job(job);
  		submit_engine(e);
  	}
 
-	if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags))
+	if (lr) {
+		xe_sched_job_set_error(job, -ENOTSUPP);
+		return NULL;
+	} else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
  		return job->fence;
-	else
+	} else {
  		return dma_fence_get(job->fence);
+	}
  }
 
  static void guc_engine_free_job(struct drm_sched_job *drm_job)
@@ -782,6 +796,57 @@ static void simple_error_capture(struct xe_engine *e)
  }
  #endif
 
+static void xe_guc_engine_trigger_cleanup(struct xe_engine *e)
+{
+	struct xe_guc *guc = engine_to_guc(e);
+
+	if (xe_engine_is_lr(e))
+		queue_work(guc_to_gt(guc)->ordered_wq, &e->guc->lr_tdr);
+	else
+		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+}
+
+static void xe_guc_engine_lr_cleanup(struct work_struct *w)
+{
+	struct xe_guc_engine *ge =
+		container_of(w, struct xe_guc_engine, lr_tdr);
+	struct xe_engine *e = ge->engine;
+	struct drm_gpu_scheduler *sched = &ge->sched;
+
+	XE_BUG_ON(!xe_engine_is_lr(e));
+	trace_xe_engine_lr_cleanup(e);
+
+	/* Kill the run_job / process_msg entry points */
+	drm_sched_run_wq_stop(sched);
+
+	/* Engine state now stable, disable scheduling / deregister if needed */
+	if (engine_registered(e)) {
+		struct xe_guc *guc = engine_to_guc(e);
+		int ret;

drivers/gpu/drm/xe/xe_guc_submit.c static void simple_error_capture(struct xe_engine *e) · line 835

Quoted 10 lines
drivers/gpu/drm/xe/xe_guc_submit.c +10 -0
+
+		set_engine_banned(e);
+		xe_engine_get(e);
+		disable_scheduling_deregister(guc, e);
+
+		/*
+		 * Must wait for scheduling to be disabled before signalling
+		 * any fences, if GT broken the GT reset code should signal us.
+		 */
+		smp_rmb();

wait_event() paired with wake_up() family of functions typically set the necessary barriers to make sure anything written prior to wake_up() is seen in wait_event(). So that smp_rmb() is most likely not needed. If it still is, its pairing smp_wmb() should be documented and pointed to as well. See documentation of set_current_state() vs __set_current_state().

Quoted 54 lines
drivers/gpu/drm/xe/xe_guc_submit.c +19 -3
+		ret = wait_event_timeout(guc->ct.wq,
+					 !engine_pending_disable(e) ||
+					 guc_read_stopped(guc), HZ * 5);
+		if (!ret) {
+			XE_WARN_ON("Schedule disable failed to respond");
+			drm_sched_run_wq_start(sched);
+			xe_gt_reset_async(e->gt);
+			return;
+		}
+	}
+
+	drm_sched_run_wq_start(sched);
+}
+
  static enum drm_gpu_sched_stat
  guc_engine_timedout_job(struct drm_sched_job *drm_job)
  {
@@ -832,7 +897,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
  			err = -EIO;
  		set_engine_banned(e);
  		xe_engine_get(e);
-		disable_scheduling_deregister(engine_to_guc(e), e);
+		disable_scheduling_deregister(guc, e);
 
  		/*
  		 * Must wait for scheduling to be disabled before signalling
@@ -865,7 +930,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
  	 */
  	list_add(&drm_job->list, &sched->pending_list);
  	drm_sched_run_wq_start(sched);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
 
  	/* Mark all outstanding jobs as bad, thus completing them */
  	spin_lock(&sched->job_list_lock);
@@ -889,6 +954,8 @@ static void __guc_engine_fini_async(struct work_struct *w)
 
  	trace_xe_engine_destroy(e);
 
+	if (xe_engine_is_lr(e))
+		cancel_work_sync(&ge->lr_tdr);
  	if (e->flags & ENGINE_FLAG_PERSISTENT)
  		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
  	release_guc_id(guc, e);
@@ -906,7 +973,7 @@ static void guc_engine_fini_async(struct xe_engine *e)
  	bool kernel = e->flags & ENGINE_FLAG_KERNEL;
 
  	INIT_WORK(&e->guc->fini_async, __guc_engine_fini_async);
-	queue_work(system_unbound_wq, &e->guc->fini_async);
+	queue_work(system_wq, &e->guc->fini_async);
 
  	/* We must block on kernel engines so slabs are empty on driver unload */
  	if (kernel) {

drivers/gpu/drm/xe/xe_guc_submit.c static int guc_engine_init(struct xe_engine *e) · line 1159

Quoted 4 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -0
@@ -1089,12 +1156,16 @@ static int guc_engine_init(struct xe_engine *e)
  	if (err)
  		goto err_free;
 
+

Unrelated whitespace?

Quoted 75 lines
drivers/gpu/drm/xe/xe_guc_submit.c +12 -3
  	sched = &ge->sched;
  	err = drm_sched_entity_init(&ge->entity, DRM_SCHED_PRIORITY_NORMAL,
  				    &sched, 1, NULL);
  	if (err)
  		goto err_sched;
 
+	if (xe_engine_is_lr(e))
+		INIT_WORK(&e->guc->lr_tdr, xe_guc_engine_lr_cleanup);
+
  	mutex_lock(&guc->submission_state.lock);
 
  	err = alloc_guc_id(guc, e);
@@ -1146,7 +1217,7 @@ static void guc_engine_kill(struct xe_engine *e)
  {
  	trace_xe_engine_kill(e);
  	set_engine_killed(e);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
  }
 
  static void guc_engine_add_msg(struct xe_engine *e, struct drm_sched_msg *msg,
@@ -1296,6 +1367,9 @@ static void guc_engine_stop(struct xe_guc *guc, struct xe_engine *e)
  	/* Stop scheduling + flush any DRM scheduler operations */
  	drm_sched_run_wq_stop(sched);
 
+	if (engine_registered(e) && xe_engine_is_lr(e))
+		xe_engine_put(e);
+
  	/* Clean up lost G2H + reset engine state */
  	if (engine_destroyed(e) && engine_registered(e)) {
  		if (engine_banned(e))
@@ -1520,6 +1594,9 @@ int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
  	trace_xe_engine_deregister_done(e);
 
  	clear_engine_registered(e);
+	if (xe_engine_is_lr(e))
+		xe_engine_put(e);
+
  	if (engine_banned(e))
  		xe_engine_put(e);
  	else
@@ -1557,7 +1634,7 @@ int xe_guc_engine_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
  	 */
  	set_engine_reset(e);
  	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
 
  	return 0;
  }
@@ -1584,7 +1661,7 @@ int xe_guc_engine_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
  	/* Treat the same as engine reset */
  	set_engine_reset(e);
  	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
 
  	return 0;
  }
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
  	     TP_ARGS(e)
  );
 
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
  DECLARE_EVENT_CLASS(xe_sched_job,
  		    TP_PROTO(struct xe_sched_job *job),
  		    TP_ARGS(job),
author maintainer
#17 lore raw

Re: [Intel-xe] [PATCH v2 05/31] drm/xe: Long running job update

Quoted 148 lines · Thomas Hellström

Hi, Matthew

In addition to Rodrigo's comments:

Quoted 143 lines · Matthew Brost

Flow control + write ring in exec, return NULL in run_job, siganl xe_hw_fence immediately, and override TDR for LR jobs.

---
  drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
  drivers/gpu/drm/xe/xe_engine.h           |  4 +
  drivers/gpu/drm/xe/xe_exec.c             |  8 ++
  drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
  drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
  drivers/gpu/drm/xe/xe_trace.h            |  5 ++
  6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +32 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
  #include "xe_macros.h"
  #include "xe_migrate.h"
  #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
  #include "xe_trace.h"
  #include "xe_vm.h"
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
  	up_write(&e->vm->lock);
  }
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
  /**
   * xe_engine_is_idle() - Whether an engine is idle.
   * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
  	return engine->width > 1;
  }
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
  bool xe_engine_is_idle(struct xe_engine *engine);
  void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
  #include "xe_device.h"
  #include "xe_engine.h"
  #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
  #include "xe_sched_job.h"
  #include "xe_sync.h"
  #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  		goto err_engine_end;
  	}
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
  	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
  				  addresses : &args->address);
  	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  		xe_sync_entry_signal(&syncs[i], job,
  				     &job->drm.s_fence->finished);
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
  	xe_sched_job_push(job);
  	xe_vm_reactivate_rebind(vm);
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
  	 */
  #define MAX_STATIC_MSG_TYPE	3
  	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
  	/** @fini_async: do final fini async from this worker */
  	struct work_struct fini_async;
  	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +9 -0
index 68d09e7a4cc0..0a41f5d04f6d 100644
@@ -500,6 +500,14 @@ static void register_engine(struct xe_engine *e)
  		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
  	}
+	/*
+	 * We must keep a reference for LR engines if engine is registered with
+	 * the GuC as jobs signal immediately and can't destroy an engine if the
+	 * GuC has a reference to it.
+	 */
+	if (xe_engine_is_lr(e))
+		xe_engine_get(e);
+
  	set_engine_registered(e);
  	trace_xe_engine_register(e);
  	if (xe_engine_is_parallel(e))
@@ -662,6 +670,7 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
  {
  	struct xe_sched_job *job = to_xe_sched_job(drm_job);
  	struct xe_engine *e = job->engine;
+	bool lr = xe_engine_is_lr(e);
  	XE_BUG_ON((engine_destroyed(e) || engine_pending_disable(e)) &&
  		  !engine_banned(e) && !engine_suspended(e));
@@ -671,14 +680,19 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
Quoted 9 lines

drivers/gpu/drm/xe/xe_guc_submit.c guc_engine_run_job(struct drm_sched_job *drm_job) · line 683

Quoted 5 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
  	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
  		if (!engine_registered(e))
  			register_engine(e);
-		e->ring_ops->emit_job(job);
+		if (!lr)	/* Written in IOCTL */

Hmm? What does "Written in IOCTL mean?" Could you rephrase to something more descriptive?

"LR jos are emitted in the IOCTL"

Does that work?

Matt

Quoted 185 lines
Quoted 45 lines
drivers/gpu/drm/xe/xe_guc_submit.c +34 -2
+			e->ring_ops->emit_job(job);
  		submit_engine(e);
  	}
-	if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags))
+	if (lr) {
+		xe_sched_job_set_error(job, -ENOTSUPP);
+		return NULL;
+	} else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
  		return job->fence;
-	else
+	} else {
  		return dma_fence_get(job->fence);
+	}
  }
  static void guc_engine_free_job(struct drm_sched_job *drm_job)
@@ -782,6 +796,57 @@ static void simple_error_capture(struct xe_engine *e)
  }
  #endif
+static void xe_guc_engine_trigger_cleanup(struct xe_engine *e)
+{
+	struct xe_guc *guc = engine_to_guc(e);
+
+	if (xe_engine_is_lr(e))
+		queue_work(guc_to_gt(guc)->ordered_wq, &e->guc->lr_tdr);
+	else
+		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+}
+
+static void xe_guc_engine_lr_cleanup(struct work_struct *w)
+{
+	struct xe_guc_engine *ge =
+		container_of(w, struct xe_guc_engine, lr_tdr);
+	struct xe_engine *e = ge->engine;
+	struct drm_gpu_scheduler *sched = &ge->sched;
+
+	XE_BUG_ON(!xe_engine_is_lr(e));
+	trace_xe_engine_lr_cleanup(e);
+
+	/* Kill the run_job / process_msg entry points */
+	drm_sched_run_wq_stop(sched);
+
+	/* Engine state now stable, disable scheduling / deregister if needed */
+	if (engine_registered(e)) {
+		struct xe_guc *guc = engine_to_guc(e);
+		int ret;

drivers/gpu/drm/xe/xe_guc_submit.c static void simple_error_capture(struct xe_engine *e) · line 834

Quoted 10 lines
drivers/gpu/drm/xe/xe_guc_submit.c +10 -0
+
+		set_engine_banned(e);
+		xe_engine_get(e);
+		disable_scheduling_deregister(guc, e);
+
+		/*
+		 * Must wait for scheduling to be disabled before signalling
+		 * any fences, if GT broken the GT reset code should signal us.
+		 */
+		smp_rmb();

wait_event() paired with wake_up() family of functions typically set the necessary barriers to make sure anything written prior to wake_up() is seen in wait_event(). So that smp_rmb() is most likely not needed. If it still is, its pairing smp_wmb() should be documented and pointed to as well. See documentation of set_current_state() vs __set_current_state().

Quoted 48 lines
drivers/gpu/drm/xe/xe_guc_submit.c +19 -3
+		ret = wait_event_timeout(guc->ct.wq,
+					 !engine_pending_disable(e) ||
+					 guc_read_stopped(guc), HZ * 5);
+		if (!ret) {
+			XE_WARN_ON("Schedule disable failed to respond");
+			drm_sched_run_wq_start(sched);
+			xe_gt_reset_async(e->gt);
+			return;
+		}
+	}
+
+	drm_sched_run_wq_start(sched);
+}
+
  static enum drm_gpu_sched_stat
  guc_engine_timedout_job(struct drm_sched_job *drm_job)
  {
@@ -832,7 +897,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
  			err = -EIO;
  		set_engine_banned(e);
  		xe_engine_get(e);
-		disable_scheduling_deregister(engine_to_guc(e), e);
+		disable_scheduling_deregister(guc, e);
  		/*
  		 * Must wait for scheduling to be disabled before signalling
@@ -865,7 +930,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
  	 */
  	list_add(&drm_job->list, &sched->pending_list);
  	drm_sched_run_wq_start(sched);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
  	/* Mark all outstanding jobs as bad, thus completing them */
  	spin_lock(&sched->job_list_lock);
@@ -889,6 +954,8 @@ static void __guc_engine_fini_async(struct work_struct *w)
  	trace_xe_engine_destroy(e);
+	if (xe_engine_is_lr(e))
+		cancel_work_sync(&ge->lr_tdr);
  	if (e->flags & ENGINE_FLAG_PERSISTENT)
  		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
  	release_guc_id(guc, e);
@@ -906,7 +973,7 @@ static void guc_engine_fini_async(struct xe_engine *e)
  	bool kernel = e->flags & ENGINE_FLAG_KERNEL;
  	INIT_WORK(&e->guc->fini_async, __guc_engine_fini_async);
-	queue_work(system_unbound_wq, &e->guc->fini_async);
+	queue_work(system_wq, &e->guc->fini_async);
  	/* We must block on kernel engines so slabs are empty on driver unload */
  	if (kernel) {

drivers/gpu/drm/xe/xe_guc_submit.c static int guc_engine_init(struct xe_engine *e) · line 1158

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -0
@@ -1089,12 +1156,16 @@ static int guc_engine_init(struct xe_engine *e)
  	if (err)
  		goto err_free;
+

Unrelated whitespace?

Quoted 67 lines
drivers/gpu/drm/xe/xe_guc_submit.c +12 -3
  	sched = &ge->sched;
  	err = drm_sched_entity_init(&ge->entity, DRM_SCHED_PRIORITY_NORMAL,
  				    &sched, 1, NULL);
  	if (err)
  		goto err_sched;
+	if (xe_engine_is_lr(e))
+		INIT_WORK(&e->guc->lr_tdr, xe_guc_engine_lr_cleanup);
+
  	mutex_lock(&guc->submission_state.lock);
  	err = alloc_guc_id(guc, e);
@@ -1146,7 +1217,7 @@ static void guc_engine_kill(struct xe_engine *e)
  {
  	trace_xe_engine_kill(e);
  	set_engine_killed(e);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
  }
  static void guc_engine_add_msg(struct xe_engine *e, struct drm_sched_msg *msg,
@@ -1296,6 +1367,9 @@ static void guc_engine_stop(struct xe_guc *guc, struct xe_engine *e)
  	/* Stop scheduling + flush any DRM scheduler operations */
  	drm_sched_run_wq_stop(sched);
+	if (engine_registered(e) && xe_engine_is_lr(e))
+		xe_engine_put(e);
+
  	/* Clean up lost G2H + reset engine state */
  	if (engine_destroyed(e) && engine_registered(e)) {
  		if (engine_banned(e))
@@ -1520,6 +1594,9 @@ int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
  	trace_xe_engine_deregister_done(e);
  	clear_engine_registered(e);
+	if (xe_engine_is_lr(e))
+		xe_engine_put(e);
+
  	if (engine_banned(e))
  		xe_engine_put(e);
  	else
@@ -1557,7 +1634,7 @@ int xe_guc_engine_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
  	 */
  	set_engine_reset(e);
  	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
  	return 0;
  }
@@ -1584,7 +1661,7 @@ int xe_guc_engine_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
  	/* Treat the same as engine reset */
  	set_engine_reset(e);
  	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
  	return 0;
  }
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
  	     TP_ARGS(e)
  );
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
  DECLARE_EVENT_CLASS(xe_sched_job,
  		    TP_PROTO(struct xe_sched_job *job),
  		    TP_ARGS(job),
#18 lore raw

Re: [Intel-xe] [PATCH v2 05/31] drm/xe: Long running job update

Quoted 157 lines · Matthew Brost
Quoted 147 lines · Thomas Hellström

Hi, Matthew

In addition to Rodrigo's comments:

Quoted 142 lines · Matthew Brost

Flow control + write ring in exec, return NULL in run_job, siganl xe_hw_fence immediately, and override TDR for LR jobs.

---
   drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
   drivers/gpu/drm/xe/xe_engine.h           |  4 +
   drivers/gpu/drm/xe/xe_exec.c             |  8 ++
   drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
   drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
   drivers/gpu/drm/xe/xe_trace.h            |  5 ++
   6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +32 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
   #include "xe_macros.h"
   #include "xe_migrate.h"
   #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
   #include "xe_trace.h"
   #include "xe_vm.h"
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
   	up_write(&e->vm->lock);
   }
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
   /**
    * xe_engine_is_idle() - Whether an engine is idle.
    * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
   	return engine->width > 1;
   }
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
   bool xe_engine_is_idle(struct xe_engine *engine);
   void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
   #include "xe_device.h"
   #include "xe_engine.h"
   #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
   #include "xe_sched_job.h"
   #include "xe_sync.h"
   #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
   		goto err_engine_end;
   	}
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
   	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
   				  addresses : &args->address);
   	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
   		xe_sync_entry_signal(&syncs[i], job,
   				     &job->drm.s_fence->finished);
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
   	xe_sched_job_push(job);
   	xe_vm_reactivate_rebind(vm);
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
   	 */
   #define MAX_STATIC_MSG_TYPE	3
   	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
   	/** @fini_async: do final fini async from this worker */
   	struct work_struct fini_async;
   	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +9 -0
index 68d09e7a4cc0..0a41f5d04f6d 100644
@@ -500,6 +500,14 @@ static void register_engine(struct xe_engine *e)
   		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
   	}
+	/*
+	 * We must keep a reference for LR engines if engine is registered with
+	 * the GuC as jobs signal immediately and can't destroy an engine if the
+	 * GuC has a reference to it.
+	 */
+	if (xe_engine_is_lr(e))
+		xe_engine_get(e);
+
   	set_engine_registered(e);
   	trace_xe_engine_register(e);
   	if (xe_engine_is_parallel(e))
@@ -662,6 +670,7 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
   {
   	struct xe_sched_job *job = to_xe_sched_job(drm_job);
   	struct xe_engine *e = job->engine;
+	bool lr = xe_engine_is_lr(e);
   	XE_BUG_ON((engine_destroyed(e) || engine_pending_disable(e)) &&
   		  !engine_banned(e) && !engine_suspended(e));
Quoted 9 lines

drivers/gpu/drm/xe/xe_guc_submit.c guc_engine_run_job(struct drm_sched_job *drm_job) · line 683

Quoted 6 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
@@ -671,14 +680,19 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
   	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
   		if (!engine_registered(e))
   			register_engine(e);
-		e->ring_ops->emit_job(job);
+		if (!lr)	/* Written in IOCTL */

Hmm? What does "Written in IOCTL mean?" Could you rephrase to something more descriptive?

Quoted 1 line

"LR jos are emitted in the IOCTL"

Ah, I read it as "the lr variable was written in IOCTL."

Perhaps LR jobs are already emitted at execbuf time?

/Thomas

Quoted 187 lines

Does that work?

Matt

Quoted 182 lines
Quoted 45 lines
drivers/gpu/drm/xe/xe_guc_submit.c +34 -2
+			e->ring_ops->emit_job(job);
   		submit_engine(e);
   	}
-	if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags))
+	if (lr) {
+		xe_sched_job_set_error(job, -ENOTSUPP);
+		return NULL;
+	} else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
   		return job->fence;
-	else
+	} else {
   		return dma_fence_get(job->fence);
+	}
   }
   static void guc_engine_free_job(struct drm_sched_job *drm_job)
@@ -782,6 +796,57 @@ static void simple_error_capture(struct xe_engine *e)
   }
   #endif
+static void xe_guc_engine_trigger_cleanup(struct xe_engine *e)
+{
+	struct xe_guc *guc = engine_to_guc(e);
+
+	if (xe_engine_is_lr(e))
+		queue_work(guc_to_gt(guc)->ordered_wq, &e->guc->lr_tdr);
+	else
+		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+}
+
+static void xe_guc_engine_lr_cleanup(struct work_struct *w)
+{
+	struct xe_guc_engine *ge =
+		container_of(w, struct xe_guc_engine, lr_tdr);
+	struct xe_engine *e = ge->engine;
+	struct drm_gpu_scheduler *sched = &ge->sched;
+
+	XE_BUG_ON(!xe_engine_is_lr(e));
+	trace_xe_engine_lr_cleanup(e);
+
+	/* Kill the run_job / process_msg entry points */
+	drm_sched_run_wq_stop(sched);
+
+	/* Engine state now stable, disable scheduling / deregister if needed */
+	if (engine_registered(e)) {
+		struct xe_guc *guc = engine_to_guc(e);
+		int ret;

drivers/gpu/drm/xe/xe_guc_submit.c static void simple_error_capture(struct xe_engine *e) · line 834

Quoted 10 lines
drivers/gpu/drm/xe/xe_guc_submit.c +10 -0
+
+		set_engine_banned(e);
+		xe_engine_get(e);
+		disable_scheduling_deregister(guc, e);
+
+		/*
+		 * Must wait for scheduling to be disabled before signalling
+		 * any fences, if GT broken the GT reset code should signal us.
+		 */
+		smp_rmb();

wait_event() paired with wake_up() family of functions typically set the necessary barriers to make sure anything written prior to wake_up() is seen in wait_event(). So that smp_rmb() is most likely not needed. If it still is, its pairing smp_wmb() should be documented and pointed to as well. See documentation of set_current_state() vs __set_current_state().

Quoted 48 lines
drivers/gpu/drm/xe/xe_guc_submit.c +19 -3
+		ret = wait_event_timeout(guc->ct.wq,
+					 !engine_pending_disable(e) ||
+					 guc_read_stopped(guc), HZ * 5);
+		if (!ret) {
+			XE_WARN_ON("Schedule disable failed to respond");
+			drm_sched_run_wq_start(sched);
+			xe_gt_reset_async(e->gt);
+			return;
+		}
+	}
+
+	drm_sched_run_wq_start(sched);
+}
+
   static enum drm_gpu_sched_stat
   guc_engine_timedout_job(struct drm_sched_job *drm_job)
   {
@@ -832,7 +897,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
   			err = -EIO;
   		set_engine_banned(e);
   		xe_engine_get(e);
-		disable_scheduling_deregister(engine_to_guc(e), e);
+		disable_scheduling_deregister(guc, e);
   		/*
   		 * Must wait for scheduling to be disabled before signalling
@@ -865,7 +930,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
   	 */
   	list_add(&drm_job->list, &sched->pending_list);
   	drm_sched_run_wq_start(sched);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
   	/* Mark all outstanding jobs as bad, thus completing them */
   	spin_lock(&sched->job_list_lock);
@@ -889,6 +954,8 @@ static void __guc_engine_fini_async(struct work_struct *w)
   	trace_xe_engine_destroy(e);
+	if (xe_engine_is_lr(e))
+		cancel_work_sync(&ge->lr_tdr);
   	if (e->flags & ENGINE_FLAG_PERSISTENT)
   		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
   	release_guc_id(guc, e);
@@ -906,7 +973,7 @@ static void guc_engine_fini_async(struct xe_engine *e)
   	bool kernel = e->flags & ENGINE_FLAG_KERNEL;
   	INIT_WORK(&e->guc->fini_async, __guc_engine_fini_async);
-	queue_work(system_unbound_wq, &e->guc->fini_async);
+	queue_work(system_wq, &e->guc->fini_async);
   	/* We must block on kernel engines so slabs are empty on driver unload */
   	if (kernel) {

drivers/gpu/drm/xe/xe_guc_submit.c static int guc_engine_init(struct xe_engine *e) · line 1158

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -0
@@ -1089,12 +1156,16 @@ static int guc_engine_init(struct xe_engine *e)
   	if (err)
   		goto err_free;
+

Unrelated whitespace?

Quoted 67 lines
drivers/gpu/drm/xe/xe_guc_submit.c +12 -3
   	sched = &ge->sched;
   	err = drm_sched_entity_init(&ge->entity, DRM_SCHED_PRIORITY_NORMAL,
   				    &sched, 1, NULL);
   	if (err)
   		goto err_sched;
+	if (xe_engine_is_lr(e))
+		INIT_WORK(&e->guc->lr_tdr, xe_guc_engine_lr_cleanup);
+
   	mutex_lock(&guc->submission_state.lock);
   	err = alloc_guc_id(guc, e);
@@ -1146,7 +1217,7 @@ static void guc_engine_kill(struct xe_engine *e)
   {
   	trace_xe_engine_kill(e);
   	set_engine_killed(e);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
   }
   static void guc_engine_add_msg(struct xe_engine *e, struct drm_sched_msg *msg,
@@ -1296,6 +1367,9 @@ static void guc_engine_stop(struct xe_guc *guc, struct xe_engine *e)
   	/* Stop scheduling + flush any DRM scheduler operations */
   	drm_sched_run_wq_stop(sched);
+	if (engine_registered(e) && xe_engine_is_lr(e))
+		xe_engine_put(e);
+
   	/* Clean up lost G2H + reset engine state */
   	if (engine_destroyed(e) && engine_registered(e)) {
   		if (engine_banned(e))
@@ -1520,6 +1594,9 @@ int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
   	trace_xe_engine_deregister_done(e);
   	clear_engine_registered(e);
+	if (xe_engine_is_lr(e))
+		xe_engine_put(e);
+
   	if (engine_banned(e))
   		xe_engine_put(e);
   	else
@@ -1557,7 +1634,7 @@ int xe_guc_engine_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
   	 */
   	set_engine_reset(e);
   	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
   	return 0;
   }
@@ -1584,7 +1661,7 @@ int xe_guc_engine_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
   	/* Treat the same as engine reset */
   	set_engine_reset(e);
   	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
   	return 0;
   }
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
   	     TP_ARGS(e)
   );
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
   DECLARE_EVENT_CLASS(xe_sched_job,
   		    TP_PROTO(struct xe_sched_job *job),
   		    TP_ARGS(job),
author maintainer
#19 lore raw

Re: [Intel-xe] [PATCH v2 05/31] drm/xe: Long running job update

Quoted 159 lines · Thomas Hellström
Quoted 157 lines · Matthew Brost
Quoted 156 lines · Thomas Hellström

Hi, Matthew

In addition to Rodrigo's comments:

Quoted 143 lines · Matthew Brost

Flow control + write ring in exec, return NULL in run_job, siganl xe_hw_fence immediately, and override TDR for LR jobs.

---
   drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
   drivers/gpu/drm/xe/xe_engine.h           |  4 +
   drivers/gpu/drm/xe/xe_exec.c             |  8 ++
   drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
   drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
   drivers/gpu/drm/xe/xe_trace.h            |  5 ++
   6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +32 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
   #include "xe_macros.h"
   #include "xe_migrate.h"
   #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
   #include "xe_trace.h"
   #include "xe_vm.h"
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
   	up_write(&e->vm->lock);
   }
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
   /**
    * xe_engine_is_idle() - Whether an engine is idle.
    * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
   	return engine->width > 1;
   }
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
   bool xe_engine_is_idle(struct xe_engine *engine);
   void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
   #include "xe_device.h"
   #include "xe_engine.h"
   #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
   #include "xe_sched_job.h"
   #include "xe_sync.h"
   #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
   		goto err_engine_end;
   	}
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
   	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
   				  addresses : &args->address);
   	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
   		xe_sync_entry_signal(&syncs[i], job,
   				     &job->drm.s_fence->finished);
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
   	xe_sched_job_push(job);
   	xe_vm_reactivate_rebind(vm);
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
   	 */
   #define MAX_STATIC_MSG_TYPE	3
   	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
   	/** @fini_async: do final fini async from this worker */
   	struct work_struct fini_async;
   	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +9 -0
index 68d09e7a4cc0..0a41f5d04f6d 100644
@@ -500,6 +500,14 @@ static void register_engine(struct xe_engine *e)
   		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
   	}
+	/*
+	 * We must keep a reference for LR engines if engine is registered with
+	 * the GuC as jobs signal immediately and can't destroy an engine if the
+	 * GuC has a reference to it.
+	 */
+	if (xe_engine_is_lr(e))
+		xe_engine_get(e);
+
   	set_engine_registered(e);
   	trace_xe_engine_register(e);
   	if (xe_engine_is_parallel(e))
@@ -662,6 +670,7 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
   {
   	struct xe_sched_job *job = to_xe_sched_job(drm_job);
   	struct xe_engine *e = job->engine;
+	bool lr = xe_engine_is_lr(e);
   	XE_BUG_ON((engine_destroyed(e) || engine_pending_disable(e)) &&
   		  !engine_banned(e) && !engine_suspended(e));

drivers/gpu/drm/xe/xe_guc_submit.c guc_engine_run_job(struct drm_sched_job *drm_job) · line 683

Quoted 5 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
@@ -671,14 +680,19 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
   	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
   		if (!engine_registered(e))
   			register_engine(e);
-		e->ring_ops->emit_job(job);
+		if (!lr)	/* Written in IOCTL */

Hmm? What does "Written in IOCTL mean?" Could you rephrase to something more descriptive?

Quoted 6 lines
Quoted 1 line

"LR jos are emitted in the IOCTL"

Ah, I read it as "the lr variable was written in IOCTL."

Perhaps LR jobs are already emitted at execbuf time?

I missed exec in my update.

s/LR jos are emitted in the IOCTL/LR jos are emitted in the exec IOCTL/

Matt

Quoted 190 lines

/Thomas

Quoted 187 lines

Does that work?

Matt

Quoted 182 lines
Quoted 45 lines
drivers/gpu/drm/xe/xe_guc_submit.c +34 -2
+			e->ring_ops->emit_job(job);
   		submit_engine(e);
   	}
-	if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags))
+	if (lr) {
+		xe_sched_job_set_error(job, -ENOTSUPP);
+		return NULL;
+	} else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
   		return job->fence;
-	else
+	} else {
   		return dma_fence_get(job->fence);
+	}
   }
   static void guc_engine_free_job(struct drm_sched_job *drm_job)
@@ -782,6 +796,57 @@ static void simple_error_capture(struct xe_engine *e)
   }
   #endif
+static void xe_guc_engine_trigger_cleanup(struct xe_engine *e)
+{
+	struct xe_guc *guc = engine_to_guc(e);
+
+	if (xe_engine_is_lr(e))
+		queue_work(guc_to_gt(guc)->ordered_wq, &e->guc->lr_tdr);
+	else
+		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+}
+
+static void xe_guc_engine_lr_cleanup(struct work_struct *w)
+{
+	struct xe_guc_engine *ge =
+		container_of(w, struct xe_guc_engine, lr_tdr);
+	struct xe_engine *e = ge->engine;
+	struct drm_gpu_scheduler *sched = &ge->sched;
+
+	XE_BUG_ON(!xe_engine_is_lr(e));
+	trace_xe_engine_lr_cleanup(e);
+
+	/* Kill the run_job / process_msg entry points */
+	drm_sched_run_wq_stop(sched);
+
+	/* Engine state now stable, disable scheduling / deregister if needed */
+	if (engine_registered(e)) {
+		struct xe_guc *guc = engine_to_guc(e);
+		int ret;

drivers/gpu/drm/xe/xe_guc_submit.c static void simple_error_capture(struct xe_engine *e) · line 834

Quoted 10 lines
drivers/gpu/drm/xe/xe_guc_submit.c +10 -0
+
+		set_engine_banned(e);
+		xe_engine_get(e);
+		disable_scheduling_deregister(guc, e);
+
+		/*
+		 * Must wait for scheduling to be disabled before signalling
+		 * any fences, if GT broken the GT reset code should signal us.
+		 */
+		smp_rmb();

wait_event() paired with wake_up() family of functions typically set the necessary barriers to make sure anything written prior to wake_up() is seen in wait_event(). So that smp_rmb() is most likely not needed. If it still is, its pairing smp_wmb() should be documented and pointed to as well. See documentation of set_current_state() vs __set_current_state().

Quoted 48 lines
drivers/gpu/drm/xe/xe_guc_submit.c +19 -3
+		ret = wait_event_timeout(guc->ct.wq,
+					 !engine_pending_disable(e) ||
+					 guc_read_stopped(guc), HZ * 5);
+		if (!ret) {
+			XE_WARN_ON("Schedule disable failed to respond");
+			drm_sched_run_wq_start(sched);
+			xe_gt_reset_async(e->gt);
+			return;
+		}
+	}
+
+	drm_sched_run_wq_start(sched);
+}
+
   static enum drm_gpu_sched_stat
   guc_engine_timedout_job(struct drm_sched_job *drm_job)
   {
@@ -832,7 +897,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
   			err = -EIO;
   		set_engine_banned(e);
   		xe_engine_get(e);
-		disable_scheduling_deregister(engine_to_guc(e), e);
+		disable_scheduling_deregister(guc, e);
   		/*
   		 * Must wait for scheduling to be disabled before signalling
@@ -865,7 +930,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
   	 */
   	list_add(&drm_job->list, &sched->pending_list);
   	drm_sched_run_wq_start(sched);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
   	/* Mark all outstanding jobs as bad, thus completing them */
   	spin_lock(&sched->job_list_lock);
@@ -889,6 +954,8 @@ static void __guc_engine_fini_async(struct work_struct *w)
   	trace_xe_engine_destroy(e);
+	if (xe_engine_is_lr(e))
+		cancel_work_sync(&ge->lr_tdr);
   	if (e->flags & ENGINE_FLAG_PERSISTENT)
   		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
   	release_guc_id(guc, e);
@@ -906,7 +973,7 @@ static void guc_engine_fini_async(struct xe_engine *e)
   	bool kernel = e->flags & ENGINE_FLAG_KERNEL;
   	INIT_WORK(&e->guc->fini_async, __guc_engine_fini_async);
-	queue_work(system_unbound_wq, &e->guc->fini_async);
+	queue_work(system_wq, &e->guc->fini_async);
   	/* We must block on kernel engines so slabs are empty on driver unload */
   	if (kernel) {

drivers/gpu/drm/xe/xe_guc_submit.c static int guc_engine_init(struct xe_engine *e) · line 1158

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -0
@@ -1089,12 +1156,16 @@ static int guc_engine_init(struct xe_engine *e)
   	if (err)
   		goto err_free;
+

Unrelated whitespace?

Quoted 67 lines
drivers/gpu/drm/xe/xe_guc_submit.c +12 -3
   	sched = &ge->sched;
   	err = drm_sched_entity_init(&ge->entity, DRM_SCHED_PRIORITY_NORMAL,
   				    &sched, 1, NULL);
   	if (err)
   		goto err_sched;
+	if (xe_engine_is_lr(e))
+		INIT_WORK(&e->guc->lr_tdr, xe_guc_engine_lr_cleanup);
+
   	mutex_lock(&guc->submission_state.lock);
   	err = alloc_guc_id(guc, e);
@@ -1146,7 +1217,7 @@ static void guc_engine_kill(struct xe_engine *e)
   {
   	trace_xe_engine_kill(e);
   	set_engine_killed(e);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
   }
   static void guc_engine_add_msg(struct xe_engine *e, struct drm_sched_msg *msg,
@@ -1296,6 +1367,9 @@ static void guc_engine_stop(struct xe_guc *guc, struct xe_engine *e)
   	/* Stop scheduling + flush any DRM scheduler operations */
   	drm_sched_run_wq_stop(sched);
+	if (engine_registered(e) && xe_engine_is_lr(e))
+		xe_engine_put(e);
+
   	/* Clean up lost G2H + reset engine state */
   	if (engine_destroyed(e) && engine_registered(e)) {
   		if (engine_banned(e))
@@ -1520,6 +1594,9 @@ int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
   	trace_xe_engine_deregister_done(e);
   	clear_engine_registered(e);
+	if (xe_engine_is_lr(e))
+		xe_engine_put(e);
+
   	if (engine_banned(e))
   		xe_engine_put(e);
   	else
@@ -1557,7 +1634,7 @@ int xe_guc_engine_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
   	 */
   	set_engine_reset(e);
   	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
   	return 0;
   }
@@ -1584,7 +1661,7 @@ int xe_guc_engine_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
   	/* Treat the same as engine reset */
   	set_engine_reset(e);
   	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
   	return 0;
   }
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
   	     TP_ARGS(e)
   );
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
   DECLARE_EVENT_CLASS(xe_sched_job,
   		    TP_PROTO(struct xe_sched_job *job),
   		    TP_ARGS(job),
#20 lore raw

Re: [Intel-xe] [PATCH v2 05/31] drm/xe: Long running job update

Quoted 159 lines · Matthew Brost
Quoted 158 lines · Thomas Hellström
Quoted 157 lines · Matthew Brost
Quoted 156 lines · Thomas Hellström

Hi, Matthew

In addition to Rodrigo's comments:

Quoted 143 lines · Matthew Brost

Flow control + write ring in exec, return NULL in run_job, siganl xe_hw_fence immediately, and override TDR for LR jobs.

---
    drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
    drivers/gpu/drm/xe/xe_engine.h           |  4 +
    drivers/gpu/drm/xe/xe_exec.c             |  8 ++
    drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
    drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
    drivers/gpu/drm/xe/xe_trace.h            |  5 ++
    6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +32 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
    #include "xe_macros.h"
    #include "xe_migrate.h"
    #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
    #include "xe_trace.h"
    #include "xe_vm.h"
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
    	up_write(&e->vm->lock);
    }
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
    /**
     * xe_engine_is_idle() - Whether an engine is idle.
     * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
    	return engine->width > 1;
    }
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
    bool xe_engine_is_idle(struct xe_engine *engine);
    void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
    #include "xe_device.h"
    #include "xe_engine.h"
    #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
    #include "xe_sched_job.h"
    #include "xe_sync.h"
    #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
    		goto err_engine_end;
    	}
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
    	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
    				  addresses : &args->address);
    	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
    		xe_sync_entry_signal(&syncs[i], job,
    				     &job->drm.s_fence->finished);
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
    	xe_sched_job_push(job);
    	xe_vm_reactivate_rebind(vm);
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
    	 */
    #define MAX_STATIC_MSG_TYPE	3
    	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
    	/** @fini_async: do final fini async from this worker */
    	struct work_struct fini_async;
    	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +9 -0
index 68d09e7a4cc0..0a41f5d04f6d 100644
@@ -500,6 +500,14 @@ static void register_engine(struct xe_engine *e)
    		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
    	}
+	/*
+	 * We must keep a reference for LR engines if engine is registered with
+	 * the GuC as jobs signal immediately and can't destroy an engine if the
+	 * GuC has a reference to it.
+	 */
+	if (xe_engine_is_lr(e))
+		xe_engine_get(e);
+
    	set_engine_registered(e);
    	trace_xe_engine_register(e);
    	if (xe_engine_is_parallel(e))
@@ -662,6 +670,7 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
    {
    	struct xe_sched_job *job = to_xe_sched_job(drm_job);
    	struct xe_engine *e = job->engine;
+	bool lr = xe_engine_is_lr(e);
    	XE_BUG_ON((engine_destroyed(e) || engine_pending_disable(e)) &&
    		  !engine_banned(e) && !engine_suspended(e));

drivers/gpu/drm/xe/xe_guc_submit.c guc_engine_run_job(struct drm_sched_job *drm_job) · line 683

Quoted 5 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
@@ -671,14 +680,19 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
    	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
    		if (!engine_registered(e))
    			register_engine(e);
-		e->ring_ops->emit_job(job);
+		if (!lr)	/* Written in IOCTL */

Hmm? What does "Written in IOCTL mean?" Could you rephrase to something more descriptive?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

"LR jos are emitted in the IOCTL"

Ah, I read it as "the lr variable was written in IOCTL."

Perhaps LR jobs are already emitted at execbuf time?

I missed exec in my update.

s/LR jos are emitted in the IOCTL/LR jos are emitted in the exec IOCTL/

Sounds good, with also s/jos/jobs/

/Thomas

Quoted 192 lines

Matt

Quoted 189 lines

/Thomas

Quoted 186 lines

Does that work?

Matt

Quoted 182 lines
Quoted 45 lines
drivers/gpu/drm/xe/xe_guc_submit.c +34 -2
+			e->ring_ops->emit_job(job);
    		submit_engine(e);
    	}
-	if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags))
+	if (lr) {
+		xe_sched_job_set_error(job, -ENOTSUPP);
+		return NULL;
+	} else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
    		return job->fence;
-	else
+	} else {
    		return dma_fence_get(job->fence);
+	}
    }
    static void guc_engine_free_job(struct drm_sched_job *drm_job)
@@ -782,6 +796,57 @@ static void simple_error_capture(struct xe_engine *e)
    }
    #endif
+static void xe_guc_engine_trigger_cleanup(struct xe_engine *e)
+{
+	struct xe_guc *guc = engine_to_guc(e);
+
+	if (xe_engine_is_lr(e))
+		queue_work(guc_to_gt(guc)->ordered_wq, &e->guc->lr_tdr);
+	else
+		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+}
+
+static void xe_guc_engine_lr_cleanup(struct work_struct *w)
+{
+	struct xe_guc_engine *ge =
+		container_of(w, struct xe_guc_engine, lr_tdr);
+	struct xe_engine *e = ge->engine;
+	struct drm_gpu_scheduler *sched = &ge->sched;
+
+	XE_BUG_ON(!xe_engine_is_lr(e));
+	trace_xe_engine_lr_cleanup(e);
+
+	/* Kill the run_job / process_msg entry points */
+	drm_sched_run_wq_stop(sched);
+
+	/* Engine state now stable, disable scheduling / deregister if needed */
+	if (engine_registered(e)) {
+		struct xe_guc *guc = engine_to_guc(e);
+		int ret;

drivers/gpu/drm/xe/xe_guc_submit.c static void simple_error_capture(struct xe_engine *e) · line 834

Quoted 10 lines
drivers/gpu/drm/xe/xe_guc_submit.c +10 -0
+
+		set_engine_banned(e);
+		xe_engine_get(e);
+		disable_scheduling_deregister(guc, e);
+
+		/*
+		 * Must wait for scheduling to be disabled before signalling
+		 * any fences, if GT broken the GT reset code should signal us.
+		 */
+		smp_rmb();

wait_event() paired with wake_up() family of functions typically set the necessary barriers to make sure anything written prior to wake_up() is seen in wait_event(). So that smp_rmb() is most likely not needed. If it still is, its pairing smp_wmb() should be documented and pointed to as well. See documentation of set_current_state() vs __set_current_state().

Quoted 48 lines
drivers/gpu/drm/xe/xe_guc_submit.c +19 -3
+		ret = wait_event_timeout(guc->ct.wq,
+					 !engine_pending_disable(e) ||
+					 guc_read_stopped(guc), HZ * 5);
+		if (!ret) {
+			XE_WARN_ON("Schedule disable failed to respond");
+			drm_sched_run_wq_start(sched);
+			xe_gt_reset_async(e->gt);
+			return;
+		}
+	}
+
+	drm_sched_run_wq_start(sched);
+}
+
    static enum drm_gpu_sched_stat
    guc_engine_timedout_job(struct drm_sched_job *drm_job)
    {
@@ -832,7 +897,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
    			err = -EIO;
    		set_engine_banned(e);
    		xe_engine_get(e);
-		disable_scheduling_deregister(engine_to_guc(e), e);
+		disable_scheduling_deregister(guc, e);
    		/*
    		 * Must wait for scheduling to be disabled before signalling
@@ -865,7 +930,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
    	 */
    	list_add(&drm_job->list, &sched->pending_list);
    	drm_sched_run_wq_start(sched);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
    	/* Mark all outstanding jobs as bad, thus completing them */
    	spin_lock(&sched->job_list_lock);
@@ -889,6 +954,8 @@ static void __guc_engine_fini_async(struct work_struct *w)
    	trace_xe_engine_destroy(e);
+	if (xe_engine_is_lr(e))
+		cancel_work_sync(&ge->lr_tdr);
    	if (e->flags & ENGINE_FLAG_PERSISTENT)
    		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
    	release_guc_id(guc, e);
@@ -906,7 +973,7 @@ static void guc_engine_fini_async(struct xe_engine *e)
    	bool kernel = e->flags & ENGINE_FLAG_KERNEL;
    	INIT_WORK(&e->guc->fini_async, __guc_engine_fini_async);
-	queue_work(system_unbound_wq, &e->guc->fini_async);
+	queue_work(system_wq, &e->guc->fini_async);
    	/* We must block on kernel engines so slabs are empty on driver unload */
    	if (kernel) {

drivers/gpu/drm/xe/xe_guc_submit.c static int guc_engine_init(struct xe_engine *e) · line 1158

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -0
@@ -1089,12 +1156,16 @@ static int guc_engine_init(struct xe_engine *e)
    	if (err)
    		goto err_free;
+

Unrelated whitespace?

Quoted 67 lines
drivers/gpu/drm/xe/xe_guc_submit.c +12 -3
    	sched = &ge->sched;
    	err = drm_sched_entity_init(&ge->entity, DRM_SCHED_PRIORITY_NORMAL,
    				    &sched, 1, NULL);
    	if (err)
    		goto err_sched;
+	if (xe_engine_is_lr(e))
+		INIT_WORK(&e->guc->lr_tdr, xe_guc_engine_lr_cleanup);
+
    	mutex_lock(&guc->submission_state.lock);
    	err = alloc_guc_id(guc, e);
@@ -1146,7 +1217,7 @@ static void guc_engine_kill(struct xe_engine *e)
    {
    	trace_xe_engine_kill(e);
    	set_engine_killed(e);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
    }
    static void guc_engine_add_msg(struct xe_engine *e, struct drm_sched_msg *msg,
@@ -1296,6 +1367,9 @@ static void guc_engine_stop(struct xe_guc *guc, struct xe_engine *e)
    	/* Stop scheduling + flush any DRM scheduler operations */
    	drm_sched_run_wq_stop(sched);
+	if (engine_registered(e) && xe_engine_is_lr(e))
+		xe_engine_put(e);
+
    	/* Clean up lost G2H + reset engine state */
    	if (engine_destroyed(e) && engine_registered(e)) {
    		if (engine_banned(e))
@@ -1520,6 +1594,9 @@ int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
    	trace_xe_engine_deregister_done(e);
    	clear_engine_registered(e);
+	if (xe_engine_is_lr(e))
+		xe_engine_put(e);
+
    	if (engine_banned(e))
    		xe_engine_put(e);
    	else
@@ -1557,7 +1634,7 @@ int xe_guc_engine_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
    	 */
    	set_engine_reset(e);
    	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
    	return 0;
    }
@@ -1584,7 +1661,7 @@ int xe_guc_engine_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
    	/* Treat the same as engine reset */
    	set_engine_reset(e);
    	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
    	return 0;
    }
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
    	     TP_ARGS(e)
    );
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
    DECLARE_EVENT_CLASS(xe_sched_job,
    		    TP_PROTO(struct xe_sched_job *job),
    		    TP_ARGS(job),
author maintainer
#21 lore raw

Re: [Intel-xe] [PATCH v2 05/31] drm/xe: Long running job update

In reply to Thomas Hellström
Quoted 4 lines · Thomas Hellström

Hi, Matthew

In addition to Rodrigo's comments:

Opps, missed a few in the first reply... Addressing below.

Quoted 206 lines · Matthew Brost
Quoted 143 lines

Flow control + write ring in exec, return NULL in run_job, siganl xe_hw_fence immediately, and override TDR for LR jobs.

---
  drivers/gpu/drm/xe/xe_engine.c           | 32 ++++++++
  drivers/gpu/drm/xe/xe_engine.h           |  4 +
  drivers/gpu/drm/xe/xe_exec.c             |  8 ++
  drivers/gpu/drm/xe/xe_guc_engine_types.h |  2 +
  drivers/gpu/drm/xe/xe_guc_submit.c       | 95 +++++++++++++++++++++---
  drivers/gpu/drm/xe/xe_trace.h            |  5 ++
  6 files changed, 137 insertions(+), 9 deletions(-)
drivers/gpu/drm/xe/xe_engine.c +32 -0
index 094ec17d3004..d1e84d7adbd4 100644
@@ -18,6 +18,7 @@
  #include "xe_macros.h"
  #include "xe_migrate.h"
  #include "xe_pm.h"
+#include "xe_ring_ops_types.h"
  #include "xe_trace.h"
  #include "xe_vm.h"
@@ -673,6 +674,37 @@ static void engine_kill_compute(struct xe_engine *e)
  	up_write(&e->vm->lock);
  }
+/**
+ * xe_engine_is_lr() - Whether an engine is long-running
+ * @e: The engine
+ *
+ * Return: True if the engine is long-running, false otherwise.
+ */
+bool xe_engine_is_lr(struct xe_engine *e)
+{
+	return e->vm && xe_vm_no_dma_fences(e->vm) &&
+		!(e->flags & ENGINE_FLAG_VM);
+}
+
+static s32 xe_engine_num_job_inflight(struct xe_engine *e)
+{
+	return e->lrc->fence_ctx.next_seqno - xe_lrc_seqno(e->lrc) - 1;
+}
+
+/**
+ * xe_engine_ring_full() - Whether an engine's ring is full
+ * @e: The engine
+ *
+ * Return: True if the engine's ring is full, false otherwise.
+ */
+bool xe_engine_ring_full(struct xe_engine *e)
+{
+	struct xe_lrc *lrc = e->lrc;
+	s32 max_job = lrc->ring.size / MAX_JOB_SIZE_BYTES;
+
+	return xe_engine_num_job_inflight(e) >= max_job;
+}
+
  /**
   * xe_engine_is_idle() - Whether an engine is idle.
   * @engine: The engine
drivers/gpu/drm/xe/xe_engine.h +4 -0
index a49cf2ab405e..2e60f6d90226 100644
@@ -42,6 +42,10 @@ static inline bool xe_engine_is_parallel(struct xe_engine *engine)
  	return engine->width > 1;
  }
+bool xe_engine_is_lr(struct xe_engine *e);
+
+bool xe_engine_ring_full(struct xe_engine *e);
+
  bool xe_engine_is_idle(struct xe_engine *engine);
  void xe_engine_kill(struct xe_engine *e);
drivers/gpu/drm/xe/xe_exec.c +8 -0
index ea869f2452ef..44ea9bcd0066 100644
@@ -13,6 +13,7 @@
  #include "xe_device.h"
  #include "xe_engine.h"
  #include "xe_macros.h"
+#include "xe_ring_ops_types.h"
  #include "xe_sched_job.h"
  #include "xe_sync.h"
  #include "xe_vm.h"
@@ -277,6 +278,11 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  		goto err_engine_end;
  	}
+	if (xe_engine_is_lr(engine) && xe_engine_ring_full(engine)) {
+		err = -EWOULDBLOCK;
+		goto err_engine_end;
+	}
+
  	job = xe_sched_job_create(engine, xe_engine_is_parallel(engine) ?
  				  addresses : &args->address);
  	if (IS_ERR(job)) {
@@ -363,6 +369,8 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  		xe_sync_entry_signal(&syncs[i], job,
  				     &job->drm.s_fence->finished);
+	if (xe_engine_is_lr(engine))
+		engine->ring_ops->emit_job(job);
  	xe_sched_job_push(job);
  	xe_vm_reactivate_rebind(vm);
drivers/gpu/drm/xe/xe_guc_engine_types.h +2 -0
index cbfb13026ec1..5d83132034a6 100644
@@ -31,6 +31,8 @@ struct xe_guc_engine {
  	 */
  #define MAX_STATIC_MSG_TYPE	3
  	struct drm_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
+	/** @lr_tdr: long running TDR worker */
+	struct work_struct lr_tdr;
  	/** @fini_async: do final fini async from this worker */
  	struct work_struct fini_async;
  	/** @resume_time: time of last resume */
drivers/gpu/drm/xe/xe_guc_submit.c +9 -0
index 68d09e7a4cc0..0a41f5d04f6d 100644
@@ -500,6 +500,14 @@ static void register_engine(struct xe_engine *e)
  		parallel_write(xe, map, wq_desc.wq_status, WQ_STATUS_ACTIVE);
  	}
+	/*
+	 * We must keep a reference for LR engines if engine is registered with
+	 * the GuC as jobs signal immediately and can't destroy an engine if the
+	 * GuC has a reference to it.
+	 */
+	if (xe_engine_is_lr(e))
+		xe_engine_get(e);
+
  	set_engine_registered(e);
  	trace_xe_engine_register(e);
  	if (xe_engine_is_parallel(e))
@@ -662,6 +670,7 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
  {
  	struct xe_sched_job *job = to_xe_sched_job(drm_job);
  	struct xe_engine *e = job->engine;
+	bool lr = xe_engine_is_lr(e);
  	XE_BUG_ON((engine_destroyed(e) || engine_pending_disable(e)) &&
  		  !engine_banned(e) && !engine_suspended(e));

drivers/gpu/drm/xe/xe_guc_submit.c guc_engine_run_job(struct drm_sched_job *drm_job) · line 683

Quoted 5 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -1
@@ -671,14 +680,19 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
  	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
  		if (!engine_registered(e))
  			register_engine(e);
-		e->ring_ops->emit_job(job);
+		if (!lr)	/* Written in IOCTL */

Hmm? What does "Written in IOCTL mean?" Could you rephrase to something more descriptive?

Quoted 53 lines
drivers/gpu/drm/xe/xe_guc_submit.c +42 -2
+			e->ring_ops->emit_job(job);
  		submit_engine(e);
  	}
-	if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags))
+	if (lr) {
+		xe_sched_job_set_error(job, -ENOTSUPP);
+		return NULL;
+	} else if (test_and_set_bit(JOB_FLAG_SUBMIT, &job->fence->flags)) {
  		return job->fence;
-	else
+	} else {
  		return dma_fence_get(job->fence);
+	}
  }
  static void guc_engine_free_job(struct drm_sched_job *drm_job)
@@ -782,6 +796,57 @@ static void simple_error_capture(struct xe_engine *e)
  }
  #endif
+static void xe_guc_engine_trigger_cleanup(struct xe_engine *e)
+{
+	struct xe_guc *guc = engine_to_guc(e);
+
+	if (xe_engine_is_lr(e))
+		queue_work(guc_to_gt(guc)->ordered_wq, &e->guc->lr_tdr);
+	else
+		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+}
+
+static void xe_guc_engine_lr_cleanup(struct work_struct *w)
+{
+	struct xe_guc_engine *ge =
+		container_of(w, struct xe_guc_engine, lr_tdr);
+	struct xe_engine *e = ge->engine;
+	struct drm_gpu_scheduler *sched = &ge->sched;
+
+	XE_BUG_ON(!xe_engine_is_lr(e));
+	trace_xe_engine_lr_cleanup(e);
+
+	/* Kill the run_job / process_msg entry points */
+	drm_sched_run_wq_stop(sched);
+
+	/* Engine state now stable, disable scheduling / deregister if needed */
+	if (engine_registered(e)) {
+		struct xe_guc *guc = engine_to_guc(e);
+		int ret;
+
+		set_engine_banned(e);
+		xe_engine_get(e);
+		disable_scheduling_deregister(guc, e);
+
+		/*
+		 * Must wait for scheduling to be disabled before signalling
+		 * any fences, if GT broken the GT reset code should signal us.
Quoted 9 lines

drivers/gpu/drm/xe/xe_guc_submit.c static void simple_error_capture(struct xe_engine *e) · line 835

Quoted 2 lines
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
+		 */
+		smp_rmb();

wait_event() paired with wake_up() family of functions typically set the necessary barriers to make sure anything written prior to wake_up() is seen in wait_event(). So that smp_rmb() is most likely not needed. If it still is, its pairing smp_wmb() should be documented and pointed to as well. See documentation of set_current_state() vs __set_current_state().

Ok, thanks. This is copy paste from other and code. I've always be a little shakey with my understanding of barriers, believe you that this is wrong. Will remove and study up on this.

Quoted 45 lines
Quoted 45 lines
drivers/gpu/drm/xe/xe_guc_submit.c +19 -3
+		ret = wait_event_timeout(guc->ct.wq,
+					 !engine_pending_disable(e) ||
+					 guc_read_stopped(guc), HZ * 5);
+		if (!ret) {
+			XE_WARN_ON("Schedule disable failed to respond");
+			drm_sched_run_wq_start(sched);
+			xe_gt_reset_async(e->gt);
+			return;
+		}
+	}
+
+	drm_sched_run_wq_start(sched);
+}
+
  static enum drm_gpu_sched_stat
  guc_engine_timedout_job(struct drm_sched_job *drm_job)
  {
@@ -832,7 +897,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
  			err = -EIO;
  		set_engine_banned(e);
  		xe_engine_get(e);
-		disable_scheduling_deregister(engine_to_guc(e), e);
+		disable_scheduling_deregister(guc, e);
  		/*
  		 * Must wait for scheduling to be disabled before signalling
@@ -865,7 +930,7 @@ guc_engine_timedout_job(struct drm_sched_job *drm_job)
  	 */
  	list_add(&drm_job->list, &sched->pending_list);
  	drm_sched_run_wq_start(sched);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
  	/* Mark all outstanding jobs as bad, thus completing them */
  	spin_lock(&sched->job_list_lock);
@@ -889,6 +954,8 @@ static void __guc_engine_fini_async(struct work_struct *w)
  	trace_xe_engine_destroy(e);
+	if (xe_engine_is_lr(e))
+		cancel_work_sync(&ge->lr_tdr);
  	if (e->flags & ENGINE_FLAG_PERSISTENT)
  		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
  	release_guc_id(guc, e);
@@ -906,7 +973,7 @@ static void guc_engine_fini_async(struct xe_engine *e)
  	bool kernel = e->flags & ENGINE_FLAG_KERNEL;
  	INIT_WORK(&e->guc->fini_async, __guc_engine_fini_async);
-	queue_work(system_unbound_wq, &e->guc->fini_async);
+	queue_work(system_wq, &e->guc->fini_async);
Quoted 8 lines

drivers/gpu/drm/xe/xe_guc_submit.c static int guc_engine_init(struct xe_engine *e) · line 1158

Quoted 6 lines
drivers/gpu/drm/xe/xe_guc_submit.c +1 -0
  	/* We must block on kernel engines so slabs are empty on driver unload */
  	if (kernel) {
@@ -1089,12 +1156,16 @@ static int guc_engine_init(struct xe_engine *e)
  	if (err)
  		goto err_free;
+

Unrelated whitespace?

Looks like it, will remove.

Matt

Quoted 70 lines
Quoted 69 lines
drivers/gpu/drm/xe/xe_guc_submit.c +12 -3
  	sched = &ge->sched;
  	err = drm_sched_entity_init(&ge->entity, DRM_SCHED_PRIORITY_NORMAL,
  				    &sched, 1, NULL);
  	if (err)
  		goto err_sched;
+	if (xe_engine_is_lr(e))
+		INIT_WORK(&e->guc->lr_tdr, xe_guc_engine_lr_cleanup);
+
  	mutex_lock(&guc->submission_state.lock);
  	err = alloc_guc_id(guc, e);
@@ -1146,7 +1217,7 @@ static void guc_engine_kill(struct xe_engine *e)
  {
  	trace_xe_engine_kill(e);
  	set_engine_killed(e);
-	drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+	xe_guc_engine_trigger_cleanup(e);
  }
  static void guc_engine_add_msg(struct xe_engine *e, struct drm_sched_msg *msg,
@@ -1296,6 +1367,9 @@ static void guc_engine_stop(struct xe_guc *guc, struct xe_engine *e)
  	/* Stop scheduling + flush any DRM scheduler operations */
  	drm_sched_run_wq_stop(sched);
+	if (engine_registered(e) && xe_engine_is_lr(e))
+		xe_engine_put(e);
+
  	/* Clean up lost G2H + reset engine state */
  	if (engine_destroyed(e) && engine_registered(e)) {
  		if (engine_banned(e))
@@ -1520,6 +1594,9 @@ int xe_guc_deregister_done_handler(struct xe_guc *guc, u32 *msg, u32 len)
  	trace_xe_engine_deregister_done(e);
  	clear_engine_registered(e);
+	if (xe_engine_is_lr(e))
+		xe_engine_put(e);
+
  	if (engine_banned(e))
  		xe_engine_put(e);
  	else
@@ -1557,7 +1634,7 @@ int xe_guc_engine_reset_handler(struct xe_guc *guc, u32 *msg, u32 len)
  	 */
  	set_engine_reset(e);
  	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
  	return 0;
  }
@@ -1584,7 +1661,7 @@ int xe_guc_engine_memory_cat_error_handler(struct xe_guc *guc, u32 *msg,
  	/* Treat the same as engine reset */
  	set_engine_reset(e);
  	if (!engine_banned(e))
-		drm_sched_set_timeout(&e->guc->sched, MIN_SCHED_TIMEOUT);
+		xe_guc_engine_trigger_cleanup(e);
  	return 0;
  }
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 2f8eb7ebe9a7..02861c26e145 100644
@@ -219,6 +219,11 @@ DEFINE_EVENT(xe_engine, xe_engine_resubmit,
  	     TP_ARGS(e)
  );
+DEFINE_EVENT(xe_engine, xe_engine_lr_cleanup,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
  DECLARE_EVENT_CLASS(xe_sched_job,
  		    TP_PROTO(struct xe_sched_job *job),
  		    TP_ARGS(job),
author maintainer
#22 lore raw

PATCH v2 6/31 drm/xe: Ensure LR engines are not persistent

In reply to Matthew Brost

With our ref counting scheme LR engines only close properly if not persistent, ensure that LR engines are non-persistent.

---
 drivers/gpu/drm/xe/xe_engine.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_engine.c +3 -1
index d1e84d7adbd4..91600b1e8249 100644
@@ -596,7 +596,9 @@ int xe_engine_create_ioctl(struct drm_device *dev, void *data,
 			return -ENOENT;
 
 		e = xe_engine_create(xe, vm, logical_mask,
-				     args->width, hwe, ENGINE_FLAG_PERSISTENT);
+				     args->width, hwe,
+				     xe_vm_no_dma_fences(vm) ? 0 :
+				     ENGINE_FLAG_PERSISTENT);
 		xe_vm_put(vm);
 		if (IS_ERR(e))
 			return PTR_ERR(e);
Signature · 1 line
2.34.1
#23 lore raw

Re: [Intel-xe] [PATCH v2 06/31] drm/xe: Ensure LR engines are not persistent

Quoted 2 lines · Matthew Brost

With our ref counting scheme LR engines only close properly if not persistent, ensure that LR engines are non-persistent.

Better to spell out long running somewhere here...

Quoted 11 lines
---
 drivers/gpu/drm/xe/xe_engine.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_engine.c +0 -0
index d1e84d7adbd4..91600b1e8249 100644

drivers/gpu/drm/xe/xe_engine.c int xe_engine_create_ioctl(struct drm_device *dev, void *data, · line 600

Quoted 6 lines
drivers/gpu/drm/xe/xe_engine.c +2 -1
@@ -596,7 +596,9 @@ int xe_engine_create_ioctl(struct drm_device *dev, void *data,
 			return -ENOENT;
 
 		e = xe_engine_create(xe, vm, logical_mask,
-				     args->width, hwe, ENGINE_FLAG_PERSISTENT);
+				     args->width, hwe,
+				     xe_vm_no_dma_fences(vm) ? 0 :

shouldn't we use that existent function xe_engine_is_lr instead of this?

Quoted 7 lines
drivers/gpu/drm/xe/xe_engine.c +1 -0
+				     ENGINE_FLAG_PERSISTENT);
 		xe_vm_put(vm);
 		if (IS_ERR(e))
 			return PTR_ERR(e);
Signature · 1 line
2.34.1
author maintainer
#24 lore raw

Re: [Intel-xe] [PATCH v2 06/31] drm/xe: Ensure LR engines are not persistent

Quoted 6 lines · Rodrigo Vivi
Quoted 2 lines · Matthew Brost

With our ref counting scheme LR engines only close properly if not persistent, ensure that LR engines are non-persistent.

Better to spell out long running somewhere here...

Will do.

Quoted 13 lines
Quoted 13 lines
---
 drivers/gpu/drm/xe/xe_engine.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_engine.c +0 -0
index d1e84d7adbd4..91600b1e8249 100644
@@ -596,7 +596,9 @@ int xe_engine_create_ioctl(struct drm_device *dev, void *data,
 			return -ENOENT;
 
Quoted 7 lines

drivers/gpu/drm/xe/xe_engine.c int xe_engine_create_ioctl(struct drm_device *dev, void *data, · line 600

Quoted 4 lines
drivers/gpu/drm/xe/xe_engine.c +2 -1
 		e = xe_engine_create(xe, vm, logical_mask,
-				     args->width, hwe, ENGINE_FLAG_PERSISTENT);
+				     args->width, hwe,
+				     xe_vm_no_dma_fences(vm) ? 0 :

shouldn't we use that existent function xe_engine_is_lr instead of this?

The engine isn't created yet... So no this seems to be correct as a LR engine us a user engine with a VM that doesn't allow DMA fences.

Matt<

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/xe/xe_engine.c +1 -0
+				     ENGINE_FLAG_PERSISTENT);
 		xe_vm_put(vm);
 		if (IS_ERR(e))
 			return PTR_ERR(e);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 6/31
#25 lore raw

Re: [Intel-xe] [PATCH v2 06/31] drm/xe: Ensure LR engines are not persistent

In reply to Matthew Brost
Quoted 4 lines · Matthew Brost

With our ref counting scheme LR engines only close properly if not persistent, ensure that LR engines are non-persistent.

With Rodrigos "Long-Running" comment addressed,

Quoted 19 lines
---
  drivers/gpu/drm/xe/xe_engine.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/xe/xe_engine.c +3 -1
index d1e84d7adbd4..91600b1e8249 100644
@@ -596,7 +596,9 @@ int xe_engine_create_ioctl(struct drm_device *dev, void *data,
  			return -ENOENT;
 
  		e = xe_engine_create(xe, vm, logical_mask,
-				     args->width, hwe, ENGINE_FLAG_PERSISTENT);
+				     args->width, hwe,
+				     xe_vm_no_dma_fences(vm) ? 0 :
+				     ENGINE_FLAG_PERSISTENT);
  		xe_vm_put(vm);
  		if (IS_ERR(e))
  			return PTR_ERR(e);
author maintainer
#26 lore raw

PATCH v2 7/31 drm/xe: Only try to lock external BOs in VM bind

In reply to Matthew Brost

Not needed and causes some issues with bulk LRU moves.

---
 drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -3
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm, struct xe_vma *vma,
 		 */
 		xe_bo_get(vbo);
 
-		tv_bo.bo = &vbo->ttm;
-		tv_bo.num_shared = 1;
-		list_add(&tv_bo.head, &objs);
+		if (!vbo->vm) {
+			tv_bo.bo = &vbo->ttm;
+			tv_bo.num_shared = 1;
+			list_add(&tv_bo.head, &objs);
+		}
 	}
 
 again:
Signature · 1 line
2.34.1
#27 lore raw

Re: [Intel-xe] [PATCH v2 07/31] drm/xe: Only try to lock external BOs in VM bind

Quoted 1 line · Matthew Brost

Not needed and causes some issues with bulk LRU moves.

I'm confused with this explanation and the code below. could you please provide a bit more wording here?

Quoted 28 lines
---
 drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -3
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm, struct xe_vma *vma,
 		 */
 		xe_bo_get(vbo);
 
-		tv_bo.bo = &vbo->ttm;
-		tv_bo.num_shared = 1;
-		list_add(&tv_bo.head, &objs);
+		if (!vbo->vm) {
+			tv_bo.bo = &vbo->ttm;
+			tv_bo.num_shared = 1;
+			list_add(&tv_bo.head, &objs);
+		}
 	}
 
 again:
Signature · 1 line
2.34.1
author maintainer
#28 lore raw

Re: [Intel-xe] [PATCH v2 07/31] drm/xe: Only try to lock external BOs in VM bind

Quoted 6 lines · Rodrigo Vivi
Quoted 1 line · Matthew Brost

Not needed and causes some issues with bulk LRU moves.

I'm confused with this explanation and the code below. could you please provide a bit more wording here?

We only need to try to lock a BO if it external as non-external BOs share the dma-resv with the already locked VM. Trying to lock non-external BOs caused an issue (list corruption) in an uncoming patch which adds bulk LRU move. Since this code isn't needed, remove it.

^^^ How about this.

Quoted 28 lines
Quoted 28 lines
---
 drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -3
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm, struct xe_vma *vma,
 		 */
 		xe_bo_get(vbo);
 
-		tv_bo.bo = &vbo->ttm;
-		tv_bo.num_shared = 1;
-		list_add(&tv_bo.head, &objs);
+		if (!vbo->vm) {
+			tv_bo.bo = &vbo->ttm;
+			tv_bo.num_shared = 1;
+			list_add(&tv_bo.head, &objs);
+		}
 	}
 
 again:
Signature · 1 line
2.34.1
#29 lore raw

Re: [Intel-xe] [PATCH v2 07/31] drm/xe: Only try to lock external BOs in VM bind

s/it external/it's external/

Otherwise, the description looks good.

Quoted 45 lines · Matthew Brost
Quoted 6 lines · Rodrigo Vivi
Quoted 1 line · Matthew Brost

Not needed and causes some issues with bulk LRU moves.

I'm confused with this explanation and the code below. could you please provide a bit more wording here?

We only need to try to lock a BO if it external as non-external BOs share the dma-resv with the already locked VM. Trying to lock non-external BOs caused an issue (list corruption) in an uncoming patch which adds bulk LRU move. Since this code isn't needed, remove it.

^^^ How about this.

Quoted 11 lines

drivers/gpu/drm/xe/xe_vm.c static int vm_bind_ioctl(struct xe_vm *vm,

Quoted 11 lines
---
 drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +0 -0
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm,

struct xe_vma *vma,

Quoted 17 lines
Quoted 17 lines
drivers/gpu/drm/xe/xe_vm.c +5 -4
             */
            xe_bo_get(vbo);
 
-           tv_bo.bo = &vbo->ttm;
-           tv_bo.num_shared = 1;
-           list_add(&tv_bo.head, &objs);
+           if (!vbo->vm) {
+                   tv_bo.bo = &vbo->ttm;
+                   tv_bo.num_shared = 1;
+                   list_add(&tv_bo.head, &objs);
+           }
    }
 
 again:
--

2.34.1

maintainer Reviewed-by
#30 lore raw

Re: [Intel-xe] [PATCH v2 07/31] drm/xe: Only try to lock external BOs in VM bind

In reply to Matthew Brost
Quoted 4 lines · Matthew Brost
Quoted 3 lines · Rodrigo Vivi
Quoted 1 line · Matthew Brost

Not needed and causes some issues with bulk LRU moves.

Quoted 8 lines
Quoted 3 lines

I'm confused with this explanation and the code below. could you please provide a bit more wording here?

We only need to try to lock a BO if it external as non-external BOs share the dma-resv with the already locked VM. Trying to lock non-external BOs caused an issue (list corruption) in an uncoming patch which adds bulk LRU move. Since this code isn't needed, remove it.

it makes more sense now. with this in commit msg (but with Christopher fix)

Quoted 31 lines

^^^ How about this.

Quoted 28 lines
Quoted 28 lines
---
 drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -3
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm, struct xe_vma *vma,
 		 */
 		xe_bo_get(vbo);
 
-		tv_bo.bo = &vbo->ttm;
-		tv_bo.num_shared = 1;
-		list_add(&tv_bo.head, &objs);
+		if (!vbo->vm) {
+			tv_bo.bo = &vbo->ttm;
+			tv_bo.num_shared = 1;
+			list_add(&tv_bo.head, &objs);
+		}
 	}
 
 again:
Signature · 1 line
2.34.1
#31 lore raw

Re: [Intel-xe] [PATCH v2 07/31] drm/xe: Only try to lock external BOs in VM bind

Quoted 10 lines · Rodrigo Vivi
Quoted 9 lines · Matthew Brost
Quoted 5 lines · Rodrigo Vivi
Quoted 1 line · Matthew Brost

Not needed and causes some issues with bulk LRU moves.

I'm confused with this explanation and the code below. could you please provide a bit more wording here?

We only need to try to lock a BO if it external as non-external BOs share the dma-resv with the already locked VM. Trying to lock non-external BOs caused an issue (list corruption) in an uncoming patch

s/uncoming/upcoming/

Also it's not clear to me how this could fix a list corruption in the bulk LRU moves? I mean, if it's a duplicate lock then it gets removed from the tv list and not touched again? Could you explain the mechanism of the fix?

Thanks,

Thomas

Quoted 36 lines
Quoted 1 line

which adds bulk LRU move. Since this code isn't needed, remove it.

it makes more sense now. with this in commit msg (but with Christopher fix)

Quoted 29 lines

^^^ How about this.

Quoted 27 lines
Quoted 27 lines
---
  drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -3
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm, struct xe_vma *vma,
  		 */
  		xe_bo_get(vbo);
 
-		tv_bo.bo = &vbo->ttm;
-		tv_bo.num_shared = 1;
-		list_add(&tv_bo.head, &objs);
+		if (!vbo->vm) {
+			tv_bo.bo = &vbo->ttm;
+			tv_bo.num_shared = 1;
+			list_add(&tv_bo.head, &objs);
+		}
  	}
 
  again:
Signature · 1 line
2.34.1
author maintainer
#32 lore raw

Re: [Intel-xe] [PATCH v2 07/31] drm/xe: Only try to lock external BOs in VM bind

Quoted 13 lines · Thomas Hellström
Quoted 10 lines · Rodrigo Vivi
Quoted 9 lines · Matthew Brost
Quoted 5 lines · Rodrigo Vivi
Quoted 1 line · Matthew Brost

Not needed and causes some issues with bulk LRU moves.

I'm confused with this explanation and the code below. could you please provide a bit more wording here?

We only need to try to lock a BO if it external as non-external BOs share the dma-resv with the already locked VM. Trying to lock non-external BOs caused an issue (list corruption) in an uncoming patch

Quoted 6 lines

s/uncoming/upcoming/

Also it's not clear to me how this could fix a list corruption in the bulk LRU moves? I mean, if it's a duplicate lock then it gets removed from the tv list and not touched again? Could you explain the mechanism of the fix?

I had my head wrappeed around this at one point but now I forget as I coded this one a while ago. This changes later series (drm_exec locking), so IMO not that big of a deal merge this along with the following patch without further explaination but if you think it is a huge deal I can try to figure out what the issue is again or another option is just stage the LRU patch after drm_exec.

Open to whatever but prefer to leave as is.

Matt

Quoted 39 lines

Thanks,

Thomas

Quoted 34 lines
Quoted 1 line

which adds bulk LRU move. Since this code isn't needed, remove it.

it makes more sense now. with this in commit msg (but with Christopher fix)

Quoted 27 lines

^^^ How about this.

Quoted 25 lines
Quoted 25 lines
---
  drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -3
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm, struct xe_vma *vma,
  		 */
  		xe_bo_get(vbo);
-		tv_bo.bo = &vbo->ttm;
-		tv_bo.num_shared = 1;
-		list_add(&tv_bo.head, &objs);
+		if (!vbo->vm) {
+			tv_bo.bo = &vbo->ttm;
+			tv_bo.num_shared = 1;
+			list_add(&tv_bo.head, &objs);
+		}
  	}
  again:
Signature · 1 line
2.34.1
#33 lore raw

Re: [Intel-xe] [PATCH v2 07/31] drm/xe: Only try to lock external BOs in VM bind

Quoted 18 lines · Matthew Brost
Quoted 8 lines · Thomas Hellström
Quoted 7 lines · Rodrigo Vivi
Quoted 6 lines · Matthew Brost
Quoted 5 lines · Rodrigo Vivi
Quoted 1 line · Matthew Brost

Not needed and causes some issues with bulk LRU moves.

I'm confused with this explanation and the code below. could you please provide a bit more wording here?

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

We only need to try to lock a BO if it external as non-external BOs share the dma-resv with the already locked VM. Trying to lock non-external BOs caused an issue (list corruption) in an uncoming patch

s/uncoming/upcoming/

Also it's not clear to me how this could fix a list corruption in the bulk LRU moves? I mean, if it's a duplicate lock then it gets removed from the tv list and not touched again? Could you explain the mechanism of the fix?

Quoted 8 lines

I had my head wrappeed around this at one point but now I forget as I coded this one a while ago. This changes later series (drm_exec locking), so IMO not that big of a deal merge this along with the following patch without further explaination but if you think it is a huge deal I can try to figure out what the issue is again or another option is just stage the LRU patch after drm_exec.

Open to whatever but prefer to leave as is.

Since this will be part of a bulk LRU series, separate from drm_exec then yes please add an explanation as why this is needed, as we can't make something dependent on a future patch that may or may not go in depending on the review outcome.

Could also consider reordering so that drm_exec goes in before bulk LRU?

/Thomas

Quoted 42 lines

Matt

Quoted 39 lines

Thanks,

Thomas

Quoted 34 lines
Quoted 1 line

which adds bulk LRU move. Since this code isn't needed, remove it.

it makes more sense now. with this in commit msg (but with Christopher fix)

Quoted 27 lines

^^^ How about this.

Quoted 25 lines
Quoted 25 lines
---
   drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
   1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -3
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm, struct xe_vma *vma,
   		 */
   		xe_bo_get(vbo);
-		tv_bo.bo = &vbo->ttm;
-		tv_bo.num_shared = 1;
-		list_add(&tv_bo.head, &objs);
+		if (!vbo->vm) {
+			tv_bo.bo = &vbo->ttm;
+			tv_bo.num_shared = 1;
+			list_add(&tv_bo.head, &objs);
+		}
   	}
   again:
Signature · 1 line
2.34.1
#34 lore raw

Re: [Intel-xe] [PATCH v2 07/31] drm/xe: Only try to lock external BOs in VM bind

In reply to Matthew Brost

Also, you've typoed your email address as [email protected] in a few places here, including the Signed-off-by. It has also ended up in the CC list for reply-all.

Quoted 31 lines · Matthew Brost

Not needed and causes some issues with bulk LRU moves.

---
 drivers/gpu/drm/xe/xe_vm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +5 -3
index 272f0f7f24fe..6c427ff92c44 100644
@@ -2064,9 +2064,11 @@ static int vm_bind_ioctl(struct xe_vm *vm, struct
 xe_vma *vma,
                 */
                xe_bo_get(vbo);
 
-               tv_bo.bo = &vbo->ttm;
-               tv_bo.num_shared = 1;
-               list_add(&tv_bo.head, &objs);
+               if (!vbo->vm) {
+                       tv_bo.bo = &vbo->ttm;
+                       tv_bo.num_shared = 1;
+                       list_add(&tv_bo.head, &objs);
+               }
        }
 

again:
--
2.34.1

author maintainer
#35 lore raw

PATCH v2 8/31 drm/xe: VM LRU bulk move

In reply to Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
 drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
 drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
 drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
 drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
 drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
 5 files changed, 40 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +28 -4
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
 	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
 }
 
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));
+
+		xe_bo_lock(bo, &ww, 0, false);
+		ttm_bo_set_bulk_move(&bo->ttm, NULL);
+		xe_bo_unlock(bo, &ww);
+	}
+}
+
+
 static bool should_migrate_to_system(struct xe_bo *bo)
 {
 	struct xe_device *xe = xe_bo_device(bo);
@@ -1040,6 +1057,7 @@ static const struct vm_operations_struct xe_gem_vm_ops = {
 
 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
 	.free = xe_gem_object_free,
+	.close = xe_gem_object_close,
 	.mmap = drm_gem_ttm_mmap,
 	.export = xe_gem_prime_export,
 	.vm_ops = &xe_gem_vm_ops,
@@ -1081,8 +1099,8 @@ void xe_bo_free(struct xe_bo *bo)
 
 struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_gt *gt, struct dma_resv *resv,
-				    size_t size, enum ttm_bo_type type,
-				    u32 flags)
+				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    enum ttm_bo_type type, u32 flags)
 {
 	struct ttm_operation_ctx ctx = {
 		.interruptible = true,
@@ -1149,7 +1167,10 @@ struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 		return ERR_PTR(err);
 
 	bo->created = true;
-	ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
+	if (bulk)
+		ttm_bo_set_bulk_move(&bo->ttm, bulk);
+	else
+		ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
 
 	return bo;
 }
@@ -1219,7 +1240,10 @@ xe_bo_create_locked_range(struct xe_device *xe,
 		}
 	}
 
-	bo = __xe_bo_create_locked(xe, bo, gt, vm ? &vm->resv : NULL, size,
+	bo = __xe_bo_create_locked(xe, bo, gt, vm ? &vm->resv : NULL,
+				   vm && !xe_vm_no_dma_fences(vm) &&
+				   flags & XE_BO_CREATE_USER_BIT ?
+				   &vm->lru_bulk_move : NULL, size,
 				   type, flags);
 	if (IS_ERR(bo))
 		return bo;
drivers/gpu/drm/xe/xe_bo.h +2 -2
index 8354d05ccdf3..25457b3c757b 100644
@@ -81,8 +81,8 @@ void xe_bo_free(struct xe_bo *bo);
 
 struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_gt *gt, struct dma_resv *resv,
-				    size_t size, enum ttm_bo_type type,
-				    u32 flags);
+				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    enum ttm_bo_type type, u32 flags);
 struct xe_bo *
 xe_bo_create_locked_range(struct xe_device *xe,
 			  struct xe_gt *gt, struct xe_vm *vm,
drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
index 9b252cc782b7..975dee1f770f 100644
@@ -199,7 +199,7 @@ xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage,
 	int ret;
 
 	dma_resv_lock(resv, NULL);
-	bo = __xe_bo_create_locked(xe, storage, NULL, resv, dma_buf->size,
+	bo = __xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size,
 				   ttm_bo_type_sg, XE_BO_CREATE_SYSTEM_BIT);
 	if (IS_ERR(bo)) {
 		ret = PTR_ERR(bo);
drivers/gpu/drm/xe/xe_exec.c +6 -0
index 44ea9bcd0066..21a9c2fddf86 100644
@@ -374,6 +374,12 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	xe_sched_job_push(job);
 	xe_vm_reactivate_rebind(vm);
 
+	if (!err && !xe_vm_no_dma_fences(vm)) {
+		spin_lock(&xe->ttm.lru_lock);
+		ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
+		spin_unlock(&xe->ttm.lru_lock);
+	}
+
 err_repin:
 	if (!xe_vm_no_dma_fences(vm))
 		up_read(&vm->userptr.notifier_lock);
drivers/gpu/drm/xe/xe_vm_types.h +3 -0
index fada7896867f..d3e99f22510d 100644
@@ -164,6 +164,9 @@ struct xe_vm {
 	/** Protects @rebind_list and the page-table structures */
 	struct dma_resv resv;
 
+	/** @lru_bulk_move: Bulk LRU move list for this VM's BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
+
 	u64 size;
 	struct rb_root vmas;
 
Signature · 1 line
2.34.1
maintainer
#36 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 19 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
 drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
 drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
 drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
 drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
 drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
 5 files changed, 40 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +0 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
 	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
 }

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +9 -0
 
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

We need to really stop using BUG_ON and move towards the usage of more WARNs.

But the rest of the patch looks good to me... I just believe it would be good to get Thomas' review here.

Quoted 116 lines
drivers/gpu/drm/xe/xe_bo.c +19 -4
+
+		xe_bo_lock(bo, &ww, 0, false);
+		ttm_bo_set_bulk_move(&bo->ttm, NULL);
+		xe_bo_unlock(bo, &ww);
+	}
+}
+
+
 static bool should_migrate_to_system(struct xe_bo *bo)
 {
 	struct xe_device *xe = xe_bo_device(bo);
@@ -1040,6 +1057,7 @@ static const struct vm_operations_struct xe_gem_vm_ops = {
 
 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
 	.free = xe_gem_object_free,
+	.close = xe_gem_object_close,
 	.mmap = drm_gem_ttm_mmap,
 	.export = xe_gem_prime_export,
 	.vm_ops = &xe_gem_vm_ops,
@@ -1081,8 +1099,8 @@ void xe_bo_free(struct xe_bo *bo)
 
 struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_gt *gt, struct dma_resv *resv,
-				    size_t size, enum ttm_bo_type type,
-				    u32 flags)
+				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    enum ttm_bo_type type, u32 flags)
 {
 	struct ttm_operation_ctx ctx = {
 		.interruptible = true,
@@ -1149,7 +1167,10 @@ struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 		return ERR_PTR(err);
 
 	bo->created = true;
-	ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
+	if (bulk)
+		ttm_bo_set_bulk_move(&bo->ttm, bulk);
+	else
+		ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
 
 	return bo;
 }
@@ -1219,7 +1240,10 @@ xe_bo_create_locked_range(struct xe_device *xe,
 		}
 	}
 
-	bo = __xe_bo_create_locked(xe, bo, gt, vm ? &vm->resv : NULL, size,
+	bo = __xe_bo_create_locked(xe, bo, gt, vm ? &vm->resv : NULL,
+				   vm && !xe_vm_no_dma_fences(vm) &&
+				   flags & XE_BO_CREATE_USER_BIT ?
+				   &vm->lru_bulk_move : NULL, size,
 				   type, flags);
 	if (IS_ERR(bo))
 		return bo;
drivers/gpu/drm/xe/xe_bo.h +2 -2
index 8354d05ccdf3..25457b3c757b 100644
@@ -81,8 +81,8 @@ void xe_bo_free(struct xe_bo *bo);
 
 struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_gt *gt, struct dma_resv *resv,
-				    size_t size, enum ttm_bo_type type,
-				    u32 flags);
+				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    enum ttm_bo_type type, u32 flags);
 struct xe_bo *
 xe_bo_create_locked_range(struct xe_device *xe,
 			  struct xe_gt *gt, struct xe_vm *vm,
drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
index 9b252cc782b7..975dee1f770f 100644
@@ -199,7 +199,7 @@ xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage,
 	int ret;
 
 	dma_resv_lock(resv, NULL);
-	bo = __xe_bo_create_locked(xe, storage, NULL, resv, dma_buf->size,
+	bo = __xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size,
 				   ttm_bo_type_sg, XE_BO_CREATE_SYSTEM_BIT);
 	if (IS_ERR(bo)) {
 		ret = PTR_ERR(bo);
drivers/gpu/drm/xe/xe_exec.c +6 -0
index 44ea9bcd0066..21a9c2fddf86 100644
@@ -374,6 +374,12 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	xe_sched_job_push(job);
 	xe_vm_reactivate_rebind(vm);
 
+	if (!err && !xe_vm_no_dma_fences(vm)) {
+		spin_lock(&xe->ttm.lru_lock);
+		ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
+		spin_unlock(&xe->ttm.lru_lock);
+	}
+
 err_repin:
 	if (!xe_vm_no_dma_fences(vm))
 		up_read(&vm->userptr.notifier_lock);
drivers/gpu/drm/xe/xe_vm_types.h +3 -0
index fada7896867f..d3e99f22510d 100644
@@ -164,6 +164,9 @@ struct xe_vm {
 	/** Protects @rebind_list and the page-table structures */
 	struct dma_resv resv;
 
+	/** @lru_bulk_move: Bulk LRU move list for this VM's BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
+
 	u64 size;
 	struct rb_root vmas;
 
Signature · 1 line
2.34.1
author maintainer
#37 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 21 lines · Rodrigo Vivi
Quoted 20 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
 drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
 drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
 drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
 drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
 drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
 5 files changed, 40 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +0 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
 	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
 }
 
Quoted 12 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 9 lines
drivers/gpu/drm/xe/xe_bo.c +9 -0
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

We need to really stop using BUG_ON and move towards the usage of more WARNs.

If that is the direction, sure I'll change this but personally I BUG_ON for things that should be impossible with a correct KMD.

Matt

Quoted 119 lines

But the rest of the patch looks good to me... I just believe it would be good to get Thomas' review here.

Quoted 116 lines
drivers/gpu/drm/xe/xe_bo.c +19 -4
+
+		xe_bo_lock(bo, &ww, 0, false);
+		ttm_bo_set_bulk_move(&bo->ttm, NULL);
+		xe_bo_unlock(bo, &ww);
+	}
+}
+
+
 static bool should_migrate_to_system(struct xe_bo *bo)
 {
 	struct xe_device *xe = xe_bo_device(bo);
@@ -1040,6 +1057,7 @@ static const struct vm_operations_struct xe_gem_vm_ops = {
 
 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
 	.free = xe_gem_object_free,
+	.close = xe_gem_object_close,
 	.mmap = drm_gem_ttm_mmap,
 	.export = xe_gem_prime_export,
 	.vm_ops = &xe_gem_vm_ops,
@@ -1081,8 +1099,8 @@ void xe_bo_free(struct xe_bo *bo)
 
 struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_gt *gt, struct dma_resv *resv,
-				    size_t size, enum ttm_bo_type type,
-				    u32 flags)
+				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    enum ttm_bo_type type, u32 flags)
 {
 	struct ttm_operation_ctx ctx = {
 		.interruptible = true,
@@ -1149,7 +1167,10 @@ struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 		return ERR_PTR(err);
 
 	bo->created = true;
-	ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
+	if (bulk)
+		ttm_bo_set_bulk_move(&bo->ttm, bulk);
+	else
+		ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
 
 	return bo;
 }
@@ -1219,7 +1240,10 @@ xe_bo_create_locked_range(struct xe_device *xe,
 		}
 	}
 
-	bo = __xe_bo_create_locked(xe, bo, gt, vm ? &vm->resv : NULL, size,
+	bo = __xe_bo_create_locked(xe, bo, gt, vm ? &vm->resv : NULL,
+				   vm && !xe_vm_no_dma_fences(vm) &&
+				   flags & XE_BO_CREATE_USER_BIT ?
+				   &vm->lru_bulk_move : NULL, size,
 				   type, flags);
 	if (IS_ERR(bo))
 		return bo;
drivers/gpu/drm/xe/xe_bo.h +2 -2
index 8354d05ccdf3..25457b3c757b 100644
@@ -81,8 +81,8 @@ void xe_bo_free(struct xe_bo *bo);
 
 struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_gt *gt, struct dma_resv *resv,
-				    size_t size, enum ttm_bo_type type,
-				    u32 flags);
+				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    enum ttm_bo_type type, u32 flags);
 struct xe_bo *
 xe_bo_create_locked_range(struct xe_device *xe,
 			  struct xe_gt *gt, struct xe_vm *vm,
drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
index 9b252cc782b7..975dee1f770f 100644
@@ -199,7 +199,7 @@ xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage,
 	int ret;
 
 	dma_resv_lock(resv, NULL);
-	bo = __xe_bo_create_locked(xe, storage, NULL, resv, dma_buf->size,
+	bo = __xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size,
 				   ttm_bo_type_sg, XE_BO_CREATE_SYSTEM_BIT);
 	if (IS_ERR(bo)) {
 		ret = PTR_ERR(bo);
drivers/gpu/drm/xe/xe_exec.c +6 -0
index 44ea9bcd0066..21a9c2fddf86 100644
@@ -374,6 +374,12 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	xe_sched_job_push(job);
 	xe_vm_reactivate_rebind(vm);
 
+	if (!err && !xe_vm_no_dma_fences(vm)) {
+		spin_lock(&xe->ttm.lru_lock);
+		ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
+		spin_unlock(&xe->ttm.lru_lock);
+	}
+
 err_repin:
 	if (!xe_vm_no_dma_fences(vm))
 		up_read(&vm->userptr.notifier_lock);
drivers/gpu/drm/xe/xe_vm_types.h +3 -0
index fada7896867f..d3e99f22510d 100644
@@ -164,6 +164,9 @@ struct xe_vm {
 	/** Protects @rebind_list and the page-table structures */
 	struct dma_resv resv;
 
+	/** @lru_bulk_move: Bulk LRU move list for this VM's BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
+
 	u64 size;
 	struct rb_root vmas;
 
Signature · 1 line
2.34.1
#38 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 35 lines · Matthew Brost
Quoted 21 lines · Rodrigo Vivi
Quoted 20 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
 drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
 drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
 drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
 drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
 drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
 5 files changed, 40 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +0 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
 	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
 }
 
Quoted 12 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 9 lines
drivers/gpu/drm/xe/xe_bo.c +9 -0
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

We need to really stop using BUG_ON and move towards the usage of more WARNs.

Quoted 2 lines

If that is the direction, sure I'll change this but personally I BUG_ON for things that should be impossible with a correct KMD.

This is the current trend and official Kernel recommendation. Part of checkpatch:

# do not use BUG() or variants
                if ($line =~ /\b(?!AA_|BUILD_|DCCP_|IDA_|KVM_|RWLOCK_|snd_|SPIN_)(?:[a-zA-Z_]*_)?BUG(?:_ON)?(?:_[A-Z_]+)?\s*\(/) {
                        my $msg_level = \&WARN;
                        $msg_level = \&CHK if ($file);
                        &{$msg_level}("AVOID_BUG",
                                      "Do not crash the kernel unless it is absolutely unavoidable--use WARN_ON_ONCE() plus recovery code (if feasible) instead of BUG() or variants\n" . $herecurr);
                }
Quoted 122 lines

Matt

Quoted 119 lines

But the rest of the patch looks good to me... I just believe it would be good to get Thomas' review here.

Quoted 116 lines
drivers/gpu/drm/xe/xe_bo.c +19 -4
+
+		xe_bo_lock(bo, &ww, 0, false);
+		ttm_bo_set_bulk_move(&bo->ttm, NULL);
+		xe_bo_unlock(bo, &ww);
+	}
+}
+
+
 static bool should_migrate_to_system(struct xe_bo *bo)
 {
 	struct xe_device *xe = xe_bo_device(bo);
@@ -1040,6 +1057,7 @@ static const struct vm_operations_struct xe_gem_vm_ops = {
 
 static const struct drm_gem_object_funcs xe_gem_object_funcs = {
 	.free = xe_gem_object_free,
+	.close = xe_gem_object_close,
 	.mmap = drm_gem_ttm_mmap,
 	.export = xe_gem_prime_export,
 	.vm_ops = &xe_gem_vm_ops,
@@ -1081,8 +1099,8 @@ void xe_bo_free(struct xe_bo *bo)
 
 struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_gt *gt, struct dma_resv *resv,
-				    size_t size, enum ttm_bo_type type,
-				    u32 flags)
+				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    enum ttm_bo_type type, u32 flags)
 {
 	struct ttm_operation_ctx ctx = {
 		.interruptible = true,
@@ -1149,7 +1167,10 @@ struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 		return ERR_PTR(err);
 
 	bo->created = true;
-	ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
+	if (bulk)
+		ttm_bo_set_bulk_move(&bo->ttm, bulk);
+	else
+		ttm_bo_move_to_lru_tail_unlocked(&bo->ttm);
 
 	return bo;
 }
@@ -1219,7 +1240,10 @@ xe_bo_create_locked_range(struct xe_device *xe,
 		}
 	}
 
-	bo = __xe_bo_create_locked(xe, bo, gt, vm ? &vm->resv : NULL, size,
+	bo = __xe_bo_create_locked(xe, bo, gt, vm ? &vm->resv : NULL,
+				   vm && !xe_vm_no_dma_fences(vm) &&
+				   flags & XE_BO_CREATE_USER_BIT ?
+				   &vm->lru_bulk_move : NULL, size,
 				   type, flags);
 	if (IS_ERR(bo))
 		return bo;
drivers/gpu/drm/xe/xe_bo.h +2 -2
index 8354d05ccdf3..25457b3c757b 100644
@@ -81,8 +81,8 @@ void xe_bo_free(struct xe_bo *bo);
 
 struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_gt *gt, struct dma_resv *resv,
-				    size_t size, enum ttm_bo_type type,
-				    u32 flags);
+				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    enum ttm_bo_type type, u32 flags);
 struct xe_bo *
 xe_bo_create_locked_range(struct xe_device *xe,
 			  struct xe_gt *gt, struct xe_vm *vm,
drivers/gpu/drm/xe/xe_dma_buf.c +1 -1
index 9b252cc782b7..975dee1f770f 100644
@@ -199,7 +199,7 @@ xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage,
 	int ret;
 
 	dma_resv_lock(resv, NULL);
-	bo = __xe_bo_create_locked(xe, storage, NULL, resv, dma_buf->size,
+	bo = __xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size,
 				   ttm_bo_type_sg, XE_BO_CREATE_SYSTEM_BIT);
 	if (IS_ERR(bo)) {
 		ret = PTR_ERR(bo);
drivers/gpu/drm/xe/xe_exec.c +6 -0
index 44ea9bcd0066..21a9c2fddf86 100644
@@ -374,6 +374,12 @@ int xe_exec_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	xe_sched_job_push(job);
 	xe_vm_reactivate_rebind(vm);
 
+	if (!err && !xe_vm_no_dma_fences(vm)) {
+		spin_lock(&xe->ttm.lru_lock);
+		ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
+		spin_unlock(&xe->ttm.lru_lock);
+	}
+
 err_repin:
 	if (!xe_vm_no_dma_fences(vm))
 		up_read(&vm->userptr.notifier_lock);
drivers/gpu/drm/xe/xe_vm_types.h +3 -1
index fada7896867f..d3e99f22510d 100644
@@ -164,6 +164,9 @@ struct xe_vm {
 	/** Protects @rebind_list and the page-table structures */
 	struct dma_resv resv;
 
+	/** @lru_bulk_move: Bulk LRU move list for this VM's BOs */
+	struct ttm_lru_bulk_move lru_bulk_move;
+
 	u64 size;
 	struct rb_root vmas;
 
--

2.34.1

#39 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

In reply to Matthew Brost
Quoted 17 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
  drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
  drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
  drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
  drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
  drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
  5 files changed, 40 insertions(+), 7 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +0 -0
index 3ab404e33fae..da99ee53e7d7 100644

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 993

Quoted 9 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
  	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
  }
 
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {

Is there a reason we don't use bulk moves for LR vms? Admittedly bumping LRU doesn't make much sense when we support user-space command buffer chaining, but I think we should be doing it on exec at least, no?

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

Also why can't we use this for kernel objects as well? At some point we want to get to evictable page-table objects? Could we do this in the release_notify() callback to cover all potential bos?

/Thomas

author maintainer
#40 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 14 lines · Thomas Hellström
Quoted 12 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
  drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
  drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
  drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
  drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
  drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
  5 files changed, 40 insertions(+), 7 deletions(-)
Quoted 16 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 992

Quoted 13 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
  	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
  }
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {

Is there a reason we don't use bulk moves for LR vms? Admittedly bumping LRU doesn't make much sense when we support user-space command buffer chaining, but I think we should be doing it on exec at least, no?

Maybe you could make the argument for compute VMs, the preempt worker in that case should probably do a bulk move. I can change this if desired.

Fot a fault VM it makes no sense as the fault handler updates the LRU for individual BOs.

Quoted 8 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

Also why can't we use this for kernel objects as well? At some point we want to get to evictable page-table objects? Could we do this in the release_notify() callback to cover all potential bos?

xe_gem_object_close is a user call, right? We can't call this on kernel BOs. This also could be outside the if statement.

Matt

Quoted 3 lines

/Thomas

#41 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 30 lines · Matthew Brost
Quoted 13 lines · Thomas Hellström
Quoted 12 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
   drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
   drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
   drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
   drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
   drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
   5 files changed, 40 insertions(+), 7 deletions(-)
Quoted 16 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 992

Quoted 13 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
   	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
   }
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {

Is there a reason we don't use bulk moves for LR vms? Admittedly bumping LRU doesn't make much sense when we support user-space command buffer chaining, but I think we should be doing it on exec at least, no?

Quoted 2 lines

Maybe you could make the argument for compute VMs, the preempt worker in that case should probably do a bulk move. I can change this if desired.

Yes, please.

Quoted 3 lines

Fot a fault VM it makes no sense as the fault handler updates the LRU for individual BOs.

Yes that makes sense.

Quoted 10 lines
Quoted 8 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 4 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

Also why can't we use this for kernel objects as well? At some point we want to get to evictable page-table objects? Could we do this in the release_notify() callback to cover all potential bos?

xe_gem_object_close is a user call, right? We can't call this on kernel BOs. This also could be outside the if statement.

Hmm, yes the question was can we stop doing this in xe_gem_object_close() and instead do it in release_notify() to cover also kernel objects. Since release_notify() is called just after individualizing dma_resv, it makes sense to individualize also LRU at that point?

/Thomas

Quoted 6 lines

Matt

Quoted 3 lines

/Thomas

author maintainer
#42 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 47 lines · Thomas Hellström
Quoted 30 lines · Matthew Brost
Quoted 13 lines · Thomas Hellström
Quoted 12 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
   drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
   drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
   drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
   drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
   drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
   5 files changed, 40 insertions(+), 7 deletions(-)
Quoted 16 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 992

Quoted 13 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
   	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
   }
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {

Is there a reason we don't use bulk moves for LR vms? Admittedly bumping LRU doesn't make much sense when we support user-space command buffer chaining, but I think we should be doing it on exec at least, no?

Quoted 2 lines

Maybe you could make the argument for compute VMs, the preempt worker in that case should probably do a bulk move. I can change this if desired.

Yes, please.

Quoted 3 lines

Fot a fault VM it makes no sense as the fault handler updates the LRU for individual BOs.

Yes that makes sense.

Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 4 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

Also why can't we use this for kernel objects as well? At some point we want to get to evictable page-table objects? Could we do this in the release_notify() callback to cover all potential bos?

Quoted 8 lines
Quoted 2 lines

xe_gem_object_close is a user call, right? We can't call this on kernel BOs. This also could be outside the if statement.

Hmm, yes the question was can we stop doing this in xe_gem_object_close() and instead do it in release_notify() to cover also kernel objects. Since release_notify() is called just after individualizing dma_resv, it makes sense to individualize also LRU at that point?

If we ever support moving kernel BOs, then yes. We need to do a lot of work to get there, with I'd rather leave this where is but I'll add a comment indicating if we want to support kernel BO eviction, this should be updated.

Sound good?

Matt

Quoted 9 lines

/Thomas

Quoted 6 lines

Matt

Quoted 3 lines

/Thomas

#43 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 52 lines · Matthew Brost
Quoted 44 lines · Thomas Hellström
Quoted 30 lines · Matthew Brost
Quoted 13 lines · Thomas Hellström
Quoted 12 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
    drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
    drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
    drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
    drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
    drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
    5 files changed, 40 insertions(+), 7 deletions(-)
Quoted 16 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 992

Quoted 13 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
    	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
    }
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {

Is there a reason we don't use bulk moves for LR vms? Admittedly bumping LRU doesn't make much sense when we support user-space command buffer chaining, but I think we should be doing it on exec at least, no?

Quoted 2 lines

Maybe you could make the argument for compute VMs, the preempt worker in that case should probably do a bulk move. I can change this if desired.

Yes, please.

Quoted 2 lines

Fot a fault VM it makes no sense as the fault handler updates the LRU for individual BOs.

Yes that makes sense.

Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

Also why can't we use this for kernel objects as well? At some point we want to get to evictable page-table objects? Could we do this in the release_notify() callback to cover all potential bos?

Quoted 7 lines
Quoted 2 lines

xe_gem_object_close is a user call, right? We can't call this on kernel BOs. This also could be outside the if statement.

Hmm, yes the question was can we stop doing this in xe_gem_object_close() and instead do it in release_notify() to cover also kernel objects. Since release_notify() is called just after individualizing dma_resv, it makes sense to individualize also LRU at that point?

Quoted 6 lines

If we ever support moving kernel BOs, then yes. We need to do a lot of work to get there, with I'd rather leave this where is but I'll add a comment indicating if we want to support kernel BO eviction, this should be updated.

Sound good?

Well, I can't see the motivation to have it in gem close? Are other drivers doing that? Whether the object should be bulk moved or not is tied to whether it's a vm private object or not and that is closely tied to whether the reservation object is the vm resv or the object resv?

/Thomas

Quoted 11 lines

Matt

Quoted 8 lines

/Thomas

Quoted 5 lines

Matt

Quoted 3 lines

/Thomas

author maintainer
#44 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 59 lines · Thomas Hellström
Quoted 57 lines · Matthew Brost
Quoted 44 lines · Thomas Hellström
Quoted 30 lines · Matthew Brost
Quoted 13 lines · Thomas Hellström
Quoted 12 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
    drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
    drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
    drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
    drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
    drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
    5 files changed, 40 insertions(+), 7 deletions(-)
Quoted 16 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 992

Quoted 13 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
    	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
    }
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {

Is there a reason we don't use bulk moves for LR vms? Admittedly bumping LRU doesn't make much sense when we support user-space command buffer chaining, but I think we should be doing it on exec at least, no?

Quoted 2 lines

Maybe you could make the argument for compute VMs, the preempt worker in that case should probably do a bulk move. I can change this if desired.

Yes, please.

Quoted 2 lines

Fot a fault VM it makes no sense as the fault handler updates the LRU for individual BOs.

Yes that makes sense.

Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 996

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

Also why can't we use this for kernel objects as well? At some point we want to get to evictable page-table objects? Could we do this in the release_notify() callback to cover all potential bos?

Quoted 7 lines
Quoted 2 lines

xe_gem_object_close is a user call, right? We can't call this on kernel BOs. This also could be outside the if statement.

Hmm, yes the question was can we stop doing this in xe_gem_object_close() and instead do it in release_notify() to cover also kernel objects. Since release_notify() is called just after individualizing dma_resv, it makes sense to individualize also LRU at that point?

If we ever support moving kernel BOs, then yes. We need to do a lot of work to get there, with I'd rather leave this where is but I'll add a comment indicating if we want to support kernel BO eviction, this should be updated.

Quoted 7 lines
Quoted 1 line

Sound good?

Well, I can't see the motivation to have it in gem close? Are other drivers doing that? Whether the object should be bulk moved or not is tied to whether it's a vm private object or not and that is closely tied to whether the reservation object is the vm resv or the object resv?

AMDGPU does via amdgpu_gem_object_close -> amdgpu_vm_bo_del, so yes.

I also think I moved it here as before release_notify() I think there is an assert TTM for the bulk move being NULL, let me find that.

319 static void ttm_bo_release(struct kref *kref) 320 {
321 struct ttm_buffer_object *bo =
322 container_of(kref, struct ttm_buffer_object, kref);
323 struct ttm_device *bdev = bo->bdev;
324 int ret;
325
326 WARN_ON_ONCE(bo->pin_count);
327 WARN_ON_ONCE(bo->bulk_move);

Matt

Quoted 13 lines

/Thomas

Quoted 11 lines

Matt

Quoted 8 lines

/Thomas

Quoted 5 lines

Matt

Quoted 3 lines

/Thomas

#45 lore raw

Re: [Intel-xe] [PATCH v2 08/31] drm/xe: VM LRU bulk move

Quoted 70 lines · Matthew Brost
Quoted 58 lines · Thomas Hellström
Quoted 57 lines · Matthew Brost
Quoted 44 lines · Thomas Hellström
Quoted 30 lines · Matthew Brost
Quoted 13 lines · Thomas Hellström
Quoted 12 lines · Matthew Brost

Use the TTM LRU bulk move for BOs tied to a VM. Update the bulk moves LRU position on every exec.

---
     drivers/gpu/drm/xe/xe_bo.c       | 32 ++++++++++++++++++++++++++++----
     drivers/gpu/drm/xe/xe_bo.h       |  4 ++--
     drivers/gpu/drm/xe/xe_dma_buf.c  |  2 +-
     drivers/gpu/drm/xe/xe_exec.c     |  6 ++++++
     drivers/gpu/drm/xe/xe_vm_types.h |  3 +++
     5 files changed, 40 insertions(+), 7 deletions(-)
Quoted 16 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 992

Quoted 13 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
index 3ab404e33fae..da99ee53e7d7 100644
@@ -985,6 +985,23 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
     	ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
     }
+static void xe_gem_object_close(struct drm_gem_object *obj,
+				struct drm_file *file_priv)
+{
+	struct xe_bo *bo = gem_to_xe_bo(obj);
+
+	if (bo->vm && !xe_vm_no_dma_fences(bo->vm)) {

Is there a reason we don't use bulk moves for LR vms? Admittedly bumping LRU doesn't make much sense when we support user-space command buffer chaining, but I think we should be doing it on exec at least, no?

Quoted 2 lines

Maybe you could make the argument for compute VMs, the preempt worker in that case should probably do a bulk move. I can change this if desired.

Yes, please.

Quoted 2 lines

Fot a fault VM it makes no sense as the fault handler updates the LRU for individual BOs.

Yes that makes sense.

Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_gem_object_free(struct drm_gem_object *obj) · line 995

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct ww_acquire_ctx ww;
+
+		XE_BUG_ON(!xe_bo_is_user(bo));

Also why can't we use this for kernel objects as well? At some point we want to get to evictable page-table objects? Could we do this in the release_notify() callback to cover all potential bos?

Quoted 7 lines
Quoted 2 lines

xe_gem_object_close is a user call, right? We can't call this on kernel BOs. This also could be outside the if statement.

Hmm, yes the question was can we stop doing this in xe_gem_object_close() and instead do it in release_notify() to cover also kernel objects. Since release_notify() is called just after individualizing dma_resv, it makes sense to individualize also LRU at that point?

If we ever support moving kernel BOs, then yes. We need to do a lot of work to get there, with I'd rather leave this where is but I'll add a comment indicating if we want to support kernel BO eviction, this should be updated.

Quoted 6 lines
Quoted 1 line

Sound good?

Well, I can't see the motivation to have it in gem close? Are other drivers doing that? Whether the object should be bulk moved or not is tied to whether it's a vm private object or not and that is closely tied to whether the reservation object is the vm resv or the object resv?

AMDGPU does via amdgpu_gem_object_close -> amdgpu_vm_bo_del, so yes.

I also think I moved it here as before release_notify() I think there is an assert TTM for the bulk move being NULL, let me find that.

Quoted 9 lines
  319 static void ttm_bo_release(struct kref *kref)
  320 {
  321         struct ttm_buffer_object *bo =
  322             container_of(kref, struct ttm_buffer_object, kref);
  323         struct ttm_device *bdev = bo->bdev;
  324         int ret;
  325
  326         WARN_ON_ONCE(bo->pin_count);
  327         WARN_ON_ONCE(bo->bulk_move);

Ugh, that's unfortunate.

In any case, it looks like if a client has multiple handles to the object, the close() callback will be called multiple times, and the bulk object released on the first, right?

The second best option would I guess then be to have it in xe_gem_object_free(), I suppose, but the problem with that is the potentially sleeping uninterruptible object lock :(.

If we could have it in release_notify() we already have the object lock.

So can we have it in xe_gem_object_free() for now and later perhaps ping Christian about moving that WARN_ON_ONCE?

/Thomas

Quoted 14 lines

Matt

Quoted 12 lines

/Thomas

Quoted 10 lines

Matt

Quoted 8 lines

/Thomas

Quoted 5 lines

Matt

Quoted 3 lines

/Thomas

author maintainer
#46 lore raw

PATCH v2 9/31 drm/xe/guc: Read HXG fields from DW1 of G2H response

In reply to Matthew Brost

The HXG fields are DW1 not DW0, fix this.

---
 drivers/gpu/drm/xe/xe_guc_ct.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_guc_ct.c +3 -3
index 9055ff133a7c..6abf1dee95af 100644
@@ -782,13 +782,13 @@ static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len)
 	if (type == GUC_HXG_TYPE_RESPONSE_FAILURE) {
 		g2h_fence->fail = true;
 		g2h_fence->error =
-			FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, msg[0]);
+			FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, msg[1]);
 		g2h_fence->hint =
-			FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, msg[0]);
+			FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, msg[1]);
 	} else if (type == GUC_HXG_TYPE_NO_RESPONSE_RETRY) {
 		g2h_fence->retry = true;
 		g2h_fence->reason =
-			FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, msg[0]);
+			FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, msg[1]);
 	} else if (g2h_fence->response_buffer) {
 		g2h_fence->response_len = response_len;
 		memcpy(g2h_fence->response_buffer, msg + GUC_CTB_MSG_MIN_LEN,
Signature · 1 line
2.34.1
Reviewed-by on v2 9/31
#47 lore raw

Re: [Intel-xe] [PATCH v2 09/31] drm/xe/guc: Read HXG fields from DW1 of G2H response

Quoted 1 line · Matthew Brost

The HXG fields are DW1 not DW0, fix this.

it took me a while to understand how the msg was constructed to agree with this... I believe we should transform this into some struct with embedded u32 with some proper names to make it clear and avoid future mistakes like this.

I also believe this patch and any other like this should be resend individually so we get the fixes already in place.

Quoted 30 lines
---
 drivers/gpu/drm/xe/xe_guc_ct.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_guc_ct.c +3 -3
index 9055ff133a7c..6abf1dee95af 100644
@@ -782,13 +782,13 @@ static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len)
 	if (type == GUC_HXG_TYPE_RESPONSE_FAILURE) {
 		g2h_fence->fail = true;
 		g2h_fence->error =
-			FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, msg[0]);
+			FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, msg[1]);
 		g2h_fence->hint =
-			FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, msg[0]);
+			FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, msg[1]);
 	} else if (type == GUC_HXG_TYPE_NO_RESPONSE_RETRY) {
 		g2h_fence->retry = true;
 		g2h_fence->reason =
-			FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, msg[0]);
+			FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, msg[1]);
 	} else if (g2h_fence->response_buffer) {
 		g2h_fence->response_len = response_len;
 		memcpy(g2h_fence->response_buffer, msg + GUC_CTB_MSG_MIN_LEN,
Signature · 1 line
2.34.1
maintainer Acked-by on v2 9/31
#48 lore raw

Re: [Intel-xe] [PATCH v2 09/31] drm/xe/guc: Read HXG fields from DW1 of G2H response

In reply to Matthew Brost
Quoted 3 lines · Matthew Brost

The HXG fields are DW1 not DW0, fix this.

Quoted 25 lines
---
  drivers/gpu/drm/xe/xe_guc_ct.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_guc_ct.c +3 -3
index 9055ff133a7c..6abf1dee95af 100644
@@ -782,13 +782,13 @@ static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len)
  	if (type == GUC_HXG_TYPE_RESPONSE_FAILURE) {
  		g2h_fence->fail = true;
  		g2h_fence->error =
-			FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, msg[0]);
+			FIELD_GET(GUC_HXG_FAILURE_MSG_0_ERROR, msg[1]);
  		g2h_fence->hint =
-			FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, msg[0]);
+			FIELD_GET(GUC_HXG_FAILURE_MSG_0_HINT, msg[1]);
  	} else if (type == GUC_HXG_TYPE_NO_RESPONSE_RETRY) {
  		g2h_fence->retry = true;
  		g2h_fence->reason =
-			FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, msg[0]);
+			FIELD_GET(GUC_HXG_RETRY_MSG_0_REASON, msg[1]);
  	} else if (g2h_fence->response_buffer) {
  		g2h_fence->response_len = response_len;
  		memcpy(g2h_fence->response_buffer, msg + GUC_CTB_MSG_MIN_LEN,
author maintainer
#49 lore raw

PATCH v2 10/31 drm/xe/guc: Return the lower part of blocking H2G message

In reply to Matthew Brost

The upper layers may need this data, an example of this is allocating DIST doorbell.

---
 drivers/gpu/drm/xe/xe_guc_ct.c | 6 +++++-
 drivers/gpu/drm/xe/xe_guc_pc.c | 6 ++++--
 drivers/gpu/drm/xe/xe_huc.c    | 2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/xe_guc_ct.c +5 -1
index 6abf1dee95af..60b69fcfac9f 100644
@@ -25,6 +25,7 @@
 struct g2h_fence {
 	u32 *response_buffer;
 	u32 seqno;
+	u32 status;
 	u16 response_len;
 	u16 error;
 	u16 hint;
@@ -727,7 +728,7 @@ static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
 		ret = -EIO;
 	}
 
-	return ret > 0 ? 0 : ret;
+	return ret > 0 ? g2h_fence.status : ret;
 }
 
 int xe_guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
@@ -793,6 +794,9 @@ static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len)
 		g2h_fence->response_len = response_len;
 		memcpy(g2h_fence->response_buffer, msg + GUC_CTB_MSG_MIN_LEN,
 		       response_len * sizeof(u32));
+	} else {
+		g2h_fence->status =
+			FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, msg[1]);
 	}
 
 	g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN);
drivers/gpu/drm/xe/xe_guc_pc.c +4 -2
index 72d460d5323b..3d2ea723a4a7 100644
@@ -204,11 +204,13 @@ static int pc_action_query_task_state(struct xe_guc_pc *pc)
 
 	/* Blocking here to ensure the results are ready before reading them */
 	ret = xe_guc_ct_send_block(ct, action, ARRAY_SIZE(action));
-	if (ret)
+	if (ret < 0) {
 		drm_err(&pc_to_xe(pc)->drm,
 			"GuC PC query task state failed: %pe", ERR_PTR(ret));
+		return ret;
+	}
 
-	return ret;
+	return 0;
 }
 
 static int pc_action_set_param(struct xe_guc_pc *pc, u8 id, u32 value)
drivers/gpu/drm/xe/xe_huc.c +1 -1
index 55dcaab34ea4..9c48c3075410 100644
@@ -39,7 +39,7 @@ int xe_huc_init(struct xe_huc *huc)
 
 	huc->fw.type = XE_UC_FW_TYPE_HUC;
 	ret = xe_uc_fw_init(&huc->fw);
-	if (ret)
+	if (ret < 0)
 		goto out;
 
 	xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_LOADABLE);
Signature · 1 line
2.34.1
#50 lore raw

Re: [Intel-xe] [PATCH v2 10/31] drm/xe/guc: Return the lower part of blocking H2G message

Quoted 23 lines · Matthew Brost

The upper layers may need this data, an example of this is allocating DIST doorbell.

---
 drivers/gpu/drm/xe/xe_guc_ct.c | 6 +++++-
 drivers/gpu/drm/xe/xe_guc_pc.c | 6 ++++--
 drivers/gpu/drm/xe/xe_huc.c    | 2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/xe_guc_ct.c +1 -0
index 6abf1dee95af..60b69fcfac9f 100644
@@ -25,6 +25,7 @@
 struct g2h_fence {
 	u32 *response_buffer;
 	u32 seqno;
+	u32 status;
 	u16 response_len;
 	u16 error;
 	u16 hint;

drivers/gpu/drm/xe/xe_guc_ct.c static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len, · line 731

Quoted 5 lines
drivers/gpu/drm/xe/xe_guc_ct.c +1 -1
@@ -727,7 +728,7 @@ static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
 		ret = -EIO;
 	}
 
-	return ret > 0 ? 0 : ret;
+	return ret > 0 ? g2h_fence.status : ret;

The problem I see here is how the upper level could differentiate between and error and a status.

should we convert the functions to have an &status argument passed in?

Quoted 49 lines
drivers/gpu/drm/xe/xe_guc_ct.c +3 -0
 }
 
 int xe_guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
@@ -793,6 +794,9 @@ static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len)
 		g2h_fence->response_len = response_len;
 		memcpy(g2h_fence->response_buffer, msg + GUC_CTB_MSG_MIN_LEN,
 		       response_len * sizeof(u32));
+	} else {
+		g2h_fence->status =
+			FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, msg[1]);
 	}
 
 	g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN);
drivers/gpu/drm/xe/xe_guc_pc.c +4 -2
index 72d460d5323b..3d2ea723a4a7 100644
@@ -204,11 +204,13 @@ static int pc_action_query_task_state(struct xe_guc_pc *pc)
 
 	/* Blocking here to ensure the results are ready before reading them */
 	ret = xe_guc_ct_send_block(ct, action, ARRAY_SIZE(action));
-	if (ret)
+	if (ret < 0) {
 		drm_err(&pc_to_xe(pc)->drm,
 			"GuC PC query task state failed: %pe", ERR_PTR(ret));
+		return ret;
+	}
 
-	return ret;
+	return 0;
 }
 
 static int pc_action_set_param(struct xe_guc_pc *pc, u8 id, u32 value)
drivers/gpu/drm/xe/xe_huc.c +1 -1
index 55dcaab34ea4..9c48c3075410 100644
@@ -39,7 +39,7 @@ int xe_huc_init(struct xe_huc *huc)
 
 	huc->fw.type = XE_UC_FW_TYPE_HUC;
 	ret = xe_uc_fw_init(&huc->fw);
-	if (ret)
+	if (ret < 0)
 		goto out;
 
 	xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_LOADABLE);
Signature · 1 line
2.34.1
author maintainer
#51 lore raw

Re: [Intel-xe] [PATCH v2 10/31] drm/xe/guc: Return the lower part of blocking H2G message

Quoted 27 lines · Rodrigo Vivi
Quoted 26 lines · Matthew Brost

The upper layers may need this data, an example of this is allocating DIST doorbell.

---
 drivers/gpu/drm/xe/xe_guc_ct.c | 6 +++++-
 drivers/gpu/drm/xe/xe_guc_pc.c | 6 ++++--
 drivers/gpu/drm/xe/xe_huc.c    | 2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/xe_guc_ct.c +1 -0
index 6abf1dee95af..60b69fcfac9f 100644
@@ -25,6 +25,7 @@
 struct g2h_fence {
 	u32 *response_buffer;
 	u32 seqno;
+	u32 status;
 	u16 response_len;
 	u16 error;
 	u16 hint;
@@ -727,7 +728,7 @@ static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
 		ret = -EIO;
 	}
 
Quoted 6 lines

drivers/gpu/drm/xe/xe_guc_ct.c static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len, · line 731

Quoted 2 lines
drivers/gpu/drm/xe/xe_guc_ct.c +1 -1
-	return ret > 0 ? 0 : ret;
+	return ret > 0 ? g2h_fence.status : ret;

The problem I see here is how the upper level could differentiate between and error and a status.

g2h_fence.status is 16 (can't be negative), so 0 or greater is a good return.

Quoted 2 lines

should we convert the functions to have an &status argument passed in?

I like it the way it is but don't really care either way. I'll change this if you like.

Matt

Quoted 49 lines
Quoted 49 lines
drivers/gpu/drm/xe/xe_guc_ct.c +3 -0
 }
 
 int xe_guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
@@ -793,6 +794,9 @@ static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len)
 		g2h_fence->response_len = response_len;
 		memcpy(g2h_fence->response_buffer, msg + GUC_CTB_MSG_MIN_LEN,
 		       response_len * sizeof(u32));
+	} else {
+		g2h_fence->status =
+			FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, msg[1]);
 	}
 
 	g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN);
drivers/gpu/drm/xe/xe_guc_pc.c +4 -2
index 72d460d5323b..3d2ea723a4a7 100644
@@ -204,11 +204,13 @@ static int pc_action_query_task_state(struct xe_guc_pc *pc)
 
 	/* Blocking here to ensure the results are ready before reading them */
 	ret = xe_guc_ct_send_block(ct, action, ARRAY_SIZE(action));
-	if (ret)
+	if (ret < 0) {
 		drm_err(&pc_to_xe(pc)->drm,
 			"GuC PC query task state failed: %pe", ERR_PTR(ret));
+		return ret;
+	}
 
-	return ret;
+	return 0;
 }
 
 static int pc_action_set_param(struct xe_guc_pc *pc, u8 id, u32 value)
drivers/gpu/drm/xe/xe_huc.c +1 -1
index 55dcaab34ea4..9c48c3075410 100644
@@ -39,7 +39,7 @@ int xe_huc_init(struct xe_huc *huc)
 
 	huc->fw.type = XE_UC_FW_TYPE_HUC;
 	ret = xe_uc_fw_init(&huc->fw);
-	if (ret)
+	if (ret < 0)
 		goto out;
 
 	xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_LOADABLE);
Signature · 1 line
2.34.1
#52 lore raw

Re: [Intel-xe] [PATCH v2 10/31] drm/xe/guc: Return the lower part of blocking H2G message

Quoted 12 lines · Matthew Brost
Quoted 11 lines · Rodrigo Vivi
Quoted 10 lines · Matthew Brost

The upper layers may need this data, an example of this is allocating DIST doorbell.

---
 drivers/gpu/drm/xe/xe_guc_ct.c | 6 +++++-
 drivers/gpu/drm/xe/xe_guc_pc.c | 6 ++++--
 drivers/gpu/drm/xe/xe_huc.c    | 2 +-
 3 files changed, 10 insertions(+), 4 deletions(-)
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/xe/xe_guc_ct.c line 28

Quoted 9 lines
drivers/gpu/drm/xe/xe_guc_ct.c +1 -0
index 6abf1dee95af..60b69fcfac9f 100644
@@ -25,6 +25,7 @@
 struct g2h_fence {
 	u32 *response_buffer;
 	u32 seqno;
+	u32 status;

if purpose of this field is to hold data from the success reply, then why it is called as misleading 'status' ?

in our spec we call it 'data0' see [1]

[1] https://www.kernel.org/doc/html/latest/gpu/i915.html?#hxg-response

Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/xe/xe_guc_ct.c line 31

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_ct.c +0 -0
 	u16 response_len;
 	u16 error;
 	u16 hint;

btw, is there any true benefit to decompose HXG replies into these fields in g2h_fence ? done/retry/fail modes are mutually exclusive, while separate flags here can't guarantee that

Quoted 11 lines
Quoted 10 lines

drivers/gpu/drm/xe/xe_guc_ct.c static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len, · line 731

Quoted 6 lines
drivers/gpu/drm/xe/xe_guc_ct.c +1 -1
@@ -727,7 +728,7 @@ static int guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
 		ret = -EIO;
 	}
 
-	return ret > 0 ? 0 : ret;
+	return ret > 0 ? g2h_fence.status : ret;

The problem I see here is how the upper level could differentiate between and error and a status.

Quoted 2 lines

g2h_fence.status is 16 (can't be negative), so 0 or greater is a good return.

to be precise, data0 from the success reply is 28-bit (see [1] above)

you probably mixed that with 16-bit error from failure reply (see [2])

[2] https://www.kernel.org/doc/html/latest/gpu/i915.html?#hxg-failure

Quoted 2 lines
Quoted 2 lines

should we convert the functions to have an &status argument passed in?

it is not 'status'

Quoted 4 lines
Quoted 1 line

I like it the way it is but don't really care either way. I'll change this if you like.

HXG spec allows reply messages longer than 1dw (1dw = HXG header only) thus if we claim support for such use case, as we do accept response_buffer, then we should return actual length of the received reply, rather than just data0, to allow caller parse flex size replies:

* Return: Non-negative response length (in dwords) or
* a negative error code on failure.

but since majority, if not all, of our currently defined H2G defines just data0, without extra data1, data2, ... then for such cases we should rather provide separate wrapper that will make sure reply size is exactly 1 (header only) and return extracted data0 from it.

and since data0 is just 28-bit we can still use single return value documented as:

* Return: Non-negative data0 from the success response or
* a negative error code on failure.

(as I still hope proper documentation will be prepared for core functions near term, when it's most desired)

Quoted 22 lines

Matt

Quoted 19 lines
Quoted 19 lines
drivers/gpu/drm/xe/xe_guc_ct.c +3 -0
 }
 
 int xe_guc_ct_send_recv(struct xe_guc_ct *ct, const u32 *action, u32 len,
@@ -793,6 +794,9 @@ static int parse_g2h_response(struct xe_guc_ct *ct, u32 *msg, u32 len)
 		g2h_fence->response_len = response_len;
 		memcpy(g2h_fence->response_buffer, msg + GUC_CTB_MSG_MIN_LEN,
 		       response_len * sizeof(u32));
+	} else {
+		g2h_fence->status =
+			FIELD_GET(GUC_HXG_RESPONSE_MSG_0_DATA0, msg[1]);
 	}
 
 	g2h_release_space(ct, GUC_CTB_HXG_MSG_MAX_LEN);
drivers/gpu/drm/xe/xe_guc_pc.c +0 -0
index 72d460d5323b..3d2ea723a4a7 100644
@@ -204,11 +204,13 @@ static int pc_action_query_task_state(struct xe_guc_pc *pc)
 
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_guc_pc.c static int pc_action_query_task_state(struct xe_guc_pc *pc) · line 207

Quoted 4 lines
drivers/gpu/drm/xe/xe_guc_pc.c +1 -1
 	/* Blocking here to ensure the results are ready before reading them */
 	ret = xe_guc_ct_send_block(ct, action, ARRAY_SIZE(action));
-	if (ret)
+	if (ret < 0) {

since almost all H2G expects data0 == 0 on success, this fix shouldn't be necessary, unless you are hiding other issue ... which in this case is lack of proper initialization of the 'status/data0' field in g2h_fence_init()

and btw, long term we should likely be better prepared for more unexpected replies from the GuC, including non-zero data0 replies ... but that should be another patch anyway

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/xe/xe_guc_pc.c +3 -1
 		drm_err(&pc_to_xe(pc)->drm,
 			"GuC PC query task state failed: %pe", ERR_PTR(ret));
+		return ret;
+	}
 
-	return ret;
+	return 0;
 }
 
 static int pc_action_set_param(struct xe_guc_pc *pc, u8 id, u32 value)
drivers/gpu/drm/xe/xe_huc.c +0 -0
index 55dcaab34ea4..9c48c3075410 100644
@@ -39,7 +39,7 @@ int xe_huc_init(struct xe_huc *huc)
 
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_huc.c int xe_huc_init(struct xe_huc *huc) · line 42

Quoted 4 lines
drivers/gpu/drm/xe/xe_huc.c +1 -1
 	huc->fw.type = XE_UC_FW_TYPE_HUC;
 	ret = xe_uc_fw_init(&huc->fw);
-	if (ret)
+	if (ret < 0)

ditto

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/xe/xe_huc.c +0 -0
 		goto out;
 
 	xe_uc_fw_change_status(&huc->fw, XE_UC_FIRMWARE_LOADABLE);
Signature · 1 line
2.34.1
author maintainer
#53 lore raw

PATCH v2 11/31 drm/xe/guc: Use doorbells for submission if possible

In reply to Matthew Brost

We have 256 doorbells (on most platforms) that we can allocate to bypass using the H2G channel for submission. This will avoid contention on the CT mutex.

---
 drivers/gpu/drm/xe/regs/xe_guc_regs.h    |   1 +
 drivers/gpu/drm/xe/xe_guc.c              |   6 +
 drivers/gpu/drm/xe/xe_guc_engine_types.h |   7 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 295 ++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_guc_submit.h       |   1 +
 drivers/gpu/drm/xe/xe_guc_types.h        |   4 +
 drivers/gpu/drm/xe/xe_trace.h            |   5 +
 7 files changed, 315 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/regs/xe_guc_regs.h +1 -0
index 37e0ac550931..11b117293a62 100644
@@ -109,6 +109,7 @@ struct guc_doorbell_info {
 
 #define DIST_DBS_POPULATED			XE_REG(0xd08)
 #define   DOORBELLS_PER_SQIDI_MASK		REG_GENMASK(23, 16)
+#define	  DOORBELLS_PER_SQIDI_SHIFT		16
 #define   SQIDIS_DOORBELL_EXIST_MASK		REG_GENMASK(15, 0)
 
 #define GUC_BCS_RCS_IER				XE_REG(0xC550)
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 89d20faced19..0c87f78a868b 100644
@@ -297,6 +297,12 @@ int xe_guc_init(struct xe_guc *guc)
  */
 int xe_guc_init_post_hwconfig(struct xe_guc *guc)
 {
+	int ret;
+
+	ret = xe_guc_submit_init_post_hwconfig(guc);
+	if (ret)
+		return ret;
+
 	return xe_guc_ads_init_post_hwconfig(&guc->ads);
 }
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +7 -0
index 5d83132034a6..420b7f53e649 100644
@@ -12,6 +12,7 @@
 #include <drm/gpu_scheduler.h>
 
 struct dma_fence;
+struct xe_bo;
 struct xe_engine;
 
 /**
@@ -37,6 +38,10 @@ struct xe_guc_engine {
 	struct work_struct fini_async;
 	/** @resume_time: time of last resume */
 	u64 resume_time;
+	/** @doorbell_bo: BO for memory doorbell */
+	struct xe_bo *doorbell_bo;
+	/** @doorbell_offset: MMIO doorbell offset */
+	u32 doorbell_offset;
 	/** @state: GuC specific state for this xe_engine */
 	atomic_t state;
 	/** @wqi_head: work queue item tail */
@@ -45,6 +50,8 @@ struct xe_guc_engine {
 	u32 wqi_tail;
 	/** @id: GuC id for this xe_engine */
 	u16 id;
+	/** @doorbell_id: doorbell id */
+	u16 doorbell_id;
 	/** @suspend_wait: wait queue used to wait on pending suspends */
 	wait_queue_head_t suspend_wait;
 	/** @suspend_pending: a suspend of the engine is pending */
drivers/gpu/drm/xe/xe_guc_submit.c +291 -4

400 lines not on this page · raw message

drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 8002734d6f24..bada6c02d6aa 100644
@@ -13,6 +13,7 @@ struct xe_engine;
 struct xe_guc;
 
 int xe_guc_submit_init(struct xe_guc *guc);
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc);
 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p);
 
 int xe_guc_submit_reset_prepare(struct xe_guc *guc);
drivers/gpu/drm/xe/xe_guc_types.h +4 -0
index ac7eec28934d..9ee4d572f4e0 100644
@@ -36,10 +36,14 @@ struct xe_guc {
 		struct xarray engine_lookup;
 		/** @guc_ids: used to allocate new guc_ids, single-lrc */
 		struct ida guc_ids;
+		/** @doorbell_ids: use to allocate new doorbells */
+		struct ida doorbell_ids;
 		/** @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc */
 		unsigned long *guc_ids_bitmap;
 		/** @stopped: submissions are stopped */
 		atomic_t stopped;
+		/** @num_doorbells: number of doorbels */
+		int num_doorbells;
 		/** @lock: protects submission state */
 		struct mutex lock;
 		/** @suspend: suspend fence state */
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 02861c26e145..38e9d7c6197b 100644
@@ -149,6 +149,11 @@ DEFINE_EVENT(xe_engine, xe_engine_submit,
 	     TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_ring_db,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
 DEFINE_EVENT(xe_engine, xe_engine_scheduling_enable,
 	     TP_PROTO(struct xe_engine *e),
 	     TP_ARGS(e)
Signature · 1 line
2.34.1
maintainer
#54 lore raw

Re: [Intel-xe] [PATCH v2 11/31] drm/xe/guc: Use doorbells for submission if possible

Quoted 278 lines · Matthew Brost

We have 256 doorbells (on most platforms) that we can allocate to bypass using the H2G channel for submission. This will avoid contention on the CT mutex.

---
 drivers/gpu/drm/xe/regs/xe_guc_regs.h    |   1 +
 drivers/gpu/drm/xe/xe_guc.c              |   6 +
 drivers/gpu/drm/xe/xe_guc_engine_types.h |   7 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 295 ++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_guc_submit.h       |   1 +
 drivers/gpu/drm/xe/xe_guc_types.h        |   4 +
 drivers/gpu/drm/xe/xe_trace.h            |   5 +
 7 files changed, 315 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/regs/xe_guc_regs.h +1 -0
index 37e0ac550931..11b117293a62 100644
@@ -109,6 +109,7 @@ struct guc_doorbell_info {
 
 #define DIST_DBS_POPULATED			XE_REG(0xd08)
 #define   DOORBELLS_PER_SQIDI_MASK		REG_GENMASK(23, 16)
+#define	  DOORBELLS_PER_SQIDI_SHIFT		16
 #define   SQIDIS_DOORBELL_EXIST_MASK		REG_GENMASK(15, 0)
 
 #define GUC_BCS_RCS_IER				XE_REG(0xC550)
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 89d20faced19..0c87f78a868b 100644
@@ -297,6 +297,12 @@ int xe_guc_init(struct xe_guc *guc)
  */
 int xe_guc_init_post_hwconfig(struct xe_guc *guc)
 {
+	int ret;
+
+	ret = xe_guc_submit_init_post_hwconfig(guc);
+	if (ret)
+		return ret;
+
 	return xe_guc_ads_init_post_hwconfig(&guc->ads);
 }
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +7 -0
index 5d83132034a6..420b7f53e649 100644
@@ -12,6 +12,7 @@
 #include <drm/gpu_scheduler.h>
 
 struct dma_fence;
+struct xe_bo;
 struct xe_engine;
 
 /**
@@ -37,6 +38,10 @@ struct xe_guc_engine {
 	struct work_struct fini_async;
 	/** @resume_time: time of last resume */
 	u64 resume_time;
+	/** @doorbell_bo: BO for memory doorbell */
+	struct xe_bo *doorbell_bo;
+	/** @doorbell_offset: MMIO doorbell offset */
+	u32 doorbell_offset;
 	/** @state: GuC specific state for this xe_engine */
 	atomic_t state;
 	/** @wqi_head: work queue item tail */
@@ -45,6 +50,8 @@ struct xe_guc_engine {
 	u32 wqi_tail;
 	/** @id: GuC id for this xe_engine */
 	u16 id;
+	/** @doorbell_id: doorbell id */
+	u16 doorbell_id;
 	/** @suspend_wait: wait queue used to wait on pending suspends */
 	wait_queue_head_t suspend_wait;
 	/** @suspend_pending: a suspend of the engine is pending */
drivers/gpu/drm/xe/xe_guc_submit.c +144 -0
index 0a41f5d04f6d..1b6f36b04cd1 100644
@@ -13,7 +13,10 @@
 
 #include <drm/drm_managed.h>
 
+#include "regs/xe_guc_regs.h"
 #include "regs/xe_lrc_layout.h"
+
+#include "xe_bo.h"
 #include "xe_device.h"
 #include "xe_engine.h"
 #include "xe_force_wake.h"
@@ -26,12 +29,22 @@
 #include "xe_lrc.h"
 #include "xe_macros.h"
 #include "xe_map.h"
+#include "xe_mmio.h"
 #include "xe_mocs.h"
 #include "xe_ring_ops_types.h"
 #include "xe_sched_job.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
+#define HAS_GUC_MMIO_DB(xe) (IS_DGFX(xe) || GRAPHICS_VERx100(xe) >= 1250)
+#define HAS_GUC_DIST_DB(xe) \
+	(GRAPHICS_VERx100(xe) >= 1200 && !HAS_GUC_MMIO_DB(xe))
+
+#define GUC_NUM_HW_DOORBELLS 256
+
+#define GUC_MMIO_DB_BAR_OFFSET SZ_4M
+#define GUC_MMIO_DB_BAR_SIZE SZ_4M
+
 static struct xe_gt *
 guc_to_gt(struct xe_guc *guc)
 {
@@ -63,6 +76,7 @@ engine_to_guc(struct xe_engine *e)
 #define ENGINE_STATE_SUSPENDED		(1 << 5)
 #define ENGINE_STATE_RESET		(1 << 6)
 #define ENGINE_STATE_KILLED		(1 << 7)
+#define ENGINE_STATE_DB_REGISTERED	(1 << 8)
 
 static bool engine_registered(struct xe_engine *e)
 {
@@ -179,6 +193,16 @@ static void set_engine_killed(struct xe_engine *e)
 	atomic_or(ENGINE_STATE_KILLED, &e->guc->state);
 }
 
+static bool engine_doorbell_registered(struct xe_engine *e)
+{
+	return atomic_read(&e->guc->state) & ENGINE_STATE_DB_REGISTERED;
+}
+
+static void set_engine_doorbell_registered(struct xe_engine *e)
+{
+	atomic_or(ENGINE_STATE_DB_REGISTERED, &e->guc->state);
+}
+
 static bool engine_killed_or_banned(struct xe_engine *e)
 {
 	return engine_killed(e) || engine_banned(e);
@@ -190,6 +214,7 @@ static void guc_submit_fini(struct drm_device *drm, void *arg)
 
 	xa_destroy(&guc->submission_state.engine_lookup);
 	ida_destroy(&guc->submission_state.guc_ids);
+	ida_destroy(&guc->submission_state.doorbell_ids);
 	bitmap_free(guc->submission_state.guc_ids_bitmap);
 }
 
@@ -230,6 +255,7 @@ int xe_guc_submit_init(struct xe_guc *guc)
 	mutex_init(&guc->submission_state.lock);
 	xa_init(&guc->submission_state.engine_lookup);
 	ida_init(&guc->submission_state.guc_ids);
+	ida_init(&guc->submission_state.doorbell_ids);
 
 	spin_lock_init(&guc->submission_state.suspend.lock);
 	guc->submission_state.suspend.context = dma_fence_context_alloc(1);
@@ -243,6 +269,237 @@ int xe_guc_submit_init(struct xe_guc *guc)
 	return 0;
 }
 
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc)
+{
+	if (HAS_GUC_DIST_DB(guc_to_xe(guc))) {
+		u32 distdbreg = xe_mmio_read32(guc_to_gt(guc),
+					       DIST_DBS_POPULATED.reg);
+		u32 num_sqidi =
+			hweight32(distdbreg & SQIDIS_DOORBELL_EXIST_MASK);
+		u32 doorbells_per_sqidi =
+			((distdbreg >> DOORBELLS_PER_SQIDI_SHIFT) &
+			 DOORBELLS_PER_SQIDI_MASK) + 1;
+
+		guc->submission_state.num_doorbells =
+			num_sqidi * doorbells_per_sqidi;
+	} else {
+		guc->submission_state.num_doorbells = GUC_NUM_HW_DOORBELLS;
+	}
+
+	return 0;
+}
+
+static bool alloc_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+	int ret;
+
+	lockdep_assert_held(&guc->submission_state.lock);
+
+	e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+	ret = ida_simple_get(&guc->submission_state.doorbell_ids, 0,
+			     guc->submission_state.num_doorbells, GFP_NOWAIT);
+	if (ret < 0)
+		return false;
+
+	e->guc->doorbell_id = ret;
+
+	return true;
+}
+
+static void release_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+	mutex_lock(&guc->submission_state.lock);
+	ida_simple_remove(&guc->submission_state.doorbell_ids,
+			  e->guc->doorbell_id);
+	mutex_unlock(&guc->submission_state.lock);
+
+	e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+}
+
+static int allocate_doorbell(struct xe_guc *guc, u16 guc_id, u16 doorbell_id,
+			     u64 gpa, u32 gtt_addr)
+{
+	u32 action[] = {
+		XE_GUC_ACTION_ALLOCATE_DOORBELL,
+		guc_id,
+		doorbell_id,
+		lower_32_bits(gpa),
+		upper_32_bits(gpa),
+		gtt_addr
+	};
+
+	return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action));
+}
+
+static void deallocate_doorbell(struct xe_guc *guc, u16 guc_id)
+{
+	u32 action[] = {
+		XE_GUC_ACTION_DEALLOCATE_DOORBELL,
+		guc_id
+	};
+
+	xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
+}
+
+static bool has_doorbell(struct xe_engine *e)
+{
+	return e->guc->doorbell_id != GUC_NUM_HW_DOORBELLS;
+}
+
+#define doorbell_read(guc_, e_, field_) ({			\
+	struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;	\
+	iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);	\
+	xe_map_rd_field(guc_to_xe((guc_)), &_vmap, 0,		\
+				  struct guc_doorbell_info, field_); \
+	})
+#define doorbell_write(guc_, e_, field_, val_) ({		\
+	struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;	\
+	iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);	\
+	xe_map_wr_field(guc_to_xe((guc_)), &_vmap, 0,		\
+				  struct guc_doorbell_info, field_, val_); \
+	})
+
+static void init_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	struct xe_device *xe = guc_to_xe(guc);
+
+	/* GuC does the initialization with distributed and MMIO doorbells */
+	if (!HAS_GUC_DIST_DB(xe) && !HAS_GUC_MMIO_DB(xe)) {
+		doorbell_write(guc, e, db_status, GUC_DOORBELL_ENABLED);
+		doorbell_write(guc, e, cookie, 0);
+	}
+}
+
+static void fini_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	if (!HAS_GUC_MMIO_DB(guc_to_xe(guc)) &&
+	    xe_device_mem_access_ongoing(guc_to_xe(guc)))
+		doorbell_write(guc, e, db_status, GUC_DOORBELL_DISABLED);
+}
+
+static void destroy_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	if (has_doorbell(e)) {
+		release_doorbell_id(guc, e);
+		xe_bo_unpin_map_no_vm(e->guc->doorbell_bo);
+	}
+}
+
+static void ring_memory_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{

drivers/gpu/drm/xe/xe_guc_submit.c int xe_guc_submit_init(struct xe_guc *guc) · line 399

Quoted 10 lines
drivers/gpu/drm/xe/xe_guc_submit.c +10 -0
+	u32 cookie;
+
+	cookie = doorbell_read(guc, e, cookie);
+	doorbell_write(guc, e, cookie, cookie + 1 ?: cookie + 2);
+
+	XE_WARN_ON(doorbell_read(guc, e, db_status) != GUC_DOORBELL_ENABLED);
+}
+
+#define GUC_MMIO_DOORBELL_RING_ACK	0xACEDBEEF
+#define GUC_MMIO_DOORBELL_RING_NACK	0xDEADBEEF

Is this a guc abi? should it be in the guc abi files?

I feel that we need someone with deeper guc knowledge on this review although based on what I followed on the discussion with Faith and others it looks like a good move in general.

Quoted 259 lines
drivers/gpu/drm/xe/xe_guc_submit.c +137 -4
+static void ring_mmio_doorbell(struct xe_guc *guc, u32 doorbell_offset)
+{
+	u32 db_value;
+
+	db_value = xe_mmio_read32(guc_to_gt(guc), GUC_MMIO_DB_BAR_OFFSET +
+				  doorbell_offset);
+
+	/*
+	 * The read from the doorbell page will return ack/nack. We don't remove
+	 * doorbells from active clients so we don't expect to ever get a nack.
+	 * XXX: if doorbell is lost, re-acquire it?
+	 */
+	XE_WARN_ON(db_value == GUC_MMIO_DOORBELL_RING_NACK);
+	XE_WARN_ON(db_value != GUC_MMIO_DOORBELL_RING_ACK);
+}
+
+static void ring_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	XE_BUG_ON(!has_doorbell(e));
+
+	if (HAS_GUC_MMIO_DB(guc_to_xe(guc)))
+		ring_mmio_doorbell(guc, e->guc->doorbell_offset);
+	else
+		ring_memory_doorbell(guc, e);
+
+	trace_xe_engine_ring_db(e);
+}
+
+static void register_engine(struct xe_engine *e);
+
+static int create_doorbell(struct xe_guc *guc, struct xe_engine *e, bool init)
+{
+	struct xe_gt *gt = guc_to_gt(guc);
+	struct xe_device *xe = gt_to_xe(gt);
+	u64 gpa;
+	u32 gtt_addr;
+	int ret;
+
+	XE_BUG_ON(!has_doorbell(e));
+
+	if (HAS_GUC_MMIO_DB(xe)) {
+		e->guc->doorbell_offset = PAGE_SIZE * e->guc->doorbell_id;
+		gpa = GUC_MMIO_DB_BAR_OFFSET + e->guc->doorbell_offset;
+		gtt_addr = 0;
+	} else {
+		struct xe_bo *bo;
+
+		if (!e->guc->doorbell_bo) {
+			bo = xe_bo_create_pin_map(xe, gt, NULL, PAGE_SIZE,
+						  ttm_bo_type_kernel,
+						  XE_BO_CREATE_VRAM_IF_DGFX(gt) |
+						  XE_BO_CREATE_GGTT_BIT);
+			if (IS_ERR(bo))
+				return PTR_ERR(bo);
+
+			e->guc->doorbell_bo = bo;
+		} else {
+			bo = e->guc->doorbell_bo;
+		}
+
+		init_doorbell(guc, e);
+		gpa = xe_bo_main_addr(bo, PAGE_SIZE);
+		gtt_addr = xe_bo_ggtt_addr(bo);
+	}
+
+	if (init && e->flags & ENGINE_FLAG_KERNEL)
+		return 0;
+
+	register_engine(e);
+	ret = allocate_doorbell(guc, e->guc->id, e->guc->doorbell_id, gpa,
+				gtt_addr);
+	if (ret < 0) {
+		fini_doorbell(guc, e);
+		return ret;
+	}
+
+	/*
+	 * In distributed doorbells, guc is returning the cacheline selected
+	 * by HW as part of the 7bit data from the allocate doorbell command:
+	 *  bit [22]   - Cacheline allocated
+	 *  bit [21:16] - Cacheline offset address
+	 * (bit 21 must be zero, or our assumption of only using half a page is
+	 * no longer correct).
+	 */
+	if (HAS_GUC_DIST_DB(xe)) {
+		u32 dd_cacheline_info;
+
+		XE_WARN_ON(!(ret & BIT(22)));
+		XE_WARN_ON(ret & BIT(21));
+
+		dd_cacheline_info = FIELD_GET(GENMASK(21, 16), ret);
+		e->guc->doorbell_offset = dd_cacheline_info * cache_line_size();
+
+		/* and verify db status was updated correctly by the guc fw */
+		XE_WARN_ON(doorbell_read(guc, e, db_status) !=
+			   GUC_DOORBELL_ENABLED);
+	}
+
+	set_engine_doorbell_registered(e);
+
+	return 0;
+}
+
 static int alloc_guc_id(struct xe_guc *guc, struct xe_engine *e)
 {
 	int ret;
@@ -623,6 +880,7 @@ static void submit_engine(struct xe_engine *e)
 	u32 num_g2h = 0;
 	int len = 0;
 	bool extra_submit = false;
+	bool enable = false;
 
 	XE_BUG_ON(!engine_registered(e));
 
@@ -642,6 +900,7 @@ static void submit_engine(struct xe_engine *e)
 		num_g2h = 1;
 		if (xe_engine_is_parallel(e))
 			extra_submit = true;
+		enable = true;
 
 		e->guc->resume_time = RESUME_PENDING;
 		set_engine_pending_enable(e);
@@ -653,7 +912,10 @@ static void submit_engine(struct xe_engine *e)
 		trace_xe_engine_submit(e);
 	}
 
-	xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+	if (enable || !engine_doorbell_registered(e))
+		xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+	else
+		ring_doorbell(guc, e);
 
 	if (extra_submit) {
 		len = 0;
@@ -678,8 +940,17 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
 	trace_xe_sched_job_run(job);
 
 	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
-		if (!engine_registered(e))
-			register_engine(e);
+		if (!engine_registered(e)) {
+			if (has_doorbell(e)) {
+				int err = create_doorbell(engine_to_guc(e), e,
+							  false);
+
+				/* Not fatal, but let's warn */
+				XE_WARN_ON(err);
+			} else {
+				register_engine(e);
+			}
+		}
 		if (!lr)	/* Written in IOCTL */
 			e->ring_ops->emit_job(job);
 		submit_engine(e);
@@ -722,6 +993,11 @@ static void disable_scheduling_deregister(struct xe_guc *guc,
 	MAKE_SCHED_CONTEXT_ACTION(e, DISABLE);
 	int ret;
 
+	if (has_doorbell(e)) {
+		fini_doorbell(guc, e);
+		deallocate_doorbell(guc, e->guc->id);
+	}
+
 	set_min_preemption_timeout(guc, e);
 	smp_rmb();
 	ret = wait_event_timeout(guc->ct.wq, !engine_pending_enable(e) ||
@@ -958,6 +1234,7 @@ static void __guc_engine_fini_async(struct work_struct *w)
 		cancel_work_sync(&ge->lr_tdr);
 	if (e->flags & ENGINE_FLAG_PERSISTENT)
 		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
+	destroy_doorbell(guc, e);
 	release_guc_id(guc, e);
 	drm_sched_entity_fini(&ge->entity);
 	drm_sched_fini(&ge->sched);
@@ -1136,6 +1413,7 @@ static int guc_engine_init(struct xe_engine *e)
 	struct xe_guc_engine *ge;
 	long timeout;
 	int err;
+	bool create_db = false;
 
 	XE_BUG_ON(!xe_device_guc_submission_enabled(guc_to_xe(guc)));
 
@@ -1177,8 +1455,17 @@ static int guc_engine_init(struct xe_engine *e)
 	if (guc_read_stopped(guc))
 		drm_sched_stop(sched, NULL);
 
+	create_db = alloc_doorbell_id(guc, e);
+
 	mutex_unlock(&guc->submission_state.lock);
 
+	if (create_db) {
+		/* Error isn't fatal as we don't need a doorbell */
+		err = create_doorbell(guc, e, true);
+		if (err)
+			release_doorbell_id(guc, e);
+	}
+
 	switch (e->class) {
 	case XE_ENGINE_CLASS_RENDER:
 		sprintf(e->name, "rcs%d", e->guc->id);
@@ -1302,7 +1589,7 @@ static int guc_engine_set_job_timeout(struct xe_engine *e, u32 job_timeout_ms)
 {
 	struct drm_gpu_scheduler *sched = &e->guc->sched;
 
-	XE_BUG_ON(engine_registered(e));
+	XE_BUG_ON(engine_registered(e) && !has_doorbell(e));
 	XE_BUG_ON(engine_banned(e));
 	XE_BUG_ON(engine_killed(e));
 
drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 8002734d6f24..bada6c02d6aa 100644
@@ -13,6 +13,7 @@ struct xe_engine;
 struct xe_guc;
 
 int xe_guc_submit_init(struct xe_guc *guc);
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc);
 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p);
 
 int xe_guc_submit_reset_prepare(struct xe_guc *guc);
drivers/gpu/drm/xe/xe_guc_types.h +4 -0
index ac7eec28934d..9ee4d572f4e0 100644
@@ -36,10 +36,14 @@ struct xe_guc {
 		struct xarray engine_lookup;
 		/** @guc_ids: used to allocate new guc_ids, single-lrc */
 		struct ida guc_ids;
+		/** @doorbell_ids: use to allocate new doorbells */
+		struct ida doorbell_ids;
 		/** @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc */
 		unsigned long *guc_ids_bitmap;
 		/** @stopped: submissions are stopped */
 		atomic_t stopped;
+		/** @num_doorbells: number of doorbels */
+		int num_doorbells;
 		/** @lock: protects submission state */
 		struct mutex lock;
 		/** @suspend: suspend fence state */
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 02861c26e145..38e9d7c6197b 100644
@@ -149,6 +149,11 @@ DEFINE_EVENT(xe_engine, xe_engine_submit,
 	     TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_ring_db,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
 DEFINE_EVENT(xe_engine, xe_engine_scheduling_enable,
 	     TP_PROTO(struct xe_engine *e),
 	     TP_ARGS(e)
Signature · 1 line
2.34.1
author maintainer
#55 lore raw

Re: [Intel-xe] [PATCH v2 11/31] drm/xe/guc: Use doorbells for submission if possible

Quoted 283 lines · Rodrigo Vivi
Quoted 282 lines · Matthew Brost

We have 256 doorbells (on most platforms) that we can allocate to bypass using the H2G channel for submission. This will avoid contention on the CT mutex.

---
 drivers/gpu/drm/xe/regs/xe_guc_regs.h    |   1 +
 drivers/gpu/drm/xe/xe_guc.c              |   6 +
 drivers/gpu/drm/xe/xe_guc_engine_types.h |   7 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 295 ++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_guc_submit.h       |   1 +
 drivers/gpu/drm/xe/xe_guc_types.h        |   4 +
 drivers/gpu/drm/xe/xe_trace.h            |   5 +
 7 files changed, 315 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/regs/xe_guc_regs.h +1 -0
index 37e0ac550931..11b117293a62 100644
@@ -109,6 +109,7 @@ struct guc_doorbell_info {
 
 #define DIST_DBS_POPULATED			XE_REG(0xd08)
 #define   DOORBELLS_PER_SQIDI_MASK		REG_GENMASK(23, 16)
+#define	  DOORBELLS_PER_SQIDI_SHIFT		16
 #define   SQIDIS_DOORBELL_EXIST_MASK		REG_GENMASK(15, 0)
 
 #define GUC_BCS_RCS_IER				XE_REG(0xC550)
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 89d20faced19..0c87f78a868b 100644
@@ -297,6 +297,12 @@ int xe_guc_init(struct xe_guc *guc)
  */
 int xe_guc_init_post_hwconfig(struct xe_guc *guc)
 {
+	int ret;
+
+	ret = xe_guc_submit_init_post_hwconfig(guc);
+	if (ret)
+		return ret;
+
 	return xe_guc_ads_init_post_hwconfig(&guc->ads);
 }
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +7 -0
index 5d83132034a6..420b7f53e649 100644
@@ -12,6 +12,7 @@
 #include <drm/gpu_scheduler.h>
 
 struct dma_fence;
+struct xe_bo;
 struct xe_engine;
 
 /**
@@ -37,6 +38,10 @@ struct xe_guc_engine {
 	struct work_struct fini_async;
 	/** @resume_time: time of last resume */
 	u64 resume_time;
+	/** @doorbell_bo: BO for memory doorbell */
+	struct xe_bo *doorbell_bo;
+	/** @doorbell_offset: MMIO doorbell offset */
+	u32 doorbell_offset;
 	/** @state: GuC specific state for this xe_engine */
 	atomic_t state;
 	/** @wqi_head: work queue item tail */
@@ -45,6 +50,8 @@ struct xe_guc_engine {
 	u32 wqi_tail;
 	/** @id: GuC id for this xe_engine */
 	u16 id;
+	/** @doorbell_id: doorbell id */
+	u16 doorbell_id;
 	/** @suspend_wait: wait queue used to wait on pending suspends */
 	wait_queue_head_t suspend_wait;
 	/** @suspend_pending: a suspend of the engine is pending */
drivers/gpu/drm/xe/xe_guc_submit.c +148 -0
index 0a41f5d04f6d..1b6f36b04cd1 100644
@@ -13,7 +13,10 @@
 
 #include <drm/drm_managed.h>
 
+#include "regs/xe_guc_regs.h"
 #include "regs/xe_lrc_layout.h"
+
+#include "xe_bo.h"
 #include "xe_device.h"
 #include "xe_engine.h"
 #include "xe_force_wake.h"
@@ -26,12 +29,22 @@
 #include "xe_lrc.h"
 #include "xe_macros.h"
 #include "xe_map.h"
+#include "xe_mmio.h"
 #include "xe_mocs.h"
 #include "xe_ring_ops_types.h"
 #include "xe_sched_job.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
+#define HAS_GUC_MMIO_DB(xe) (IS_DGFX(xe) || GRAPHICS_VERx100(xe) >= 1250)
+#define HAS_GUC_DIST_DB(xe) \
+	(GRAPHICS_VERx100(xe) >= 1200 && !HAS_GUC_MMIO_DB(xe))
+
+#define GUC_NUM_HW_DOORBELLS 256
+
+#define GUC_MMIO_DB_BAR_OFFSET SZ_4M
+#define GUC_MMIO_DB_BAR_SIZE SZ_4M
+
 static struct xe_gt *
 guc_to_gt(struct xe_guc *guc)
 {
@@ -63,6 +76,7 @@ engine_to_guc(struct xe_engine *e)
 #define ENGINE_STATE_SUSPENDED		(1 << 5)
 #define ENGINE_STATE_RESET		(1 << 6)
 #define ENGINE_STATE_KILLED		(1 << 7)
+#define ENGINE_STATE_DB_REGISTERED	(1 << 8)
 
 static bool engine_registered(struct xe_engine *e)
 {
@@ -179,6 +193,16 @@ static void set_engine_killed(struct xe_engine *e)
 	atomic_or(ENGINE_STATE_KILLED, &e->guc->state);
 }
 
+static bool engine_doorbell_registered(struct xe_engine *e)
+{
+	return atomic_read(&e->guc->state) & ENGINE_STATE_DB_REGISTERED;
+}
+
+static void set_engine_doorbell_registered(struct xe_engine *e)
+{
+	atomic_or(ENGINE_STATE_DB_REGISTERED, &e->guc->state);
+}
+
 static bool engine_killed_or_banned(struct xe_engine *e)
 {
 	return engine_killed(e) || engine_banned(e);
@@ -190,6 +214,7 @@ static void guc_submit_fini(struct drm_device *drm, void *arg)
 
 	xa_destroy(&guc->submission_state.engine_lookup);
 	ida_destroy(&guc->submission_state.guc_ids);
+	ida_destroy(&guc->submission_state.doorbell_ids);
 	bitmap_free(guc->submission_state.guc_ids_bitmap);
 }
 
@@ -230,6 +255,7 @@ int xe_guc_submit_init(struct xe_guc *guc)
 	mutex_init(&guc->submission_state.lock);
 	xa_init(&guc->submission_state.engine_lookup);
 	ida_init(&guc->submission_state.guc_ids);
+	ida_init(&guc->submission_state.doorbell_ids);
 
 	spin_lock_init(&guc->submission_state.suspend.lock);
 	guc->submission_state.suspend.context = dma_fence_context_alloc(1);
@@ -243,6 +269,237 @@ int xe_guc_submit_init(struct xe_guc *guc)
 	return 0;
 }
 
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc)
+{
+	if (HAS_GUC_DIST_DB(guc_to_xe(guc))) {
+		u32 distdbreg = xe_mmio_read32(guc_to_gt(guc),
+					       DIST_DBS_POPULATED.reg);
+		u32 num_sqidi =
+			hweight32(distdbreg & SQIDIS_DOORBELL_EXIST_MASK);
+		u32 doorbells_per_sqidi =
+			((distdbreg >> DOORBELLS_PER_SQIDI_SHIFT) &
+			 DOORBELLS_PER_SQIDI_MASK) + 1;
+
+		guc->submission_state.num_doorbells =
+			num_sqidi * doorbells_per_sqidi;
+	} else {
+		guc->submission_state.num_doorbells = GUC_NUM_HW_DOORBELLS;
+	}
+
+	return 0;
+}
+
+static bool alloc_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+	int ret;
+
+	lockdep_assert_held(&guc->submission_state.lock);
+
+	e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+	ret = ida_simple_get(&guc->submission_state.doorbell_ids, 0,
+			     guc->submission_state.num_doorbells, GFP_NOWAIT);
+	if (ret < 0)
+		return false;
+
+	e->guc->doorbell_id = ret;
+
+	return true;
+}
+
+static void release_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+	mutex_lock(&guc->submission_state.lock);
+	ida_simple_remove(&guc->submission_state.doorbell_ids,
+			  e->guc->doorbell_id);
+	mutex_unlock(&guc->submission_state.lock);
+
+	e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+}
+
+static int allocate_doorbell(struct xe_guc *guc, u16 guc_id, u16 doorbell_id,
+			     u64 gpa, u32 gtt_addr)
+{
+	u32 action[] = {
+		XE_GUC_ACTION_ALLOCATE_DOORBELL,
+		guc_id,
+		doorbell_id,
+		lower_32_bits(gpa),
+		upper_32_bits(gpa),
+		gtt_addr
+	};
+
+	return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action));
+}
+
+static void deallocate_doorbell(struct xe_guc *guc, u16 guc_id)
+{
+	u32 action[] = {
+		XE_GUC_ACTION_DEALLOCATE_DOORBELL,
+		guc_id
+	};
+
+	xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
+}
+
+static bool has_doorbell(struct xe_engine *e)
+{
+	return e->guc->doorbell_id != GUC_NUM_HW_DOORBELLS;
+}
+
+#define doorbell_read(guc_, e_, field_) ({			\
+	struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;	\
+	iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);	\
+	xe_map_rd_field(guc_to_xe((guc_)), &_vmap, 0,		\
+				  struct guc_doorbell_info, field_); \
+	})
+#define doorbell_write(guc_, e_, field_, val_) ({		\
+	struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;	\
+	iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);	\
+	xe_map_wr_field(guc_to_xe((guc_)), &_vmap, 0,		\
+				  struct guc_doorbell_info, field_, val_); \
+	})
+
+static void init_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	struct xe_device *xe = guc_to_xe(guc);
+
+	/* GuC does the initialization with distributed and MMIO doorbells */
+	if (!HAS_GUC_DIST_DB(xe) && !HAS_GUC_MMIO_DB(xe)) {
+		doorbell_write(guc, e, db_status, GUC_DOORBELL_ENABLED);
+		doorbell_write(guc, e, cookie, 0);
+	}
+}
+
+static void fini_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	if (!HAS_GUC_MMIO_DB(guc_to_xe(guc)) &&
+	    xe_device_mem_access_ongoing(guc_to_xe(guc)))
+		doorbell_write(guc, e, db_status, GUC_DOORBELL_DISABLED);
+}
+
+static void destroy_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	if (has_doorbell(e)) {
+		release_doorbell_id(guc, e);
+		xe_bo_unpin_map_no_vm(e->guc->doorbell_bo);
+	}
+}
+
+static void ring_memory_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	u32 cookie;
+
+	cookie = doorbell_read(guc, e, cookie);
+	doorbell_write(guc, e, cookie, cookie + 1 ?: cookie + 2);
Quoted 9 lines

drivers/gpu/drm/xe/xe_guc_submit.c int xe_guc_submit_init(struct xe_guc *guc) · line 399

Quoted 6 lines
drivers/gpu/drm/xe/xe_guc_submit.c +6 -0
+
+	XE_WARN_ON(doorbell_read(guc, e, db_status) != GUC_DOORBELL_ENABLED);
+}
+
+#define GUC_MMIO_DOORBELL_RING_ACK	0xACEDBEEF
+#define GUC_MMIO_DOORBELL_RING_NACK	0xDEADBEEF

Is this a guc abi? should it be in the guc abi files?

Probably? I can move this.

Quoted 4 lines

I feel that we need someone with deeper guc knowledge on this review although based on what I followed on the discussion with Faith and others it looks like a good move in general.

We probably need Daniele or JOhn H. to review this one.

Matt

Quoted 259 lines
Quoted 259 lines
drivers/gpu/drm/xe/xe_guc_submit.c +137 -4
+static void ring_mmio_doorbell(struct xe_guc *guc, u32 doorbell_offset)
+{
+	u32 db_value;
+
+	db_value = xe_mmio_read32(guc_to_gt(guc), GUC_MMIO_DB_BAR_OFFSET +
+				  doorbell_offset);
+
+	/*
+	 * The read from the doorbell page will return ack/nack. We don't remove
+	 * doorbells from active clients so we don't expect to ever get a nack.
+	 * XXX: if doorbell is lost, re-acquire it?
+	 */
+	XE_WARN_ON(db_value == GUC_MMIO_DOORBELL_RING_NACK);
+	XE_WARN_ON(db_value != GUC_MMIO_DOORBELL_RING_ACK);
+}
+
+static void ring_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	XE_BUG_ON(!has_doorbell(e));
+
+	if (HAS_GUC_MMIO_DB(guc_to_xe(guc)))
+		ring_mmio_doorbell(guc, e->guc->doorbell_offset);
+	else
+		ring_memory_doorbell(guc, e);
+
+	trace_xe_engine_ring_db(e);
+}
+
+static void register_engine(struct xe_engine *e);
+
+static int create_doorbell(struct xe_guc *guc, struct xe_engine *e, bool init)
+{
+	struct xe_gt *gt = guc_to_gt(guc);
+	struct xe_device *xe = gt_to_xe(gt);
+	u64 gpa;
+	u32 gtt_addr;
+	int ret;
+
+	XE_BUG_ON(!has_doorbell(e));
+
+	if (HAS_GUC_MMIO_DB(xe)) {
+		e->guc->doorbell_offset = PAGE_SIZE * e->guc->doorbell_id;
+		gpa = GUC_MMIO_DB_BAR_OFFSET + e->guc->doorbell_offset;
+		gtt_addr = 0;
+	} else {
+		struct xe_bo *bo;
+
+		if (!e->guc->doorbell_bo) {
+			bo = xe_bo_create_pin_map(xe, gt, NULL, PAGE_SIZE,
+						  ttm_bo_type_kernel,
+						  XE_BO_CREATE_VRAM_IF_DGFX(gt) |
+						  XE_BO_CREATE_GGTT_BIT);
+			if (IS_ERR(bo))
+				return PTR_ERR(bo);
+
+			e->guc->doorbell_bo = bo;
+		} else {
+			bo = e->guc->doorbell_bo;
+		}
+
+		init_doorbell(guc, e);
+		gpa = xe_bo_main_addr(bo, PAGE_SIZE);
+		gtt_addr = xe_bo_ggtt_addr(bo);
+	}
+
+	if (init && e->flags & ENGINE_FLAG_KERNEL)
+		return 0;
+
+	register_engine(e);
+	ret = allocate_doorbell(guc, e->guc->id, e->guc->doorbell_id, gpa,
+				gtt_addr);
+	if (ret < 0) {
+		fini_doorbell(guc, e);
+		return ret;
+	}
+
+	/*
+	 * In distributed doorbells, guc is returning the cacheline selected
+	 * by HW as part of the 7bit data from the allocate doorbell command:
+	 *  bit [22]   - Cacheline allocated
+	 *  bit [21:16] - Cacheline offset address
+	 * (bit 21 must be zero, or our assumption of only using half a page is
+	 * no longer correct).
+	 */
+	if (HAS_GUC_DIST_DB(xe)) {
+		u32 dd_cacheline_info;
+
+		XE_WARN_ON(!(ret & BIT(22)));
+		XE_WARN_ON(ret & BIT(21));
+
+		dd_cacheline_info = FIELD_GET(GENMASK(21, 16), ret);
+		e->guc->doorbell_offset = dd_cacheline_info * cache_line_size();
+
+		/* and verify db status was updated correctly by the guc fw */
+		XE_WARN_ON(doorbell_read(guc, e, db_status) !=
+			   GUC_DOORBELL_ENABLED);
+	}
+
+	set_engine_doorbell_registered(e);
+
+	return 0;
+}
+
 static int alloc_guc_id(struct xe_guc *guc, struct xe_engine *e)
 {
 	int ret;
@@ -623,6 +880,7 @@ static void submit_engine(struct xe_engine *e)
 	u32 num_g2h = 0;
 	int len = 0;
 	bool extra_submit = false;
+	bool enable = false;
 
 	XE_BUG_ON(!engine_registered(e));
 
@@ -642,6 +900,7 @@ static void submit_engine(struct xe_engine *e)
 		num_g2h = 1;
 		if (xe_engine_is_parallel(e))
 			extra_submit = true;
+		enable = true;
 
 		e->guc->resume_time = RESUME_PENDING;
 		set_engine_pending_enable(e);
@@ -653,7 +912,10 @@ static void submit_engine(struct xe_engine *e)
 		trace_xe_engine_submit(e);
 	}
 
-	xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+	if (enable || !engine_doorbell_registered(e))
+		xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+	else
+		ring_doorbell(guc, e);
 
 	if (extra_submit) {
 		len = 0;
@@ -678,8 +940,17 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
 	trace_xe_sched_job_run(job);
 
 	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
-		if (!engine_registered(e))
-			register_engine(e);
+		if (!engine_registered(e)) {
+			if (has_doorbell(e)) {
+				int err = create_doorbell(engine_to_guc(e), e,
+							  false);
+
+				/* Not fatal, but let's warn */
+				XE_WARN_ON(err);
+			} else {
+				register_engine(e);
+			}
+		}
 		if (!lr)	/* Written in IOCTL */
 			e->ring_ops->emit_job(job);
 		submit_engine(e);
@@ -722,6 +993,11 @@ static void disable_scheduling_deregister(struct xe_guc *guc,
 	MAKE_SCHED_CONTEXT_ACTION(e, DISABLE);
 	int ret;
 
+	if (has_doorbell(e)) {
+		fini_doorbell(guc, e);
+		deallocate_doorbell(guc, e->guc->id);
+	}
+
 	set_min_preemption_timeout(guc, e);
 	smp_rmb();
 	ret = wait_event_timeout(guc->ct.wq, !engine_pending_enable(e) ||
@@ -958,6 +1234,7 @@ static void __guc_engine_fini_async(struct work_struct *w)
 		cancel_work_sync(&ge->lr_tdr);
 	if (e->flags & ENGINE_FLAG_PERSISTENT)
 		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
+	destroy_doorbell(guc, e);
 	release_guc_id(guc, e);
 	drm_sched_entity_fini(&ge->entity);
 	drm_sched_fini(&ge->sched);
@@ -1136,6 +1413,7 @@ static int guc_engine_init(struct xe_engine *e)
 	struct xe_guc_engine *ge;
 	long timeout;
 	int err;
+	bool create_db = false;
 
 	XE_BUG_ON(!xe_device_guc_submission_enabled(guc_to_xe(guc)));
 
@@ -1177,8 +1455,17 @@ static int guc_engine_init(struct xe_engine *e)
 	if (guc_read_stopped(guc))
 		drm_sched_stop(sched, NULL);
 
+	create_db = alloc_doorbell_id(guc, e);
+
 	mutex_unlock(&guc->submission_state.lock);
 
+	if (create_db) {
+		/* Error isn't fatal as we don't need a doorbell */
+		err = create_doorbell(guc, e, true);
+		if (err)
+			release_doorbell_id(guc, e);
+	}
+
 	switch (e->class) {
 	case XE_ENGINE_CLASS_RENDER:
 		sprintf(e->name, "rcs%d", e->guc->id);
@@ -1302,7 +1589,7 @@ static int guc_engine_set_job_timeout(struct xe_engine *e, u32 job_timeout_ms)
 {
 	struct drm_gpu_scheduler *sched = &e->guc->sched;
 
-	XE_BUG_ON(engine_registered(e));
+	XE_BUG_ON(engine_registered(e) && !has_doorbell(e));
 	XE_BUG_ON(engine_banned(e));
 	XE_BUG_ON(engine_killed(e));
 
drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 8002734d6f24..bada6c02d6aa 100644
@@ -13,6 +13,7 @@ struct xe_engine;
 struct xe_guc;
 
 int xe_guc_submit_init(struct xe_guc *guc);
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc);
 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p);
 
 int xe_guc_submit_reset_prepare(struct xe_guc *guc);
drivers/gpu/drm/xe/xe_guc_types.h +4 -0
index ac7eec28934d..9ee4d572f4e0 100644
@@ -36,10 +36,14 @@ struct xe_guc {
 		struct xarray engine_lookup;
 		/** @guc_ids: used to allocate new guc_ids, single-lrc */
 		struct ida guc_ids;
+		/** @doorbell_ids: use to allocate new doorbells */
+		struct ida doorbell_ids;
 		/** @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc */
 		unsigned long *guc_ids_bitmap;
 		/** @stopped: submissions are stopped */
 		atomic_t stopped;
+		/** @num_doorbells: number of doorbels */
+		int num_doorbells;
 		/** @lock: protects submission state */
 		struct mutex lock;
 		/** @suspend: suspend fence state */
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 02861c26e145..38e9d7c6197b 100644
@@ -149,6 +149,11 @@ DEFINE_EVENT(xe_engine, xe_engine_submit,
 	     TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_ring_db,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
 DEFINE_EVENT(xe_engine, xe_engine_scheduling_enable,
 	     TP_PROTO(struct xe_engine *e),
 	     TP_ARGS(e)
Signature · 1 line
2.34.1
#56 lore raw

Re: [Intel-xe] [PATCH v2 11/31] drm/xe/guc: Use doorbells for submission if possible

In reply to Matthew Brost
Quoted 6 lines · Matthew Brost

We have 256 doorbells (on most platforms) that we can allocate to bypass using the H2G channel for submission. This will avoid contention on the CT mutex.

Could we describe in a DOC section how doorbells are distributed and if there are any suggestions on how to improve that moving forward?

/Thomas

Quoted 538 lines
---
  drivers/gpu/drm/xe/regs/xe_guc_regs.h    |   1 +
  drivers/gpu/drm/xe/xe_guc.c              |   6 +
  drivers/gpu/drm/xe/xe_guc_engine_types.h |   7 +
  drivers/gpu/drm/xe/xe_guc_submit.c       | 295 ++++++++++++++++++++++-
  drivers/gpu/drm/xe/xe_guc_submit.h       |   1 +
  drivers/gpu/drm/xe/xe_guc_types.h        |   4 +
  drivers/gpu/drm/xe/xe_trace.h            |   5 +
  7 files changed, 315 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/regs/xe_guc_regs.h +1 -0
index 37e0ac550931..11b117293a62 100644
@@ -109,6 +109,7 @@ struct guc_doorbell_info {
 
  #define DIST_DBS_POPULATED			XE_REG(0xd08)
  #define   DOORBELLS_PER_SQIDI_MASK		REG_GENMASK(23, 16)
+#define	  DOORBELLS_PER_SQIDI_SHIFT		16
  #define   SQIDIS_DOORBELL_EXIST_MASK		REG_GENMASK(15, 0)
 
  #define GUC_BCS_RCS_IER				XE_REG(0xC550)
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 89d20faced19..0c87f78a868b 100644
@@ -297,6 +297,12 @@ int xe_guc_init(struct xe_guc *guc)
   */
  int xe_guc_init_post_hwconfig(struct xe_guc *guc)
  {
+	int ret;
+
+	ret = xe_guc_submit_init_post_hwconfig(guc);
+	if (ret)
+		return ret;
+
  	return xe_guc_ads_init_post_hwconfig(&guc->ads);
  }
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +7 -0
index 5d83132034a6..420b7f53e649 100644
@@ -12,6 +12,7 @@
  #include <drm/gpu_scheduler.h>
 
  struct dma_fence;
+struct xe_bo;
  struct xe_engine;
 
  /**
@@ -37,6 +38,10 @@ struct xe_guc_engine {
  	struct work_struct fini_async;
  	/** @resume_time: time of last resume */
  	u64 resume_time;
+	/** @doorbell_bo: BO for memory doorbell */
+	struct xe_bo *doorbell_bo;
+	/** @doorbell_offset: MMIO doorbell offset */
+	u32 doorbell_offset;
  	/** @state: GuC specific state for this xe_engine */
  	atomic_t state;
  	/** @wqi_head: work queue item tail */
@@ -45,6 +50,8 @@ struct xe_guc_engine {
  	u32 wqi_tail;
  	/** @id: GuC id for this xe_engine */
  	u16 id;
+	/** @doorbell_id: doorbell id */
+	u16 doorbell_id;
  	/** @suspend_wait: wait queue used to wait on pending suspends */
  	wait_queue_head_t suspend_wait;
  	/** @suspend_pending: a suspend of the engine is pending */
drivers/gpu/drm/xe/xe_guc_submit.c +291 -4
index 0a41f5d04f6d..1b6f36b04cd1 100644
@@ -13,7 +13,10 @@
 
  #include <drm/drm_managed.h>
 
+#include "regs/xe_guc_regs.h"
  #include "regs/xe_lrc_layout.h"
+
+#include "xe_bo.h"
  #include "xe_device.h"
  #include "xe_engine.h"
  #include "xe_force_wake.h"
@@ -26,12 +29,22 @@
  #include "xe_lrc.h"
  #include "xe_macros.h"
  #include "xe_map.h"
+#include "xe_mmio.h"
  #include "xe_mocs.h"
  #include "xe_ring_ops_types.h"
  #include "xe_sched_job.h"
  #include "xe_trace.h"
  #include "xe_vm.h"
 
+#define HAS_GUC_MMIO_DB(xe) (IS_DGFX(xe) || GRAPHICS_VERx100(xe) >= 1250)
+#define HAS_GUC_DIST_DB(xe) \
+	(GRAPHICS_VERx100(xe) >= 1200 && !HAS_GUC_MMIO_DB(xe))
+
+#define GUC_NUM_HW_DOORBELLS 256
+
+#define GUC_MMIO_DB_BAR_OFFSET SZ_4M
+#define GUC_MMIO_DB_BAR_SIZE SZ_4M
+
  static struct xe_gt *
  guc_to_gt(struct xe_guc *guc)
  {
@@ -63,6 +76,7 @@ engine_to_guc(struct xe_engine *e)
  #define ENGINE_STATE_SUSPENDED		(1 << 5)
  #define ENGINE_STATE_RESET		(1 << 6)
  #define ENGINE_STATE_KILLED		(1 << 7)
+#define ENGINE_STATE_DB_REGISTERED	(1 << 8)
 
  static bool engine_registered(struct xe_engine *e)
  {
@@ -179,6 +193,16 @@ static void set_engine_killed(struct xe_engine *e)
  	atomic_or(ENGINE_STATE_KILLED, &e->guc->state);
  }
 
+static bool engine_doorbell_registered(struct xe_engine *e)
+{
+	return atomic_read(&e->guc->state) & ENGINE_STATE_DB_REGISTERED;
+}
+
+static void set_engine_doorbell_registered(struct xe_engine *e)
+{
+	atomic_or(ENGINE_STATE_DB_REGISTERED, &e->guc->state);
+}
+
  static bool engine_killed_or_banned(struct xe_engine *e)
  {
  	return engine_killed(e) || engine_banned(e);
@@ -190,6 +214,7 @@ static void guc_submit_fini(struct drm_device *drm, void *arg)
 
  	xa_destroy(&guc->submission_state.engine_lookup);
  	ida_destroy(&guc->submission_state.guc_ids);
+	ida_destroy(&guc->submission_state.doorbell_ids);
  	bitmap_free(guc->submission_state.guc_ids_bitmap);
  }
 
@@ -230,6 +255,7 @@ int xe_guc_submit_init(struct xe_guc *guc)
  	mutex_init(&guc->submission_state.lock);
  	xa_init(&guc->submission_state.engine_lookup);
  	ida_init(&guc->submission_state.guc_ids);
+	ida_init(&guc->submission_state.doorbell_ids);
 
  	spin_lock_init(&guc->submission_state.suspend.lock);
  	guc->submission_state.suspend.context = dma_fence_context_alloc(1);
@@ -243,6 +269,237 @@ int xe_guc_submit_init(struct xe_guc *guc)
  	return 0;
  }
 
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc)
+{
+	if (HAS_GUC_DIST_DB(guc_to_xe(guc))) {
+		u32 distdbreg = xe_mmio_read32(guc_to_gt(guc),
+					       DIST_DBS_POPULATED.reg);
+		u32 num_sqidi =
+			hweight32(distdbreg & SQIDIS_DOORBELL_EXIST_MASK);
+		u32 doorbells_per_sqidi =
+			((distdbreg >> DOORBELLS_PER_SQIDI_SHIFT) &
+			 DOORBELLS_PER_SQIDI_MASK) + 1;
+
+		guc->submission_state.num_doorbells =
+			num_sqidi * doorbells_per_sqidi;
+	} else {
+		guc->submission_state.num_doorbells = GUC_NUM_HW_DOORBELLS;
+	}
+
+	return 0;
+}
+
+static bool alloc_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+	int ret;
+
+	lockdep_assert_held(&guc->submission_state.lock);
+
+	e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+	ret = ida_simple_get(&guc->submission_state.doorbell_ids, 0,
+			     guc->submission_state.num_doorbells, GFP_NOWAIT);
+	if (ret < 0)
+		return false;
+
+	e->guc->doorbell_id = ret;
+
+	return true;
+}
+
+static void release_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+	mutex_lock(&guc->submission_state.lock);
+	ida_simple_remove(&guc->submission_state.doorbell_ids,
+			  e->guc->doorbell_id);
+	mutex_unlock(&guc->submission_state.lock);
+
+	e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+}
+
+static int allocate_doorbell(struct xe_guc *guc, u16 guc_id, u16 doorbell_id,
+			     u64 gpa, u32 gtt_addr)
+{
+	u32 action[] = {
+		XE_GUC_ACTION_ALLOCATE_DOORBELL,
+		guc_id,
+		doorbell_id,
+		lower_32_bits(gpa),
+		upper_32_bits(gpa),
+		gtt_addr
+	};
+
+	return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action));
+}
+
+static void deallocate_doorbell(struct xe_guc *guc, u16 guc_id)
+{
+	u32 action[] = {
+		XE_GUC_ACTION_DEALLOCATE_DOORBELL,
+		guc_id
+	};
+
+	xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
+}
+
+static bool has_doorbell(struct xe_engine *e)
+{
+	return e->guc->doorbell_id != GUC_NUM_HW_DOORBELLS;
+}
+
+#define doorbell_read(guc_, e_, field_) ({			\
+	struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;	\
+	iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);	\
+	xe_map_rd_field(guc_to_xe((guc_)), &_vmap, 0,		\
+				  struct guc_doorbell_info, field_); \
+	})
+#define doorbell_write(guc_, e_, field_, val_) ({		\
+	struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;	\
+	iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);	\
+	xe_map_wr_field(guc_to_xe((guc_)), &_vmap, 0,		\
+				  struct guc_doorbell_info, field_, val_); \
+	})
+
+static void init_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	struct xe_device *xe = guc_to_xe(guc);
+
+	/* GuC does the initialization with distributed and MMIO doorbells */
+	if (!HAS_GUC_DIST_DB(xe) && !HAS_GUC_MMIO_DB(xe)) {
+		doorbell_write(guc, e, db_status, GUC_DOORBELL_ENABLED);
+		doorbell_write(guc, e, cookie, 0);
+	}
+}
+
+static void fini_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	if (!HAS_GUC_MMIO_DB(guc_to_xe(guc)) &&
+	    xe_device_mem_access_ongoing(guc_to_xe(guc)))
+		doorbell_write(guc, e, db_status, GUC_DOORBELL_DISABLED);
+}
+
+static void destroy_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	if (has_doorbell(e)) {
+		release_doorbell_id(guc, e);
+		xe_bo_unpin_map_no_vm(e->guc->doorbell_bo);
+	}
+}
+
+static void ring_memory_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	u32 cookie;
+
+	cookie = doorbell_read(guc, e, cookie);
+	doorbell_write(guc, e, cookie, cookie + 1 ?: cookie + 2);
+
+	XE_WARN_ON(doorbell_read(guc, e, db_status) != GUC_DOORBELL_ENABLED);
+}
+
+#define GUC_MMIO_DOORBELL_RING_ACK	0xACEDBEEF
+#define GUC_MMIO_DOORBELL_RING_NACK	0xDEADBEEF
+static void ring_mmio_doorbell(struct xe_guc *guc, u32 doorbell_offset)
+{
+	u32 db_value;
+
+	db_value = xe_mmio_read32(guc_to_gt(guc), GUC_MMIO_DB_BAR_OFFSET +
+				  doorbell_offset);
+
+	/*
+	 * The read from the doorbell page will return ack/nack. We don't remove
+	 * doorbells from active clients so we don't expect to ever get a nack.
+	 * XXX: if doorbell is lost, re-acquire it?
+	 */
+	XE_WARN_ON(db_value == GUC_MMIO_DOORBELL_RING_NACK);
+	XE_WARN_ON(db_value != GUC_MMIO_DOORBELL_RING_ACK);
+}
+
+static void ring_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	XE_BUG_ON(!has_doorbell(e));
+
+	if (HAS_GUC_MMIO_DB(guc_to_xe(guc)))
+		ring_mmio_doorbell(guc, e->guc->doorbell_offset);
+	else
+		ring_memory_doorbell(guc, e);
+
+	trace_xe_engine_ring_db(e);
+}
+
+static void register_engine(struct xe_engine *e);
+
+static int create_doorbell(struct xe_guc *guc, struct xe_engine *e, bool init)
+{
+	struct xe_gt *gt = guc_to_gt(guc);
+	struct xe_device *xe = gt_to_xe(gt);
+	u64 gpa;
+	u32 gtt_addr;
+	int ret;
+
+	XE_BUG_ON(!has_doorbell(e));
+
+	if (HAS_GUC_MMIO_DB(xe)) {
+		e->guc->doorbell_offset = PAGE_SIZE * e->guc->doorbell_id;
+		gpa = GUC_MMIO_DB_BAR_OFFSET + e->guc->doorbell_offset;
+		gtt_addr = 0;
+	} else {
+		struct xe_bo *bo;
+
+		if (!e->guc->doorbell_bo) {
+			bo = xe_bo_create_pin_map(xe, gt, NULL, PAGE_SIZE,
+						  ttm_bo_type_kernel,
+						  XE_BO_CREATE_VRAM_IF_DGFX(gt) |
+						  XE_BO_CREATE_GGTT_BIT);
+			if (IS_ERR(bo))
+				return PTR_ERR(bo);
+
+			e->guc->doorbell_bo = bo;
+		} else {
+			bo = e->guc->doorbell_bo;
+		}
+
+		init_doorbell(guc, e);
+		gpa = xe_bo_main_addr(bo, PAGE_SIZE);
+		gtt_addr = xe_bo_ggtt_addr(bo);
+	}
+
+	if (init && e->flags & ENGINE_FLAG_KERNEL)
+		return 0;
+
+	register_engine(e);
+	ret = allocate_doorbell(guc, e->guc->id, e->guc->doorbell_id, gpa,
+				gtt_addr);
+	if (ret < 0) {
+		fini_doorbell(guc, e);
+		return ret;
+	}
+
+	/*
+	 * In distributed doorbells, guc is returning the cacheline selected
+	 * by HW as part of the 7bit data from the allocate doorbell command:
+	 *  bit [22]   - Cacheline allocated
+	 *  bit [21:16] - Cacheline offset address
+	 * (bit 21 must be zero, or our assumption of only using half a page is
+	 * no longer correct).
+	 */
+	if (HAS_GUC_DIST_DB(xe)) {
+		u32 dd_cacheline_info;
+
+		XE_WARN_ON(!(ret & BIT(22)));
+		XE_WARN_ON(ret & BIT(21));
+
+		dd_cacheline_info = FIELD_GET(GENMASK(21, 16), ret);
+		e->guc->doorbell_offset = dd_cacheline_info * cache_line_size();
+
+		/* and verify db status was updated correctly by the guc fw */
+		XE_WARN_ON(doorbell_read(guc, e, db_status) !=
+			   GUC_DOORBELL_ENABLED);
+	}
+
+	set_engine_doorbell_registered(e);
+
+	return 0;
+}
+
  static int alloc_guc_id(struct xe_guc *guc, struct xe_engine *e)
  {
  	int ret;
@@ -623,6 +880,7 @@ static void submit_engine(struct xe_engine *e)
  	u32 num_g2h = 0;
  	int len = 0;
  	bool extra_submit = false;
+	bool enable = false;
 
  	XE_BUG_ON(!engine_registered(e));
 
@@ -642,6 +900,7 @@ static void submit_engine(struct xe_engine *e)
  		num_g2h = 1;
  		if (xe_engine_is_parallel(e))
  			extra_submit = true;
+		enable = true;
 
  		e->guc->resume_time = RESUME_PENDING;
  		set_engine_pending_enable(e);
@@ -653,7 +912,10 @@ static void submit_engine(struct xe_engine *e)
  		trace_xe_engine_submit(e);
  	}
 
-	xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+	if (enable || !engine_doorbell_registered(e))
+		xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+	else
+		ring_doorbell(guc, e);
 
  	if (extra_submit) {
  		len = 0;
@@ -678,8 +940,17 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
  	trace_xe_sched_job_run(job);
 
  	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
-		if (!engine_registered(e))
-			register_engine(e);
+		if (!engine_registered(e)) {
+			if (has_doorbell(e)) {
+				int err = create_doorbell(engine_to_guc(e), e,
+							  false);
+
+				/* Not fatal, but let's warn */
+				XE_WARN_ON(err);
+			} else {
+				register_engine(e);
+			}
+		}
  		if (!lr)	/* Written in IOCTL */
  			e->ring_ops->emit_job(job);
  		submit_engine(e);
@@ -722,6 +993,11 @@ static void disable_scheduling_deregister(struct xe_guc *guc,
  	MAKE_SCHED_CONTEXT_ACTION(e, DISABLE);
  	int ret;
 
+	if (has_doorbell(e)) {
+		fini_doorbell(guc, e);
+		deallocate_doorbell(guc, e->guc->id);
+	}
+
  	set_min_preemption_timeout(guc, e);
  	smp_rmb();
  	ret = wait_event_timeout(guc->ct.wq, !engine_pending_enable(e) ||
@@ -958,6 +1234,7 @@ static void __guc_engine_fini_async(struct work_struct *w)
  		cancel_work_sync(&ge->lr_tdr);
  	if (e->flags & ENGINE_FLAG_PERSISTENT)
  		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
+	destroy_doorbell(guc, e);
  	release_guc_id(guc, e);
  	drm_sched_entity_fini(&ge->entity);
  	drm_sched_fini(&ge->sched);
@@ -1136,6 +1413,7 @@ static int guc_engine_init(struct xe_engine *e)
  	struct xe_guc_engine *ge;
  	long timeout;
  	int err;
+	bool create_db = false;
 
  	XE_BUG_ON(!xe_device_guc_submission_enabled(guc_to_xe(guc)));
 
@@ -1177,8 +1455,17 @@ static int guc_engine_init(struct xe_engine *e)
  	if (guc_read_stopped(guc))
  		drm_sched_stop(sched, NULL);
 
+	create_db = alloc_doorbell_id(guc, e);
+
  	mutex_unlock(&guc->submission_state.lock);
 
+	if (create_db) {
+		/* Error isn't fatal as we don't need a doorbell */
+		err = create_doorbell(guc, e, true);
+		if (err)
+			release_doorbell_id(guc, e);
+	}
+
  	switch (e->class) {
  	case XE_ENGINE_CLASS_RENDER:
  		sprintf(e->name, "rcs%d", e->guc->id);
@@ -1302,7 +1589,7 @@ static int guc_engine_set_job_timeout(struct xe_engine *e, u32 job_timeout_ms)
  {
  	struct drm_gpu_scheduler *sched = &e->guc->sched;
 
-	XE_BUG_ON(engine_registered(e));
+	XE_BUG_ON(engine_registered(e) && !has_doorbell(e));
  	XE_BUG_ON(engine_banned(e));
  	XE_BUG_ON(engine_killed(e));
 
drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 8002734d6f24..bada6c02d6aa 100644
@@ -13,6 +13,7 @@ struct xe_engine;
  struct xe_guc;
 
  int xe_guc_submit_init(struct xe_guc *guc);
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc);
  void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p);
 
  int xe_guc_submit_reset_prepare(struct xe_guc *guc);
drivers/gpu/drm/xe/xe_guc_types.h +4 -0
index ac7eec28934d..9ee4d572f4e0 100644
@@ -36,10 +36,14 @@ struct xe_guc {
  		struct xarray engine_lookup;
  		/** @guc_ids: used to allocate new guc_ids, single-lrc */
  		struct ida guc_ids;
+		/** @doorbell_ids: use to allocate new doorbells */
+		struct ida doorbell_ids;
  		/** @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc */
  		unsigned long *guc_ids_bitmap;
  		/** @stopped: submissions are stopped */
  		atomic_t stopped;
+		/** @num_doorbells: number of doorbels */
+		int num_doorbells;
  		/** @lock: protects submission state */
  		struct mutex lock;
  		/** @suspend: suspend fence state */
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 02861c26e145..38e9d7c6197b 100644
@@ -149,6 +149,11 @@ DEFINE_EVENT(xe_engine, xe_engine_submit,
  	     TP_ARGS(e)
  );
 
+DEFINE_EVENT(xe_engine, xe_engine_ring_db,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
  DEFINE_EVENT(xe_engine, xe_engine_scheduling_enable,
  	     TP_PROTO(struct xe_engine *e),
  	     TP_ARGS(e)
author maintainer
#57 lore raw

Re: [Intel-xe] [PATCH v2 11/31] drm/xe/guc: Use doorbells for submission if possible

Quoted 6 lines · Thomas Hellström
Quoted 4 lines · Matthew Brost

We have 256 doorbells (on most platforms) that we can allocate to bypass using the H2G channel for submission. This will avoid contention on the CT mutex.

Quoted 6 lines
Quoted 2 lines

Could we describe in a DOC section how doorbells are distributed and if there are any suggestions on how to improve that moving forward?

We I do the GuC documentation (not yet done) I can include some doorbell documentation.

Matt

Quoted 510 lines

/Thomas

Quoted 508 lines
---
  drivers/gpu/drm/xe/regs/xe_guc_regs.h    |   1 +
  drivers/gpu/drm/xe/xe_guc.c              |   6 +
  drivers/gpu/drm/xe/xe_guc_engine_types.h |   7 +
  drivers/gpu/drm/xe/xe_guc_submit.c       | 295 ++++++++++++++++++++++-
  drivers/gpu/drm/xe/xe_guc_submit.h       |   1 +
  drivers/gpu/drm/xe/xe_guc_types.h        |   4 +
  drivers/gpu/drm/xe/xe_trace.h            |   5 +
  7 files changed, 315 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/regs/xe_guc_regs.h +1 -0
index 37e0ac550931..11b117293a62 100644
@@ -109,6 +109,7 @@ struct guc_doorbell_info {
  #define DIST_DBS_POPULATED			XE_REG(0xd08)
  #define   DOORBELLS_PER_SQIDI_MASK		REG_GENMASK(23, 16)
+#define	  DOORBELLS_PER_SQIDI_SHIFT		16
  #define   SQIDIS_DOORBELL_EXIST_MASK		REG_GENMASK(15, 0)
  #define GUC_BCS_RCS_IER				XE_REG(0xC550)
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 89d20faced19..0c87f78a868b 100644
@@ -297,6 +297,12 @@ int xe_guc_init(struct xe_guc *guc)
   */
  int xe_guc_init_post_hwconfig(struct xe_guc *guc)
  {
+	int ret;
+
+	ret = xe_guc_submit_init_post_hwconfig(guc);
+	if (ret)
+		return ret;
+
  	return xe_guc_ads_init_post_hwconfig(&guc->ads);
  }
drivers/gpu/drm/xe/xe_guc_engine_types.h +7 -0
index 5d83132034a6..420b7f53e649 100644
@@ -12,6 +12,7 @@
  #include <drm/gpu_scheduler.h>
  struct dma_fence;
+struct xe_bo;
  struct xe_engine;
  /**
@@ -37,6 +38,10 @@ struct xe_guc_engine {
  	struct work_struct fini_async;
  	/** @resume_time: time of last resume */
  	u64 resume_time;
+	/** @doorbell_bo: BO for memory doorbell */
+	struct xe_bo *doorbell_bo;
+	/** @doorbell_offset: MMIO doorbell offset */
+	u32 doorbell_offset;
  	/** @state: GuC specific state for this xe_engine */
  	atomic_t state;
  	/** @wqi_head: work queue item tail */
@@ -45,6 +50,8 @@ struct xe_guc_engine {
  	u32 wqi_tail;
  	/** @id: GuC id for this xe_engine */
  	u16 id;
+	/** @doorbell_id: doorbell id */
+	u16 doorbell_id;
  	/** @suspend_wait: wait queue used to wait on pending suspends */
  	wait_queue_head_t suspend_wait;
  	/** @suspend_pending: a suspend of the engine is pending */
drivers/gpu/drm/xe/xe_guc_submit.c +291 -4
index 0a41f5d04f6d..1b6f36b04cd1 100644
@@ -13,7 +13,10 @@
  #include <drm/drm_managed.h>
+#include "regs/xe_guc_regs.h"
  #include "regs/xe_lrc_layout.h"
+
+#include "xe_bo.h"
  #include "xe_device.h"
  #include "xe_engine.h"
  #include "xe_force_wake.h"
@@ -26,12 +29,22 @@
  #include "xe_lrc.h"
  #include "xe_macros.h"
  #include "xe_map.h"
+#include "xe_mmio.h"
  #include "xe_mocs.h"
  #include "xe_ring_ops_types.h"
  #include "xe_sched_job.h"
  #include "xe_trace.h"
  #include "xe_vm.h"
+#define HAS_GUC_MMIO_DB(xe) (IS_DGFX(xe) || GRAPHICS_VERx100(xe) >= 1250)
+#define HAS_GUC_DIST_DB(xe) \
+	(GRAPHICS_VERx100(xe) >= 1200 && !HAS_GUC_MMIO_DB(xe))
+
+#define GUC_NUM_HW_DOORBELLS 256
+
+#define GUC_MMIO_DB_BAR_OFFSET SZ_4M
+#define GUC_MMIO_DB_BAR_SIZE SZ_4M
+
  static struct xe_gt *
  guc_to_gt(struct xe_guc *guc)
  {
@@ -63,6 +76,7 @@ engine_to_guc(struct xe_engine *e)
  #define ENGINE_STATE_SUSPENDED		(1 << 5)
  #define ENGINE_STATE_RESET		(1 << 6)
  #define ENGINE_STATE_KILLED		(1 << 7)
+#define ENGINE_STATE_DB_REGISTERED	(1 << 8)
  static bool engine_registered(struct xe_engine *e)
  {
@@ -179,6 +193,16 @@ static void set_engine_killed(struct xe_engine *e)
  	atomic_or(ENGINE_STATE_KILLED, &e->guc->state);
  }
+static bool engine_doorbell_registered(struct xe_engine *e)
+{
+	return atomic_read(&e->guc->state) & ENGINE_STATE_DB_REGISTERED;
+}
+
+static void set_engine_doorbell_registered(struct xe_engine *e)
+{
+	atomic_or(ENGINE_STATE_DB_REGISTERED, &e->guc->state);
+}
+
  static bool engine_killed_or_banned(struct xe_engine *e)
  {
  	return engine_killed(e) || engine_banned(e);
@@ -190,6 +214,7 @@ static void guc_submit_fini(struct drm_device *drm, void *arg)
  	xa_destroy(&guc->submission_state.engine_lookup);
  	ida_destroy(&guc->submission_state.guc_ids);
+	ida_destroy(&guc->submission_state.doorbell_ids);
  	bitmap_free(guc->submission_state.guc_ids_bitmap);
  }
@@ -230,6 +255,7 @@ int xe_guc_submit_init(struct xe_guc *guc)
  	mutex_init(&guc->submission_state.lock);
  	xa_init(&guc->submission_state.engine_lookup);
  	ida_init(&guc->submission_state.guc_ids);
+	ida_init(&guc->submission_state.doorbell_ids);
  	spin_lock_init(&guc->submission_state.suspend.lock);
  	guc->submission_state.suspend.context = dma_fence_context_alloc(1);
@@ -243,6 +269,237 @@ int xe_guc_submit_init(struct xe_guc *guc)
  	return 0;
  }
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc)
+{
+	if (HAS_GUC_DIST_DB(guc_to_xe(guc))) {
+		u32 distdbreg = xe_mmio_read32(guc_to_gt(guc),
+					       DIST_DBS_POPULATED.reg);
+		u32 num_sqidi =
+			hweight32(distdbreg & SQIDIS_DOORBELL_EXIST_MASK);
+		u32 doorbells_per_sqidi =
+			((distdbreg >> DOORBELLS_PER_SQIDI_SHIFT) &
+			 DOORBELLS_PER_SQIDI_MASK) + 1;
+
+		guc->submission_state.num_doorbells =
+			num_sqidi * doorbells_per_sqidi;
+	} else {
+		guc->submission_state.num_doorbells = GUC_NUM_HW_DOORBELLS;
+	}
+
+	return 0;
+}
+
+static bool alloc_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+	int ret;
+
+	lockdep_assert_held(&guc->submission_state.lock);
+
+	e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+	ret = ida_simple_get(&guc->submission_state.doorbell_ids, 0,
+			     guc->submission_state.num_doorbells, GFP_NOWAIT);
+	if (ret < 0)
+		return false;
+
+	e->guc->doorbell_id = ret;
+
+	return true;
+}
+
+static void release_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+	mutex_lock(&guc->submission_state.lock);
+	ida_simple_remove(&guc->submission_state.doorbell_ids,
+			  e->guc->doorbell_id);
+	mutex_unlock(&guc->submission_state.lock);
+
+	e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+}
+
+static int allocate_doorbell(struct xe_guc *guc, u16 guc_id, u16 doorbell_id,
+			     u64 gpa, u32 gtt_addr)
+{
+	u32 action[] = {
+		XE_GUC_ACTION_ALLOCATE_DOORBELL,
+		guc_id,
+		doorbell_id,
+		lower_32_bits(gpa),
+		upper_32_bits(gpa),
+		gtt_addr
+	};
+
+	return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action));
+}
+
+static void deallocate_doorbell(struct xe_guc *guc, u16 guc_id)
+{
+	u32 action[] = {
+		XE_GUC_ACTION_DEALLOCATE_DOORBELL,
+		guc_id
+	};
+
+	xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
+}
+
+static bool has_doorbell(struct xe_engine *e)
+{
+	return e->guc->doorbell_id != GUC_NUM_HW_DOORBELLS;
+}
+
+#define doorbell_read(guc_, e_, field_) ({			\
+	struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;	\
+	iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);	\
+	xe_map_rd_field(guc_to_xe((guc_)), &_vmap, 0,		\
+				  struct guc_doorbell_info, field_); \
+	})
+#define doorbell_write(guc_, e_, field_, val_) ({		\
+	struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;	\
+	iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);	\
+	xe_map_wr_field(guc_to_xe((guc_)), &_vmap, 0,		\
+				  struct guc_doorbell_info, field_, val_); \
+	})
+
+static void init_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	struct xe_device *xe = guc_to_xe(guc);
+
+	/* GuC does the initialization with distributed and MMIO doorbells */
+	if (!HAS_GUC_DIST_DB(xe) && !HAS_GUC_MMIO_DB(xe)) {
+		doorbell_write(guc, e, db_status, GUC_DOORBELL_ENABLED);
+		doorbell_write(guc, e, cookie, 0);
+	}
+}
+
+static void fini_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	if (!HAS_GUC_MMIO_DB(guc_to_xe(guc)) &&
+	    xe_device_mem_access_ongoing(guc_to_xe(guc)))
+		doorbell_write(guc, e, db_status, GUC_DOORBELL_DISABLED);
+}
+
+static void destroy_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	if (has_doorbell(e)) {
+		release_doorbell_id(guc, e);
+		xe_bo_unpin_map_no_vm(e->guc->doorbell_bo);
+	}
+}
+
+static void ring_memory_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	u32 cookie;
+
+	cookie = doorbell_read(guc, e, cookie);
+	doorbell_write(guc, e, cookie, cookie + 1 ?: cookie + 2);
+
+	XE_WARN_ON(doorbell_read(guc, e, db_status) != GUC_DOORBELL_ENABLED);
+}
+
+#define GUC_MMIO_DOORBELL_RING_ACK	0xACEDBEEF
+#define GUC_MMIO_DOORBELL_RING_NACK	0xDEADBEEF
+static void ring_mmio_doorbell(struct xe_guc *guc, u32 doorbell_offset)
+{
+	u32 db_value;
+
+	db_value = xe_mmio_read32(guc_to_gt(guc), GUC_MMIO_DB_BAR_OFFSET +
+				  doorbell_offset);
+
+	/*
+	 * The read from the doorbell page will return ack/nack. We don't remove
+	 * doorbells from active clients so we don't expect to ever get a nack.
+	 * XXX: if doorbell is lost, re-acquire it?
+	 */
+	XE_WARN_ON(db_value == GUC_MMIO_DOORBELL_RING_NACK);
+	XE_WARN_ON(db_value != GUC_MMIO_DOORBELL_RING_ACK);
+}
+
+static void ring_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+	XE_BUG_ON(!has_doorbell(e));
+
+	if (HAS_GUC_MMIO_DB(guc_to_xe(guc)))
+		ring_mmio_doorbell(guc, e->guc->doorbell_offset);
+	else
+		ring_memory_doorbell(guc, e);
+
+	trace_xe_engine_ring_db(e);
+}
+
+static void register_engine(struct xe_engine *e);
+
+static int create_doorbell(struct xe_guc *guc, struct xe_engine *e, bool init)
+{
+	struct xe_gt *gt = guc_to_gt(guc);
+	struct xe_device *xe = gt_to_xe(gt);
+	u64 gpa;
+	u32 gtt_addr;
+	int ret;
+
+	XE_BUG_ON(!has_doorbell(e));
+
+	if (HAS_GUC_MMIO_DB(xe)) {
+		e->guc->doorbell_offset = PAGE_SIZE * e->guc->doorbell_id;
+		gpa = GUC_MMIO_DB_BAR_OFFSET + e->guc->doorbell_offset;
+		gtt_addr = 0;
+	} else {
+		struct xe_bo *bo;
+
+		if (!e->guc->doorbell_bo) {
+			bo = xe_bo_create_pin_map(xe, gt, NULL, PAGE_SIZE,
+						  ttm_bo_type_kernel,
+						  XE_BO_CREATE_VRAM_IF_DGFX(gt) |
+						  XE_BO_CREATE_GGTT_BIT);
+			if (IS_ERR(bo))
+				return PTR_ERR(bo);
+
+			e->guc->doorbell_bo = bo;
+		} else {
+			bo = e->guc->doorbell_bo;
+		}
+
+		init_doorbell(guc, e);
+		gpa = xe_bo_main_addr(bo, PAGE_SIZE);
+		gtt_addr = xe_bo_ggtt_addr(bo);
+	}
+
+	if (init && e->flags & ENGINE_FLAG_KERNEL)
+		return 0;
+
+	register_engine(e);
+	ret = allocate_doorbell(guc, e->guc->id, e->guc->doorbell_id, gpa,
+				gtt_addr);
+	if (ret < 0) {
+		fini_doorbell(guc, e);
+		return ret;
+	}
+
+	/*
+	 * In distributed doorbells, guc is returning the cacheline selected
+	 * by HW as part of the 7bit data from the allocate doorbell command:
+	 *  bit [22]   - Cacheline allocated
+	 *  bit [21:16] - Cacheline offset address
+	 * (bit 21 must be zero, or our assumption of only using half a page is
+	 * no longer correct).
+	 */
+	if (HAS_GUC_DIST_DB(xe)) {
+		u32 dd_cacheline_info;
+
+		XE_WARN_ON(!(ret & BIT(22)));
+		XE_WARN_ON(ret & BIT(21));
+
+		dd_cacheline_info = FIELD_GET(GENMASK(21, 16), ret);
+		e->guc->doorbell_offset = dd_cacheline_info * cache_line_size();
+
+		/* and verify db status was updated correctly by the guc fw */
+		XE_WARN_ON(doorbell_read(guc, e, db_status) !=
+			   GUC_DOORBELL_ENABLED);
+	}
+
+	set_engine_doorbell_registered(e);
+
+	return 0;
+}
+
  static int alloc_guc_id(struct xe_guc *guc, struct xe_engine *e)
  {
  	int ret;
@@ -623,6 +880,7 @@ static void submit_engine(struct xe_engine *e)
  	u32 num_g2h = 0;
  	int len = 0;
  	bool extra_submit = false;
+	bool enable = false;
  	XE_BUG_ON(!engine_registered(e));
@@ -642,6 +900,7 @@ static void submit_engine(struct xe_engine *e)
  		num_g2h = 1;
  		if (xe_engine_is_parallel(e))
  			extra_submit = true;
+		enable = true;
  		e->guc->resume_time = RESUME_PENDING;
  		set_engine_pending_enable(e);
@@ -653,7 +912,10 @@ static void submit_engine(struct xe_engine *e)
  		trace_xe_engine_submit(e);
  	}
-	xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+	if (enable || !engine_doorbell_registered(e))
+		xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+	else
+		ring_doorbell(guc, e);
  	if (extra_submit) {
  		len = 0;
@@ -678,8 +940,17 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
  	trace_xe_sched_job_run(job);
  	if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
-		if (!engine_registered(e))
-			register_engine(e);
+		if (!engine_registered(e)) {
+			if (has_doorbell(e)) {
+				int err = create_doorbell(engine_to_guc(e), e,
+							  false);
+
+				/* Not fatal, but let's warn */
+				XE_WARN_ON(err);
+			} else {
+				register_engine(e);
+			}
+		}
  		if (!lr)	/* Written in IOCTL */
  			e->ring_ops->emit_job(job);
  		submit_engine(e);
@@ -722,6 +993,11 @@ static void disable_scheduling_deregister(struct xe_guc *guc,
  	MAKE_SCHED_CONTEXT_ACTION(e, DISABLE);
  	int ret;
+	if (has_doorbell(e)) {
+		fini_doorbell(guc, e);
+		deallocate_doorbell(guc, e->guc->id);
+	}
+
  	set_min_preemption_timeout(guc, e);
  	smp_rmb();
  	ret = wait_event_timeout(guc->ct.wq, !engine_pending_enable(e) ||
@@ -958,6 +1234,7 @@ static void __guc_engine_fini_async(struct work_struct *w)
  		cancel_work_sync(&ge->lr_tdr);
  	if (e->flags & ENGINE_FLAG_PERSISTENT)
  		xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
+	destroy_doorbell(guc, e);
  	release_guc_id(guc, e);
  	drm_sched_entity_fini(&ge->entity);
  	drm_sched_fini(&ge->sched);
@@ -1136,6 +1413,7 @@ static int guc_engine_init(struct xe_engine *e)
  	struct xe_guc_engine *ge;
  	long timeout;
  	int err;
+	bool create_db = false;
  	XE_BUG_ON(!xe_device_guc_submission_enabled(guc_to_xe(guc)));
@@ -1177,8 +1455,17 @@ static int guc_engine_init(struct xe_engine *e)
  	if (guc_read_stopped(guc))
  		drm_sched_stop(sched, NULL);
+	create_db = alloc_doorbell_id(guc, e);
+
  	mutex_unlock(&guc->submission_state.lock);
+	if (create_db) {
+		/* Error isn't fatal as we don't need a doorbell */
+		err = create_doorbell(guc, e, true);
+		if (err)
+			release_doorbell_id(guc, e);
+	}
+
  	switch (e->class) {
  	case XE_ENGINE_CLASS_RENDER:
  		sprintf(e->name, "rcs%d", e->guc->id);
@@ -1302,7 +1589,7 @@ static int guc_engine_set_job_timeout(struct xe_engine *e, u32 job_timeout_ms)
  {
  	struct drm_gpu_scheduler *sched = &e->guc->sched;
-	XE_BUG_ON(engine_registered(e));
+	XE_BUG_ON(engine_registered(e) && !has_doorbell(e));
  	XE_BUG_ON(engine_banned(e));
  	XE_BUG_ON(engine_killed(e));
drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 8002734d6f24..bada6c02d6aa 100644
@@ -13,6 +13,7 @@ struct xe_engine;
  struct xe_guc;
  int xe_guc_submit_init(struct xe_guc *guc);
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc);
  void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p);
  int xe_guc_submit_reset_prepare(struct xe_guc *guc);
drivers/gpu/drm/xe/xe_guc_types.h +4 -0
index ac7eec28934d..9ee4d572f4e0 100644
@@ -36,10 +36,14 @@ struct xe_guc {
  		struct xarray engine_lookup;
  		/** @guc_ids: used to allocate new guc_ids, single-lrc */
  		struct ida guc_ids;
+		/** @doorbell_ids: use to allocate new doorbells */
+		struct ida doorbell_ids;
  		/** @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc */
  		unsigned long *guc_ids_bitmap;
  		/** @stopped: submissions are stopped */
  		atomic_t stopped;
+		/** @num_doorbells: number of doorbels */
+		int num_doorbells;
  		/** @lock: protects submission state */
  		struct mutex lock;
  		/** @suspend: suspend fence state */
drivers/gpu/drm/xe/xe_trace.h +5 -0
index 02861c26e145..38e9d7c6197b 100644
@@ -149,6 +149,11 @@ DEFINE_EVENT(xe_engine, xe_engine_submit,
  	     TP_ARGS(e)
  );
+DEFINE_EVENT(xe_engine, xe_engine_ring_db,
+	     TP_PROTO(struct xe_engine *e),
+	     TP_ARGS(e)
+);
+
  DEFINE_EVENT(xe_engine, xe_engine_scheduling_enable,
  	     TP_PROTO(struct xe_engine *e),
  	     TP_ARGS(e)
#58 lore raw

Re: [Intel-xe] [PATCH v2 11/31] drm/xe/guc: Use doorbells for submission if possible

In reply to Matthew Brost
Quoted 321 lines · Matthew Brost

We have 256 doorbells (on most platforms) that we can allocate to bypass using the H2G channel for submission. This will avoid contention on the CT mutex.

---
 drivers/gpu/drm/xe/regs/xe_guc_regs.h    |   1 +
 drivers/gpu/drm/xe/xe_guc.c              |   6 +
 drivers/gpu/drm/xe/xe_guc_engine_types.h |   7 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 295 ++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_guc_submit.h       |   1 +
 drivers/gpu/drm/xe/xe_guc_types.h        |   4 +
 drivers/gpu/drm/xe/xe_trace.h            |   5 +
 7 files changed, 315 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/regs/xe_guc_regs.h +1 -0
index 37e0ac550931..11b117293a62 100644
@@ -109,6 +109,7 @@ struct guc_doorbell_info {
 
 #define DIST_DBS_POPULATED                     XE_REG(0xd08)
 #define   DOORBELLS_PER_SQIDI_MASK             REG_GENMASK(23, 16)
+#define          DOORBELLS_PER_SQIDI_SHIFT             16
 #define   SQIDIS_DOORBELL_EXIST_MASK           REG_GENMASK(15, 0)
 
 #define GUC_BCS_RCS_IER                                XE_REG(0xC550)
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 89d20faced19..0c87f78a868b 100644
@@ -297,6 +297,12 @@ int xe_guc_init(struct xe_guc *guc)
  */
 int xe_guc_init_post_hwconfig(struct xe_guc *guc)
 {
+       int ret;
+
+       ret = xe_guc_submit_init_post_hwconfig(guc);
+       if (ret)
+               return ret;
+
        return xe_guc_ads_init_post_hwconfig(&guc->ads);
 }
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +7 -0
index 5d83132034a6..420b7f53e649 100644
@@ -12,6 +12,7 @@
 #include <drm/gpu_scheduler.h>
 
 struct dma_fence;
+struct xe_bo;
 struct xe_engine;
 
 /**
@@ -37,6 +38,10 @@ struct xe_guc_engine {
        struct work_struct fini_async;
        /** @resume_time: time of last resume */
        u64 resume_time;
+       /** @doorbell_bo: BO for memory doorbell */
+       struct xe_bo *doorbell_bo;
+       /** @doorbell_offset: MMIO doorbell offset */
+       u32 doorbell_offset;
        /** @state: GuC specific state for this xe_engine */
        atomic_t state;
        /** @wqi_head: work queue item tail */
@@ -45,6 +50,8 @@ struct xe_guc_engine {
        u32 wqi_tail;
        /** @id: GuC id for this xe_engine */
        u16 id;
+       /** @doorbell_id: doorbell id */
+       u16 doorbell_id;
        /** @suspend_wait: wait queue used to wait on pending suspends */
        wait_queue_head_t suspend_wait;
        /** @suspend_pending: a suspend of the engine is pending */
drivers/gpu/drm/xe/xe_guc_submit.c +186 -0
index 0a41f5d04f6d..1b6f36b04cd1 100644
@@ -13,7 +13,10 @@
 
 #include <drm/drm_managed.h>
 
+#include "regs/xe_guc_regs.h"
 #include "regs/xe_lrc_layout.h"
+
+#include "xe_bo.h"
 #include "xe_device.h"
 #include "xe_engine.h"
 #include "xe_force_wake.h"
@@ -26,12 +29,22 @@
 #include "xe_lrc.h"
 #include "xe_macros.h"
 #include "xe_map.h"
+#include "xe_mmio.h"
 #include "xe_mocs.h"
 #include "xe_ring_ops_types.h"
 #include "xe_sched_job.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
+#define HAS_GUC_MMIO_DB(xe) (IS_DGFX(xe) || GRAPHICS_VERx100(xe) >= 1250)
+#define HAS_GUC_DIST_DB(xe) \
+       (GRAPHICS_VERx100(xe) >= 1200 && !HAS_GUC_MMIO_DB(xe))
+
+#define GUC_NUM_HW_DOORBELLS 256
+
+#define GUC_MMIO_DB_BAR_OFFSET SZ_4M
+#define GUC_MMIO_DB_BAR_SIZE SZ_4M
+
 static struct xe_gt *
 guc_to_gt(struct xe_guc *guc)
 {
@@ -63,6 +76,7 @@ engine_to_guc(struct xe_engine *e)
 #define ENGINE_STATE_SUSPENDED         (1 << 5)
 #define ENGINE_STATE_RESET             (1 << 6)
 #define ENGINE_STATE_KILLED            (1 << 7)
+#define ENGINE_STATE_DB_REGISTERED     (1 << 8)
 
 static bool engine_registered(struct xe_engine *e)
 {
@@ -179,6 +193,16 @@ static void set_engine_killed(struct xe_engine *e)
        atomic_or(ENGINE_STATE_KILLED, &e->guc->state);
 }
 
+static bool engine_doorbell_registered(struct xe_engine *e)
+{
+       return atomic_read(&e->guc->state) & ENGINE_STATE_DB_REGISTERED;
+}
+
+static void set_engine_doorbell_registered(struct xe_engine *e)
+{
+       atomic_or(ENGINE_STATE_DB_REGISTERED, &e->guc->state);
+}
+
 static bool engine_killed_or_banned(struct xe_engine *e)
 {
        return engine_killed(e) || engine_banned(e);
@@ -190,6 +214,7 @@ static void guc_submit_fini(struct drm_device *drm, void *arg)
 
        xa_destroy(&guc->submission_state.engine_lookup);
        ida_destroy(&guc->submission_state.guc_ids);
+       ida_destroy(&guc->submission_state.doorbell_ids);
        bitmap_free(guc->submission_state.guc_ids_bitmap);
 }
 
@@ -230,6 +255,7 @@ int xe_guc_submit_init(struct xe_guc *guc)
        mutex_init(&guc->submission_state.lock);
        xa_init(&guc->submission_state.engine_lookup);
        ida_init(&guc->submission_state.guc_ids);
+       ida_init(&guc->submission_state.doorbell_ids);
 
        spin_lock_init(&guc->submission_state.suspend.lock);
        guc->submission_state.suspend.context = dma_fence_context_alloc(1);
@@ -243,6 +269,237 @@ int xe_guc_submit_init(struct xe_guc *guc)
        return 0;
 }
 
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc)
+{
+       if (HAS_GUC_DIST_DB(guc_to_xe(guc))) {
+               u32 distdbreg = xe_mmio_read32(guc_to_gt(guc),
+                                              DIST_DBS_POPULATED.reg);
+               u32 num_sqidi =
+                       hweight32(distdbreg & SQIDIS_DOORBELL_EXIST_MASK);
+               u32 doorbells_per_sqidi =
+                       ((distdbreg >> DOORBELLS_PER_SQIDI_SHIFT) &
+                        DOORBELLS_PER_SQIDI_MASK) + 1;
+
+               guc->submission_state.num_doorbells =
+                       num_sqidi * doorbells_per_sqidi;
+       } else {
+               guc->submission_state.num_doorbells = GUC_NUM_HW_DOORBELLS;
+       }
+
+       return 0;
+}
+
+static bool alloc_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+       int ret;
+
+       lockdep_assert_held(&guc->submission_state.lock);
+
+       e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+       ret = ida_simple_get(&guc->submission_state.doorbell_ids, 0,
+                            guc->submission_state.num_doorbells, GFP_NOWAIT);
+       if (ret < 0)
+               return false;
+
+       e->guc->doorbell_id = ret;
+
+       return true;
+}
+
+static void release_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+       mutex_lock(&guc->submission_state.lock);
+       ida_simple_remove(&guc->submission_state.doorbell_ids,
+                         e->guc->doorbell_id);
+       mutex_unlock(&guc->submission_state.lock);
+
+       e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+}
+
+static int allocate_doorbell(struct xe_guc *guc, u16 guc_id, u16 doorbell_id,
+                            u64 gpa, u32 gtt_addr)
+{
+       u32 action[] = {
+               XE_GUC_ACTION_ALLOCATE_DOORBELL,
+               guc_id,
+               doorbell_id,
+               lower_32_bits(gpa),
+               upper_32_bits(gpa),
+               gtt_addr
+       };
+
+       return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action));
+}
+
+static void deallocate_doorbell(struct xe_guc *guc, u16 guc_id)
+{
+       u32 action[] = {
+               XE_GUC_ACTION_DEALLOCATE_DOORBELL,
+               guc_id
+       };
+
+       xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
+}
+
+static bool has_doorbell(struct xe_engine *e)
+{
+       return e->guc->doorbell_id != GUC_NUM_HW_DOORBELLS;
+}
+
+#define doorbell_read(guc_, e_, field_) ({                     \
+       struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;  \
+       iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);     \
+       xe_map_rd_field(guc_to_xe((guc_)), &_vmap, 0,           \
+                                 struct guc_doorbell_info, field_); \
+       })
+#define doorbell_write(guc_, e_, field_, val_) ({              \
+       struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;  \
+       iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);     \
+       xe_map_wr_field(guc_to_xe((guc_)), &_vmap, 0,           \
+                                 struct guc_doorbell_info, field_, val_); \
+       })
+
+static void init_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       struct xe_device *xe = guc_to_xe(guc);
+
+       /* GuC does the initialization with distributed and MMIO doorbells */
+       if (!HAS_GUC_DIST_DB(xe) && !HAS_GUC_MMIO_DB(xe)) {
+               doorbell_write(guc, e, db_status, GUC_DOORBELL_ENABLED);
+               doorbell_write(guc, e, cookie, 0);
+       }
+}
+
+static void fini_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       if (!HAS_GUC_MMIO_DB(guc_to_xe(guc)) &&
+           xe_device_mem_access_ongoing(guc_to_xe(guc)))
+               doorbell_write(guc, e, db_status, GUC_DOORBELL_DISABLED);
+}
+
+static void destroy_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       if (has_doorbell(e)) {
+               release_doorbell_id(guc, e);
+               xe_bo_unpin_map_no_vm(e->guc->doorbell_bo);
+       }
+}
+
+static void ring_memory_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       u32 cookie;
+
+       cookie = doorbell_read(guc, e, cookie);
+       doorbell_write(guc, e, cookie, cookie + 1 ?: cookie + 2);
+
+       XE_WARN_ON(doorbell_read(guc, e, db_status) != GUC_DOORBELL_ENABLED);
+}
+
+#define GUC_MMIO_DOORBELL_RING_ACK     0xACEDBEEF
+#define GUC_MMIO_DOORBELL_RING_NACK    0xDEADBEEF
+static void ring_mmio_doorbell(struct xe_guc *guc, u32 doorbell_offset)
+{
+       u32 db_value;
+
+       db_value = xe_mmio_read32(guc_to_gt(guc), GUC_MMIO_DB_BAR_OFFSET +
+                                 doorbell_offset);
+
+       /*
+        * The read from the doorbell page will return ack/nack. We don't remove
+        * doorbells from active clients so we don't expect to ever get a nack.
+        * XXX: if doorbell is lost, re-acquire it?
+        */
+       XE_WARN_ON(db_value == GUC_MMIO_DOORBELL_RING_NACK);
+       XE_WARN_ON(db_value != GUC_MMIO_DOORBELL_RING_ACK);
+}
+
+static void ring_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       XE_BUG_ON(!has_doorbell(e));
+
+       if (HAS_GUC_MMIO_DB(guc_to_xe(guc)))
+               ring_mmio_doorbell(guc, e->guc->doorbell_offset);
+       else
+               ring_memory_doorbell(guc, e);
+
+       trace_xe_engine_ring_db(e);
+}
+
+static void register_engine(struct xe_engine *e);
+
+static int create_doorbell(struct xe_guc *guc, struct xe_engine *e, bool init)
+{

drivers/gpu/drm/xe/xe_guc_submit.c int xe_guc_submit_init(struct xe_guc *guc) · line 441

Quoted 10 lines
drivers/gpu/drm/xe/xe_guc_submit.c +10 -0
+       struct xe_gt *gt = guc_to_gt(guc);
+       struct xe_device *xe = gt_to_xe(gt);
+       u64 gpa;
+       u32 gtt_addr;
+       int ret;
+
+       XE_BUG_ON(!has_doorbell(e));
+
+       if (HAS_GUC_MMIO_DB(xe)) {
+               e->guc->doorbell_offset = PAGE_SIZE * e->guc->doorbell_id;

I think there is an implied assumption here (and in the code below) that PAGE_SIZE is always 4KB, which is problematic in non-x86 architectures.
If there is no limitation on xe being used solely on x86_64, then I think it would be better to change this to SZ_4K

Quoted 40 lines
drivers/gpu/drm/xe/xe_guc_submit.c +40 -0
+               gpa = GUC_MMIO_DB_BAR_OFFSET + e->guc->doorbell_offset;
+               gtt_addr = 0;
+       } else {
+               struct xe_bo *bo;
+
+               if (!e->guc->doorbell_bo) {
+                       bo = xe_bo_create_pin_map(xe, gt, NULL, PAGE_SIZE,
+                                                 ttm_bo_type_kernel,
+                                                 XE_BO_CREATE_VRAM_IF_DGFX(gt) |
+                                                 XE_BO_CREATE_GGTT_BIT);
+                       if (IS_ERR(bo))
+                               return PTR_ERR(bo);
+
+                       e->guc->doorbell_bo = bo;
+               } else {
+                       bo = e->guc->doorbell_bo;
+               }
+
+               init_doorbell(guc, e);
+               gpa = xe_bo_main_addr(bo, PAGE_SIZE);
+               gtt_addr = xe_bo_ggtt_addr(bo);
+       }
+
+       if (init && e->flags & ENGINE_FLAG_KERNEL)
+               return 0;
+
+       register_engine(e);
+       ret = allocate_doorbell(guc, e->guc->id, e->guc->doorbell_id, gpa,
+                               gtt_addr);
+       if (ret < 0) {
+               fini_doorbell(guc, e);
+               return ret;
+       }
+
+       /*
+        * In distributed doorbells, guc is returning the cacheline selected
+        * by HW as part of the 7bit data from the allocate doorbell command:
+        *  bit [22]   - Cacheline allocated
+        *  bit [21:16] - Cacheline offset address
+        * (bit 21 must be zero, or our assumption of only using half a page is

drivers/gpu/drm/xe/xe_guc_submit.c int xe_guc_submit_init(struct xe_guc *guc) · line 491

Quoted 10 lines
drivers/gpu/drm/xe/xe_guc_submit.c +10 -0
+        * no longer correct).
+        */
+       if (HAS_GUC_DIST_DB(xe)) {
+               u32 dd_cacheline_info;
+
+               XE_WARN_ON(!(ret & BIT(22)));
+               XE_WARN_ON(ret & BIT(21));
+
+               dd_cacheline_info = FIELD_GET(GENMASK(21, 16), ret);
+               e->guc->doorbell_offset = dd_cacheline_info * cache_line_size();

I don't understand something. We have 256 doorbells, but here you overrun the doorbell_offset, where dd_cacheline_info can be 0-31 (because bit 21 is always 0).
This has overlap... Some doorbells will get the same address.

Quoted 168 lines
drivers/gpu/drm/xe/xe_guc_submit.c +45 -4
+
+               /* and verify db status was updated correctly by the guc fw */
+               XE_WARN_ON(doorbell_read(guc, e, db_status) !=
+                          GUC_DOORBELL_ENABLED);
+       }
+
+       set_engine_doorbell_registered(e);
+
+       return 0;
+}
+
 static int alloc_guc_id(struct xe_guc *guc, struct xe_engine *e)
 {
        int ret;
@@ -623,6 +880,7 @@ static void submit_engine(struct xe_engine *e)
        u32 num_g2h = 0;
        int len = 0;
        bool extra_submit = false;
+       bool enable = false;
 
        XE_BUG_ON(!engine_registered(e));
 
@@ -642,6 +900,7 @@ static void submit_engine(struct xe_engine *e)
                num_g2h = 1;
                if (xe_engine_is_parallel(e))
                        extra_submit = true;
+               enable = true;
 
                e->guc->resume_time = RESUME_PENDING;
                set_engine_pending_enable(e);
@@ -653,7 +912,10 @@ static void submit_engine(struct xe_engine *e)
                trace_xe_engine_submit(e);
        }
 
-       xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+       if (enable || !engine_doorbell_registered(e))
+               xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+       else
+               ring_doorbell(guc, e);
 
        if (extra_submit) {
                len = 0;
@@ -678,8 +940,17 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
        trace_xe_sched_job_run(job);
 
        if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
-               if (!engine_registered(e))
-                       register_engine(e);
+               if (!engine_registered(e)) {
+                       if (has_doorbell(e)) {
+                               int err = create_doorbell(engine_to_guc(e), e,
+                                                         false);
+
+                               /* Not fatal, but let's warn */
+                               XE_WARN_ON(err);
+                       } else {
+                               register_engine(e);
+                       }
+               }
                if (!lr)        /* Written in IOCTL */
                        e->ring_ops->emit_job(job);
                submit_engine(e);
@@ -722,6 +993,11 @@ static void disable_scheduling_deregister(struct xe_guc *guc,
        MAKE_SCHED_CONTEXT_ACTION(e, DISABLE);
        int ret;
 
+       if (has_doorbell(e)) {
+               fini_doorbell(guc, e);
+               deallocate_doorbell(guc, e->guc->id);
+       }
+
        set_min_preemption_timeout(guc, e);
        smp_rmb();
        ret = wait_event_timeout(guc->ct.wq, !engine_pending_enable(e) ||
@@ -958,6 +1234,7 @@ static void __guc_engine_fini_async(struct work_struct *w)
                cancel_work_sync(&ge->lr_tdr);
        if (e->flags & ENGINE_FLAG_PERSISTENT)
                xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
+       destroy_doorbell(guc, e);
        release_guc_id(guc, e);
        drm_sched_entity_fini(&ge->entity);
        drm_sched_fini(&ge->sched);
@@ -1136,6 +1413,7 @@ static int guc_engine_init(struct xe_engine *e)
        struct xe_guc_engine *ge;
        long timeout;
        int err;
+       bool create_db = false;
 
        XE_BUG_ON(!xe_device_guc_submission_enabled(guc_to_xe(guc)));
 
@@ -1177,8 +1455,17 @@ static int guc_engine_init(struct xe_engine *e)
        if (guc_read_stopped(guc))
                drm_sched_stop(sched, NULL);
 
+       create_db = alloc_doorbell_id(guc, e);
+
        mutex_unlock(&guc->submission_state.lock);
 
+       if (create_db) {
+               /* Error isn't fatal as we don't need a doorbell */
+               err = create_doorbell(guc, e, true);
+               if (err)
+                       release_doorbell_id(guc, e);
+       }
+
        switch (e->class) {
        case XE_ENGINE_CLASS_RENDER:
                sprintf(e->name, "rcs%d", e->guc->id);
@@ -1302,7 +1589,7 @@ static int guc_engine_set_job_timeout(struct xe_engine *e, u32 job_timeout_ms)
 {
        struct drm_gpu_scheduler *sched = &e->guc->sched;
 
-       XE_BUG_ON(engine_registered(e));
+       XE_BUG_ON(engine_registered(e) && !has_doorbell(e));
        XE_BUG_ON(engine_banned(e));
        XE_BUG_ON(engine_killed(e));
 
drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 8002734d6f24..bada6c02d6aa 100644
@@ -13,6 +13,7 @@ struct xe_engine;
 struct xe_guc;
 
 int xe_guc_submit_init(struct xe_guc *guc);
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc);
 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p);
 
 int xe_guc_submit_reset_prepare(struct xe_guc *guc);
drivers/gpu/drm/xe/xe_guc_types.h +4 -0
index ac7eec28934d..9ee4d572f4e0 100644
@@ -36,10 +36,14 @@ struct xe_guc {
                struct xarray engine_lookup;
                /** @guc_ids: used to allocate new guc_ids, single-lrc */
                struct ida guc_ids;
+               /** @doorbell_ids: use to allocate new doorbells */
+               struct ida doorbell_ids;
                /** @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc */
                unsigned long *guc_ids_bitmap;
                /** @stopped: submissions are stopped */
                atomic_t stopped;
+               /** @num_doorbells: number of doorbels */
+               int num_doorbells;
                /** @lock: protects submission state */
                struct mutex lock;
                /** @suspend: suspend fence state */
drivers/gpu/drm/xe/xe_trace.h +5 -1
index 02861c26e145..38e9d7c6197b 100644
@@ -149,6 +149,11 @@ DEFINE_EVENT(xe_engine, xe_engine_submit,
             TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_ring_db,
+            TP_PROTO(struct xe_engine *e),
+            TP_ARGS(e)
+);
+
 DEFINE_EVENT(xe_engine, xe_engine_scheduling_enable,
             TP_PROTO(struct xe_engine *e),
             TP_ARGS(e)
--

2.34.1

author maintainer
#59 lore raw

Re: [Intel-xe] [PATCH v2 11/31] drm/xe/guc: Use doorbells for submission if possible

Quoted 329 lines · Oded Gabbay
Quoted 328 lines · Matthew Brost

We have 256 doorbells (on most platforms) that we can allocate to bypass using the H2G channel for submission. This will avoid contention on the CT mutex.

---
 drivers/gpu/drm/xe/regs/xe_guc_regs.h    |   1 +
 drivers/gpu/drm/xe/xe_guc.c              |   6 +
 drivers/gpu/drm/xe/xe_guc_engine_types.h |   7 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 295 ++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_guc_submit.h       |   1 +
 drivers/gpu/drm/xe/xe_guc_types.h        |   4 +
 drivers/gpu/drm/xe/xe_trace.h            |   5 +
 7 files changed, 315 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/regs/xe_guc_regs.h +1 -0
index 37e0ac550931..11b117293a62 100644
@@ -109,6 +109,7 @@ struct guc_doorbell_info {
 
 #define DIST_DBS_POPULATED                     XE_REG(0xd08)
 #define   DOORBELLS_PER_SQIDI_MASK             REG_GENMASK(23, 16)
+#define          DOORBELLS_PER_SQIDI_SHIFT             16
 #define   SQIDIS_DOORBELL_EXIST_MASK           REG_GENMASK(15, 0)
 
 #define GUC_BCS_RCS_IER                                XE_REG(0xC550)
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 89d20faced19..0c87f78a868b 100644
@@ -297,6 +297,12 @@ int xe_guc_init(struct xe_guc *guc)
  */
 int xe_guc_init_post_hwconfig(struct xe_guc *guc)
 {
+       int ret;
+
+       ret = xe_guc_submit_init_post_hwconfig(guc);
+       if (ret)
+               return ret;
+
        return xe_guc_ads_init_post_hwconfig(&guc->ads);
 }
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +7 -0
index 5d83132034a6..420b7f53e649 100644
@@ -12,6 +12,7 @@
 #include <drm/gpu_scheduler.h>
 
 struct dma_fence;
+struct xe_bo;
 struct xe_engine;
 
 /**
@@ -37,6 +38,10 @@ struct xe_guc_engine {
        struct work_struct fini_async;
        /** @resume_time: time of last resume */
        u64 resume_time;
+       /** @doorbell_bo: BO for memory doorbell */
+       struct xe_bo *doorbell_bo;
+       /** @doorbell_offset: MMIO doorbell offset */
+       u32 doorbell_offset;
        /** @state: GuC specific state for this xe_engine */
        atomic_t state;
        /** @wqi_head: work queue item tail */
@@ -45,6 +50,8 @@ struct xe_guc_engine {
        u32 wqi_tail;
        /** @id: GuC id for this xe_engine */
        u16 id;
+       /** @doorbell_id: doorbell id */
+       u16 doorbell_id;
        /** @suspend_wait: wait queue used to wait on pending suspends */
        wait_queue_head_t suspend_wait;
        /** @suspend_pending: a suspend of the engine is pending */
drivers/gpu/drm/xe/xe_guc_submit.c +193 -0
index 0a41f5d04f6d..1b6f36b04cd1 100644
@@ -13,7 +13,10 @@
 
 #include <drm/drm_managed.h>
 
+#include "regs/xe_guc_regs.h"
 #include "regs/xe_lrc_layout.h"
+
+#include "xe_bo.h"
 #include "xe_device.h"
 #include "xe_engine.h"
 #include "xe_force_wake.h"
@@ -26,12 +29,22 @@
 #include "xe_lrc.h"
 #include "xe_macros.h"
 #include "xe_map.h"
+#include "xe_mmio.h"
 #include "xe_mocs.h"
 #include "xe_ring_ops_types.h"
 #include "xe_sched_job.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
+#define HAS_GUC_MMIO_DB(xe) (IS_DGFX(xe) || GRAPHICS_VERx100(xe) >= 1250)
+#define HAS_GUC_DIST_DB(xe) \
+       (GRAPHICS_VERx100(xe) >= 1200 && !HAS_GUC_MMIO_DB(xe))
+
+#define GUC_NUM_HW_DOORBELLS 256
+
+#define GUC_MMIO_DB_BAR_OFFSET SZ_4M
+#define GUC_MMIO_DB_BAR_SIZE SZ_4M
+
 static struct xe_gt *
 guc_to_gt(struct xe_guc *guc)
 {
@@ -63,6 +76,7 @@ engine_to_guc(struct xe_engine *e)
 #define ENGINE_STATE_SUSPENDED         (1 << 5)
 #define ENGINE_STATE_RESET             (1 << 6)
 #define ENGINE_STATE_KILLED            (1 << 7)
+#define ENGINE_STATE_DB_REGISTERED     (1 << 8)
 
 static bool engine_registered(struct xe_engine *e)
 {
@@ -179,6 +193,16 @@ static void set_engine_killed(struct xe_engine *e)
        atomic_or(ENGINE_STATE_KILLED, &e->guc->state);
 }
 
+static bool engine_doorbell_registered(struct xe_engine *e)
+{
+       return atomic_read(&e->guc->state) & ENGINE_STATE_DB_REGISTERED;
+}
+
+static void set_engine_doorbell_registered(struct xe_engine *e)
+{
+       atomic_or(ENGINE_STATE_DB_REGISTERED, &e->guc->state);
+}
+
 static bool engine_killed_or_banned(struct xe_engine *e)
 {
        return engine_killed(e) || engine_banned(e);
@@ -190,6 +214,7 @@ static void guc_submit_fini(struct drm_device *drm, void *arg)
 
        xa_destroy(&guc->submission_state.engine_lookup);
        ida_destroy(&guc->submission_state.guc_ids);
+       ida_destroy(&guc->submission_state.doorbell_ids);
        bitmap_free(guc->submission_state.guc_ids_bitmap);
 }
 
@@ -230,6 +255,7 @@ int xe_guc_submit_init(struct xe_guc *guc)
        mutex_init(&guc->submission_state.lock);
        xa_init(&guc->submission_state.engine_lookup);
        ida_init(&guc->submission_state.guc_ids);
+       ida_init(&guc->submission_state.doorbell_ids);
 
        spin_lock_init(&guc->submission_state.suspend.lock);
        guc->submission_state.suspend.context = dma_fence_context_alloc(1);
@@ -243,6 +269,237 @@ int xe_guc_submit_init(struct xe_guc *guc)
        return 0;
 }
 
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc)
+{
+       if (HAS_GUC_DIST_DB(guc_to_xe(guc))) {
+               u32 distdbreg = xe_mmio_read32(guc_to_gt(guc),
+                                              DIST_DBS_POPULATED.reg);
+               u32 num_sqidi =
+                       hweight32(distdbreg & SQIDIS_DOORBELL_EXIST_MASK);
+               u32 doorbells_per_sqidi =
+                       ((distdbreg >> DOORBELLS_PER_SQIDI_SHIFT) &
+                        DOORBELLS_PER_SQIDI_MASK) + 1;
+
+               guc->submission_state.num_doorbells =
+                       num_sqidi * doorbells_per_sqidi;
+       } else {
+               guc->submission_state.num_doorbells = GUC_NUM_HW_DOORBELLS;
+       }
+
+       return 0;
+}
+
+static bool alloc_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+       int ret;
+
+       lockdep_assert_held(&guc->submission_state.lock);
+
+       e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+       ret = ida_simple_get(&guc->submission_state.doorbell_ids, 0,
+                            guc->submission_state.num_doorbells, GFP_NOWAIT);
+       if (ret < 0)
+               return false;
+
+       e->guc->doorbell_id = ret;
+
+       return true;
+}
+
+static void release_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+       mutex_lock(&guc->submission_state.lock);
+       ida_simple_remove(&guc->submission_state.doorbell_ids,
+                         e->guc->doorbell_id);
+       mutex_unlock(&guc->submission_state.lock);
+
+       e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+}
+
+static int allocate_doorbell(struct xe_guc *guc, u16 guc_id, u16 doorbell_id,
+                            u64 gpa, u32 gtt_addr)
+{
+       u32 action[] = {
+               XE_GUC_ACTION_ALLOCATE_DOORBELL,
+               guc_id,
+               doorbell_id,
+               lower_32_bits(gpa),
+               upper_32_bits(gpa),
+               gtt_addr
+       };
+
+       return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action));
+}
+
+static void deallocate_doorbell(struct xe_guc *guc, u16 guc_id)
+{
+       u32 action[] = {
+               XE_GUC_ACTION_DEALLOCATE_DOORBELL,
+               guc_id
+       };
+
+       xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
+}
+
+static bool has_doorbell(struct xe_engine *e)
+{
+       return e->guc->doorbell_id != GUC_NUM_HW_DOORBELLS;
+}
+
+#define doorbell_read(guc_, e_, field_) ({                     \
+       struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;  \
+       iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);     \
+       xe_map_rd_field(guc_to_xe((guc_)), &_vmap, 0,           \
+                                 struct guc_doorbell_info, field_); \
+       })
+#define doorbell_write(guc_, e_, field_, val_) ({              \
+       struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;  \
+       iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);     \
+       xe_map_wr_field(guc_to_xe((guc_)), &_vmap, 0,           \
+                                 struct guc_doorbell_info, field_, val_); \
+       })
+
+static void init_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       struct xe_device *xe = guc_to_xe(guc);
+
+       /* GuC does the initialization with distributed and MMIO doorbells */
+       if (!HAS_GUC_DIST_DB(xe) && !HAS_GUC_MMIO_DB(xe)) {
+               doorbell_write(guc, e, db_status, GUC_DOORBELL_ENABLED);
+               doorbell_write(guc, e, cookie, 0);
+       }
+}
+
+static void fini_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       if (!HAS_GUC_MMIO_DB(guc_to_xe(guc)) &&
+           xe_device_mem_access_ongoing(guc_to_xe(guc)))
+               doorbell_write(guc, e, db_status, GUC_DOORBELL_DISABLED);
+}
+
+static void destroy_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       if (has_doorbell(e)) {
+               release_doorbell_id(guc, e);
+               xe_bo_unpin_map_no_vm(e->guc->doorbell_bo);
+       }
+}
+
+static void ring_memory_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       u32 cookie;
+
+       cookie = doorbell_read(guc, e, cookie);
+       doorbell_write(guc, e, cookie, cookie + 1 ?: cookie + 2);
+
+       XE_WARN_ON(doorbell_read(guc, e, db_status) != GUC_DOORBELL_ENABLED);
+}
+
+#define GUC_MMIO_DOORBELL_RING_ACK     0xACEDBEEF
+#define GUC_MMIO_DOORBELL_RING_NACK    0xDEADBEEF
+static void ring_mmio_doorbell(struct xe_guc *guc, u32 doorbell_offset)
+{
+       u32 db_value;
+
+       db_value = xe_mmio_read32(guc_to_gt(guc), GUC_MMIO_DB_BAR_OFFSET +
+                                 doorbell_offset);
+
+       /*
+        * The read from the doorbell page will return ack/nack. We don't remove
+        * doorbells from active clients so we don't expect to ever get a nack.
+        * XXX: if doorbell is lost, re-acquire it?
+        */
+       XE_WARN_ON(db_value == GUC_MMIO_DOORBELL_RING_NACK);
+       XE_WARN_ON(db_value != GUC_MMIO_DOORBELL_RING_ACK);
+}
+
+static void ring_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       XE_BUG_ON(!has_doorbell(e));
+
+       if (HAS_GUC_MMIO_DB(guc_to_xe(guc)))
+               ring_mmio_doorbell(guc, e->guc->doorbell_offset);
+       else
+               ring_memory_doorbell(guc, e);
+
+       trace_xe_engine_ring_db(e);
+}
+
+static void register_engine(struct xe_engine *e);
+
+static int create_doorbell(struct xe_guc *guc, struct xe_engine *e, bool init)
+{
+       struct xe_gt *gt = guc_to_gt(guc);
+       struct xe_device *xe = gt_to_xe(gt);
+       u64 gpa;
+       u32 gtt_addr;
+       int ret;
+
+       XE_BUG_ON(!has_doorbell(e));
Quoted 9 lines

drivers/gpu/drm/xe/xe_guc_submit.c int xe_guc_submit_init(struct xe_guc *guc) · line 441

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +3 -0
+
+       if (HAS_GUC_MMIO_DB(xe)) {
+               e->guc->doorbell_offset = PAGE_SIZE * e->guc->doorbell_id;

I think there is an implied assumption here (and in the code below) that PAGE_SIZE is always 4KB, which is problematic in non-x86 architectures.
If there is no limitation on xe being used solely on x86_64, then I think it would be better to change this to SZ_4K

Will fix.

Quoted 46 lines
Quoted 46 lines
drivers/gpu/drm/xe/xe_guc_submit.c +46 -0
+               gpa = GUC_MMIO_DB_BAR_OFFSET + e->guc->doorbell_offset;
+               gtt_addr = 0;
+       } else {
+               struct xe_bo *bo;
+
+               if (!e->guc->doorbell_bo) {
+                       bo = xe_bo_create_pin_map(xe, gt, NULL, PAGE_SIZE,
+                                                 ttm_bo_type_kernel,
+                                                 XE_BO_CREATE_VRAM_IF_DGFX(gt) |
+                                                 XE_BO_CREATE_GGTT_BIT);
+                       if (IS_ERR(bo))
+                               return PTR_ERR(bo);
+
+                       e->guc->doorbell_bo = bo;
+               } else {
+                       bo = e->guc->doorbell_bo;
+               }
+
+               init_doorbell(guc, e);
+               gpa = xe_bo_main_addr(bo, PAGE_SIZE);
+               gtt_addr = xe_bo_ggtt_addr(bo);
+       }
+
+       if (init && e->flags & ENGINE_FLAG_KERNEL)
+               return 0;
+
+       register_engine(e);
+       ret = allocate_doorbell(guc, e->guc->id, e->guc->doorbell_id, gpa,
+                               gtt_addr);
+       if (ret < 0) {
+               fini_doorbell(guc, e);
+               return ret;
+       }
+
+       /*
+        * In distributed doorbells, guc is returning the cacheline selected
+        * by HW as part of the 7bit data from the allocate doorbell command:
+        *  bit [22]   - Cacheline allocated
+        *  bit [21:16] - Cacheline offset address
+        * (bit 21 must be zero, or our assumption of only using half a page is
+        * no longer correct).
+        */
+       if (HAS_GUC_DIST_DB(xe)) {
+               u32 dd_cacheline_info;
+
+               XE_WARN_ON(!(ret & BIT(22)));
Quoted 8 lines

drivers/gpu/drm/xe/xe_guc_submit.c int xe_guc_submit_init(struct xe_guc *guc) · line 491

Quoted 4 lines
drivers/gpu/drm/xe/xe_guc_submit.c +4 -0
+               XE_WARN_ON(ret & BIT(21));
+
+               dd_cacheline_info = FIELD_GET(GENMASK(21, 16), ret);
+               e->guc->doorbell_offset = dd_cacheline_info * cache_line_size();

I don't understand something. We have 256 doorbells, but here you overrun the doorbell_offset, where dd_cacheline_info can be 0-31 (because bit 21 is always 0).
This has overlap... Some doorbells will get the same address.

Yes, there is a 8 to 1 multiplexing behind the scenes here.

Matt

Quoted 168 lines
Quoted 168 lines
drivers/gpu/drm/xe/xe_guc_submit.c +45 -4
+
+               /* and verify db status was updated correctly by the guc fw */
+               XE_WARN_ON(doorbell_read(guc, e, db_status) !=
+                          GUC_DOORBELL_ENABLED);
+       }
+
+       set_engine_doorbell_registered(e);
+
+       return 0;
+}
+
 static int alloc_guc_id(struct xe_guc *guc, struct xe_engine *e)
 {
        int ret;
@@ -623,6 +880,7 @@ static void submit_engine(struct xe_engine *e)
        u32 num_g2h = 0;
        int len = 0;
        bool extra_submit = false;
+       bool enable = false;
 
        XE_BUG_ON(!engine_registered(e));
 
@@ -642,6 +900,7 @@ static void submit_engine(struct xe_engine *e)
                num_g2h = 1;
                if (xe_engine_is_parallel(e))
                        extra_submit = true;
+               enable = true;
 
                e->guc->resume_time = RESUME_PENDING;
                set_engine_pending_enable(e);
@@ -653,7 +912,10 @@ static void submit_engine(struct xe_engine *e)
                trace_xe_engine_submit(e);
        }
 
-       xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+       if (enable || !engine_doorbell_registered(e))
+               xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+       else
+               ring_doorbell(guc, e);
 
        if (extra_submit) {
                len = 0;
@@ -678,8 +940,17 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
        trace_xe_sched_job_run(job);
 
        if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
-               if (!engine_registered(e))
-                       register_engine(e);
+               if (!engine_registered(e)) {
+                       if (has_doorbell(e)) {
+                               int err = create_doorbell(engine_to_guc(e), e,
+                                                         false);
+
+                               /* Not fatal, but let's warn */
+                               XE_WARN_ON(err);
+                       } else {
+                               register_engine(e);
+                       }
+               }
                if (!lr)        /* Written in IOCTL */
                        e->ring_ops->emit_job(job);
                submit_engine(e);
@@ -722,6 +993,11 @@ static void disable_scheduling_deregister(struct xe_guc *guc,
        MAKE_SCHED_CONTEXT_ACTION(e, DISABLE);
        int ret;
 
+       if (has_doorbell(e)) {
+               fini_doorbell(guc, e);
+               deallocate_doorbell(guc, e->guc->id);
+       }
+
        set_min_preemption_timeout(guc, e);
        smp_rmb();
        ret = wait_event_timeout(guc->ct.wq, !engine_pending_enable(e) ||
@@ -958,6 +1234,7 @@ static void __guc_engine_fini_async(struct work_struct *w)
                cancel_work_sync(&ge->lr_tdr);
        if (e->flags & ENGINE_FLAG_PERSISTENT)
                xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
+       destroy_doorbell(guc, e);
        release_guc_id(guc, e);
        drm_sched_entity_fini(&ge->entity);
        drm_sched_fini(&ge->sched);
@@ -1136,6 +1413,7 @@ static int guc_engine_init(struct xe_engine *e)
        struct xe_guc_engine *ge;
        long timeout;
        int err;
+       bool create_db = false;
 
        XE_BUG_ON(!xe_device_guc_submission_enabled(guc_to_xe(guc)));
 
@@ -1177,8 +1455,17 @@ static int guc_engine_init(struct xe_engine *e)
        if (guc_read_stopped(guc))
                drm_sched_stop(sched, NULL);
 
+       create_db = alloc_doorbell_id(guc, e);
+
        mutex_unlock(&guc->submission_state.lock);
 
+       if (create_db) {
+               /* Error isn't fatal as we don't need a doorbell */
+               err = create_doorbell(guc, e, true);
+               if (err)
+                       release_doorbell_id(guc, e);
+       }
+
        switch (e->class) {
        case XE_ENGINE_CLASS_RENDER:
                sprintf(e->name, "rcs%d", e->guc->id);
@@ -1302,7 +1589,7 @@ static int guc_engine_set_job_timeout(struct xe_engine *e, u32 job_timeout_ms)
 {
        struct drm_gpu_scheduler *sched = &e->guc->sched;
 
-       XE_BUG_ON(engine_registered(e));
+       XE_BUG_ON(engine_registered(e) && !has_doorbell(e));
        XE_BUG_ON(engine_banned(e));
        XE_BUG_ON(engine_killed(e));
 
drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 8002734d6f24..bada6c02d6aa 100644
@@ -13,6 +13,7 @@ struct xe_engine;
 struct xe_guc;
 
 int xe_guc_submit_init(struct xe_guc *guc);
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc);
 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p);
 
 int xe_guc_submit_reset_prepare(struct xe_guc *guc);
drivers/gpu/drm/xe/xe_guc_types.h +4 -0
index ac7eec28934d..9ee4d572f4e0 100644
@@ -36,10 +36,14 @@ struct xe_guc {
                struct xarray engine_lookup;
                /** @guc_ids: used to allocate new guc_ids, single-lrc */
                struct ida guc_ids;
+               /** @doorbell_ids: use to allocate new doorbells */
+               struct ida doorbell_ids;
                /** @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc */
                unsigned long *guc_ids_bitmap;
                /** @stopped: submissions are stopped */
                atomic_t stopped;
+               /** @num_doorbells: number of doorbels */
+               int num_doorbells;
                /** @lock: protects submission state */
                struct mutex lock;
                /** @suspend: suspend fence state */
drivers/gpu/drm/xe/xe_trace.h +5 -1
index 02861c26e145..38e9d7c6197b 100644
@@ -149,6 +149,11 @@ DEFINE_EVENT(xe_engine, xe_engine_submit,
             TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_ring_db,
+            TP_PROTO(struct xe_engine *e),
+            TP_ARGS(e)
+);
+
 DEFINE_EVENT(xe_engine, xe_engine_scheduling_enable,
             TP_PROTO(struct xe_engine *e),
             TP_ARGS(e)
--

2.34.1

#60 lore raw

Re: [Intel-xe] [PATCH v2 11/31] drm/xe/guc: Use doorbells for submission if possible

Quoted 398 lines · Matthew Brost
Quoted 329 lines · Oded Gabbay
Quoted 328 lines · Matthew Brost

We have 256 doorbells (on most platforms) that we can allocate to bypass using the H2G channel for submission. This will avoid contention on the CT mutex.

---
 drivers/gpu/drm/xe/regs/xe_guc_regs.h    |   1 +
 drivers/gpu/drm/xe/xe_guc.c              |   6 +
 drivers/gpu/drm/xe/xe_guc_engine_types.h |   7 +
 drivers/gpu/drm/xe/xe_guc_submit.c       | 295 ++++++++++++++++++++++-
 drivers/gpu/drm/xe/xe_guc_submit.h       |   1 +
 drivers/gpu/drm/xe/xe_guc_types.h        |   4 +
 drivers/gpu/drm/xe/xe_trace.h            |   5 +
 7 files changed, 315 insertions(+), 4 deletions(-)
drivers/gpu/drm/xe/regs/xe_guc_regs.h +1 -0
index 37e0ac550931..11b117293a62 100644
@@ -109,6 +109,7 @@ struct guc_doorbell_info {
 
 #define DIST_DBS_POPULATED                     XE_REG(0xd08)
 #define   DOORBELLS_PER_SQIDI_MASK             REG_GENMASK(23, 16)
+#define          DOORBELLS_PER_SQIDI_SHIFT             16
 #define   SQIDIS_DOORBELL_EXIST_MASK           REG_GENMASK(15, 0)
 
 #define GUC_BCS_RCS_IER                                XE_REG(0xC550)
drivers/gpu/drm/xe/xe_guc.c +6 -0
index 89d20faced19..0c87f78a868b 100644
@@ -297,6 +297,12 @@ int xe_guc_init(struct xe_guc *guc)
  */
 int xe_guc_init_post_hwconfig(struct xe_guc *guc)
 {
+       int ret;
+
+       ret = xe_guc_submit_init_post_hwconfig(guc);
+       if (ret)
+               return ret;
+
        return xe_guc_ads_init_post_hwconfig(&guc->ads);
 }
 
drivers/gpu/drm/xe/xe_guc_engine_types.h +7 -0
index 5d83132034a6..420b7f53e649 100644
@@ -12,6 +12,7 @@
 #include <drm/gpu_scheduler.h>
 
 struct dma_fence;
+struct xe_bo;
 struct xe_engine;
 
 /**
@@ -37,6 +38,10 @@ struct xe_guc_engine {
        struct work_struct fini_async;
        /** @resume_time: time of last resume */
        u64 resume_time;
+       /** @doorbell_bo: BO for memory doorbell */
+       struct xe_bo *doorbell_bo;
+       /** @doorbell_offset: MMIO doorbell offset */
+       u32 doorbell_offset;
        /** @state: GuC specific state for this xe_engine */
        atomic_t state;
        /** @wqi_head: work queue item tail */
@@ -45,6 +50,8 @@ struct xe_guc_engine {
        u32 wqi_tail;
        /** @id: GuC id for this xe_engine */
        u16 id;
+       /** @doorbell_id: doorbell id */
+       u16 doorbell_id;
        /** @suspend_wait: wait queue used to wait on pending suspends */
        wait_queue_head_t suspend_wait;
        /** @suspend_pending: a suspend of the engine is pending */
drivers/gpu/drm/xe/xe_guc_submit.c +193 -0
index 0a41f5d04f6d..1b6f36b04cd1 100644
@@ -13,7 +13,10 @@
 
 #include <drm/drm_managed.h>
 
+#include "regs/xe_guc_regs.h"
 #include "regs/xe_lrc_layout.h"
+
+#include "xe_bo.h"
 #include "xe_device.h"
 #include "xe_engine.h"
 #include "xe_force_wake.h"
@@ -26,12 +29,22 @@
 #include "xe_lrc.h"
 #include "xe_macros.h"
 #include "xe_map.h"
+#include "xe_mmio.h"
 #include "xe_mocs.h"
 #include "xe_ring_ops_types.h"
 #include "xe_sched_job.h"
 #include "xe_trace.h"
 #include "xe_vm.h"
 
+#define HAS_GUC_MMIO_DB(xe) (IS_DGFX(xe) || GRAPHICS_VERx100(xe) >= 1250)
+#define HAS_GUC_DIST_DB(xe) \
+       (GRAPHICS_VERx100(xe) >= 1200 && !HAS_GUC_MMIO_DB(xe))
+
+#define GUC_NUM_HW_DOORBELLS 256
+
+#define GUC_MMIO_DB_BAR_OFFSET SZ_4M
+#define GUC_MMIO_DB_BAR_SIZE SZ_4M
+
 static struct xe_gt *
 guc_to_gt(struct xe_guc *guc)
 {
@@ -63,6 +76,7 @@ engine_to_guc(struct xe_engine *e)
 #define ENGINE_STATE_SUSPENDED         (1 << 5)
 #define ENGINE_STATE_RESET             (1 << 6)
 #define ENGINE_STATE_KILLED            (1 << 7)
+#define ENGINE_STATE_DB_REGISTERED     (1 << 8)
 
 static bool engine_registered(struct xe_engine *e)
 {
@@ -179,6 +193,16 @@ static void set_engine_killed(struct xe_engine *e)
        atomic_or(ENGINE_STATE_KILLED, &e->guc->state);
 }
 
+static bool engine_doorbell_registered(struct xe_engine *e)
+{
+       return atomic_read(&e->guc->state) & ENGINE_STATE_DB_REGISTERED;
+}
+
+static void set_engine_doorbell_registered(struct xe_engine *e)
+{
+       atomic_or(ENGINE_STATE_DB_REGISTERED, &e->guc->state);
+}
+
 static bool engine_killed_or_banned(struct xe_engine *e)
 {
        return engine_killed(e) || engine_banned(e);
@@ -190,6 +214,7 @@ static void guc_submit_fini(struct drm_device *drm, void *arg)
 
        xa_destroy(&guc->submission_state.engine_lookup);
        ida_destroy(&guc->submission_state.guc_ids);
+       ida_destroy(&guc->submission_state.doorbell_ids);
        bitmap_free(guc->submission_state.guc_ids_bitmap);
 }
 
@@ -230,6 +255,7 @@ int xe_guc_submit_init(struct xe_guc *guc)
        mutex_init(&guc->submission_state.lock);
        xa_init(&guc->submission_state.engine_lookup);
        ida_init(&guc->submission_state.guc_ids);
+       ida_init(&guc->submission_state.doorbell_ids);
 
        spin_lock_init(&guc->submission_state.suspend.lock);
        guc->submission_state.suspend.context = dma_fence_context_alloc(1);
@@ -243,6 +269,237 @@ int xe_guc_submit_init(struct xe_guc *guc)
        return 0;
 }
 
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc)
+{
+       if (HAS_GUC_DIST_DB(guc_to_xe(guc))) {
+               u32 distdbreg = xe_mmio_read32(guc_to_gt(guc),
+                                              DIST_DBS_POPULATED.reg);
+               u32 num_sqidi =
+                       hweight32(distdbreg & SQIDIS_DOORBELL_EXIST_MASK);
+               u32 doorbells_per_sqidi =
+                       ((distdbreg >> DOORBELLS_PER_SQIDI_SHIFT) &
+                        DOORBELLS_PER_SQIDI_MASK) + 1;
+
+               guc->submission_state.num_doorbells =
+                       num_sqidi * doorbells_per_sqidi;
+       } else {
+               guc->submission_state.num_doorbells = GUC_NUM_HW_DOORBELLS;
+       }
+
+       return 0;
+}
+
+static bool alloc_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+       int ret;
+
+       lockdep_assert_held(&guc->submission_state.lock);
+
+       e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+       ret = ida_simple_get(&guc->submission_state.doorbell_ids, 0,
+                            guc->submission_state.num_doorbells, GFP_NOWAIT);
+       if (ret < 0)
+               return false;
+
+       e->guc->doorbell_id = ret;
+
+       return true;
+}
+
+static void release_doorbell_id(struct xe_guc *guc, struct xe_engine *e)
+{
+       mutex_lock(&guc->submission_state.lock);
+       ida_simple_remove(&guc->submission_state.doorbell_ids,
+                         e->guc->doorbell_id);
+       mutex_unlock(&guc->submission_state.lock);
+
+       e->guc->doorbell_id = GUC_NUM_HW_DOORBELLS;
+}
+
+static int allocate_doorbell(struct xe_guc *guc, u16 guc_id, u16 doorbell_id,
+                            u64 gpa, u32 gtt_addr)
+{
+       u32 action[] = {
+               XE_GUC_ACTION_ALLOCATE_DOORBELL,
+               guc_id,
+               doorbell_id,
+               lower_32_bits(gpa),
+               upper_32_bits(gpa),
+               gtt_addr
+       };
+
+       return xe_guc_ct_send_block(&guc->ct, action, ARRAY_SIZE(action));
+}
+
+static void deallocate_doorbell(struct xe_guc *guc, u16 guc_id)
+{
+       u32 action[] = {
+               XE_GUC_ACTION_DEALLOCATE_DOORBELL,
+               guc_id
+       };
+
+       xe_guc_ct_send(&guc->ct, action, ARRAY_SIZE(action), 0, 0);
+}
+
+static bool has_doorbell(struct xe_engine *e)
+{
+       return e->guc->doorbell_id != GUC_NUM_HW_DOORBELLS;
+}
+
+#define doorbell_read(guc_, e_, field_) ({                     \
+       struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;  \
+       iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);     \
+       xe_map_rd_field(guc_to_xe((guc_)), &_vmap, 0,           \
+                                 struct guc_doorbell_info, field_); \
+       })
+#define doorbell_write(guc_, e_, field_, val_) ({              \
+       struct iosys_map _vmap = (e_)->guc->doorbell_bo->vmap;  \
+       iosys_map_incr(&_vmap, (e_)->guc->doorbell_offset);     \
+       xe_map_wr_field(guc_to_xe((guc_)), &_vmap, 0,           \
+                                 struct guc_doorbell_info, field_, val_); \
+       })
+
+static void init_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       struct xe_device *xe = guc_to_xe(guc);
+
+       /* GuC does the initialization with distributed and MMIO doorbells */
+       if (!HAS_GUC_DIST_DB(xe) && !HAS_GUC_MMIO_DB(xe)) {
+               doorbell_write(guc, e, db_status, GUC_DOORBELL_ENABLED);
+               doorbell_write(guc, e, cookie, 0);
+       }
+}
+
+static void fini_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       if (!HAS_GUC_MMIO_DB(guc_to_xe(guc)) &&
+           xe_device_mem_access_ongoing(guc_to_xe(guc)))
+               doorbell_write(guc, e, db_status, GUC_DOORBELL_DISABLED);
+}
+
+static void destroy_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       if (has_doorbell(e)) {
+               release_doorbell_id(guc, e);
+               xe_bo_unpin_map_no_vm(e->guc->doorbell_bo);
+       }
+}
+
+static void ring_memory_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       u32 cookie;
+
+       cookie = doorbell_read(guc, e, cookie);
+       doorbell_write(guc, e, cookie, cookie + 1 ?: cookie + 2);
+
+       XE_WARN_ON(doorbell_read(guc, e, db_status) != GUC_DOORBELL_ENABLED);
+}
+
+#define GUC_MMIO_DOORBELL_RING_ACK     0xACEDBEEF
+#define GUC_MMIO_DOORBELL_RING_NACK    0xDEADBEEF
+static void ring_mmio_doorbell(struct xe_guc *guc, u32 doorbell_offset)
+{
+       u32 db_value;
+
+       db_value = xe_mmio_read32(guc_to_gt(guc), GUC_MMIO_DB_BAR_OFFSET +
+                                 doorbell_offset);
+
+       /*
+        * The read from the doorbell page will return ack/nack. We don't remove
+        * doorbells from active clients so we don't expect to ever get a nack.
+        * XXX: if doorbell is lost, re-acquire it?
+        */
+       XE_WARN_ON(db_value == GUC_MMIO_DOORBELL_RING_NACK);
+       XE_WARN_ON(db_value != GUC_MMIO_DOORBELL_RING_ACK);
+}
+
+static void ring_doorbell(struct xe_guc *guc, struct xe_engine *e)
+{
+       XE_BUG_ON(!has_doorbell(e));
+
+       if (HAS_GUC_MMIO_DB(guc_to_xe(guc)))
+               ring_mmio_doorbell(guc, e->guc->doorbell_offset);
+       else
+               ring_memory_doorbell(guc, e);
+
+       trace_xe_engine_ring_db(e);
+}
+
+static void register_engine(struct xe_engine *e);
+
+static int create_doorbell(struct xe_guc *guc, struct xe_engine *e, bool init)
+{
+       struct xe_gt *gt = guc_to_gt(guc);
+       struct xe_device *xe = gt_to_xe(gt);
+       u64 gpa;
+       u32 gtt_addr;
+       int ret;
+
+       XE_BUG_ON(!has_doorbell(e));
Quoted 9 lines

drivers/gpu/drm/xe/xe_guc_submit.c int xe_guc_submit_init(struct xe_guc *guc) · line 441

Quoted 3 lines
drivers/gpu/drm/xe/xe_guc_submit.c +3 -0
+
+       if (HAS_GUC_MMIO_DB(xe)) {
+               e->guc->doorbell_offset = PAGE_SIZE * e->guc->doorbell_id;

I think there is an implied assumption here (and in the code below) that PAGE_SIZE is always 4KB, which is problematic in non-x86 architectures.
If there is no limitation on xe being used solely on x86_64, then I think it would be better to change this to SZ_4K

Will fix.

Quoted 46 lines
Quoted 46 lines
drivers/gpu/drm/xe/xe_guc_submit.c +46 -0
+               gpa = GUC_MMIO_DB_BAR_OFFSET + e->guc->doorbell_offset;
+               gtt_addr = 0;
+       } else {
+               struct xe_bo *bo;
+
+               if (!e->guc->doorbell_bo) {
+                       bo = xe_bo_create_pin_map(xe, gt, NULL, PAGE_SIZE,
+                                                 ttm_bo_type_kernel,
+                                                 XE_BO_CREATE_VRAM_IF_DGFX(gt) |
+                                                 XE_BO_CREATE_GGTT_BIT);
+                       if (IS_ERR(bo))
+                               return PTR_ERR(bo);
+
+                       e->guc->doorbell_bo = bo;
+               } else {
+                       bo = e->guc->doorbell_bo;
+               }
+
+               init_doorbell(guc, e);
+               gpa = xe_bo_main_addr(bo, PAGE_SIZE);
+               gtt_addr = xe_bo_ggtt_addr(bo);
+       }
+
+       if (init && e->flags & ENGINE_FLAG_KERNEL)
+               return 0;
+
+       register_engine(e);
+       ret = allocate_doorbell(guc, e->guc->id, e->guc->doorbell_id, gpa,
+                               gtt_addr);
+       if (ret < 0) {
+               fini_doorbell(guc, e);
+               return ret;
+       }
+
+       /*
+        * In distributed doorbells, guc is returning the cacheline selected
+        * by HW as part of the 7bit data from the allocate doorbell command:
+        *  bit [22]   - Cacheline allocated
+        *  bit [21:16] - Cacheline offset address
+        * (bit 21 must be zero, or our assumption of only using half a page is
+        * no longer correct).
+        */
+       if (HAS_GUC_DIST_DB(xe)) {
+               u32 dd_cacheline_info;
+
+               XE_WARN_ON(!(ret & BIT(22)));
Quoted 8 lines

drivers/gpu/drm/xe/xe_guc_submit.c int xe_guc_submit_init(struct xe_guc *guc) · line 491

Quoted 4 lines
drivers/gpu/drm/xe/xe_guc_submit.c +4 -0
+               XE_WARN_ON(ret & BIT(21));
+
+               dd_cacheline_info = FIELD_GET(GENMASK(21, 16), ret);
+               e->guc->doorbell_offset = dd_cacheline_info * cache_line_size();

I don't understand something. We have 256 doorbells, but here you overrun the doorbell_offset, where dd_cacheline_info can be 0-31 (because bit 21 is always 0).
This has overlap... Some doorbells will get the same address.

Quoted 3 lines

Yes, there is a 8 to 1 multiplexing behind the scenes here.

Matt

Could you please elaborate more on what is happening behind the scenes ? Maybe add that explanation as a comment on this line ?

Oded

Quoted 169 lines
Quoted 169 lines
Quoted 169 lines
drivers/gpu/drm/xe/xe_guc_submit.c +45 -4
+
+               /* and verify db status was updated correctly by the guc fw */
+               XE_WARN_ON(doorbell_read(guc, e, db_status) !=
+                          GUC_DOORBELL_ENABLED);
+       }
+
+       set_engine_doorbell_registered(e);
+
+       return 0;
+}
+
 static int alloc_guc_id(struct xe_guc *guc, struct xe_engine *e)
 {
        int ret;
@@ -623,6 +880,7 @@ static void submit_engine(struct xe_engine *e)
        u32 num_g2h = 0;
        int len = 0;
        bool extra_submit = false;
+       bool enable = false;
 
        XE_BUG_ON(!engine_registered(e));
 
@@ -642,6 +900,7 @@ static void submit_engine(struct xe_engine *e)
                num_g2h = 1;
                if (xe_engine_is_parallel(e))
                        extra_submit = true;
+               enable = true;
 
                e->guc->resume_time = RESUME_PENDING;
                set_engine_pending_enable(e);
@@ -653,7 +912,10 @@ static void submit_engine(struct xe_engine *e)
                trace_xe_engine_submit(e);
        }
 
-       xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+       if (enable || !engine_doorbell_registered(e))
+               xe_guc_ct_send(&guc->ct, action, len, g2h_len, num_g2h);
+       else
+               ring_doorbell(guc, e);
 
        if (extra_submit) {
                len = 0;
@@ -678,8 +940,17 @@ guc_engine_run_job(struct drm_sched_job *drm_job)
        trace_xe_sched_job_run(job);
 
        if (!engine_killed_or_banned(e) && !xe_sched_job_is_error(job)) {
-               if (!engine_registered(e))
-                       register_engine(e);
+               if (!engine_registered(e)) {
+                       if (has_doorbell(e)) {
+                               int err = create_doorbell(engine_to_guc(e), e,
+                                                         false);
+
+                               /* Not fatal, but let's warn */
+                               XE_WARN_ON(err);
+                       } else {
+                               register_engine(e);
+                       }
+               }
                if (!lr)        /* Written in IOCTL */
                        e->ring_ops->emit_job(job);
                submit_engine(e);
@@ -722,6 +993,11 @@ static void disable_scheduling_deregister(struct xe_guc *guc,
        MAKE_SCHED_CONTEXT_ACTION(e, DISABLE);
        int ret;
 
+       if (has_doorbell(e)) {
+               fini_doorbell(guc, e);
+               deallocate_doorbell(guc, e->guc->id);
+       }
+
        set_min_preemption_timeout(guc, e);
        smp_rmb();
        ret = wait_event_timeout(guc->ct.wq, !engine_pending_enable(e) ||
@@ -958,6 +1234,7 @@ static void __guc_engine_fini_async(struct work_struct *w)
                cancel_work_sync(&ge->lr_tdr);
        if (e->flags & ENGINE_FLAG_PERSISTENT)
                xe_device_remove_persistent_engines(gt_to_xe(e->gt), e);
+       destroy_doorbell(guc, e);
        release_guc_id(guc, e);
        drm_sched_entity_fini(&ge->entity);
        drm_sched_fini(&ge->sched);
@@ -1136,6 +1413,7 @@ static int guc_engine_init(struct xe_engine *e)
        struct xe_guc_engine *ge;
        long timeout;
        int err;
+       bool create_db = false;
 
        XE_BUG_ON(!xe_device_guc_submission_enabled(guc_to_xe(guc)));
 
@@ -1177,8 +1455,17 @@ static int guc_engine_init(struct xe_engine *e)
        if (guc_read_stopped(guc))
                drm_sched_stop(sched, NULL);
 
+       create_db = alloc_doorbell_id(guc, e);
+
        mutex_unlock(&guc->submission_state.lock);
 
+       if (create_db) {
+               /* Error isn't fatal as we don't need a doorbell */
+               err = create_doorbell(guc, e, true);
+               if (err)
+                       release_doorbell_id(guc, e);
+       }
+
        switch (e->class) {
        case XE_ENGINE_CLASS_RENDER:
                sprintf(e->name, "rcs%d", e->guc->id);
@@ -1302,7 +1589,7 @@ static int guc_engine_set_job_timeout(struct xe_engine *e, u32 job_timeout_ms)
 {
        struct drm_gpu_scheduler *sched = &e->guc->sched;
 
-       XE_BUG_ON(engine_registered(e));
+       XE_BUG_ON(engine_registered(e) && !has_doorbell(e));
        XE_BUG_ON(engine_banned(e));
        XE_BUG_ON(engine_killed(e));
 
drivers/gpu/drm/xe/xe_guc_submit.h +1 -0
index 8002734d6f24..bada6c02d6aa 100644
@@ -13,6 +13,7 @@ struct xe_engine;
 struct xe_guc;
 
 int xe_guc_submit_init(struct xe_guc *guc);
+int xe_guc_submit_init_post_hwconfig(struct xe_guc *guc);
 void xe_guc_submit_print(struct xe_guc *guc, struct drm_printer *p);
 
 int xe_guc_submit_reset_prepare(struct xe_guc *guc);
drivers/gpu/drm/xe/xe_guc_types.h +4 -0
index ac7eec28934d..9ee4d572f4e0 100644
@@ -36,10 +36,14 @@ struct xe_guc {
                struct xarray engine_lookup;
                /** @guc_ids: used to allocate new guc_ids, single-lrc */
                struct ida guc_ids;
+               /** @doorbell_ids: use to allocate new doorbells */
+               struct ida doorbell_ids;
                /** @guc_ids_bitmap: used to allocate new guc_ids, multi-lrc */
                unsigned long *guc_ids_bitmap;
                /** @stopped: submissions are stopped */
                atomic_t stopped;
+               /** @num_doorbells: number of doorbels */
+               int num_doorbells;
                /** @lock: protects submission state */
                struct mutex lock;
                /** @suspend: suspend fence state */
drivers/gpu/drm/xe/xe_trace.h +5 -1
index 02861c26e145..38e9d7c6197b 100644
@@ -149,6 +149,11 @@ DEFINE_EVENT(xe_engine, xe_engine_submit,
             TP_ARGS(e)
 );
 
+DEFINE_EVENT(xe_engine, xe_engine_ring_db,
+            TP_PROTO(struct xe_engine *e),
+            TP_ARGS(e)
+);
+
 DEFINE_EVENT(xe_engine, xe_engine_scheduling_enable,
             TP_PROTO(struct xe_engine *e),
             TP_ARGS(e)
--

2.34.1

author maintainer
#61 lore raw

PATCH v2 12/31 drm/xe/guc: Print doorbell ID in GuC engine debugfs entry

In reply to Matthew Brost

This information is helpful so print it.

---
 drivers/gpu/drm/xe/xe_guc_submit.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
index 1b6f36b04cd1..880f480c6d5f 100644
@@ -2016,6 +2016,8 @@ static void guc_engine_print(struct xe_engine *e, struct drm_printer *p)
 	drm_printf(p, "\tTimeslice: %u (us)\n", e->sched_props.timeslice_us);
 	drm_printf(p, "\tPreempt timeout: %u (us)\n",
 		   e->sched_props.preempt_timeout_us);
+	drm_printf(p, "\tDoorbell ID: %u\n",
+		   e->guc->doorbell_id);
 	for (i = 0; i < e->width; ++i ) {
 		struct xe_lrc *lrc = e->lrc + i;
 
Signature · 1 line
2.34.1
Reviewed-by on v2 12/31
#62 lore raw

Re: [Intel-xe] [PATCH v2 12/31] drm/xe/guc: Print doorbell ID in GuC engine debugfs entry

Quoted 3 lines · Matthew Brost

This information is helpful so print it.

Quoted 20 lines
---
 drivers/gpu/drm/xe/xe_guc_submit.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
index 1b6f36b04cd1..880f480c6d5f 100644
@@ -2016,6 +2016,8 @@ static void guc_engine_print(struct xe_engine *e, struct drm_printer *p)
 	drm_printf(p, "\tTimeslice: %u (us)\n", e->sched_props.timeslice_us);
 	drm_printf(p, "\tPreempt timeout: %u (us)\n",
 		   e->sched_props.preempt_timeout_us);
+	drm_printf(p, "\tDoorbell ID: %u\n",
+		   e->guc->doorbell_id);
 	for (i = 0; i < e->width; ++i ) {
 		struct xe_lrc *lrc = e->lrc + i;
 
Signature · 1 line
2.34.1
maintainer Reviewed-by
#63 lore raw

Re: [Intel-xe] [PATCH v2 12/31] drm/xe/guc: Print doorbell ID in GuC engine debugfs entry

Quoted 5 lines · Rodrigo Vivi
Quoted 3 lines · Matthew Brost

This information is helpful so print it.

Quoted 21 lines
Quoted 21 lines
---
  drivers/gpu/drm/xe/xe_guc_submit.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/gpu/drm/xe/xe_guc_submit.c +2 -0
index 1b6f36b04cd1..880f480c6d5f 100644
@@ -2016,6 +2016,8 @@ static void guc_engine_print(struct xe_engine *e, struct drm_printer *p)
  	drm_printf(p, "\tTimeslice: %u (us)\n", e->sched_props.timeslice_us);
  	drm_printf(p, "\tPreempt timeout: %u (us)\n",
  		   e->sched_props.preempt_timeout_us);
+	drm_printf(p, "\tDoorbell ID: %u\n",
+		   e->guc->doorbell_id);
  	for (i = 0; i < e->width; ++i ) {
  		struct xe_lrc *lrc = e->lrc + i;
 
Signature · 1 line
2.34.1
author maintainer
#64 lore raw

PATCH v2 13/31 maple_tree: split up MA_STATE() macro

In reply to Matthew Brost

From: Danilo Krummrich <[email protected]>

Split up the MA_STATE() macro such that components using the maple tree can easily inherit from struct ma_state and build custom tree walk macros to hide their internals from users.

Example:

struct sample_iterator {
        struct ma_state mas;
        struct sample_mgr *mgr;
};
\#define SAMPLE_ITERATOR(name, __mgr, start)                    \
        struct sample_iterator name = {                         \
                .mas = MA_STATE_INIT(&(__mgr)->mt, start, 0),   \
                .mgr = __mgr,                                   \
        }
\#define sample_iter_for_each_range(it__, entry__, end__) \
        mas_for_each(&(it__).mas, entry__, end__)

--

struct sample *sample;
SAMPLE_ITERATOR(si, min);

sample_iter_for_each_range(&si, sample, max) {
        frob(mgr, sample);
}
---
 include/linux/maple_tree.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
include/linux/maple_tree.h +5 -2
index 1fadb5f5978b..87d55334f1c2 100644
@@ -423,8 +423,8 @@ struct ma_wr_state {
 #define MA_ERROR(err) \
 		((struct maple_enode *)(((unsigned long)err << 2) | 2UL))
 
-#define MA_STATE(name, mt, first, end)					\
-	struct ma_state name = {					\
+#define MA_STATE_INIT(mt, first, end)					\
+	{								\
 		.tree = mt,						\
 		.index = first,						\
 		.last = end,						\
@@ -435,6 +435,9 @@ struct ma_wr_state {
 		.mas_flags = 0,						\
 	}
 
+#define MA_STATE(name, mt, first, end)					\
+	struct ma_state name = MA_STATE_INIT(mt, first, end)
+
 #define MA_WR_STATE(name, ma_state, wr_entry)				\
 	struct ma_wr_state name = {					\
 		.mas = ma_state,					\
Signature · 1 line
2.34.1
#65 lore raw

Re: [Intel-xe] [PATCH v2 13/31] maple_tree: split up MA_STATE() macro

Quoted 5 lines · Matthew Brost

From: Danilo Krummrich <[email protected]>

Split up the MA_STATE() macro such that components using the maple tree can easily inherit from struct ma_state and build custom tree walk macros to hide their internals from users.

I might misunderstand the patch, but isn't the real purpose to provide an MA_STATE initializer,and the way to achieve that is to split up the MA_STATE macro?

Quoted 36 lines

Example:

struct sample_iterator {
         struct ma_state mas;
         struct sample_mgr *mgr;
};
\#define SAMPLE_ITERATOR(name, __mgr, start)                    \
         struct sample_iterator name = {                         \
                 .mas = MA_STATE_INIT(&(__mgr)->mt, start, 0),   \
                 .mgr = __mgr,                                   \
         }
\#define sample_iter_for_each_range(it__, entry__, end__) \
         mas_for_each(&(it__).mas, entry__, end__)

--

struct sample *sample;
SAMPLE_ITERATOR(si, min);

sample_iter_for_each_range(&si, sample, max) {
         frob(mgr, sample);
}
---
  include/linux/maple_tree.h | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
include/linux/maple_tree.h +0 -0
index 1fadb5f5978b..87d55334f1c2 100644

include/linux/maple_tree.h struct ma_wr_state { · line 427

Quoted 7 lines
include/linux/maple_tree.h +2 -2
@@ -423,8 +423,8 @@ struct ma_wr_state {
  #define MA_ERROR(err) \
  		((struct maple_enode *)(((unsigned long)err << 2) | 2UL))
 
-#define MA_STATE(name, mt, first, end)					\
-	struct ma_state name = {					\
+#define MA_STATE_INIT(mt, first, end)					\
+	{								\

Naming: following the convention in, for example, the mutex and ww mutex code this should've been called

__MA_STATE_INITIALIZER(),

whereas the decapitalized name ma_state_init() would've been a (possibly inline) init function if it existed.

But this all should ofc be run by the maple tree maintainer(s).

FWIW, with these things addressed the change LGTM.

/Thomas

Quoted 13 lines
include/linux/maple_tree.h +3 -0
  		.tree = mt,						\
  		.index = first,						\
  		.last = end,						\
@@ -435,6 +435,9 @@ struct ma_wr_state {
  		.mas_flags = 0,						\
  	}
 
+#define MA_STATE(name, mt, first, end)					\
+	struct ma_state name = MA_STATE_INIT(mt, first, end)
+
  #define MA_WR_STATE(name, ma_state, wr_entry)				\
  	struct ma_wr_state name = {					\
  		.mas = ma_state,					\
author maintainer
#66 lore raw

Re: [Intel-xe] [PATCH v2 13/31] maple_tree: split up MA_STATE() macro

Quoted 4 lines · Thomas Hellström
Quoted 2 lines · Matthew Brost

From: Danilo Krummrich <[email protected]>

Quoted 8 lines
Quoted 3 lines

Split up the MA_STATE() macro such that components using the maple tree can easily inherit from struct ma_state and build custom tree walk macros to hide their internals from users.

I might misunderstand the patch, but isn't the real purpose to provide an MA_STATE initializer,and the way to achieve that is to split up the MA_STATE macro?

This patch isn't mine and on dri-devel. Can you put your comments there?
https://patchwork.freedesktop.org/patch/530673/?series=112994&rev=4

Matt

Quoted 70 lines
Quoted 36 lines

Example:

struct sample_iterator {
         struct ma_state mas;
         struct sample_mgr *mgr;
};
\#define SAMPLE_ITERATOR(name, __mgr, start)                    \
         struct sample_iterator name = {                         \
                 .mas = MA_STATE_INIT(&(__mgr)->mt, start, 0),   \
                 .mgr = __mgr,                                   \
         }
\#define sample_iter_for_each_range(it__, entry__, end__) \
         mas_for_each(&(it__).mas, entry__, end__)

--

struct sample *sample;
SAMPLE_ITERATOR(si, min);

sample_iter_for_each_range(&si, sample, max) {
         frob(mgr, sample);
}
---
  include/linux/maple_tree.h | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
include/linux/maple_tree.h +0 -0
index 1fadb5f5978b..87d55334f1c2 100644

include/linux/maple_tree.h struct ma_wr_state { · line 426

Quoted 6 lines
include/linux/maple_tree.h +2 -2
@@ -423,8 +423,8 @@ struct ma_wr_state {
  #define MA_ERROR(err) \
  		((struct maple_enode *)(((unsigned long)err << 2) | 2UL))
-#define MA_STATE(name, mt, first, end)					\
-	struct ma_state name = {					\
+#define MA_STATE_INIT(mt, first, end)					\
+	{								\

Naming: following the convention in, for example, the mutex and ww mutex code this should've been called

__MA_STATE_INITIALIZER(),

whereas the decapitalized name ma_state_init() would've been a (possibly inline) init function if it existed.

But this all should ofc be run by the maple tree maintainer(s).

FWIW, with these things addressed the change LGTM.

/Thomas

Quoted 12 lines
include/linux/maple_tree.h +3 -0
  		.tree = mt,						\
  		.index = first,						\
  		.last = end,						\
@@ -435,6 +435,9 @@ struct ma_wr_state {
  		.mas_flags = 0,						\
  	}
+#define MA_STATE(name, mt, first, end)					\
+	struct ma_state name = MA_STATE_INIT(mt, first, end)
+
  #define MA_WR_STATE(name, ma_state, wr_entry)				\
  	struct ma_wr_state name = {					\
  		.mas = ma_state,					\
author maintainer
#67 lore raw

PATCH v2 14/31 maple_tree: Export mas_preallocate

In reply to Matthew Brost

The DRM GPUVA implementation needs this function.

---
 lib/maple_tree.c | 1 +
 1 file changed, 1 insertion(+)
lib/maple_tree.c +1 -0
index 9e2735cbc2b4..ae37a167e25d 100644
@@ -5726,6 +5726,7 @@ int mas_preallocate(struct ma_state *mas, gfp_t gfp)
 	mas_reset(mas);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(mas_preallocate);
 
 /*
  * mas_destroy() - destroy a maple state.
Signature · 1 line
2.34.1
#68 lore raw

Re: [Intel-xe] [PATCH v2 14/31] maple_tree: Export mas_preallocate

Quoted 1 line · Matthew Brost

The DRM GPUVA implementation needs this function.

A more thorough explanation as to why  it's needed would help convince maple tree maintainers an export is needed.

Otherwise the change itself LGTM.

/Thomas

Quoted 18 lines
---
  lib/maple_tree.c | 1 +
  1 file changed, 1 insertion(+)
lib/maple_tree.c +1 -0
index 9e2735cbc2b4..ae37a167e25d 100644
@@ -5726,6 +5726,7 @@ int mas_preallocate(struct ma_state *mas, gfp_t gfp)
  	mas_reset(mas);
  	return ret;
  }
+EXPORT_SYMBOL_GPL(mas_preallocate);
 
  /*
   * mas_destroy() - destroy a maple state.
author maintainer
#69 lore raw

Re: [Intel-xe] [PATCH v2 14/31] maple_tree: Export mas_preallocate

Quoted 11 lines · Thomas Hellström
Quoted 1 line · Matthew Brost

The DRM GPUVA implementation needs this function.

A more thorough explanation as to why  it's needed would help convince maple tree maintainers an export is needed.

Otherwise the change itself LGTM.

/Thomas

I think version of this is in Linus tree. I think just get this after a rebase.

Matt

Quoted 18 lines
Quoted 18 lines
---
  lib/maple_tree.c | 1 +
  1 file changed, 1 insertion(+)
lib/maple_tree.c +1 -0
index 9e2735cbc2b4..ae37a167e25d 100644
@@ -5726,6 +5726,7 @@ int mas_preallocate(struct ma_state *mas, gfp_t gfp)
  	mas_reset(mas);
  	return ret;
  }
+EXPORT_SYMBOL_GPL(mas_preallocate);
  /*
   * mas_destroy() - destroy a maple state.
author maintainer
#70 lore raw

PATCH v2 15/31 drm: manager to keep track of GPUs VA mappings

In reply to Matthew Brost

From: Danilo Krummrich <[email protected]>

Add infrastructure to keep track of GPU virtual address (VA) mappings with a decicated VA space manager implementation.

New UAPIs, motivated by Vulkan sparse memory bindings graphics drivers start implementing, allow userspace applications to request multiple and arbitrary GPU VA mappings of buffer objects. The DRM GPU VA manager is intended to serve the following purposes in this context.

1) Provide infrastructure to track GPU VA allocations and mappings, making use of the maple_tree.

2) Generically connect GPU VA mappings to their backing buffers, in particular DRM GEM objects.

3) Provide a common implementation to perform more complex mapping operations on the GPU VA space. In particular splitting and merging of GPU VA mappings, e.g. for intersecting mapping requests or partial unmap requests.

---
 Documentation/gpu/drm-mm.rst    |   31 +
 drivers/gpu/drm/Makefile        |    1 +
 drivers/gpu/drm/drm_debugfs.c   |   41 +
 drivers/gpu/drm/drm_gem.c       |    3 +
 drivers/gpu/drm/drm_gpuva_mgr.c | 1686 +++++++++++++++++++++++++++++++
 include/drm/drm_debugfs.h       |   24 +
 include/drm/drm_drv.h           |    7 +
 include/drm/drm_gem.h           |   75 ++
 include/drm/drm_gpuva_mgr.h     |  681 +++++++++++++
 9 files changed, 2549 insertions(+)
 create mode 100644 drivers/gpu/drm/drm_gpuva_mgr.c
 create mode 100644 include/drm/drm_gpuva_mgr.h
Documentation/gpu/drm-mm.rst +31 -0
index a79fd3549ff8..fe40ee686f6e 100644
@@ -466,6 +466,37 @@ DRM MM Range Allocator Function References
 .. kernel-doc:: drivers/gpu/drm/drm_mm.c
    :export:
 
+DRM GPU VA Manager
+==================
+
+Overview
+--------
+
+.. kernel-doc:: drivers/gpu/drm/drm_gpuva_mgr.c
+   :doc: Overview
+
+Split and Merge
+---------------
+
+.. kernel-doc:: drivers/gpu/drm/drm_gpuva_mgr.c
+   :doc: Split and Merge
+
+Locking
+-------
+
+.. kernel-doc:: drivers/gpu/drm/drm_gpuva_mgr.c
+   :doc: Locking
+
+
+DRM GPU VA Manager Function References
+--------------------------------------
+
+.. kernel-doc:: include/drm/drm_gpuva_mgr.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_gpuva_mgr.c
+   :export:
+
 DRM Buddy Allocator
 ===================
 
drivers/gpu/drm/Makefile +1 -0
index 66dd2c48944a..ad6267273503 100644
@@ -46,6 +46,7 @@ drm-y := \
 	drm_vblank.o \
 	drm_vblank_work.o \
 	drm_vma_manager.o \
+	drm_gpuva_mgr.o \
 	drm_writeback.o
 drm-$(CONFIG_DRM_LEGACY) += \
 	drm_agpsupport.o \
drivers/gpu/drm/drm_debugfs.c +41 -0

53 lines not on this page · raw message

drivers/gpu/drm/drm_gem.c +3 -0
index a6208e2c089b..15fe61856190 100644
@@ -164,6 +164,9 @@ void drm_gem_private_object_init(struct drm_device *dev,
 	if (!obj->resv)
 		obj->resv = &obj->_resv;
 
+	if (drm_core_check_feature(dev, DRIVER_GEM_GPUVA))
+		drm_gem_gpuva_init(obj);
+
 	drm_vma_node_reset(&obj->vma_node);
 	INIT_LIST_HEAD(&obj->lru_node);
 }
drivers/gpu/drm/drm_gpuva_mgr.c new file +1686 -0

1,686 lines not on this page · raw message

include/drm/drm_debugfs.h +24 -0
index 7616f457ce70..3031fcb96b39 100644
@@ -34,6 +34,22 @@
 
 #include <linux/types.h>
 #include <linux/seq_file.h>
+
+#include <drm/drm_gpuva_mgr.h>
+
+/**
+ * DRM_DEBUGFS_GPUVA_INFO - &drm_info_list entry to dump a GPU VA space
+ * @show: the &drm_info_list's show callback
+ * @data: driver private data
+ *
+ * Drivers should use this macro to define a &drm_info_list entry to provide a
+ * debugfs file for dumping the GPU VA space regions and mappings.
+ *
+ * For each DRM GPU VA space drivers should call drm_debugfs_gpuva_info() from
+ * their @show callback.
+ */
+#define DRM_DEBUGFS_GPUVA_INFO(show, data) {"gpuvas", show, DRIVER_GEM_GPUVA, data}
+
 /**
  * struct drm_info_list - debugfs info list entry
  *
@@ -134,6 +150,8 @@ void drm_debugfs_add_file(struct drm_device *dev, const char *name,
 
 void drm_debugfs_add_files(struct drm_device *dev,
 			   const struct drm_debugfs_info *files, int count);
+int drm_debugfs_gpuva_info(struct seq_file *m,
+			   struct drm_gpuva_manager *mgr);
 #else
 static inline void drm_debugfs_create_files(const struct drm_info_list *files,
 					    int count, struct dentry *root,
@@ -155,6 +173,12 @@ static inline void drm_debugfs_add_files(struct drm_device *dev,
 					 const struct drm_debugfs_info *files,
 					 int count)
 {}
+
+static inline int drm_debugfs_gpuva_info(struct seq_file *m,
+					 struct drm_gpuva_manager *mgr)
+{
+	return 0;
+}
 #endif
 
 #endif /* _DRM_DEBUGFS_H_ */
include/drm/drm_drv.h +7 -0
index b419c59c4bef..9e2ec2da0685 100644
@@ -105,6 +105,13 @@ enum drm_driver_feature {
 	 */
 	DRIVER_COMPUTE_ACCEL            = BIT(7),
 
+	/**
+	 * @DRIVER_GEM_GPUVA:
+	 *
+	 * Driver supports user defined GPU VA bindings for GEM objects.
+	 */
+	DRIVER_GEM_GPUVA		= BIT(8),
+
 	/* IMPORTANT: Below are all the legacy flags, add new ones above. */
 
 	/**
include/drm/drm_gem.h +75 -0

91 lines not on this page · raw message

include/drm/drm_gpuva_mgr.h new file +681 -0

681 lines not on this page · raw message

Signature · 1 line
2.34.1
#71 lore raw

Re: [Intel-xe] [PATCH v2 15/31] drm: manager to keep track of GPUs VA mappings

Quoted 16 lines · Matthew Brost

From: Danilo Krummrich <[email protected]>

Add infrastructure to keep track of GPU virtual address (VA) mappings with a decicated VA space manager implementation.

New UAPIs, motivated by Vulkan sparse memory bindings graphics drivers start implementing, allow userspace applications to request multiple and arbitrary GPU VA mappings of buffer objects. The DRM GPU VA manager is intended to serve the following purposes in this context.

1) Provide infrastructure to track GPU VA allocations and mappings, making use of the maple_tree.

2) Generically connect GPU VA mappings to their backing buffers, in particular DRM GEM objects.

Quoted 8 lines

3) Provide a common implementation to perform more complex mapping operations on the GPU VA space. In particular splitting and merging of GPU VA mappings, e.g. for intersecting mapping requests or partial unmap requests.

Danilo, Matthew

Before embarking on a full review of this (saving this for last) I heard there might be plans to add userptr support, rebind_work interaction and such and resolve any driver differences using vfuncs.

Just wanted to raise a warning that helpers that attempt to "do it all" and depend on vfuncs are easy traps to start creating middle layers (like TTM) which are typically frowned upon. (See for example the discussion on the partly rejected patch series on the TTM shrinker).

So just as a recommendation to avoid redoing a lot of stuff, please be careful with additional helpers that require vfuncs and check if they can be implemented in another way by rethinking the layering.

Thanks,

Thomas

author maintainer
#72 lore raw

Re: [Intel-xe] [PATCH v2 15/31] drm: manager to keep track of GPUs VA mappings

Quoted 33 lines · Thomas Hellström
Quoted 16 lines · Matthew Brost

From: Danilo Krummrich <[email protected]>

Add infrastructure to keep track of GPU virtual address (VA) mappings with a decicated VA space manager implementation.

New UAPIs, motivated by Vulkan sparse memory bindings graphics drivers start implementing, allow userspace applications to request multiple and arbitrary GPU VA mappings of buffer objects. The DRM GPU VA manager is intended to serve the following purposes in this context.

1) Provide infrastructure to track GPU VA allocations and mappings, making use of the maple_tree.

2) Generically connect GPU VA mappings to their backing buffers, in particular DRM GEM objects.

Quoted 8 lines

3) Provide a common implementation to perform more complex mapping operations on the GPU VA space. In particular splitting and merging of GPU VA mappings, e.g. for intersecting mapping requests or partial unmap requests.

Danilo, Matthew

Before embarking on a full review of this (saving this for last) I heard there might be plans to add userptr support, rebind_work interaction and such and resolve any driver differences using vfuncs.

Quoted 9 lines

Just wanted to raise a warning that helpers that attempt to "do it all" and depend on vfuncs are easy traps to start creating middle layers (like TTM) which are typically frowned upon. (See for example the discussion on the partly rejected patch series on the TTM shrinker).

So just as a recommendation to avoid redoing a lot of stuff, please be careful with additional helpers that require vfuncs and check if they can be implemented in another way by rethinking the layering.

Noted. This series goes as far as supporting userptr as a GPUVA in a standard way, an extobj list per manager (VM in Xe), plus common ways to lock a manager. Agree much more than would be difficult to do in a standard way.

Matt

Quoted 5 lines

Thanks,

Thomas

author maintainer
#73 lore raw

PATCH v2 16/31 drm/xe: Port Xe to GPUVA

In reply to Matthew Brost

Rather than open coding VM binds and VMA tracking, use the GPUVA library. GPUVA provides a common infrastructure for VM binds to use mmap / munmap semantics and support for VK sparse bindings.

The concepts are:

1) xe_vm inherits from drm_gpuva_manager
2) xe_vma inherits from drm_gpuva
3) xe_vma_op inherits from drm_gpuva_op
4) VM bind operations (MAP, UNMAP, PREFETCH, UNMAP_ALL) call into the GPUVA code to generate an VMA operations list which is parsed, commited, and executed.

v2 (CI): Add break after default in case statement.
v3: Rebase
v4: Fix some error handling

---
 drivers/gpu/drm/xe/xe_bo.c                  |   10 +-
 drivers/gpu/drm/xe/xe_device.c              |    2 +-
 drivers/gpu/drm/xe/xe_exec.c                |    2 +-
 drivers/gpu/drm/xe/xe_gt_pagefault.c        |   23 +-
 drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c |   14 +-
 drivers/gpu/drm/xe/xe_migrate.c             |    8 +-
 drivers/gpu/drm/xe/xe_pt.c                  |  106 +-
 drivers/gpu/drm/xe/xe_trace.h               |   10 +-
 drivers/gpu/drm/xe/xe_vm.c                  | 1849 +++++++++----------
 drivers/gpu/drm/xe/xe_vm.h                  |   66 +-
 drivers/gpu/drm/xe/xe_vm_madvise.c          |   88 +-
 drivers/gpu/drm/xe/xe_vm_types.h            |  167 +-
 12 files changed, 1171 insertions(+), 1174 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +5 -5
index da99ee53e7d7..a475d0584916 100644
@@ -391,7 +391,8 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 {
 	struct dma_resv_iter cursor;
 	struct dma_fence *fence;
-	struct xe_vma *vma;
+	struct drm_gpuva *gpuva;
+	struct drm_gem_object *obj = &bo->ttm.base;
 	int ret = 0;
 
 	dma_resv_assert_held(bo->ttm.base.resv);
@@ -404,8 +405,9 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 		dma_resv_iter_end(&cursor);
 	}
 
-	list_for_each_entry(vma, &bo->vmas, bo_link) {
-		struct xe_vm *vm = vma->vm;
+	drm_gem_for_each_gpuva(gpuva, obj) {
+		struct xe_vma *vma = gpuva_to_vma(gpuva);
+		struct xe_vm *vm = xe_vma_vm(vma);
 
 		trace_xe_vma_evict(vma);
 
@@ -430,10 +432,8 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 			} else {
 				ret = timeout;
 			}
-
 		} else {
 			bool vm_resv_locked = false;
-			struct xe_vm *vm = vma->vm;
 
 			/*
 			 * We need to put the vma on the vm's rebind_list,
drivers/gpu/drm/xe/xe_device.c +1 -1
index 45d6e5ff47fd..ab2ecd208f97 100644
@@ -131,7 +131,7 @@ static struct drm_driver driver = {
 	.driver_features =
 	    DRIVER_GEM |
 	    DRIVER_RENDER | DRIVER_SYNCOBJ |
-	    DRIVER_SYNCOBJ_TIMELINE,
+	    DRIVER_SYNCOBJ_TIMELINE | DRIVER_GEM_GPUVA,
 	.open = xe_file_open,
 	.postclose = xe_file_close,
 
drivers/gpu/drm/xe/xe_exec.c +1 -1
index 21a9c2fddf86..90c46d092737 100644
@@ -119,7 +119,7 @@ static int xe_exec_begin(struct xe_engine *e, struct ww_acquire_ctx *ww,
 		if (xe_vma_is_userptr(vma))
 			continue;
 
-		err = xe_bo_validate(vma->bo, vm, false);
+		err = xe_bo_validate(xe_vma_bo(vma), vm, false);
 		if (err) {
 			xe_vm_unlock_dma_resv(vm, tv_onstack, *tv, ww, objs);
 			*tv = NULL;
drivers/gpu/drm/xe/xe_gt_pagefault.c +9 -14
index 1677640e1075..f7a066090a13 100644
@@ -75,9 +75,10 @@ static bool vma_is_valid(struct xe_gt *gt, struct xe_vma *vma)
 		!(BIT(gt->info.id) & vma->usm.gt_invalidated);
 }
 
-static bool vma_matches(struct xe_vma *vma, struct xe_vma *lookup)
+static bool vma_matches(struct xe_vma *vma, u64 page_addr)
 {
-	if (lookup->start > vma->end || lookup->end < vma->start)
+	if (page_addr > xe_vma_end(vma) - 1 ||
+	    page_addr + SZ_4K < xe_vma_start(vma))
 		return false;
 
 	return true;
@@ -90,16 +91,14 @@ static bool only_needs_bo_lock(struct xe_bo *bo)
 
 static struct xe_vma *lookup_vma(struct xe_vm *vm, u64 page_addr)
 {
-	struct xe_vma *vma = NULL, lookup;
+	struct xe_vma *vma = NULL;
 
-	lookup.start = page_addr;
-	lookup.end = lookup.start + SZ_4K - 1;
 	if (vm->usm.last_fault_vma) {   /* Fast lookup */
-		if (vma_matches(vm->usm.last_fault_vma, &lookup))
+		if (vma_matches(vm->usm.last_fault_vma, page_addr))
 			vma = vm->usm.last_fault_vma;
 	}
 	if (!vma)
-		vma = xe_vm_find_overlapping_vma(vm, &lookup);
+		vma = xe_vm_find_overlapping_vma(vm, page_addr, SZ_4K);
 
 	return vma;
 }
@@ -170,7 +169,7 @@ static int handle_pagefault(struct xe_gt *gt, struct pagefault *pf)
 	}
 
 	/* Lock VM and BOs dma-resv */
-	bo = vma->bo;
+	bo = xe_vma_bo(vma);
 	if (only_needs_bo_lock(bo)) {
 		/* This path ensures the BO's LRU is updated */
 		ret = xe_bo_lock(bo, &ww, xe->info.tile_count, false);
@@ -487,12 +486,8 @@ static struct xe_vma *get_acc_vma(struct xe_vm *vm, struct acc *acc)
 {
 	u64 page_va = acc->va_range_base + (ffs(acc->sub_granularity) - 1) *
 		sub_granularity_in_byte(acc->granularity);
-	struct xe_vma lookup;
-
-	lookup.start = page_va;
-	lookup.end = lookup.start + SZ_4K - 1;
 
-	return xe_vm_find_overlapping_vma(vm, &lookup);
+	return xe_vm_find_overlapping_vma(vm, page_va, SZ_4K);
 }
 
 static int handle_acc(struct xe_gt *gt, struct acc *acc)
@@ -536,7 +531,7 @@ static int handle_acc(struct xe_gt *gt, struct acc *acc)
 		goto unlock_vm;
 
 	/* Lock VM and BOs dma-resv */
-	bo = vma->bo;
+	bo = xe_vma_bo(vma);
 	if (only_needs_bo_lock(bo)) {
 		/* This path ensures the BO's LRU is updated */
 		ret = xe_bo_lock(bo, &ww, xe->info.tile_count, false);
drivers/gpu/drm/xe/xe_gt_tlb_invalidation.c +7 -7
index 604f189dbd70..66fd67ffa8a9 100644
@@ -201,8 +201,8 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 	if (!xe->info.has_range_tlb_invalidation) {
 		action[len++] = MAKE_INVAL_OP(XE_GUC_TLB_INVAL_FULL);
 	} else {
-		u64 start = vma->start;
-		u64 length = vma->end - vma->start + 1;
+		u64 start = xe_vma_start(vma);
+		u64 length = xe_vma_size(vma);
 		u64 align, end;
 
 		if (length < SZ_4K)
@@ -215,12 +215,12 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		 * address mask covering the required range.
 		 */
 		align = roundup_pow_of_two(length);
-		start = ALIGN_DOWN(vma->start, align);
-		end = ALIGN(vma->start + length, align);
+		start = ALIGN_DOWN(xe_vma_start(vma), align);
+		end = ALIGN(xe_vma_start(vma) + length, align);
 		length = align;
 		while (start + length < end) {
 			length <<= 1;
-			start = ALIGN_DOWN(vma->start, length);
+			start = ALIGN_DOWN(xe_vma_start(vma), length);
 		}
 
 		/*
@@ -229,7 +229,7 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		 */
 		if (length >= SZ_2M) {
 			length = max_t(u64, SZ_16M, length);
-			start = ALIGN_DOWN(vma->start, length);
+			start = ALIGN_DOWN(xe_vma_start(vma), length);
 		}
 
 		XE_BUG_ON(length < SZ_4K);
@@ -238,7 +238,7 @@ int xe_gt_tlb_invalidation_vma(struct xe_gt *gt,
 		XE_BUG_ON(!IS_ALIGNED(start, length));
 
 		action[len++] = MAKE_INVAL_OP(XE_GUC_TLB_INVAL_PAGE_SELECTIVE);
-		action[len++] = vma->vm->usm.asid;
+		action[len++] = xe_vma_vm(vma)->usm.asid;
 		action[len++] = lower_32_bits(start);
 		action[len++] = upper_32_bits(start);
 		action[len++] = ilog2(length) - ilog2(SZ_4K);
drivers/gpu/drm/xe/xe_migrate.c +6 -2
index f40f47ccb76f..b44aa094a466 100644
@@ -1050,8 +1050,10 @@ xe_migrate_update_pgtables_cpu(struct xe_migrate *m,
 		return ERR_PTR(-ETIME);
 
 	if (wait_vm && !dma_resv_test_signaled(&vm->resv,
-					       DMA_RESV_USAGE_BOOKKEEP))
+					       DMA_RESV_USAGE_BOOKKEEP)) {
+		vm_dbg(&vm->xe->drm, "wait on VM for munmap");
 		return ERR_PTR(-ETIME);
+	}
 
 	if (ops->pre_commit) {
 		err = ops->pre_commit(pt_update);
@@ -1139,7 +1141,8 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	u64 addr;
 	int err = 0;
 	bool usm = !eng && xe->info.supports_usm;
-	bool first_munmap_rebind = vma && vma->first_munmap_rebind;
+	bool first_munmap_rebind = vma &&
+		vma->gpuva.flags & XE_VMA_FIRST_REBIND;
 
 	/* Use the CPU if no in syncs and engine is idle */
 	if (no_in_syncs(syncs, num_syncs) && (!eng || xe_engine_is_idle(eng))) {
@@ -1260,6 +1263,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	 * trigger preempts before moving forward
 	 */
 	if (first_munmap_rebind) {
+		vm_dbg(&vm->xe->drm, "wait on first_munmap_rebind");
 		err = job_add_deps(job, &vm->resv,
 				   DMA_RESV_USAGE_BOOKKEEP);
 		if (err)
drivers/gpu/drm/xe/xe_pt.c +55 -51

288 lines not on this page · raw message

drivers/gpu/drm/xe/xe_trace.h +5 -5
index 38e9d7c6197b..5c7515296345 100644
@@ -18,7 +18,7 @@
 #include "xe_gt_types.h"
 #include "xe_guc_engine_types.h"
 #include "xe_sched_job.h"
-#include "xe_vm_types.h"
+#include "xe_vm.h"
 
 DECLARE_EVENT_CLASS(xe_gt_tlb_invalidation_fence,
 		    TP_PROTO(struct xe_gt_tlb_invalidation_fence *fence),
@@ -378,10 +378,10 @@ DECLARE_EVENT_CLASS(xe_vma,
 
 		    TP_fast_assign(
 			   __entry->vma = (unsigned long)vma;
-			   __entry->asid = vma->vm->usm.asid;
-			   __entry->start = vma->start;
-			   __entry->end = vma->end;
-			   __entry->ptr = (u64)vma->userptr.ptr;
+			   __entry->asid = xe_vma_vm(vma)->usm.asid;
+			   __entry->start = xe_vma_start(vma);
+			   __entry->end = xe_vma_end(vma) - 1;
+			   __entry->ptr = xe_vma_userptr(vma);
 			   ),
 
 		    TP_printk("vma=0x%016llx, asid=0x%05x, start=0x%012llx, end=0x%012llx, ptr=0x%012llx,",
drivers/gpu/drm/xe/xe_vm.c +885 -964

2,474 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.h +63 -3

42 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm_madvise.c +21 -67

88 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm_types.h +113 -54

167 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

#74 lore raw

Re: [Intel-xe] [PATCH v2 16/31] drm/xe: Port Xe to GPUVA

Hi, Matthew,

Quoted 13 lines · Matthew Brost

Rather than open coding VM binds and VMA tracking, use the GPUVA library. GPUVA provides a common infrastructure for VM binds to use mmap / munmap semantics and support for VK sparse bindings.

The concepts are:

1) xe_vm inherits from drm_gpuva_manager
2) xe_vma inherits from drm_gpuva
3) xe_vma_op inherits from drm_gpuva_op
4) VM bind operations (MAP, UNMAP, PREFETCH, UNMAP_ALL) call into the GPUVA code to generate an VMA operations list which is parsed, commited, and executed.

Quoted 5 lines

v2 (CI): Add break after default in case statement.
v3: Rebase
v4: Fix some error handling

Before embarking on a second review of this code it would really be beneficial if you could address some comments from the first review. In particular splitting this huge patch up if possible (and I also think that removing the async worker *before* this patch if at all possible would really ease the review both for me and potential upcoming reviewers).

Thanks,

Thomas

author maintainer
#75 lore raw

Re: [Intel-xe] [PATCH v2 16/31] drm/xe: Port Xe to GPUVA

Quoted 20 lines · Thomas Hellström

Hi, Matthew,

Quoted 17 lines · Matthew Brost

Rather than open coding VM binds and VMA tracking, use the GPUVA library. GPUVA provides a common infrastructure for VM binds to use mmap / munmap semantics and support for VK sparse bindings.

The concepts are:

1) xe_vm inherits from drm_gpuva_manager
2) xe_vma inherits from drm_gpuva
3) xe_vma_op inherits from drm_gpuva_op
4) VM bind operations (MAP, UNMAP, PREFETCH, UNMAP_ALL) call into the GPUVA code to generate an VMA operations list which is parsed, commited, and executed.

v2 (CI): Add break after default in case statement.
v3: Rebase
v4: Fix some error handling

Quoted 8 lines
Quoted 1 line

Before embarking on a second review of this code it would really be beneficial if you could address some comments from the first review. In particular splitting this huge patch up if possible (and I also think that removing the async worker *before* this patch if at all possible would really ease the review both for me and potential upcoming reviewers).

My bad that I missed your comments on the list, yes I will address your comments in the respin, expect it by Mondayish.

Removing the async worker first doesn't make a ton of sense as GPUVA makes the error handling a lot easier plus that basically means a complete rewrite.

Matt

Quoted 5 lines

Thanks,

Thomas

#76 lore raw

Re: [Intel-xe] [PATCH v2 16/31] drm/xe: Port Xe to GPUVA

Quoted 22 lines · Matthew Brost
Quoted 21 lines · Thomas Hellström

Hi, Matthew,

Quoted 18 lines · Matthew Brost

Rather than open coding VM binds and VMA tracking, use the GPUVA library. GPUVA provides a common infrastructure for VM binds to use mmap / munmap semantics and support for VK sparse bindings.

The concepts are:

1) xe_vm inherits from drm_gpuva_manager
2) xe_vma inherits from drm_gpuva
3) xe_vma_op inherits from drm_gpuva_op
4) VM bind operations (MAP, UNMAP, PREFETCH, UNMAP_ALL) call into the GPUVA code to generate an VMA operations list which is parsed, commited, and executed.

v2 (CI): Add break after default in case statement.
v3: Rebase
v4: Fix some error handling

Quoted 8 lines
Quoted 6 lines

Before embarking on a second review of this code it would really be beneficial if you could address some comments from the first review. In particular splitting this huge patch up if possible (and I also think that removing the async worker *before* this patch if at all possible would really ease the review both for me and potential upcoming reviewers).

My bad that I missed your comments on the list, yes I will address your comments in the respin, expect it by Mondayish.

NP, basically this was a comment saying I'd rather wait for a respin before tackling this again :).

Quoted 4 lines

Removing the async worker first doesn't make a ton of sense as GPUVA makes the error handling a lot easier plus that basically means a complete rewrite.

OK, yes I guess from a reviewer's point of view the other way around would be easier, but if you keep the ordering please split into separate patch series for GPUVA and async removal, because invasive changes in code that is later removed in the same series is typically not well received.

Thanks,

Thomas

Quoted 8 lines

Matt

Quoted 5 lines

Thanks,

Thomas

author maintainer
#77 lore raw

PATCH v2 17/31 drm/xe: NULL binding implementation

In reply to Matthew Brost

Add uAPI and implementation for NULL bindings. A NULL binding is defined as writes dropped and read zero. A single bit in the uAPI has been added which results in a single bit in the PTEs being set.

NULL bindings are indended to be used to implement VK sparse bindings.

---
 drivers/gpu/drm/xe/xe_bo.h           |  1 +
 drivers/gpu/drm/xe/xe_exec.c         |  2 +
 drivers/gpu/drm/xe/xe_gt_pagefault.c |  4 +-
 drivers/gpu/drm/xe/xe_pt.c           | 77 ++++++++++++++++-------
 drivers/gpu/drm/xe/xe_vm.c           | 92 ++++++++++++++++++----------
 drivers/gpu/drm/xe/xe_vm.h           | 10 +++
 drivers/gpu/drm/xe/xe_vm_madvise.c   |  2 +-
 drivers/gpu/drm/xe/xe_vm_types.h     |  3 +
 include/uapi/drm/xe_drm.h            |  8 +++
 9 files changed, 144 insertions(+), 55 deletions(-)
drivers/gpu/drm/xe/xe_bo.h +1 -0
index 25457b3c757b..81051f456874 100644
@@ -56,6 +56,7 @@
 #define XE_PDE_IPS_64K			BIT_ULL(11)
 
 #define XE_GGTT_PTE_LM			BIT_ULL(1)
+#define XE_PTE_NULL			BIT_ULL(9)
 #define XE_USM_PPGTT_PTE_AE		BIT_ULL(10)
 #define XE_PPGTT_PTE_LM			BIT_ULL(11)
 #define XE_PDE_64K			BIT_ULL(6)
drivers/gpu/drm/xe/xe_exec.c +2 -0
index 90c46d092737..68f876afd13c 100644
@@ -116,6 +116,8 @@ static int xe_exec_begin(struct xe_engine *e, struct ww_acquire_ctx *ww,
 	 * to a location where the GPU can access it).
 	 */
 	list_for_each_entry(vma, &vm->rebind_list, rebind_link) {
+		XE_BUG_ON(xe_vma_is_null(vma));
+
 		if (xe_vma_is_userptr(vma))
 			continue;
 
drivers/gpu/drm/xe/xe_gt_pagefault.c +2 -2
index f7a066090a13..cfffe3398fe4 100644
@@ -526,8 +526,8 @@ static int handle_acc(struct xe_gt *gt, struct acc *acc)
 
 	trace_xe_vma_acc(vma);
 
-	/* Userptr can't be migrated, nothing to do */
-	if (xe_vma_is_userptr(vma))
+	/* Userptr or null can't be migrated, nothing to do */
+	if (xe_vma_has_no_bo(vma))
 		goto unlock_vm;
 
 	/* Lock VM and BOs dma-resv */
drivers/gpu/drm/xe/xe_pt.c +56 -21

157 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.c +61 -31

241 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm.h +10 -0
index 21b1054949c4..96e2c6b07bf8 100644
@@ -175,7 +175,17 @@ static inline void xe_vm_reactivate_rebind(struct xe_vm *vm)
 	}
 }
 
+static inline bool xe_vma_is_null(struct xe_vma *vma)
+{
+	return vma->gpuva.flags & XE_VMA_NULL;
+}
+
 static inline bool xe_vma_is_userptr(struct xe_vma *vma)
+{
+	return !xe_vma_bo(vma) && !xe_vma_is_null(vma);
+}
+
+static inline bool xe_vma_has_no_bo(struct xe_vma *vma)
 {
 	return !xe_vma_bo(vma);
 }
drivers/gpu/drm/xe/xe_vm_madvise.c +1 -1
index 02d27a354b36..03508645fa08 100644
@@ -227,7 +227,7 @@ get_vmas(struct xe_vm *vm, int *num_vmas, u64 addr, u64 range)
 	drm_gpuva_iter_for_each_range(gpuva, it, addr + range) {
 		struct xe_vma *vma = gpuva_to_vma(gpuva);
 
-		if (xe_vma_is_userptr(vma))
+		if (xe_vma_has_no_bo(vma))
 			continue;
 
 		if (*num_vmas == max_vmas) {
drivers/gpu/drm/xe/xe_vm_types.h +3 -0
index 243dc91a61b0..b61007b70502 100644
@@ -29,6 +29,7 @@ struct xe_vm;
 #define XE_VMA_ATOMIC_PTE_BIT	(DRM_GPUVA_USERBITS << 2)
 #define XE_VMA_FIRST_REBIND	(DRM_GPUVA_USERBITS << 3)
 #define XE_VMA_LAST_REBIND	(DRM_GPUVA_USERBITS << 4)
+#define XE_VMA_NULL		(DRM_GPUVA_USERBITS << 5)
 
 struct xe_vma {
 	/** @gpuva: Base GPUVA object */
@@ -315,6 +316,8 @@ struct xe_vma_op_map {
 	bool immediate;
 	/** @read_only: Read only */
 	bool read_only;
+	/** @null: NULL (writes dropped, read zero) */
+	bool null;
 };
 
 /** struct xe_vma_op_unmap - VMA unmap operation */
include/uapi/drm/xe_drm.h +8 -0
index b0b80aae3ee8..27c51946fadd 100644
@@ -447,6 +447,14 @@ struct drm_xe_vm_bind_op {
 	 * than differing the MAP to the page fault handler.
 	 */
 #define XE_VM_BIND_FLAG_IMMEDIATE	(0x1 << 18)
+	/*
+	 * When the NULL flag is set, the page tables are setup with a special
+	 * bit which indicates writes are dropped and all reads return zero. The
+	 * NULL flags is only valid for XE_VM_BIND_OP_MAP operations, the BO
+	 * handle MBZ, and the BO offset MBZ. This flag is intended to implement
+	 * VK sparse bindings.
+	 */
+#define XE_VM_BIND_FLAG_NULL		(0x1 << 19)
 
 	/** @reserved: Reserved */
 	__u64 reserved[2];
Signature · 1 line
2.34.1