SPM: RXTX map test on TFTF

Some FF-A interfaces use RXTX buffers to exchange information with SPMC.
To avoid repetition of RXTX mapping across the spm-related tests, and
prevent allocation of multiple pages for RXTX buffers within TFTF
runtime:
- Implemented test helpers that hold address of RXTX buffers;
- Implemented test to FFA_RXTX_MAP ABI, that also sets value of RXTX
buffers;
- Cleaned up memory sharing tests that previously implemented RXTX
mapping.

Signed-off-by: J-Alves <joao.alves@arm.com>
Change-Id: I4a67982d3d185bf83809156e4fce03c6edb967d9
diff --git a/include/common/test_helpers.h b/include/common/test_helpers.h
index f9d41dd..a6ec074 100644
--- a/include/common/test_helpers.h
+++ b/include/common/test_helpers.h
@@ -267,4 +267,19 @@
 test_result_t map_test_unmap(const map_args_unmap_t *args,
 			     test_function_arg_t test);
 
+/*
+ * Helper function to set TFTF global mailbox for SPM related tests.
+ * This function should be invoked by the first TFTF test that requires
+ * RX and/or TX buffers.
+ */
+void set_tftf_mailbox(const struct mailbox_buffers *mb);
+
+/*
+ * Helper function to get TFTF global mailbox for SPM related tests.
+ * This function should be called by all tests that require access to RX or TX
+ * buffers, after the function 'set_tftf_mailbox' has been used by the first
+ * test to rely on RX and TX buffers.
+ */
+bool get_tftf_mailbox(struct mailbox_buffers *mb);
+
 #endif /* __TEST_HELPERS_H__ */
diff --git a/tftf/tests/common/test_helpers.c b/tftf/tests/common/test_helpers.c
index 8fdfded..b1868cd 100644
--- a/tftf/tests/common/test_helpers.c
+++ b/tftf/tests/common/test_helpers.c
@@ -1,16 +1,19 @@
 /*
- * Copyright (c) 2018, Arm Limited. All rights reserved.
+ * Copyright (c) 2020, 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>
 #include <test_helpers.h>
 #include <tftf_lib.h>
 
+static struct mailbox_buffers test_mb = {.send = NULL, .recv = NULL};
+
 int is_sys_suspend_state_ready(void)
 {
 	int aff_info;
@@ -128,3 +131,19 @@
 
 	return test_ret;
 }
+
+void set_tftf_mailbox(const struct mailbox_buffers *mb)
+{
+	if (mb != NULL) {
+		test_mb = *mb;
+	}
+}
+
+bool get_tftf_mailbox(struct mailbox_buffers *mb)
+{
+	if ((test_mb.recv != NULL) && (test_mb.send != NULL)) {
+		*mb = test_mb;
+		return true;
+	}
+	return false;
+}
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 0ae8a8d..6e73161 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
@@ -20,17 +20,6 @@
 /* Memory section to be sent over mem management ABIs */
 static __aligned(PAGE_SIZE) uint8_t share_page[PAGE_SIZE];
 
-static __aligned(PAGE_SIZE) uint8_t send_page[PAGE_SIZE];
-static __aligned(PAGE_SIZE) uint8_t recv_page[PAGE_SIZE];
-
-/* Within the same test the RXTX Buffers only need to be shared once */
-static bool rxtx_mapped;
-
-static struct mailbox_buffers mb = {
-					.recv = (void *)recv_page,
-					.send = (void *)send_page,
-				};
-
 static test_result_t test_memory_send_sp(uint32_t mem_func)
 {
 	smc_ret_values ret;
@@ -40,6 +29,7 @@
 	uint32_t sent_length;
 	ffa_memory_handle_t handle;
 	uint32_t *ptr;
+	struct mailbox_buffers mb;
 
 	/**********************************************************************
 	 * Verify that FFA is there and that it has the correct version.
@@ -54,16 +44,14 @@
 		return TEST_RESULT_SKIPPED;
 	}
 
-	if (!rxtx_mapped) {
-		ret = ffa_rxtx_map((uintptr_t)mb.send, (uintptr_t)mb.recv, 1);
-
-		if (ret.ret0 != FFA_SUCCESS_SMC32) {
-			ERROR("ffa_rxtx_map failed (%lx)\n", ret.ret0);
-			return TEST_RESULT_FAIL;
-		}
-		rxtx_mapped = true;
+	if (!get_tftf_mailbox(&mb)) {
+		ERROR("Mailbox not configured!\n This test relies on"
+		      " test suite \"FF-A RXTX Mapping\" to map/configure"
+		      " RXTX buffers\n");
+		return TEST_RESULT_FAIL;
 	}
 
+
 	/**********************************************************************
 	 * Verify that cactus primary SP is deployed in the system.
 	 **********************************************************************/
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
new file mode 100644
index 0000000..5251dc4
--- /dev/null
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_rxtx_map.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2020, 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>
+
+static struct mailbox_buffers mb;
+
+static test_result_t test_ffa_rxtx_map(uint32_t expected_return)
+{
+	smc_ret_values ret;
+
+	/**********************************************************************
+	 * Verify that FFA is there and that it has the correct version.
+	 **********************************************************************/
+	SKIP_TEST_IF_FFA_VERSION_LESS_THAN(1, 0);
+
+	/**********************************************************************
+	 * If OP-TEE is SPMC skip this test.
+	 **********************************************************************/
+	if (check_spmc_execution_level()) {
+		VERBOSE("OP-TEE as SPMC at S-EL1. Skipping test!\n");
+		return TEST_RESULT_SKIPPED;
+	}
+
+	/*
+	 * Declare RXTX buffers, assign them to the mailbox and call
+	 * FFA_RXTX_MAP.
+	 */
+	CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
+	if (ret.ret0 != expected_return) {
+		ERROR("Failed to map RXTX buffers %lx!\n", ret.ret2);
+		return TEST_RESULT_FAIL;
+	}
+
+	return TEST_RESULT_SUCCESS;
+}
+
+/**
+ * Test mapping RXTX buffers from NWd.
+ * This test also sets the Mailbox for other SPM related tests that need to use
+ * RXTX buffers.
+ */
+test_result_t test_ffa_rxtx_map_success(void)
+{
+	test_result_t ret = test_ffa_rxtx_map(FFA_SUCCESS_SMC32);
+
+	if (ret == TEST_RESULT_SUCCESS) {
+		INFO("Set RXTX Mailbox for remaining spm tests!\n");
+		set_tftf_mailbox(&mb);
+	}
+	return ret;
+}
+
+/**
+ * Test to verify that 2nd call to FFA_RXTX_MAP should fail.
+ */
+test_result_t test_ffa_rxtx_map_fail(void)
+{
+	INFO("This test expects error log.\n");
+	return test_ffa_rxtx_map(FFA_ERROR);
+}
diff --git a/tftf/tests/tests-spm.mk b/tftf/tests/tests-spm.mk
index ee339b5..c6b304a 100644
--- a/tftf/tests/tests-spm.mk
+++ b/tftf/tests/tests-spm.mk
@@ -11,4 +11,5 @@
 		test_ffa_version.c					\
 		test_ffa_features.c					\
 		test_ffa_memory_sharing.c				\
+		test_ffa_rxtx_map.c					\
 	)
diff --git a/tftf/tests/tests-spm.xml b/tftf/tests/tests-spm.xml
index 85001f2..981ce71 100644
--- a/tftf/tests/tests-spm.xml
+++ b/tftf/tests/tests-spm.xml
@@ -30,6 +30,14 @@
 
   </testsuite>
 
+  <testsuite name="FF-A RXTX Mapping"
+             description="Test to FF-A RXTX mapping ABI" >
+     <testcase name="FF-A RXTX Map API success"
+               function="test_ffa_rxtx_map_success" />
+     <testcase name="FF-A RXTX Map API consecutive"
+               function="test_ffa_rxtx_map_fail" />
+  </testsuite>
+
   <testsuite name="FF-A Memory Sharing"
              description="Test FF-A Memory Sharing ABIs" >
   <testcase name="Lend Memory to Secure World"