Set RPC caller session SHM size for Corstone1000 SMMGW

Set RPC caller session shared memory size so it fits the UEFI variable
index. Validate if SMM_GATEWAY_MAX_UEFI_VARIABLES * [descriptor size]
would fit into the shared memory size. Also validate the heap size
accordingly.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: Ib593a9ed9dad5c97fbca9d482fb5fbc38e5e25a2
diff --git a/components/service/locator/sp/ffa/spffa_service_context.c b/components/service/locator/sp/ffa/spffa_service_context.c
index 4ddc53a..f7730d0 100644
--- a/components/service/locator/sp/ffa/spffa_service_context.c
+++ b/components/service/locator/sp/ffa/spffa_service_context.c
@@ -10,10 +10,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-#ifndef RPC_CALLER_SESSION_SHARED_MEMORY_SIZE
-#define RPC_CALLER_SESSION_SHARED_MEMORY_SIZE	(4096)
-#endif /* RPC_CALLER_SESSION_SHARED_MEMORY_SIZE */
-
 /* Concrete service_context methods */
 static struct rpc_caller_session *sp_ts_service_context_open(void *context);
 static void sp_ts_service_context_close(void *context, struct rpc_caller_session *session);
diff --git a/components/service/locator/sp/ffa/spffa_service_context.h b/components/service/locator/sp/ffa/spffa_service_context.h
index ff9b2f2..e9ed9c6 100644
--- a/components/service/locator/sp/ffa/spffa_service_context.h
+++ b/components/service/locator/sp/ffa/spffa_service_context.h
@@ -13,6 +13,10 @@
 extern "C" {
 #endif
 
+#ifndef RPC_CALLER_SESSION_SHARED_MEMORY_SIZE
+#define RPC_CALLER_SESSION_SHARED_MEMORY_SIZE	(4096)
+#endif /* RPC_CALLER_SESSION_SHARED_MEMORY_SIZE */
+
 /*
  * A service_context that represents a service instance located in
  * a partition, accessed via FFA.  This service_context is suitable
diff --git a/components/service/uefi/smm_variable/backend/uefi_variable_store.h b/components/service/uefi/smm_variable/backend/uefi_variable_store.h
index dc3d3ca..8be5f36 100644
--- a/components/service/uefi/smm_variable/backend/uefi_variable_store.h
+++ b/components/service/uefi/smm_variable/backend/uefi_variable_store.h
@@ -22,6 +22,11 @@
 #endif
 
 /**
+ * \brief Size of the variable store index for a given entry count
+ */
+#define UEFI_VARIABLE_STORE_INDEX_SIZE(count) (sizeof(struct variable_metadata) * (count))
+
+/**
  * \brief delegate_variable_store structure definition
  *
  * A delegate_variable_store combines an association with a concrete
diff --git a/deployments/smm-gateway/common/smm_gateway.c b/deployments/smm-gateway/common/smm_gateway.c
index f7279fa..eaa8613 100644
--- a/deployments/smm-gateway/common/smm_gateway.c
+++ b/deployments/smm-gateway/common/smm_gateway.c
@@ -11,6 +11,7 @@
 #include "psa/crypto.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/sp/ffa/spffa_service_context.h>
 #include <service_locator.h>
 
 /* Build-time default configuration */
@@ -32,6 +33,26 @@
 #define SMM_GATEWAY_MAX_UEFI_VARIABLES		(40)
 #endif
 
+/**
+ * The UEFI variable store index must fit into the RPC shared memory, otherwise
+ * load_variable_index/sync_variable_index will fail.
+ */
+#define SMM_UEFI_VARIABLE_STORE_INDEX_SIZE \
+	UEFI_VARIABLE_STORE_INDEX_SIZE(SMM_GATEWAY_MAX_UEFI_VARIABLES)
+
+_Static_assert(SMM_UEFI_VARIABLE_STORE_INDEX_SIZE < RPC_CALLER_SESSION_SHARED_MEMORY_SIZE,
+	       "The UEFI variable index does not fit into the RPC shared memory, please increase " \
+	       "RPC_CALLER_SESSION_SHARED_MEMORY_SIZE");
+
+/**
+ * The SP heap must be large enough for storing the UEFI variable index, the RPC shared memory and
+ * ~16kB of miscellaneous data.
+ */
+#define SMM_MIN_HEAP_SIZE \
+	SMM_UEFI_VARIABLE_STORE_INDEX_SIZE + RPC_CALLER_SESSION_SHARED_MEMORY_SIZE + 16 * 1024
+
+_Static_assert(SP_HEAP_SIZE > SMM_MIN_HEAP_SIZE, "Please increase SP_HEAP_SIZE");
+
 /* The smm_gateway instance - it's a singleton */
 static struct smm_gateway
 {
diff --git a/deployments/smm-gateway/config/default-opteesp/CMakeLists.txt b/deployments/smm-gateway/config/default-opteesp/CMakeLists.txt
index 3f844e6..7c9328d 100644
--- a/deployments/smm-gateway/config/default-opteesp/CMakeLists.txt
+++ b/deployments/smm-gateway/config/default-opteesp/CMakeLists.txt
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2021-2024, Arm Limited and Contributors. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -34,11 +34,26 @@
 set(SP_BIN_UUID_CANON "ed32d533-99e6-4209-9cc0-2d72cdd998a7")
 set(SP_FFA_UUID_CANON "${SP_BIN_UUID_CANON}")
 
+#-------------------------------------------------------------------------------
+#  Set target platform to provide drivers needed by the deployment
+#
+#-------------------------------------------------------------------------------
+add_platform(TARGET "smm-gateway")
+
+# SMM variable and RPC caller settings
+set(SMM_GATEWAY_MAX_UEFI_VARIABLES 40 CACHE STRING "Maximum UEFI variable count")
+set(SMM_RPC_CALLER_SESSION_SHARED_MEMORY_SIZE 2*4096 CACHE STRING "RPC caller buffer size in SMMGW")
 if (UEFI_AUTH_VAR)
-set(SP_HEAP_SIZE "64 * 1024" CACHE STRING "SP heap size in bytes")
+set(SMM_SP_HEAP_SIZE 64*1024 CACHE STRING "SMM gateway SP heap size")
 else()
-set(SP_HEAP_SIZE "32 * 1024" CACHE STRING "SP heap size in bytes")
+set(SMM_SP_HEAP_SIZE 32*1024 CACHE STRING "SMM gateway SP heap size")
 endif()
+set(SP_HEAP_SIZE "${SMM_SP_HEAP_SIZE}" CACHE STRING "SP heap size in bytes")
+
+target_compile_definitions("smm-gateway" PRIVATE
+	RPC_CALLER_SESSION_SHARED_MEMORY_SIZE=${SMM_RPC_CALLER_SESSION_SHARED_MEMORY_SIZE}
+	SMM_GATEWAY_MAX_UEFI_VARIABLES=${SMM_GATEWAY_MAX_UEFI_VARIABLES}
+)
 
 set(TRACE_PREFIX "SMMGW" CACHE STRING "Trace prefix")
 
@@ -65,11 +80,6 @@
 include(../../infra/psa-varstore.cmake REQUIRED)
 include(../../smm-gateway.cmake REQUIRED)
 
-#-------------------------------------------------------------------------------
-#  Set target platform to provide drivers needed by the deployment
-#
-#-------------------------------------------------------------------------------
-add_platform(TARGET "smm-gateway")
 
 #-------------------------------------------------------------------------------
 #  Deployment specific build options
diff --git a/deployments/smm-gateway/config/default-sp/CMakeLists.txt b/deployments/smm-gateway/config/default-sp/CMakeLists.txt
index 6a3f890..be4692e 100644
--- a/deployments/smm-gateway/config/default-sp/CMakeLists.txt
+++ b/deployments/smm-gateway/config/default-sp/CMakeLists.txt
@@ -39,11 +39,26 @@
 set(TRACE_PREFIX "SMMGW" CACHE STRING "Trace prefix")
 set(SP_STACK_SIZE "64 * 1024" CACHE STRING "Stack size")
 
+#-------------------------------------------------------------------------------
+#  Set target platform to provide drivers needed by the deployment
+#
+#-------------------------------------------------------------------------------
+add_platform(TARGET "smm-gateway")
+
+# SMM variable and RPC caller settings
+set(SMM_GATEWAY_MAX_UEFI_VARIABLES 40 CACHE STRING "Maximum UEFI variable count")
+set(SMM_RPC_CALLER_SESSION_SHARED_MEMORY_SIZE 2*4096 CACHE STRING "RPC caller buffer size in SMMGW")
 if (UEFI_AUTH_VAR)
-set(SP_HEAP_SIZE "64 * 1024" CACHE STRING "Heap size")
+set(SMM_SP_HEAP_SIZE 64*1024 CACHE STRING "SMM gateway SP heap size")
 else()
-set(SP_HEAP_SIZE "32 * 1024" CACHE STRING "Heap size")
+set(SMM_SP_HEAP_SIZE 32*1024 CACHE STRING "SMM gateway SP heap size")
 endif()
+set(SP_HEAP_SIZE "${SMM_SP_HEAP_SIZE}" CACHE STRING "SP heap size in bytes")
+
+target_compile_definitions("smm-gateway" PRIVATE
+	RPC_CALLER_SESSION_SHARED_MEMORY_SIZE=${SMM_RPC_CALLER_SESSION_SHARED_MEMORY_SIZE}
+	SMM_GATEWAY_MAX_UEFI_VARIABLES=${SMM_GATEWAY_MAX_UEFI_VARIABLES}
+)
 
 # Setting the MM communication buffer parameters
 set(MM_COMM_BUFFER_ADDRESS "0x00000008 0x81000000" CACHE STRING "Address of MM communicte buffer in 64 bit DTS format")
@@ -64,12 +79,6 @@
 include(../../smm-gateway.cmake REQUIRED)
 
 #-------------------------------------------------------------------------------
-#  Set target platform to provide drivers needed by the deployment
-#
-#-------------------------------------------------------------------------------
-add_platform(TARGET "smm-gateway")
-
-#-------------------------------------------------------------------------------
 #  Deployment specific build options
 #-------------------------------------------------------------------------------
 target_compile_definitions(smm-gateway PRIVATE
diff --git a/platform/providers/arm/corstone1000/platform.cmake b/platform/providers/arm/corstone1000/platform.cmake
index 1dae83e..6632267 100644
--- a/platform/providers/arm/corstone1000/platform.cmake
+++ b/platform/providers/arm/corstone1000/platform.cmake
@@ -6,9 +6,12 @@
 # Platform definition for the 'fvp_base_revc-2xaem8a' virtual platform.
 #-------------------------------------------------------------------------------
 
+set(SMM_GATEWAY_MAX_UEFI_VARIABLES 80 CACHE STRING "Maximum UEFI variable count")
+set(SMM_RPC_CALLER_SESSION_SHARED_MEMORY_SIZE 4*4096 CACHE STRING "RPC caller buffer size in SMMGW")
+set(SMM_SP_HEAP_SIZE 48*1024 CACHE STRING "SMM gateway SP heap size")
+
 target_compile_definitions(${TGT} PRIVATE
 	SMM_VARIABLE_INDEX_STORAGE_UID=0x787
-	SMM_GATEWAY_MAX_UEFI_VARIABLES=80
 	PLAT_RSS_COMMS_PAYLOAD_MAX_SIZE=0x2080
 )