fix(rme): append realm.bin at end of tftf.bin

Currently realm.bin is appended to tftf.bin at offset of 10 MB.
This patch removes this dependency by reserving empty sections
for realm image and dependencies, in tftf binary after
all loadable sections (end of binary),
and append realm.bin at end of tftf.bin later in build process.

The patch removes the need for TFTF to map memory corresponding
to Realm payload dynamically at runtime.

Change-Id: Iead2dc62ff2965cf7bb03e61c93e76df218da973
Signed-off-by: Shruti Gupta <shruti.gupta@arm.com>
diff --git a/Makefile b/Makefile
index 4896ba9..54293b5 100644
--- a/Makefile
+++ b/Makefile
@@ -192,6 +192,7 @@
 $(eval $(call add_define,TFTF_DEFINES,NEW_TEST_SESSION))
 $(eval $(call add_define,TFTF_DEFINES,PLAT_${PLAT}))
 $(eval $(call add_define,TFTF_DEFINES,USE_NVM))
+$(eval $(call add_define,TFTF_DEFINES,ENABLE_REALM_PAYLOAD_TESTS))
 
 ################################################################################
 
@@ -606,7 +607,7 @@
 tftf: realm
 	@echo "  PACK REALM PAYLOAD"
 	$(shell dd if=$(BUILD_PLAT)/realm.bin of=$(BUILD_PLAT)/tftf.bin obs=1 \
-	seek=$(TFTF_MAX_IMAGE_SIZE))
+	oflag=append conv=notrunc)
 endif
 
 ifeq (${ARCH}-${PLAT},aarch64-fvp)
@@ -615,7 +616,7 @@
 pack_realm: realm tftf
 	@echo "  PACK REALM PAYLOAD"
 	$(shell dd if=$(BUILD_PLAT)/realm.bin of=$(BUILD_PLAT)/tftf.bin obs=1 \
-	seek=$(TFTF_MAX_IMAGE_SIZE))
+	oflag=append conv=notrunc)
 endif
 
 ifeq (${ARCH}-${PLAT},aarch64-tc)
diff --git a/docs/getting_started/build-options.rst b/docs/getting_started/build-options.rst
index 8e62380..699b79d 100644
--- a/docs/getting_started/build-options.rst
+++ b/docs/getting_started/build-options.rst
@@ -152,10 +152,8 @@
 Realm payload specific Build Options
 ------------------------------------
 
--  ``TFTF_MAX_IMAGE_SIZE``: The option needs to be either set by the user or
-   by the platform makefile to specify the maximum size of TFTF binary. This
-   is needed so that the Realm payload binary can be appended to TFTF binary
-   via ``make ENABLE_REALM_PAYLOAD_TESTS=1 tftf`` build command.
+-  ``ENABLE_REALM_PAYLOAD_TESTS=1`` This option builds and packs Realm payload tests
+   realm.bin to tftf.bin.
 
 FWU-specific Build Options
 --------------------------
diff --git a/include/runtime_services/host_realm_managment/host_realm_mem_layout.h b/include/runtime_services/host_realm_managment/host_realm_mem_layout.h
index 6e94b30..c895c71 100644
--- a/include/runtime_services/host_realm_managment/host_realm_mem_layout.h
+++ b/include/runtime_services/host_realm_managment/host_realm_mem_layout.h
@@ -10,17 +10,19 @@
 
 #include <realm_def.h>
 
-#include <platform_def.h>
-
 /*
- * Realm payload Memory Usage Layout
+ * Realm payload Memory Usage Layout in TFTF.bin.
+ * The realm.bin is appended to tftf.bin to create a unified
+ * tftf.bin.
+ *   +---------------------------+
+ *   | TFTF.bin                  |
+ *   |                           |
+ *   +---------------------------+
+ *   | Realm Image               |
+ *   | (REALM_MAX_LOAD_IMG_SIZE  |
+ *   +---------------------------+
  *
- * +--------------------------+     +---------------------------+
- * |                          |     | Host Image                |
- * |    TFTF                  |     | (TFTF_MAX_IMAGE_SIZE)     |
- * | Normal World             | ==> +---------------------------+
- * |    Image                 |     | Realm Image               |
- * | (MAX_NS_IMAGE_SIZE)      |     | (REALM_MAX_LOAD_IMG_SIZE  |
+ * The realm memory pool is a combination of PAGE_POOL and NS_SHARED_MEM
  * +--------------------------+     +---------------------------+
  * |  Memory Pool             |     | Heap Memory               |
  * | (NS_REALM_SHARED_MEM_SIZE|     | (PAGE_POOL_MAX_SIZE)      |
@@ -31,30 +33,24 @@
  * |                          |     | (NS_REALM_SHARED_MEM_SIZE)|
  * +--------------------------+     +---------------------------+
  *
+ * Refer to tftf.lds for the layout.
  */
 
-/*
- * Default values defined in platform.mk, and can be provided as build arguments
- * TFTF_MAX_IMAGE_SIZE: 1MB
- */
-
-#ifdef TFTF_MAX_IMAGE_SIZE
-/* 1MB for shared buffer between Realm and Host */
- #define NS_REALM_SHARED_MEM_SIZE	U(0x100000)
-/* 3MB of memory used as a pool for realm's objects creation */
- #define PAGE_POOL_MAX_SIZE		U(0x300000)
-/* Base address of each section */
- #define REALM_IMAGE_BASE		(TFTF_BASE + TFTF_MAX_IMAGE_SIZE)
- #define PAGE_POOL_BASE			(REALM_IMAGE_BASE + REALM_MAX_LOAD_IMG_SIZE)
+#if !(defined(__LINKER__) || defined(__ASSEMBLY__))
+ /* Base address of each section */
+ IMPORT_SYM(uintptr_t, __REALM_PAYLOAD_START__, REALM_IMAGE_BASE);
+ IMPORT_SYM(uintptr_t, __REALM_POOL_START__, PAGE_POOL_BASE);
  #define NS_REALM_SHARED_MEM_BASE	(PAGE_POOL_BASE + PAGE_POOL_MAX_SIZE)
+#endif
+
+#ifdef ENABLE_REALM_PAYLOAD_TESTS
+ /* 1MB for shared buffer between Realm and Host */
+ #define NS_REALM_SHARED_MEM_SIZE	U(0x100000)
+ /* 3MB of memory used as a pool for realm's objects creation */
+ #define PAGE_POOL_MAX_SIZE		U(0x300000)
 #else
- #define NS_REALM_SHARED_MEM_SIZE	0U
- #define PAGE_POOL_MAX_SIZE		0U
- #define TFTF_MAX_IMAGE_SIZE		DRAM_SIZE
-/* Base address of each section */
- #define REALM_IMAGE_BASE		0U
- #define PAGE_POOL_BASE			0U
- #define NS_REALM_SHARED_MEM_BASE	0U
+ #define NS_REALM_SHARED_MEM_SIZE       U(0x0)
+ #define PAGE_POOL_MAX_SIZE             U(0x0)
 #endif
 
 #endif /* HOST_REALM_MEM_LAYOUT_H */
diff --git a/plat/arm/fvp/platform.mk b/plat/arm/fvp/platform.mk
index cb1a37b..99183ec 100644
--- a/plat/arm/fvp/platform.mk
+++ b/plat/arm/fvp/platform.mk
@@ -4,9 +4,6 @@
 # SPDX-License-Identifier: BSD-3-Clause
 #
 
-# If not specified as build arguments, set default to 10 MB
-TFTF_MAX_IMAGE_SIZE:=10485760
-
 # Default number of threads per CPU on FVP
 FVP_MAX_PE_PER_CPU		:= 1
 
@@ -82,7 +79,6 @@
 $(eval $(call add_define,TFTF_DEFINES,FVP_CLUSTER_COUNT))
 $(eval $(call add_define,TFTF_DEFINES,FVP_MAX_CPUS_PER_CLUSTER))
 $(eval $(call add_define,TFTF_DEFINES,FVP_MAX_PE_PER_CPU))
-$(eval $(call add_define,TFTF_DEFINES,TFTF_MAX_IMAGE_SIZE))
 
 # Default PA size for FVP platform
 PA_SIZE := 34
diff --git a/tftf/framework/tftf.ld.S b/tftf/framework/tftf.ld.S
index e403af0..4559b03 100644
--- a/tftf/framework/tftf.ld.S
+++ b/tftf/framework/tftf.ld.S
@@ -6,17 +6,15 @@
 
 #include <platform_def.h>
 #include <xlat_tables_defs.h>
+#include <host_realm_mem_layout.h>
+
 
 OUTPUT_FORMAT(PLATFORM_LINKER_FORMAT)
 OUTPUT_ARCH(PLATFORM_LINKER_ARCH)
 ENTRY(tftf_entrypoint)
 
-#ifndef TFTF_MAX_IMAGE_SIZE
-#define TFTF_MAX_IMAGE_SIZE DRAM_SIZE
-#endif
-
 MEMORY {
-    RAM (rwx): ORIGIN = TFTF_BASE, LENGTH = TFTF_MAX_IMAGE_SIZE
+    RAM (rwx): ORIGIN = TFTF_BASE, LENGTH = DRAM_SIZE
 }
 
 
@@ -49,10 +47,29 @@
     .data : {
         __DATA_START__ = .;
         *(.data*)
+        . = NEXT(PAGE_SIZE); /* This ensures tftf.bin is aligned to page size. */
         __DATA_END__ = .;
     } >RAM
 
-    stacks (NOLOAD) : {
+   /* End of LOAD Sections. NOLOAD sections begin here. */
+   /*
+    * Memory for Realm Image has to follow next as it will appended to end
+    * of tftf.bin.
+    */
+    realm_payload (NOLOAD) : {
+        __REALM_PAYLOAD_START__ = .;
+        . = __REALM_PAYLOAD_START__ + REALM_MAX_LOAD_IMG_SIZE;
+        __REALM_PAYLOAD_END__ = .;
+    } >RAM
+
+    /* Memory pool for Realm payload tests. */
+    realm_pool (NOLOAD)  : ALIGN(PAGE_SIZE) {
+        __REALM_POOL_START__ = .;
+        . = __REALM_POOL_START__ + NS_REALM_SHARED_MEM_SIZE + PAGE_POOL_MAX_SIZE;
+        __REALM_POOL_END__ = .;
+    } >RAM
+
+    stacks (NOLOAD) : ALIGN(16) {
         __STACKS_START__ = .;
         *(tftf_normal_stacks)
         __STACKS_END__ = .;
@@ -60,9 +77,9 @@
 
     /*
      * The .bss section gets initialised to 0 at runtime.
-     * Its base address must be 16-byte aligned.
+     * Its base address is always PAGE_SIZE aligned.
      */
-    .bss : ALIGN(16) {
+    .bss : {
         __BSS_START__ = .;
         *(SORT_BY_ALIGNMENT(.bss*))
         *(COMMON)
@@ -71,10 +88,9 @@
 
     /*
      * The xlat_table section is for full, aligned page tables (4K).
-     * Removing them from .bss avoids forcing 4K alignment on
-     * the .bss section and eliminates the unecessary zero init
+     * Removing them from .bss eliminates the unecessary zero init
      */
-    xlat_table (NOLOAD) : {
+    xlat_table (NOLOAD) : ALIGN(PAGE_SIZE) {
         *(xlat_table)
     } >RAM
 
@@ -109,7 +125,6 @@
     __COHERENT_RAM_UNALIGNED_SIZE__ =
         __COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__;
 
-
     __TFTF_END__ = .;
 
     __BSS_SIZE__ = SIZEOF(.bss);
diff --git a/tftf/tests/runtime_services/host_realm_managment/host_realm_helper.c b/tftf/tests/runtime_services/host_realm_managment/host_realm_helper.c
index fc18a75..09ac9a0 100644
--- a/tftf/tests/runtime_services/host_realm_managment/host_realm_helper.c
+++ b/tftf/tests/runtime_services/host_realm_managment/host_realm_helper.c
@@ -24,17 +24,8 @@
 static struct realm realm;
 static bool realm_payload_created;
 static bool shared_mem_created;
-static bool realm_payload_mmaped;
 static volatile bool timer_enabled;
 
-/* From the TFTF_BASE offset, memory used by TFTF + Shared + Realm + POOL should
- * not exceed DRAM_END offset
- * NS_REALM_SHARED_MEM_BASE + NS_REALM_SHARED_MEM_SIZE is considered last offset
- */
-CASSERT((((uint64_t)NS_REALM_SHARED_MEM_BASE + (uint64_t)NS_REALM_SHARED_MEM_SIZE)\
-	< ((uint64_t)DRAM_BASE + (uint64_t)DRAM_SIZE)),\
-	error_ns_memory_and_realm_payload_exceed_DRAM_SIZE);
-
 #define RMI_EXIT(id)	\
 	[RMI_EXIT_##id] = #id
 
@@ -114,41 +105,6 @@
 	timer_enabled = true;
 }
 
-/**
- *   @brief    - Add regions assigned to Host into its translation table data
- *   structure.
- **/
-static test_result_t host_mmap_realm_payload(u_register_t realm_payload_adr,
-		u_register_t plat_mem_pool_adr,
-		u_register_t plat_mem_pool_size)
-{
-	if (realm_payload_mmaped) {
-		return REALM_SUCCESS;
-	}
-
-	/* Memory Pool region */
-	int rc = mmap_add_dynamic_region(plat_mem_pool_adr,
-					plat_mem_pool_adr,
-					plat_mem_pool_size,
-					MT_RW_DATA | MT_NS);
-	if (rc != 0) {
-		ERROR("%u: mmap_add_dynamic_region() %d\n", __LINE__, rc);
-		return TEST_RESULT_FAIL;
-	}
-
-	/* Realm Image region */
-	rc = mmap_add_dynamic_region(realm_payload_adr,
-					realm_payload_adr,
-					REALM_MAX_LOAD_IMG_SIZE,
-					MT_RW_DATA | MT_NS);
-	if (rc != 0) {
-		ERROR("%u: mmap_add_dynamic_region() %d\n", __LINE__, rc);
-		return TEST_RESULT_FAIL;
-	}
-	realm_payload_mmaped = true;
-	return REALM_SUCCESS;
-}
-
 static bool host_enter_realm(u_register_t *exit_reason,
 				unsigned int *host_call_result)
 {
@@ -199,6 +155,8 @@
 			"plat_mem_pool_size or realm_pages_size is NULL\n");
 		return false;
 	}
+
+	INFO("Realm base adr=0x%lx\n", realm_payload_adr);
 	/* Initialize  Host NS heap memory to be used in Realm creation*/
 	if (page_pool_init(plat_mem_pool_adr, realm_pages_size)
 		!= HEAP_INIT_SUCCESS) {
@@ -206,14 +164,6 @@
 		return false;
 	}
 
-	/* Mmap Realm payload region */
-	if (host_mmap_realm_payload(realm_payload_adr,
-			plat_mem_pool_adr,
-			plat_mem_pool_size) != REALM_SUCCESS) {
-		ERROR("%s() failed\n", "host_mmap_realm_payload");
-		return false;
-	}
-
 	/* Read Realm Feature Reg 0 */
 	if (host_rmi_features(0UL, &realm.rmm_feat_reg0) != REALM_SUCCESS) {
 		ERROR("%s() failed\n", "host_rmi_features");