AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation
@ 2026-11-23  9:56 Heng Zhou
  2026-11-23 10:21 ` sashiko-bot
  2026-12-01  7:44 ` Heng Zhou
  0 siblings, 2 replies; 4+ messages in thread
From: Heng Zhou @ 2026-11-23  9:56 UTC (permalink / raw)
  To: amd-gfx
  Cc: Lijo.Lazar, Christian.Koenig, Emily.Deng, Victor.Zhao,
	Felix.Kuehling, phasta, HaiJun.Chang, Heng.Zhou, James.Zhu

The CWSR trap buffer can reside in pinned VRAM, which is skipped by
TTM eviction during hibernation. Restoring the process GPUVM mappings
does not restore the buffer contents. This can lead to SQ errors and
failed queue preemption after resume.

Keep the second-level TBA/TMA and trap flags in system memory. Rebuild
the buffer from the trap ISA and saved TMA values and flush HDP before
restoring process queues.

Signed-off-by: Heng Zhou <Heng.Zhou@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h    |  7 ++++++
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 31 ++++++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
index d8631847f0eb..a719c3be0db1 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
@@ -723,6 +723,13 @@ struct qcm_process_device {
 	uint64_t cwsr_base;
 	uint64_t tba_addr;
 	uint64_t tma_addr;
+	/*
+	 * CPU copies of the second-level TBA/TMA and trap flags.
+	 * Keep them in sync with the first-level TMA for hibernation restore.
+	 */
+	uint64_t cwsr_second_level_tba;
+	uint64_t cwsr_second_level_tma;
+	uint64_t cwsr_tma_flags;
 
 	/* IB memory */
 	struct kgd_mem *ib_mem;
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index dd938788bdd9..307851d3d882 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
@@ -1590,6 +1590,8 @@ void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
 		/* KFD trap handler is bound, record as second-level TBA/TMA
 		 * in first-level TMA. First-level trap will jump to second.
 		 */
+		qpd->cwsr_second_level_tba = tba_addr;
+		qpd->cwsr_second_level_tma = tma_addr;
 		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET,
 			     uint64_t, tba_addr);
 		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET + sizeof(uint64_t),
@@ -1660,12 +1662,40 @@ void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd,
 				     bool enabled)
 {
 	if (!iosys_map_is_null(&qpd->cwsr_map)) {
+		qpd->cwsr_tma_flags = enabled;
 		iosys_map_wr(&qpd->cwsr_map,
 			     KFD_CWSR_TMA_OFFSET + 2 * sizeof(uint64_t),
 			     uint64_t, enabled);
 	}
 }
 
+static void kfd_process_rebuild_cwsr(struct kfd_process *p)
+{
+	struct kfd_process_device *pdd;
+	struct qcm_process_device *qpd;
+	int i;
+
+	for (i = 0; i < p->n_pdds; i++) {
+		pdd = p->pdds[i];
+		qpd = &pdd->qpd;
+
+		if (!pdd->dev->adev->in_s4 || !qpd->cwsr_base ||
+		    iosys_map_is_null(&qpd->cwsr_map))
+			continue;
+
+		iosys_map_memset(&qpd->cwsr_map, 0, 0, KFD_CWSR_TBA_TMA_SIZE);
+		iosys_map_memcpy_to(&qpd->cwsr_map, 0, pdd->dev->kfd->cwsr_isa,
+				    pdd->dev->kfd->cwsr_isa_size);
+		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET,
+			     uint64_t, qpd->cwsr_second_level_tba);
+		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET + sizeof(uint64_t),
+			     uint64_t, qpd->cwsr_second_level_tma);
+		iosys_map_wr(&qpd->cwsr_map, KFD_CWSR_TMA_OFFSET + 2 * sizeof(uint64_t),
+			     uint64_t, qpd->cwsr_tma_flags);
+		amdgpu_device_flush_hdp(pdd->dev->adev, NULL);
+	}
+}
+
 /*
  * On return the kfd_process is fully operational and will be freed when the
  * mm is released
@@ -2301,6 +2331,7 @@ int kfd_resume_all_processes(void)
 	int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
 
 	hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
+		kfd_process_rebuild_cwsr(p);
 		if (restore_process_helper(p)) {
 			pr_err("Restore process pid %d failed during resume\n",
 			      p->lead_thread->pid);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-01 18:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  9:56 [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation Heng Zhou
2026-11-23 10:21 ` sashiko-bot
2026-12-01  7:44 ` Heng Zhou
2026-12-01 18:56   ` Felix Kuehling

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox