Skip to contentSkip to conversation

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

PATCH Superseded Prike Liang 2 participants 4 messages Jul 28 – Jul 29

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c    | 28 +++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h    |  8 +++----
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 12 ++++++----
 3 files changed, 26 insertions(+), 22 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/mes_userqueue.c +8 -4
index a914198a4d62..7369a8e05101 100644
@@ -144,9 +144,13 @@ static int mes_userq_map(struct amdgpu_usermode_queue *queue)
 	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);
+		uint32_t index;
+
+		amdgpu_mes_alloc_proc_ctx_index(mes, &index);
+		queue->proc_ctx_array_index = index;
 		queue_input.process_context_array_index = queue->proc_ctx_array_index;
-		amdgpu_mes_alloc_gang_ctx_index(mes, queue);
+		amdgpu_mes_alloc_gang_ctx_index(mes, &index);
+		queue->gang_ctx_array_index = index;
 		queue_input.gang_context_array_index = queue->gang_ctx_array_index;
 	}
 	amdgpu_mes_lock(&adev->mes);
@@ -180,8 +184,8 @@ static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
 	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);
+		amdgpu_mes_free_proc_ctx_index(mes, queue->proc_ctx_array_index);
+		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);
Signature · 1 line
2.34.1
#1 lore raw

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

Enabled RS64mem for KFD queues by integrating
process and gang context index allocation.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 25 ++++++++++++++++---
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  3 +++
 2 files changed, 25 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +22 -3
index 51ee9c39104b..ce0ca3445ccd 100644
@@ -210,8 +210,10 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
 	struct kfd_process_device *pdd = qpd_to_pdd(qpd);
 	struct mes_add_queue_input queue_input;
+	struct amdgpu_mes *mes = &adev->mes;
 	int r, queue_type;
 	uint64_t wptr_addr_off;
+	uint32_t index;
 
 	if (!dqm->sched_running || dqm->sched_halt)
 		return 0;
@@ -265,9 +267,18 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
 	queue_input.vm_cntx_cntl = qpd->vm_cntx_cntl;
 	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
 
-	amdgpu_mes_lock(&adev->mes);
-	r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
-	amdgpu_mes_unlock(&adev->mes);
+	if (mes->use_rs64mem) {
+		amdgpu_mes_alloc_proc_ctx_index(mes, &index);
+		pdd->proc_ctx_array_index = index;
+		queue_input.process_context_array_index = index;
+		amdgpu_mes_alloc_gang_ctx_index(mes, &index);
+		pdd->gang_ctx_array_index = index;
+		queue_input.gang_context_array_index = index;
+	}
+
+	amdgpu_mes_lock(mes);
+	r = adev->mes.funcs->add_hw_queue(mes, &queue_input);
+	amdgpu_mes_unlock(mes);
 	up_read(&adev->reset_domain->sem);
 	if (r) {
 		dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=0x%x\n",
@@ -287,6 +298,8 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
 	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
 	int r;
 	struct mes_remove_queue_input queue_input;
+	struct kfd_process_device *pdd = qpd_to_pdd(qpd);
+	struct amdgpu_mes *mes = &adev->mes;
 
 	/* queue was already removed during reset */
 	if (q->properties.is_reset)
@@ -303,10 +316,16 @@ 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 = pdd->gang_ctx_array_index;
 
 	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, pdd->proc_ctx_array_index);
+		amdgpu_mes_free_gang_ctx_index(mes, pdd->gang_ctx_array_index);
+	}
+
 	up_read(&adev->reset_domain->sem);
 
 	/* If is_for_reset set, it is a mes internal cleanup */
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
index 88191a4c1657..c1bbae9e6e82 100644
@@ -871,6 +871,9 @@ struct kfd_process_device {
 	uint64_t proc_ctx_gpu_addr;
 	void *proc_ctx_cpu_ptr;
 
+	uint32_t	proc_ctx_array_index;
+	uint32_t	gang_ctx_array_index;
+
 	/* Tracks queue reset status */
 	bool has_reset_queue;
 
Signature · 1 line
2.34.1
#2 lore raw

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

AMD General

________________________________
From: Liang, Prike <[email protected]>
Sent: Tuesday, July 28, 2026 8:53 AM
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 2/2] drm/amdkfd: enable rs64mem for kfd queue

Enabled RS64mem for KFD queues by integrating
process and gang context index allocation.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 25 ++++++++++++++++---
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  3 +++
 2 files changed, 25 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +10 -3
index 51ee9c39104b..ce0ca3445ccd 100644
@@ -210,8 +210,10 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
         struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
         struct kfd_process_device *pdd = qpd_to_pdd(qpd);
         struct mes_add_queue_input queue_input;
+       struct amdgpu_mes *mes = &adev->mes;
         int r, queue_type;
         uint64_t wptr_addr_off;
+       uint32_t index;
 
         if (!dqm->sched_running || dqm->sched_halt)
                 return 0;
@@ -265,9 +267,18 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
         queue_input.vm_cntx_cntl = qpd->vm_cntx_cntl;
         queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
 
-       amdgpu_mes_lock(&adev->mes);
-       r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
-       amdgpu_mes_unlock(&adev->mes);
+       if (mes->use_rs64mem) {
+               amdgpu_mes_alloc_proc_ctx_index(mes, &index);
+               pdd->proc_ctx_array_index = index;
+               queue_input.process_context_array_index = index;
+               amdgpu_mes_alloc_gang_ctx_index(mes, &index);
+               pdd->gang_ctx_array_index = index;
+               queue_input.gang_context_array_index = index;
+       }
 

Looks like you allocate one unique proc_ctx_array_index for each queue, but shouldn't all queues of the same process share one index?

+

+       amdgpu_mes_lock(mes);
+       r = adev->mes.funcs->add_hw_queue(mes, &queue_input);
+       amdgpu_mes_unlock(mes);
         up_read(&adev->reset_domain->sem);
         if (r) {
                 dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=0x%x\n",
@@ -287,6 +298,8 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
         struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
         int r;
         struct mes_remove_queue_input queue_input;

+ struct kfd_process_device *pdd = qpd_to_pdd(qpd);
+ struct amdgpu_mes *mes = &adev->mes;

         /* queue was already removed during reset */
         if (q->properties.is_reset)
@@ -303,10 +316,16 @@ 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 = pdd->gang_ctx_array_index;

         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, pdd->proc_ctx_array_index);
+ amdgpu_mes_free_gang_ctx_index(mes, pdd->gang_ctx_array_index);
+ }
+

         up_read(&adev->reset_domain->sem);

         /* If is_for_reset set, it is a mes internal cleanup */
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
index 88191a4c1657..c1bbae9e6e82 100644
@@ -871,6 +871,9 @@ struct kfd_process_device {
         uint64_t proc_ctx_gpu_addr;
         void *proc_ctx_cpu_ptr;
 
+       uint32_t        proc_ctx_array_index;
+       uint32_t        gang_ctx_array_index;
+
         /* Tracks queue reset status */
         bool has_reset_queue;
 

--
2.34.1

#3 lore raw

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

AMD General

Regards,
Prike

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

AMD General

________________________________
From: Liang, Prike <[email protected]<mailto:[email protected]>>
Sent: Tuesday, July 28, 2026 8:53 AM
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 2/2] drm/amdkfd: enable rs64mem for kfd queue

Enabled RS64mem for KFD queues by integrating
process and gang context index allocation.

---
 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 25 ++++++++++++++++---
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  3 +++
 2 files changed, 25 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +10 -3
index 51ee9c39104b..ce0ca3445ccd 100644
@@ -210,8 +210,10 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
         struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
         struct kfd_process_device *pdd = qpd_to_pdd(qpd);
         struct mes_add_queue_input queue_input;
+       struct amdgpu_mes *mes = &adev->mes;
         int r, queue_type;
         uint64_t wptr_addr_off;
+       uint32_t index;
 
         if (!dqm->sched_running || dqm->sched_halt)
                 return 0;
@@ -265,9 +267,18 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
         queue_input.vm_cntx_cntl = qpd->vm_cntx_cntl;
         queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
 
-       amdgpu_mes_lock(&adev->mes);
-       r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
-       amdgpu_mes_unlock(&adev->mes);
+       if (mes->use_rs64mem) {
+               amdgpu_mes_alloc_proc_ctx_index(mes, &index);
+               pdd->proc_ctx_array_index = index;
+               queue_input.process_context_array_index = index;
+               amdgpu_mes_alloc_gang_ctx_index(mes, &index);
+               pdd->gang_ctx_array_index = index;
+               queue_input.gang_context_array_index = index;
+       }
 

Looks like you allocate one unique proc_ctx_array_index for each queue, but shouldn't all queues of the same process share one index?

Thanks for pointing it out, it's more reasonable and efficient to bind the process/gang index to per process/gang, and I will update it in the next version.

+

+       amdgpu_mes_lock(mes);
+       r = adev->mes.funcs->add_hw_queue(mes, &queue_input);
+       amdgpu_mes_unlock(mes);
         up_read(&adev->reset_domain->sem);
         if (r) {
                 dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=0x%x\n",
@@ -287,6 +298,8 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
         struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
         int r;
         struct mes_remove_queue_input queue_input;

+ struct kfd_process_device *pdd = qpd_to_pdd(qpd);
+ struct amdgpu_mes *mes = &adev->mes;

         /* queue was already removed during reset */
         if (q->properties.is_reset)
@@ -303,10 +316,16 @@ 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 = pdd->gang_ctx_array_index;

         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, pdd->proc_ctx_array_index);
+ amdgpu_mes_free_gang_ctx_index(mes, pdd->gang_ctx_array_index);
+ }
+

         up_read(&adev->reset_domain->sem);

         /* If is_for_reset set, it is a mes internal cleanup */
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
index 88191a4c1657..c1bbae9e6e82 100644
@@ -871,6 +871,9 @@ struct kfd_process_device {
         uint64_t proc_ctx_gpu_addr;
         void *proc_ctx_cpu_ptr;
 
+       uint32_t        proc_ctx_array_index;
+       uint32_t        gang_ctx_array_index;
+
         /* Tracks queue reset status */
         bool has_reset_queue;
 

--
2.34.1