refactor(memory share): continue memory sharing

Continue memory sharing operation between NWd and SWd.
If the handle is allocated by the SPMC, the hypervisor forward the
FFA_MEM_FRAG_TX to the SWd.

The SPMC can only handle calls to FFA_MEM_FRAG_TX ABI if it relates to a
handle allocated by the SPMC. Otherwise returns error.

Change-Id: I9801f1fd5fdc84af7317ac7570c428ece5da53f6
Signed-off-by: J-Alves <joao.alves@arm.com>
diff --git a/inc/hf/arch/plat/ffa.h b/inc/hf/arch/plat/ffa.h
index d8e6844..ff28b07 100644
--- a/inc/hf/arch/plat/ffa.h
+++ b/inc/hf/arch/plat/ffa.h
@@ -342,3 +342,11 @@
 struct ffa_value plat_ffa_other_world_mem_retrieve(
 	struct vm_locked to_locked, struct ffa_memory_region *retrieve_request,
 	uint32_t length, struct mpool *page_pool);
+
+/**
+ * Handles the continuation of the memory send operation in case the memory
+ * region descriptor contains multiple segments.
+ */
+struct ffa_value plat_ffa_other_world_mem_send_continue(
+	struct vm *from, void *fragment, uint32_t fragment_length,
+	ffa_memory_handle_t handle, struct mpool *page_pool);
diff --git a/inc/hf/ffa_memory.h b/inc/hf/ffa_memory.h
index 1fccfbf..36a325b 100644
--- a/inc/hf/ffa_memory.h
+++ b/inc/hf/ffa_memory.h
@@ -23,10 +23,6 @@
 					  uint32_t fragment_length,
 					  ffa_memory_handle_t handle,
 					  struct mpool *page_pool);
-struct ffa_value ffa_memory_other_world_send_continue(
-	struct vm_locked from_locked, struct vm_locked to_locked,
-	void *fragment, uint32_t fragment_length, ffa_memory_handle_t handle,
-	struct mpool *page_pool);
 struct ffa_value ffa_memory_retrieve(struct vm_locked to_locked,
 				     struct ffa_memory_region *retrieve_request,
 				     uint32_t retrieve_request_length,
diff --git a/inc/hf/ffa_memory_internal.h b/inc/hf/ffa_memory_internal.h
index be5897d..dbf61582 100644
--- a/inc/hf/ffa_memory_internal.h
+++ b/inc/hf/ffa_memory_internal.h
@@ -105,6 +105,15 @@
 			  struct ffa_memory_share_state **share_state_ret);
 struct share_states_locked share_states_lock(void);
 void share_states_unlock(struct share_states_locked *share_states);
+void share_state_free(struct share_states_locked share_states,
+		      struct ffa_memory_share_state *share_state,
+		      struct mpool *page_pool);
+uint32_t share_state_next_fragment_offset(
+	struct share_states_locked share_states,
+	struct ffa_memory_share_state *share_state);
+/** Checks whether the given share state has been fully sent. */
+bool share_state_sending_complete(struct share_states_locked share_states,
+				  struct ffa_memory_share_state *share_state);
 void dump_share_states(void);
 
 /**
@@ -136,21 +145,38 @@
 	uint32_t share_func, struct ffa_memory_access *receivers,
 	uint32_t receivers_count, struct mpool *page_pool, bool clear,
 	uint32_t *orig_from_mode_ret);
-bool ffa_region_group_identity_map(
-	struct vm_locked vm_locked,
+struct ffa_value ffa_memory_send_complete(
+	struct vm_locked from_locked, struct share_states_locked share_states,
+	struct ffa_memory_share_state *share_state, struct mpool *page_pool,
+	uint32_t *orig_from_mode_ret);
+struct ffa_value ffa_memory_send_continue_validate(
+	struct share_states_locked share_states, ffa_memory_handle_t handle,
+	struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
+	struct mpool *page_pool);
+struct ffa_value ffa_send_check_update(
+	struct vm_locked from_locked,
 	struct ffa_memory_region_constituent **fragments,
-	const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
-	uint32_t mode, struct mpool *ppool, bool commit);
+	uint32_t *fragment_constituent_counts, uint32_t fragment_count,
+	uint32_t share_func, struct ffa_memory_access *receivers,
+	uint32_t receivers_count, struct mpool *page_pool, bool clear,
+	uint32_t *orig_from_mode_ret);
 struct ffa_value ffa_retrieve_check_transition(
 	struct vm_locked to, uint32_t share_func,
 	struct ffa_memory_region_constituent **fragments,
 	uint32_t *fragment_constituent_counts, uint32_t fragment_count,
 	uint32_t memory_to_attributes, uint32_t *to_mode);
-uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
-					ffa_vm_id_t receiver);
 struct ffa_value ffa_retrieve_check_update(
 	struct vm_locked to_locked, ffa_vm_id_t from_id,
 	struct ffa_memory_region_constituent **fragments,
 	uint32_t *fragment_constituent_counts, uint32_t fragment_count,
 	uint32_t memory_to_attributes, uint32_t share_func, bool clear,
 	struct mpool *page_pool);
+uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
+					ffa_vm_id_t receiver);
+bool ffa_region_group_identity_map(
+	struct vm_locked vm_locked,
+	struct ffa_memory_region_constituent **fragments,
+	const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
+	uint32_t mode, struct mpool *ppool, bool commit);
+bool memory_region_receivers_from_other_world(
+	struct ffa_memory_region *memory_region);