Skip to contentSkip to conversation

drm/amdgpu: reinitialize MES KIQ after partition switch

PATCH In review Alex Sierra 3 participants 11 messages Oct 29 – Nov 13

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

PATCH 1/1 drm/amdgpu: reinitialize MES KIQ after partition switch

A runtime compute partition switch updates the XCP mode and IMU partition without passing through MES hw init. The MES KIQ and scheduler therefore remain programmed for the previous partition, including its cooperative mode.

After committing the new mode, tear KIQ down on every XCC, clear the KIQ and scheduler rings and their writeback pointers, and initialize KIQ again. This remaps both queues and sends SET_HW_RSRC followed by SET_HW_RSRC_1 with cooperative mode enabled for SPX, DPX, and QPX and disabled for CPX.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  5 +++
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c  | 54 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/soc_v1_0.c   |  9 +++++
 3 files changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +5 -0
index 0f368fbd2c80..5a14f4e7b64d 100644
@@ -141,6 +141,8 @@ struct amdgpu_mes {
                                                    uint32_t xcc_id);
 	int                             (*kiq_hw_fini)(struct amdgpu_device *adev,
                                                    uint32_t xcc_id);
+	/* Tear down KIQ, clear its rings, and run kiq_hw_init again. */
+	int                             (*kiq_hw_reinit)(struct amdgpu_device *adev);
 
 	/* MES doorbells */
 	uint32_t			db_start_dw_offset;
@@ -553,6 +555,9 @@ struct amdgpu_mqd {
 	(adev)->mes.kiq_hw_init((adev), (xcc_id))
 #define amdgpu_mes_kiq_hw_fini(adev, xcc_id) \
 	(adev)->mes.kiq_hw_fini((adev), (xcc_id))
+#define amdgpu_mes_kiq_hw_reinit(adev) \
+	((adev)->mes.kiq_hw_reinit ? \
+	 (adev)->mes.kiq_hw_reinit(adev) : 0)
 
 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe);
 void amdgpu_mes_validate_fw_version(struct amdgpu_device *adev);
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +54 -0

78 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +9 -0
index 506060fc5368..95ebbd4a7369 100644
@@ -1100,6 +1100,15 @@ static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
 	*num_xcps = num_xcc / num_xcc_per_xcp;
 	amdgpu_xcp_init(xcp_mgr, *num_xcps, mode);
 
+	/*
+	 * xcp_mgr->mode is the new partition now. MES KIQ was programmed for
+	 * the previous mode and is not reset by the IMU switch, so tear it
+	 * down, clear the rings, and run kiq_hw_init again.
+	 */
+	ret = amdgpu_mes_kiq_hw_reinit(adev);
+	if (ret)
+		goto out;
+
 	ret = amdgpu_xcp_post_partition_switch(xcp_mgr, flags);
 	if (!ret)
 		__soc_v1_0_update_available_partition_mode(xcp_mgr);
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

AMD General

________________________________
From: Sierra Guiza, Alejandro (Alex) <[email protected]>
Sent: Thursday, October 29, 2026 4:34 PM
To: [email protected] <[email protected]>
Cc: Kuehling, Felix <[email protected]>; Zhang, Hawking <[email protected]>; Lazar, Lijo <[email protected]>; Joshi, Mukul <[email protected]>; Chen, Michael <[email protected]>; Sierra Guiza, Alejandro (Alex) <[email protected]>
Subject: [PATCH] drm/amdgpu: reinitialize MES KIQ after partition switch

A runtime compute partition switch updates the XCP mode and IMU partition without passing through MES hw init. The MES KIQ and scheduler therefore remain programmed for the previous partition, including its cooperative mode.

After committing the new mode, tear KIQ down on every XCC, clear the KIQ and scheduler rings and their writeback pointers, and initialize KIQ again. This remaps both queues and sends SET_HW_RSRC followed by SET_HW_RSRC_1 with cooperative mode enabled for SPX, DPX, and QPX and disabled for CPX.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  5 +++
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c  | 54 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/soc_v1_0.c   |  9 +++++
 3 files changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +5 -0
index 0f368fbd2c80..5a14f4e7b64d 100644
@@ -141,6 +141,8 @@ struct amdgpu_mes {
                                                    uint32_t xcc_id);
         int                             (*kiq_hw_fini)(struct amdgpu_device *adev,
                                                    uint32_t xcc_id);
+       /* Tear down KIQ, clear its rings, and run kiq_hw_init again. */
+       int                             (*kiq_hw_reinit)(struct amdgpu_device *adev);
 
         /* MES doorbells */
         uint32_t                        db_start_dw_offset;
@@ -553,6 +555,9 @@ struct amdgpu_mqd {
         (adev)->mes.kiq_hw_init((adev), (xcc_id))
 #define amdgpu_mes_kiq_hw_fini(adev, xcc_id) \
         (adev)->mes.kiq_hw_fini((adev), (xcc_id))
+#define amdgpu_mes_kiq_hw_reinit(adev) \
+       ((adev)->mes.kiq_hw_reinit ? \
+        (adev)->mes.kiq_hw_reinit(adev) : 0)
 
 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe);
 void amdgpu_mes_validate_fw_version(struct amdgpu_device *adev);
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +54 -0

78 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +9 -0
index 506060fc5368..95ebbd4a7369 100644
@@ -1100,6 +1100,15 @@ static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
         *num_xcps = num_xcc / num_xcc_per_xcp;
         amdgpu_xcp_init(xcp_mgr, *num_xcps, mode);
 
+       /*
+        * xcp_mgr->mode is the new partition now. MES KIQ was programmed for
+        * the previous mode and is not reset by the IMU switch, so tear it
+        * down, clear the rings, and run kiq_hw_init again.
+        */
+       ret = amdgpu_mes_kiq_hw_reinit(adev);
+       if (ret)
+               goto out;
+
         ret = amdgpu_xcp_post_partition_switch(xcp_mgr, flags);
         if (!ret)
                 __soc_v1_0_update_available_partition_mode(xcp_mgr);

--
2.34.1

#2 lore raw
In reply to Alex Sierra
Quoted 92 lines · Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through MES hw init. The MES KIQ and scheduler therefore remain programmed for the previous partition, including its cooperative mode.

After committing the new mode, tear KIQ down on every XCC, clear the KIQ and scheduler rings and their writeback pointers, and initialize KIQ again. This remaps both queues and sends SET_HW_RSRC followed by SET_HW_RSRC_1 with cooperative mode enabled for SPX, DPX, and QPX and disabled for CPX.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  5 +++
  drivers/gpu/drm/amd/amdgpu/mes_v12_1.c  | 54 +++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/soc_v1_0.c   |  9 +++++
  3 files changed, 68 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +5 -0
index 0f368fbd2c80..5a14f4e7b64d 100644
@@ -141,6 +141,8 @@ struct amdgpu_mes {
                                                     uint32_t xcc_id);
  	int                             (*kiq_hw_fini)(struct amdgpu_device *adev,
                                                     uint32_t xcc_id);
+	/* Tear down KIQ, clear its rings, and run kiq_hw_init again. */
+	int                             (*kiq_hw_reinit)(struct amdgpu_device *adev);
 
  	/* MES doorbells */
  	uint32_t			db_start_dw_offset;
@@ -553,6 +555,9 @@ struct amdgpu_mqd {
  	(adev)->mes.kiq_hw_init((adev), (xcc_id))
  #define amdgpu_mes_kiq_hw_fini(adev, xcc_id) \
  	(adev)->mes.kiq_hw_fini((adev), (xcc_id))
+#define amdgpu_mes_kiq_hw_reinit(adev) \
+	((adev)->mes.kiq_hw_reinit ? \
+	 (adev)->mes.kiq_hw_reinit(adev) : 0)
 
  int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe);
  void amdgpu_mes_validate_fw_version(struct amdgpu_device *adev);
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +24 -0
index 5573fe981a22..760c93740a7e 100644
@@ -42,6 +42,7 @@ static int mes_v12_1_hw_fini(struct amdgpu_ip_block *ip_block);
  static int mes_v12_1_kiq_hw_init(struct amdgpu_device *adev, uint32_t xcc_id);
  static int mes_v12_1_kiq_hw_fini(struct amdgpu_device *adev, uint32_t xcc_id);
  static int mes_v12_1_setup_coop_mode(struct amdgpu_device *adev, int xcc_id);
+static int mes_v12_1_kiq_hw_reinit(struct amdgpu_device *adev);
 
  #define MES_EOP_SIZE   2048
  #define MES12_HUNG_DB_OFFSET_ARRAY_SIZE 8 /* [0:3] = db offset [4:7] hqd info */
@@ -674,6 +675,14 @@ static int mes_v12_1_set_hw_resources_1(struct amdgpu_mes *mes,
  			mes->shared_cmd_buf_gpu_addr[MES_PIPE_INST(master_xcc_id, pipe)];
  	}
 
+	dev_dbg(mes->adev->dev,
+		"MES(%d, %d) SET_HW_RSRC_1 coop=%u enable_coop=%u kiq_ver=0x%x mode=%s\n",
+		xcc_id, pipe, mes_set_hw_res_1_pkt.mes_coop_mode,
+		mes->enable_coop_mode,
+		mes->kiq_version & AMDGPU_MES_VERSION_MASK,
+		amdgpu_gfx_compute_mode_desc(mes->adev->xcp_mgr ?
+					     mes->adev->xcp_mgr->mode : -1));
+
  	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
  			&mes_set_hw_res_1_pkt, sizeof(mes_set_hw_res_1_pkt),
  			offsetof(union MESAPI_SET_HW_RESOURCES_1, api_status));
@@ -1530,6 +1539,7 @@ static int mes_v12_1_sw_init(struct amdgpu_ip_block *ip_block)
  	adev->mes.funcs = &mes_v12_1_funcs;
  	adev->mes.kiq_hw_init = &mes_v12_1_kiq_hw_init;
  	adev->mes.kiq_hw_fini = &mes_v12_1_kiq_hw_fini;
+	adev->mes.kiq_hw_reinit = &mes_v12_1_kiq_hw_reinit;
  	adev->mes.enable_legacy_queue_map = true;
 
  	adev->mes.event_log_size = AMDGPU_MAX_MES_PIPES *
@@ -1774,6 +1784,50 @@ static int mes_v12_1_setup_coop_mode(struct amdgpu_device *adev, int xcc_id)
  	return r;
  }
 
+/*
+ * Runtime partition switches do not pass through MES IP hw_init. Tear KIQ
+ * down, clear both MES rings, and run kiq_hw_init again. That init maps
+ * the KIQ and scheduler queues and, through setup_coop_mode, programs
+ * mes_coop_mode for the new partition.
+ *
+ * If a scheduler ring is not ready, MES has not been initialized yet and
+ * the kiq_hw_init call from CP resume covers this mode.
+ */
+static int mes_v12_1_kiq_hw_reinit(struct amdgpu_device *adev)
+{
+	int xcc_id, pipe, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+	int r;
+

drivers/gpu/drm/amd/amdgpu/mes_v12_1.c static int mes_v12_1_setup_coop_mode(struct amdgpu_device *adev, int xcc_id) · line 1810

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +10 -0
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		if (!adev->mes.ring[MES_PIPE_INST(xcc_id,
+						  AMDGPU_MES_SCHED_PIPE)].sched.ready)
+			return 0;
+	}
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		r = mes_v12_1_kiq_hw_fini(adev, xcc_id);
+		if (r)
+			return r;

There is a amdgpu_xcp_pre_partition_switch. In case you want to do this before the actual switch. Then the corresponding post switch. It may need a check like mes_enabled or similar.

Thanks,
Lijo

Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +20 -0
+
+		/*
+		 * The firmware restarts with its read pointer at zero, so
+		 * leaving the old contents and writeback pointers in place
+		 * would make it replay stale packets.
+		 */
+		for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++)
+			amdgpu_ring_clear_ring_and_ptrs(
+				&adev->mes.ring[MES_PIPE_INST(xcc_id, pipe)]);
+	}
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		r = mes_v12_1_kiq_hw_init(adev, xcc_id);
+		if (r)
+			return r;
+	}
+
+	return 0;
+}
+
  static int mes_v12_1_xcc_hw_init(struct amdgpu_ip_block *ip_block, int xcc_id)
  {
  	int r;
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +9 -0
index 506060fc5368..95ebbd4a7369 100644
@@ -1100,6 +1100,15 @@ static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
  	*num_xcps = num_xcc / num_xcc_per_xcp;
  	amdgpu_xcp_init(xcp_mgr, *num_xcps, mode);
 
+	/*
+	 * xcp_mgr->mode is the new partition now. MES KIQ was programmed for
+	 * the previous mode and is not reset by the IMU switch, so tear it
+	 * down, clear the rings, and run kiq_hw_init again.
+	 */
+	ret = amdgpu_mes_kiq_hw_reinit(adev);
+	if (ret)
+		goto out;
+
  	ret = amdgpu_xcp_post_partition_switch(xcp_mgr, flags);
  	if (!ret)
  		__soc_v1_0_update_available_partition_mode(xcp_mgr);
#3 lore raw

PATCH 1/1 drm/amdgpu: reinitialize MES KIQ across a partition switch

In reply to Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through MES hw init. The MES KIQ and scheduler therefore remain programmed for the previous partition, including its cooperative mode.

Stop KIQ in amdgpu_xcp_pre_partition_switch, while the old partition is still in place. After the new mode is committed, start KIQ again from amdgpu_xcp_post_partition_switch, before KFD recreates queues. That init remaps both queues and sends SET_HW_RSRC followed by SET_HW_RSRC_1 with cooperative mode enabled for SPX, DPX, and QPX and disabled for CPX.

The calls are gated on enable_mes_kiq. If the switch fails after KIQ was stopped, start it again for the mode the switch settles on.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h | 14 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 51 +++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c  | 72 +++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +14 -0
index 652cd0950453..cd020959192c 100644
@@ -141,6 +141,14 @@ struct amdgpu_mes {
                                                    uint32_t xcc_id);
 	int                             (*kiq_hw_fini)(struct amdgpu_device *adev,
                                                    uint32_t xcc_id);
+	/*
+	 * Partition switch: stop KIQ before the hardware switch, start it
+	 * again once the new mode is committed. kiq_hw_stopped is set by
+	 * the pre callback and cleared when post runs.
+	 */
+	int                             (*kiq_hw_pre_switch)(struct amdgpu_device *adev);
+	int                             (*kiq_hw_post_switch)(struct amdgpu_device *adev);
+	bool                            kiq_hw_stopped;
 
 	/* MES doorbells */
 	uint32_t			db_start_dw_offset;
@@ -572,6 +580,12 @@ struct amdgpu_mqd {
 	(adev)->mes.kiq_hw_init((adev), (xcc_id))
 #define amdgpu_mes_kiq_hw_fini(adev, xcc_id) \
 	(adev)->mes.kiq_hw_fini((adev), (xcc_id))
+#define amdgpu_mes_kiq_hw_pre_switch(adev) \
+	((adev)->mes.kiq_hw_pre_switch ? \
+	 (adev)->mes.kiq_hw_pre_switch(adev) : 0)
+#define amdgpu_mes_kiq_hw_post_switch(adev) \
+	((adev)->mes.kiq_hw_post_switch ? \
+	 (adev)->mes.kiq_hw_post_switch(adev) : 0)
 
 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe);
 void amdgpu_mes_validate_fw_version(struct amdgpu_device *adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +45 -6

78 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +72 -0

96 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH 1/1 drm/amdgpu: reinitialize MES KIQ across a partition switch

In reply to Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through MES hw init. The MES KIQ and scheduler therefore remain programmed for the previous partition, including its cooperative mode.

Stop KIQ in amdgpu_xcp_pre_partition_switch, while the old partition is still in place. After the new mode is committed, start KIQ again from amdgpu_xcp_post_partition_switch, before KFD recreates queues. That init remaps both queues and sends SET_HW_RSRC followed by SET_HW_RSRC_1 with cooperative mode enabled for SPX, DPX, and QPX and disabled for CPX.

The calls are gated on enable_mes_kiq. If the switch fails after KIQ was stopped, start it again for the mode the switch settles on.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h | 14 +++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 51 +++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c  | 72 +++++++++++++++++++++++++
 3 files changed, 131 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +14 -0
index 652cd0950453..cd020959192c 100644
@@ -141,6 +141,14 @@ struct amdgpu_mes {
                                                    uint32_t xcc_id);
 	int                             (*kiq_hw_fini)(struct amdgpu_device *adev,
                                                    uint32_t xcc_id);
+	/*
+	 * Partition switch: stop KIQ before the hardware switch, start it
+	 * again once the new mode is committed. kiq_hw_stopped is set by
+	 * the pre callback and cleared when post runs.
+	 */
+	int                             (*kiq_hw_pre_switch)(struct amdgpu_device *adev);
+	int                             (*kiq_hw_post_switch)(struct amdgpu_device *adev);
+	bool                            kiq_hw_stopped;
 
 	/* MES doorbells */
 	uint32_t			db_start_dw_offset;
@@ -572,6 +580,12 @@ struct amdgpu_mqd {
 	(adev)->mes.kiq_hw_init((adev), (xcc_id))
 #define amdgpu_mes_kiq_hw_fini(adev, xcc_id) \
 	(adev)->mes.kiq_hw_fini((adev), (xcc_id))
+#define amdgpu_mes_kiq_hw_pre_switch(adev) \
+	((adev)->mes.kiq_hw_pre_switch ? \
+	 (adev)->mes.kiq_hw_pre_switch(adev) : 0)
+#define amdgpu_mes_kiq_hw_post_switch(adev) \
+	((adev)->mes.kiq_hw_post_switch ? \
+	 (adev)->mes.kiq_hw_post_switch(adev) : 0)
 
 int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe);
 void amdgpu_mes_validate_fw_version(struct amdgpu_device *adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +45 -6

78 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +72 -0

96 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH] drm/amdgpu: reinitialize MES KIQ across a partition switch

Quoted 27 lines · Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through MES hw init. The MES KIQ and scheduler therefore remain programmed for the previous partition, including its cooperative mode.

Stop KIQ in amdgpu_xcp_pre_partition_switch, while the old partition is still in place. After the new mode is committed, start KIQ again from amdgpu_xcp_post_partition_switch, before KFD recreates queues. That init remaps both queues and sends SET_HW_RSRC followed by SET_HW_RSRC_1 with cooperative mode enabled for SPX, DPX, and QPX and disabled for CPX.

The calls are gated on enable_mes_kiq. If the switch fails after KIQ was stopped, start it again for the mode the switch settles on.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h | 14 +++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c | 51 +++++++++++++++---
  drivers/gpu/drm/amd/amdgpu/mes_v12_1.c  | 72 +++++++++++++++++++++++++
  3 files changed, 131 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +0 -0
index 652cd0950453..cd020959192c 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h struct amdgpu_mes { · line 150

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +7 -0
@@ -141,6 +141,14 @@ struct amdgpu_mes {
                                                     uint32_t xcc_id);
  	int                             (*kiq_hw_fini)(struct amdgpu_device *adev,
                                                     uint32_t xcc_id);
+	/*
+	 * Partition switch: stop KIQ before the hardware switch, start it
+	 * again once the new mode is committed. kiq_hw_stopped is set by
+	 * the pre callback and cleared when post runs.
+	 */
+	int                             (*kiq_hw_pre_switch)(struct amdgpu_device *adev);
+	int                             (*kiq_hw_post_switch)(struct amdgpu_device *adev);

Suggest to keep it generic like pre_px_switch and post_px_switch. Other preparations specific to a MES situation (enable_mes_kie etc.) may be handled by the IP specific callback.

Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +7 -0
+	bool                            kiq_hw_stopped;
 
  	/* MES doorbells */
  	uint32_t			db_start_dw_offset;
@@ -572,6 +580,12 @@ struct amdgpu_mqd {
  	(adev)->mes.kiq_hw_init((adev), (xcc_id))
  #define amdgpu_mes_kiq_hw_fini(adev, xcc_id) \
  	(adev)->mes.kiq_hw_fini((adev), (xcc_id))
+#define amdgpu_mes_kiq_hw_pre_switch(adev) \
+	((adev)->mes.kiq_hw_pre_switch ? \
+	 (adev)->mes.kiq_hw_pre_switch(adev) : 0)
+#define amdgpu_mes_kiq_hw_post_switch(adev) \
+	((adev)->mes.kiq_hw_post_switch ? \
+	 (adev)->mes.kiq_hw_post_switch(adev) : 0)
 
  int amdgpu_mes_init_microcode(struct amdgpu_device *adev, int pipe);
  void amdgpu_mes_validate_fw_version(struct amdgpu_device *adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +0 -0
index c29a60d6f0a6..4bdd9b60a35e 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c static int __amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, · line 220

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +6 -0
@@ -212,6 +212,20 @@ static int __amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
  		else
  			xcp_mgr->mode = curr_mode;
 
+		/*
+		 * pre_partition_switch may have halted MES KIQ. This
+		 * failure returned before post_partition_switch, so
+		 * start KIQ again for the mode settled above.
+		 */
+		if (xcp_mgr->adev->mes.kiq_hw_stopped) {

Ex: this type of check will be inside mes post_switch logic.

Other than that, the patch looks good.

Thanks,
Lijo

Quoted 174 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +39 -6
+			int kiq_ret = amdgpu_mes_kiq_hw_post_switch(xcp_mgr->adev);
+
+			if (kiq_ret)
+				dev_err(xcp_mgr->adev->dev,
+					"Failed to restore MES KIQ after partition switch failure: %d\n",
+					kiq_ret);
+		}
+
  		goto out;
  	}
  	amdgpu_xcp_sysfs_entries_update(xcp_mgr);
@@ -721,27 +735,52 @@ void amdgpu_xcp_update_supported_modes(struct amdgpu_xcp_mgr *xcp_mgr)
 
  int amdgpu_xcp_pre_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
  {
+	struct amdgpu_device *adev = xcp_mgr->adev;
  	int ret = 0;
 
  	/* TODO:
  	 * Stop user queues and threads, and make sure GPU is empty of work.
  	 */
 
-	if (flags & AMDGPU_XCP_OPS_KFD)
-		ret = amdgpu_amdkfd_prepare_partition_switch(xcp_mgr->adev);
+	if (flags & AMDGPU_XCP_OPS_KFD) {
+		ret = amdgpu_amdkfd_prepare_partition_switch(adev);
+		if (ret)
+			return ret;
+	}
 
-	return ret;
+	/*
+	 * Halt MES KIQ while the old partition is still programmed.
+	 * Devices without MES KIQ leave the callback NULL.
+	 */
+	if (adev->enable_mes_kiq) {
+		ret = amdgpu_mes_kiq_hw_pre_switch(adev);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
  }
 
  int amdgpu_xcp_post_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
  {
+	struct amdgpu_device *adev = xcp_mgr->adev;
  	int ret = 0;
 
+	/*
+	 * The new partition mode is already committed. Program KIQ
+	 * before KFD recreates queues.
+	 */
+	if (adev->enable_mes_kiq) {
+		ret = amdgpu_mes_kiq_hw_post_switch(adev);
+		if (ret)
+			return ret;
+	}
+
  	if (flags & AMDGPU_XCP_OPS_KFD) {
-		amdgpu_amdkfd_device_probe(xcp_mgr->adev);
-		amdgpu_amdkfd_device_init(xcp_mgr->adev);
+		amdgpu_amdkfd_device_probe(adev);
+		amdgpu_amdkfd_device_init(adev);
  		/* If KFD init failed, return failure */
-		if (!xcp_mgr->adev->kfd.init_complete)
+		if (!adev->kfd.init_complete)
  			ret = -EIO;
  	}
 
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +72 -0
index ec595449963e..0957e56bb069 100644
@@ -46,6 +46,8 @@ static int mes_v12_1_setup_coop_mode(struct amdgpu_device *adev, int xcc_id);
  static int mes_v12_1_enable_dbgext_irq_xcc(struct amdgpu_mes *mes,
  					   bool enable, u32 xcc_id);
  static int mes_v12_1_enable_dbgext_irq(struct amdgpu_mes *mes, bool enable);
+static int mes_v12_1_kiq_hw_pre_switch(struct amdgpu_device *adev);
+static int mes_v12_1_kiq_hw_post_switch(struct amdgpu_device *adev);
 
  #define MES_EOP_SIZE   2048
  #define MES12_HUNG_DB_OFFSET_ARRAY_SIZE 8 /* [0:3] = db offset [4:7] hqd info */
@@ -689,6 +691,14 @@ static int mes_v12_1_set_hw_resources_1(struct amdgpu_mes *mes,
  			mes->shared_cmd_buf_gpu_addr[MES_PIPE_INST(master_xcc_id, pipe)];
  	}
 
+	dev_dbg(mes->adev->dev,
+		"MES(%d, %d) SET_HW_RSRC_1 coop=%u enable_coop=%u kiq_ver=0x%x mode=%s\n",
+		xcc_id, pipe, mes_set_hw_res_1_pkt.mes_coop_mode,
+		mes->enable_coop_mode,
+		mes->kiq_version & AMDGPU_MES_VERSION_MASK,
+		amdgpu_gfx_compute_mode_desc(mes->adev->xcp_mgr ?
+					     mes->adev->xcp_mgr->mode : -1));
+
  	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
  			&mes_set_hw_res_1_pkt, sizeof(mes_set_hw_res_1_pkt),
  			offsetof(union MESAPI_SET_HW_RESOURCES_1, api_status));
@@ -1611,6 +1621,8 @@ static int mes_v12_1_sw_init(struct amdgpu_ip_block *ip_block)
  	adev->mes.funcs = &mes_v12_1_funcs;
  	adev->mes.kiq_hw_init = &mes_v12_1_kiq_hw_init;
  	adev->mes.kiq_hw_fini = &mes_v12_1_kiq_hw_fini;
+	adev->mes.kiq_hw_pre_switch = &mes_v12_1_kiq_hw_pre_switch;
+	adev->mes.kiq_hw_post_switch = &mes_v12_1_kiq_hw_post_switch;
  	adev->mes.enable_legacy_queue_map = true;
 
  	/*
@@ -1881,6 +1893,66 @@ static int mes_v12_1_setup_coop_mode(struct amdgpu_device *adev, int xcc_id)
  	return r;
  }
 
+/*
+ * Runtime partition switches do not pass through MES IP hw_init. Stop KIQ
+ * on the pre-switch side, while the old partition is still in place, and
+ * start it again once the new mode is committed. The post path maps the
+ * KIQ and scheduler queues and, through setup_coop_mode, programs
+ * mes_coop_mode for that mode.
+ *
+ * If a scheduler ring is not ready, MES has not been initialized yet and
+ * the kiq_hw_init call from CP resume covers this mode.
+ */
+static int mes_v12_1_kiq_hw_pre_switch(struct amdgpu_device *adev)
+{
+	int xcc_id, pipe, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+	int r;
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		if (!adev->mes.ring[MES_PIPE_INST(xcc_id,
+						  AMDGPU_MES_SCHED_PIPE)].sched.ready)
+			return 0;
+	}
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		r = mes_v12_1_kiq_hw_fini(adev, xcc_id);
+		if (r)
+			return r;
+
+		/*
+		 * The firmware restarts with its read pointer at zero, so
+		 * leaving the old contents and writeback pointers in place
+		 * would make it replay stale packets.
+		 */
+		for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++)
+			amdgpu_ring_clear_ring_and_ptrs(
+				&adev->mes.ring[MES_PIPE_INST(xcc_id, pipe)]);
+	}
+
+	adev->mes.kiq_hw_stopped = true;
+	return 0;
+}
+
+static int mes_v12_1_kiq_hw_post_switch(struct amdgpu_device *adev)
+{
+	int xcc_id, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+	int r;
+
+	if (!adev->mes.kiq_hw_stopped)
+		return 0;
+
+	/* Cleared first so a failed init is not run a second time. */
+	adev->mes.kiq_hw_stopped = false;
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		r = mes_v12_1_kiq_hw_init(adev, xcc_id);
+		if (r)
+			return r;
+	}
+
+	return 0;
+}
+
  static int mes_v12_1_xcc_hw_init(struct amdgpu_ip_block *ip_block, int xcc_id)
  {
  	int r;
#6 lore raw

PATCH 1/1 drm/amdgpu: reinitialize MES KIQ across a partition switch

In reply to Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through MES hw init. The MES KIQ and scheduler therefore remain programmed for the previous partition, including its cooperative mode.

Add optional pre_px_switch and post_px_switch hooks to amd_ip_funcs. amdgpu_xcp_pre_partition_switch runs the pre hooks while the old partition is still in place. amdgpu_xcp_post_partition_switch runs the post hooks after the new mode is committed and before KFD recreates queues. A failed switch also runs the post hooks, so each IP can undo whatever its pre hook stopped.

MES v12.1 stops KIQ in its pre hook, tearing it down on every XCC and clearing both MES rings. Its post hook runs kiq_hw_init again, which remaps both queues and sends SET_HW_RSRC followed by SET_HW_RSRC_1 with cooperative mode enabled for SPX, DPX, and QPX and disabled for CPX. Both hooks return early unless enable_mes_kiq is set. If the MES scheduler ring is not ready yet, the pre hook does nothing, and CP resume programs KIQ for the current mode.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h  |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c  | 70 ++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c   | 75 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/include/amd_shared.h |  5 ++
 4 files changed, 146 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +2 -0
index 652cd0950453..4e386b33a815 100644
@@ -141,6 +141,8 @@ struct amdgpu_mes {
                                                    uint32_t xcc_id);
 	int                             (*kiq_hw_fini)(struct amdgpu_device *adev,
                                                    uint32_t xcc_id);
+	/* Set by MES pre_px_switch, cleared by MES post_px_switch. */
+	bool                            kiq_hw_stopped;
 
 	/* MES doorbells */
 	uint32_t			db_start_dw_offset;
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +64 -6

103 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +75 -0

93 lines not on this page · raw message

drivers/gpu/drm/amd/include/amd_shared.h +5 -0
index e81ce9d79009..db24bacb8e0e 100644
@@ -446,6 +446,9 @@ enum amd_dpm_forced_level;
  * @get_clockgating_state: get current clockgating status
  * @dump_ip_state: dump the IP state of the ASIC during a gpu hang
  * @print_ip_state: print the IP state in devcoredump for each IP of the ASIC
+ * @pre_px_switch: IP-specific prep before a compute partition switch - Optional
+ * @post_px_switch: IP-specific programming after a compute partition switch,
+ *                  or after a failed switch - Optional
  *
  * These hooks provide an interface for controlling the operational state
  * of IP blocks. After acquiring a list of IP blocks for the GPU in use,
@@ -476,6 +479,8 @@ struct amd_ip_funcs {
 	void (*get_clockgating_state)(struct amdgpu_ip_block *ip_block, u64 *flags);
 	void (*dump_ip_state)(struct amdgpu_ip_block *ip_block);
 	void (*print_ip_state)(struct amdgpu_ip_block *ip_block, struct drm_printer *p);
+	int (*pre_px_switch)(struct amdgpu_ip_block *ip_block);
+	int (*post_px_switch)(struct amdgpu_ip_block *ip_block);
 };
 
 
Signature · 1 line
2.34.1
#7 lore raw

PATCH 1/1 drm/amdgpu: reinitialize GFX compute across a partition switch

In reply to Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through GFX hw_fini and hw_init. The MEC, KCQs, and MES KIQ therefore remain programmed for the previous partition, including its cooperative mode.

Add optional pre_px_switch and post_px_switch hooks to amd_ip_funcs. amdgpu_xcp_pre_partition_switch runs the pre hooks while the old partition is still in place. amdgpu_xcp_post_partition_switch runs the post hooks after the new mode is committed and before KFD recreates queues. A failed switch also runs the post hooks, so each IP can undo whatever its pre hook stopped.

GFX v12.1 uses its regular per-XCC teardown in the pre hook. This disables KCQs, tears down MES KIQ, and disables the MEC compute pipes. Clear both MES rings and their writeback pointers before the switch so firmware cannot replay stale packets when it restarts.

The post hook runs the matching CP resume path, which enables MEC, initializes MES KIQ for the new cooperative mode, restores KCQs, and tests the compute rings. If the MES scheduler rings are not ready, the pre hook does nothing and the normal CP resume initializes the compute path.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h  |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c  | 70 ++++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c   | 54 ++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c   |  8 +++
 drivers/gpu/drm/amd/include/amd_shared.h |  5 ++
 5 files changed, 132 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +1 -0
index 41471bc560cd..df6e9da1a11d 100644
@@ -532,6 +532,7 @@ struct amdgpu_gfx {
 	uint16_t 			xcc_mask;
 	uint32_t			num_xcc_per_xcp;
 	struct mutex			partition_mutex;
+	bool				px_switch_stopped;
 	bool				mcbp; /* mid command buffer preemption */
 
 	/* IP reg dump */
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +64 -6

103 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c +54 -0

66 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +8 -0
index 3585897c817d..a8153c504c41 100644
@@ -679,6 +679,14 @@ static int mes_v12_1_set_hw_resources_1(struct amdgpu_mes *mes,
 			mes->shared_cmd_buf_gpu_addr[MES_PIPE_INST(master_xcc_id, pipe)];
 	}
 
+	dev_dbg(mes->adev->dev,
+		"MES(%d, %d) SET_HW_RSRC_1 coop=%u enable_coop=%u kiq_ver=0x%x mode=%s\n",
+		xcc_id, pipe, mes_set_hw_res_1_pkt.mes_coop_mode,
+		mes->enable_coop_mode,
+		mes->kiq_version & AMDGPU_MES_VERSION_MASK,
+		amdgpu_gfx_compute_mode_desc(mes->adev->xcp_mgr ?
+					     mes->adev->xcp_mgr->mode : -1));
+
 	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
 			&mes_set_hw_res_1_pkt, sizeof(mes_set_hw_res_1_pkt),
 			offsetof(union MESAPI_SET_HW_RESOURCES_1, api_status));
drivers/gpu/drm/amd/include/amd_shared.h +5 -0
index e81ce9d79009..db24bacb8e0e 100644
@@ -446,6 +446,9 @@ enum amd_dpm_forced_level;
  * @get_clockgating_state: get current clockgating status
  * @dump_ip_state: dump the IP state of the ASIC during a gpu hang
  * @print_ip_state: print the IP state in devcoredump for each IP of the ASIC
+ * @pre_px_switch: IP-specific prep before a compute partition switch - Optional
+ * @post_px_switch: IP-specific programming after a compute partition switch,
+ *                  or after a failed switch - Optional
  *
  * These hooks provide an interface for controlling the operational state
  * of IP blocks. After acquiring a list of IP blocks for the GPU in use,
@@ -476,6 +479,8 @@ struct amd_ip_funcs {
 	void (*get_clockgating_state)(struct amdgpu_ip_block *ip_block, u64 *flags);
 	void (*dump_ip_state)(struct amdgpu_ip_block *ip_block);
 	void (*print_ip_state)(struct amdgpu_ip_block *ip_block, struct drm_printer *p);
+	int (*pre_px_switch)(struct amdgpu_ip_block *ip_block);
+	int (*post_px_switch)(struct amdgpu_ip_block *ip_block);
 };
 
 
Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH] drm/amdgpu: reinitialize GFX compute across a partition switch

Quoted 114 lines · Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through GFX hw_fini and hw_init. The MEC, KCQs, and MES KIQ therefore remain programmed for the previous partition, including its cooperative mode.

Add optional pre_px_switch and post_px_switch hooks to amd_ip_funcs. amdgpu_xcp_pre_partition_switch runs the pre hooks while the old partition is still in place. amdgpu_xcp_post_partition_switch runs the post hooks after the new mode is committed and before KFD recreates queues. A failed switch also runs the post hooks, so each IP can undo whatever its pre hook stopped.

GFX v12.1 uses its regular per-XCC teardown in the pre hook. This disables KCQs, tears down MES KIQ, and disables the MEC compute pipes. Clear both MES rings and their writeback pointers before the switch so firmware cannot replay stale packets when it restarts.

The post hook runs the matching CP resume path, which enables MEC, initializes MES KIQ for the new cooperative mode, restores KCQs, and tests the compute rings. If the MES scheduler rings are not ready, the pre hook does nothing and the normal CP resume initializes the compute path.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h  |  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c  | 70 ++++++++++++++++++++++--
  drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c   | 54 ++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/mes_v12_1.c   |  8 +++
  drivers/gpu/drm/amd/include/amd_shared.h |  5 ++
  5 files changed, 132 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +1 -0
index 41471bc560cd..df6e9da1a11d 100644
@@ -532,6 +532,7 @@ struct amdgpu_gfx {
  	uint16_t 			xcc_mask;
  	uint32_t			num_xcc_per_xcp;
  	struct mutex			partition_mutex;
+	bool				px_switch_stopped;
  	bool				mcbp; /* mid command buffer preemption */
 
  	/* IP reg dump */
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +40 -2
index 9f4158e06e60..77d9782cf799 100644
@@ -213,6 +213,30 @@ int amdgpu_xcp_init(struct amdgpu_xcp_mgr *xcp_mgr, int num_xcps, int mode)
  	return 0;
  }
 
+static int amdgpu_xcp_run_px_switch(struct amdgpu_device *adev, bool post)
+{
+	struct amdgpu_ip_block *ip_block;
+	int (*px_switch)(struct amdgpu_ip_block *ip_block);
+	int i, ret;
+
+	for (i = 0; i < adev->num_ip_blocks; i++) {
+		ip_block = &adev->ip_blocks[i];
+		if (!ip_block->version || !ip_block->version->funcs)
+			continue;
+
+		px_switch = post ? ip_block->version->funcs->post_px_switch :
+				   ip_block->version->funcs->pre_px_switch;
+		if (!px_switch)
+			continue;
+
+		ret = px_switch(ip_block);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
  static int __amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
  					      int mode)
  {
@@ -237,6 +261,19 @@ static int __amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
  		else
  			xcp_mgr->mode = curr_mode;
 
+		/*
+		 * pre_px_switch may have stopped an IP. Each post_px_switch
+		 * decides what to bring back for the mode settled above.
+		 */
+		{
+			int px_ret = amdgpu_xcp_run_px_switch(xcp_mgr->adev, true);
+
+			if (px_ret)
+				dev_err(xcp_mgr->adev->dev,
+					"Failed to restore IP state after partition switch failure: %d\n",
+					px_ret);
+		}
+
  		goto out;
  	}
  	amdgpu_xcp_sysfs_entries_update(xcp_mgr);
@@ -855,27 +892,48 @@ void amdgpu_xcp_update_supported_modes(struct amdgpu_xcp_mgr *xcp_mgr)
 
  int amdgpu_xcp_pre_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
  {
+	struct amdgpu_device *adev = xcp_mgr->adev;
  	int ret = 0;
 
  	/* TODO:
  	 * Stop user queues and threads, and make sure GPU is empty of work.
  	 */
 
-	if (flags & AMDGPU_XCP_OPS_KFD)
-		ret = amdgpu_amdkfd_prepare_partition_switch(xcp_mgr->adev);
+	if (flags & AMDGPU_XCP_OPS_KFD) {
+		ret = amdgpu_amdkfd_prepare_partition_switch(adev);

drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c void amdgpu_xcp_update_supported_modes(struct amdgpu_xcp_mgr *xcp_mgr) · line 912

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +8 -1
+		if (ret)
+			return ret;
+	}
 
-	return ret;
+	/*
+	 * IP-specific prep while the old partition is still programmed.
+	 * enable_mes_kiq and similar checks belong in the IP callback.
+	 */
+	ret = amdgpu_xcp_run_px_switch(adev, false);

There is no need to run pre/post preparation when partition switch is done for the first time (unknown mode) or while coming out of a reset (IP blocks will be reinitialized). May add a flag to skip those two cases.

Thanks,
Lijo

Quoted 144 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +16 -3
+	if (ret)
+		return ret;
+
+	return 0;
  }
 
  int amdgpu_xcp_post_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
  {
+	struct amdgpu_device *adev = xcp_mgr->adev;
  	int ret = 0;
 
+	/*
+	 * The new partition mode is already committed. Let each IP
+	 * program itself before KFD recreates queues.
+	 */
+	ret = amdgpu_xcp_run_px_switch(adev, true);
+	if (ret)
+		return ret;
+
  	if (flags & AMDGPU_XCP_OPS_KFD) {
-		amdgpu_amdkfd_device_probe(xcp_mgr->adev);
-		amdgpu_amdkfd_device_init(xcp_mgr->adev);
+		amdgpu_amdkfd_device_probe(adev);
+		amdgpu_amdkfd_device_init(adev);
  		/* If KFD init failed, return failure */
-		if (!xcp_mgr->adev->kfd.init_complete)
+		if (!adev->kfd.init_complete)
  			ret = -EIO;
  	}
 
drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c +54 -0
index fee6b1b433cd..3592b95371b2 100644
@@ -4041,6 +4041,58 @@ static void gfx_v12_1_ring_emit_cleaner_shader(struct amdgpu_ring *ring)
  	amdgpu_ring_write(ring, 0);  /* RESERVED field, programmed to zero */
  }
 
+/*
+ * A runtime partition switch does not pass through GFX hw_fini/hw_init.
+ * Stop the GFX compute path while the old partition is still programmed,
+ * then restore it after the new mode is committed.
+ */
+static int gfx_v12_1_pre_px_switch(struct amdgpu_ip_block *ip_block)
+{
+	struct amdgpu_device *adev = ip_block->adev;
+	int xcc_id, pipe, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+
+	if (!adev->enable_mes_kiq)
+		return 0;
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		if (!adev->mes.ring[MES_PIPE_INST(xcc_id,
+						  AMDGPU_MES_SCHED_PIPE)].sched.ready)
+			return 0;
+	}
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++)
+		gfx_v12_1_xcc_fini(adev, xcc_id);
+
+	/*
+	 * MES firmware restarts with its read pointer at zero. Clear both
+	 * rings and their writeback pointers before bringing the GFX compute
+	 * path back up, otherwise firmware can replay stale packets.
+	 */
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++)
+			amdgpu_ring_clear_ring_and_ptrs(
+				&adev->mes.ring[MES_PIPE_INST(xcc_id, pipe)]);
+	}
+
+	adev->gfx.px_switch_stopped = true;
+	return 0;
+}
+
+static int gfx_v12_1_post_px_switch(struct amdgpu_ip_block *ip_block)
+{
+	struct amdgpu_device *adev = ip_block->adev;
+	uint16_t xcc_mask;
+
+	if (!adev->enable_mes_kiq || !adev->gfx.px_switch_stopped)
+		return 0;
+
+	/* Cleared first so a failed resume is not run a second time. */
+	adev->gfx.px_switch_stopped = false;
+	xcc_mask = GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0);
+
+	return gfx_v12_1_xcc_cp_resume(adev, xcc_mask);
+}
+
  static const struct amd_ip_funcs gfx_v12_1_ip_funcs = {
  	.name = "gfx_v12_1",
  	.early_init = gfx_v12_1_early_init,
@@ -4056,6 +4108,8 @@ static const struct amd_ip_funcs gfx_v12_1_ip_funcs = {
  	.get_clockgating_state = gfx_v12_1_get_clockgating_state,
  	.dump_ip_state = gfx_v12_1_ip_dump,
  	.print_ip_state = gfx_v12_1_ip_print,
+	.pre_px_switch = gfx_v12_1_pre_px_switch,
+	.post_px_switch = gfx_v12_1_post_px_switch,
  };
 
  static const struct amdgpu_ring_funcs gfx_v12_1_ring_funcs_compute = {
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +8 -0
index 3585897c817d..a8153c504c41 100644
@@ -679,6 +679,14 @@ static int mes_v12_1_set_hw_resources_1(struct amdgpu_mes *mes,
  			mes->shared_cmd_buf_gpu_addr[MES_PIPE_INST(master_xcc_id, pipe)];
  	}
 
+	dev_dbg(mes->adev->dev,
+		"MES(%d, %d) SET_HW_RSRC_1 coop=%u enable_coop=%u kiq_ver=0x%x mode=%s\n",
+		xcc_id, pipe, mes_set_hw_res_1_pkt.mes_coop_mode,
+		mes->enable_coop_mode,
+		mes->kiq_version & AMDGPU_MES_VERSION_MASK,
+		amdgpu_gfx_compute_mode_desc(mes->adev->xcp_mgr ?
+					     mes->adev->xcp_mgr->mode : -1));
+
  	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
  			&mes_set_hw_res_1_pkt, sizeof(mes_set_hw_res_1_pkt),
  			offsetof(union MESAPI_SET_HW_RESOURCES_1, api_status));
drivers/gpu/drm/amd/include/amd_shared.h +5 -0
index e81ce9d79009..db24bacb8e0e 100644
@@ -446,6 +446,9 @@ enum amd_dpm_forced_level;
   * @get_clockgating_state: get current clockgating status
   * @dump_ip_state: dump the IP state of the ASIC during a gpu hang
   * @print_ip_state: print the IP state in devcoredump for each IP of the ASIC
+ * @pre_px_switch: IP-specific prep before a compute partition switch - Optional
+ * @post_px_switch: IP-specific programming after a compute partition switch,
+ *                  or after a failed switch - Optional
   *
   * These hooks provide an interface for controlling the operational state
   * of IP blocks. After acquiring a list of IP blocks for the GPU in use,
@@ -476,6 +479,8 @@ struct amd_ip_funcs {
  	void (*get_clockgating_state)(struct amdgpu_ip_block *ip_block, u64 *flags);
  	void (*dump_ip_state)(struct amdgpu_ip_block *ip_block);
  	void (*print_ip_state)(struct amdgpu_ip_block *ip_block, struct drm_printer *p);
+	int (*pre_px_switch)(struct amdgpu_ip_block *ip_block);
+	int (*post_px_switch)(struct amdgpu_ip_block *ip_block);
  };
 
 
#9 lore raw

PATCH 1/1 drm/amdgpu: reinitialize GFX compute across a partition switch

In reply to Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through GFX hw_fini and hw_init. The MEC, KCQs, and MES KIQ therefore remain programmed for the previous partition, including its cooperative mode.

Add optional pre_px_switch and post_px_switch hooks to amd_ip_funcs. amdgpu_xcp_pre_partition_switch runs the pre hooks while the old partition is still in place. amdgpu_xcp_post_partition_switch runs the post hooks after the new mode is committed and before KFD recreates queues.

Run the IP hooks only for a runtime switch with a known current mode. Skip them during initial mode setup, reset or suspend recovery, and amdgpu_xcp_restore_partition_mode, where the regular IP initialization path restores the hardware. If an eligible runtime switch fails, run the post hooks to undo any state stopped by its pre hooks.

GFX v12.1 uses its regular per-XCC teardown in the pre hook. This disables KCQs, tears down MES KIQ, and disables the MEC compute pipes. Clear both MES rings and their writeback pointers before the switch so firmware cannot replay stale packets when it restarts.

The post hook runs the matching CP resume path, which enables MEC, initializes MES KIQ for the new cooperative mode, restores KCQs, and tests the compute rings. Track whether the pre hook stopped GFX so the post hook only resumes a path that was actually torn down.

Add debug output for SET_HW_RSRC_1 to expose the cooperative-mode selection used while validating the new partition state.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h    |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c    | 88 +++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h    |  3 +-
 drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c |  4 +-
 drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c     | 54 +++++++++++++
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c     |  8 ++
 drivers/gpu/drm/amd/amdgpu/soc_v1_0.c      |  4 +-
 drivers/gpu/drm/amd/include/amd_shared.h   |  5 ++
 8 files changed, 152 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +1 -0
index 41471bc560cd..df6e9da1a11d 100644
@@ -532,6 +532,7 @@ struct amdgpu_gfx {
 	uint16_t 			xcc_mask;
 	uint32_t			num_xcc_per_xcp;
 	struct mutex			partition_mutex;
+	bool				px_switch_stopped;
 	bool				mcbp; /* mid command buffer preemption */
 
 	/* IP reg dump */
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +78 -10

143 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h +2 -1
index 6b3fdafc8b02..b4c0f80488dc 100644
@@ -41,6 +41,7 @@
 #define AMDGPU_XCP_NO_PARTITION (~0)
 
 #define AMDGPU_XCP_OPS_KFD	(1 << 0)
+#define AMDGPU_XCP_OPS_IP	BIT(1)
 
 #define XCP_INST_MASK(num_inst, xcp_id)		\
 	(num_inst ? GENMASK(num_inst - 1, 0) << (xcp_id * num_inst) : 0)
@@ -150,7 +151,7 @@ struct amdgpu_xcp_mgr {
 
 struct amdgpu_xcp_mgr_funcs {
 	int (*switch_partition_mode)(struct amdgpu_xcp_mgr *xcp_mgr, int mode,
-				     int *num_xcps);
+				     int *num_xcps, u32 flags);
 	int (*query_partition_mode)(struct amdgpu_xcp_mgr *xcp_mgr);
 	int (*get_ip_details)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
 			      enum AMDGPU_XCP_IP_BLOCK ip_id,
drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +2 -2
index 05e4edf72e4d..ec8b0c8f5e6d 100644
@@ -364,11 +364,11 @@ static void __aqua_vanjaram_update_available_partition_mode(struct amdgpu_xcp_mg
 }
 
 static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
-					       int mode, int *num_xcps)
+					       int mode, int *num_xcps,
+					       u32 flags)
 {
 	int num_xcc_per_xcp, num_xcc, ret;
 	struct amdgpu_device *adev;
-	u32 flags = 0;
 
 	adev = xcp_mgr->adev;
 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c +54 -0

66 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +8 -0
index 3585897c817d..a8153c504c41 100644
@@ -679,6 +679,14 @@ static int mes_v12_1_set_hw_resources_1(struct amdgpu_mes *mes,
 			mes->shared_cmd_buf_gpu_addr[MES_PIPE_INST(master_xcc_id, pipe)];
 	}
 
+	dev_dbg(mes->adev->dev,
+		"MES(%d, %d) SET_HW_RSRC_1 coop=%u enable_coop=%u kiq_ver=0x%x mode=%s\n",
+		xcc_id, pipe, mes_set_hw_res_1_pkt.mes_coop_mode,
+		mes->enable_coop_mode,
+		mes->kiq_version & AMDGPU_MES_VERSION_MASK,
+		amdgpu_gfx_compute_mode_desc(mes->adev->xcp_mgr ?
+					     mes->adev->xcp_mgr->mode : -1));
+
 	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
 			&mes_set_hw_res_1_pkt, sizeof(mes_set_hw_res_1_pkt),
 			offsetof(union MESAPI_SET_HW_RESOURCES_1, api_status));
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +2 -2
index b782fe2841b5..ad9913723cfa 100644
@@ -1166,11 +1166,11 @@ static void __soc_v1_0_update_available_partition_mode(struct amdgpu_xcp_mgr *xc
 }
 
 static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
-					  int mode, int *num_xcps)
+					  int mode, int *num_xcps,
+					  u32 flags)
 {
 	int num_xcc_per_xcp, num_xcc, ret;
 	struct amdgpu_device *adev;
-	u32 flags = 0;
 
 	adev = xcp_mgr->adev;
 	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
drivers/gpu/drm/amd/include/amd_shared.h +5 -0
index e81ce9d79009..db24bacb8e0e 100644
@@ -446,6 +446,9 @@ enum amd_dpm_forced_level;
  * @get_clockgating_state: get current clockgating status
  * @dump_ip_state: dump the IP state of the ASIC during a gpu hang
  * @print_ip_state: print the IP state in devcoredump for each IP of the ASIC
+ * @pre_px_switch: IP-specific prep before a compute partition switch - Optional
+ * @post_px_switch: IP-specific programming after a compute partition switch,
+ *                  or after a failed switch - Optional
  *
  * These hooks provide an interface for controlling the operational state
  * of IP blocks. After acquiring a list of IP blocks for the GPU in use,
@@ -476,6 +479,8 @@ struct amd_ip_funcs {
 	void (*get_clockgating_state)(struct amdgpu_ip_block *ip_block, u64 *flags);
 	void (*dump_ip_state)(struct amdgpu_ip_block *ip_block);
 	void (*print_ip_state)(struct amdgpu_ip_block *ip_block, struct drm_printer *p);
+	int (*pre_px_switch)(struct amdgpu_ip_block *ip_block);
+	int (*post_px_switch)(struct amdgpu_ip_block *ip_block);
 };
 
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#10 lore raw

Re: [PATCH] drm/amdgpu: reinitialize GFX compute across a partition switch

Quoted 27 lines · Alex Sierra

A runtime compute partition switch updates the XCP mode and IMU partition without passing through GFX hw_fini and hw_init. The MEC, KCQs, and MES KIQ therefore remain programmed for the previous partition, including its cooperative mode.

Add optional pre_px_switch and post_px_switch hooks to amd_ip_funcs. amdgpu_xcp_pre_partition_switch runs the pre hooks while the old partition is still in place. amdgpu_xcp_post_partition_switch runs the post hooks after the new mode is committed and before KFD recreates queues.

Run the IP hooks only for a runtime switch with a known current mode. Skip them during initial mode setup, reset or suspend recovery, and amdgpu_xcp_restore_partition_mode, where the regular IP initialization path restores the hardware. If an eligible runtime switch fails, run the post hooks to undo any state stopped by its pre hooks.

GFX v12.1 uses its regular per-XCC teardown in the pre hook. This disables KCQs, tears down MES KIQ, and disables the MEC compute pipes. Clear both MES rings and their writeback pointers before the switch so firmware cannot replay stale packets when it restarts.

The post hook runs the matching CP resume path, which enables MEC, initializes MES KIQ for the new cooperative mode, restores KCQs, and tests the compute rings. Track whether the pre hook stopped GFX so the post hook only resumes a path that was actually torn down.

Quoted 4 lines

Add debug output for SET_HW_RSRC_1 to expose the cooperative-mode selection used while validating the new partition state.

Thanks,
Lijo

Quoted 347 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h    |  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c    | 88 +++++++++++++++++++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h    |  3 +-
  drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c |  4 +-
  drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c     | 54 +++++++++++++
  drivers/gpu/drm/amd/amdgpu/mes_v12_1.c     |  8 ++
  drivers/gpu/drm/amd/amdgpu/soc_v1_0.c      |  4 +-
  drivers/gpu/drm/amd/include/amd_shared.h   |  5 ++
  8 files changed, 152 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +1 -0
index 41471bc560cd..df6e9da1a11d 100644
@@ -532,6 +532,7 @@ struct amdgpu_gfx {
  	uint16_t 			xcc_mask;
  	uint32_t			num_xcc_per_xcp;
  	struct mutex			partition_mutex;
+	bool				px_switch_stopped;
  	bool				mcbp; /* mid command buffer preemption */
 
  	/* IP reg dump */
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c +78 -10
index 9f4158e06e60..158faa362606 100644
@@ -213,8 +213,32 @@ int amdgpu_xcp_init(struct amdgpu_xcp_mgr *xcp_mgr, int num_xcps, int mode)
  	return 0;
  }
 
+static int amdgpu_xcp_run_px_switch(struct amdgpu_device *adev, bool post)
+{
+	struct amdgpu_ip_block *ip_block;
+	int (*px_switch)(struct amdgpu_ip_block *ip_block);
+	int i, ret;
+
+	for (i = 0; i < adev->num_ip_blocks; i++) {
+		ip_block = &adev->ip_blocks[i];
+		if (!ip_block->version || !ip_block->version->funcs)
+			continue;
+
+		px_switch = post ? ip_block->version->funcs->post_px_switch :
+				   ip_block->version->funcs->pre_px_switch;
+		if (!px_switch)
+			continue;
+
+		ret = px_switch(ip_block);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
  static int __amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
-					      int mode)
+					      int mode, u32 flags)
  {
  	int ret, curr_mode, num_xcps = 0;
 
@@ -224,10 +248,15 @@ static int __amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
  	mutex_lock(&xcp_mgr->xcp_lock);
 
  	curr_mode = xcp_mgr->mode;
+	if (amdgpu_in_reset(xcp_mgr->adev) || xcp_mgr->adev->in_suspend ||
+	    curr_mode == AMDGPU_UNKNOWN_COMPUTE_PARTITION_MODE)
+		flags &= ~AMDGPU_XCP_OPS_IP;
+
  	/* State set to transient mode */
  	xcp_mgr->mode = AMDGPU_XCP_MODE_TRANS;
 
-	ret = xcp_mgr->funcs->switch_partition_mode(xcp_mgr, mode, &num_xcps);
+	ret = xcp_mgr->funcs->switch_partition_mode(xcp_mgr, mode, &num_xcps,
+						    flags);
 
  	if (ret) {
  		/* Failed, get whatever mode it's at now */
@@ -237,6 +266,19 @@ static int __amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
  		else
  			xcp_mgr->mode = curr_mode;
 
+		/*
+		 * pre_px_switch may have stopped an IP. Each post_px_switch
+		 * decides what to bring back for the mode settled above.
+		 */
+		if (flags & AMDGPU_XCP_OPS_IP) {
+			int px_ret = amdgpu_xcp_run_px_switch(xcp_mgr->adev, true);
+
+			if (px_ret)
+				dev_err(xcp_mgr->adev->dev,
+					"Failed to restore IP state after partition switch failure: %d\n",
+					px_ret);
+		}
+
  		goto out;
  	}
  	amdgpu_xcp_sysfs_entries_update(xcp_mgr);
@@ -254,7 +296,8 @@ int amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, int mode)
  	if (xcp_mgr->mode == mode)
  		return 0;
 
-	return __amdgpu_xcp_switch_partition_mode(xcp_mgr, mode);
+	return __amdgpu_xcp_switch_partition_mode(xcp_mgr, mode,
+						  AMDGPU_XCP_OPS_IP);
  }
 
  int amdgpu_xcp_restore_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr)
@@ -262,7 +305,7 @@ int amdgpu_xcp_restore_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr)
  	if (!xcp_mgr || xcp_mgr->mode == AMDGPU_XCP_MODE_NONE)
  		return 0;
 
-	return __amdgpu_xcp_switch_partition_mode(xcp_mgr, xcp_mgr->mode);
+	return __amdgpu_xcp_switch_partition_mode(xcp_mgr, xcp_mgr->mode, 0);
  }
 
  static bool __amdgpu_xcp_is_cached_mode_valid(struct amdgpu_xcp_mgr *xcp_mgr)
@@ -855,27 +898,52 @@ void amdgpu_xcp_update_supported_modes(struct amdgpu_xcp_mgr *xcp_mgr)
 
  int amdgpu_xcp_pre_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
  {
+	struct amdgpu_device *adev = xcp_mgr->adev;
  	int ret = 0;
 
  	/* TODO:
  	 * Stop user queues and threads, and make sure GPU is empty of work.
  	 */
 
-	if (flags & AMDGPU_XCP_OPS_KFD)
-		ret = amdgpu_amdkfd_prepare_partition_switch(xcp_mgr->adev);
+	if (flags & AMDGPU_XCP_OPS_KFD) {
+		ret = amdgpu_amdkfd_prepare_partition_switch(adev);
+		if (ret)
+			return ret;
+	}
 
-	return ret;
+	/*
+	 * IP-specific prep while the old partition is still programmed.
+	 * enable_mes_kiq and similar checks belong in the IP callback.
+	 */
+	if (flags & AMDGPU_XCP_OPS_IP) {
+		ret = amdgpu_xcp_run_px_switch(adev, false);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
  }
 
  int amdgpu_xcp_post_partition_switch(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
  {
+	struct amdgpu_device *adev = xcp_mgr->adev;
  	int ret = 0;
 
+	/*
+	 * The new partition mode is already committed. Let each IP
+	 * program itself before KFD recreates queues.
+	 */
+	if (flags & AMDGPU_XCP_OPS_IP) {
+		ret = amdgpu_xcp_run_px_switch(adev, true);
+		if (ret)
+			return ret;
+	}
+
  	if (flags & AMDGPU_XCP_OPS_KFD) {
-		amdgpu_amdkfd_device_probe(xcp_mgr->adev);
-		amdgpu_amdkfd_device_init(xcp_mgr->adev);
+		amdgpu_amdkfd_device_probe(adev);
+		amdgpu_amdkfd_device_init(adev);
  		/* If KFD init failed, return failure */
-		if (!xcp_mgr->adev->kfd.init_complete)
+		if (!adev->kfd.init_complete)
  			ret = -EIO;
  	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.h +2 -1
index 6b3fdafc8b02..b4c0f80488dc 100644
@@ -41,6 +41,7 @@
  #define AMDGPU_XCP_NO_PARTITION (~0)
 
  #define AMDGPU_XCP_OPS_KFD	(1 << 0)
+#define AMDGPU_XCP_OPS_IP	BIT(1)
 
  #define XCP_INST_MASK(num_inst, xcp_id)		\
  	(num_inst ? GENMASK(num_inst - 1, 0) << (xcp_id * num_inst) : 0)
@@ -150,7 +151,7 @@ struct amdgpu_xcp_mgr {
 
  struct amdgpu_xcp_mgr_funcs {
  	int (*switch_partition_mode)(struct amdgpu_xcp_mgr *xcp_mgr, int mode,
-				     int *num_xcps);
+				     int *num_xcps, u32 flags);
  	int (*query_partition_mode)(struct amdgpu_xcp_mgr *xcp_mgr);
  	int (*get_ip_details)(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
  			      enum AMDGPU_XCP_IP_BLOCK ip_id,
drivers/gpu/drm/amd/amdgpu/aqua_vanjaram.c +2 -2
index 05e4edf72e4d..ec8b0c8f5e6d 100644
@@ -364,11 +364,11 @@ static void __aqua_vanjaram_update_available_partition_mode(struct amdgpu_xcp_mg
  }
 
  static int aqua_vanjaram_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
-					       int mode, int *num_xcps)
+					       int mode, int *num_xcps,
+					       u32 flags)
  {
  	int num_xcc_per_xcp, num_xcc, ret;
  	struct amdgpu_device *adev;
-	u32 flags = 0;
 
  	adev = xcp_mgr->adev;
  	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
drivers/gpu/drm/amd/amdgpu/gfx_v12_1.c +54 -0
index fee6b1b433cd..3592b95371b2 100644
@@ -4041,6 +4041,58 @@ static void gfx_v12_1_ring_emit_cleaner_shader(struct amdgpu_ring *ring)
  	amdgpu_ring_write(ring, 0);  /* RESERVED field, programmed to zero */
  }
 
+/*
+ * A runtime partition switch does not pass through GFX hw_fini/hw_init.
+ * Stop the GFX compute path while the old partition is still programmed,
+ * then restore it after the new mode is committed.
+ */
+static int gfx_v12_1_pre_px_switch(struct amdgpu_ip_block *ip_block)
+{
+	struct amdgpu_device *adev = ip_block->adev;
+	int xcc_id, pipe, num_xcc = NUM_XCC(adev->gfx.xcc_mask);
+
+	if (!adev->enable_mes_kiq)
+		return 0;
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		if (!adev->mes.ring[MES_PIPE_INST(xcc_id,
+						  AMDGPU_MES_SCHED_PIPE)].sched.ready)
+			return 0;
+	}
+
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++)
+		gfx_v12_1_xcc_fini(adev, xcc_id);
+
+	/*
+	 * MES firmware restarts with its read pointer at zero. Clear both
+	 * rings and their writeback pointers before bringing the GFX compute
+	 * path back up, otherwise firmware can replay stale packets.
+	 */
+	for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
+		for (pipe = 0; pipe < AMDGPU_MAX_MES_PIPES; pipe++)
+			amdgpu_ring_clear_ring_and_ptrs(
+				&adev->mes.ring[MES_PIPE_INST(xcc_id, pipe)]);
+	}
+
+	adev->gfx.px_switch_stopped = true;
+	return 0;
+}
+
+static int gfx_v12_1_post_px_switch(struct amdgpu_ip_block *ip_block)
+{
+	struct amdgpu_device *adev = ip_block->adev;
+	uint16_t xcc_mask;
+
+	if (!adev->enable_mes_kiq || !adev->gfx.px_switch_stopped)
+		return 0;
+
+	/* Cleared first so a failed resume is not run a second time. */
+	adev->gfx.px_switch_stopped = false;
+	xcc_mask = GENMASK(NUM_XCC(adev->gfx.xcc_mask) - 1, 0);
+
+	return gfx_v12_1_xcc_cp_resume(adev, xcc_mask);
+}
+
  static const struct amd_ip_funcs gfx_v12_1_ip_funcs = {
  	.name = "gfx_v12_1",
  	.early_init = gfx_v12_1_early_init,
@@ -4056,6 +4108,8 @@ static const struct amd_ip_funcs gfx_v12_1_ip_funcs = {
  	.get_clockgating_state = gfx_v12_1_get_clockgating_state,
  	.dump_ip_state = gfx_v12_1_ip_dump,
  	.print_ip_state = gfx_v12_1_ip_print,
+	.pre_px_switch = gfx_v12_1_pre_px_switch,
+	.post_px_switch = gfx_v12_1_post_px_switch,
  };
 
  static const struct amdgpu_ring_funcs gfx_v12_1_ring_funcs_compute = {
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +8 -0
index 3585897c817d..a8153c504c41 100644
@@ -679,6 +679,14 @@ static int mes_v12_1_set_hw_resources_1(struct amdgpu_mes *mes,
  			mes->shared_cmd_buf_gpu_addr[MES_PIPE_INST(master_xcc_id, pipe)];
  	}
 
+	dev_dbg(mes->adev->dev,
+		"MES(%d, %d) SET_HW_RSRC_1 coop=%u enable_coop=%u kiq_ver=0x%x mode=%s\n",
+		xcc_id, pipe, mes_set_hw_res_1_pkt.mes_coop_mode,
+		mes->enable_coop_mode,
+		mes->kiq_version & AMDGPU_MES_VERSION_MASK,
+		amdgpu_gfx_compute_mode_desc(mes->adev->xcp_mgr ?
+					     mes->adev->xcp_mgr->mode : -1));
+
  	return mes_v12_1_submit_pkt_and_poll_completion(mes, xcc_id, pipe,
  			&mes_set_hw_res_1_pkt, sizeof(mes_set_hw_res_1_pkt),
  			offsetof(union MESAPI_SET_HW_RESOURCES_1, api_status));
drivers/gpu/drm/amd/amdgpu/soc_v1_0.c +2 -2
index b782fe2841b5..ad9913723cfa 100644
@@ -1166,11 +1166,11 @@ static void __soc_v1_0_update_available_partition_mode(struct amdgpu_xcp_mgr *xc
  }
 
  static int soc_v1_0_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr,
-					  int mode, int *num_xcps)
+					  int mode, int *num_xcps,
+					  u32 flags)
  {
  	int num_xcc_per_xcp, num_xcc, ret;
  	struct amdgpu_device *adev;
-	u32 flags = 0;
 
  	adev = xcp_mgr->adev;
  	num_xcc = NUM_XCC(adev->gfx.xcc_mask);
drivers/gpu/drm/amd/include/amd_shared.h +5 -0
index e81ce9d79009..db24bacb8e0e 100644
@@ -446,6 +446,9 @@ enum amd_dpm_forced_level;
   * @get_clockgating_state: get current clockgating status
   * @dump_ip_state: dump the IP state of the ASIC during a gpu hang
   * @print_ip_state: print the IP state in devcoredump for each IP of the ASIC
+ * @pre_px_switch: IP-specific prep before a compute partition switch - Optional
+ * @post_px_switch: IP-specific programming after a compute partition switch,
+ *                  or after a failed switch - Optional
   *
   * These hooks provide an interface for controlling the operational state
   * of IP blocks. After acquiring a list of IP blocks for the GPU in use,
@@ -476,6 +479,8 @@ struct amd_ip_funcs {
  	void (*get_clockgating_state)(struct amdgpu_ip_block *ip_block, u64 *flags);
  	void (*dump_ip_state)(struct amdgpu_ip_block *ip_block);
  	void (*print_ip_state)(struct amdgpu_ip_block *ip_block, struct drm_printer *p);
+	int (*pre_px_switch)(struct amdgpu_ip_block *ip_block);
+	int (*post_px_switch)(struct amdgpu_ip_block *ip_block);
  };