Create logging SP deployment

Create an OP-TEE SP deployment for logging SP with UART backend in
the current configuration.

Signed-off-by: Gabor Toth <gabor.toth2@arm.com>
Signed-off-by: Gabor Ambrus <gabor.ambrus@arm.com>
Change-Id: Id2dbae916ea55a1fa7e8a7b156c772934a123d45
diff --git a/deployments/logging/config/default-opteesp/CMakeLists.txt b/deployments/logging/config/default-opteesp/CMakeLists.txt
new file mode 100644
index 0000000..dfe6e1c
--- /dev/null
+++ b/deployments/logging/config/default-opteesp/CMakeLists.txt
@@ -0,0 +1,95 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
+
+# Set default platform.
+set(TS_PLATFORM "arm/fvp/fvp_base_revc-2xaemv8a" CACHE STRING "Target platform location.")
+
+
+include(../../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the logging deployment for opteesp
+#
+#  Builds the logging 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(logging)
+target_include_directories(logging PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+set(SP_BIN_UUID_CANON "da9dffbd-d590-40ed-975f-19c65a3d52d3")
+set(SP_FFA_UUID_CANON "${TS_RPC_UUID_CANON}")
+set(SP_HEAP_SIZE "32 * 1024" CACHE STRING "SP heap size in bytes")
+set(TRACE_PREFIX "LOGGING" CACHE STRING "Trace prefix")
+
+target_include_directories(logging PRIVATE
+	${CMAKE_CURRENT_LIST_DIR}
+)
+
+#-------------------------------------------------------------------------------
+#  Add components - this deployment uses an infrastructure that provides
+#  ram backed storage for SFS.
+#
+#-------------------------------------------------------------------------------
+add_components(TARGET "logging"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		environments/opteesp
+)
+
+include(../../env/commonsp/logging_sp.cmake REQUIRED)
+include(../../logging.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  Set target platform to provide drivers needed by the deployment
+#
+#-------------------------------------------------------------------------------
+add_platform(TARGET "logging")
+
+#-------------------------------------------------------------------------------
+#  Deployment specific build options
+#
+#-------------------------------------------------------------------------------
+target_compile_definitions(logging PRIVATE
+	ARM64=1
+)
+
+if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
+	target_compile_options(logging PRIVATE
+		-std=c99
+	)
+
+endif()
+
+#-------------------------------------------------------------------------------
+#  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 logging
+			PUBLIC_HEADER DESTINATION ${TS_ENV}/include
+			RUNTIME DESTINATION ${TS_ENV}/bin
+		)
+
+install(TARGETS logging
+			PUBLIC_HEADER DESTINATION ${TS_ENV}/include
+			RUNTIME DESTINATION ${TS_ENV}/bin
+		)
+
+include(${TS_ROOT}/tools/cmake/common/ExportSp.cmake)
+export_sp(
+	SP_FFA_UUID_CANON ${SP_FFA_UUID_CANON}
+	SP_BIN_UUID_CANON ${SP_BIN_UUID_CANON}
+	SP_UUID_LE ${SP_UUID_LE}
+	SP_NAME "logging"
+	MK_IN ${TS_ROOT}/environments/opteesp/sp.mk.in
+	DTS_IN ${CMAKE_CURRENT_LIST_DIR}/default_logging.dts.in
+	JSON_IN ${TS_ROOT}/environments/opteesp/sp_pkg.json.in
+)
diff --git a/deployments/logging/config/default-opteesp/default_logging.dts.in b/deployments/logging/config/default-opteesp/default_logging.dts.in
new file mode 100644
index 0000000..8eaf795
--- /dev/null
+++ b/deployments/logging/config/default-opteesp/default_logging.dts.in
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2023, 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 = "Logging";
+	execution-ctx-count = <1>;
+	exception-level = <1>; /* S-EL0 */
+	execution-state = <0>; /* AArch64 */
+	xlat-granule = <0>; /* 4KiB */
+	messaging-method = <3>; /* Direct messaging only */
+	ns-interrupts-action = <2>; /* Non-secure interrupts are signaled */
+	elf-format = <1>;
+
+	device-regions {
+		compatible = "arm,ffa-manifest-device-regions";
+
+		uart {
+			/* Armv8 A Foundation Platform values */
+			base-address = <0x00000000 0x1c0b0000>;
+			pages-count = <1>;
+			attributes = <0x3>; /* read-write */
+		};
+	};
+};
+
diff --git a/deployments/logging/config/default-opteesp/optee_sp_user_defines.h b/deployments/logging/config/default-opteesp/optee_sp_user_defines.h
new file mode 100644
index 0000000..e5fb8c1
--- /dev/null
+++ b/deployments/logging/config/default-opteesp/optee_sp_user_defines.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2023, 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/deployments/logging/config/default-sp/CMakeLists.txt b/deployments/logging/config/default-sp/CMakeLists.txt
new file mode 100644
index 0000000..95da442
--- /dev/null
+++ b/deployments/logging/config/default-sp/CMakeLists.txt
@@ -0,0 +1,106 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
+
+# Set default platform.
+set(TS_PLATFORM "arm/fvp/fvp_base_revc-2xaemv8a" CACHE STRING "Target platform location.")
+
+
+include(../../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the protected-storage deployment for generic
+#  sp environment.
+#
+#  Builds the secure storage service provider for running in an SEL0 secure
+#  partition hosted by any SPM.
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/sp/env.cmake)
+project(trusted-services LANGUAGES C ASM)
+add_executable(logging)
+target_include_directories(logging PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+
+set(SP_NAME "logging")
+set(SP_BIN_UUID_CANON "da9dffbd-d590-40ed-975f-19c65a3d52d3")
+set(SP_FFA_UUID_CANON "${TS_RPC_UUID_CANON}")
+set(SP_HEAP_SIZE "32 * 1024" CACHE STRING "SP heap size in bytes")
+set(SP_STACK_SIZE "64 * 1024" CACHE STRING "Stack size")
+set(TRACE_PREFIX "LOGGING" CACHE STRING "Trace prefix")
+
+target_include_directories(logging PRIVATE
+	${CMAKE_CURRENT_LIST_DIR}
+)
+
+#-------------------------------------------------------------------------------
+#  Add components - this deployment uses an infrastructure that provides
+#  ram backed storage for SFS.
+#
+#-------------------------------------------------------------------------------
+add_components(TARGET "logging"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		environments/sp
+)
+
+include(../../env/commonsp/logging_sp.cmake REQUIRED)
+include(../../logging.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  Set target platform to provide drivers needed by the deployment
+#
+#-------------------------------------------------------------------------------
+add_platform(TARGET "logging")
+
+#-------------------------------------------------------------------------------
+#  Deployment specific build options
+#
+#-------------------------------------------------------------------------------
+target_compile_definitions(logging PRIVATE
+	ARM64=1
+)
+
+if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
+	target_compile_options(logging PRIVATE
+		-std=c99
+		-Werror
+	)
+
+endif()
+
+compiler_generate_binary_output(TARGET logging NAME "${SP_BIN_UUID_CANON}.bin" SP_BINARY)
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${SP_BIN_UUID_CANON}.bin DESTINATION ${TS_ENV}/bin)
+
+include(${TS_ROOT}/tools/cmake/common/ExportMemoryRegionsToManifest.cmake REQUIRED)
+export_memory_regions_to_manifest(TARGET logging NAME "${SP_BIN_UUID_CANON}_memory_regions.dtsi" RES EXPORT_MEMORY_REGIONS_DTSI)
+
+#-------------------------------------------------------------------------------
+#  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 logging
+			PUBLIC_HEADER DESTINATION ${TS_ENV}/include
+			RUNTIME DESTINATION ${TS_ENV}/bin
+		)
+
+install(TARGETS logging
+			PUBLIC_HEADER DESTINATION ${TS_ENV}/include
+			RUNTIME DESTINATION ${TS_ENV}/bin
+		)
+
+include(${TS_ROOT}/tools/cmake/common/ExportSp.cmake)
+export_sp(
+	SP_FFA_UUID_CANON ${SP_FFA_UUID_CANON}
+	SP_BIN_UUID_CANON ${SP_BIN_UUID_CANON}
+	SP_UUID_LE ${SP_UUID_LE}
+	SP_NAME ${SP_NAME}
+	DTS_IN ${CMAKE_CURRENT_LIST_DIR}/default_${SP_NAME}.dts.in
+	DTS_MEM_REGIONS ${SP_BIN_UUID_CANON}_memory_regions.dtsi
+	JSON_IN ${TS_ROOT}/environments/sp/sp_pkg.json.in
+)
diff --git a/deployments/logging/config/default-sp/default_logging.dts.in b/deployments/logging/config/default-sp/default_logging.dts.in
new file mode 100644
index 0000000..165ea2d
--- /dev/null
+++ b/deployments/logging/config/default-sp/default_logging.dts.in
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2024, 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 = "Logging";
+	execution-ctx-count = <1>;
+	exception-level = <1>; /* S-EL0 */
+	execution-state = <0>; /* AArch64 */
+	xlat-granule = <0>; /* 4KiB */
+	messaging-method = <3>; /* Direct messaging only */
+	ns-interrupts-action = <2>; /* Non-secure interrupts are signaled */
+
+	memory-regions {
+		compatible = "arm,ffa-manifest-memory-regions";
+
+		#include "@EXPORT_DTS_MEM_REGIONS@"
+	};
+
+	device-regions {
+		compatible = "arm,ffa-manifest-device-regions";
+
+		uart {
+			/* Armv8 A Foundation Platform values */
+			base-address = <0x00000000 0x1c0b0000>;
+			pages-count = <1>;
+			attributes = <0x3>; /* read-write */
+		};
+	};
+};
+
diff --git a/deployments/logging/env/commonsp/logging_sp.c b/deployments/logging/env/commonsp/logging_sp.c
new file mode 100644
index 0000000..af81d2c
--- /dev/null
+++ b/deployments/logging/env/commonsp/logging_sp.c
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <components/service/log/backend/uart/uart_backend.h>
+#include <components/service/log/factory/log_factory.h>
+#include <components/service/log/provider/log_provider.h>
+#include <sp_api.h>
+#include <sp_discovery.h>
+#include <sp_messaging.h>
+#include <sp_rxtx.h>
+#include <trace.h>
+
+#include "config/loader/sp/sp_config_loader.h"
+#include "config/ramstore/config_ramstore.h"
+#include "rpc/ts_rpc/endpoint/sp/ts_rpc_endpoint_sp.h"
+
+static bool sp_init(uint16_t *own_sp_id);
+
+void __noreturn sp_main(union ffa_boot_info *boot_info)
+{
+	struct rpc_service_interface *log_iface = NULL;
+	struct ts_rpc_endpoint_sp rpc_endpoint = { 0 };
+	struct sp_msg req_msg = { 0 };
+	struct sp_msg resp_msg = { 0 };
+	struct log_provider log_provider = { 0 };
+	struct log_backend *log_backend = NULL;
+	uint16_t own_id = 0;
+	sp_result result = SP_RESULT_INTERNAL_ERROR;
+	rpc_status_t rpc_status = RPC_ERROR_INTERNAL;
+
+	/* Boot */
+	if (!sp_init(&own_id)) {
+		EMSG("Failed to init SP");
+		goto fatal_error;
+	}
+
+	config_ramstore_init();
+
+	if (!sp_config_load(boot_info)) {
+		EMSG("Failed to load SP config");
+		goto fatal_error;
+	}
+
+	log_backend = uart_backend_init(0);
+	if (!log_backend) {
+		EMSG("Failed to create log backend.");
+		goto fatal_error;
+	}
+	log_iface = log_provider_init(&log_provider, log_backend);
+
+	if (!log_iface) {
+		EMSG("Failed to init service provider");
+		goto fatal_error;
+	}
+
+	rpc_status = ts_rpc_endpoint_sp_init(&rpc_endpoint, 1, 16);
+	if (rpc_status != RPC_SUCCESS) {
+		EMSG("Failed to initialize RPC endpoint: %d", rpc_status);
+		goto fatal_error;
+	}
+
+	rpc_status = ts_rpc_endpoint_sp_add_service(&rpc_endpoint, log_iface);
+	if (rpc_status != RPC_SUCCESS) {
+		EMSG("Failed to add service to RPC endpoint: %d", rpc_status);
+		goto fatal_error;
+	}
+
+	/* End of boot phase */
+	result = sp_msg_wait(&req_msg);
+	if (result != SP_RESULT_OK) {
+		EMSG("Failed to send message wait %d", result);
+		goto fatal_error;
+	}
+
+	while (1) {
+		ts_rpc_endpoint_sp_receive(&rpc_endpoint, &req_msg, &resp_msg);
+
+		result = sp_msg_send_direct_resp(&resp_msg, &req_msg);
+		if (result != SP_RESULT_OK) {
+			EMSG("Failed to send direct response %d", result);
+			result = sp_msg_wait(&req_msg);
+			if (result != SP_RESULT_OK) {
+				EMSG("Failed to send message wait %d", result);
+				goto fatal_error;
+			}
+		}
+	}
+
+fatal_error:
+	/* SP is not viable */
+	EMSG("LOGGING SP error");
+	while (1) {
+	}
+}
+
+void sp_interrupt_handler(uint32_t interrupt_id)
+{
+	(void)interrupt_id;
+}
+
+static bool sp_init(uint16_t *own_id)
+{
+	sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
+	static uint8_t tx_buffer[4096] __aligned(4096);
+	static uint8_t rx_buffer[4096] __aligned(4096);
+
+	sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("Failed to map RXTX buffers: %d", sp_res);
+		return false;
+	}
+
+	sp_res = sp_discovery_own_id_get(own_id);
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("Failed to query own ID: %d", sp_res);
+		return false;
+	}
+
+	return true;
+}
diff --git a/deployments/logging/env/commonsp/logging_sp.cmake b/deployments/logging/env/commonsp/logging_sp.cmake
new file mode 100644
index 0000000..d3e2e4f
--- /dev/null
+++ b/deployments/logging/env/commonsp/logging_sp.cmake
@@ -0,0 +1,32 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Includes components needed for deploying the logging service provider
+# within a secure partition.
+#-------------------------------------------------------------------------------
+
+#-------------------------------------------------------------------------------
+# Common components for logging sp deployments
+#
+#-------------------------------------------------------------------------------
+add_components(TARGET "logging"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/common/fdt"
+		"components/common/trace"
+		"components/common/utils"
+		"components/config/ramstore"
+		"components/config/loader/sp"
+		"components/messaging/ffa/libsp"
+		"components/rpc/common/interface"
+		"components/rpc/common/endpoint"
+		"components/rpc/ts_rpc/common"
+		"components/rpc/ts_rpc/endpoint/sp"
+		"components/service/log/backend/uart/uart_adapter/platform"
+)
+
+target_sources(logging PRIVATE
+	${CMAKE_CURRENT_LIST_DIR}/logging_sp.c
+)
diff --git a/deployments/logging/env/commonsp/logging_sp.h b/deployments/logging/env/commonsp/logging_sp.h
new file mode 100644
index 0000000..618123f
--- /dev/null
+++ b/deployments/logging/env/commonsp/logging_sp.h
@@ -0,0 +1,10 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef LOGGING_SP_H
+#define LOGGING_SP_H
+
+#endif /* LOGGING_SP_H */
diff --git a/deployments/logging/logging.cmake b/deployments/logging/logging.cmake
new file mode 100644
index 0000000..7792911
--- /dev/null
+++ b/deployments/logging/logging.cmake
@@ -0,0 +1,26 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+add_components(TARGET "logging"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		components/rpc/common/caller
+		components/service/common/include
+		components/service/common/client
+		components/service/common/provider
+		components/service/log/client
+		components/service/log/factory
+		components/service/log/provider
+		components/service/log/backend/uart
+		protocols/rpc/common/packed-c
+		protocols/service/log/packed-c
+)
+
+target_include_directories(logging PRIVATE
+	${TS_ROOT}
+	${TS_ROOT}/components
+)