refactor(plat/ffa): memory handle refactorings

Replace the need for each platform to provide a definition of
`plat_ffa_memory_handle_make` and
`plat_ffa_memory_handle_allocated_by_current_world`. Instead, they only
need to provide a value for `plat_ffa_memory_handle_allocator`.

Move `FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR` and
`FFA_MEMORY_HANDLE_ALLOCATOR_SPMC` constants into an enum.

Add helper functions for getting a memory handle's allocator and index.

Change-Id: I4f4661a2e7e37769e947383b4b45718fb0ddfa1a
Signed-off-by: Karl Meakin <karl.meakin@arm.com>
diff --git a/inc/hf/arch/plat/ffa/ffa_memory.h b/inc/hf/arch/plat/ffa/ffa_memory.h
index 7ef5c20..62d876f 100644
--- a/inc/hf/arch/plat/ffa/ffa_memory.h
+++ b/inc/hf/arch/plat/ffa/ffa_memory.h
@@ -15,17 +15,27 @@
 bool plat_ffa_is_memory_send_valid(ffa_id_t receiver, ffa_id_t sender,
 				   uint32_t share_func, bool multiple_borrower);
 
+enum ffa_memory_handle_allocator plat_ffa_memory_handle_allocator(void);
+
 /**
  * Encodes memory handle according to section 5.10.2 of the FF-A v1.0 spec.
  */
-ffa_memory_handle_t plat_ffa_memory_handle_make(uint64_t index);
+static inline ffa_memory_handle_t plat_ffa_memory_handle_make(uint64_t index)
+{
+	return ffa_memory_handle_make(index,
+				      plat_ffa_memory_handle_allocator());
+}
 
 /**
  * Checks whether given handle was allocated by current world, according to
  * handle encoding rules.
  */
-bool plat_ffa_memory_handle_allocated_by_current_world(
-	ffa_memory_handle_t handle);
+static inline bool plat_ffa_memory_handle_allocated_by_current_world(
+	ffa_memory_handle_t handle)
+{
+	return ffa_memory_handle_allocator(handle) ==
+	       plat_ffa_memory_handle_allocator();
+}
 
 /**
  * For non-secure memory, retrieve the NS mode if the partition manager supports
diff --git a/inc/vmapi/hf/ffa.h b/inc/vmapi/hf/ffa.h
index 5786b9a..8cc7998 100644
--- a/inc/vmapi/hf/ffa.h
+++ b/inc/vmapi/hf/ffa.h
@@ -621,14 +621,35 @@
  */
 typedef uint64_t ffa_memory_handle_t;
 
-#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
-	((ffa_memory_handle_t)(UINT64_C(1) << 63))
-#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
-	((ffa_memory_handle_t)(UINT64_C(1) << 63))
+enum ffa_memory_handle_allocator {
+	FFA_MEMORY_HANDLE_ALLOCATOR_SPMC = 0,
+	FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR = 1,
+};
 
-#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
+#define FFA_MEMORY_HANDLE_ALLOCATOR_BIT UINT64_C(63)
+#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
+	(UINT64_C(1) << FFA_MEMORY_HANDLE_ALLOCATOR_BIT)
 #define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
 
+static inline ffa_memory_handle_t ffa_memory_handle_make(
+	uint64_t index, enum ffa_memory_handle_allocator allocator)
+{
+	return index | ((uint64_t)allocator << FFA_MEMORY_HANDLE_ALLOCATOR_BIT);
+}
+
+static inline uint64_t ffa_memory_handle_index(ffa_memory_handle_t handle)
+{
+	return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
+}
+
+static inline enum ffa_memory_handle_allocator ffa_memory_handle_allocator(
+	ffa_memory_handle_t handle)
+{
+	return ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) != 0)
+		       ? FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR
+		       : FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
+}
+
 /**
  * A count of VMs. This has the same range as the VM IDs but we give it a
  * different name to make the different semantics clear.
diff --git a/src/arch/aarch64/plat/ffa/absent.c b/src/arch/aarch64/plat/ffa/absent.c
index 4fb34c4..9628b17 100644
--- a/src/arch/aarch64/plat/ffa/absent.c
+++ b/src/arch/aarch64/plat/ffa/absent.c
@@ -161,19 +161,6 @@
 	return false;
 }
 
-ffa_memory_handle_t plat_ffa_memory_handle_make(uint64_t index)
-{
-	return index | FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
-}
-
-bool plat_ffa_memory_handle_allocated_by_current_world(
-	ffa_memory_handle_t handle)
-{
-	(void)handle;
-
-	return false;
-}
-
 uint32_t plat_ffa_other_world_mode(void)
 {
 	return 0U;
diff --git a/src/arch/aarch64/plat/ffa/hypervisor/ffa_memory.c b/src/arch/aarch64/plat/ffa/hypervisor/ffa_memory.c
index 76feee9..6e588cf 100644
--- a/src/arch/aarch64/plat/ffa/hypervisor/ffa_memory.c
+++ b/src/arch/aarch64/plat/ffa/hypervisor/ffa_memory.c
@@ -18,6 +18,11 @@
 #include "hypervisor.h"
 #include "sysregs.h"
 
+enum ffa_memory_handle_allocator plat_ffa_memory_handle_allocator(void)
+{
+	return FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
+}
+
 static struct ffa_value ffa_other_world_mem_reclaim(
 	ffa_memory_handle_t handle, ffa_memory_region_flags_t flags)
 {
@@ -49,18 +54,6 @@
 	return true;
 }
 
-ffa_memory_handle_t plat_ffa_memory_handle_make(uint64_t index)
-{
-	return index | FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
-}
-
-bool plat_ffa_memory_handle_allocated_by_current_world(
-	ffa_memory_handle_t handle)
-{
-	return (handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
-	       FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
-}
-
 uint32_t plat_ffa_other_world_mode(void)
 {
 	return 0U;
diff --git a/src/arch/aarch64/plat/ffa/spmc/ffa_memory.c b/src/arch/aarch64/plat/ffa/spmc/ffa_memory.c
index 5126253..57d7834 100644
--- a/src/arch/aarch64/plat/ffa/spmc/ffa_memory.c
+++ b/src/arch/aarch64/plat/ffa/spmc/ffa_memory.c
@@ -16,6 +16,11 @@
 
 #include "sysregs.h"
 
+enum ffa_memory_handle_allocator plat_ffa_memory_handle_allocator(void)
+{
+	return FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
+}
+
 /** Check validity of the FF-A memory send function attempt. */
 bool plat_ffa_is_memory_send_valid(ffa_id_t receiver, ffa_id_t sender,
 				   uint32_t share_func, bool multiple_borrower)
@@ -57,19 +62,6 @@
 	}
 }
 
-ffa_memory_handle_t plat_ffa_memory_handle_make(uint64_t index)
-{
-	return (index & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK) |
-	       FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
-}
-
-bool plat_ffa_memory_handle_allocated_by_current_world(
-	ffa_memory_handle_t handle)
-{
-	return (handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) ==
-	       FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
-}
-
 uint32_t plat_ffa_other_world_mode(void)
 {
 	return MM_MODE_NS;
diff --git a/src/arch/fake/hypervisor/ffa.c b/src/arch/fake/hypervisor/ffa.c
index 69effef..71fae1d 100644
--- a/src/arch/fake/hypervisor/ffa.c
+++ b/src/arch/fake/hypervisor/ffa.c
@@ -134,18 +134,6 @@
 	return false;
 }
 
-ffa_memory_handle_t plat_ffa_memory_handle_make(uint64_t index)
-{
-	return index;
-}
-
-bool plat_ffa_memory_handle_allocated_by_current_world(
-	ffa_memory_handle_t handle)
-{
-	(void)handle;
-	return false;
-}
-
 uint32_t plat_ffa_other_world_mode(void)
 {
 	return 0U;
diff --git a/src/ffa_memory.c b/src/ffa_memory.c
index 802d26f..fd92e9f 100644
--- a/src/ffa_memory.c
+++ b/src/ffa_memory.c
@@ -70,14 +70,6 @@
 }
 
 /**
- * Extracts the index from a memory handle allocated by Hafnium's current world.
- */
-uint64_t ffa_memory_handle_get_index(ffa_memory_handle_t handle)
-{
-	return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
-}
-
-/**
  * Initialises the next available `struct ffa_memory_share_state`. If `handle`
  * is `FFA_MEMORY_HANDLE_INVALID` then allocates an appropriate handle,
  * otherwise uses the provided handle which is assumed to be globally unique.
@@ -161,7 +153,7 @@
 	 * handle is based on the index.
 	 */
 	if (plat_ffa_memory_handle_allocated_by_current_world(handle)) {
-		uint64_t index = ffa_memory_handle_get_index(handle);
+		uint64_t index = ffa_memory_handle_index(handle);
 
 		if (index < MAX_MEM_SHARES) {
 			share_state = &share_states.share_states[index];
diff --git a/test/inc/test/vmapi/ffa.h b/test/inc/test/vmapi/ffa.h
index 19ce6ba..3ce21d9 100644
--- a/test/inc/test/vmapi/ffa.h
+++ b/test/inc/test/vmapi/ffa.h
@@ -118,7 +118,8 @@
 	struct ffa_memory_region_constituent constituents[],
 	uint32_t constituent_count, uint32_t remaining_constituent_count,
 	uint32_t sent_length, uint32_t total_length,
-	ffa_memory_handle_t *handle, uint64_t allocator_mask);
+	ffa_memory_handle_t *handle,
+	enum ffa_memory_handle_allocator allocator);
 void memory_region_desc_from_rx_fragments(uint32_t fragment_length,
 					  uint32_t total_length,
 					  ffa_memory_handle_t handle,
diff --git a/test/vmapi/common/ffa.c b/test/vmapi/common/ffa.c
index fe72999..92fe80f 100644
--- a/test/vmapi/common/ffa.c
+++ b/test/vmapi/common/ffa.c
@@ -107,7 +107,7 @@
 	struct ffa_memory_region_constituent constituents[],
 	uint32_t constituent_count, uint32_t remaining_constituent_count,
 	uint32_t sent_length, uint32_t total_length,
-	ffa_memory_handle_t *handle, uint64_t allocator_mask)
+	ffa_memory_handle_t *handle, enum ffa_memory_handle_allocator allocator)
 {
 	const ffa_memory_handle_t INVALID_FRAGMENT_HANDLE = 0xffffffffffffffff;
 	ffa_memory_handle_t fragment_handle = INVALID_FRAGMENT_HANDLE;
@@ -138,7 +138,7 @@
 	EXPECT_EQ(sent_length, total_length);
 	EXPECT_EQ(send_ret->func, FFA_SUCCESS_32);
 	*handle = ffa_mem_success_handle(*send_ret);
-	EXPECT_EQ(*handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK, allocator_mask);
+	EXPECT_EQ(ffa_memory_handle_allocator(*handle), allocator);
 	if (fragment_handle != INVALID_FRAGMENT_HANDLE) {
 		EXPECT_EQ(*handle, fragment_handle);
 	}
@@ -165,7 +165,7 @@
 	uint32_t remaining_constituent_count;
 	uint32_t i;
 	struct ffa_partition_msg *retrieve_message = tx_buffer;
-	uint64_t allocator_mask;
+	enum ffa_memory_handle_allocator allocator;
 	bool contains_secure_receiver = false;
 
 	/* Send the first fragment of the memory. */
@@ -210,14 +210,14 @@
 	 * the allocator will be the SPMC.
 	 * Else, it will be the hypervisor.
 	 */
-	allocator_mask = (!ffa_is_vm_id(sender) || contains_secure_receiver)
-				 ? FFA_MEMORY_HANDLE_ALLOCATOR_SPMC
-				 : FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
+	allocator = (!ffa_is_vm_id(sender) || contains_secure_receiver)
+			    ? FFA_MEMORY_HANDLE_ALLOCATOR_SPMC
+			    : FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR;
 
 	send_fragmented_memory_region(
 		&ret, tx_buffer, constituents, constituent_count,
 		remaining_constituent_count, fragment_length, total_length,
-		&handle, allocator_mask);
+		&handle, allocator);
 
 	msg_size = ffa_memory_retrieve_request_init(
 		(struct ffa_memory_region *)retrieve_message->payload, handle,
diff --git a/test/vmapi/ffa_secure_partitions/memory_sharing.c b/test/vmapi/ffa_secure_partitions/memory_sharing.c
index ab43cf3..064ff61 100644
--- a/test/vmapi/ffa_secure_partitions/memory_sharing.c
+++ b/test/vmapi/ffa_secure_partitions/memory_sharing.c
@@ -162,7 +162,7 @@
 	uint32_t fragment_length;
 	uint32_t remaining_constituent_count;
 	ffa_memory_handle_t handle;
-	uint64_t allocator_mask;
+	enum ffa_memory_handle_allocator allocator;
 
 	/* Initialise the memory before giving it. */
 	for (uint32_t i = 0; i < PAGE_SIZE; i++) {
@@ -194,12 +194,12 @@
 
 	ASSERT_TRUE(!ffa_is_vm_id(sender_id) ||
 		    !ffa_is_vm_id(service1_info->vm_id));
-	allocator_mask = FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
+	allocator = FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
 
 	send_fragmented_memory_region(
 		&ret, mb.send, constituents, ARRAY_SIZE(constituents),
 		remaining_constituent_count, fragment_length, total_length,
-		&handle, allocator_mask);
+		&handle, allocator);
 
 	ret = sp_ffa_mem_retrieve_cmd_send(sender_id, receiver_id, handle,
 					   FFA_VERSION_1_0);