refactor(plat/ffa): refactor ffa_memory

* Rename `plat_ffa_memory_security_mode` to
  `plat_ffa_memory_add_security_bit_from_mode`.
* Return from `plat_ffa_is_memory_send_valid` rather than mutating
  `result` parameter.
* Extract calls to `FFA_MEM_RECLAIM` to a helper function

Change-Id: Ia2e992afbd492759100a2a9284cbc06050e48ab0
Signed-off-by: Karl Meakin <karl.meakin@arm.com>
diff --git a/src/arch/aarch64/plat/ffa/hypervisor/ffa_memory.c b/src/arch/aarch64/plat/ffa/hypervisor/ffa_memory.c
index 73c743b..76feee9 100644
--- a/src/arch/aarch64/plat/ffa/hypervisor/ffa_memory.c
+++ b/src/arch/aarch64/plat/ffa/hypervisor/ffa_memory.c
@@ -18,6 +18,17 @@
 #include "hypervisor.h"
 #include "sysregs.h"
 
+static struct ffa_value ffa_other_world_mem_reclaim(
+	ffa_memory_handle_t handle, ffa_memory_region_flags_t flags)
+{
+	return arch_other_world_call((struct ffa_value){
+		.func = FFA_MEM_RECLAIM_32,
+		.arg1 = (uint32_t)handle,
+		.arg2 = (uint32_t)(handle >> 32),
+		.arg3 = flags,
+	});
+}
+
 /**
  * Check validity of the FF-A memory send function attempt.
  */
@@ -81,10 +92,11 @@
 
 	other_world_locked.vm->mailbox.recv_func = share_func;
 	other_world_locked.vm->mailbox.state = MAILBOX_STATE_FULL;
-	ret = arch_other_world_call(
-		(struct ffa_value){.func = share_func,
-				   .arg1 = memory_share_length,
-				   .arg2 = fragment_length});
+	ret = arch_other_world_call((struct ffa_value){
+		.func = share_func,
+		.arg1 = memory_share_length,
+		.arg2 = fragment_length,
+	});
 	/*
 	 * After the call to the other world completes it must have finished
 	 * reading its RX buffer, so it is ready for another message.
@@ -159,11 +171,7 @@
 				     __func__);
 			ret = ffa_error(FFA_NO_MEMORY);
 
-			reclaim_ret = arch_other_world_call((struct ffa_value){
-				.func = FFA_MEM_RECLAIM_32,
-				.arg1 = (uint32_t)handle,
-				.arg2 = (uint32_t)(handle >> 32),
-				.arg3 = 0});
+			reclaim_ret = ffa_other_world_mem_reclaim(handle, 0);
 			assert(reclaim_ret.func == FFA_SUCCESS_32);
 			goto out;
 		}
@@ -177,11 +185,7 @@
 				__func__, ffa_func_name(ret.func),
 				ffa_error_name(ffa_error_code(ret)));
 
-			reclaim_ret = arch_other_world_call((struct ffa_value){
-				.func = FFA_MEM_RECLAIM_32,
-				.arg1 = (uint32_t)handle,
-				.arg2 = (uint32_t)(handle >> 32),
-				.arg3 = 0});
+			reclaim_ret = ffa_other_world_mem_reclaim(handle, 0);
 			assert(reclaim_ret.func == FFA_SUCCESS_32);
 			goto out;
 		}
@@ -213,16 +217,6 @@
 				ffa_error_name(ffa_error_code(ret)));
 			goto out;
 		}
-		if (ret.func != FFA_MEM_FRAG_RX_32) {
-			dlog_warning(
-				"%s: got unexpected response to %s "
-				"from other world (expected %s, got %s)\n",
-				__func__, ffa_func_name(share_func),
-				ffa_func_name(FFA_MEM_FRAG_RX_32),
-				ffa_func_name(ret.func));
-			ret = ffa_error(FFA_INVALID_PARAMETERS);
-			goto out;
-		}
 		if (ret.arg3 != fragment_length) {
 			dlog_warning(
 				"%s: got unexpected fragment offset for %s "
@@ -250,11 +244,7 @@
 				     __func__);
 			ret = ffa_error(FFA_NO_MEMORY);
 
-			reclaim_ret = arch_other_world_call((struct ffa_value){
-				.func = FFA_MEM_RECLAIM_32,
-				.arg1 = (uint32_t)handle,
-				.arg2 = (uint32_t)(handle >> 32),
-				.arg3 = 0});
+			reclaim_ret = ffa_other_world_mem_reclaim(handle, 0);
 			assert(reclaim_ret.func == FFA_SUCCESS_32);
 			goto out;
 		}
@@ -402,11 +392,7 @@
 	 * structures. This can fail if the SPs haven't finished using the
 	 * memory.
 	 */
-	ret = arch_other_world_call(
-		(struct ffa_value){.func = FFA_MEM_RECLAIM_32,
-				   .arg1 = (uint32_t)handle,
-				   .arg2 = (uint32_t)(handle >> 32),
-				   .arg3 = flags});
+	ret = ffa_other_world_mem_reclaim(handle, flags);
 
 	if (ret.func != FFA_SUCCESS_32) {
 		dlog_verbose(
@@ -612,10 +598,7 @@
 		} else {
 			/* Abort sending to other_world. */
 			struct ffa_value other_world_ret =
-				arch_other_world_call((struct ffa_value){
-					.func = FFA_MEM_RECLAIM_32,
-					.arg1 = (uint32_t)handle,
-					.arg2 = (uint32_t)(handle >> 32)});
+				ffa_other_world_mem_reclaim(handle, 0);
 
 			if (other_world_ret.func != FFA_SUCCESS_32) {
 				/*
@@ -708,7 +691,7 @@
 	return ret;
 }
 
-ffa_memory_attributes_t plat_ffa_memory_security_mode(
+ffa_memory_attributes_t plat_ffa_memory_add_security_bit_from_mode(
 	ffa_memory_attributes_t attributes, uint32_t mode)
 {
 	(void)mode;
diff --git a/src/arch/aarch64/plat/ffa/spmc/ffa_memory.c b/src/arch/aarch64/plat/ffa/spmc/ffa_memory.c
index 45a61c6..5126253 100644
--- a/src/arch/aarch64/plat/ffa/spmc/ffa_memory.c
+++ b/src/arch/aarch64/plat/ffa/spmc/ffa_memory.c
@@ -20,7 +20,6 @@
 bool plat_ffa_is_memory_send_valid(ffa_id_t receiver, ffa_id_t sender,
 				   uint32_t share_func, bool multiple_borrower)
 {
-	bool result;
 	const bool is_receiver_sp = vm_id_is_current_world(receiver);
 	const bool is_sender_sp = vm_id_is_current_world(sender);
 
@@ -37,14 +36,13 @@
 	case FFA_MEM_DONATE_32:
 	case FFA_MEM_LEND_64:
 	case FFA_MEM_LEND_32:
-		result = is_receiver_sp;
-		break;
+		return is_receiver_sp;
 	case FFA_MEM_SHARE_64:
-	case FFA_MEM_SHARE_32:
-		result = (is_sender_sp && is_receiver_sp) ||
-			 (!is_sender_sp && !multiple_borrower &&
-			  is_receiver_sp) ||
-			 (!is_sender_sp && multiple_borrower);
+	case FFA_MEM_SHARE_32: {
+		bool result = (is_sender_sp && is_receiver_sp) ||
+			      (!is_sender_sp && !multiple_borrower &&
+			       is_receiver_sp) ||
+			      (!is_sender_sp && multiple_borrower);
 
 		if (!result) {
 			dlog_verbose(
@@ -52,12 +50,11 @@
 				"single SP, or multiple borrowers with mixed "
 				"world borrowers.\n");
 		}
-		break;
-	default:
-		result = false;
+		return result;
 	}
-
-	return result;
+	default:
+		return false;
+	}
 }
 
 ffa_memory_handle_t plat_ffa_memory_handle_make(uint64_t index)
@@ -145,7 +142,7 @@
  * Update the memory region attributes with the security state bit based on the
  * supplied mode.
  */
-ffa_memory_attributes_t plat_ffa_memory_security_mode(
+ffa_memory_attributes_t plat_ffa_memory_add_security_bit_from_mode(
 	ffa_memory_attributes_t attributes, uint32_t mode)
 {
 	ffa_memory_attributes_t ret = attributes;
diff --git a/src/arch/fake/hypervisor/ffa.c b/src/arch/fake/hypervisor/ffa.c
index 3ee082e..03e54d4 100644
--- a/src/arch/fake/hypervisor/ffa.c
+++ b/src/arch/fake/hypervisor/ffa.c
@@ -611,7 +611,7 @@
 	return true;
 }
 
-ffa_memory_attributes_t plat_ffa_memory_security_mode(
+ffa_memory_attributes_t plat_ffa_memory_add_security_bit_from_mode(
 	ffa_memory_attributes_t attributes, uint32_t mode)
 {
 	(void)mode;
diff --git a/src/ffa_memory.c b/src/ffa_memory.c
index c4bf5ad..7e8d3e1 100644
--- a/src/ffa_memory.c
+++ b/src/ffa_memory.c
@@ -3405,8 +3405,8 @@
 	 * Set the security state in the memory retrieve response attributes
 	 * if specified by the target mode.
 	 */
-	attributes = plat_ffa_memory_security_mode(memory_region->attributes,
-						   retrieve_mode);
+	attributes = plat_ffa_memory_add_security_bit_from_mode(
+		memory_region->attributes, retrieve_mode);
 
 	/*
 	 * Constituents which we received in the first fragment should
@@ -3525,7 +3525,7 @@
 	 * Set the security state in the memory retrieve response attributes
 	 * if specified by the target mode.
 	 */
-	attributes = plat_ffa_memory_security_mode(
+	attributes = plat_ffa_memory_add_security_bit_from_mode(
 		memory_region->attributes, share_state->sender_orig_mode);
 
 	receiver = ffa_memory_region_get_receiver(memory_region, 0);