Add Test Secure Payload Dispatcher (TSPD) service

This patch adds the TSPD service which is responsible for managing
communication between the non-secure state and the Test Secure Payload
(TSP) executing in S-EL1.

The TSPD does the following:

1. Determines the location of the TSP (BL3-2) image and passes control
   to it for initialization. This is done by exporting the 'bl32_init()'
   function.

2. Receives a structure containing the various entry points into the TSP
   image as a response to being initialized. The TSPD uses this
   information to determine how the TSP should be entered depending on
   the type of operation.

3. Implements a synchronous mechanism for entering into and returning
   from the TSP image. This mechanism saves the current C runtime
   context on top of the current stack and jumps to the TSP through an
   ERET instruction. The TSP issues an SMC to indicate completion of the
   previous request. The TSPD restores the saved C runtime context and
   resumes TSP execution.

This patch also introduces a Make variable 'SPD' to choose the specific
SPD to include in the build. By default, no SPDs are included in the
build.

Change-Id: I124da5695cdc510999b859a1bf007f4d049e04f3
Co-authored-by: Jeenu Viswambharan <jeenu.viswambharan@arm.com>
diff --git a/Makefile b/Makefile
index a5a3a78..7b0f78b 100644
--- a/Makefile
+++ b/Makefile
@@ -70,6 +70,8 @@
 
 # By default, build all platforms available
 PLAT			?=	all
+# By default, build no SPD component
+SPD			?=	none
 
 BUILD_BASE		:=	./build
 BUILD_PLAT		:=	${BUILD_BASE}/${PLAT}/${BUILD_TYPE}
@@ -79,6 +81,7 @@
 BUILD_DIRS		:=	${BUILD_BL1} ${BUILD_BL2} ${BUILD_BL31}
 
 PLATFORMS		:=	$(shell ls -I common plat/)
+SPDS			:=	$(shell ls -I none services/spd)
 HELP_PLATFORMS		:=	$(shell echo ${PLATFORMS} | sed 's/ /|/g')
 
 ifeq (${PLAT},)
@@ -111,13 +114,29 @@
   include bl31/bl31.mk
 endif
 
+# Include SPD Makefile if one has been specified
+ifneq (${SPD},none)
+  # We expect to locate an spd.mk under the specified SPD directory
+  SPD_MAKE		:=	$(shell m="services/spd/${SPD}/${SPD}.mk"; [ -f "$$m" ] && echo "$$m")
+
+  ifeq (${SPD_MAKE},)
+    $(error Error: No services/spd/${SPD}/${SPD}.mk located)
+  endif
+  $(info Including ${SPD_MAKE})
+  include ${SPD_MAKE}
+
+  # If there's BL32 companion for the chosen SPD, and the SPD wants to build the
+  # BL2 from source, we expect that the SPD's Makefile would set NEED_BL32
+  # variable to "yes"
+endif
+
 .PHONY:			all msg_start ${PLATFORMS} dump clean realclean distclean bl1 bl2 bl31 cscope locate-checkpatch checkcodebase checkpatch fiptool fip locate-bl33
 .SUFFIXES:
 
 
 BL1_OBJS		:= 	$(addprefix ${BUILD_BL1}/,${BL1_OBJS} ${BL_COMMON_OBJS} ${PLAT_BL_COMMON_OBJS})
 BL2_OBJS		:= 	$(addprefix ${BUILD_BL2}/,${BL2_OBJS} ${BL_COMMON_OBJS} ${PLAT_BL_COMMON_OBJS})
-BL31_OBJS		:= 	$(addprefix ${BUILD_BL31}/,${BL31_OBJS} ${BL_COMMON_OBJS} ${PLAT_BL_COMMON_OBJS})
+BL31_OBJS		:= 	$(addprefix ${BUILD_BL31}/,${BL31_OBJS} ${BL_COMMON_OBJS} ${PLAT_BL_COMMON_OBJS} ${SPD_OBJS})
 BL1_MAPFILE		:= 	$(addprefix ${BUILD_BL1}/,${BL1_MAPFILE})
 BL2_MAPFILE		:= 	$(addprefix ${BUILD_BL2}/,${BL2_MAPFILE})
 BL31_MAPFILE		:= 	$(addprefix ${BUILD_BL31}/,${BL31_MAPFILE})
@@ -125,7 +144,6 @@
 BL2_LINKERFILE		:= 	$(addprefix ${BUILD_BL2}/,${BL2_LINKERFILE})
 BL31_LINKERFILE		:= 	$(addprefix ${BUILD_BL31}/,${BL31_LINKERFILE})
 
-
 INCLUDES		+=	-Ilib/include/			\
 				-Idrivers/io			\
 				-Iinclude/${ARCH}/		\
@@ -135,7 +153,8 @@
 				-Iinclude/stdlib		\
 				-Iinclude/stdlib/sys		\
 				-Iplat/${PLAT}			\
-				${PLAT_INCLUDES}
+				${PLAT_INCLUDES}		\
+				${SPD_INCLUDES}
 
 ASFLAGS			+= 	-nostdinc -ffreestanding -Wa,--fatal-warnings	\
 				-mgeneral-regs-only -D__ASSEMBLY__ ${INCLUDES}	\
@@ -185,7 +204,7 @@
 
 # Variables for use with Firmware Image Package
 FIPTOOLPATH		?=	tools/fip_create
-FIPTOOL		?=	${FIPTOOLPATH}/fip_create
+FIPTOOL			?=	${FIPTOOLPATH}/fip_create
 fiptool:		${FIPTOOL}
 fip:			${BUILD_PLAT}/fip.bin
 
@@ -214,6 +233,53 @@
 endif
 
 
+# If BL32 needs to be built, provide necessary build rules and targets
+ifeq (${NEED_BL32},yes)
+BUILD_BL32		:=	${BUILD_PLAT}/bl32
+BUILD_DIRS		+=	${BUILD_BL32}
+
+BL32_OBJS		:=	$(addprefix ${BUILD_BL32}/,${BL32_OBJS})
+BL32_MAPFILE		:=	$(addprefix ${BUILD_BL32}/,${BL32_MAPFILE})
+BL32_LINKERFILE	:=	$(addprefix ${BUILD_BL32}/,${BL32_LINKERFILE})
+BL32_LDFLAGS		:=	-Map=${BL32_MAPFILE} --script ${BL32_LINKERFILE} --entry=${BL32_ENTRY_POINT}
+
+bl32:			${BUILD_BL32} ${BUILD_PLAT}/bl32.bin
+all:			bl32
+dump:			bl32_dump
+.PHONY:			bl32
+
+# Add BL32 image to FIP's input image list
+FIP_DEPS		:= bl32
+FIP_ARGS		:= --bl32 ${BUILD_PLAT}/bl32.bin
+
+${BUILD_BL32}/%.o:	%.S
+			@echo "  AS      $<"
+			${Q}${AS} ${ASFLAGS} -c $< -o $@
+
+${BUILD_BL32}/%.o:	%.c
+			@echo "  CC      $<"
+			${Q}${CC} ${CFLAGS} -c $< -o $@
+
+${BUILD_BL32}/%.ld:	%.ld.S
+			@echo "  PP      $<"
+			${Q}${AS} ${ASFLAGS} -P -E $< -o $@
+
+${BUILD_BL32}/bl32.elf:	${BL32_OBJS} ${BL32_LINKERFILE}
+			@echo "  LD      $@"
+			${Q}${LD} -o $@ ${LDFLAGS} ${BL32_LDFLAGS} ${BL32_OBJS}
+
+${BUILD_PLAT}/bl32.bin:	${BUILD_BL32}/bl32.elf
+			@echo "  BIN     $@"
+			${Q}${OC} -O binary $< $@
+			@echo
+			@echo "Built $@ successfully"
+			@echo
+
+bl32_dump:
+	${Q}${OD} -d ${BUILD_BL32}/bl32.elf > ${BUILD_BL32}/bl32.dump
+endif
+
+
 clean:
 			@echo "  CLEAN"
 			${Q}rm -rf ${BUILD_PLAT}
@@ -323,13 +389,14 @@
 			@echo "Built $@ successfully"
 			@echo
 
-${BUILD_PLAT}/fip.bin:	bl2 bl31 locate-bl33 ${FIPTOOL}
+${BUILD_PLAT}/fip.bin:	bl2 bl31 ${FIP_DEPS} locate-bl33 ${FIPTOOL}
 			@echo " CREATE FIRMWARE IMAGE PACKAGE $@"
 			@echo
 			${Q}${FIPTOOL} --dump \
 				--bl2 ${BUILD_PLAT}/bl2.bin \
 				--bl31 ${BUILD_PLAT}/bl31.bin \
 				--bl33 ${BL33} \
+				${FIP_ARGS} \
 				$@
 			@echo
 
@@ -345,9 +412,8 @@
 	@echo "PLAT is used to specify which platform you wish to build."
 	@echo ""
 	@echo "Supported Targets:"
-	@echo "  all            Build the BL1, BL2 and BL31 binaries"
+	@echo "  all            Build the BL1, BL31 binaries"
 	@echo "  bl1            Build the BL1 binary"
-	@echo "  bl2            Build the BL2 binary"
 	@echo "  bl31           Build the BL31 binary"
 	@echo "  checkcodebase  Check the coding style of the entire source tree"
 	@echo "  checkpatch     Check the coding style on changes in the current"