refactor(spm): helper functions for SPM's MP tests

Factored out code to power on cpus from direct messaging test, and
placed in a helper function. To be used to test other multicore
functionality.
The function expects the cpu on handler to be implemented in the scope
of the functionality to be tested.

Signed-off-by: J-Alves <joao.alves@arm.com>
Change-Id: Ic0074dc85e0a906bae9d7b8cc071aff476f94de5
diff --git a/include/common/test_helpers.h b/include/common/test_helpers.h
index 332f1d8..9031b34 100644
--- a/include/common/test_helpers.h
+++ b/include/common/test_helpers.h
@@ -10,6 +10,7 @@
 #include <arch_features.h>
 #include <ffa_svc.h>
 #include <plat_topology.h>
+#include <events.h>
 #include <psci.h>
 #include <spm_common.h>
 #include <tftf_lib.h>
@@ -341,4 +342,14 @@
 	uint32_t ffa_version_minor, const struct ffa_uuid *ffa_uuids,
 	size_t ffa_uuids_size);
 
+/**
+ * Turn on all cpus to execute a test in all.
+ * - 'cpu_on_handler' should have the code containing the test.
+ * - 'cpu_booted' is used for notifying which cores the test has been executed.
+ * This should be used in the test executed by cpu_on_handler at the end of
+ * processing to make sure it complies with this function's implementation.
+ */
+test_result_t spm_run_multi_core_test(uintptr_t cpu_on_handler,
+				      event_t *cpu_booted);
+
 #endif /* __TEST_HELPERS_H__ */
diff --git a/tftf/tests/common/test_helpers.c b/tftf/tests/common/test_helpers.c
index fffad67..80497bd 100644
--- a/tftf/tests/common/test_helpers.c
+++ b/tftf/tests/common/test_helpers.c
@@ -176,3 +176,46 @@
 
 	return TEST_RESULT_SUCCESS;
 }
+
+test_result_t spm_run_multi_core_test(uintptr_t cpu_on_handler,
+				      event_t *cpu_done)
+{
+	unsigned int lead_mpid = read_mpidr_el1() & MPID_MASK;
+	unsigned int core_pos, cpu_node, mpidr;
+	int32_t ret;
+
+	VERBOSE("Powering on all cpus.\n");
+
+	for (unsigned int i = 0U; i < PLATFORM_CORE_COUNT; i++) {
+		tftf_init_event(&cpu_done[i]);
+	}
+
+	for_each_cpu(cpu_node) {
+		mpidr = tftf_get_mpidr_from_node(cpu_node);
+		if (mpidr == lead_mpid) {
+			continue;
+		}
+
+		ret = tftf_cpu_on(mpidr, cpu_on_handler, 0U);
+		if (ret != 0) {
+			ERROR("tftf_cpu_on mpidr 0x%x returns %d\n",
+			      mpidr, ret);
+		}
+	}
+
+	VERBOSE("Waiting secondary CPUs to turn off ...\n");
+
+	for_each_cpu(cpu_node) {
+		mpidr = tftf_get_mpidr_from_node(cpu_node);
+		if (mpidr == lead_mpid) {
+			continue;
+		}
+
+		core_pos = platform_get_core_pos(mpidr);
+		tftf_wait_for_event(&cpu_done[core_pos]);
+	}
+
+	VERBOSE("Done exiting.\n");
+
+	return TEST_RESULT_SUCCESS;
+}
diff --git a/tftf/tests/runtime_services/secure_service/test_ffa_direct_messaging.c b/tftf/tests/runtime_services/secure_service/test_ffa_direct_messaging.c
index 1b9abe9..fb76362 100644
--- a/tftf/tests/runtime_services/secure_service/test_ffa_direct_messaging.c
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_direct_messaging.c
@@ -24,7 +24,6 @@
 		{PRIMARY_UUID}, {SECONDARY_UUID}, {TERTIARY_UUID}
 	};
 
-
 static event_t cpu_booted[PLATFORM_CORE_COUNT];
 
 static test_result_t send_cactus_echo_cmd(ffa_id_t sender,
@@ -165,7 +164,6 @@
 /**
  * Handler that is passed during tftf_cpu_on to individual CPU cores.
  * Runs a specific core and send a direct message request.
- * Expects core_pos | SP_ID as a response.
  */
 static test_result_t cpu_on_handler(void)
 {
@@ -255,48 +253,9 @@
  */
 test_result_t test_ffa_secondary_core_direct_msg(void)
 {
-	unsigned int lead_mpid = read_mpidr_el1() & MPID_MASK;
-	unsigned int core_pos, cpu_node, mpidr;
-	int32_t ret;
-
 	/**********************************************************************
 	 * Check SPMC has ffa_version and expected FFA endpoints are deployed.
 	 **********************************************************************/
 	CHECK_SPMC_TESTING_SETUP(1, 0, expected_sp_uuids);
-
-	for (unsigned int i = 0U; i < PLATFORM_CORE_COUNT; i++) {
-		tftf_init_event(&cpu_booted[i]);
-	}
-
-	for_each_cpu(cpu_node) {
-		mpidr = tftf_get_mpidr_from_node(cpu_node);
-		if (mpidr == lead_mpid) {
-			continue;
-		}
-
-		ret = tftf_cpu_on(mpidr, (uintptr_t)cpu_on_handler, 0U);
-		if (ret != 0) {
-			ERROR("tftf_cpu_on mpidr 0x%x returns %d\n", mpidr, ret);
-		}
-	}
-
-	VERBOSE("Waiting secondary CPUs to turn off ...\n");
-
-	for_each_cpu(cpu_node) {
-		mpidr = tftf_get_mpidr_from_node(cpu_node);
-		if (mpidr == lead_mpid) {
-			continue;
-		}
-
-		core_pos = platform_get_core_pos(mpidr);
-		tftf_wait_for_event(&cpu_booted[core_pos]);
-	}
-
-	VERBOSE("Done exiting.\n");
-
-	/**********************************************************************
-	 * All tests passed.
-	 **********************************************************************/
-
-	return TEST_RESULT_SUCCESS;
+	return spm_run_multi_core_test((uintptr_t)cpu_on_handler, cpu_booted);
 }