Reduce deep nesting of header files

Reduce the number of header files included from other header
files as much as possible without splitting the files. Use forward
declarations where possible. This allows removal of some unnecessary
"#ifndef __ASSEMBLY__" statements.

Also, review the .c and .S files for which header files really need
including and reorder the #include statements alphabetically.

Fixes ARM-software/tf-issues#31

Change-Id: Iec92fb976334c77453e010b60bcf56f3be72bd3e
diff --git a/include/bl1/bl1.h b/include/bl1/bl1.h
index 1750a1f..d8afae4 100644
--- a/include/bl1/bl1.h
+++ b/include/bl1/bl1.h
@@ -31,16 +31,16 @@
 #ifndef __BL1_H__
 #define __BL1_H__
 
-#include <bl_common.h>
 
-#ifndef __ASSEMBLY__
+/******************************************
+ * Forward declarations
+ *****************************************/
+struct meminfo;
 
 /******************************************
  * Function prototypes
  *****************************************/
 extern void bl1_platform_setup(void);
-extern meminfo_t *bl1_plat_sec_mem_layout(void);
-
-#endif /*__ASSEMBLY__*/
+extern struct meminfo *bl1_plat_sec_mem_layout(void);
 
 #endif /* __BL1_H__ */
diff --git a/include/bl2/bl2.h b/include/bl2/bl2.h
index 9a5094f..25b6f9c 100644
--- a/include/bl2/bl2.h
+++ b/include/bl2/bl2.h
@@ -31,18 +31,22 @@
 #ifndef __BL2_H__
 #define __BL2_H__
 
-#include <bl_common.h>
-
 /******************************************
  * Data declarations
  *****************************************/
 extern unsigned long long bl2_entrypoint;
 
 /******************************************
+ * Forward declarations
+ *****************************************/
+struct meminfo;
+struct bl31_args;
+
+/******************************************
  * Function prototypes
  *****************************************/
 extern void bl2_platform_setup(void);
-extern meminfo_t *bl2_plat_sec_mem_layout(void);
-extern bl31_args_t *bl2_get_bl31_args_ptr(void);
+extern struct meminfo *bl2_plat_sec_mem_layout(void);
+extern struct bl31_args *bl2_get_bl31_args_ptr(void);
 
 #endif /* __BL2_H__ */
diff --git a/include/bl31/bl31.h b/include/bl31/bl31.h
index 6dd7596..b60e32c 100644
--- a/include/bl31/bl31.h
+++ b/include/bl31/bl31.h
@@ -31,13 +31,19 @@
 #ifndef __BL31_H__
 #define __BL31_H__
 
-#include <bl_common.h>
+#include <stdint.h>
 
 /*******************************************************************************
  * Data declarations
  ******************************************************************************/
 extern unsigned long bl31_entrypoint;
 
+/******************************************
+ * Forward declarations
+ *****************************************/
+struct meminfo;
+struct el_change_info;
+
 /*******************************************************************************
  * Function prototypes
  ******************************************************************************/
@@ -46,9 +52,10 @@
 extern void bl31_set_next_image_type(uint32_t type);
 extern uint32_t bl31_get_next_image_type(void);
 extern void bl31_prepare_next_image_entry();
-extern el_change_info_t *bl31_get_next_image_info(uint32_t type);
+extern struct el_change_info *bl31_get_next_image_info(uint32_t type);
 extern void bl31_platform_setup(void);
-extern meminfo_t *bl31_plat_get_bl32_mem_layout(void);
-extern meminfo_t *bl31_plat_sec_mem_layout(void);
-extern void bl31_register_bl32_init(int32_t (*)(meminfo_t *));
+extern struct meminfo *bl31_plat_get_bl32_mem_layout(void);
+extern struct meminfo *bl31_plat_sec_mem_layout(void);
+extern void bl31_register_bl32_init(int32_t (*)(struct meminfo *));
+
 #endif /* __BL31_H__ */
diff --git a/include/bl31/cm_macros.S b/include/bl31/cm_macros.S
index 9eeec18..d264956 100644
--- a/include/bl31/cm_macros.S
+++ b/include/bl31/cm_macros.S
@@ -27,6 +27,11 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
+
+#include <arch.h>
+#include <context.h>
+
+
 	/* ---------------------------------------------
 	 * Zero out the callee saved register to prevent
 	 * leakage of secure state into the normal world
diff --git a/include/bl31/context.h b/include/bl31/context.h
index c7eda7d..989b2e6 100644
--- a/include/bl31/context.h
+++ b/include/bl31/context.h
@@ -31,9 +31,6 @@
 #ifndef __CONTEXT_H__
 #define __CONTEXT_H__
 
-#include <arch.h>
-#include <cassert.h>
-
 /*******************************************************************************
  * Constants that allow assembler code to access members of and the 'gp_regs'
  * structure at their correct offsets.
@@ -171,6 +168,9 @@
 
 #ifndef __ASSEMBLY__
 
+#include <cassert.h>
+#include <stdint.h>
+
 /*
  * Common constants to help define the 'cpu_context' structure and its
  * members below.
diff --git a/include/bl31/context_mgmt.h b/include/bl31/context_mgmt.h
index 35f7c8c..efcdcd2 100644
--- a/include/bl31/context_mgmt.h
+++ b/include/bl31/context_mgmt.h
@@ -31,9 +31,8 @@
 #ifndef __CM_H__
 #define __CM_H__
 
-#include <context.h>
+#include <stdint.h>
 
-#ifndef __ASSEMBLY__
 /*******************************************************************************
  * Function & variable prototypes
  ******************************************************************************/
@@ -51,5 +50,5 @@
 extern void cm_set_el3_elr(uint32_t security_state, uint64_t entrypoint);
 extern void cm_set_next_eret_context(uint32_t security_state);
 extern void cm_init_exception_stack(uint64_t mpidr, uint32_t security_state);
-#endif /*__ASSEMBLY__*/
+
 #endif /* __CM_H__ */
diff --git a/include/bl31/runtime_svc.h b/include/bl31/runtime_svc.h
index ac85fa3..12b5db8 100644
--- a/include/bl31/runtime_svc.h
+++ b/include/bl31/runtime_svc.h
@@ -30,8 +30,6 @@
 
 #ifndef __RUNTIME_SVC_H__
 #define __RUNTIME_SVC_H__
-#include <psci.h>
-#include <cassert.h>
 
 /*******************************************************************************
  * Bit definitions inside the function id as per the SMC calling convention
@@ -122,6 +120,10 @@
 
 #ifndef __ASSEMBLY__
 
+#include <cassert.h>
+#include <context.h>
+#include <stdint.h>
+
 /* Various flags passed to SMC handlers */
 #define SMC_FROM_SECURE		(0 << 0)
 #define SMC_FROM_NON_SECURE	(1 << 0)
diff --git a/include/bl31/services/psci.h b/include/bl31/services/psci.h
index ab7b7bd..570fe5b 100644
--- a/include/bl31/services/psci.h
+++ b/include/bl31/services/psci.h
@@ -31,6 +31,7 @@
 #ifndef __PSCI_H__
 #define __PSCI_H__
 
+
 /*******************************************************************************
  * Defines for runtime services func ids
  ******************************************************************************/
@@ -131,6 +132,10 @@
 #define PSCI_NUM_AFFS		32ull
 
 #ifndef __ASSEMBLY__
+
+#include <stdint.h>
+
+
 /*******************************************************************************
  * Structure populated by platform specific code to export routines which
  * perform common low level pm functions
diff --git a/include/bl32/bl32.h b/include/bl32/bl32.h
index e469881..3239487 100644
--- a/include/bl32/bl32.h
+++ b/include/bl32/bl32.h
@@ -31,15 +31,18 @@
 #ifndef __BL32_H__
 #define __BL32_H__
 
-#ifndef __ASSEMBLY__
 #include <stdint.h>
 
-#include <bl_common.h>
+/******************************************
+ * Forward declarations
+ *****************************************/
+struct meminfo;
 
+/******************************************
+ * Function prototypes
+ *****************************************/
 extern void bl32_platform_setup(void);
-extern meminfo_t *bl32_plat_sec_mem_layout(void);
+extern struct meminfo *bl32_plat_sec_mem_layout(void);
 extern uint64_t bl32_main(void);
 
-#endif /* __ASSEMBLY__ */
-
 #endif /* __BL32_H__ */
diff --git a/include/bl32/payloads/tsp.h b/include/bl32/payloads/tsp.h
index 4ac7a1a..1f542d5 100644
--- a/include/bl32/payloads/tsp.h
+++ b/include/bl32/payloads/tsp.h
@@ -31,9 +31,6 @@
 #ifndef __TSP_H__
 #define __TSP_H__
 
-#include <platform.h>
-#include <cassert.h>
-
 /*
  * SMC function IDs that TSP uses to signal various forms of completions
  * to the secure payload dispatcher.
@@ -86,6 +83,9 @@
 #define TSP_ARGS_END		0x40
 
 #ifndef __ASSEMBLY__
+
+#include <cassert.h>
+#include <platform.h>	/* For CACHE_WRITEBACK_GRANULE */
 #include <stdint.h>
 
 typedef void (*tsp_generic_fptr_t)(uint64_t arg0,
diff --git a/include/common/asm_macros.S b/include/common/asm_macros.S
index 8bcb7d2..6cf1a19 100644
--- a/include/common/asm_macros.S
+++ b/include/common/asm_macros.S
@@ -28,6 +28,9 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <arch.h>
+
+
 	.macro	func_prologue
 	stp	x29, x30, [sp, #-0x10]!
 	mov	x29,sp
diff --git a/include/common/bl_common.h b/include/common/bl_common.h
index 1eb6d79..9fba9c0 100644
--- a/include/common/bl_common.h
+++ b/include/common/bl_common.h
@@ -58,7 +58,8 @@
 
 
 #ifndef __ASSEMBLY__
-#include <stdio.h>
+
+#include <cdefs.h> /* For __dead2 */
 
 /*******************************************************************************
  * Structure used for telling the next BL how much of a particular type of
diff --git a/include/common/debug.h b/include/common/debug.h
index f829477..e4fa31e 100644
--- a/include/common/debug.h
+++ b/include/common/debug.h
@@ -31,9 +31,6 @@
 #ifndef __DEBUG_H__
 #define __DEBUG_H__
 
-/* Do not try to call this from ASM code. */
-#ifndef __ASSEMBLY__
-
 #include <stdio.h>
 
 /* If building the project with DEBUG disabled the INFO and WARN macros
@@ -66,5 +63,4 @@
 		;
 }
 
-#endif /* __ASSEMBLY__ */
 #endif /* __DEBUG_H__ */
diff --git a/include/drivers/arm/pl011.h b/include/drivers/arm/pl011.h
index 5ad2fc7..28aef54 100644
--- a/include/drivers/arm/pl011.h
+++ b/include/drivers/arm/pl011.h
@@ -31,6 +31,9 @@
 #ifndef __PL011_H__
 #define __PL011_H__
 
+#include <mmio.h>
+
+
 /* PL011 Registers */
 #define UARTDR                    0x000
 #define UARTRSR                   0x004
diff --git a/include/drivers/arm/tzc400.h b/include/drivers/arm/tzc400.h
index 7ac82ae..b4aa3ba 100644
--- a/include/drivers/arm/tzc400.h
+++ b/include/drivers/arm/tzc400.h
@@ -147,8 +147,6 @@
 /* Filters are bit mapped 0 to 3. */
 #define TZC400_COMPONENT_ID	0xb105f00d
 
-#ifndef __ASSEMBLY__
-
 /*******************************************************************************
  * Function & variable prototypes
  ******************************************************************************/
@@ -205,6 +203,5 @@
 void tzc_disable_filters(const tzc_instance_t *controller);
 void tzc_set_action(const tzc_instance_t *controller, tzc_action_t action);
 
-#endif /*__ASSEMBLY__*/
 
 #endif /* __TZC400__ */
diff --git a/include/drivers/io_driver.h b/include/drivers/io_driver.h
index cade5e7..cc01d3b 100644
--- a/include/drivers/io_driver.h
+++ b/include/drivers/io_driver.h
@@ -31,7 +31,9 @@
 #ifndef __IO_DRIVER_H__
 #define __IO_DRIVER_H__
 
+#include <io_storage.h>
 #include <platform.h>   /* For MAX_IO_DEVICES */
+#include <stdint.h>
 
 
 /* Generic IO entity structure,representing an accessible IO construct on the
diff --git a/include/drivers/io_fip.h b/include/drivers/io_fip.h
index 56dd1e0..212570d 100644
--- a/include/drivers/io_fip.h
+++ b/include/drivers/io_fip.h
@@ -31,6 +31,8 @@
 #ifndef __IO_FIP_H__
 #define __IO_FIP_H__
 
+struct io_dev_connector;
+
 int register_io_dev_fip(struct io_dev_connector **dev_con);
 
 #endif /* __IO_FIP_H__ */
diff --git a/include/drivers/io_memmap.h b/include/drivers/io_memmap.h
index 5fa7bc9..0e59ecb 100644
--- a/include/drivers/io_memmap.h
+++ b/include/drivers/io_memmap.h
@@ -31,6 +31,8 @@
 #ifndef __IO_MEMMAP_H__
 #define __IO_MEMMAP_H__
 
+struct io_dev_connector;
+
 int register_io_dev_memmap(struct io_dev_connector **dev_con);
 
 #endif /* __IO_MEMMAP_H__ */
diff --git a/include/drivers/io_semihosting.h b/include/drivers/io_semihosting.h
index 7dc632d..eab290a 100644
--- a/include/drivers/io_semihosting.h
+++ b/include/drivers/io_semihosting.h
@@ -31,6 +31,8 @@
 #ifndef __IO_SH_H__
 #define __IO_SH_H__
 
+struct io_dev_connector;
+
 int register_io_dev_sh(struct io_dev_connector **dev_con);
 
 #endif /* __IO_SH_H__ */
diff --git a/include/lib/aarch64/arch.h b/include/lib/aarch64/arch.h
index 32167eb..d7e65b3 100644
--- a/include/lib/aarch64/arch.h
+++ b/include/lib/aarch64/arch.h
@@ -31,7 +31,6 @@
 #ifndef __ARCH_H__
 #define __ARCH_H__
 
-#include <bl_common.h>
 
 /*******************************************************************************
  * MIDR bit definitions
diff --git a/include/lib/aarch64/arch_helpers.h b/include/lib/aarch64/arch_helpers.h
index f55c003..565b1b4 100644
--- a/include/lib/aarch64/arch_helpers.h
+++ b/include/lib/aarch64/arch_helpers.h
@@ -31,10 +31,8 @@
 #ifndef __ARCH_HELPERS_H__
 #define __ARCH_HELPERS_H__
 
-#include <arch.h>
+#include <cdefs.h> /* For __dead2 */
 
-#ifndef __ASSEMBLY__
-#include <stdio.h>
 
 /*******************************************************************************
  * Aarch64 translation tables manipulation helper prototypes
@@ -268,6 +266,5 @@
 extern void write_cptr_el2(unsigned long);
 extern void write_cptr_el3(unsigned long);
 
-#endif /*__ASSEMBLY__*/
 
 #endif /* __ARCH_HELPERS_H__ */
diff --git a/include/lib/bakery_lock.h b/include/lib/bakery_lock.h
index 0def067..e6744a5 100644
--- a/include/lib/bakery_lock.h
+++ b/include/lib/bakery_lock.h
@@ -35,7 +35,6 @@
 
 #define BAKERY_LOCK_MAX_CPUS		PLATFORM_CORE_COUNT
 
-#ifndef __ASSEMBLY__
 typedef struct bakery_lock {
 	int owner;
 	volatile char entering[BAKERY_LOCK_MAX_CPUS];
@@ -48,6 +47,5 @@
 void bakery_lock_get(unsigned long mpidr, bakery_lock_t *bakery);
 void bakery_lock_release(unsigned long mpidr, bakery_lock_t *bakery);
 int bakery_lock_try(unsigned long mpidr, bakery_lock_t *bakery);
-#endif /*__ASSEMBLY__*/
 
 #endif /* __BAKERY_LOCK_H__ */
diff --git a/include/lib/io_storage.h b/include/lib/io_storage.h
index 59fd3f4..b6c5f9f 100644
--- a/include/lib/io_storage.h
+++ b/include/lib/io_storage.h
@@ -31,11 +31,8 @@
 #ifndef __IO_H__
 #define __IO_H__
 
-#ifndef __ASSEMBLY__
+#include <stdio.h> /* For ssize_t */
 
-#include <stdint.h>
-#include <stdio.h>	/* For ssize_t */
-
 
 /* Device type which can be used to enable policy decisions about which device
  * to access */
@@ -128,5 +125,4 @@
 int io_close(io_handle handle);
 
 
-#endif /* __ASSEMBLY__ */
 #endif /* __IO_H__ */
diff --git a/include/lib/mmio.h b/include/lib/mmio.h
index c79c3f5..d3c2cae 100644
--- a/include/lib/mmio.h
+++ b/include/lib/mmio.h
@@ -31,8 +31,6 @@
 #ifndef __MMIO_H__
 #define __MMIO_H__
 
-#ifndef __ASSEMBLY__
-
 #include <stdint.h>
 
 extern void mmio_write_8(uintptr_t addr, uint8_t value);
@@ -44,6 +42,4 @@
 extern void mmio_write_64(uintptr_t addr, uint64_t value);
 extern uint64_t mmio_read_64(uintptr_t addr);
 
-#endif /*__ASSEMBLY__*/
-
 #endif /* __MMIO_H__ */
diff --git a/include/lib/semihosting.h b/include/lib/semihosting.h
index e688618..9d0b39f 100644
--- a/include/lib/semihosting.h
+++ b/include/lib/semihosting.h
@@ -31,6 +31,9 @@
 #ifndef __SEMIHOSTING_H__
 #define __SEMIHOSTING_H__
 
+#include <stdio.h> /* For ssize_t */
+
+
 #define SEMIHOSTING_SYS_OPEN            0x01
 #define SEMIHOSTING_SYS_CLOSE           0x02
 #define SEMIHOSTING_SYS_WRITE0          0x04