All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heng Zhou <Heng.Zhou@amd.com>
To: <amd-gfx@lists.freedesktop.org>
Cc: <Lijo.Lazar@amd.com>, <Christian.Koenig@amd.com>,
	<Emily.Deng@amd.com>, <Victor.Zhao@amd.com>,
	<Felix.Kuehling@amd.com>, <phasta@kernel.org>,
	<HaiJun.Chang@amd.com>, <Heng.Zhou@amd.com>, <James.Zhu@amd.com>
Subject: [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation
Date: Mon, 23 Nov 2026 17:56:05 +0800	[thread overview]
Message-ID: <20261123095605.162656-1-Heng.Zhou@amd.com> (raw)

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


             reply	other threads:[~2026-11-23  9:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  9:56 Heng Zhou [this message]
2026-11-23 10:21 ` [PATCH] drm/amdkfd: rebuild CWSR trap buffers after hibernation sashiko-bot
2026-12-01  7:44 ` Heng Zhou
2026-12-01 18:56   ` Felix Kuehling

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123095605.162656-1-Heng.Zhou@amd.com \
    --to=heng.zhou@amd.com \
    --cc=Christian.Koenig@amd.com \
    --cc=Emily.Deng@amd.com \
    --cc=Felix.Kuehling@amd.com \
    --cc=HaiJun.Chang@amd.com \
    --cc=James.Zhu@amd.com \
    --cc=Lijo.Lazar@amd.com \
    --cc=Victor.Zhao@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=phasta@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.