feat(realm): add tests for FEAT_MEC

This patch creates two test in order to verify the correct
assignment of MECIDs to realms.

Signed-off-by: Juan Pablo Conde <juanpablo.conde@arm.com>
Change-Id: I9de84f1ba8a8b42e55b1ea163f90e9daf56c74c9
diff --git a/tftf/tests/runtime_services/realm_payload/host_realm_mec_tests.c b/tftf/tests/runtime_services/realm_payload/host_realm_mec_tests.c
new file mode 100644
index 0000000..54c4d86
--- /dev/null
+++ b/tftf/tests/runtime_services/realm_payload/host_realm_mec_tests.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2021-2025, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+
+#include <arch_features.h>
+#include <debug.h>
+#include <host_realm_helper.h>
+#include <host_realm_mem_layout.h>
+#include <test_helpers.h>
+
+/*
+ * @Test_Aim@ Test MECID assignment to Realms
+ * Test whether two realms accept different MECIDs
+ */
+test_result_t host_realm_test_mecid(void)
+{
+	bool ret1 = false, ret2 = false, fail = false;
+	u_register_t rec_flag[] = {RMI_RUNNABLE};
+	struct realm realm1, realm2;
+	u_register_t feature_flag0 = 0UL;
+	unsigned long feat_reg1;
+	long sl = RTT_MIN_LEVEL;
+
+	SKIP_TEST_IF_RME_NOT_SUPPORTED_OR_RMM_IS_TRP();
+
+	if (!is_feat_mec_supported()) {
+		return TEST_RESULT_SKIPPED;
+	}
+
+	/* Only test when RMM v1.1 is supported */
+	if ((host_rmi_features(1UL, &feat_reg1) != 0UL) || (feat_reg1 == 0UL)) {
+		return TEST_RESULT_SKIPPED;
+	}
+
+	if (is_feat_52b_on_4k_2_supported()) {
+		feature_flag0 = RMI_FEATURE_REGISTER_0_LPA2;
+		sl = RTT_MIN_LEVEL_LPA2;
+	}
+
+	if (!host_create_activate_realm_payload(&realm1, (u_register_t)REALM_IMAGE_BASE,
+				feature_flag0, 0U, sl, rec_flag, 1U, 0U, TEST_MECID1)) {
+		fail = true;
+	}
+
+	if (!host_create_activate_realm_payload(&realm2, (u_register_t)REALM_IMAGE_BASE,
+				feature_flag0, 0U, sl, rec_flag, 1U, 0U, TEST_MECID2)) {
+		fail = true;
+		goto destroy_realm1;
+	}
+
+	ret2 = host_destroy_realm(&realm2);
+destroy_realm1:
+	ret1 = host_destroy_realm(&realm1);
+
+	if (fail || !ret1 || !ret2) {
+		ERROR("%s(): fail=%d destroy1=%d destroy2=%d\n",
+				__func__, fail, ret1, ret2);
+		return TEST_RESULT_FAIL;
+	}
+
+	return TEST_RESULT_SUCCESS;
+}
+
+/*
+ * @Test_Aim@ Test MECID assignment to Realms
+ * Test whether two realms accept the same MECID
+ */
+test_result_t host_realm_test_mecid_fault(void)
+{
+	bool ret1 = false, ret2 = false, fail = false;
+	u_register_t rec_flag[] = {RMI_RUNNABLE};
+	struct realm realm1, realm2;
+	u_register_t feature_flag0 = 0UL;
+	unsigned long feat_reg1;
+	long sl = RTT_MIN_LEVEL;
+
+	SKIP_TEST_IF_RME_NOT_SUPPORTED_OR_RMM_IS_TRP();
+
+	if (!is_feat_mec_supported()) {
+		return TEST_RESULT_SKIPPED;
+	}
+
+	/* Only test when RMM v1.1 is supported */
+	if ((host_rmi_features(1UL, &feat_reg1) != 0UL) || (feat_reg1 == 0UL)) {
+		return TEST_RESULT_SKIPPED;
+	}
+
+	if (is_feat_52b_on_4k_2_supported()) {
+		feature_flag0 = RMI_FEATURE_REGISTER_0_LPA2;
+		sl = RTT_MIN_LEVEL_LPA2;
+	}
+
+	if (!host_create_activate_realm_payload(&realm1, (u_register_t)REALM_IMAGE_BASE,
+				feature_flag0, 0U, sl, rec_flag, 1U, 0U, TEST_MECID1)) {
+		fail = true;
+	}
+
+	if (!host_create_activate_realm_payload(&realm2, (u_register_t)REALM_IMAGE_BASE,
+				feature_flag0, 0U, sl, rec_flag, 1U, 0U, TEST_MECID1)) {
+		/*
+		 * Creation should fail as there should not be two Realms with the same
+		 * MECID.
+		 */
+		ret2 = true;
+
+		goto destroy_realm1;
+	}
+	fail = true;
+
+	ret2 = host_destroy_realm(&realm2);
+destroy_realm1:
+	ret1 = host_destroy_realm(&realm1);
+
+	if (fail || !ret1 || !ret2) {
+		ERROR("%s(): fail=%d destroy1=%d destroy2=%d\n",
+				__func__, fail, ret1, ret2);
+		return TEST_RESULT_FAIL;
+	}
+
+	return TEST_RESULT_SUCCESS;
+}
diff --git a/tftf/tests/tests-realm-payload.mk b/tftf/tests/tests-realm-payload.mk
index 5859d31..b5e245b 100644
--- a/tftf/tests/tests-realm-payload.mk
+++ b/tftf/tests/tests-realm-payload.mk
@@ -18,6 +18,7 @@
 		host_realm_lpa2_tests.c					\
 		host_realm_mpam_tests.c					\
 		host_realm_brbe_tests.c					\
+		host_realm_mec_tests.c					\
 	)
 
 TESTS_SOURCES	+=							\
diff --git a/tftf/tests/tests-realm-payload.xml b/tftf/tests/tests-realm-payload.xml
index c9dc0cb..40825ee 100644
--- a/tftf/tests/tests-realm-payload.xml
+++ b/tftf/tests/tests-realm-payload.xml
@@ -160,6 +160,9 @@
 	  <!-- Test case related to FEAT_BRBE -->
 	  <testcase name="Test if BRBE realted registers are preserved "
 	  function="host_realm_test_brbe_save_restore" />
+	  <!-- Test cases related to FEAT_MEC -->
+	  <testcase name="Test Realms with different MECIDs" function="host_realm_test_mecid" />
+	  <testcase name="Test Realms with the same MECID" function="host_realm_test_mecid_fault" />
 	 <!-- Test case for EL3-RMM IDE KM Interface -->
 	  <testcase name="Test for Root Port Key management interface"
 	  function="host_realm_test_root_port_key_management" />