SPM: Helpers for error logging after FF-A calls

Signed-off-by: J-Alves <joao.alves@arm.com>
Change-Id: I7c4cb416baf6eb024eb134edb4bbb62d8746629f
diff --git a/include/runtime_services/ffa_helpers.h b/include/runtime_services/ffa_helpers.h
index e296299..4af051b 100644
--- a/include/runtime_services/ffa_helpers.h
+++ b/include/runtime_services/ffa_helpers.h
@@ -392,12 +392,6 @@
 					  uint32_t arg1, uint32_t arg2,
 					  uint32_t arg3, uint32_t arg4);
 
-static inline bool is_ffa_direct_response(smc_ret_values ret)
-{
-	return (ffa_func_id(ret) == FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
-	       (ffa_func_id(ret) == FFA_MSG_SEND_DIRECT_RESP_SMC64);
-}
-
 smc_ret_values ffa_run(uint32_t dest_id, uint32_t vcpu_id);
 smc_ret_values ffa_version(uint32_t input_version);
 smc_ret_values ffa_id_get(void);
diff --git a/include/runtime_services/spm_common.h b/include/runtime_services/spm_common.h
index 74a2b27..dbb113b 100644
--- a/include/runtime_services/spm_common.h
+++ b/include/runtime_services/spm_common.h
@@ -53,11 +53,18 @@
 	CONFIGURE_MAILBOX(mb_name, buffers_size);				\
 	smc_ret = ffa_rxtx_map(							\
 				(uintptr_t)mb_name.send,			\
-				(uintptr_t)mb_name.recv, 			\
+				(uintptr_t)mb_name.recv,			\
 				buffers_size / PAGE_SIZE			\
 			);							\
 	} while (false)
 
+/**
+ * Helpers to evaluate returns of FF-A calls.
+ */
+bool is_ffa_call_error(smc_ret_values val);
+bool is_ffa_direct_response(smc_ret_values ret);
+bool is_expected_ffa_return(smc_ret_values ret, uint32_t func_id);
+
 /*
  * Vector length:
  * SIMD: 128 bits = 16 bytes
diff --git a/spm/cactus/cactus_tests/cactus_test_direct_messaging.c b/spm/cactus/cactus_tests/cactus_test_direct_messaging.c
index f4c7890..31324b6 100644
--- a/spm/cactus/cactus_tests/cactus_test_direct_messaging.c
+++ b/spm/cactus/cactus_tests/cactus_test_direct_messaging.c
@@ -33,8 +33,6 @@
 	ffa_ret = cactus_echo_send_cmd(vm_id, echo_dest, echo_val);
 
 	if (!is_ffa_direct_response(ffa_ret)) {
-		ERROR("Failed to send message. error: %x\n",
-			ffa_error_code(ffa_ret));
 		return cactus_error_resp(vm_id, ffa_dir_msg_source(*args),
 					 CACTUS_ERROR_FFA_CALL);
 	}
diff --git a/spm/cactus/cactus_tests/cactus_test_memory_sharing.c b/spm/cactus/cactus_tests/cactus_test_memory_sharing.c
index 82cdac3..f7d9235 100644
--- a/spm/cactus/cactus_tests/cactus_test_memory_sharing.c
+++ b/spm/cactus/cactus_tests/cactus_test_memory_sharing.c
@@ -137,9 +137,6 @@
 	ffa_ret = cactus_mem_send_cmd(vm_id, receiver, mem_func, handle);
 
 	if (!is_ffa_direct_response(ffa_ret)) {
-		ERROR("Failed to send message. ret: %x error: %x\n",
-		      ffa_func_id(ffa_ret),
-		      ffa_error_code(ffa_ret));
 		return cactus_error_resp(vm_id, source, CACTUS_ERROR_FFA_CALL);
 	}
 
@@ -156,9 +153,7 @@
 		 * permanently given up access to the memory region.
 		 */
 		ffa_ret = ffa_mem_reclaim(handle, 0);
-		if (ffa_func_id(ffa_ret) == FFA_ERROR) {
-			ERROR("Failed to reclaim memory! error: %x\n",
-			      ffa_error_code(ffa_ret));
+		if (is_ffa_call_error(ffa_ret)) {
 			return cactus_error_resp(vm_id, source,
 						 CACTUS_ERROR_TEST);
 		}
diff --git a/tftf/tests/runtime_services/secure_service/ffa_helpers.c b/tftf/tests/runtime_services/secure_service/ffa_helpers.c
index 41dd94b..8e7b58c 100644
--- a/tftf/tests/runtime_services/secure_service/ffa_helpers.c
+++ b/tftf/tests/runtime_services/secure_service/ffa_helpers.c
@@ -3,8 +3,6 @@
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
-
-#include <debug.h>
 #include <smccc.h>
 
 #include <ffa_endpoints.h>
diff --git a/tftf/tests/runtime_services/secure_service/spm_common.c b/tftf/tests/runtime_services/secure_service/spm_common.c
index 7e54a3a..12b70a9 100644
--- a/tftf/tests/runtime_services/secure_service/spm_common.c
+++ b/tftf/tests/runtime_services/secure_service/spm_common.c
@@ -13,6 +13,52 @@
 #define STR(x) __STR(x)
 #define SIMD_TWO_VECTORS_BYTES_STR	(2 * SIMD_VECTOR_LEN_BYTES)
 
+/**
+ * Helper to log errors after FF-A calls.
+ */
+bool is_ffa_call_error(smc_ret_values ret)
+{
+	if (ffa_func_id(ret) == FFA_ERROR) {
+		ERROR("FF-A call returned error (%x): %d\n",
+		      ffa_func_id(ret), ffa_error_code(ret));
+		return true;
+	}
+	return false;
+}
+
+/**
+ * Helper to verify return of FF-A call is an FFA_MSG_SEND_DIRECT_RESP.
+ * Should be used after FFA_MSG_SEND_DIRECT_REQ, or after sending a test command
+ * to an SP.
+ */
+bool is_ffa_direct_response(smc_ret_values ret)
+{
+	if ((ffa_func_id(ret) == FFA_MSG_SEND_DIRECT_RESP_SMC32) ||
+	    (ffa_func_id(ret) == FFA_MSG_SEND_DIRECT_RESP_SMC64)) {
+		return true;
+	}
+
+	ERROR("%x is not FF-A response.\n", ffa_func_id(ret));
+	/* To log error in case it is FFA_ERROR*/
+	is_ffa_call_error(ret);
+
+	return false;
+}
+
+/**
+ * Helper to check the return value of FF-A call is as expected.
+ */
+bool is_expected_ffa_return(smc_ret_values ret, uint32_t func_id)
+{
+	if (ffa_func_id(ret) == func_id) {
+		return true;
+	}
+
+	ERROR("Expecting %x, FF-A return was %x\n", func_id, ffa_func_id(ret));
+
+	return false;
+}
+
 void fill_simd_vector_regs(const simd_vector_t v[SIMD_NUM_VECTORS])
 {
 #ifdef __aarch64__
@@ -275,9 +321,8 @@
 		return FFA_MEMORY_HANDLE_INVALID;
 	}
 
-	if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
-		ERROR("Failed to send memory to %x, error: %x.\n",
-				      receiver, ffa_error_code(ret));
+	if (is_ffa_call_error(ret)) {
+		ERROR("Failed to send message to: %x\n", receiver);
 		return FFA_MEMORY_HANDLE_INVALID;
 	}
 
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 72daae4..83510b0 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
@@ -29,9 +29,11 @@
 	smc_ret_values ret;
 	ret = cactus_echo_send_cmd(sender, dest, value);
 
+	/*
+	 * Return responses may be FFA_MSG_SEND_DIRECT_RESP or FFA_INTERRUPT,
+	 * but only expect the former. Expect SMC32 convention from SP.
+	 */
 	if (!is_ffa_direct_response(ret)) {
-		ERROR("Failed to send message. error: %x\n",
-		      ffa_error_code(ret));
 		return TEST_RESULT_FAIL;
 	}
 
@@ -95,8 +97,6 @@
 	ret = cactus_req_echo_send_cmd(sender, dest, echo_dest, value);
 
 	if (!is_ffa_direct_response(ret)) {
-		ERROR("Failed to send message. error: %x\n",
-		      ffa_error_code(ret));
 		return TEST_RESULT_FAIL;
 	}
 
@@ -147,8 +147,6 @@
 	ret = cactus_req_deadlock_send_cmd(HYP_ID, SP_ID(1), SP_ID(2), SP_ID(3));
 
 	if (!is_ffa_direct_response(ret)) {
-		ERROR("Failed to send message. error: %x\n",
-		      ffa_error_code(ret));
 		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 61b6edd..f126c57 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
@@ -10,6 +10,7 @@
 #include <ffa_endpoints.h>
 #include <test_helpers.h>
 #include <tftf_lib.h>
+#include <spm_common.h>
 #include <xlat_tables_defs.h>
 
 #define MAILBOX_SIZE PAGE_SIZE
@@ -80,8 +81,6 @@
 	ret = cactus_mem_send_cmd(SENDER, RECEIVER, mem_func, handle);
 
 	if (!is_ffa_direct_response(ret)) {
-		ERROR("Failed to send message. error: %x\n",
-		      ffa_error_code(ret));
 		return TEST_RESULT_FAIL;
 	}
 
@@ -103,7 +102,7 @@
 		(void)ptr;
 
 	if (mem_func != FFA_MEM_DONATE_SMC32 &&
-	    ffa_mem_reclaim(handle, 0).ret0 == FFA_ERROR) {
+	    is_ffa_call_error(ffa_mem_reclaim(handle, 0))) {
 			tftf_testcase_printf("Couldn't reclaim memory\n");
 			return TEST_RESULT_FAIL;
 	}
@@ -145,8 +144,6 @@
 					   receiver_sp);
 
 	if (!is_ffa_direct_response(ret)) {
-		ERROR("Failed to send message. error: %x\n",
-		      ffa_error_code(ret));
 		return TEST_RESULT_FAIL;
 	}
 
diff --git a/tftf/tests/runtime_services/secure_service/test_spm_cpu_features.c b/tftf/tests/runtime_services/secure_service/test_spm_cpu_features.c
index 8ce4cc3..f57fa24 100644
--- a/tftf/tests/runtime_services/secure_service/test_spm_cpu_features.c
+++ b/tftf/tests/runtime_services/secure_service/test_spm_cpu_features.c
@@ -56,8 +56,6 @@
 	smc_ret_values ret = cactus_req_simd_fill_send_cmd(SENDER, RECEIVER);
 
 	if (!is_ffa_direct_response(ret)) {
-		ERROR("Failed to send message. error: %x\n",
-		      ffa_error_code(ret));
 		return TEST_RESULT_FAIL;
 	}