feat(realm): assign MECID when creating realms

This change allows TFTF to assign a MECID to every realm that is
created by passing an extra parameter to the Realm creation helpers.

Signed-off-by: Juan Pablo Conde <juanpablo.conde@arm.com>
Change-Id: I89bf08011eb005d949a195b406b073955f23f5ad
diff --git a/include/common/test_helpers.h b/include/common/test_helpers.h
index 6482d6d..6a4b412 100644
--- a/include/common/test_helpers.h
+++ b/include/common/test_helpers.h
@@ -17,6 +17,9 @@
 #include <uuid_utils.h>
 #include <uuid.h>
 
+#define TEST_MECID1	(unsigned short)1
+#define TEST_MECID2	(unsigned short)2
+
 typedef struct {
 	uintptr_t addr;
 	size_t size;
diff --git a/include/lib/aarch64/arch.h b/include/lib/aarch64/arch.h
index 2a9d0d2..f460016 100644
--- a/include/lib/aarch64/arch.h
+++ b/include/lib/aarch64/arch.h
@@ -483,6 +483,11 @@
 #define ID_AA64MMFR3_EL1_TCRX_WIDTH		U(4)
 #define ID_AA64MMFR3_EL1_TCR2_SUPPORTED		ULL(0x1)
 
+#define ID_AA64MMFR3_EL1_MEC_SHIFT              U(28)
+#define ID_AA64MMFR3_EL1_MEC_MASK               ULL(0xf)
+#define ID_AA64MMFR3_EL1_MEC_WIDTH		U(4)
+#define ID_AA64MMFR3_EL1_MEC_SUPPORTED		ULL(0x1)
+
 /* ID_AA64PFR1_EL1 definitions */
 #define ID_AA64PFR1_EL1_DF2_SHIFT		U(56)
 #define ID_AA64PFR1_EL1_DF2_WIDTH		U(4)
@@ -1825,4 +1830,10 @@
 /* RNDRRS definition */
 #define RNDRRS			S3_3_C2_C4_1
 
+/* MEC Registers */
+#define MECIDR_EL2				S3_4_C10_C8_7
+
+#define MECIDR_EL2_MECIDWidthm1_SHIFT		U(0)
+#define MECIDR_EL2_MECIDWidthm1_WIDTH		U(4)
+
 #endif /* ARCH_H */
diff --git a/include/lib/aarch64/arch_features.h b/include/lib/aarch64/arch_features.h
index 3b4fffc..fc004b3 100644
--- a/include/lib/aarch64/arch_features.h
+++ b/include/lib/aarch64/arch_features.h
@@ -583,4 +583,10 @@
 	return EXTRACT(ID_AA64DFR0_DOUBLELOCK, read_id_aa64dfr0_el1())
 			>= DOUBLELOCK_IMPLEMENTED;
 }
+
+static inline bool is_feat_mec_supported(void)
+{
+	return EXTRACT(ID_AA64MMFR3_EL1_MEC, read_id_aa64mmfr3_el1())
+		== ID_AA64MMFR3_EL1_MEC_SUPPORTED;
+}
 #endif /* ARCH_FEATURES_H */
diff --git a/include/lib/aarch64/arch_helpers.h b/include/lib/aarch64/arch_helpers.h
index f562018..6847ce3 100644
--- a/include/lib/aarch64/arch_helpers.h
+++ b/include/lib/aarch64/arch_helpers.h
@@ -733,6 +733,9 @@
 DEFINE_RENAME_SYSREG_RW_FUNCS(tpidr_el2, TPIDR_EL2)
 DEFINE_RENAME_SYSREG_RW_FUNCS(vtcr_el2, VTCR_EL2)
 
+/* Memory Encryption Contexts (MEC) */
+DEFINE_RENAME_SYSREG_READ_FUNC(mecidr_el2, MECIDR_EL2)
+
 static inline u_register_t read_sp(void)
 {
 	u_register_t v;
diff --git a/include/runtime_services/host_realm_managment/host_realm_helper.h b/include/runtime_services/host_realm_managment/host_realm_helper.h
index 3c94d02..d2d2fc1 100644
--- a/include/runtime_services/host_realm_managment/host_realm_helper.h
+++ b/include/runtime_services/host_realm_managment/host_realm_helper.h
@@ -21,7 +21,8 @@
 		long sl,
 		const u_register_t *rec_flag,
 		unsigned int rec_count,
-		unsigned int num_aux_planes);
+		unsigned int num_aux_planes,
+		unsigned short mecid);
 
 /*
  * Creates realm, initializes heap, creates RTTs and also
@@ -34,7 +35,8 @@
 		long sl,
 		const u_register_t *rec_flag,
 		unsigned int rec_count,
-		unsigned int num_aux_planes);
+		unsigned int num_aux_planes,
+		unsigned short mecid);
 
 /*
  * Creates realm, initializes heap, creates RTTs,
@@ -47,7 +49,8 @@
 		long sl,
 		const u_register_t *rec_flag,
 		unsigned int rec_count,
-		unsigned int num_aux_planes);
+		unsigned int num_aux_planes,
+		unsigned short mecid);
 bool host_destroy_realm(struct realm *realm_ptr);
 void host_rec_send_sgi(struct realm *realm_ptr,
 		unsigned int sgi, unsigned int rec_num);
diff --git a/include/runtime_services/host_realm_managment/host_realm_rmi.h b/include/runtime_services/host_realm_managment/host_realm_rmi.h
index 05570fd..f018f9a 100644
--- a/include/runtime_services/host_realm_managment/host_realm_rmi.h
+++ b/include/runtime_services/host_realm_managment/host_realm_rmi.h
@@ -1363,6 +1363,7 @@
 	u_register_t     host_mpidr[MAX_REC_COUNT];
 	u_register_t     num_aux;
 	u_register_t     rmm_feat_reg0;
+	u_register_t     rmm_feat_reg1;
 	u_register_t     ipa_ns_buffer;
 	u_register_t     ns_buffer_size;
 	u_register_t     aux_pages_all_rec[MAX_REC_COUNT][REC_PARAMS_AUX_GRANULES];
@@ -1375,6 +1376,7 @@
 	bool             rtt_tree_single;
 	bool		 rtt_s2ap_enc_indirect;
 	unsigned short   vmid;
+	unsigned short   mecid;
 	enum realm_state state;
 	long start_level;
 	u_register_t     aux_rtt_addr[MAX_AUX_PLANE_COUNT];
diff --git a/include/runtime_services/host_realm_managment/realm_def.h b/include/runtime_services/host_realm_managment/realm_def.h
index 5d3a196..6fca05c 100644
--- a/include/runtime_services/host_realm_managment/realm_def.h
+++ b/include/runtime_services/host_realm_managment/realm_def.h
@@ -107,4 +107,6 @@
 
 #define REALM_TOKEN_BUF_SIZE		GRANULE_SIZE
 
+#define DEFAULT_MECID			(unsigned short)0
+
 #endif /* REALM_DEF_H */