refactor(lib/s2tt): move the S2TT component to its own library

Minor renames also done as part of the refactor.

Signed-off-by: Javier Almansa Sobrino <javier.almansasobrino@arm.com>
Change-Id: If414cbd2669063853aebaa5d7c0b05ec36dc3c46
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 6ffd4b5..d97c80f 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -16,6 +16,7 @@
               rmm-lib-realm
               rmm-lib-rmm_el3_ifc
               rmm-lib-smc
+              rmm-lib-s2tt
               rmm-lib-timers
               rmm-lib-xlat
               t_cose)
@@ -32,6 +33,7 @@
 add_subdirectory("realm")
 add_subdirectory("rmm_el3_ifc")
 add_subdirectory("smc")
+add_subdirectory("s2tt")
 add_subdirectory("t_cose")
 add_subdirectory("timers")
 add_subdirectory("xlat")
diff --git a/lib/realm/CMakeLists.txt b/lib/realm/CMakeLists.txt
index 978f20a..bf2ce52 100644
--- a/lib/realm/CMakeLists.txt
+++ b/lib/realm/CMakeLists.txt
@@ -23,25 +23,28 @@
     PRIVATE rmm-lib-arch
             rmm-lib-common
             rmm-lib-debug
-            rmm-lib-gic
-            rmm-lib-xlat
-            rmm-lib-allocator
-            rmm-lib-attestation
-            rmm-platform)
+            rmm-lib-xlat)
+
+# Needed to link with the platform callbacks for buffer map/unmap
+if(RMM_ARCH STREQUAL fake_host)
+    target_link_libraries(rmm-lib-realm
+        PRIVATE rmm-platform)
+endif()
 
 target_link_libraries(rmm-lib-realm
-    PUBLIC rmm-lib-measurement
+    PUBLIC rmm-lib-attestation
+           rmm-lib-gic
+           rmm-lib-measurement
+           rmm-lib-s2tt
            rmm-lib-smc)
 
 target_include_directories(rmm-lib-realm
     PUBLIC "include"
     PRIVATE "src/include/${RMM_ARCH}"
-    PRIVATE "src/include"
-    PRIVATE "src")
+    PRIVATE "src/include")
 
 target_sources(rmm-lib-realm
     PRIVATE "src/buffer.c"
-            "src/granule.c"
-            "src/s2tt.c")
+            "src/granule.c")
 
 include (tests/CMakeLists.txt)
diff --git a/lib/realm/include/realm.h b/lib/realm/include/realm.h
index fc6b061..b9dbfd5 100644
--- a/lib/realm/include/realm.h
+++ b/lib/realm/include/realm.h
@@ -16,30 +16,6 @@
 #define REALM_ACTIVE		1U
 #define REALM_SYSTEM_OFF	2U
 
-/*
- * Stage 2 configuration of the Realm
- */
-struct realm_s2_context {
-	/* Number of IPA bits */
-	unsigned int ipa_bits;
-
-	/* Starting level of the stage 2 translation */
-	int s2_starting_level;
-
-	/* Number of concatenated starting level rtts */
-	unsigned int num_root_rtts;
-
-	/* First level RTT, pointed to by Realm TTBR */
-	struct granule *g_rtt;
-
-	/* Virtual Machine Identifier */
-	unsigned int vmid;
-
-	/*
-	 * TODO: we will need other translation regime state, e.g. TCR, MAIR(?).
-	 */
-};
-
 /* struct rd is protected by the rd granule lock */
 struct rd {
 	/*
@@ -67,7 +43,7 @@
 	unsigned char measurement[MEASUREMENT_SLOT_NR][MAX_MEASUREMENT_SIZE];
 
 	/* Stage 2 configuration of the Realm */
-	struct realm_s2_context s2_ctx;
+	struct s2tt_context s2_ctx;
 
 	/* Number of auxiliary REC granules for the Realm */
 	unsigned int num_rec_aux;
diff --git a/lib/realm/tests/buffer.cpp b/lib/realm/tests/buffer.cpp
index 444c61c..74a4d7c 100644
--- a/lib/realm/tests/buffer.cpp
+++ b/lib/realm/tests/buffer.cpp
@@ -348,7 +348,7 @@
 	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
-	test_granule = realm_test_util_granule_struct_base() - 1U;
+	test_granule = test_helpers_granule_struct_base() - 1U;
 	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
 						(unsigned long)(SLOT_NS + 1U),
 						(unsigned long)NR_CPU_SLOTS);
@@ -382,7 +382,7 @@
 	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
-	test_granule = realm_test_util_granule_struct_base() +
+	test_granule = test_helpers_granule_struct_base() +
 							HOST_NR_GRANULES;
 	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
 						(unsigned long)(SLOT_NS + 1U),
diff --git a/lib/realm/tests/granule.cpp b/lib/realm/tests/granule.cpp
index c6b02ae..56ec940 100644
--- a/lib/realm/tests/granule.cpp
+++ b/lib/realm/tests/granule.cpp
@@ -127,7 +127,7 @@
 		 * Clean RMM's internal struct granule array
 		 * for a clean state for the next tests.
 		 */
-		memset((void *)realm_test_util_granule_struct_base(), 0,
+		memset((void *)test_helpers_granule_struct_base(), 0,
 			sizeof(struct granule) *
 					test_helpers_get_nr_granules());
 
@@ -159,7 +159,7 @@
 
 	for (unsigned int i = 0U; i < 3; i++) {
 		/* Calculate the expected granule address */
-		expected_granule = realm_test_util_granule_struct_base() +
+		expected_granule = test_helpers_granule_struct_base() +
 							granule_indexes[i];
 		/* Calculated the expected PA for the granule */
 		addr = (granule_indexes[i] * GRANULE_SIZE) +
@@ -254,7 +254,7 @@
 	 * granules in between.
 	 ******************************************************************/
 	for (unsigned int i = 0U; i < 3U; i++) {
-		granule = realm_test_util_granule_struct_base() +
+		granule = test_helpers_granule_struct_base() +
 							granule_indexes[i];
 		expected_address = (granule_indexes[i] * GRANULE_SIZE) +
 						host_util_get_granule_base();
@@ -296,7 +296,7 @@
 	 ******************************************************************/
 
 	idx += (unsigned long)test_helpers_get_rand_in_range(1UL, 10UL);
-	granule = realm_test_util_granule_struct_base() + idx;
+	granule = test_helpers_granule_struct_base() + idx;
 	test_helpers_expect_assert_fail(true);
 	(void)granule_addr(granule);
 	test_helpers_fail_if_no_assert_failed();
@@ -313,7 +313,7 @@
 	 * granule[0];
 	 ******************************************************************/
 
-	granule = realm_test_util_granule_struct_base() - 1U;
+	granule = test_helpers_granule_struct_base() - 1U;
 	test_helpers_expect_assert_fail(true);
 	(void)granule_addr(granule);
 	test_helpers_fail_if_no_assert_failed();
@@ -331,7 +331,7 @@
 	 * not properly aligned.
 	 ******************************************************************/
 
-	granule = (uintptr_t)realm_test_util_granule_struct_base();
+	granule = (uintptr_t)test_helpers_granule_struct_base();
 	granule += test_helpers_get_rand_in_range(1UL,
 					sizeof(struct granule) - 1U);
 	test_helpers_expect_assert_fail(true);
@@ -425,7 +425,7 @@
 	 ******************************************************************/
 
 	for (unsigned int i = 0U; i < 3U; i++) {
-		expected_granule = realm_test_util_granule_struct_base() +
+		expected_granule = test_helpers_granule_struct_base() +
 							granule_indexes[i];
 		address = (granule_indexes[i] * GRANULE_SIZE) +
 						host_util_get_granule_base();
@@ -505,8 +505,8 @@
 	} while (g1_index == g2_index);
 
 	/* Get the expected address for the granules */
-	exp_g1 = realm_test_util_granule_struct_base() + g1_index;
-	exp_g2 = realm_test_util_granule_struct_base() + g2_index;
+	exp_g1 = test_helpers_granule_struct_base() + g1_index;
+	exp_g2 = test_helpers_granule_struct_base() + g2_index;
 
 	/* Get the expected PA for the corresponding granules */
 	addr1 = (g1_index * GRANULE_SIZE) + host_util_get_granule_base();
diff --git a/lib/realm/tests/realm_test_utils.c b/lib/realm/tests/realm_test_utils.c
index c90ecae..ea17f69 100644
--- a/lib/realm/tests/realm_test_utils.c
+++ b/lib/realm/tests/realm_test_utils.c
@@ -48,11 +48,3 @@
 	return (uintptr_t)NULL;
 }
 
-/*
- * Function to return the base pointer to granule structure.
- * This function relies on addr_to_granule().
- */
-struct granule *realm_test_util_granule_struct_base(void)
-{
-	return addr_to_granule(host_util_get_granule_base());
-}
diff --git a/lib/realm/tests/realm_test_utils.h b/lib/realm/tests/realm_test_utils.h
index 1787907..9dcc56c 100644
--- a/lib/realm/tests/realm_test_utils.h
+++ b/lib/realm/tests/realm_test_utils.h
@@ -10,6 +10,5 @@
 
 uintptr_t realm_test_util_slot_to_pa(enum buffer_slot slot);
 uintptr_t realm_test_util_slot_va_from_pa(uintptr_t pa);
-struct granule *realm_test_util_granule_struct_base(void);
 
 #endif /* REALM_TEST_UTILS_H */
diff --git a/lib/s2tt/CMakeLists.txt b/lib/s2tt/CMakeLists.txt
new file mode 100644
index 0000000..8c04844
--- /dev/null
+++ b/lib/s2tt/CMakeLists.txt
@@ -0,0 +1,20 @@
+#
+# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
+#
+
+add_library(rmm-lib-s2tt)
+
+target_link_libraries(rmm-lib-s2tt
+    PRIVATE rmm-lib-arch
+            rmm-lib-common
+            rmm-lib-debug
+            rmm-lib-realm)
+
+target_include_directories(rmm-lib-s2tt
+    PUBLIC "include"
+    PRIVATE "src")
+
+target_sources(rmm-lib-s2tt
+    PRIVATE "src/s2tt.c")
+
diff --git a/lib/realm/include/ripas.h b/lib/s2tt/include/ripas.h
similarity index 100%
rename from lib/realm/include/ripas.h
rename to lib/s2tt/include/ripas.h
diff --git a/lib/realm/include/s2tt.h b/lib/s2tt/include/s2tt.h
similarity index 87%
rename from lib/realm/include/s2tt.h
rename to lib/s2tt/include/s2tt.h
index 7ecd953..7d3c1ff 100644
--- a/lib/realm/include/s2tt.h
+++ b/lib/s2tt/include/s2tt.h
@@ -7,8 +7,33 @@
 #define S2TT_H
 
 #include <arch_features.h>
+#include <granule_types.h>
 #include <memory.h>
 
+/*
+ * Stage 2 configuration of the Realm
+ */
+struct s2tt_context {
+	/* Number of IPA bits */
+	unsigned int ipa_bits;
+
+	/* Starting level of the stage 2 translation */
+	int s2_starting_level;
+
+	/* Number of concatenated starting level rtts */
+	unsigned int num_root_rtts;
+
+	/* First level RTT, pointed to by Realm TTBR */
+	struct granule *g_rtt;
+
+	/* Virtual Machine Identifier */
+	unsigned int vmid;
+
+	/*
+	 * TODO: we will need other translation regime state, e.g. TCR, MAIR(?).
+	 */
+};
+
 #define S2TT_MIN_IPA_BITS		32U
 #define S2TT_MAX_IPA_BITS		48U
 
@@ -111,10 +136,9 @@
 			   unsigned long pa, long level);
 void s2tt_init_assigned_destroyed(unsigned long *s2tt, unsigned long pa, long level);
 
-struct realm_s2_context;
-void s2tt_invalidate_page(const struct realm_s2_context *s2_ctx, unsigned long addr);
-void s2tt_invalidate_block(const struct realm_s2_context *s2_ctx, unsigned long addr);
-void s2tt_invalidate_pages_in_block(const struct realm_s2_context *s2_ctx,
+void s2tt_invalidate_page(const struct s2tt_context *s2_ctx, unsigned long addr);
+void s2tt_invalidate_block(const struct s2tt_context *s2_ctx, unsigned long addr);
+void s2tt_invalidate_pages_in_block(const struct s2tt_context *s2_ctx,
 				    unsigned long addr);
 
 bool s2tt_is_unassigned_empty_block(unsigned long *table);
diff --git a/lib/realm/src/s2tt.c b/lib/s2tt/src/s2tt.c
similarity index 98%
rename from lib/realm/src/s2tt.c
rename to lib/s2tt/src/s2tt.c
index 130ffb1..fe604f4 100644
--- a/lib/realm/src/s2tt.c
+++ b/lib/s2tt/src/s2tt.c
@@ -5,18 +5,12 @@
 
 #include <arch_helpers.h>
 #include <bitmap.h>
-#include <buffer.h>
-#include <gic.h>
 #include <granule.h>
-#include <memory_alloc.h>
-#include <realm.h>
 #include <ripas.h>
 #include <s2tt.h>
 #include <s2tt_pvt_defs.h>
 #include <smc.h>
-#include <status.h>
 #include <stddef.h>
-#include <string.h>
 
 /* TODO: Fix this when introducing LPA2 support */
 COMPILER_ASSERT(S2TT_MIN_STARTING_LEVEL >= 0);
@@ -24,7 +18,7 @@
 /*
  * Invalidates S2 TLB entries from [ipa, ipa + size] region tagged with `vmid`.
  */
-static void stage2_tlbi_ipa(const struct realm_s2_context *s2_ctx,
+static void stage2_tlbi_ipa(const struct s2tt_context *s2_ctx,
 			    unsigned long ipa,
 			    unsigned long size)
 {
@@ -87,7 +81,7 @@
  * Call this function after:
  * 1.  A L3 page desc has been removed.
  */
-void s2tt_invalidate_page(const struct realm_s2_context *s2_ctx, unsigned long addr)
+void s2tt_invalidate_page(const struct s2tt_context *s2_ctx, unsigned long addr)
 {
 	stage2_tlbi_ipa(s2_ctx, addr, GRANULE_SIZE);
 }
@@ -99,7 +93,7 @@
  * 2a. A L2 table desc has been removed, where
  * 2b. All S2TTEs in L3 table that the L2 table desc was pointed to were invalid.
  */
-void s2tt_invalidate_block(const struct realm_s2_context *s2_ctx, unsigned long addr)
+void s2tt_invalidate_block(const struct s2tt_context *s2_ctx, unsigned long addr)
 {
 	stage2_tlbi_ipa(s2_ctx, addr, GRANULE_SIZE);
 }
@@ -110,7 +104,8 @@
  * 1a. A L2 table desc has been removed, where
  * 1b. Some S2TTEs in the table that the L2 table desc was pointed to were valid.
  */
-void s2tt_invalidate_pages_in_block(const struct realm_s2_context *s2_ctx, unsigned long addr)
+void s2tt_invalidate_pages_in_block(const struct s2tt_context *s2_ctx,
+				    unsigned long addr)
 {
 	stage2_tlbi_ipa(s2_ctx, addr, BLOCK_L2_SIZE);
 }
diff --git a/lib/realm/src/s2tt_pvt_defs.h b/lib/s2tt/src/s2tt_pvt_defs.h
similarity index 100%
rename from lib/realm/src/s2tt_pvt_defs.h
rename to lib/s2tt/src/s2tt_pvt_defs.h
diff --git a/plat/host/host_cbmc/src/tb_realm.c b/plat/host/host_cbmc/src/tb_realm.c
index bc0aadb..b4eb570 100644
--- a/plat/host/host_cbmc/src/tb_realm.c
+++ b/plat/host/host_cbmc/src/tb_realm.c
@@ -72,7 +72,7 @@
 }
 
 /* Detail of the valid state */
-bool valid_realm_s2_context(struct realm_s2_context value)
+bool valid_s2tt_context(struct s2tt_context value)
 {
 	unsigned int vmid_count = is_feat_vmid16_present() ? VMID16_COUNT : VMID8_COUNT;
 
@@ -94,7 +94,7 @@
 bool valid_rd(struct rd value)
 {
 	return valid_realm_state(value.state)
-		&& valid_realm_s2_context(value.s2_ctx)
+		&& valid_s2tt_context(value.s2_ctx)
 		&& valid_hash_algo(value.algorithm)
 		&& value.num_rec_aux <= MAX_REC_AUX_GRANULES;
 }
diff --git a/plat/host/host_test/include/test_helpers.h b/plat/host/host_test/include/test_helpers.h
index aeb5584..3f0cd2e 100644
--- a/plat/host/host_test/include/test_helpers.h
+++ b/plat/host/host_test/include/test_helpers.h
@@ -190,4 +190,9 @@
  */
 void test_helpers_init(void);
 
+/*
+ * Return a pointer to the first available granule structure
+ */
+struct granule *test_helpers_granule_struct_base(void);
+
 #endif
diff --git a/plat/host/host_test/src/test_helpers.c b/plat/host/host_test/src/test_helpers.c
index 47148ff..b66e77b 100644
--- a/plat/host/host_test/src/test_helpers.c
+++ b/plat/host/host_test/src/test_helpers.c
@@ -6,6 +6,7 @@
 #include <arch_helpers.h>
 #include <debug.h>
 #include <errno.h>
+#include <granule.h>
 #include <host_defs.h>
 #include <host_utils.h>
 #include <limits.h>
@@ -241,6 +242,11 @@
 	}
 }
 
+struct granule *test_helpers_granule_struct_base(void)
+{
+	return addr_to_granule(host_util_get_granule_base());
+}
+
 /******************************************************************
  * Private APIs shared with other host_test files.
  *****************************************************************/
diff --git a/runtime/rmi/rtt.c b/runtime/rmi/rtt.c
index 01d7044..298acf8 100644
--- a/runtime/rmi/rtt.c
+++ b/runtime/rmi/rtt.c
@@ -88,7 +88,7 @@
 	long level = (long)ulevel;
 	unsigned long ipa_bits;
 	unsigned long ret;
-	struct realm_s2_context s2_ctx;
+	struct s2tt_context s2_ctx;
 	int sl;
 
 	if (!find_lock_two_granules(rtt_addr,
@@ -291,7 +291,7 @@
 	long level = (long)ulevel;
 	unsigned long ipa_bits, rtt_addr;
 	unsigned long ret;
-	struct realm_s2_context s2_ctx;
+	struct s2tt_context s2_ctx;
 	int sl;
 
 	g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
@@ -478,7 +478,7 @@
 	long level = (long)ulevel;
 	unsigned long ipa_bits, rtt_addr;
 	unsigned long ret;
-	struct realm_s2_context s2_ctx;
+	struct s2tt_context s2_ctx;
 	int sl;
 	bool in_par, skip_non_live = false;
 
@@ -621,7 +621,7 @@
 	unsigned long *s2tt, s2tte;
 	struct s2tt_walk wi;
 	unsigned long ipa_bits;
-	struct realm_s2_context s2_ctx;
+	struct s2tt_context s2_ctx;
 	int sl;
 
 	g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
@@ -1023,7 +1023,7 @@
 	unsigned long data_addr, s2tte, *s2tt;
 	struct rd *rd;
 	unsigned long ipa_bits;
-	struct realm_s2_context s2_ctx;
+	struct s2tt_context s2_ctx;
 	int sl;
 
 	g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
@@ -1292,7 +1292,7 @@
 	granule_unlock(g_rd);
 }
 
-static void rtt_set_ripas_range(struct realm_s2_context *s2_ctx,
+static void rtt_set_ripas_range(struct s2tt_context *s2_ctx,
 				unsigned long *s2tt,
 				unsigned long base,
 				unsigned long top,
@@ -1365,7 +1365,7 @@
 	unsigned long ipa_bits;
 	struct s2tt_walk wi;
 	unsigned long *s2tt;
-	struct realm_s2_context s2_ctx;
+	struct s2tt_context s2_ctx;
 	enum ripas ripas_val;
 	enum ripas_change_destroyed change_destroyed;
 	int sl;