soc/amd: fully commonize clear_tvalid

Signed-off-by: Felix Held <felix-coreboot@felixheld.de>
Change-Id: I90526a566a5fbc19a7368f90421067a6c716614e
Reviewed-on: https://review.coreboot.org/c/coreboot/+/50466
Reviewed-by: Raul Rangel <rrangel@chromium.org>
Reviewed-by: Marshall Dawson <marshalldawson3rd@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/soc/amd/common/block/cpu/noncar/memmap.c b/src/soc/amd/common/block/cpu/noncar/memmap.c
index 470b517..7a7f8ef 100644
--- a/src/soc/amd/common/block/cpu/noncar/memmap.c
+++ b/src/soc/amd/common/block/cpu/noncar/memmap.c
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
 #include <amdblocks/memmap.h>
+#include <amdblocks/smm.h>
 #include <console/console.h>
 #include <cbmem.h>
 #include <cpu/amd/msr.h>
@@ -33,28 +34,6 @@
 	return e;
 }
 
-/*
- * For data stored in TSEG, ensure TValid is clear so R/W access can reach
- * the DRAM when not in SMM.
- */
-static void clear_tvalid(void)
-{
-	msr_t hwcr = rdmsr(HWCR_MSR);
-	msr_t mask = rdmsr(SMM_MASK_MSR);
-	int tvalid = !!(mask.lo & SMM_TSEG_VALID);
-
-	if (hwcr.lo & SMM_LOCK) {
-		if (!tvalid) /* not valid but locked means still accessible */
-			return;
-
-		printk(BIOS_ERR, "Error: can't clear TValid, already locked\n");
-		return;
-	}
-
-	mask.lo &= ~SMM_TSEG_VALID;
-	wrmsr(SMM_MASK_MSR, mask);
-}
-
 void smm_region(uintptr_t *start, size_t *size)
 {
 	static int once;
diff --git a/src/soc/amd/common/block/cpu/smm/Makefile.inc b/src/soc/amd/common/block/cpu/smm/Makefile.inc
index df54d93..3008868 100644
--- a/src/soc/amd/common/block/cpu/smm/Makefile.inc
+++ b/src/soc/amd/common/block/cpu/smm/Makefile.inc
@@ -2,7 +2,10 @@
 
 subdirs-y += ../../../../../../cpu/x86/smm
 
+romstage-y += smm_helper.c
+postcar-y += smm_helper.c
 ramstage-y += smm_relocate.c
+ramstage-y += smm_helper.c
 smm-y += smi_handler.c
 
 endif # CONFIG_SOC_AMD_COMMON_BLOCK_SMM
diff --git a/src/soc/amd/common/block/cpu/smm/smm_helper.c b/src/soc/amd/common/block/cpu/smm/smm_helper.c
new file mode 100644
index 0000000..bf01b8b
--- /dev/null
+++ b/src/soc/amd/common/block/cpu/smm/smm_helper.c
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <amdblocks/smm.h>
+#include <console/console.h>
+#include <cpu/x86/msr.h>
+#include <cpu/amd/msr.h>
+
+/*
+ * For data stored in TSEG, ensure TValid is clear so R/W access can reach
+ * the DRAM when not in SMM.
+ */
+void clear_tvalid(void)
+{
+	msr_t hwcr = rdmsr(HWCR_MSR);
+	msr_t mask = rdmsr(SMM_MASK_MSR);
+	int tvalid = !!(mask.lo & SMM_TSEG_VALID);
+
+	if (hwcr.lo & SMM_LOCK) {
+		if (!tvalid) /* not valid but locked means still accessible */
+			return;
+
+		printk(BIOS_ERR, "Error: can't clear TValid, already locked\n");
+		return;
+	}
+
+	mask.lo &= ~SMM_TSEG_VALID;
+	wrmsr(SMM_MASK_MSR, mask);
+}
diff --git a/src/soc/amd/common/block/include/amdblocks/smm.h b/src/soc/amd/common/block/include/amdblocks/smm.h
index 637f034..5f14b1e 100644
--- a/src/soc/amd/common/block/include/amdblocks/smm.h
+++ b/src/soc/amd/common/block/include/amdblocks/smm.h
@@ -11,3 +11,4 @@
 void get_smm_info(uintptr_t *perm_smbase, size_t *perm_smsize, size_t *smm_save_state_size);
 void smm_relocation_handler(int cpu, uintptr_t curr_smbase, uintptr_t staggered_smbase);
 void *get_smi_source_handler(int source);
+void clear_tvalid(void);
diff --git a/src/soc/amd/stoneyridge/memmap.c b/src/soc/amd/stoneyridge/memmap.c
index 32790ec..9da3e65 100644
--- a/src/soc/amd/stoneyridge/memmap.c
+++ b/src/soc/amd/stoneyridge/memmap.c
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <amdblocks/smm.h>
 #include <assert.h>
 #include <stdint.h>
 #include <console/console.h>
@@ -56,28 +57,6 @@
 	return CONFIG_SMM_TSEG_SIZE;
 }
 
-/*
- * For data stored in TSEG, ensure TValid is clear so R/W access can reach
- * the DRAM when not in SMM.
- */
-static void clear_tvalid(void)
-{
-	msr_t hwcr = rdmsr(HWCR_MSR);
-	msr_t mask = rdmsr(SMM_MASK_MSR);
-	int tvalid = !!(mask.lo & SMM_TSEG_VALID);
-
-	if (hwcr.lo & SMM_LOCK) {
-		if (!tvalid) /* not valid but locked means still accessible */
-			return;
-
-		printk(BIOS_ERR, "Error: can't clear TValid, already locked\n");
-		return;
-	}
-
-	mask.lo &= ~SMM_TSEG_VALID;
-	wrmsr(SMM_MASK_MSR, mask);
-}
-
 void smm_region(uintptr_t *start, size_t *size)
 {
 	static int once;