feat(rme/spm): code reuse and refactoring

- Reuse of existing wait_for_non_lead_cpus() function helper.
- Create a function to reset delegated buffers to undelegated state.

Signed-off-by: Nabil Kahlouche <nabil.kahlouche@arm.com>
Change-Id: If5c24da0002d100419220fe0e5409b73596e773c
diff --git a/tftf/tests/runtime_services/realm_payload/realm_payload_spm_test.c b/tftf/tests/runtime_services/realm_payload/realm_payload_spm_test.c
index 22c1752..70b1b09 100644
--- a/tftf/tests/runtime_services/realm_payload/realm_payload_spm_test.c
+++ b/tftf/tests/runtime_services/realm_payload/realm_payload_spm_test.c
@@ -100,6 +100,25 @@
 	return TEST_RESULT_SUCCESS;
 }
 
+static test_result_t reset_buffer_del_spm_rmi(void)
+{
+	u_register_t retrmm;
+
+	for (uint32_t i = 0; i < (NUM_GRANULES * PLATFORM_CORE_COUNT) ; i++) {
+		if (bufferstate[i] == B_DELEGATED) {
+			retrmm = realm_granule_undelegate(
+				(u_register_t)&bufferdelegate[i * GRANULE_SIZE]);
+			if (retrmm != 0UL) {
+				ERROR("Undelegate operation returns fail, %lx\n",
+				retrmm);
+				return TEST_RESULT_FAIL;
+			}
+			bufferstate[i] = B_UNDELEGATED;
+		}
+	}
+	return TEST_RESULT_SUCCESS;
+}
+
 /*
  * SPM functions for the direct messaging
  */
@@ -332,39 +351,11 @@
 	}
 
 	VERBOSE("Waiting for secondary CPUs to turn off ...\n");
-
-	for_each_cpu(cpu_node) {
-		mpidr = tftf_get_mpidr_from_node(cpu_node);
-		if (mpidr == lead_mpid) {
-			continue;
-		}
-
-		while (tftf_psci_affinity_info(mpidr, MPIDR_AFFLVL0) !=
-				PSCI_STATE_OFF) {
-			continue;
-		}
-
-	}
-
-	for (int i = 0; i < (NUM_GRANULES * PLATFORM_CORE_COUNT) ; i++) {
-		if (bufferstate[i] == B_DELEGATED) {
-			retrmm = realm_granule_undelegate(
-				(u_register_t)&bufferdelegate[i * GRANULE_SIZE]);
-			bufferstate[i] = B_UNDELEGATED;
-			if (retrmm != 0UL) {
-				tftf_testcase_printf("Delegate operation returns fail, %lx\n", retrmm);
-				return TEST_RESULT_FAIL;
-			}
-		}
-	}
+	wait_for_non_lead_cpus();
 
 	VERBOSE("Done exiting.\n");
 
-	/**********************************************************************
-	 * All tests passed.
-	 **********************************************************************/
-
-	return TEST_RESULT_SUCCESS;
+	return reset_buffer_del_spm_rmi();
 }
 
 /*