Separate the generic part of SP deployments

Move the non opteesp environment dependent code of SP deployments into
a separate directory and split CMake files into a generic and an
environment specific part.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I4f09d6d3adef07644e98f2a05d6cb077a92b385b
diff --git a/deployments/smm-gateway/common/smm_gateway.c b/deployments/smm-gateway/common/smm_gateway.c
new file mode 100644
index 0000000..4884a04
--- /dev/null
+++ b/deployments/smm-gateway/common/smm_gateway.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <stddef.h>
+#include <protocols/rpc/common/packed-c/encoding.h>
+#include <service/smm_variable/provider/smm_variable_provider.h>
+#include <service/secure_storage/backend/secure_storage_client/secure_storage_client.h>
+#include <service/secure_storage/backend/mock_store/mock_store.h>
+#include <service_locator.h>
+
+/* Build-time default configuration */
+
+/* Default to using the Protected Storage SP */
+#ifndef SMM_GATEWAY_NV_STORE_SN
+#define SMM_GATEWAY_NV_STORE_SN		"sn:ffa:751bf801-3dde-4768-a514-0f10aeed1790:0"
+#endif
+
+/* Default maximum number of UEFI variables */
+#ifndef SMM_GATEWAY_MAX_UEFI_VARIABLES
+#define SMM_GATEWAY_MAX_UEFI_VARIABLES		(40)
+#endif
+
+/* The smm_gateway instance - it's a singleton */
+static struct smm_gateway
+{
+	struct smm_variable_provider smm_variable_provider;
+	struct secure_storage_client nv_store_client;
+	struct mock_store volatile_store;
+	struct service_context *nv_storage_service_context;
+	rpc_session_handle nv_storage_session_handle;
+
+} smm_gateway_instance;
+
+
+static struct rpc_caller *locate_nv_store(void)
+{
+	int status = 0;
+	struct rpc_caller *caller = NULL;
+
+	/* todo - add option to use configurable service location */
+	smm_gateway_instance.nv_storage_service_context =
+		service_locator_query(SMM_GATEWAY_NV_STORE_SN, &status);
+
+	if (smm_gateway_instance.nv_storage_service_context) {
+
+		smm_gateway_instance.nv_storage_session_handle = service_context_open(
+			smm_gateway_instance.nv_storage_service_context,
+			TS_RPC_ENCODING_PACKED_C,
+			&caller);
+	}
+
+	return caller;
+}
+
+struct rpc_interface *smm_gateway_create(uint32_t owner_id)
+{
+	service_locator_init();
+
+	/* Initialize a storage client to access the remote NV store */
+	struct rpc_caller *nv_store_caller = locate_nv_store();
+	struct storage_backend *persistent_backend = secure_storage_client_init(
+		&smm_gateway_instance.nv_store_client,
+		nv_store_caller);
+
+	/* Initialize the volatile storage backend */
+	struct storage_backend *volatile_backend  = mock_store_init(
+		&smm_gateway_instance.volatile_store);
+
+	/* Initialize the smm_variable service provider */
+	struct rpc_interface *service_iface = smm_variable_provider_init(
+		&smm_gateway_instance.smm_variable_provider,
+ 		owner_id,
+		SMM_GATEWAY_MAX_UEFI_VARIABLES,
+		persistent_backend,
+		volatile_backend);
+
+	return service_iface;
+}
diff --git a/deployments/smm-gateway/common/smm_gateway.h b/deployments/smm-gateway/common/smm_gateway.h
new file mode 100644
index 0000000..71cadaa
--- /dev/null
+++ b/deployments/smm-gateway/common/smm_gateway.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef SMM_GATEWAY_H
+#define SMM_GATEWAY_H
+
+#include <rpc/common/endpoint/rpc_interface.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Factory method for constructing an smm_gateway
+ *
+ * A common factory method for constructing an smm_gateway. The gateway
+ * presents a set of UEFI SMM service interfaces via the returned
+ * rpc_interface. An environment specific initializer calls this function
+ * to construct the environment independent smm_gateway provider.
+ *
+ * @param[in] owner_id The id of the owning security domain (e.g. partition id)
+ *
+ * \return An rpc_interface or NULL on failure
+ */
+struct rpc_interface *smm_gateway_create(
+ 	uint32_t owner_id);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* SMM_GATEWAY_H */
diff --git a/deployments/smm-gateway/common/smm_gateway_sp.c b/deployments/smm-gateway/common/smm_gateway_sp.c
new file mode 100644
index 0000000..2187fea
--- /dev/null
+++ b/deployments/smm-gateway/common/smm_gateway_sp.c
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include <rpc/ffarpc/endpoint/ffarpc_call_ep.h>
+#include "smm_gateway.h"
+#include <config/ramstore/config_ramstore.h>
+#include "config/interface/config_store.h"
+#include <config/loader/sp/sp_config_loader.h>
+#include "components/rpc/mm_communicate/endpoint/sp/mm_communicate_call_ep.h"
+#include "components/service/smm_variable/frontend/mm_communicate/smm_variable_mm_service.h"
+#include "platform/interface/memory_region.h"
+#include <ffa_api.h>
+#include <sp_api.h>
+#include <sp_messaging.h>
+#include <sp_rxtx.h>
+#include <trace.h>
+
+#define CONFIG_NAME_MM_COMM_BUFFER_REGION	"mm-comm-buffer"
+
+uint16_t own_id = 0; /* !!Needs refactoring as parameter to ffarpc_caller_init */
+
+static int sp_init(uint16_t *own_sp_id);
+
+void __noreturn sp_main(struct ffa_init_info *init_info)
+{
+	struct memory_region mm_comm_buffer_region = { 0 };
+	struct rpc_interface *gateway_iface = NULL;
+	struct smm_variable_mm_service smm_var_service = { 0 };
+	struct mm_service_interface *smm_var_service_interface = NULL;
+	struct mm_communicate_ep mm_communicate_call_ep = { 0 };
+	struct ffa_direct_msg req_msg = { 0 };
+	struct ffa_direct_msg resp_msg = { 0 };
+
+	static const EFI_GUID smm_variable_guid = SMM_VARIABLE_GUID;
+
+	/* Boot phase */
+	if (sp_init(&own_id) != 0) goto fatal_error;
+
+	/* Load any dynamic configuration */
+	config_ramstore_init();
+	sp_config_load(init_info);
+
+	if (!config_store_query(CONFIG_CLASSIFIER_MEMORY_REGION, CONFIG_NAME_MM_COMM_BUFFER_REGION,
+				0, &mm_comm_buffer_region, sizeof(mm_comm_buffer_region))) {
+		EMSG(CONFIG_NAME_MM_COMM_BUFFER_REGION " is not set in SP configuration");
+		goto fatal_error;
+	}
+
+	/* Initialize service layer and associate with RPC endpoint */
+	gateway_iface = smm_gateway_create(own_id);
+
+	/* Initialize SMM variable MM service */
+	smm_var_service_interface = smm_variable_mm_service_init(&smm_var_service, gateway_iface);
+
+	/* Initialize MM communication layer */
+	if (!mm_communicate_call_ep_init(&mm_communicate_call_ep,
+					 (void *)mm_comm_buffer_region.base_addr,
+					 mm_comm_buffer_region.region_size))
+		goto fatal_error;
+
+	/* Attach SMM variable service to MM communication layer */
+	mm_communicate_call_ep_attach_service(&mm_communicate_call_ep, &smm_variable_guid,
+					      smm_var_service_interface);
+
+	/* End of boot phase */
+	ffa_msg_wait(&req_msg);
+
+	while (1) {
+		mm_communicate_call_ep_receive(&mm_communicate_call_ep, &req_msg, &resp_msg);
+
+		ffa_msg_send_direct_resp(req_msg.destination_id,
+					 req_msg.source_id, resp_msg.args[0],
+					 resp_msg.args[1], resp_msg.args[2],
+					 resp_msg.args[3], resp_msg.args[4],
+					 &req_msg);
+	}
+
+fatal_error:
+	/* SP is not viable */
+	EMSG("SMM gateway SP error");
+	while (1) {}
+}
+
+void sp_interrupt_handler(uint32_t interrupt_id)
+{
+	(void)interrupt_id;
+}
+
+static int sp_init(uint16_t *own_sp_id)
+{
+	int status = -1;
+	ffa_result ffa_res;
+	sp_result sp_res;
+	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) {
+		ffa_res = ffa_id_get(own_sp_id);
+		if (ffa_res == FFA_OK) {
+			status = 0;
+		}
+	}
+
+	return status;
+}
diff --git a/deployments/smm-gateway/common/smm_gateway_sp.h b/deployments/smm-gateway/common/smm_gateway_sp.h
new file mode 100644
index 0000000..13317e7
--- /dev/null
+++ b/deployments/smm-gateway/common/smm_gateway_sp.h
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef SMM_GATEWAY_SP_H
+#define SMM_GATEWAY_SP_H
+
+#define SMM_GATEWAY_UUID_BYTES \
+	{0xed, 0x32, 0xd5, 0x33, 0x99, 0xe6, 0x42, 0x09, \
+	 0x9c, 0xc0, 0x2d, 0x72, 0xcd, 0xd9, 0x98, 0xa7}
+
+#endif /* SMM_GATEWAY_SP_H */