Upstream-Status: Pending [Not submitted to upstream yet]
Signed-off-by: Vishnu Banavath <vishnu.banavath@arm.com>

From 8e25c9b4617dcbb5800b25ace93371d9bcd68e61 Mon Sep 17 00:00:00 2001
From: Vishnu Banavath <vishnu.banavath@arm.com>
Date: Fri, 3 Dec 2021 16:36:51 +0000
Subject: [PATCH] Add openamp to SE proxy deployment

Openamp is required to communicate between secure partitions(running on
Cortex-A) and trusted-firmware-m(running on Cortex-M).
These changes are to fetch libmetal and openamp from github repo's
and build it.

Signed-off-by: Rui Miguel Silva <rui.silva@arm.com>
Signed-off-by: Vishnu Banavath <vishnu.banavath@arm.com>

%% original patch: 0007-Add-openamp-to-SE-proxy-deployment.patch

diff --git a/deployments/se-proxy/opteesp/CMakeLists.txt b/deployments/se-proxy/opteesp/CMakeLists.txt
index 4e2069a..248bd7e 100644
--- a/deployments/se-proxy/opteesp/CMakeLists.txt
+++ b/deployments/se-proxy/opteesp/CMakeLists.txt
@@ -89,6 +89,7 @@ add_components(TARGET "se-proxy"
 target_sources(se-proxy PRIVATE
 	se_proxy_sp.c
 	service_proxy_factory.c
+	lse.S
 )
 
 #-------------------------------------------------------------------------------
@@ -108,6 +109,19 @@ include(../../../external/nanopb/nanopb.cmake)
 target_link_libraries(se-proxy PRIVATE nanopb::protobuf-nanopb-static)
 protobuf_generate_all(TGT "se-proxy" NAMESPACE "protobuf" BASE_DIR "${TS_ROOT}/protocols")
 
+# libmetal
+list(APPEND LIBMETAL_EXTERNAL_INCLUDE_PATHS ${SP_DEV_KIT_INCLUDE_DIR})
+set(LIBMETAL_EXTRA_INCLUDE_PATHS ${LIBMETAL_EXTRA_INCLUDE_PATHS}
+	CACHE STRING "" FORCE)
+include(../../../external/openamp/libmetal.cmake)
+
+# OpenAMP
+list(APPEND OPENAMP_EXTERNAL_INCLUDE_PATHS ${SP_DEV_KIT_INCLUDE_DIR})
+set(OPENAMP_EXTRA_INCLUDE_PATHS ${OPENAMP_EXTRA_INCLUDE_PATHS}
+	CACHE STRING "" FORCE)
+include(../../../external/openamp/openamp.cmake)
+target_link_libraries(se-proxy PRIVATE openamp libmetal)
+
 #################################################################
 
 target_compile_definitions(se-proxy PRIVATE
diff --git a/deployments/se-proxy/opteesp/lse.S b/deployments/se-proxy/opteesp/lse.S
new file mode 100644
index 0000000..840683a
--- /dev/null
+++ b/deployments/se-proxy/opteesp/lse.S
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ */
+
+.text
+.globl __aarch64_cas4_acq_rel
+.globl __aarch64_cas4_sync
+
+__aarch64_cas4_acq_rel:
+       mov     w16, w0
+       ldaxr   w0, [x2]
+       cmp     w0, w16
+0:     bne     1f
+
+       stlxr   w17, w1, [x2]
+       cbnz    w17, 0b
+1:     ret
+
+__aarch64_cas4_sync:
+       mov     w16, w0
+       ldxr    w0, [x2]
+       cmp     w0, w16
+0:     bne     1f
+
+       stlxr   w17, w1, [x2]
+       cbnz    w17, 0b
+1:     ret
diff --git a/external/openamp/libmetal.cmake b/external/openamp/libmetal.cmake
new file mode 100644
index 0000000..3a647e6
--- /dev/null
+++ b/external/openamp/libmetal.cmake
@@ -0,0 +1,81 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021 Linaro Limited
+# Copyright (c) 2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set (LIBMETAL_URL "https://github.com/OpenAMP/libmetal.git" CACHE STRING "libmetal repository URL")
+set (LIBMETAL_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/libmetal_install" CACHE PATH "libmetal installation directory")
+set (LIBMETAL_PACKAGE_PATH "${LIBMETAL_INSTALL_PATH}/libmetal/cmake" CACHE PATH "libmetal CMake package directory")
+set (LIBMETAL_TARGET_NAME "libmetal")
+set (LIBMETAL_VERSION "f252f0e007fbfb8b3a52b1d5901250ddac96baad"  CACHE STRING "The version of libmetal to use")
+
+if(NOT LIBMETAL_DEBUG)
+	set(LIBMETAL_BUILD_TYPE "Release")
+else()
+	set(LIBMETAL_BUILD_TYPE "Debug")
+endif()
+
+include(FetchContent)
+
+# Checking git
+find_program(GIT_COMMAND "git")
+if (NOT GIT_COMMAND)
+	message(FATAL_ERROR "Please install git")
+endif()
+
+FetchContent_Declare(
+	libmetal
+	GIT_REPOSITORY ${LIBMETAL_URL}
+	GIT_TAG ${LIBMETAL_VERSION}
+)
+
+# FetchContent_GetProperties exports libmetal_SOURCE_DIR and libmetal_BINARY_DIR variables
+FetchContent_GetProperties(libmetal)
+if(NOT libmetal_POPULATED)
+	message(STATUS "Fetching libmetal")
+	FetchContent_Populate(libmetal)
+endif()
+
+# Ensure list of include paths is separated correctly
+string(REPLACE ";" "\\;" LIBMETAL_EXTERNAL_INCLUDE_PATHS "${LIBMETAL_EXTERNAL_INCLUDE_PATHS}")
+
+#Configure the library
+execute_process(COMMAND
+	${CMAKE_COMMAND}
+			-DCMAKE_BUILD_TYPE=${LIBMETAL_BUILD_TYPE}
+			-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
+			-DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}
+			-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
+			-DCMAKE_INSTALL_PREFIX=${LIBMETAL_INSTALL_PATH}
+			-DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
+			-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
+			-DCMAKE_C_OUTPUT_EXTENSION=.o
+			-DCMAKE_C_COMPILER_WORKS=true
+			-DCMAKE_SYSTEM_PROCESSOR=arm
+			-DWITH_DOC=off
+			-DWITH_TESTS=off
+			-DWITH_EXAMPLES=off
+			-DWITH_DEFAULT_LOGGER=off
+			-DEXTERNAL_INCLUDE_PATHS=${LIBMETAL_EXTERNAL_INCLUDE_PATHS}
+			-DMACHINE=template
+			${libmetal_SOURCE_DIR}
+	WORKING_DIRECTORY
+			${libmetal_BINARY_DIR}
+)
+
+# Build the library
+execute_process(COMMAND
+    ${CMAKE_COMMAND} --build ${libmetal_BINARY_DIR} -- install
+		RESULT_VARIABLE _exec_error
+	)
+if (_exec_error)
+	message(FATAL_ERROR "Build step of libmetal failed with ${_exec_error}.")
+endif()
+
+#Create an imported target to have clean abstraction in the build-system.
+add_library(libmetal STATIC IMPORTED)
+set_property(TARGET libmetal PROPERTY IMPORTED_LOCATION "${LIBMETAL_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}metal${CMAKE_STATIC_LIBRARY_SUFFIX}")
+set_property(TARGET libmetal PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${LIBMETAL_INSTALL_PATH}/include")
diff --git a/external/openamp/openamp.cmake b/external/openamp/openamp.cmake
new file mode 100644
index 0000000..aae13ba
--- /dev/null
+++ b/external/openamp/openamp.cmake
@@ -0,0 +1,82 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021 Linaro Limited
+# Copyright (c) 2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set (OPENAMP_URL "https://github.com/OpenAMP/open-amp.git" CACHE STRING "OpenAMP repository URL")
+set (OPENAMP_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/openamp_install" CACHE PATH "OpenAMP installation directory")
+set (OPENAMP_PACKAGE_PATH "${OPENAMP_INSTALL_PATH}/openamp/cmake" CACHE PATH "OpenAMP CMake package directory")
+set (OPENAMP_TARGET_NAME "openamp")
+set (OPENAMP_VERSION "347397decaa43372fc4d00f965640ebde042966d"  CACHE STRING "The version of openamp to use")
+
+
+if(NOT OPENAMP_DEBUG)
+	set(OPENAMP_BUILD_TYPE "Release")
+else()
+	set(OPENAMP_BUILD_TYPE "Debug")
+endif()
+
+include(FetchContent)
+
+# Checking git
+find_program(GIT_COMMAND "git")
+if (NOT GIT_COMMAND)
+	message(FATAL_ERROR "Please install git")
+endif()
+
+FetchContent_Declare(
+	openamp
+	GIT_REPOSITORY ${OPENAMP_URL}
+	GIT_TAG ${OPENAMP_VERSION}
+)
+
+# FetchContent_GetProperties exports openamp_SOURCE_DIR and openamp_BINARY_DIR variables
+FetchContent_GetProperties(openamp)
+if(NOT openamp_POPULATED)
+	message(STATUS "Fetching openamp")
+	FetchContent_Populate(openamp)
+endif()
+
+# Ensure list of include paths is separated correctly
+get_target_property(_libmetal_inc libmetal INTERFACE_INCLUDE_DIRECTORIES)
+set (_openam_external_include_paths ${_libmetal_inc} ${OPENAMP_EXTERNAL_INCLUDE_PATHS})
+string(REPLACE ";" "\\;" OPENAMP_EXTERNAL_INCLUDE_PATHS "${_openam_external_include_paths}")
+
+#Configure the library
+execute_process(COMMAND
+	${CMAKE_COMMAND}
+			-DCMAKE_BUILD_TYPE=${OPENAMP_BUILD_TYPE}
+			-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
+			-DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}
+			-DCMAKE_TOOLCHAIN_FILE=${TS_EXTERNAL_LIB_TOOLCHAIN_FILE}
+			-DCMAKE_INSTALL_PREFIX=${OPENAMP_INSTALL_PATH}
+			-DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
+			-DLIBMETAL_INCLUDE_DIR=${CMAKE_CURRENT_BINARY_DIR}/libmetal/lib/include
+			-DLIBMETAL_LIB=${CMAKE_CURRENT_BINARY_DIR}/libmetal/lib
+			-DCMAKE_C_OUTPUT_EXTENSION=.o
+			-DCMAKE_C_COMPILER_WORKS=true
+			-DCMAKE_SYSTEM_PROCESSOR=arm
+			-DEXTERNAL_INCLUDE_PATHS=${OPENAMP_EXTERNAL_INCLUDE_PATHS}
+			-DMACHINE=template
+			-DRPMSG_BUFFER_SIZE=512
+			${openamp_SOURCE_DIR}
+	WORKING_DIRECTORY
+			${openamp_BINARY_DIR}
+)
+
+# Build the library
+execute_process(COMMAND
+    ${CMAKE_COMMAND} --build ${openamp_BINARY_DIR} -- install
+		RESULT_VARIABLE _exec_error
+	)
+if (_exec_error)
+	message(FATAL_ERROR "Build step of OpenAMP failed with ${_exec_error}.")
+endif()
+
+#Create an imported target to have clean abstraction in the build-system.
+add_library(openamp STATIC IMPORTED)
+set_property(TARGET openamp PROPERTY IMPORTED_LOCATION "${OPENAMP_INSTALL_PATH}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}open_amp${CMAKE_STATIC_LIBRARY_SUFFIX}")
+set_property(TARGET openamp PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${OPENAMP_INSTALL_PATH}/include")
-- 
2.25.1

