Add tf-a external component

Allows code from tf-a to be compiled into TS deployments. tf-a includes
many components that can be reused in TS deployments, particularly in
the area of storage access and firmware update. This commit adds a
new tf-a external component which can be used to make tf-a components
available in TS builds. To provide a pattern for how tf-a components
can be used, a CFI NOR flash driver has been added. This shows how
the a component within the TS tree is used to reference source files
from the fetched tf-a repo. The CFI driver can be built but has not
actually been tested so more work is almost certainly needed to make
it usable. Even in its current state, the driver provides a reference
for how other components from tf-a can be reused. To enable a full
build to be tested that includes tf-a dependencies, the cfi-flash-optee
block-storage build configuartion has been added.

This allows
reuse of platform level drivers and code from TF-A. The first TF-A
code to reuse is a CFI NOR flash driver which is compatible with fvp
base platforms.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: I69dacab1f4a0b2ab894e4130a0b54cff2a914563
diff --git a/deployments/block-storage/config/cfi-flash-optee/CMakeLists.txt b/deployments/block-storage/config/cfi-flash-optee/CMakeLists.txt
new file mode 100644
index 0000000..156cfb1
--- /dev/null
+++ b/deployments/block-storage/config/cfi-flash-optee/CMakeLists.txt
@@ -0,0 +1,108 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
+
+#-------------------------------------------------------------------------------
+# Set default platform. TS_PLATFORM should be set externally to build for
+# an alternative platform from the default one.
+#
+#-------------------------------------------------------------------------------
+set(TS_PLATFORM "arm/fvp/fvp_base_revc-2xaemv8a" CACHE STRING "Target platform location.")
+
+include(../../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the block_storage deployment for opteesp
+#
+#  Builds the block_storage service provider for running in an SEL0 secure partition
+#  hosted by OPTEE in the role of SPM.
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/opteesp/env.cmake)
+project(trusted-services LANGUAGES C ASM)
+add_executable(block-storage)
+target_include_directories(block-storage PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+set(SP_UUID_CANON "63646e80-eb52-462f-ac4f-8cdf3987519c")
+set(SP_HEAP_SIZE "120 * 1024" CACHE STRING "SP heap size in bytes")
+set(TRACE_PREFIX "BLOCK" CACHE STRING "Trace prefix")
+include(${TS_ROOT}/tools/cmake/common/TargetCompileDefinitions.cmake)
+set_target_uuids(
+	SP_UUID ${SP_UUID_CANON}
+	SP_NAME "block-storage"
+)
+
+target_include_directories(block-storage PRIVATE
+	${CMAKE_CURRENT_LIST_DIR}
+)
+
+#-------------------------------------------------------------------------------
+#  Deployment specific components. This deployment uses an infrastructure that
+#  that provides ram-backed block stoarged, configured with storage partitions
+#  that conform to the 'ref' scheme used for test.
+#-------------------------------------------------------------------------------
+add_components(TARGET "block-storage"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"environments/opteesp"
+)
+
+include(../../env/commonsp/block_storage_sp.cmake REQUIRED)
+include(../../block-storage.cmake REQUIRED)
+
+# Replace with suitable infrastructure when suitable nor flash adapter is implemented
+include(../../infra/ref-ram.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  External project dependencies
+#
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/external/tf_a/tf-a.cmake)
+add_tfa_dependency(TARGET "block-storage")
+
+#-------------------------------------------------------------------------------
+#  Set target platform to provide drivers needed by the deployment
+#
+#-------------------------------------------------------------------------------
+add_platform(TARGET "block-storage")
+
+#-------------------------------------------------------------------------------
+#  Deployment specific build options
+#-------------------------------------------------------------------------------
+target_compile_definitions(block-storage PRIVATE
+	ARM64=1
+)
+
+if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
+	target_compile_options(block-storage PRIVATE
+		-std=c99
+	)
+
+endif()
+
+compiler_generate_stripped_elf(TARGET block-storage NAME "${SP_UUID_CANON}.stripped.elf" RES STRIPPED_ELF)
+
+#-------------------------------------------------------------------------------
+#  Deployment specific install options
+#-------------------------------------------------------------------------------
+if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
+	set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "location to install build output to." FORCE)
+endif()
+
+install(TARGETS block-storage
+			PUBLIC_HEADER DESTINATION ${TS_ENV}/include
+			RUNTIME DESTINATION ${TS_ENV}/bin
+		)
+install(FILES ${STRIPPED_ELF} DESTINATION ${TS_ENV}/bin)
+
+include(${TS_ROOT}/tools/cmake/common/ExportSp.cmake)
+export_sp(
+	SP_UUID_CANON ${SP_UUID_CANON}
+	SP_UUID_LE ${SP_UUID_LE}
+	SP_NAME "block-storage"
+	MK_IN ${TS_ROOT}/environments/opteesp/sp.mk.in
+	DTS_IN ${CMAKE_CURRENT_LIST_DIR}/default_block-storage.dts.in
+	JSON_IN ${TS_ROOT}/environments/opteesp/sp_pkg.json.in
+)
diff --git a/deployments/block-storage/config/cfi-flash-optee/default_block-storage.dts.in b/deployments/block-storage/config/cfi-flash-optee/default_block-storage.dts.in
new file mode 100644
index 0000000..0d30738
--- /dev/null
+++ b/deployments/block-storage/config/cfi-flash-optee/default_block-storage.dts.in
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+@DTS_TAG@
+
+@DTS_NODE@ {
+	compatible = "arm,ffa-manifest-1.0";
+	ffa-version = <0x00010000>; /* 31:16 - Major, 15:0 - Minor */
+	uuid = <@EXPORT_SP_UUID_DT@>;
+	description = "BlockStorage";
+	execution-ctx-count = <1>;
+	exception-level = <1>; /* S-EL0 */
+	execution-state = <0>; /* AArch64 */
+	xlat-granule = <0>; /* 4KiB */
+	messaging-method = <3>; /* Direct messaging only */
+	legacy-elf-format = <1>;
+};
diff --git a/deployments/block-storage/config/cfi-flash-optee/optee_sp_user_defines.h b/deployments/block-storage/config/cfi-flash-optee/optee_sp_user_defines.h
new file mode 100644
index 0000000..798e873
--- /dev/null
+++ b/deployments/block-storage/config/cfi-flash-optee/optee_sp_user_defines.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#ifndef OPTEE_SP_USER_DEFINES_H
+#define OPTEE_SP_USER_DEFINES_H
+
+#define OPTEE_SP_FLAGS			0
+
+/* Provisioned stack size */
+#define OPTEE_SP_STACK_SIZE			(64 * 1024)
+
+#endif /* SP_HEADER_DEFINES_H */
diff --git a/external/tf_a/tf-a.cmake b/external/tf_a/tf-a.cmake
new file mode 100644
index 0000000..2f5d81f
--- /dev/null
+++ b/external/tf_a/tf-a.cmake
@@ -0,0 +1,56 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+#-------------------------------------------------------------------------------
+#  Fetch or use externally provided source tree
+#
+#-------------------------------------------------------------------------------
+if (DEFINED ENV{TS_TFA_PATH})
+	# Use externally provided source tree
+	set(TFA_SOURCE_DIR $ENV{TS_TFA_PATH} CACHE PATH "tf-a location" FORCE)
+else()
+	# Otherwise clone the tf-a repo
+	set(TFA_URL "https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git" CACHE STRING "tf-a repository URL")
+	set(TFA_REFSPEC "v2.7.0" CACHE STRING "tf-a git refspec")
+	set(TFA_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/tf-a-src" CACHE PATH "Location of tf-a source")
+
+	# Checking git
+	find_program(GIT_COMMAND "git")
+	if (NOT GIT_COMMAND)
+		message(FATAL_ERROR "Please install git")
+	endif()
+
+	set(GIT_OPTIONS
+		GIT_REPOSITORY ${TFA_URL}
+		GIT_TAG ${TFA_REFSPEC}
+		GIT_SHALLOW FALSE
+	)
+
+	include(${TS_ROOT}/tools/cmake/common/LazyFetch.cmake REQUIRED)
+	LazyFetch_MakeAvailable(
+		DEP_NAME tf-a
+		FETCH_OPTIONS "${GIT_OPTIONS}"
+		SOURCE_DIR ${TFA_SOURCE_DIR}
+	)
+endif()
+
+#-------------------------------------------------------------------------------
+#  Add tf-a as an external dependency
+#
+#-------------------------------------------------------------------------------
+function(add_tfa_dependency)
+	set(options  )
+	set(oneValueArgs TARGET)
+	cmake_parse_arguments(MY_PARAMS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
+
+	if(NOT DEFINED MY_PARAMS_TARGET)
+		message(FATAL_ERROR "add_tf-a: mandatory parameter TARGET not defined!")
+	endif()
+
+	target_include_directories(${MY_PARAMS_TARGET} PRIVATE "${TFA_SOURCE_DIR}/include")
+
+endfunction()
\ No newline at end of file
diff --git a/platform/drivers/tf-a/drivers/cfi/v2m/v2m_flash.cmake b/platform/drivers/tf-a/drivers/cfi/v2m/v2m_flash.cmake
new file mode 100644
index 0000000..cd68572
--- /dev/null
+++ b/platform/drivers/tf-a/drivers/cfi/v2m/v2m_flash.cmake
@@ -0,0 +1,13 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(_DRIVER_DIR "${TFA_SOURCE_DIR}/drivers/cfi/v2m")
+
+target_sources(${TGT} PRIVATE
+	"${_DRIVER_DIR}/v2m_flash.c"
+)
+
diff --git a/platform/providers/arm/fvp/fvp_base_revc-2xaemv8a/platform.cmake b/platform/providers/arm/fvp/fvp_base_revc-2xaemv8a/platform.cmake
index ff7c049..adefbae 100644
--- a/platform/providers/arm/fvp/fvp_base_revc-2xaemv8a/platform.cmake
+++ b/platform/providers/arm/fvp/fvp_base_revc-2xaemv8a/platform.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -20,3 +20,7 @@
 if ("trng" IN_LIST _platform_driver_dependencies)
 	include(${TS_ROOT}/platform/drivers/arm/juno_trng/driver.cmake)
 endif()
+
+if ("secure-nor-flash" IN_LIST _platform_driver_dependencies)
+	include(${TS_ROOT}/platform/drivers/tf-a/drivers/cfi/v2m/v2m_flash.cmake)
+endif()
\ No newline at end of file