[SPM] tidying common code to tftf and cactus

This patch moves the code used to test SPM functionality, not explicitly
described in FF-A specification, from ffa_helpers to spm_common which is
built for both tftf and cactus.

Signed-off-by: Max Shvetsov <maksims.svecovs@arm.com>
Change-Id: I461efad977cc4d02701feea7b2215a61453716ef
diff --git a/tftf/tests/common/test_helpers.c b/tftf/tests/common/test_helpers.c
index 05b09d7..d794beb 100644
--- a/tftf/tests/common/test_helpers.c
+++ b/tftf/tests/common/test_helpers.c
@@ -1,11 +1,10 @@
 /*
- * Copyright (c) 2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
 #include <arch_helpers.h>
-#include <ffa_helpers.h>
 #include <plat_topology.h>
 #include <platform.h>
 #include <power_management.h>
diff --git a/tftf/tests/runtime_services/secure_service/ffa_helpers.c b/tftf/tests/runtime_services/secure_service/ffa_helpers.c
index 1b79018..7d94d27 100644
--- a/tftf/tests/runtime_services/secure_service/ffa_helpers.c
+++ b/tftf/tests/runtime_services/secure_service/ffa_helpers.c
@@ -1,11 +1,12 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
 #include <debug.h>
 #include <smccc.h>
+
 #include <ffa_endpoints.h>
 #include <ffa_helpers.h>
 #include <ffa_svc.h>
@@ -108,49 +109,6 @@
 					      message, 0, 0, 0, 0);
 }
 
-/*
- * check_spmc_execution_level
- *
- * Attempt sending impdef protocol messages to OP-TEE through direct messaging.
- * Criteria for detecting OP-TEE presence is that responses match defined
- * version values. In the case of SPMC running at S-EL2 (and Cactus instances
- * running at S-EL1) the response will not match the pre-defined version IDs.
- *
- * Returns true if SPMC is probed as being OP-TEE at S-EL1.
- *
- */
-bool check_spmc_execution_level(void)
-{
-	unsigned int is_optee_spmc_criteria = 0U;
-	smc_ret_values ret_values;
-
-	/*
-	 * Send a first OP-TEE-defined protocol message through
-	 * FFA direct message.
-	 *
-	 */
-	ret_values = ffa_msg_send_direct_req(HYP_ID, SP_ID(1),
-						OPTEE_FFA_GET_API_VERSION);
-	if ((ret_values.ret3 == FFA_VERSION_MAJOR) &&
-	    (ret_values.ret4 == FFA_VERSION_MINOR)) {
-		is_optee_spmc_criteria++;
-	}
-
-	/*
-	 * Send a second OP-TEE-defined protocol message through
-	 * FFA direct message.
-	 *
-	 */
-	ret_values = ffa_msg_send_direct_req(HYP_ID, SP_ID(1),
-						OPTEE_FFA_GET_OS_VERSION);
-	if ((ret_values.ret3 == OPTEE_FFA_GET_OS_VERSION_MAJOR) &&
-	    (ret_values.ret4 == OPTEE_FFA_GET_OS_VERSION_MINOR)) {
-		is_optee_spmc_criteria++;
-	}
-
-	return (is_optee_spmc_criteria == 2U);
-}
-
 /**
  * Initialises the header of the given `ffa_memory_region`, not including the
  * composite memory region offset.
diff --git a/tftf/tests/runtime_services/secure_service/spm_common.c b/tftf/tests/runtime_services/secure_service/spm_common.c
new file mode 100644
index 0000000..74a19a6
--- /dev/null
+++ b/tftf/tests/runtime_services/secure_service/spm_common.c
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <ffa_endpoints.h>
+#include <spm_common.h>
+
+/*
+ * check_spmc_execution_level
+ *
+ * Attempt sending impdef protocol messages to OP-TEE through direct messaging.
+ * Criteria for detecting OP-TEE presence is that responses match defined
+ * version values. In the case of SPMC running at S-EL2 (and Cactus instances
+ * running at S-EL1) the response will not match the pre-defined version IDs.
+ *
+ * Returns true if SPMC is probed as being OP-TEE at S-EL1.
+ *
+ */
+bool check_spmc_execution_level(void)
+{
+	unsigned int is_optee_spmc_criteria = 0U;
+	smc_ret_values ret_values;
+
+	/*
+	 * Send a first OP-TEE-defined protocol message through
+	 * FFA direct message.
+	 */
+	ret_values = ffa_msg_send_direct_req(HYP_ID, SP_ID(1),
+						OPTEE_FFA_GET_API_VERSION);
+	if ((ret_values.ret3 == FFA_VERSION_MAJOR) &&
+	    (ret_values.ret4 == FFA_VERSION_MINOR)) {
+		is_optee_spmc_criteria++;
+	}
+
+	/*
+	 * Send a second OP-TEE-defined protocol message through
+	 * FFA direct message.
+	 */
+	ret_values = ffa_msg_send_direct_req(HYP_ID, SP_ID(1),
+						OPTEE_FFA_GET_OS_VERSION);
+	if ((ret_values.ret3 == OPTEE_FFA_GET_OS_VERSION_MAJOR) &&
+	    (ret_values.ret4 == OPTEE_FFA_GET_OS_VERSION_MINOR)) {
+		is_optee_spmc_criteria++;
+	}
+
+	return (is_optee_spmc_criteria == 2U);
+}
+
+static const struct ffa_features_test ffa_feature_test_target[] = {
+	{"FFA_ERROR_32 check", FFA_ERROR, FFA_SUCCESS_SMC32},
+	{"FFA_SUCCESS_32 check", FFA_SUCCESS_SMC32, FFA_SUCCESS_SMC32},
+	{"FFA_INTERRUPT_32 check", FFA_INTERRUPT, FFA_SUCCESS_SMC32},
+	{"FFA_VERSION_32 check", FFA_VERSION, FFA_SUCCESS_SMC32},
+	{"FFA_FEATURES_32 check", FFA_FEATURES, FFA_SUCCESS_SMC32},
+	{"FFA_RX_RELEASE_32 check", FFA_RX_RELEASE, FFA_SUCCESS_SMC32},
+	{"FFA_RXTX_MAP_32 check", FFA_RXTX_MAP_SMC32, FFA_ERROR},
+	{"FFA_RXTX_MAP_64 check", FFA_RXTX_MAP_SMC64, FFA_SUCCESS_SMC32},
+	{"FFA_RXTX_UNMAP_32 check", FFA_RXTX_UNMAP, FFA_ERROR},
+	{"FFA_PARTITION_INFO_GET_32 check", FFA_PARTITION_INFO_GET, FFA_SUCCESS_SMC32},
+	{"FFA_ID_GET_32 check", FFA_ID_GET, FFA_SUCCESS_SMC32},
+	{"FFA_MSG_POLL_32 check", FFA_MSG_POLL, FFA_SUCCESS_SMC32},
+	{"FFA_MSG_WAIT_32 check", FFA_MSG_WAIT, FFA_SUCCESS_SMC32},
+	{"FFA_YIELD_32 check", FFA_MSG_YIELD, FFA_SUCCESS_SMC32},
+	{"FFA_RUN_32 check", FFA_MSG_RUN, FFA_SUCCESS_SMC32},
+	{"FFA_MSG_SEND_32 check", FFA_MSG_SEND, FFA_SUCCESS_SMC32},
+	{"FFA_MEM_DONATE_32 check", FFA_MEM_DONATE_SMC32, FFA_SUCCESS_SMC32},
+	{"FFA_MEM_LEND_32 check", FFA_MEM_LEND_SMC32, FFA_SUCCESS_SMC32},
+	{"FFA_MEM_SHARE_32 check", FFA_MEM_SHARE_SMC32, FFA_SUCCESS_SMC32},
+	{"FFA_MEM_RETRIEVE_REQ_32 check", FFA_MEM_RETRIEVE_REQ_SMC32, FFA_SUCCESS_SMC32},
+	{"FFA_MEM_RETRIEVE_RESP_32 check", FFA_MEM_RETRIEVE_RESP, FFA_SUCCESS_SMC32},
+	{"FFA_MEM_RELINQUISH_32 check", FFA_MEM_RELINQUISH, FFA_SUCCESS_SMC32},
+	{"FFA_MEM_RECLAIM_32 check", FFA_MEM_RECLAIM, FFA_SUCCESS_SMC32},
+	{"Check non-existent command", 0xFFFF, FFA_ERROR}
+};
+
+/*
+ * Populates test_target with content of ffa_feature_test_target.
+ *
+ * Returns number of elements in the *test_target.
+ */
+unsigned int get_ffa_feature_test_target(
+	const struct ffa_features_test **test_target)
+{
+	if (test_target != NULL) {
+		*test_target = ffa_feature_test_target;
+	}
+
+	return sizeof(ffa_feature_test_target) /
+	       sizeof(struct ffa_features_test);
+}
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 6e79a88..3bdacda 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
@@ -1,17 +1,17 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
+#include <debug.h>
+#include <smccc.h>
+
 #include <arch_helpers.h>
 #include <cactus_test_cmds.h>
-#include <debug.h>
 #include <ffa_endpoints.h>
-#include <platform.h>
-#include <smccc.h>
-#include <ffa_helpers.h>
 #include <ffa_svc.h>
+#include <platform.h>
 #include <test_helpers.h>
 
 #define ECHO_VAL1 U(0xa0a0a0a0)
diff --git a/tftf/tests/runtime_services/secure_service/test_ffa_features.c b/tftf/tests/runtime_services/secure_service/test_ffa_features.c
index 7d67bf8..beee5d2 100644
--- a/tftf/tests/runtime_services/secure_service/test_ffa_features.c
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_features.c
@@ -1,46 +1,13 @@
 /*
- * Copyright (c) 2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include <ffa_helpers.h>
+#include <spm_common.h>
 #include <test_helpers.h>
 #include <tftf_lib.h>
 
-struct feature_test {
-	const char *test_name;
-	unsigned int feature;
-	u_register_t expected_ret;
-};
-
-static const struct feature_test test_target[] = {
-	{"FFA_ERROR_32 check", FFA_ERROR, FFA_SUCCESS_SMC32},
-	{"FFA_SUCCESS_32 check", FFA_SUCCESS_SMC32, FFA_SUCCESS_SMC32},
-	{"FFA_INTERRUPT_32 check", FFA_INTERRUPT, FFA_SUCCESS_SMC32},
-	{"FFA_VERSION_32 check", FFA_VERSION, FFA_SUCCESS_SMC32},
-	{"FFA_FEATURES_32 check", FFA_FEATURES, FFA_SUCCESS_SMC32},
-	{"FFA_RX_RELEASE_32 check", FFA_RX_RELEASE, FFA_SUCCESS_SMC32},
-	{"FFA_RXTX_MAP_32 check", FFA_RXTX_MAP_SMC32, FFA_ERROR},
-	{"FFA_RXTX_MAP_64 check", FFA_RXTX_MAP_SMC64, FFA_SUCCESS_SMC32},
-	{"FFA_RXTX_UNMAP_32 check", FFA_RXTX_UNMAP, FFA_ERROR},
-	{"FFA_PARTITION_INFO_GET_32 check", FFA_PARTITION_INFO_GET, FFA_SUCCESS_SMC32},
-	{"FFA_ID_GET_32 check", FFA_ID_GET, FFA_SUCCESS_SMC32},
-	{"FFA_MSG_POLL_32 check", FFA_MSG_POLL, FFA_SUCCESS_SMC32},
-	{"FFA_MSG_WAIT_32 check", FFA_MSG_WAIT, FFA_SUCCESS_SMC32},
-	{"FFA_YIELD_32 check", FFA_MSG_YIELD, FFA_SUCCESS_SMC32},
-	{"FFA_RUN_32 check", FFA_MSG_RUN, FFA_SUCCESS_SMC32},
-	{"FFA_MSG_SEND_32 check", FFA_MSG_SEND, FFA_SUCCESS_SMC32},
-	{"FFA_MEM_DONATE_32 check", FFA_MEM_DONATE_SMC32, FFA_SUCCESS_SMC32},
-	{"FFA_MEM_LEND_32 check", FFA_MEM_LEND_SMC32, FFA_SUCCESS_SMC32},
-	{"FFA_MEM_SHARE_32 check", FFA_MEM_SHARE_SMC32, FFA_SUCCESS_SMC32},
-	{"FFA_MEM_RETRIEVE_REQ_32 check", FFA_MEM_RETRIEVE_REQ_SMC32, FFA_SUCCESS_SMC32},
-	{"FFA_MEM_RETRIEVE_RESP_32 check", FFA_MEM_RETRIEVE_RESP, FFA_SUCCESS_SMC32},
-	{"FFA_MEM_RELINQUISH_32 check", FFA_MEM_RELINQUISH, FFA_SUCCESS_SMC32},
-	{"FFA_MEM_RECLAIM_32 check", FFA_MEM_RECLAIM, FFA_SUCCESS_SMC32},
-	{"Check non-existent command", 0xFFFF, FFA_ERROR}
-};
-
 test_result_t test_ffa_features(void)
 {
 	SKIP_TEST_IF_FFA_VERSION_LESS_THAN(1, 0);
@@ -52,23 +19,24 @@
 	}
 
 	smc_ret_values ffa_ret;
+	const struct ffa_features_test *ffa_feature_test_target;
 	unsigned int i, test_target_size =
-		sizeof(test_target) / sizeof(struct feature_test);
+		get_ffa_feature_test_target(&ffa_feature_test_target);
 
 	for (i = 0U; i < test_target_size; i++) {
-		ffa_ret = ffa_features(test_target[i].feature);
-		if (ffa_ret.ret0 != test_target[i].expected_ret) {
-			tftf_testcase_printf("%s returned %lx, expected %lx\n",
-					     test_target[i].test_name,
+		ffa_ret = ffa_features(ffa_feature_test_target[i].feature);
+		if (ffa_ret.ret0 != ffa_feature_test_target[i].expected_ret) {
+			tftf_testcase_printf("%s returned %lx, expected %x\n",
+					     ffa_feature_test_target[i].test_name,
 					     ffa_ret.ret0,
-					     test_target[i].expected_ret);
+					     ffa_feature_test_target[i].expected_ret);
 			return TEST_RESULT_FAIL;
 		}
-		if ((test_target[i].expected_ret == (u_register_t)FFA_ERROR) &&
+		if ((ffa_feature_test_target[i].expected_ret == (u_register_t)FFA_ERROR) &&
 		    (ffa_ret.ret2 != (u_register_t)FFA_ERROR_NOT_SUPPORTED)) {
 			tftf_testcase_printf("%s failed for the wrong reason: "
 					     "returned %lx, expected %lx\n",
-					     test_target[i].test_name,
+					     ffa_feature_test_target[i].test_name,
 					     ffa_ret.ret2,
 					     (u_register_t)FFA_ERROR_NOT_SUPPORTED);
 			return TEST_RESULT_FAIL;
diff --git a/tftf/tests/runtime_services/secure_service/test_ffa_memory_sharing.c b/tftf/tests/runtime_services/secure_service/test_ffa_memory_sharing.c
index b861d09..5b59c43 100644
--- a/tftf/tests/runtime_services/secure_service/test_ffa_memory_sharing.c
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_memory_sharing.c
@@ -1,13 +1,13 @@
 /*
- * Copyright (c) 2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include <cactus_test_cmds.h>
 #include <debug.h>
+
+#include <cactus_test_cmds.h>
 #include <ffa_endpoints.h>
-#include <ffa_helpers.h>
 #include <test_helpers.h>
 #include <tftf_lib.h>
 #include <xlat_tables_defs.h>
diff --git a/tftf/tests/runtime_services/secure_service/test_ffa_rxtx_map.c b/tftf/tests/runtime_services/secure_service/test_ffa_rxtx_map.c
index 5251dc4..1e4c4e0 100644
--- a/tftf/tests/runtime_services/secure_service/test_ffa_rxtx_map.c
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_rxtx_map.c
@@ -1,11 +1,11 @@
 /*
- * Copyright (c) 2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
 #include <debug.h>
-#include <ffa_helpers.h>
+
 #include <test_helpers.h>
 #include <xlat_tables_defs.h>
 
diff --git a/tftf/tests/runtime_services/secure_service/test_ffa_version.c b/tftf/tests/runtime_services/secure_service/test_ffa_version.c
index fae058d..41eca5a 100644
--- a/tftf/tests/runtime_services/secure_service/test_ffa_version.c
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_version.c
@@ -1,10 +1,9 @@
 /*
- * Copyright (c) 2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#include <ffa_helpers.h>
 #include <ffa_svc.h>
 #include <test_helpers.h>
 #include <tftf_lib.h>
diff --git a/tftf/tests/tests-spm.mk b/tftf/tests/tests-spm.mk
index c6b304a..2a94983 100644
--- a/tftf/tests/tests-spm.mk
+++ b/tftf/tests/tests-spm.mk
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+# Copyright (c) 2018-2021, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -7,9 +7,10 @@
 TESTS_SOURCES	+=							\
 	$(addprefix tftf/tests/runtime_services/secure_service/,	\
 		ffa_helpers.c						\
+		spm_common.c						\
 		test_ffa_direct_messaging.c				\
-		test_ffa_version.c					\
 		test_ffa_features.c					\
 		test_ffa_memory_sharing.c				\
 		test_ffa_rxtx_map.c					\
+		test_ffa_version.c					\
 	)