Skip to contentSkip to conversation

drm/amdgpu: add RS64 local memory context array init/fini

PATCH In review Prike Liang 3 participants 10 messages Jul 9 – Jul 13

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

PATCH 1/6 drm/amdgpu: add RS64 local memory context array init/fini

Add amdgpu_mes_rs64mem_init() and amdgpu_mes_rs64mem_fini() to manage the RS64 local memory context arrays used by the MES scheduler.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c | 44 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  7 ++++
 2 files changed, 51 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +44 -0
index c66e69b62361..8ba1c941c03d 100644
@@ -921,6 +921,50 @@ int amdgpu_mes_update_enforce_isolation(struct amdgpu_device *adev)
 	return r;
 }
 
+/**
+ * amdgpu_mes_rs64mem_init - initialize RS64 local memory context arrays
+ *
+ * @mes: MES instance
+ *
+ * Returns 0 on success, negative errno on failure.
+ */
+int amdgpu_mes_rs64mem_init(struct amdgpu_mes *mes)
+{
+	struct amdgpu_device *adev = container_of(mes, struct amdgpu_device, mes);
+	int r;
+
+	if (!mes->use_rs64mem)
+		return 0;
+
+	r = amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+				    AMDGPU_GEM_DOMAIN_GTT,
+				    &mes->ctx_array_size_bo,
+				    &mes->ctx_array_size_gpu_addr,
+				    (void **)&mes->ctx_array_size_cpu_ptr);
+	if (r) {
+		dev_err(adev->dev,
+			"Failed to allocate ctx array size BO, r=%d\n", r);
+		return r;
+	}
+
+	memset(mes->ctx_array_size_cpu_ptr, 0, PAGE_SIZE);
+
+	return 0;
+}
+
+/**
+ * amdgpu_mes_rs64mem_fini - tear down RS64 local memory management
+ */
+void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes)
+{
+	if (mes->ctx_array_size_bo) {
+		amdgpu_bo_free_kernel(&mes->ctx_array_size_bo,
+				      &mes->ctx_array_size_gpu_addr,
+				      (void **)&mes->ctx_array_size_cpu_ptr);
+	}
+	mes->use_rs64mem = false;
+}
+
 #if defined(CONFIG_DEBUG_FS)
 
 static int amdgpu_debugfs_mes_event_log_show(struct seq_file *m, void *unused)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +7 -0
index 389e3324caea..520bcd8ee202 100644
@@ -171,6 +171,11 @@ struct amdgpu_mes {
 
 	bool			compute_pipe_reset_enabled;
 	bool			gfx_pipe_reset_enabled;
+
+	bool				use_rs64mem;
+	struct amdgpu_bo		*ctx_array_size_bo;
+	uint64_t			ctx_array_size_gpu_addr;
+	uint32_t			*ctx_array_size_cpu_ptr;
 };
 
 struct amdgpu_mes_hung_queue_hqd_info {
@@ -617,4 +622,6 @@ bool amdgpu_mes_queue_reset_by_mes_supported(struct amdgpu_device *adev);
 
 int amdgpu_mes_update_enforce_isolation(struct amdgpu_device *adev);
 
+int amdgpu_mes_rs64mem_init(struct amdgpu_mes *mes);
+void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes);
 #endif /* __AMDGPU_MES_H__ */
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/6 drm/amdgpu/mes: add MES process/gang context size and bitmap helper

Allocating the MES context bitmap to track the process/gang index usage.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c | 70 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  9 +++-
 2 files changed, 78 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +70 -0
index 8ba1c941c03d..f65f2a30154d 100644
@@ -962,9 +962,79 @@ void amdgpu_mes_rs64mem_fini(struct amdgpu_mes *mes)
 				      &mes->ctx_array_size_gpu_addr,
 				      (void **)&mes->ctx_array_size_cpu_ptr);
 	}
+	
+	bitmap_free(mes->proc_ctx_bitmap);
+	bitmap_free(mes->gang_ctx_bitmap);
 	mes->use_rs64mem = false;
 }
 
+/**
+ * amdgpu_mes_rs64mem_setup_bitmaps - allocate bitmaps after querying MES
+ *
+ * Called after QUERY_SCHEDULER_STATUS returns and MES has written
+ * the array sizes to the GPU buffer. Reads the sizes and allocates
+ * the tracking bitmaps.
+ *
+ * @mes: MES instance
+ *
+ * Returns 0 on success, negative errno on failure.
+ */
+int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes)
+{
+	struct amdgpu_device *adev = container_of(mes, struct amdgpu_device, mes);
+
+	if (!mes->use_rs64mem || !mes->ctx_array_size_cpu_ptr)
+		return 0;
+
+	/*
+	 * MES FW wrote the sizes to the GPU buffer:
+	 *   ctx_array_size_cpu_ptr[0] = proc_ctx_array_size (N)
+	 *   ctx_array_size_cpu_ptr[1] = gang_ctx_array_size (M)
+	 */
+	mes->proc_ctx_array_size = mes->ctx_array_size_cpu_ptr[0];
+	mes->gang_ctx_array_size = mes->ctx_array_size_cpu_ptr[1];
+
+	/* Sanity check - MES FW typically returns N=50, M=300 */
+	if (mes->proc_ctx_array_size == 0 || mes->gang_ctx_array_size == 0) {
+		dev_warn(adev->dev,
+			 "MES returned zero ctx array sizes (proc=%u, gang=%u), "
+			 "disabling RS64 local memory optimization\n",
+			 mes->proc_ctx_array_size, mes->gang_ctx_array_size);
+		mes->use_rs64mem = false;
+		return 0;
+	}
+
+	/* Cap to safety limits */
+	if (mes->proc_ctx_array_size > AMDGPU_MES_PROC_CTX_ARRAY_MAX)
+		mes->proc_ctx_array_size = AMDGPU_MES_PROC_CTX_ARRAY_MAX;
+	if (mes->gang_ctx_array_size > AMDGPU_MES_GANG_CTX_ARRAY_MAX)
+		mes->gang_ctx_array_size = AMDGPU_MES_GANG_CTX_ARRAY_MAX;
+
+	dev_info(adev->dev,
+		 "MES RS64 local memory: proc_ctx_array_size:%u, "
+		 "gang_ctx_array_size:%u\n",
+		 mes->proc_ctx_array_size, mes->gang_ctx_array_size);
+
+	/* Allocate bitmaps */
+	mes->proc_ctx_bitmap = bitmap_zalloc(mes->proc_ctx_array_size,
+					     GFP_KERNEL);
+	if (!mes->proc_ctx_bitmap) {
+		mes->use_rs64mem = false;
+		return -ENOMEM;
+	}
+
+	mes->gang_ctx_bitmap = bitmap_zalloc(mes->gang_ctx_array_size,
+					     GFP_KERNEL);
+	if (!mes->gang_ctx_bitmap) {
+		bitmap_free(mes->proc_ctx_bitmap);
+		mes->proc_ctx_bitmap = NULL;
+		mes->use_rs64mem = false;
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
 #if defined(CONFIG_DEBUG_FS)
 
 static int amdgpu_debugfs_mes_event_log_show(struct seq_file *m, void *unused)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +8 -1
index 520bcd8ee202..5c50bc9616b7 100644
@@ -54,7 +54,8 @@ enum amdgpu_mes_priority_level {
 
 #define AMDGPU_MES_PROC_CTX_SIZE 0x1000 /* one page area */
 #define AMDGPU_MES_GANG_CTX_SIZE 0x1000 /* one page area */
-
+#define AMDGPU_MES_PROC_CTX_ARRAY_MAX  128
+#define AMDGPU_MES_GANG_CTX_ARRAY_MAX  512
 struct amdgpu_mes_funcs;
 
 enum amdgpu_mes_pipe {
@@ -176,6 +177,11 @@ struct amdgpu_mes {
 	struct amdgpu_bo		*ctx_array_size_bo;
 	uint64_t			ctx_array_size_gpu_addr;
 	uint32_t			*ctx_array_size_cpu_ptr;
+
+	uint32_t			proc_ctx_array_size;
+	unsigned long			*proc_ctx_bitmap;
+	uint32_t			gang_ctx_array_size;
+	unsigned long			*gang_ctx_bitmap;
 };
 
 struct amdgpu_mes_hung_queue_hqd_info {
@@ -624,4 +630,5 @@ int amdgpu_mes_update_enforce_isolation(struct amdgpu_device *adev);
 
 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);
 #endif /* __AMDGPU_MES_H__ */
Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/6 drm/amdgpu/mes11: get MES process/gang contex size

In reply to Prike Liang

Setup the MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE MES firmware command request, and get the MES11 process/gang contex size.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c        | 79 +++++++++++++++++++
 drivers/gpu/drm/amd/include/mes_v11_api_def.h | 61 ++++++++++++--
 2 files changed, 132 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +79 -0
index c721a90051e9..195dc3b4058d 100644
@@ -832,6 +832,58 @@ static int mes_v11_0_query_sched_status(struct amdgpu_mes *mes)
 			offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
 }
 
+/**
+ * QUERY_SCHEDULER_STATUS: get proc/gang context array sizes
+ */
+static int mes_v11_0_query_ctx_array_sizes(struct amdgpu_mes *mes)
+{
+	struct amdgpu_device *adev = mes->adev;;
+	union MESAPI__QUERY_MES_STATUS mes_query_pkt;
+	int r;
+
+	if (!mes->ctx_array_size_cpu_ptr)
+		return -EINVAL;
+
+	/* Clear the output buffer */
+	mes->ctx_array_size_cpu_ptr[0] = 0;  /* proc_ctx_array_size */
+	mes->ctx_array_size_cpu_ptr[1] = 0;  /* gang_ctx_array_size */
+
+	memset(&mes_query_pkt, 0, sizeof(mes_query_pkt));
+
+	mes_query_pkt.header.type = MES_API_TYPE_SCHEDULER;
+	mes_query_pkt.header.opcode = MES_SCH_API_QUERY_SCHEDULER_STATUS;
+	mes_query_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
+
+	mes_query_pkt.subopcode = MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE;
+
+	/*
+	 * MES FW will write the array sizes to these GPU addresses:
+	 *   proc_ctx_array_size_addr -> uint32_t N (e.g., 50)
+	 *   gang_ctx_array_size_addr -> uint32_t M (e.g., 300)
+	 */
+	mes_query_pkt.ctx_array_size.proc_ctx_array_size_addr =
+		mes->ctx_array_size_gpu_addr;
+	mes_query_pkt.ctx_array_size.gang_ctx_array_size_addr =
+		mes->ctx_array_size_gpu_addr + sizeof(uint32_t);
+
+	mes_query_pkt.api_status.api_completion_fence_addr =
+		mes->ring[0].fence_drv.gpu_addr;
+	mes_query_pkt.api_status.api_completion_fence_value =
+		++mes->ring[0].fence_drv.sync_seq;
+
+	r = mes_v11_0_submit_pkt_and_poll_completion(mes,
+			&mes_query_pkt, sizeof(mes_query_pkt),
+			offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
+	if (r) {
+		dev_err(adev->dev,
+			"MES QUERY_SCHEDULER_STATUS (GET_CTX_ARRAY_SIZE) failed, r=%d\n", r);
+		return r;
+	}
+
+	/* MES has written the sizes - now set up bitmaps */
+	return amdgpu_mes_rs64mem_setup_bitmaps(mes);
+}
+
 static int mes_v11_0_misc_op(struct amdgpu_mes *mes,
 			     struct mes_misc_op_input *input)
 {
@@ -1907,10 +1959,33 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 	if (r)
 		goto failure;
 
+	/* Allocate GPU buffer for array size query results */
+	r = amdgpu_mes_rs64mem_init(&adev->mes);
+	if (r)
+		dev_warn(adev->dev,
+			 "RS64 local memory init failed (%d), "
+			 "falling back to system memory path\n", r);
+
 	r = mes_v11_0_set_hw_resources(&adev->mes);
+
 	if (r)
 		goto failure;
 
+	/*
+	 * QUERY_SCHEDULER_STATUS to get array sizes (N, M).
+	 * MES writes the sizes to the GPU buffer, then we allocate bitmaps.
+	 */
+	if (adev->mes.use_rs64mem) {
+		r = mes_v11_0_query_ctx_array_sizes(&adev->mes);
+		if (r) {
+			dev_warn(adev->dev,
+				 "Failed to query ctx array sizes (%d), "
+				 "disabling RS64 local memory\n", r);
+			/* Continue without optimization - not fatal */
+		}
+	}
+
+
 	if ((adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x52) {
 		r = mes_v11_0_set_hw_resources_1(&adev->mes);
 		if (r) {
@@ -1948,6 +2023,10 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 
 static int mes_v11_0_hw_fini(struct amdgpu_ip_block *ip_block)
 {
+	struct amdgpu_device *adev = ip_block->adev;
+
+	if (adev->mes.use_rs64mem)
+		amdgpu_mes_rs64mem_fini(&adev->mes);
 	return 0;
 }
 
drivers/gpu/drm/amd/include/mes_v11_api_def.h +53 -8

79 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH 3/6] drm/amdgpu/mes11: get MES process/gang contex size

Quoted 193 lines · Prike Liang

Setup the MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE MES firmware command request, and get the MES11 process/gang contex size.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c        | 79 +++++++++++++++++++
 drivers/gpu/drm/amd/include/mes_v11_api_def.h | 61 ++++++++++++--
 2 files changed, 132 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +79 -0
index c721a90051e9..195dc3b4058d 100644
@@ -832,6 +832,58 @@ static int mes_v11_0_query_sched_status(struct amdgpu_mes *mes)
                        offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
 }
 
+/**
+ * QUERY_SCHEDULER_STATUS: get proc/gang context array sizes
+ */
+static int mes_v11_0_query_ctx_array_sizes(struct amdgpu_mes *mes)
+{
+       struct amdgpu_device *adev = mes->adev;;
+       union MESAPI__QUERY_MES_STATUS mes_query_pkt;
+       int r;
+
+       if (!mes->ctx_array_size_cpu_ptr)
+               return -EINVAL;
+
+       /* Clear the output buffer */
+       mes->ctx_array_size_cpu_ptr[0] = 0;  /* proc_ctx_array_size */
+       mes->ctx_array_size_cpu_ptr[1] = 0;  /* gang_ctx_array_size */
+
+       memset(&mes_query_pkt, 0, sizeof(mes_query_pkt));
+
+       mes_query_pkt.header.type = MES_API_TYPE_SCHEDULER;
+       mes_query_pkt.header.opcode = MES_SCH_API_QUERY_SCHEDULER_STATUS;
+       mes_query_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
+
+       mes_query_pkt.subopcode = MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE;
+
+       /*
+        * MES FW will write the array sizes to these GPU addresses:
+        *   proc_ctx_array_size_addr -> uint32_t N (e.g., 50)
+        *   gang_ctx_array_size_addr -> uint32_t M (e.g., 300)
+        */
+       mes_query_pkt.ctx_array_size.proc_ctx_array_size_addr =
+               mes->ctx_array_size_gpu_addr;
+       mes_query_pkt.ctx_array_size.gang_ctx_array_size_addr =
+               mes->ctx_array_size_gpu_addr + sizeof(uint32_t);
+
+       mes_query_pkt.api_status.api_completion_fence_addr =
+               mes->ring[0].fence_drv.gpu_addr;
+       mes_query_pkt.api_status.api_completion_fence_value =
+               ++mes->ring[0].fence_drv.sync_seq;
+
+       r = mes_v11_0_submit_pkt_and_poll_completion(mes,
+                       &mes_query_pkt, sizeof(mes_query_pkt),
+                       offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
+       if (r) {
+               dev_err(adev->dev,
+                       "MES QUERY_SCHEDULER_STATUS (GET_CTX_ARRAY_SIZE) failed, r=%d\n", r);
+               return r;
+       }
+
+       /* MES has written the sizes - now set up bitmaps */
+       return amdgpu_mes_rs64mem_setup_bitmaps(mes);
+}
+
 static int mes_v11_0_misc_op(struct amdgpu_mes *mes,
                             struct mes_misc_op_input *input)
 {
@@ -1907,10 +1959,33 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
        if (r)
                goto failure;
 
+       /* Allocate GPU buffer for array size query results */
+       r = amdgpu_mes_rs64mem_init(&adev->mes);
+       if (r)
+               dev_warn(adev->dev,
+                        "RS64 local memory init failed (%d), "
+                        "falling back to system memory path\n", r);
+
        r = mes_v11_0_set_hw_resources(&adev->mes);
+
        if (r)
                goto failure;
 
+       /*
+        * QUERY_SCHEDULER_STATUS to get array sizes (N, M).
+        * MES writes the sizes to the GPU buffer, then we allocate bitmaps.
+        */
+       if (adev->mes.use_rs64mem) {
+               r = mes_v11_0_query_ctx_array_sizes(&adev->mes);
+               if (r) {
+                       dev_warn(adev->dev,
+                                "Failed to query ctx array sizes (%d), "
+                                "disabling RS64 local memory\n", r);
+                       /* Continue without optimization - not fatal */
+               }
+       }
+
+
        if ((adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x52) {
                r = mes_v11_0_set_hw_resources_1(&adev->mes);
                if (r) {
@@ -1948,6 +2023,10 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 
 static int mes_v11_0_hw_fini(struct amdgpu_ip_block *ip_block)
 {
+       struct amdgpu_device *adev = ip_block->adev;
+
+       if (adev->mes.use_rs64mem)
+               amdgpu_mes_rs64mem_fini(&adev->mes);
        return 0;
 }
 
drivers/gpu/drm/amd/include/mes_v11_api_def.h +45 -8
index b06412ac8583..dfc27be4454d 100644
@@ -329,6 +329,7 @@ union MESAPI__ADD_QUEUE {
                uint32_t                    pipe_id;
                uint32_t                    queue_id;
                uint32_t                    alignment_mode_setting;
+               uint32_t                    full_sh_mem_config_data;
                uint64_t                    unmap_flag_addr;
        };
 
@@ -358,6 +359,7 @@ union MESAPI__REMOVE_QUEUE {
                uint32_t                    tf_data;
 
                enum MES_QUEUE_TYPE         queue_type;
+               uint64_t                    timestamp;
        };
 
        uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
@@ -516,14 +518,57 @@ union MESAPI__SET_LOGGING_BUFFER {
        uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
 };
 
-union MESAPI__QUERY_MES_STATUS {
-       struct {
-               union MES_API_HEADER    header;
-               bool                    mes_healthy; /* 0 - not healthy, 1 - healthy */
-               struct MES_API_STATUS   api_status;
-       };
-
-       uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
+enum MES_API_QUERY_MES_OPCODE
+{
+    MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE,
+    MES_API_QUERY_MES__GET_CAPS = MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE,
+    MES_API_QUERY_MES__CHECK_HEALTHY,
+    MES_API_QUERY_MES__MAX,
+};
+
+enum { QUERY_MES_MAX_SIZE_IN_DWORDS = 20 };
+
+/**
+ * Obsolete
+ * to be removed once KMD stopped refering to it.
+ * use MES_API_QUERY_MES__CAPS instead
+*/
+struct MES_API_QUERY_MES__CTX_ARRAY_SIZE
+{
+    uint64_t    proc_ctx_array_size_addr;
+    uint64_t    gang_ctx_array_size_addr;
+};
+
+struct MES_API_QUERY_MES__HEALTHY_CHECK
+{
+    uint64_t    healthy_addr;
+};
+
+struct MES_API_QUERY_MES__CAPS
+{
+    uint64_t    proc_ctx_array_size_addr;
+    uint64_t    gang_ctx_array_size_addr;
+    uint64_t    features_enablement_addr;
+};
+
+union MESAPI__QUERY_MES_STATUS
+{
+    struct
+    {
+        union MES_API_HEADER            header;
+        enum MES_API_QUERY_MES_OPCODE   subopcode;
+        struct MES_API_STATUS           api_status;
+        uint64_t                        timestamp;
+        union
+        {

drivers/gpu/drm/amd/include/mes_v11_api_def.h union MESAPI__SET_LOGGING_BUFFER { · line 573

Quoted 10 lines
drivers/gpu/drm/amd/include/mes_v11_api_def.h +8 -0
+            struct MES_API_QUERY_MES__CTX_ARRAY_SIZE    ctx_array_size;
+            struct MES_API_QUERY_MES__CAPS              caps;
+            struct MES_API_QUERY_MES__HEALTHY_CHECK     healthy_check;
+            uint32_t data[QUERY_MES_MAX_SIZE_IN_DWORDS];
+        };
+    };
+
+    uint32_t max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
 };
 

Presumably we need a firmware version check to determine when this is available? What about mes 12?

Alex

Quoted 4 lines
drivers/gpu/drm/amd/include/mes_v11_api_def.h +0 -1
 union MESAPI__PROGRAM_GDS {
--

2.34.1

#4 lore raw

Re: [PATCH 3/6] drm/amdgpu/mes11: get MES process/gang contex size

AMD General

________________________________
From: Alex Deucher <[email protected]>
Sent: Friday, July 10, 2026 9:17 AM
To: Liang, Prike <[email protected]>
Cc: [email protected] <[email protected]>; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>
Subject: Re: [PATCH 3/6] drm/amdgpu/mes11: get MES process/gang contex size

Quoted 193 lines · Prike Liang

Setup the MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE MES firmware command request, and get the MES11 process/gang contex size.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c        | 79 +++++++++++++++++++
 drivers/gpu/drm/amd/include/mes_v11_api_def.h | 61 ++++++++++++--
 2 files changed, 132 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +79 -0
index c721a90051e9..195dc3b4058d 100644
@@ -832,6 +832,58 @@ static int mes_v11_0_query_sched_status(struct amdgpu_mes *mes)
                        offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
 }
 
+/**
+ * QUERY_SCHEDULER_STATUS: get proc/gang context array sizes
+ */
+static int mes_v11_0_query_ctx_array_sizes(struct amdgpu_mes *mes)
+{
+       struct amdgpu_device *adev = mes->adev;;
+       union MESAPI__QUERY_MES_STATUS mes_query_pkt;
+       int r;
+
+       if (!mes->ctx_array_size_cpu_ptr)
+               return -EINVAL;
+
+       /* Clear the output buffer */
+       mes->ctx_array_size_cpu_ptr[0] = 0;  /* proc_ctx_array_size */
+       mes->ctx_array_size_cpu_ptr[1] = 0;  /* gang_ctx_array_size */
+
+       memset(&mes_query_pkt, 0, sizeof(mes_query_pkt));
+
+       mes_query_pkt.header.type = MES_API_TYPE_SCHEDULER;
+       mes_query_pkt.header.opcode = MES_SCH_API_QUERY_SCHEDULER_STATUS;
+       mes_query_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
+
+       mes_query_pkt.subopcode = MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE;
+
+       /*
+        * MES FW will write the array sizes to these GPU addresses:
+        *   proc_ctx_array_size_addr -> uint32_t N (e.g., 50)
+        *   gang_ctx_array_size_addr -> uint32_t M (e.g., 300)
+        */
+       mes_query_pkt.ctx_array_size.proc_ctx_array_size_addr =
+               mes->ctx_array_size_gpu_addr;
+       mes_query_pkt.ctx_array_size.gang_ctx_array_size_addr =
+               mes->ctx_array_size_gpu_addr + sizeof(uint32_t);
+
+       mes_query_pkt.api_status.api_completion_fence_addr =
+               mes->ring[0].fence_drv.gpu_addr;
+       mes_query_pkt.api_status.api_completion_fence_value =
+               ++mes->ring[0].fence_drv.sync_seq;
+
+       r = mes_v11_0_submit_pkt_and_poll_completion(mes,
+                       &mes_query_pkt, sizeof(mes_query_pkt),
+                       offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
+       if (r) {
+               dev_err(adev->dev,
+                       "MES QUERY_SCHEDULER_STATUS (GET_CTX_ARRAY_SIZE) failed, r=%d\n", r);
+               return r;
+       }
+
+       /* MES has written the sizes - now set up bitmaps */
+       return amdgpu_mes_rs64mem_setup_bitmaps(mes);
+}
+
 static int mes_v11_0_misc_op(struct amdgpu_mes *mes,
                             struct mes_misc_op_input *input)
 {
@@ -1907,10 +1959,33 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
        if (r)
                goto failure;
 
+       /* Allocate GPU buffer for array size query results */
+       r = amdgpu_mes_rs64mem_init(&adev->mes);
+       if (r)
+               dev_warn(adev->dev,
+                        "RS64 local memory init failed (%d), "
+                        "falling back to system memory path\n", r);
+
        r = mes_v11_0_set_hw_resources(&adev->mes);
+
        if (r)
                goto failure;
 
+       /*
+        * QUERY_SCHEDULER_STATUS to get array sizes (N, M).
+        * MES writes the sizes to the GPU buffer, then we allocate bitmaps.
+        */
+       if (adev->mes.use_rs64mem) {
+               r = mes_v11_0_query_ctx_array_sizes(&adev->mes);
+               if (r) {
+                       dev_warn(adev->dev,
+                                "Failed to query ctx array sizes (%d), "
+                                "disabling RS64 local memory\n", r);
+                       /* Continue without optimization - not fatal */
+               }
+       }
+
+
        if ((adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x52) {
                r = mes_v11_0_set_hw_resources_1(&adev->mes);
                if (r) {
@@ -1948,6 +2023,10 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 
 static int mes_v11_0_hw_fini(struct amdgpu_ip_block *ip_block)
 {
+       struct amdgpu_device *adev = ip_block->adev;
+
+       if (adev->mes.use_rs64mem)
+               amdgpu_mes_rs64mem_fini(&adev->mes);
        return 0;
 }
 
drivers/gpu/drm/amd/include/mes_v11_api_def.h +45 -8
index b06412ac8583..dfc27be4454d 100644
@@ -329,6 +329,7 @@ union MESAPI__ADD_QUEUE {
                uint32_t                    pipe_id;
                uint32_t                    queue_id;
                uint32_t                    alignment_mode_setting;
+               uint32_t                    full_sh_mem_config_data;
                uint64_t                    unmap_flag_addr;
        };
 
@@ -358,6 +359,7 @@ union MESAPI__REMOVE_QUEUE {
                uint32_t                    tf_data;
 
                enum MES_QUEUE_TYPE         queue_type;
+               uint64_t                    timestamp;
        };
 
        uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
@@ -516,14 +518,57 @@ union MESAPI__SET_LOGGING_BUFFER {
        uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
 };
 
-union MESAPI__QUERY_MES_STATUS {
-       struct {
-               union MES_API_HEADER    header;
-               bool                    mes_healthy; /* 0 - not healthy, 1 - healthy */
-               struct MES_API_STATUS   api_status;
-       };
-
-       uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
+enum MES_API_QUERY_MES_OPCODE
+{
+    MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE,
+    MES_API_QUERY_MES__GET_CAPS = MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE,
+    MES_API_QUERY_MES__CHECK_HEALTHY,
+    MES_API_QUERY_MES__MAX,
+};
+
+enum { QUERY_MES_MAX_SIZE_IN_DWORDS = 20 };
+
+/**
+ * Obsolete
+ * to be removed once KMD stopped refering to it.
+ * use MES_API_QUERY_MES__CAPS instead
+*/
+struct MES_API_QUERY_MES__CTX_ARRAY_SIZE
+{
+    uint64_t    proc_ctx_array_size_addr;
+    uint64_t    gang_ctx_array_size_addr;
+};
+
+struct MES_API_QUERY_MES__HEALTHY_CHECK
+{
+    uint64_t    healthy_addr;
+};
+
+struct MES_API_QUERY_MES__CAPS
+{
+    uint64_t    proc_ctx_array_size_addr;
+    uint64_t    gang_ctx_array_size_addr;
+    uint64_t    features_enablement_addr;
+};
+
+union MESAPI__QUERY_MES_STATUS
+{
+    struct
+    {
+        union MES_API_HEADER            header;
+        enum MES_API_QUERY_MES_OPCODE   subopcode;
+        struct MES_API_STATUS           api_status;
+        uint64_t                        timestamp;
+        union
+        {

drivers/gpu/drm/amd/include/mes_v11_api_def.h union MESAPI__SET_LOGGING_BUFFER { · line 573

Quoted 10 lines
drivers/gpu/drm/amd/include/mes_v11_api_def.h +8 -0
+            struct MES_API_QUERY_MES__CTX_ARRAY_SIZE    ctx_array_size;
+            struct MES_API_QUERY_MES__CAPS              caps;
+            struct MES_API_QUERY_MES__HEALTHY_CHECK     healthy_check;
+            uint32_t data[QUERY_MES_MAX_SIZE_IN_DWORDS];
+        };
+    };
+
+    uint32_t max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
 };
 

Presumably we need a firmware version check to determine when this is available? What about mes 12?

This was already supported from MES a few years back, so version check is not necessary.

Alex

Quoted 4 lines
drivers/gpu/drm/amd/include/mes_v11_api_def.h +0 -1
 union MESAPI__PROGRAM_GDS {
--

2.34.1

#5 lore raw

RE: [PATCH 3/6] drm/amdgpu/mes11: get MES process/gang contex size

AMD General

Regards,
Prike

From: Chen, Michael <[email protected]>
Sent: Saturday, July 11, 2026 3:44 AM
To: Alex Deucher <[email protected]>; Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH 3/6] drm/amdgpu/mes11: get MES process/gang contex size

AMD General

________________________________
From: Alex Deucher <[email protected]<mailto:[email protected]>>
Sent: Friday, July 10, 2026 9:17 AM
To: Liang, Prike <[email protected]<mailto:[email protected]>>
Cc: [email protected]<mailto:[email protected]> <[email protected]<mailto:[email protected]>>; Deucher, Alexander <[email protected]<mailto:[email protected]>>; Koenig, Christian <[email protected]<mailto:[email protected]>>; Chen, Michael <[email protected]<mailto:[email protected]>>
Subject: Re: [PATCH 3/6] drm/amdgpu/mes11: get MES process/gang contex size

Quoted 193 lines · On Thu, Jul 9, 2026 at 3:09 AM Prike Liang…

Setup the MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE MES firmware command request, and get the MES11 process/gang contex size.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c        | 79 +++++++++++++++++++
 drivers/gpu/drm/amd/include/mes_v11_api_def.h | 61 ++++++++++++--
 2 files changed, 132 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +79 -0
index c721a90051e9..195dc3b4058d 100644
@@ -832,6 +832,58 @@ static int mes_v11_0_query_sched_status(struct amdgpu_mes *mes)
                        offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
 }
 
+/**
+ * QUERY_SCHEDULER_STATUS: get proc/gang context array sizes
+ */
+static int mes_v11_0_query_ctx_array_sizes(struct amdgpu_mes *mes)
+{
+       struct amdgpu_device *adev = mes->adev;;
+       union MESAPI__QUERY_MES_STATUS mes_query_pkt;
+       int r;
+
+       if (!mes->ctx_array_size_cpu_ptr)
+               return -EINVAL;
+
+       /* Clear the output buffer */
+       mes->ctx_array_size_cpu_ptr[0] = 0;  /* proc_ctx_array_size */
+       mes->ctx_array_size_cpu_ptr[1] = 0;  /* gang_ctx_array_size */
+
+       memset(&mes_query_pkt, 0, sizeof(mes_query_pkt));
+
+       mes_query_pkt.header.type = MES_API_TYPE_SCHEDULER;
+       mes_query_pkt.header.opcode = MES_SCH_API_QUERY_SCHEDULER_STATUS;
+       mes_query_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
+
+       mes_query_pkt.subopcode = MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE;
+
+       /*
+        * MES FW will write the array sizes to these GPU addresses:
+        *   proc_ctx_array_size_addr -> uint32_t N (e.g., 50)
+        *   gang_ctx_array_size_addr -> uint32_t M (e.g., 300)
+        */
+       mes_query_pkt.ctx_array_size.proc_ctx_array_size_addr =
+               mes->ctx_array_size_gpu_addr;
+       mes_query_pkt.ctx_array_size.gang_ctx_array_size_addr =
+               mes->ctx_array_size_gpu_addr + sizeof(uint32_t);
+
+       mes_query_pkt.api_status.api_completion_fence_addr =
+               mes->ring[0].fence_drv.gpu_addr;
+       mes_query_pkt.api_status.api_completion_fence_value =
+               ++mes->ring[0].fence_drv.sync_seq;
+
+       r = mes_v11_0_submit_pkt_and_poll_completion(mes,
+                       &mes_query_pkt, sizeof(mes_query_pkt),
+                       offsetof(union MESAPI__QUERY_MES_STATUS, api_status));
+       if (r) {
+               dev_err(adev->dev,
+                       "MES QUERY_SCHEDULER_STATUS (GET_CTX_ARRAY_SIZE) failed, r=%d\n", r);
+               return r;
+       }
+
+       /* MES has written the sizes - now set up bitmaps */
+       return amdgpu_mes_rs64mem_setup_bitmaps(mes);
+}
+
 static int mes_v11_0_misc_op(struct amdgpu_mes *mes,
                             struct mes_misc_op_input *input)
 {
@@ -1907,10 +1959,33 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
        if (r)
                goto failure;
 
+       /* Allocate GPU buffer for array size query results */
+       r = amdgpu_mes_rs64mem_init(&adev->mes);
+       if (r)
+               dev_warn(adev->dev,
+                        "RS64 local memory init failed (%d), "
+                        "falling back to system memory path\n", r);
+
        r = mes_v11_0_set_hw_resources(&adev->mes);
+
        if (r)
                goto failure;
 
+       /*
+        * QUERY_SCHEDULER_STATUS to get array sizes (N, M).
+        * MES writes the sizes to the GPU buffer, then we allocate bitmaps.
+        */
+       if (adev->mes.use_rs64mem) {
+               r = mes_v11_0_query_ctx_array_sizes(&adev->mes);
+               if (r) {
+                       dev_warn(adev->dev,
+                                "Failed to query ctx array sizes (%d), "
+                                "disabling RS64 local memory\n", r);
+                       /* Continue without optimization - not fatal */
+               }
+       }
+
+
        if ((adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x52) {
                r = mes_v11_0_set_hw_resources_1(&adev->mes);
                if (r) {
@@ -1948,6 +2023,10 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 
 static int mes_v11_0_hw_fini(struct amdgpu_ip_block *ip_block)
 {
+       struct amdgpu_device *adev = ip_block->adev;
+
+       if (adev->mes.use_rs64mem)
+               amdgpu_mes_rs64mem_fini(&adev->mes);
        return 0;
 }
 
drivers/gpu/drm/amd/include/mes_v11_api_def.h +45 -8
index b06412ac8583..dfc27be4454d 100644
@@ -329,6 +329,7 @@ union MESAPI__ADD_QUEUE {
                uint32_t                    pipe_id;
                uint32_t                    queue_id;
                uint32_t                    alignment_mode_setting;
+               uint32_t                    full_sh_mem_config_data;
                uint64_t                    unmap_flag_addr;
        };
 
@@ -358,6 +359,7 @@ union MESAPI__REMOVE_QUEUE {
                uint32_t                    tf_data;
 
                enum MES_QUEUE_TYPE         queue_type;
+               uint64_t                    timestamp;
        };
 
        uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
@@ -516,14 +518,57 @@ union MESAPI__SET_LOGGING_BUFFER {
        uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
 };
 
-union MESAPI__QUERY_MES_STATUS {
-       struct {
-               union MES_API_HEADER    header;
-               bool                    mes_healthy; /* 0 - not healthy, 1 - healthy */
-               struct MES_API_STATUS   api_status;
-       };
-
-       uint32_t        max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
+enum MES_API_QUERY_MES_OPCODE
+{
+    MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE,
+    MES_API_QUERY_MES__GET_CAPS = MES_API_QUERY_MES__GET_CTX_ARRAY_SIZE,
+    MES_API_QUERY_MES__CHECK_HEALTHY,
+    MES_API_QUERY_MES__MAX,
+};
+
+enum { QUERY_MES_MAX_SIZE_IN_DWORDS = 20 };
+
+/**
+ * Obsolete
+ * to be removed once KMD stopped refering to it.
+ * use MES_API_QUERY_MES__CAPS instead
+*/
+struct MES_API_QUERY_MES__CTX_ARRAY_SIZE
+{
+    uint64_t    proc_ctx_array_size_addr;
+    uint64_t    gang_ctx_array_size_addr;
+};
+
+struct MES_API_QUERY_MES__HEALTHY_CHECK
+{
+    uint64_t    healthy_addr;
+};
+
+struct MES_API_QUERY_MES__CAPS
+{
+    uint64_t    proc_ctx_array_size_addr;
+    uint64_t    gang_ctx_array_size_addr;
+    uint64_t    features_enablement_addr;
+};
+
+union MESAPI__QUERY_MES_STATUS
+{
+    struct
+    {
+        union MES_API_HEADER            header;
+        enum MES_API_QUERY_MES_OPCODE   subopcode;
+        struct MES_API_STATUS           api_status;
+        uint64_t                        timestamp;
+        union
+        {

drivers/gpu/drm/amd/include/mes_v11_api_def.h union MESAPI__SET_LOGGING_BUFFER { · line 573

Quoted 10 lines
drivers/gpu/drm/amd/include/mes_v11_api_def.h +8 -0
+            struct MES_API_QUERY_MES__CTX_ARRAY_SIZE    ctx_array_size;
+            struct MES_API_QUERY_MES__CAPS              caps;
+            struct MES_API_QUERY_MES__HEALTHY_CHECK     healthy_check;
+            uint32_t data[QUERY_MES_MAX_SIZE_IN_DWORDS];
+        };
+    };
+
+    uint32_t max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
 };
 

Presumably we need a firmware version check to determine when this is available? What about mes 12?

This was already supported from MES a few years back, so version check is not necessary.

Thank you for the input. Currently, I plan to enable the RS64mem on the MES11 first, and then will enable it on the other onwards MES driver.

Alex

Quoted 4 lines
drivers/gpu/drm/amd/include/mes_v11_api_def.h +0 -1
 union MESAPI__PROGRAM_GDS {
--

2.34.1

#6 lore raw

PATCH 4/6 drm/amdgpu: add mes process context alloc/free

In reply to Prike Liang

Those helpers allocates/frees slots from bitmaps for process_context_array_index processed by MES firmware.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c    | 46 ++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h    |  5 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |  2 +
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c |  9 ++++-
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c     |  1 +
 5 files changed, 62 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +46 -0
index f65f2a30154d..db9c46ece2a0 100644
@@ -1035,6 +1035,52 @@ int amdgpu_mes_rs64mem_setup_bitmaps(struct amdgpu_mes *mes)
 	return 0;
 }
 
+/**
+ * amdgpu_mes_alloc_proc_ctx_index - allocate a process context slot
+ *
+ * @mes: MES instance
+ *
+ * Returns 0 on success, -ENOSPC if all slots are used (caller should
+ * fall back to system memory path).
+ */
+int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
+				    struct amdgpu_usermode_queue *queue)
+{
+	unsigned long bit;
+
+	if (!mes->use_rs64mem || !mes->proc_ctx_bitmap)
+		return -EOPNOTSUPP;
+
+	amdgpu_mes_lock(mes);
+	bit = find_first_zero_bit(mes->proc_ctx_bitmap,
+				  mes->proc_ctx_array_size);
+	if (bit >= mes->proc_ctx_array_size) {
+		amdgpu_mes_unlock(mes);
+		return -ENOSPC;
+	}
+	set_bit(bit, mes->proc_ctx_bitmap);
+	queue->proc_ctx_array_index = (uint32_t)bit;
+	amdgpu_mes_unlock(mes);
+
+	return 0;
+}
+
+/**
+ * amdgpu_mes_free_proc_ctx_index - free a process context slot
+ */
+void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
+				    struct amdgpu_usermode_queue *queue)
+{
+	if (!mes->use_rs64mem || !mes->proc_ctx_bitmap)
+		return;
+	if (queue->proc_ctx_array_index >= mes->proc_ctx_array_size)
+		return;
+
+	amdgpu_mes_lock(mes);
+	clear_bit(queue->proc_ctx_array_index, mes->proc_ctx_bitmap);
+	amdgpu_mes_unlock(mes);
+}
+
 #if defined(CONFIG_DEBUG_FS)
 
 static int amdgpu_debugfs_mes_event_log_show(struct seq_file *m, void *unused)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +5 -0
index 5c50bc9616b7..33a426f58e72 100644
@@ -279,6 +279,7 @@ struct mes_add_queue_input {
 	uint32_t	exclusively_scheduled;
 	uint32_t	sh_mem_config_data;
 	uint32_t	vm_cntx_cntl;
+	uint32_t	process_context_array_index;
 };
 
 struct mes_remove_queue_input {
@@ -631,4 +632,8 @@ int amdgpu_mes_update_enforce_isolation(struct amdgpu_device *adev);
 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);
+void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
+				    struct amdgpu_usermode_queue *queue);
 #endif /* __AMDGPU_MES_H__ */
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index 61e5f8a06eb2..8bb0d3213abf 100644
@@ -100,6 +100,8 @@ struct amdgpu_usermode_queue {
 		} va;
 		u64 va_array[6];
 	} userq_vas;
+
+	uint32_t	proc_ctx_array_index;
 };
 
 struct amdgpu_userq_funcs {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +8 -1
index 0bcc5afc9e84..e1ff0f22a6e1 100644
@@ -121,6 +121,7 @@ static int mes_userq_map(struct amdgpu_usermode_queue *queue)
 	struct amdgpu_userq_obj *ctx = &queue->fw_obj;
 	struct amdgpu_mqd_prop *userq_props = queue->userq_prop;
 	struct mes_add_queue_input queue_input;
+	struct amdgpu_mes *mes = &adev->mes;
 	int r;
 
 	memset(&queue_input, 0x0, sizeof(struct mes_add_queue_input));
@@ -146,7 +147,10 @@ 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_lock(&adev->mes);
 	r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
 	amdgpu_mes_unlock(&adev->mes);
@@ -165,6 +169,7 @@ static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
 	struct amdgpu_device *adev = uq_mgr->adev;
 	struct mes_remove_queue_input queue_input;
 	struct amdgpu_userq_obj *ctx = &queue->fw_obj;
+	struct amdgpu_mes *mes = &adev->mes;
 	int r;
 
 	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
@@ -175,6 +180,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);
 	if (r)
 		DRM_ERROR("Failed to unmap queue in HW, err (%d)\n", r);
 	return r;
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +1 -0
index 195dc3b4058d..db09f63ec1ad 100644
@@ -335,6 +335,7 @@ static int mes_v11_0_add_hw_queue(struct amdgpu_mes *mes,
 	mes_add_queue_pkt.process_va_end = input->process_va_end;
 	mes_add_queue_pkt.process_quantum = input->process_quantum;
 	mes_add_queue_pkt.process_context_addr = input->process_context_addr;
+	mes_add_queue_pkt.process_context_array_index = input->process_context_array_index;
 	mes_add_queue_pkt.gang_quantum = input->gang_quantum;
 	mes_add_queue_pkt.gang_context_addr = input->gang_context_addr;
 	mes_add_queue_pkt.inprocess_gang_priority =
Signature · 1 line
2.34.1
#7 lore raw

PATCH 5/6 drm/amdgpu: add mes gang contex alloc/free helper

In reply to Prike Liang

Implement the MES gang contex alloc and free heplers.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c       | 41 +++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h       |  7 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  1 +
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c    |  7 +++-
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c        |  2 +
 drivers/gpu/drm/amd/include/mes_v11_api_def.h |  1 +
 6 files changed, 58 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +41 -0
index db9c46ece2a0..56671f98b052 100644
@@ -1081,6 +1081,47 @@ void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
 	amdgpu_mes_unlock(mes);
 }
 
+/**
+ * amdgpu_mes_alloc_gang_ctx_index - allocate a gang context slot
+ */
+int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
+				    struct amdgpu_usermode_queue *queue)
+{
+	unsigned long bit;
+
+	if (!mes->use_rs64mem || !mes->gang_ctx_bitmap)
+		return -EOPNOTSUPP;
+
+	amdgpu_mes_lock(mes);
+	bit = find_first_zero_bit(mes->gang_ctx_bitmap,
+				  mes->gang_ctx_array_size);
+	if (bit >= mes->gang_ctx_array_size) {
+		amdgpu_mes_unlock(mes);
+		return -ENOSPC;
+	}
+	set_bit(bit, mes->gang_ctx_bitmap);
+	queue->gang_ctx_array_index = bit;
+	amdgpu_mes_unlock(mes);
+
+	return 0;
+}
+
+/**
+ * amdgpu_mes_free_gang_ctx_index - free a gang context slot
+ */
+void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
+				    struct amdgpu_usermode_queue *queue)
+{
+	if (!mes->use_rs64mem || !mes->gang_ctx_bitmap)
+		return;
+	if (queue->gang_ctx_array_index >= mes->gang_ctx_array_size)
+		return;
+
+	amdgpu_mes_lock(mes);
+	clear_bit(queue->gang_ctx_array_index, mes->gang_ctx_bitmap);
+	amdgpu_mes_unlock(mes);
+}
+
 #if defined(CONFIG_DEBUG_FS)
 
 static int amdgpu_debugfs_mes_event_log_show(struct seq_file *m, void *unused)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +7 -0
index 33a426f58e72..c67db2d6e122 100644
@@ -181,6 +181,7 @@ struct amdgpu_mes {
 	uint32_t			proc_ctx_array_size;
 	unsigned long			*proc_ctx_bitmap;
 	uint32_t			gang_ctx_array_size;
+	uint32_t			gang_ctx_array_index;
 	unsigned long			*gang_ctx_bitmap;
 };
 
@@ -280,6 +281,7 @@ struct mes_add_queue_input {
 	uint32_t	sh_mem_config_data;
 	uint32_t	vm_cntx_cntl;
 	uint32_t	process_context_array_index;
+	uint32_t	gang_context_array_index;
 };
 
 struct mes_remove_queue_input {
@@ -288,6 +290,7 @@ struct mes_remove_queue_input {
 	uint64_t	gang_context_addr;
 	uint32_t	queue_type;
 	bool		remove_queue_after_reset;
+	uint32_t	gang_context_array_index;
 };
 
 struct mes_map_legacy_queue_input {
@@ -636,4 +639,8 @@ int amdgpu_mes_alloc_proc_ctx_index(struct amdgpu_mes *mes,
 				    struct amdgpu_usermode_queue *queue);
 void amdgpu_mes_free_proc_ctx_index(struct amdgpu_mes *mes,
 				    struct amdgpu_usermode_queue *queue);
+int amdgpu_mes_alloc_gang_ctx_index(struct amdgpu_mes *mes,
+				    struct amdgpu_usermode_queue *queue);
+void amdgpu_mes_free_gang_ctx_index(struct amdgpu_mes *mes,
+				    struct amdgpu_usermode_queue *queue);
 #endif /* __AMDGPU_MES_H__ */
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1 -0
index 8bb0d3213abf..8333bab537d4 100644
@@ -102,6 +102,7 @@ struct amdgpu_usermode_queue {
 	} userq_vas;
 
 	uint32_t	proc_ctx_array_index;
+	uint32_t	gang_ctx_array_index;
 };
 
 struct amdgpu_userq_funcs {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -1
index e1ff0f22a6e1..0dc10553dcef 100644
@@ -150,6 +150,8 @@ static int mes_userq_map(struct amdgpu_usermode_queue *queue)
 	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);
+		queue_input.gang_context_array_index = queue->gang_ctx_array_index;
 	}
 	amdgpu_mes_lock(&adev->mes);
 	r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
@@ -173,6 +175,7 @@ static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
 	int r;
 
 	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
+	queue_input.gang_context_array_index = queue->gang_ctx_array_index;
 	queue_input.doorbell_offset = queue->doorbell_index;
 	queue_input.gang_context_addr = ctx->gpu_addr;
 	queue_input.queue_type = queue->queue_type;
@@ -180,8 +183,10 @@ 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)
+	if (mes->use_rs64mem) {
 		amdgpu_mes_free_proc_ctx_index(mes, queue);
+		amdgpu_mes_free_gang_ctx_index(mes, queue);
+	}
 	if (r)
 		DRM_ERROR("Failed to unmap queue in HW, err (%d)\n", r);
 	return r;
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +2 -0
index db09f63ec1ad..c2129ea6614b 100644
@@ -338,6 +338,7 @@ static int mes_v11_0_add_hw_queue(struct amdgpu_mes *mes,
 	mes_add_queue_pkt.process_context_array_index = input->process_context_array_index;
 	mes_add_queue_pkt.gang_quantum = input->gang_quantum;
 	mes_add_queue_pkt.gang_context_addr = input->gang_context_addr;
+	mes_add_queue_pkt.gang_context_array_index = input->gang_context_array_index;
 	mes_add_queue_pkt.inprocess_gang_priority =
 		convert_to_mes_priority_level(input->inprocess_gang_priority);
 	mes_add_queue_pkt.gang_global_priority_level =
@@ -388,6 +389,7 @@ static int mes_v11_0_remove_hw_queue(struct amdgpu_mes *mes,
 
 	mes_remove_queue_pkt.doorbell_offset = input->doorbell_offset;
 	mes_remove_queue_pkt.gang_context_addr = input->gang_context_addr;
+	mes_remove_queue_pkt.gang_context_array_index = input->gang_context_array_index;
 	mes_remove_queue_pkt.queue_type =
 		convert_to_mes_queue_type(input->queue_type);
 
drivers/gpu/drm/amd/include/mes_v11_api_def.h +1 -0
index dfc27be4454d..04005a03987a 100644
@@ -360,6 +360,7 @@ union MESAPI__REMOVE_QUEUE {
 
 		enum MES_QUEUE_TYPE         queue_type;
 		uint64_t                    timestamp;
+		uint32_t                    gang_context_array_index;
 	};
 
 	uint32_t	max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
Signature · 1 line
2.34.1
#8 lore raw

PATCH 6/6 drm/amdgpu/mes11: enable MES process/gang load from MES local mem

In reply to Prike Liang

Enable the MES process/gang context load from MES local memory, this will reduce the MES firmware and driver handshake latency.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +7 -2
index c2129ea6614b..a85387122b11 100644
@@ -1012,6 +1012,7 @@ static int mes_v11_0_set_hw_resources(struct amdgpu_mes *mes)
 	mes_set_hw_res_pkt.enable_reg_active_poll = 1;
 	mes_set_hw_res_pkt.enable_level_process_quantum_check = 1;
 	mes_set_hw_res_pkt.oversubscription_timer = 50;
+	mes_set_hw_res_pkt.use_rs64mem_for_proc_gang_ctx = 1;
 
 	if (amdgpu_mes_log_enable) {
 		mes_set_hw_res_pkt.enable_mes_event_int_logging = 1;
@@ -1945,6 +1946,8 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 	if (adev->mes.ring[0].sched.ready)
 		goto out;
 
+	adev->mes.use_rs64mem = true;
+
 	if (!adev->enable_mes_kiq) {
 		if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
 			r = mes_v11_0_load_microcode(adev,
@@ -1964,11 +1967,12 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 
 	/* Allocate GPU buffer for array size query results */
 	r = amdgpu_mes_rs64mem_init(&adev->mes);
-	if (r)
+	if (r) {
 		dev_warn(adev->dev,
 			 "RS64 local memory init failed (%d), "
 			 "falling back to system memory path\n", r);
-
+		adev->mes.use_rs64mem = false;
+	}
 	r = mes_v11_0_set_hw_resources(&adev->mes);
 
 	if (r)
@@ -1985,6 +1989,7 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 				 "Failed to query ctx array sizes (%d), "
 				 "disabling RS64 local memory\n", r);
 			/* Continue without optimization - not fatal */
+			adev->mes.use_rs64mem = false;
 		}
 	}
 
Signature · 1 line
2.34.1
Reviewed-by on v1 6/6
#9 lore raw

Re: [PATCH 6/6] drm/amdgpu/mes11: enable MES process/gang load from MES local mem

AMD General

The series is

________________________________
From: Liang, Prike <[email protected]>
Sent: Thursday, July 9, 2026 2:58 AM
To: [email protected] <[email protected]>
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Chen, Michael <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH 6/6] drm/amdgpu/mes11: enable MES process/gang load from MES local mem

Enable the MES process/gang context load from MES local memory, this will reduce the MES firmware and driver handshake latency.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +7 -2
index c2129ea6614b..a85387122b11 100644
@@ -1012,6 +1012,7 @@ static int mes_v11_0_set_hw_resources(struct amdgpu_mes *mes)
         mes_set_hw_res_pkt.enable_reg_active_poll = 1;
         mes_set_hw_res_pkt.enable_level_process_quantum_check = 1;
         mes_set_hw_res_pkt.oversubscription_timer = 50;
+       mes_set_hw_res_pkt.use_rs64mem_for_proc_gang_ctx = 1;
 
         if (amdgpu_mes_log_enable) {
                 mes_set_hw_res_pkt.enable_mes_event_int_logging = 1;
@@ -1945,6 +1946,8 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
         if (adev->mes.ring[0].sched.ready)
                 goto out;
 
+       adev->mes.use_rs64mem = true;
+
         if (!adev->enable_mes_kiq) {
                 if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
                         r = mes_v11_0_load_microcode(adev,
@@ -1964,11 +1967,12 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 
         /* Allocate GPU buffer for array size query results */
         r = amdgpu_mes_rs64mem_init(&adev->mes);
-       if (r)
+       if (r) {
                 dev_warn(adev->dev,
                          "RS64 local memory init failed (%d), "
                          "falling back to system memory path\n", r);
-
+               adev->mes.use_rs64mem = false;
+       }
         r = mes_v11_0_set_hw_resources(&adev->mes);
 
         if (r)
@@ -1985,6 +1989,7 @@ static int mes_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
                                  "Failed to query ctx array sizes (%d), "
                                  "disabling RS64 local memory\n", r);
                         /* Continue without optimization - not fatal */
+                       adev->mes.use_rs64mem = false;
                 }
         }
 

--
2.34.1