Skip to contentSkip to conversation

drm/amdgpu/mes: refactor the amdgpu_mes_alloc/free_proc|gang()

PATCH Superseded Prike Liang 2 participants 7 messages Jul 30 – Aug 4

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 drm/amdgpu/mes: refactor the amdgpu_mes_alloc/free_proc|gang()

- Unify amdgpu_mes_alloc/free_proc|gang_ctx_index to provide centralized RS64mem bitmap management for both KGD and KFD.
- Retrieve the bitmap bit for userq contex index based on a per process granularity.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c    | 28 +++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h    |  8 +++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  |  6 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  3 ++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 20 ++++++++++------
 5 files changed, 39 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +14 -14
index 9af7c97af5be..e3862095ff99 100644
@@ -1040,13 +1040,13 @@ int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes)
  * amdgpu_mes_alloc_proc_ctx_index - allocate a process context slot
  *
  * @mes: MES instance
- * @queue: Usermode queue receiving the allocated process context index
+ * @index: the allocated process context index
  *
  * Returns 0 on success, -ENOSPC if all slots are used, or
  * -EOPNOTSUPP if RS64 local memory is unavailable.
  */
 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue)
+				    uint32_t *index)
 {
 	unsigned long bit;
 
@@ -1061,7 +1061,7 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
 		return -ENOSPC;
 	}
 	set_bit(bit, mes->proc_ctx_bitmap);
-	queue->proc_ctx_array_index = (uint32_t)bit;
+	*index = (uint32_t)bit;
 	amdgpu_mes_unlock(mes);
 
 	return 0;
@@ -1071,18 +1071,18 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_free_proc_ctx_index - free a process context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue whose process context index is released
+  * @index: process context index is released
   */
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue)
+				    uint32_t index)
 {
 	if (!mes->use_rs64mem || !mes->proc_ctx_bitmap)
 		return;
-	if (queue->proc_ctx_array_index >= mes->proc_ctx_array_size)
+	if (index >= mes->proc_ctx_array_size)
 		return;
 
 	amdgpu_mes_lock(mes);
-	clear_bit(queue->proc_ctx_array_index, mes->proc_ctx_bitmap);
+	clear_bit(index, mes->proc_ctx_bitmap);
 	amdgpu_mes_unlock(mes);
 }
 
@@ -1090,13 +1090,13 @@ void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_alloc_gang_ctx_index - allocate a gang context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue receiving the allocated gang context index
+  * @queue: the allocated gang context index
   *
   * Returns 0 on success, -ENOSPC if all slots are used, or
   * -EOPNOTSUPP if RS64 local memory is unavailable.
   */
 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue)
+				    uint32_t *index)
 {
 	unsigned long bit;
 
@@ -1111,7 +1111,7 @@ int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
 		return -ENOSPC;
 	}
 	set_bit(bit, mes->gang_ctx_bitmap);
-	queue->gang_ctx_array_index = bit;
+	*index = bit;
 	amdgpu_mes_unlock(mes);
 
 	return 0;
@@ -1121,18 +1121,18 @@ int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_free_gang_ctx_index - free a gang context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue whose gang context index is released
+  * @queue: gang context index is released
   */
 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue)
+				    uint32_t index)
 {
 	if (!mes->use_rs64mem || !mes->gang_ctx_bitmap)
 		return;
-	if (queue->gang_ctx_array_index >= mes->gang_ctx_array_size)
+	if (index >= mes->gang_ctx_array_size)
 		return;
 
 	amdgpu_mes_lock(mes);
-	clear_bit(queue->gang_ctx_array_index, mes->gang_ctx_bitmap);
+	clear_bit(index, mes->gang_ctx_bitmap);
 	amdgpu_mes_unlock(mes);
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +4 -4
index c67db2d6e122..977c057dcce8 100644
@@ -636,11 +636,11 @@ int amdgpu_mes_rs64mem_init(struct amdgpu_mes *mes);
 void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes);
 int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes);
 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue);
+				    uint32_t *index);
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue);
+				    uint32_t index);
 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue);
+				    uint32_t *index);
 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
-				    struct amdgpu_usermode_queue *queue);
+				    uint32_t index);
 #endif /* __AMDGPU_MES_H__ */
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
index a1f19888dc55..0e9657f2dbe7 100644
@@ -1249,6 +1249,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
 	xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC);
 	userq_mgr->adev = adev;
 	userq_mgr->file = file_priv;
+	userq_mgr->proc_ctx_allocated = false;
 	mutex_init(&userq_mgr->proc_ctx_lock);
 
 	INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
@@ -1277,6 +1278,7 @@ void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr)
 
 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 {
+	struct amdgpu_mes *mes = &userq_mgr->adev->mes;
 	struct amdgpu_usermode_queue *queue;
 	unsigned long queue_id = 0;
 
@@ -1303,6 +1305,10 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 	 */
 	cancel_work_sync(&userq_mgr->reset_work);
 
+	if (userq_mgr->proc_ctx_allocated) {
+		amdgpu_mes_free_proc_ctx_index(mes, userq_mgr->proc_ctx_array_index);
+		userq_mgr->proc_ctx_allocated = false;
+	}
 	amdgpu_bo_free_kernel(&userq_mgr->proc_ctx_obj.obj,
 			      &userq_mgr->proc_ctx_obj.gpu_addr,
 			      &userq_mgr->proc_ctx_obj.cpu_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -1
index 8333bab537d4..6412a7f7b6ef 100644
@@ -101,7 +101,6 @@ struct amdgpu_usermode_queue {
 		u64 va_array[6];
 	} userq_vas;
 
-	uint32_t	proc_ctx_array_index;
 	uint32_t	gang_ctx_array_index;
 };
 
@@ -133,6 +132,8 @@ struct amdgpu_userq_mgr {
 	struct mutex			proc_ctx_lock;
 	struct amdgpu_userq_obj		proc_ctx_obj;
 
+	bool				proc_ctx_allocated;
+	uint32_t			proc_ctx_array_index;
 	/**
 	 * @reset_work:
 	 *
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +13 -7
index a914198a4d62..1fd72f18e9a8 100644
@@ -143,10 +143,18 @@ static int mes_userq_map(struct amdgpu_usermode_queue *queue)
 	queue_input.doorbell_offset = userq_props->doorbell_index;
 	queue_input.page_table_base_addr = amdgpu_gmc_pd_addr(queue->vm->root.bo);
 	queue_input.wptr_mc_addr = queue->wptr_obj.gpu_addr;
+
 	if (mes->use_rs64mem) {
-		amdgpu_mes_alloc_proc_ctx_index(mes, queue);
-		queue_input.process_context_array_index = queue->proc_ctx_array_index;
-		amdgpu_mes_alloc_gang_ctx_index(mes, queue);
+		 if (!uq_mgr->proc_ctx_allocated) {
+			r = amdgpu_mes_alloc_proc_ctx_index(mes, &uq_mgr->proc_ctx_array_index);
+			if (r) {
+				DRM_ERROR("Failed to allocate userq process index err:%d\n", r);
+				return r;
+			}
+			uq_mgr->proc_ctx_allocated = true;
+		}
+		amdgpu_mes_alloc_gang_ctx_index(mes, &queue->gang_ctx_array_index);
+		queue_input.process_context_array_index = uq_mgr->proc_ctx_array_index;
 		queue_input.gang_context_array_index = queue->gang_ctx_array_index;
 	}
 	amdgpu_mes_lock(&adev->mes);
@@ -179,10 +187,8 @@ static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
 	amdgpu_mes_lock(&adev->mes);
 	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
 	amdgpu_mes_unlock(&adev->mes);
-	if (mes->use_rs64mem) {
-		amdgpu_mes_free_proc_ctx_index(mes, queue);
-		amdgpu_mes_free_gang_ctx_index(mes, queue);
-	}
+	if (mes->use_rs64mem)
+		amdgpu_mes_free_gang_ctx_index(mes, queue->gang_ctx_array_index);
 	if (r)
 		DRM_ERROR("Failed to unmap queue in HW, err (%d)\n", r);
 	return r;
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 2/2 drm/amdkfd: enable rs64mem for kfd queue

Enabled RS64mem for KFD queues by integrating
process and gang context index allocation in
the per KFD device process and queue creation.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h                  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c               |  5 ++++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 10 ++++++++++
 4 files changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3 -0
index 51ee9c39104b..ea9d87450eae 100644
@@ -226,8 +226,10 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 	/* MES unit for quantum is 100ns */
 	queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM;  /* Equivalent to 10ms. */
 	queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
+	queue_input.process_context_array_index = pdd->proc_ctx_array_index;
 	queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */
 	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+	queue_input.gang_context_array_index = q->gang_ctx_array_index;
 	queue_input.inprocess_gang_priority = q->properties.priority;
 	queue_input.gang_global_priority_level =
 					AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
@@ -303,6 +305,7 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
 	queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
 	queue_input.remove_queue_after_reset = flush_mes_queue;
 	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
+	queue_input.gang_context_array_index = q->gang_ctx_array_index;
 
 	amdgpu_mes_lock(&adev->mes);
 	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
index 88191a4c1657..3b89bcc625b3 100644
@@ -635,6 +635,7 @@ struct queue {
 	void *gang_ctx_bo;
 	uint64_t gang_ctx_gpu_addr;
 	void *gang_ctx_cpu_ptr;
+	uint32_t gang_ctx_array_index;
 
 	struct amdgpu_bo *wptr_bo_gart;
 };
@@ -871,6 +872,8 @@ struct kfd_process_device {
 	uint64_t proc_ctx_gpu_addr;
 	void *proc_ctx_cpu_ptr;
 
+	uint32_t proc_ctx_array_index;
+
 	/* Tracks queue reset status */
 	bool has_reset_queue;
 
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -1
index eb508fe3ded7..0a7c1900da95 100644
@@ -1215,9 +1215,12 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
 		kfd_free_process_doorbells(pdd->dev->kfd, pdd);
 
 		if (pdd->dev->kfd->shared_resources.enable_mes &&
-			pdd->proc_ctx_cpu_ptr)
+			pdd->proc_ctx_cpu_ptr) {
+			amdgpu_mes_free_proc_ctx_index(&pdd->dev->adev->mes,
+						       pdd->proc_ctx_array_index);
 			amdgpu_amdkfd_free_kernel_mem(pdd->dev->adev,
 						   &pdd->proc_ctx_bo);
+		}
 		/*
 		 * before destroying pdd, make sure to report availability
 		 * for auto suspend
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +10 -0
index 8054e4fe0381..72913b5c8338 100644
@@ -210,6 +210,7 @@ static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
 	}
 
 	if (dev->kfd->shared_resources.enable_mes) {
+		amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, pqn->q->gang_ctx_array_index);
 		amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q->gang_ctx_bo);
 		amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
 	}
@@ -282,6 +283,9 @@ static int init_user_queue(struct process_queue_manager *pqm,
 			goto cleanup;
 		}
 		memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
+		/* Bind one MES gang context slot per queue (gang). */
+		amdgpu_mes_alloc_gang_ctx_index(&dev->adev->mes,
+						&(*q)->gang_ctx_array_index);
 
 		/* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
 		 * on unmapped queues for usermode queue oversubscription (no aggregated doorbell)
@@ -304,6 +308,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
 	return 0;
 
 free_gang_ctx_bo:
+	amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index);
 	amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
 	uninit_queue(*q);
@@ -386,6 +391,11 @@ int pqm_create_queue(struct process_queue_manager *pqm,
 			goto err_allocate_pqn;
 		}
 		memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
+		/* Bind one MES process context slot to the whole process
+		 * (per device); every queue of this process reuses it.
+		 */
+		amdgpu_mes_alloc_proc_ctx_index(&dev->adev->mes,
+						&pdd->proc_ctx_array_index);
 	}
 
 	pqn = kzalloc_obj(*pqn);
Signature · 1 line
2.34.1
#2 lore raw

RE: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

AMD General

Ping..

Regards,
Prike

Quoted 148 lines

-----Original Message-----
From: Liang, Prike <[email protected]>
Sent: Thursday, July 30, 2026 10:09 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>; Liu, Shaoyun <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

Enabled RS64mem for KFD queues by integrating process and gang context index allocation in the per KFD device process and queue creation.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h                  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c               |  5 ++++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 10 ++++++++++
 4 files changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0 -0
index 51ee9c39104b..ea9d87450eae 100644
@@ -226,8 +226,10 @@ static int add_queue_mes(struct device_queue_manager
 *dqm, struct queue *q,
      /* MES unit for quantum is 100ns */
      queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM;  /*

Equivalent to 10ms. */

      queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
+     queue_input.process_context_array_index = pdd->proc_ctx_array_index;
      queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent
to 1ms */
      queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -0
+     queue_input.gang_context_array_index = q->gang_ctx_array_index;
      queue_input.inprocess_gang_priority = q->properties.priority;
      queue_input.gang_global_priority_level =
 
      AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
@@ -303,6 +305,7 @@ static int remove_queue_mes_on_reset_option(struct

device_queue_manager *dqm, st
queue_input.queue_type = convert_to_amdgpu_ring_type(q-

Quoted 1 line

properties.type);

      queue_input.remove_queue_after_reset = flush_mes_queue;
      queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -1
+     queue_input.gang_context_array_index = q->gang_ctx_array_index;
 
      amdgpu_mes_lock(&adev->mes);
      r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input); diff -
-git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h

b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index 88191a4c1657..3b89bcc625b3 100644

drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
@@ -635,6 +635,7 @@ struct queue {
      void *gang_ctx_bo;
      uint64_t gang_ctx_gpu_addr;
      void *gang_ctx_cpu_ptr;
+     uint32_t gang_ctx_array_index;
 
      struct amdgpu_bo *wptr_bo_gart;
 };
@@ -871,6 +872,8 @@ struct kfd_process_device {
      uint64_t proc_ctx_gpu_addr;
      void *proc_ctx_cpu_ptr;
 
+     uint32_t proc_ctx_array_index;
+
      /* Tracks queue reset status */
      bool has_reset_queue;
 
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -1
index eb508fe3ded7..0a7c1900da95 100644
@@ -1215,9 +1215,12 @@ static void kfd_process_destroy_pdds(struct
 kfd_process *p)
              kfd_free_process_doorbells(pdd->dev->kfd, pdd);
 
              if (pdd->dev->kfd->shared_resources.enable_mes &&
-                     pdd->proc_ctx_cpu_ptr)
+                     pdd->proc_ctx_cpu_ptr) {
+                     amdgpu_mes_free_proc_ctx_index(&pdd->dev->adev->mes,
+                                                    pdd->proc_ctx_array_index);
                      amdgpu_amdkfd_free_kernel_mem(pdd->dev->adev,
                                                 &pdd->proc_ctx_bo);
+             }
              /*
               * before destroying pdd, make sure to report availability

* for auto suspend

drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +2 -0
index 8054e4fe0381..72913b5c8338 100644
@@ -210,6 +210,7 @@ static void pqm_clean_queue_resource(struct
 process_queue_manager *pqm,
      }
 
      if (dev->kfd->shared_resources.enable_mes) {
+             amdgpu_mes_free_gang_ctx_index(&dev->adev->mes,
+pqn->q->gang_ctx_array_index);
              amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q-
Quoted 1 line

gang_ctx_bo);

              amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q-
Quoted 1 line

wptr_bo_gart);

      }
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +3 -0
@@ -282,6 +283,9 @@ static int init_user_queue(struct process_queue_manager
 *pqm,
                      goto cleanup;
              }
              memset((*q)->gang_ctx_cpu_ptr, 0,
 AMDGPU_MES_GANG_CTX_SIZE);
+             /* Bind one MES gang context slot per queue (gang). */
+             amdgpu_mes_alloc_gang_ctx_index(&dev->adev->mes,
+                                             &(*q)->gang_ctx_array_index);
 
              /* Starting with GFX11, wptr BOs must be mapped to GART for MES

to determine work
* on unmapped queues for usermode queue oversubscription (no
aggregated doorbell) @@ -304,6 +308,7 @@ static int init_user_queue(struct process_queue_manager *pqm,

      return 0;

free_gang_ctx_bo:

drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +7 -0
+     amdgpu_mes_free_gang_ctx_index(&dev->adev->mes,
+(*q)->gang_ctx_array_index);
      amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
      uninit_queue(*q);
@@ -386,6 +391,11 @@ int pqm_create_queue(struct process_queue_manager
 *pqm,
                      goto err_allocate_pqn;
              }
              memset(pdd->proc_ctx_cpu_ptr, 0,
 AMDGPU_MES_PROC_CTX_SIZE);
+             /* Bind one MES process context slot to the whole process
+              * (per device); every queue of this process reuses it.
+              */
+             amdgpu_mes_alloc_proc_ctx_index(&dev->adev->mes,
+                                             &pdd->proc_ctx_array_index);
      }
      pqn = kzalloc_obj(*pqn);

--
2.34.1

#3 lore raw

Re: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

In reply to Prike Liang

AMD General

________________________________
From: Liang, Prike <[email protected]>
Sent: Wednesday, July 29, 2026 10:09 PM
To: [email protected] <[email protected]>
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>; Liu, Shaoyun <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

Enabled RS64mem for KFD queues by integrating
process and gang context index allocation in
the per KFD device process and queue creation.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h                  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c               |  5 ++++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 10 ++++++++++
 4 files changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3 -0
index 51ee9c39104b..ea9d87450eae 100644
@@ -226,8 +226,10 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
         /* MES unit for quantum is 100ns */
         queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM;  /* Equivalent to 10ms. */
         queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
+       queue_input.process_context_array_index = pdd->proc_ctx_array_index;
         queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */
         queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+       queue_input.gang_context_array_index = q->gang_ctx_array_index;
         queue_input.inprocess_gang_priority = q->properties.priority;
         queue_input.gang_global_priority_level =
                                         AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
@@ -303,6 +305,7 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
         queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
         queue_input.remove_queue_after_reset = flush_mes_queue;
         queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
+       queue_input.gang_context_array_index = q->gang_ctx_array_index;
 
         amdgpu_mes_lock(&adev->mes);
         r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
index 88191a4c1657..3b89bcc625b3 100644
@@ -635,6 +635,7 @@ struct queue {
         void *gang_ctx_bo;
         uint64_t gang_ctx_gpu_addr;
         void *gang_ctx_cpu_ptr;
+       uint32_t gang_ctx_array_index;
 
         struct amdgpu_bo *wptr_bo_gart;
 };
@@ -871,6 +872,8 @@ struct kfd_process_device {
         uint64_t proc_ctx_gpu_addr;
         void *proc_ctx_cpu_ptr;
 
+       uint32_t proc_ctx_array_index;
+
         /* Tracks queue reset status */
         bool has_reset_queue;
 
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -1
index eb508fe3ded7..0a7c1900da95 100644
@@ -1215,9 +1215,12 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
                 kfd_free_process_doorbells(pdd->dev->kfd, pdd);
 
                 if (pdd->dev->kfd->shared_resources.enable_mes &&
-                       pdd->proc_ctx_cpu_ptr)
+                       pdd->proc_ctx_cpu_ptr) {
+                       amdgpu_mes_free_proc_ctx_index(&pdd->dev->adev->mes,
+                                                      pdd->proc_ctx_array_index);
                         amdgpu_amdkfd_free_kernel_mem(pdd->dev->adev,
                                                    &pdd->proc_ctx_bo);
+               }
                 /*
                  * before destroying pdd, make sure to report availability
                  * for auto suspend
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +4 -0
index 8054e4fe0381..72913b5c8338 100644
@@ -210,6 +210,7 @@ static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
         }
 
         if (dev->kfd->shared_resources.enable_mes) {
+               amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, pqn->q->gang_ctx_array_index);
                 amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q->gang_ctx_bo);
                 amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
         }
@@ -282,6 +283,9 @@ static int init_user_queue(struct process_queue_manager *pqm,
                         goto cleanup;
                 }
                 memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
+               /* Bind one MES gang context slot per queue (gang). */
+               amdgpu_mes_alloc_gang_ctx_index(&dev->adev->mes,
+                                               &(*q)->gang_ctx_array_index);
 
 
  1.

Should check "mes->use_rs64mem" as you do in patch #1?
2.
Check return error code (in oversubscription case, it may run out of index)

/* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
* on unmapped queues for usermode queue oversubscription (no aggregated doorbell)
@@ -304,6 +308,7 @@ static int init_user_queue(struct process_queue_manager *pqm,

         return 0;

free_gang_ctx_bo:

+       amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index);
         amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
         uninit_queue(*q);
@@ -386,6 +391,11 @@ int pqm_create_queue(struct process_queue_manager *pqm,
                         goto err_allocate_pqn;
                 }
                 memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);

+ /* Bind one MES process context slot to the whole process
+ * (per device); every queue of this process reuses it.
+ */
+ amdgpu_mes_alloc_proc_ctx_index(&dev->adev->mes,
+ &pdd->proc_ctx_array_index);

Same comment as above.

         }

         pqn = kzalloc_obj(*pqn);

--
2.34.1

#4 lore raw

RE: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

AMD General

Regards,
Prike

From: Chen, Michael <[email protected]>
Sent: Tuesday, August 4, 2026 4:31 AM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liu, Shaoyun <[email protected]>
Subject: Re: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

AMD General

________________________________
From: Liang, Prike <[email protected]<mailto:[email protected]>>
Sent: Wednesday, July 29, 2026 10:09 PM
To: [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>
Cc: Deucher, Alexander <[email protected]<mailto:[email protected]>>; Koenig, Christian <[email protected]<mailto:[email protected]>>; Chen, Michael <[email protected]<mailto:[email protected]>>; Liu, Shaoyun <[email protected]<mailto:[email protected]>>; Liang, Prike <[email protected]<mailto:[email protected]>>
Subject: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

Enabled RS64mem for KFD queues by integrating
process and gang context index allocation in
the per KFD device process and queue creation.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h                  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c               |  5 ++++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 10 ++++++++++
 4 files changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3 -0
index 51ee9c39104b..ea9d87450eae 100644
@@ -226,8 +226,10 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
         /* MES unit for quantum is 100ns */
         queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM;  /* Equivalent to 10ms. */
         queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
+       queue_input.process_context_array_index = pdd->proc_ctx_array_index;
         queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */
         queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+       queue_input.gang_context_array_index = q->gang_ctx_array_index;
         queue_input.inprocess_gang_priority = q->properties.priority;
         queue_input.gang_global_priority_level =
                                         AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
@@ -303,6 +305,7 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
         queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
         queue_input.remove_queue_after_reset = flush_mes_queue;
         queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
+       queue_input.gang_context_array_index = q->gang_ctx_array_index;
 
         amdgpu_mes_lock(&adev->mes);
         r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
index 88191a4c1657..3b89bcc625b3 100644
@@ -635,6 +635,7 @@ struct queue {
         void *gang_ctx_bo;
         uint64_t gang_ctx_gpu_addr;
         void *gang_ctx_cpu_ptr;
+       uint32_t gang_ctx_array_index;
 
         struct amdgpu_bo *wptr_bo_gart;
 };
@@ -871,6 +872,8 @@ struct kfd_process_device {
         uint64_t proc_ctx_gpu_addr;
         void *proc_ctx_cpu_ptr;
 
+       uint32_t proc_ctx_array_index;
+
         /* Tracks queue reset status */
         bool has_reset_queue;
 
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -1
index eb508fe3ded7..0a7c1900da95 100644
@@ -1215,9 +1215,12 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
                 kfd_free_process_doorbells(pdd->dev->kfd, pdd);
 
                 if (pdd->dev->kfd->shared_resources.enable_mes &&
-                       pdd->proc_ctx_cpu_ptr)
+                       pdd->proc_ctx_cpu_ptr) {
+                       amdgpu_mes_free_proc_ctx_index(&pdd->dev->adev->mes,
+                                                      pdd->proc_ctx_array_index);
                         amdgpu_amdkfd_free_kernel_mem(pdd->dev->adev,
                                                    &pdd->proc_ctx_bo);
+               }
                 /*
                  * before destroying pdd, make sure to report availability
                  * for auto suspend
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +4 -0
index 8054e4fe0381..72913b5c8338 100644
@@ -210,6 +210,7 @@ static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
         }
 
         if (dev->kfd->shared_resources.enable_mes) {
+               amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, pqn->q->gang_ctx_array_index);
                 amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q->gang_ctx_bo);
                 amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
         }
@@ -282,6 +283,9 @@ static int init_user_queue(struct process_queue_manager *pqm,
                         goto cleanup;
                 }
                 memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
+               /* Bind one MES gang context slot per queue (gang). */
+               amdgpu_mes_alloc_gang_ctx_index(&dev->adev->mes,
+                                               &(*q)->gang_ctx_array_index);
 
 
  1.  Should check "mes->use_rs64mem" as you do in patch #1?

It doesn't have to check here, since the rs64mem support will be checked in the amdgpu_mes_alloc_gang_ctx_index() as well.

1. Check return error code (in oversubscription case, it may run out of index)

/* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
* on unmapped queues for usermode queue oversubscription (no aggregated doorbell)
@@ -304,6 +308,7 @@ static int init_user_queue(struct process_queue_manager *pqm,

         return 0;

free_gang_ctx_bo:

+       amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index);
         amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
         uninit_queue(*q);
@@ -386,6 +391,11 @@ int pqm_create_queue(struct process_queue_manager *pqm,
                         goto err_allocate_pqn;
                 }
                 memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);

+ /* Bind one MES process context slot to the whole process
+ * (per device); every queue of this process reuses it.
+ */
+ amdgpu_mes_alloc_proc_ctx_index(&dev->adev->mes,
+ &pdd->proc_ctx_array_index);

Same comment as above.

         }

         pqn = kzalloc_obj(*pqn);

--
2.34.1

#5 lore raw

RE: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

AMD General

Regards,
Prike

From: amd-gfx <[email protected]> On Behalf Of Liang, Prike
Sent: Tuesday, August 4, 2026 9:57 AM
To: Chen, Michael <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liu, Shaoyun <[email protected]>
Subject: RE: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

AMD General

Regards,
Prike

From: Chen, Michael <[email protected]<mailto:[email protected]>>
Sent: Tuesday, August 4, 2026 4:31 AM
To: Liang, Prike <[email protected]<mailto:[email protected]>>; [email protected]<mailto:[email protected]>
Cc: Deucher, Alexander <[email protected]<mailto:[email protected]>>; Koenig, Christian <[email protected]<mailto:[email protected]>>; Liu, Shaoyun <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

AMD General

________________________________
From: Liang, Prike <[email protected]<mailto:[email protected]>>
Sent: Wednesday, July 29, 2026 10:09 PM
To: [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>
Cc: Deucher, Alexander <[email protected]<mailto:[email protected]>>; Koenig, Christian <[email protected]<mailto:[email protected]>>; Chen, Michael <[email protected]<mailto:[email protected]>>; Liu, Shaoyun <[email protected]<mailto:[email protected]>>; Liang, Prike <[email protected]<mailto:[email protected]>>
Subject: [PATCH v2 2/2] drm/amdkfd: enable rs64mem for kfd queue

Enabled RS64mem for KFD queues by integrating
process and gang context index allocation in
the per KFD device process and queue creation.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h                  |  3 +++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c               |  5 ++++-
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 10 ++++++++++
 4 files changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +3 -0
index 51ee9c39104b..ea9d87450eae 100644
@@ -226,8 +226,10 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
         /* MES unit for quantum is 100ns */
         queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM;  /* Equivalent to 10ms. */
         queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
+       queue_input.process_context_array_index = pdd->proc_ctx_array_index;
         queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */
         queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
+       queue_input.gang_context_array_index = q->gang_ctx_array_index;
         queue_input.inprocess_gang_priority = q->properties.priority;
         queue_input.gang_global_priority_level =
                                         AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
@@ -303,6 +305,7 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
         queue_input.queue_type = convert_to_amdgpu_ring_type(q->properties.type);
         queue_input.remove_queue_after_reset = flush_mes_queue;
         queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
+       queue_input.gang_context_array_index = q->gang_ctx_array_index;
 
         amdgpu_mes_lock(&adev->mes);
         r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
index 88191a4c1657..3b89bcc625b3 100644
@@ -635,6 +635,7 @@ struct queue {
         void *gang_ctx_bo;
         uint64_t gang_ctx_gpu_addr;
         void *gang_ctx_cpu_ptr;
+       uint32_t gang_ctx_array_index;
 
         struct amdgpu_bo *wptr_bo_gart;
 };
@@ -871,6 +872,8 @@ struct kfd_process_device {
         uint64_t proc_ctx_gpu_addr;
         void *proc_ctx_cpu_ptr;
 
+       uint32_t proc_ctx_array_index;
+
         /* Tracks queue reset status */
         bool has_reset_queue;
 
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -1
index eb508fe3ded7..0a7c1900da95 100644
@@ -1215,9 +1215,12 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
                 kfd_free_process_doorbells(pdd->dev->kfd, pdd);
 
                 if (pdd->dev->kfd->shared_resources.enable_mes &&
-                       pdd->proc_ctx_cpu_ptr)
+                       pdd->proc_ctx_cpu_ptr) {
+                       amdgpu_mes_free_proc_ctx_index(&pdd->dev->adev->mes,
+                                                      pdd->proc_ctx_array_index);
                         amdgpu_amdkfd_free_kernel_mem(pdd->dev->adev,
                                                    &pdd->proc_ctx_bo);
+               }
                 /*
                  * before destroying pdd, make sure to report availability
                  * for auto suspend
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +4 -0
index 8054e4fe0381..72913b5c8338 100644
@@ -210,6 +210,7 @@ static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
         }
 
         if (dev->kfd->shared_resources.enable_mes) {
+               amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, pqn->q->gang_ctx_array_index);
                 amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q->gang_ctx_bo);
                 amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
         }
@@ -282,6 +283,9 @@ static int init_user_queue(struct process_queue_manager *pqm,
                         goto cleanup;
                 }
                 memset((*q)->gang_ctx_cpu_ptr, 0, AMDGPU_MES_GANG_CTX_SIZE);
+               /* Bind one MES gang context slot per queue (gang). */
+               amdgpu_mes_alloc_gang_ctx_index(&dev->adev->mes,
+                                               &(*q)->gang_ctx_array_index);
 
 
  1.  Should check "mes->use_rs64mem" as you do in patch #1?

It doesn't have to check here, since the rs64mem support will be checked in the amdgpu_mes_alloc_gang_ctx_index() as well.

Quoted 1 line
Quoted 1 line

Think more about it, we may still need to check the rs64mem flag at the invocation place to make the case which disables the rs64mem work properly.

1. Check return error code (in oversubscription case, it may run out of index)
Yeah, we add the error handler.

/* Starting with GFX11, wptr BOs must be mapped to GART for MES to determine work
* on unmapped queues for usermode queue oversubscription (no aggregated doorbell)
@@ -304,6 +308,7 @@ static int init_user_queue(struct process_queue_manager *pqm,

         return 0;

free_gang_ctx_bo:

+       amdgpu_mes_free_gang_ctx_index(&dev->adev->mes, (*q)->gang_ctx_array_index);
         amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
         uninit_queue(*q);
@@ -386,6 +391,11 @@ int pqm_create_queue(struct process_queue_manager *pqm,
                         goto err_allocate_pqn;
                 }
                 memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);

+ /* Bind one MES process context slot to the whole process
+ * (per device); every queue of this process reuses it.
+ */
+ amdgpu_mes_alloc_proc_ctx_index(&dev->adev->mes,
+ &pdd->proc_ctx_array_index);

Same comment as above.

         }

         pqn = kzalloc_obj(*pqn);

--
2.34.1

#6 lore raw
In reply to Prike Liang

AMD General

________________________________
From: Liang, Prike <[email protected]>
Sent: Wednesday, July 29, 2026 10:09 PM
To: [email protected] <[email protected]>
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>; Liu, Shaoyun <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH v2 1/2] drm/amdgpu/mes: refactor the amdgpu_mes_alloc/free_proc|gang()

- Unify amdgpu_mes_alloc/free_proc|gang_ctx_index to provide centralized RS64mem bitmap management for both KGD and KFD.
- Retrieve the bitmap bit for userq contex index based on a per process granularity.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c    | 28 +++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h    |  8 +++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  |  6 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  3 ++-
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 20 ++++++++++------
 5 files changed, 39 insertions(+), 26 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +8 -8
index 9af7c97af5be..e3862095ff99 100644
@@ -1040,13 +1040,13 @@ int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes)
  * amdgpu_mes_alloc_proc_ctx_index - allocate a process context slot
  *
  * @mes: MES instance
- * @queue: Usermode queue receiving the allocated process context index
+ * @index: the allocated process context index
  *
  * Returns 0 on success, -ENOSPC if all slots are used, or
  * -EOPNOTSUPP if RS64 local memory is unavailable.
  */
 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue)
+                                   uint32_t *index)
 {
         unsigned long bit;
 
@@ -1061,7 +1061,7 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
                 return -ENOSPC;
         }
         set_bit(bit, mes->proc_ctx_bitmap);
-       queue->proc_ctx_array_index = (uint32_t)bit;
+       *index = (uint32_t)bit;
         amdgpu_mes_unlock(mes);
 
         return 0;
@@ -1071,18 +1071,18 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_free_proc_ctx_index - free a process context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue whose process context index is released
+  * @index: process context index is released
   */
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue)
+                                   uint32_t index)
 {
         if (!mes->use_rs64mem || !mes->proc_ctx_bitmap)
                 return;
-       if (queue->proc_ctx_array_index >= mes->proc_ctx_array_size)
+       if (index >= mes->proc_ctx_array_size)
                 return;
 
         amdgpu_mes_lock(mes);
-       clear_bit(queue->proc_ctx_array_index, mes->proc_ctx_bitmap);
+       clear_bit(index, mes->proc_ctx_bitmap);
         amdgpu_mes_unlock(mes);
 }
 
@@ -1090,13 +1090,13 @@ void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
   * amdgpu_mes_alloc_gang_ctx_index - allocate a gang context slot
   *
   * @mes: MES instance
-  * @queue: Usermode queue receiving the allocated gang context index
+  * @queue: the allocated gang context index
 

The parameter is "index", not "queue".

*
* Returns 0 on success, -ENOSPC if all slots are used, or
* -EOPNOTSUPP if RS64 local memory is unavailable. */
int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
- struct amdgpu_usermode_queue *queue)
+ uint32_t *index)

 {
         unsigned long bit;

@@ -1111,7 +1111,7 @@ int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,

                 return -ENOSPC;
         }
         set_bit(bit, mes->gang_ctx_bitmap);
-       queue->gang_ctx_array_index = bit;
+       *index = bit;
         amdgpu_mes_unlock(mes);

         return 0;

@@ -1121,18 +1121,18 @@ int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
* amdgpu_mes_free_gang_ctx_index - free a gang context slot
*
* @mes: MES instance
- * @queue: Usermode queue whose gang context index is released
+ * @queue: gang context index is released

The parameter is "index", not "queue".

*/
void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
- struct amdgpu_usermode_queue *queue)
+ uint32_t index)

 {
         if (!mes->use_rs64mem || !mes->gang_ctx_bitmap)
                 return;
-       if (queue->gang_ctx_array_index >= mes->gang_ctx_array_size)
+       if (index >= mes->gang_ctx_array_size)
                 return;

         amdgpu_mes_lock(mes);
-       clear_bit(queue->gang_ctx_array_index, mes->gang_ctx_bitmap);
+       clear_bit(index, mes->gang_ctx_bitmap);
         amdgpu_mes_unlock(mes);

}

drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +4 -4
index c67db2d6e122..977c057dcce8 100644
@@ -636,11 +636,11 @@ int amdgpu_mes_rs64mem_init(struct amdgpu_mes *mes);
 void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes);
 int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes);
 int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue);
+                                   uint32_t *index);
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue);
+                                   uint32_t index);
 int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue);
+                                   uint32_t *index);
 void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
-                                   struct amdgpu_usermode_queue *queue);
+                                   uint32_t index);
 #endif /* __AMDGPU_MES_H__ */
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
index a1f19888dc55..0e9657f2dbe7 100644
@@ -1249,6 +1249,7 @@ int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *f
         xa_init_flags(&userq_mgr->userq_xa, XA_FLAGS_ALLOC);
         userq_mgr->adev = adev;
         userq_mgr->file = file_priv;
+       userq_mgr->proc_ctx_allocated = false;
         mutex_init(&userq_mgr->proc_ctx_lock);
 
         INIT_DELAYED_WORK(&userq_mgr->resume_work, amdgpu_userq_restore_worker);
@@ -1277,6 +1278,7 @@ void amdgpu_userq_mgr_cancel_resume(struct amdgpu_userq_mgr *userq_mgr)
 
 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 {
+       struct amdgpu_mes *mes = &userq_mgr->adev->mes;
         struct amdgpu_usermode_queue *queue;
         unsigned long queue_id = 0;
 
@@ -1303,6 +1305,10 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
          */
         cancel_work_sync(&userq_mgr->reset_work);
 
+       if (userq_mgr->proc_ctx_allocated) {
+               amdgpu_mes_free_proc_ctx_index(mes, userq_mgr->proc_ctx_array_index);
+               userq_mgr->proc_ctx_allocated = false;
+       }
         amdgpu_bo_free_kernel(&userq_mgr->proc_ctx_obj.obj,
                               &userq_mgr->proc_ctx_obj.gpu_addr,
                               &userq_mgr->proc_ctx_obj.cpu_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -1
index 8333bab537d4..6412a7f7b6ef 100644
@@ -101,7 +101,6 @@ struct amdgpu_usermode_queue {
                 u64 va_array[6];
         } userq_vas;
 
-       uint32_t        proc_ctx_array_index;
         uint32_t        gang_ctx_array_index;
 };
 
@@ -133,6 +132,8 @@ struct amdgpu_userq_mgr {
         struct mutex                    proc_ctx_lock;
         struct amdgpu_userq_obj         proc_ctx_obj;
 
+       bool                            proc_ctx_allocated;
+       uint32_t                        proc_ctx_array_index;
         /**
          * @reset_work:
          *
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +10 -3
index a914198a4d62..1fd72f18e9a8 100644
@@ -143,10 +143,18 @@ static int mes_userq_map(struct amdgpu_usermode_queue *queue)
         queue_input.doorbell_offset = userq_props->doorbell_index;
         queue_input.page_table_base_addr = amdgpu_gmc_pd_addr(queue->vm->root.bo);
         queue_input.wptr_mc_addr = queue->wptr_obj.gpu_addr;
+
         if (mes->use_rs64mem) {
-               amdgpu_mes_alloc_proc_ctx_index(mes, queue);
-               queue_input.process_context_array_index = queue->proc_ctx_array_index;
-               amdgpu_mes_alloc_gang_ctx_index(mes, queue);
+                if (!uq_mgr->proc_ctx_allocated) {
+                       r = amdgpu_mes_alloc_proc_ctx_index(mes, &uq_mgr->proc_ctx_array_index);
+                       if (r) {
+                               DRM_ERROR("Failed to allocate userq process index err:%d\n", r);
+                               return r;
+                       }
+                       uq_mgr->proc_ctx_allocated = true;
+               }
+               amdgpu_mes_alloc_gang_ctx_index(mes, &queue->gang_ctx_array_index);
 

Need to check the return code, and handle error (e.g. running out of gang ctx index).

+               queue_input.process_context_array_index = uq_mgr->proc_ctx_array_index;
                 queue_input.gang_context_array_index = queue->gang_ctx_array_index;
         }
         amdgpu_mes_lock(&adev->mes);
@@ -179,10 +187,8 @@ static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
         amdgpu_mes_lock(&adev->mes);
         r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
         amdgpu_mes_unlock(&adev->mes);

- if (mes->use_rs64mem) {
- amdgpu_mes_free_proc_ctx_index(mes, queue);
- amdgpu_mes_free_gang_ctx_index(mes, queue);
- }
+ if (mes->use_rs64mem)

+               amdgpu_mes_free_gang_ctx_index(mes, queue->gang_ctx_array_index);
         if (r)
                 DRM_ERROR("Failed to unmap queue in HW, err (%d)\n", r);
         return r;

--
2.34.1