test(ff-a): add tests for the FFA_RXTX_UNMAP ABI

Signed-off-by: Daniel Boulby <daniel.boulby@arm.com>
Change-Id: Id4d8cfd2f94ff15201de70a13eececf444d5e66e
diff --git a/include/runtime_services/ffa_helpers.h b/include/runtime_services/ffa_helpers.h
index a17f115..c3f6294 100644
--- a/include/runtime_services/ffa_helpers.h
+++ b/include/runtime_services/ffa_helpers.h
@@ -414,6 +414,7 @@
 smc_ret_values ffa_partition_info_get(const struct ffa_uuid uuid);
 smc_ret_values ffa_rx_release(void);
 smc_ret_values ffa_rxtx_map(uintptr_t send, uintptr_t recv, uint32_t pages);
+smc_ret_values ffa_rxtx_unmap(void);
 smc_ret_values ffa_mem_donate(uint32_t descriptor_length,
 			      uint32_t fragment_length);
 smc_ret_values ffa_mem_lend(uint32_t descriptor_length,
diff --git a/tftf/tests/runtime_services/secure_service/ffa_helpers.c b/tftf/tests/runtime_services/secure_service/ffa_helpers.c
index 8d043a1..2660edb 100644
--- a/tftf/tests/runtime_services/secure_service/ffa_helpers.c
+++ b/tftf/tests/runtime_services/secure_service/ffa_helpers.c
@@ -393,6 +393,23 @@
 	return tftf_smc(&args);
 }
 
+/* Unmap the RXTX buffer allocated by the given FF-A component */
+smc_ret_values ffa_rxtx_unmap(void)
+{
+	smc_args args = {
+		.fid = FFA_RXTX_UNMAP,
+		.arg1 = FFA_PARAM_MBZ,
+		.arg2 = FFA_PARAM_MBZ,
+		.arg3 = FFA_PARAM_MBZ,
+		.arg4 = FFA_PARAM_MBZ,
+		.arg5 = FFA_PARAM_MBZ,
+		.arg6 = FFA_PARAM_MBZ,
+		.arg7 = FFA_PARAM_MBZ
+	};
+
+	return tftf_smc(&args);
+}
+
 /* Donate memory to another partition */
 smc_ret_values ffa_mem_donate(uint32_t descriptor_length,
 				uint32_t fragment_length)
diff --git a/tftf/tests/runtime_services/secure_service/spm_common.c b/tftf/tests/runtime_services/secure_service/spm_common.c
index 7aa4861..e3c7862 100644
--- a/tftf/tests/runtime_services/secure_service/spm_common.c
+++ b/tftf/tests/runtime_services/secure_service/spm_common.c
@@ -252,7 +252,7 @@
 	{"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_RXTX_UNMAP_32 check", FFA_RXTX_UNMAP, FFA_SUCCESS_SMC32},
 	{"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_SPM_ID_GET_32 check", FFA_SPM_ID_GET, FFA_SUCCESS_SMC32,
diff --git a/tftf/tests/runtime_services/secure_service/test_ffa_setup_and_discovery.c b/tftf/tests/runtime_services/secure_service/test_ffa_setup_and_discovery.c
index 572db98..9907284 100644
--- a/tftf/tests/runtime_services/secure_service/test_ffa_setup_and_discovery.c
+++ b/tftf/tests/runtime_services/secure_service/test_ffa_setup_and_discovery.c
@@ -222,18 +222,10 @@
 
 /**
  * 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;
+	return test_ffa_rxtx_map(FFA_SUCCESS_SMC32);
 }
 
 /**
@@ -245,6 +237,63 @@
 	return test_ffa_rxtx_map(FFA_ERROR);
 }
 
+static test_result_t test_ffa_rxtx_unmap(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;
+	}
+
+	ret = ffa_rxtx_unmap();
+	if (!is_expected_ffa_return(ret, expected_return)) {
+		return TEST_RESULT_FAIL;
+	}
+
+	return TEST_RESULT_SUCCESS;
+}
+
+/**
+ * Test unmapping RXTX buffers from NWd.
+ */
+test_result_t test_ffa_rxtx_unmap_success(void)
+{
+	return test_ffa_rxtx_unmap(FFA_SUCCESS_SMC32);
+}
+
+/**
+ * Test to verify that 2nd call to FFA_RXTX_UNMAP should fail.
+ */
+test_result_t test_ffa_rxtx_unmap_fail(void)
+{
+	INFO("This test expects error log.\n");
+	return test_ffa_rxtx_unmap(FFA_ERROR);
+}
+
+/**
+ * Test mapping RXTX buffers that have been previously unmapped 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_unmapped_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;
+}
 /******************************************************************************
  * FF-A SPM_ID_GET ABI Tests
  ******************************************************************************/
@@ -308,4 +357,4 @@
 	}
 
 	return TEST_RESULT_SUCCESS;
-}
\ No newline at end of file
+}
diff --git a/tftf/tests/tests-spm.xml b/tftf/tests/tests-spm.xml
index 360e32d..3e6c72c 100644
--- a/tftf/tests/tests-spm.xml
+++ b/tftf/tests/tests-spm.xml
@@ -21,10 +21,21 @@
      <testcase name="Smaller FFA version than SPM"
                function="test_ffa_version_smaller" />
 
+       <!--
+	 The ordering of the RXTX Buffer tests must be maintained.
+         With test_ffa_rxtx_map_unmapped_success coming last as the
+         mailbox for the remaining tftf tests is set here.
+       -->
      <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" />
+     <testcase name="FF-A RXTX Unmap API success"
+	       function="test_ffa_rxtx_unmap_success" />
+     <testcase name="FF-A RXTX Unmap API consecutive"
+	       function="test_ffa_rxtx_unmap_fail" />
+     <testcase name="FF-A RXTX remap unmapped region success"
+	     function="test_ffa_rxtx_map_unmapped_success" />
 
      <testcase name="Test FFA_SPM_ID_GET"
                function="test_ffa_spm_id_get" />