romstage_handoff: remove code duplication

The same pattern was being used throughout the code base
for initializing the romstage handoff structure. Provide
a helper function to initialize the structure with the S3
resume state then utilize it at all the existing call sites.

Change-Id: I1e9d588ab6b9ace67757387dbb5963ae31ceb252
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: https://review.coreboot.org/17646
Tested-by: build bot (Jenkins)
Reviewed-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
Reviewed-by: Furquan Shaikh <furquan@google.com>
diff --git a/src/cpu/amd/car/post_cache_as_ram.c b/src/cpu/amd/car/post_cache_as_ram.c
index 296adc9..88b8637 100644
--- a/src/cpu/amd/car/post_cache_as_ram.c
+++ b/src/cpu/amd/car/post_cache_as_ram.c
@@ -129,14 +129,7 @@
 
 	prepare_romstage_ramstack(s3resume);
 
-	if (IS_ENABLED(CONFIG_EARLY_CBMEM_INIT)) {
-		struct romstage_handoff *handoff;
-		handoff = romstage_handoff_find_or_add();
-		if (handoff != NULL)
-			handoff->s3_resume = s3resume;
-		else
-			printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
-	}
+	romstage_handoff_init(s3resume);
 
 	/* from here don't store more data in CAR */
 	if (family >= 0x1f && family <= 0x3f) {
diff --git a/src/cpu/intel/haswell/romstage.c b/src/cpu/intel/haswell/romstage.c
index 48920b3..7eb115c 100644
--- a/src/cpu/intel/haswell/romstage.c
+++ b/src/cpu/intel/haswell/romstage.c
@@ -172,7 +172,6 @@
 {
 	int boot_mode;
 	int wake_from_s3;
-	struct romstage_handoff *handoff;
 
 	timestamp_init(get_initial_timestamp());
 	timestamp_add_now(TS_START_ROMSTAGE);
@@ -245,11 +244,7 @@
 	#endif
 	}
 
-	handoff = romstage_handoff_find_or_add();
-	if (handoff != NULL)
-		handoff->s3_resume = wake_from_s3;
-	else
-		printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
+	romstage_handoff_init(wake_from_s3);
 
 	post_code(0x3f);
 	if (IS_ENABLED(CONFIG_LPC_TPM)) {
diff --git a/src/drivers/intel/fsp1_1/romstage.c b/src/drivers/intel/fsp1_1/romstage.c
index 97379b2..b222082 100644
--- a/src/drivers/intel/fsp1_1/romstage.c
+++ b/src/drivers/intel/fsp1_1/romstage.c
@@ -99,7 +99,6 @@
 void romstage_common(struct romstage_params *params)
 {
 	const struct mrc_saved_data *cache;
-	struct romstage_handoff *handoff;
 	struct pei_data *pei_data;
 
 	post_code(0x32);
@@ -165,14 +164,9 @@
 	mainboard_save_dimm_info(params);
 
 	/* Create romstage handof information */
-	handoff = romstage_handoff_find_or_add();
-	if (handoff != NULL)
-		handoff->s3_resume = (params->power_state->prev_sleep_state ==
-				      ACPI_S3);
-	else {
-		printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
+	if (romstage_handoff_init(
+			params->power_state->prev_sleep_state == ACPI_S3) < 0)
 		hard_reset();
-	}
 
 	/*
 	 * Initialize the TPM, unless the TPM was already initialized
diff --git a/src/drivers/intel/fsp2_0/memory_init.c b/src/drivers/intel/fsp2_0/memory_init.c
index 56de0ef..283b179d 100644
--- a/src/drivers/intel/fsp2_0/memory_init.c
+++ b/src/drivers/intel/fsp2_0/memory_init.c
@@ -117,7 +117,6 @@
 static void do_fsp_post_memory_init(bool s3wake, uint32_t fsp_version)
 {
 	struct range_entry fsp_mem;
-	struct romstage_handoff *handoff;
 
 	if (fsp_find_reserved_memory(&fsp_mem))
 		die("Failed to find FSP_RESERVED_MEMORY_RESOURCE_HOB!\n");
@@ -144,11 +143,7 @@
 	save_memory_training_data(s3wake, fsp_version);
 
 	/* Create romstage handof information */
-	handoff = romstage_handoff_find_or_add();
-	if (handoff != NULL)
-		handoff->s3_resume = s3wake;
-	else
-		printk(BIOS_SPEW, "Romstage handoff structure not added!\n");
+	romstage_handoff_init(s3wake);
 }
 
 static const char *mrc_cache_get_region_name(void)
diff --git a/src/include/romstage_handoff.h b/src/include/romstage_handoff.h
index 4aba2ce..3eba0fd 100644
--- a/src/include/romstage_handoff.h
+++ b/src/include/romstage_handoff.h
@@ -18,6 +18,8 @@
 #include <stdint.h>
 #include <string.h>
 #include <cbmem.h>
+#include <console/console.h>
+#include <rules.h>
 
 /* It is the chipset's responsibility for maintaining the integrity of this
  * structure in CBMEM. For instance, if chipset code adds this structure
@@ -48,13 +50,32 @@
 	 * found so it can be initialized to 0. */
 	handoff = cbmem_find(CBMEM_ID_ROMSTAGE_INFO);
 
-	if (handoff == NULL) {
-		handoff = cbmem_add(CBMEM_ID_ROMSTAGE_INFO, sizeof(*handoff));
-		if (handoff != NULL)
-			memset(handoff, 0, sizeof(*handoff));
-	}
+	if (handoff)
+		return handoff;
+
+	handoff = cbmem_add(CBMEM_ID_ROMSTAGE_INFO, sizeof(*handoff));
+
+	if (handoff != NULL)
+		memset(handoff, 0, sizeof(*handoff));
+	else
+		printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
 
 	return handoff;
 }
 
+/* Returns 0 if initialized. Else < 0 if handoff structure not added. */
+static inline int romstage_handoff_init(int is_s3_resume)
+{
+	struct romstage_handoff *handoff;
+
+	handoff = romstage_handoff_find_or_add();
+
+	if (handoff == NULL)
+		return -1;
+
+	handoff->s3_resume = is_s3_resume;
+
+	return 0;
+}
+
 #endif /* ROMSTAGE_HANDOFF_H */
diff --git a/src/northbridge/intel/sandybridge/early_init.c b/src/northbridge/intel/sandybridge/early_init.c
index ef97a1a..16ea29d 100644
--- a/src/northbridge/intel/sandybridge/early_init.c
+++ b/src/northbridge/intel/sandybridge/early_init.c
@@ -227,13 +227,7 @@
 
 void northbridge_romstage_finalize(int s3resume)
 {
-	struct romstage_handoff *handoff;
-
 	MCHBAR16(SSKPD) = 0xCAFE;
 
-	handoff = romstage_handoff_find_or_add();
-	if (handoff != NULL)
-		handoff->s3_resume = s3resume;
-	else
-		printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
+	romstage_handoff_init(s3resume);
 }
diff --git a/src/soc/intel/baytrail/romstage/romstage.c b/src/soc/intel/baytrail/romstage/romstage.c
index a61b571..124eb6e 100644
--- a/src/soc/intel/baytrail/romstage/romstage.c
+++ b/src/soc/intel/baytrail/romstage/romstage.c
@@ -210,7 +210,6 @@
 /* Entry from the mainboard. */
 void romstage_common(struct romstage_params *params)
 {
-	struct romstage_handoff *handoff;
 	struct chipset_power_state *ps;
 	int prev_sleep_state;
 
@@ -232,11 +231,7 @@
 
 	timestamp_add_now(TS_AFTER_INITRAM);
 
-	handoff = romstage_handoff_find_or_add();
-	if (handoff != NULL)
-		handoff->s3_resume = (prev_sleep_state == ACPI_S3);
-	else
-		printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
+	romstage_handoff_init(prev_sleep_state == ACPI_S3);
 
 	if (IS_ENABLED(CONFIG_LPC_TPM)) {
 		init_tpm(prev_sleep_state == ACPI_S3);
diff --git a/src/soc/intel/broadwell/romstage/romstage.c b/src/soc/intel/broadwell/romstage/romstage.c
index 44df88c..849c55d 100644
--- a/src/soc/intel/broadwell/romstage/romstage.c
+++ b/src/soc/intel/broadwell/romstage/romstage.c
@@ -89,8 +89,6 @@
 /* Entry from the mainboard. */
 void romstage_common(struct romstage_params *params)
 {
-	struct romstage_handoff *handoff;
-
 	post_code(0x32);
 
 	timestamp_add_now(TS_BEFORE_INITRAM);
@@ -114,12 +112,7 @@
 
 	timestamp_add_now(TS_AFTER_INITRAM);
 
-	handoff = romstage_handoff_find_or_add();
-	if (handoff != NULL)
-		handoff->s3_resume = (params->power_state->prev_sleep_state ==
-				      ACPI_S3);
-	else
-		printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
+	romstage_handoff_init(params->power_state->prev_sleep_state == ACPI_S3);
 
 #if CONFIG_LPC_TPM
 	init_tpm(params->power_state->prev_sleep_state == ACPI_S3);
diff --git a/src/soc/intel/fsp_baytrail/romstage/romstage.c b/src/soc/intel/fsp_baytrail/romstage/romstage.c
index a7ed414..9d204d1 100644
--- a/src/soc/intel/fsp_baytrail/romstage/romstage.c
+++ b/src/soc/intel/fsp_baytrail/romstage/romstage.c
@@ -218,7 +218,6 @@
 	int cbmem_was_initted;
 	void *cbmem_hob_ptr;
 	uint32_t prev_sleep_state;
-	struct romstage_handoff *handoff;
 
 	timestamp_add_now(TS_AFTER_INITRAM);
 
@@ -257,11 +256,7 @@
 	*(u32*)cbmem_hob_ptr = (u32)hob_list_ptr;
 	post_code(0x4e);
 
-	handoff = romstage_handoff_find_or_add();
-	if (handoff != NULL)
-		handoff->s3_resume = (prev_sleep_state == ACPI_S3);
-	else
-		printk(BIOS_DEBUG, "Romstage handoff structure not added!\n");
+	romstage_handoff_init(prev_sleep_state == ACPI_S3);
 
 	post_code(0x4f);