test(realm): fix multi rec PMU tests

- set different values of PMU event counters for each rec
- check PMU counters are preserved for each rec

Change-Id: I7c4ad3971d4a10b4515be0dfe096bebf8d903c71
Signed-off-by: Shruti Gupta <shruti.gupta@arm.com>
diff --git a/include/runtime_services/host_realm_managment/host_realm_pmu.h b/include/runtime_services/host_realm_managment/host_realm_pmu.h
index 844bb29..66bd385 100644
--- a/include/runtime_services/host_realm_managment/host_realm_pmu.h
+++ b/include/runtime_services/host_realm_managment/host_realm_pmu.h
@@ -23,7 +23,25 @@
 #define GET_PMU_CNT	\
 	((read_pmcr_el0() >> PMCR_EL0_N_SHIFT) & PMCR_EL0_N_MASK)
 
-void host_set_pmu_state(void);
-bool host_check_pmu_state(void);
+#define MAX_COUNTERS	31U
+
+struct pmu_registers {
+	unsigned long pmcr_el0;
+	unsigned long pmcntenset_el0;
+	unsigned long pmovsset_el0;
+	unsigned long pmintenset_el1;
+	unsigned long pmccntr_el0;
+	unsigned long pmccfiltr_el0;
+	unsigned long pmuserenr_el0;
+	unsigned long pmevcntr_el0[MAX_COUNTERS];
+	unsigned long pmevtyper_el0[MAX_COUNTERS];
+	unsigned long pmselr_el0;
+	unsigned long pmxevcntr_el0;
+	unsigned long pmxevtyper_el0;
+} __aligned(64);
+
+void host_set_pmu_state(struct pmu_registers *pmu_ptr);
+
+bool host_check_pmu_state(struct pmu_registers *pmu_ptr);
 
 #endif /* HOST_REALM_PMU_H */
diff --git a/realm/realm_pmuv3.c b/realm/realm_pmuv3.c
index 0d4782a..214f6df 100644
--- a/realm/realm_pmuv3.c
+++ b/realm/realm_pmuv3.c
@@ -19,7 +19,6 @@
 #define PMU_EVT_MEM_ACCESS	0x13
 
 #define NOP_REPETITIONS		50
-#define MAX_COUNTERS		32
 
 #define PRE_OVERFLOW		~(0xF)
 
diff --git a/tftf/tests/runtime_services/host_realm_managment/host_pmuv3.c b/tftf/tests/runtime_services/host_realm_managment/host_pmuv3.c
index ceca36d..16e87e9 100644
--- a/tftf/tests/runtime_services/host_realm_managment/host_pmuv3.c
+++ b/tftf/tests/runtime_services/host_realm_managment/host_pmuv3.c
@@ -15,8 +15,6 @@
 #include <host_realm_pmu.h>
 #include <platform.h>
 
-#define MAX_COUNTERS		31
-
 /* PMCCFILTR_EL0 mask */
 #define PMCCFILTR_EL0_MASK (	  \
 	PMCCFILTR_EL0_P_BIT	| \
@@ -82,32 +80,10 @@
 	}							\
 }
 
-struct pmu_registers {
-	unsigned long pmcr_el0;
-	unsigned long pmcntenset_el0;
-	unsigned long pmovsset_el0;
-	unsigned long pmintenset_el1;
-	unsigned long pmccntr_el0;
-	unsigned long pmccfiltr_el0;
-	unsigned long pmuserenr_el0;
-
-	unsigned long pmevcntr_el0[MAX_COUNTERS];
-	unsigned long pmevtyper_el0[MAX_COUNTERS];
-
-	unsigned long pmselr_el0;
-	unsigned long pmxevcntr_el0;
-	unsigned long pmxevtyper_el0;
-
-} __aligned(CACHE_WRITEBACK_GRANULE);
-
-static struct pmu_registers pmu_state[PLATFORM_CORE_COUNT];
-
-void host_set_pmu_state(void)
+void host_set_pmu_state(struct pmu_registers *pmu_ptr)
 {
-	unsigned int core_pos = platform_get_core_pos(read_mpidr_el1());
-	struct pmu_registers *pmu_ptr = &pmu_state[core_pos];
-	unsigned int num_cnts = GET_PMU_CNT;
 	unsigned long val;
+	unsigned int num_cnts = GET_PMU_CNT;
 
 	val = read_pmcr_el0() | PMCR_EL0_DP_BIT;
 	pmu_ptr->pmcr_el0 = val;
@@ -181,12 +157,13 @@
 	pmu_ptr->pmxevtyper_el0 = read_pmxevtyper_el0();
 }
 
-bool host_check_pmu_state(void)
+bool host_check_pmu_state(struct pmu_registers *pmu_ptr)
 {
-	unsigned int core_pos = platform_get_core_pos(read_mpidr_el1());
-	struct pmu_registers *pmu_ptr = &pmu_state[core_pos];
-	unsigned int num_cnts = GET_PMU_CNT;
 	unsigned long val, read_val;
+	unsigned int num_cnts = GET_PMU_CNT;
+
+	INFO("Check PMU core_pos=%u num_cnts=%u\n",
+			platform_get_core_pos(read_mpidr_el1()), num_cnts);
 
 	CHECK_PMREG(pmcr_el0);
 	CHECK_PMREG(pmcntenset_el0);
diff --git a/tftf/tests/runtime_services/realm_payload/host_realm_payload_multiple_rec_tests.c b/tftf/tests/runtime_services/realm_payload/host_realm_payload_multiple_rec_tests.c
index 2aeca64..00d2d57 100644
--- a/tftf/tests/runtime_services/realm_payload/host_realm_payload_multiple_rec_tests.c
+++ b/tftf/tests/runtime_services/realm_payload/host_realm_payload_multiple_rec_tests.c
@@ -22,6 +22,7 @@
 static uint64_t is_secondary_cpu_on;
 static struct realm realm;
 static struct realm realm1;
+static struct pmu_registers pmu_state[PLATFORM_CORE_COUNT];
 
 /*
  * Test tries to create max Rec
@@ -587,6 +588,9 @@
 	spin_lock(&secondary_cpu_lock);
 	i = is_secondary_cpu_on++;
 	spin_unlock(&secondary_cpu_lock);
+
+	host_set_pmu_state(&pmu_state[i]);
+
 	ret1 = host_enter_realm_execute(&realm, REALM_PMU_COUNTER, RMI_EXIT_HOST_CALL, i);
 	if (!ret1) {
 		return TEST_RESULT_FAIL;
@@ -599,10 +603,16 @@
 	if (!ret1) {
 		return TEST_RESULT_FAIL;
 	}
+
 	ret1 = host_enter_realm_execute(&realm1, REALM_PMU_PRESERVE, RMI_EXIT_HOST_CALL, i);
-	if (ret1) {
+	if (!ret1) {
+		return TEST_RESULT_FAIL;
+	}
+
+	if (host_check_pmu_state(&pmu_state[i])) {
 		return TEST_RESULT_SUCCESS;
 	}
+
 	return TEST_RESULT_FAIL;
 }
 
@@ -623,7 +633,7 @@
 	u_register_t rmm_feat_reg0;
 	u_register_t rec_flag[MAX_REC_COUNT];
 	bool ret1 = false, ret2;
-	unsigned int num_cnts, rec_count, i;
+	unsigned int rec_count, i, num_cnts;
 	u_register_t other_mpidr, my_mpidr, ret;
 	int cpu_node;
 	long sl = RTT_MIN_LEVEL;
@@ -649,7 +659,8 @@
 	}
 
 	num_cnts = EXTRACT(RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS, rmm_feat_reg0);
-	host_set_pmu_state();
+	host_set_pmu_state(&pmu_state[0U]);
+
 	is_secondary_cpu_on = 0;
 	my_mpidr = read_mpidr_el1() & MPID_MASK;
 
@@ -776,6 +787,10 @@
 		goto test_exit2;
 	}
 
+	if (!host_check_pmu_state(&pmu_state[0U])) {
+		goto test_exit;
+	}
+
 	i = 0U;
 
 	/* Turn on all CPUs */
@@ -825,9 +840,5 @@
 		return TEST_RESULT_FAIL;
 	}
 
-	if (!host_check_pmu_state()) {
-		return TEST_RESULT_FAIL;
-	}
-
 	return TEST_RESULT_SUCCESS;
 }
diff --git a/tftf/tests/runtime_services/realm_payload/host_realm_payload_tests.c b/tftf/tests/runtime_services/realm_payload/host_realm_payload_tests.c
index 0c7289a..760a286 100644
--- a/tftf/tests/runtime_services/realm_payload/host_realm_payload_tests.c
+++ b/tftf/tests/runtime_services/realm_payload/host_realm_payload_tests.c
@@ -26,6 +26,7 @@
 extern const char *rmi_exit[];
 
 static struct realm realm[MAX_REALM_COUNT];
+static struct pmu_registers pmu_state;
 
 #if ENABLE_PAUTH
 static uint128_t pauth_keys_before[NUM_KEYS];
@@ -440,7 +441,7 @@
 	}
 
 	num_cnts = EXTRACT(RMI_FEATURE_REGISTER_0_PMU_NUM_CTRS, rmm_feat_reg0);
-	host_set_pmu_state();
+	host_set_pmu_state(&pmu_state);
 
 	feature_flag = RMI_FEATURE_REGISTER_0_PMU_EN |
 			INPLACE(FEATURE_PMU_NUM_CTRS, num_cnts);
@@ -471,7 +472,7 @@
 		return TEST_RESULT_FAIL;
 	}
 
-	if (!host_check_pmu_state()) {
+	if (!host_check_pmu_state(&pmu_state)) {
 		return TEST_RESULT_FAIL;
 	}