Rework BL2 to BL3-1 hand over interface

This patch reworks BL2 to BL3-1 hand over interface by introducing a
composite structure (bl31_args) that holds the superset of information
that needs to be passed from BL2 to BL3-1.

  - The extents of secure memory available to BL3-1
  - The extents of memory available to BL3-2 (not yet implemented) and
    BL3-3
  - Information to execute BL3-2 (not yet implemented) and BL3-3 images

This patch also introduces a new platform API (bl2_get_bl31_args_ptr)
that needs to be implemented by the platform code to export reference to
bl31_args structure which has been allocated in platform-defined memory.

The platform will initialize the extents of memory available to BL3-3
during early platform setup in bl31_args structure. This obviates the
need for bl2_get_ns_mem_layout platform API.

BL2 calls the bl2_get_bl31_args_ptr function to get a reference to
bl31_args structure. It uses the 'bl33_meminfo' field of this structure
to load the BL3-3 image. It sets the entry point information for the
BL3-3 image in the 'bl33_image_info' field of this structure. The
reference to this structure is passed to the BL3-1 image.

Also fixes issue ARM-software/tf-issues#25

Change-Id: Ic36426196dd5ebf89e60ff42643bed01b3500517
diff --git a/plat/fvp/bl2_plat_setup.c b/plat/fvp/bl2_plat_setup.c
index f8c922e..2e367d7 100644
--- a/plat/fvp/bl2_plat_setup.c
+++ b/plat/fvp/bl2_plat_setup.c
@@ -70,19 +70,25 @@
 static meminfo bl2_tzram_layout
 __attribute__ ((aligned(PLATFORM_CACHE_LINE_SIZE),
 		section("tzfw_coherent_mem")));
-/* Data structure which holds the extents of the Non-Secure DRAM for BL33 */
-static meminfo bl33_dram_layout
-__attribute__ ((aligned(PLATFORM_CACHE_LINE_SIZE),
-		section("tzfw_coherent_mem")));
+
+/*******************************************************************************
+ * Reference to structure which holds the arguments which need to be passed
+ * to BL31
+ ******************************************************************************/
+static bl31_args *bl2_to_bl31_args;
 
 meminfo *bl2_plat_sec_mem_layout(void)
 {
 	return &bl2_tzram_layout;
 }
 
-meminfo *bl2_get_ns_mem_layout(void)
+/*******************************************************************************
+ * This function returns a pointer to the memory that the platform has kept
+ * aside to pass all the information that BL31 could need.
+ ******************************************************************************/
+bl31_args *bl2_get_bl31_args_ptr(void)
 {
-	return &bl33_dram_layout;
+	return bl2_to_bl31_args;
 }
 
 /*******************************************************************************
@@ -101,16 +107,6 @@
 	bl2_tzram_layout.attr = mem_layout->attr;
 	bl2_tzram_layout.next = 0;
 
-	/* Setup the BL3-3 memory layout.
-	 * Normal World Firmware loaded into main DRAM.
-	 */
-	bl33_dram_layout.total_base = DRAM_BASE;
-	bl33_dram_layout.total_size = DRAM_SIZE;
-	bl33_dram_layout.free_base = DRAM_BASE;
-	bl33_dram_layout.free_size = DRAM_SIZE;
-	bl33_dram_layout.attr = 0;
-	bl33_dram_layout.next = 0;
-
 	/* Initialize the platform config for future decision making */
 	platform_config_setup();
 
@@ -127,7 +123,15 @@
 	io_setup();
 
 	/* Use the Trusted DRAM for passing args to BL31 */
-	bl2_el_change_mem_ptr = (unsigned char **) TZDRAM_BASE;
+	bl2_to_bl31_args = (bl31_args *) TZDRAM_BASE;
+
+	/* Populate the extents of memory available for loading BL33 */
+	bl2_to_bl31_args->bl33_meminfo.total_base = DRAM_BASE;
+	bl2_to_bl31_args->bl33_meminfo.total_size = DRAM_SIZE;
+	bl2_to_bl31_args->bl33_meminfo.free_base = DRAM_BASE;
+	bl2_to_bl31_args->bl33_meminfo.free_size = DRAM_SIZE;
+	bl2_to_bl31_args->bl33_meminfo.attr = 0;
+	bl2_to_bl31_args->bl33_meminfo.next = 0;
 }
 
 /*******************************************************************************
diff --git a/plat/fvp/bl31_plat_setup.c b/plat/fvp/bl31_plat_setup.c
index 425fad6..019b8e1 100644
--- a/plat/fvp/bl31_plat_setup.c
+++ b/plat/fvp/bl31_plat_setup.c
@@ -30,6 +30,7 @@
 
 #include <platform.h>
 #include <fvp_pwrc.h>
+#include <bl_common.h>
 
 /*******************************************************************************
  * Declarations of linker defined symbols which will help us find the layout
@@ -61,22 +62,14 @@
 #define BL31_COHERENT_RAM_LIMIT (unsigned long)(&__COHERENT_RAM_END__)
 
 /*******************************************************************************
- * This data structure holds information copied by BL31 from BL2 to pass
- * control to the normal world software images.
- * TODO: Can this be moved out of device memory.
+ * Reference to structure which holds the arguments that have been passed to
+ * BL31 from BL2.
  ******************************************************************************/
-static el_change_info ns_entry_info
-__attribute__ ((aligned(PLATFORM_CACHE_LINE_SIZE),
-		section("tzfw_coherent_mem")));
-
-/* Data structure which holds the extents of the trusted SRAM for BL31 */
-static meminfo bl31_tzram_layout
-__attribute__ ((aligned(PLATFORM_CACHE_LINE_SIZE),
-		section("tzfw_coherent_mem")));
+static bl31_args *bl2_to_bl31_args;
 
 meminfo *bl31_plat_sec_mem_layout(void)
 {
-	return &bl31_tzram_layout;
+	return &bl2_to_bl31_args->bl31_meminfo;
 }
 
 /*******************************************************************************
@@ -87,34 +80,24 @@
  ******************************************************************************/
 el_change_info *bl31_get_next_image_info(void)
 {
-	return &ns_entry_info;
+	return &bl2_to_bl31_args->bl33_image_info;
 }
 
 /*******************************************************************************
- * Perform any BL31 specific platform actions. Here we copy parameters passed
- * by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they are lost
- * (potentially). This is done before the MMU is initialized so that the memory
- * layout can be used while creating page tables.
+ * Perform any BL31 specific platform actions. Here is an opportunity to copy
+ * parameters passed by the calling EL (S-EL1 in BL2 & S-EL3 in BL1) before they
+ * are lost (potentially). This needs to be done before the MMU is initialized
+ * so that the memory layout can be used while creating page tables. On the FVP
+ * we know that BL2 has populated the parameters in secure DRAM. So we just use
+ * the reference passed in 'from_bl2' instead of copying. The 'data' parameter
+ * is not used since all the information is contained in 'from_bl2'. Also, BL2
+ * has flushed this information to memory, so we are guaranteed to pick up good
+ * data
  ******************************************************************************/
-void bl31_early_platform_setup(meminfo *mem_layout,
+void bl31_early_platform_setup(bl31_args *from_bl2,
 			       void *data)
 {
-	el_change_info *image_info = (el_change_info *) data;
-
-	/* Setup the BL31 memory layout */
-	bl31_tzram_layout.total_base = mem_layout->total_base;
-	bl31_tzram_layout.total_size = mem_layout->total_size;
-	bl31_tzram_layout.free_base = mem_layout->free_base;
-	bl31_tzram_layout.free_size = mem_layout->free_size;
-	bl31_tzram_layout.attr = mem_layout->attr;
-	bl31_tzram_layout.next = 0;
-
-	/* Save information about jumping into the normal world */
-	ns_entry_info.entrypoint = image_info->entrypoint;
-	ns_entry_info.spsr = image_info->spsr;
-	ns_entry_info.args = image_info->args;
-	ns_entry_info.security_state = image_info->security_state;
-	ns_entry_info.next = image_info->next;
+	bl2_to_bl31_args = from_bl2;
 
 	/* Initialize the platform config for future decision making */
 	platform_config_setup();
@@ -163,7 +146,7 @@
  ******************************************************************************/
 void bl31_plat_arch_setup()
 {
-	configure_mmu(&bl31_tzram_layout,
+	configure_mmu(&bl2_to_bl31_args->bl31_meminfo,
 		      BL31_RO_BASE,
 		      BL31_RO_LIMIT,
 		      BL31_COHERENT_RAM_BASE,
diff --git a/plat/fvp/platform.h b/plat/fvp/platform.h
index 5826d35..76a9fca 100644
--- a/plat/fvp/platform.h
+++ b/plat/fvp/platform.h
@@ -53,17 +53,20 @@
 
 /* Trusted Boot Firmware BL2 */
 #define BL2_IMAGE_NAME			"bl2.bin"
+
 /* EL3 Runtime Firmware BL31 */
-#define BL31_IMAGE_NAME		"bl31.bin"
+#define BL31_IMAGE_NAME			"bl31.bin"
+
 /* Secure Payload BL32 (Trusted OS) */
-#define BL32_IMAGE_NAME		"bl32.bin"
+#define BL32_IMAGE_NAME			"bl32.bin"
+
 /* Non-Trusted Firmware BL33 and its load address */
-#define BL33_IMAGE_NAME		"bl33.bin" /* e.g. UEFI */
-#define NS_IMAGE_OFFSET		(DRAM_BASE + 0x8000000) /* DRAM + 128MB */
+#define BL33_IMAGE_NAME			"bl33.bin" /* e.g. UEFI */
+#define NS_IMAGE_OFFSET			(DRAM_BASE + 0x8000000) /* DRAM + 128MB */
+
 /* Firmware Image Package */
 #define FIP_IMAGE_NAME			"fip.bin"
 
-
 #define PLATFORM_CACHE_LINE_SIZE	64
 #define PLATFORM_CLUSTER_COUNT		2ull
 #define PLATFORM_CLUSTER0_CORE_COUNT	4