Build: Improve the setting of number of mailbox queue slots

Move configuration of number of mailbox queue slots from platform's
device_cfg.h into a common header file tfm_mailbox_config.h.

tfm_mailbox_config.h is automatically generated during build. The
value of NUM_MAILBOX_QUEUE_SLOT is passed from build configuration
and then set in tfm_mailbox_config.h.

Change-Id: I9a3ac465b71b316accf6cd41cea80745eec8607c
Signed-off-by: David Hu <david.hu@arm.com>
diff --git a/cmake/install.cmake b/cmake/install.cmake
index 981bfba..106369e 100644
--- a/cmake/install.cmake
+++ b/cmake/install.cmake
@@ -56,6 +56,7 @@
     install(FILES       ${INTERFACE_INC_DIR}/multi_core/tfm_multi_core_api.h
                         ${INTERFACE_INC_DIR}/multi_core/tfm_ns_mailbox.h
                         ${INTERFACE_INC_DIR}/multi_core/tfm_mailbox.h
+                        ${INTERFACE_INC_DIR}/multi_core/tfm_mailbox_config.h
             DESTINATION ${INSTALL_INTERFACE_INC_DIR})
 else()
     install(FILES       ${CMAKE_BINARY_DIR}/generated/interface/include/tfm_veneers.h
diff --git a/config/check_config.cmake b/config/check_config.cmake
index 6fb8cdd..395f4a5 100644
--- a/config/check_config.cmake
+++ b/config/check_config.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2021, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -29,6 +29,7 @@
 
 tfm_invalid_config(TFM_MULTI_CORE_TOPOLOGY AND NOT TFM_PSA_API)
 tfm_invalid_config(TFM_MULTI_CORE_MULTI_CLIENT_CALL AND NOT TFM_MULTI_CORE_TOPOLOGY)
+tfm_invalid_config(NUM_MAILBOX_QUEUE_SLOT GREATER 1 AND NOT TFM_MULTI_CORE_MULTI_CLIENT_CALL)
 
 tfm_invalid_config(TEST_S  AND TEST_PSA_API)
 tfm_invalid_config(TEST_NS AND TEST_PSA_API)
diff --git a/config/config_default.cmake b/config/config_default.cmake
index ef002db..4438def 100644
--- a/config/config_default.cmake
+++ b/config/config_default.cmake
@@ -71,6 +71,7 @@
 
 set(TFM_MULTI_CORE_TOPOLOGY             OFF         CACHE BOOL      "Whether to build for a dual-cpu architecture")
 set(TFM_MULTI_CORE_MULTI_CLIENT_CALL    OFF         CACHE BOOL      "Whether to enable multiple PSA client calls feature")
+set(NUM_MAILBOX_QUEUE_SLOT              1           CACHE BOOL      "Number of mailbox queue slots")
 
 set(DEBUG_AUTHENTICATION                CHIP_DEFAULT CACHE STRING   "Debug authentication setting. [CHIP_DEFAULT, NONE, NS_ONLY, FULL")
 set(SECURE_UART1                        OFF         CACHE BOOL      "Enable secure UART1")
diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt
index 0351754..188e651 100644
--- a/interface/CMakeLists.txt
+++ b/interface/CMakeLists.txt
@@ -61,3 +61,12 @@
         $<$<BOOL:${TFM_PSA_API}>:${CMAKE_CURRENT_SOURCE_DIR}/src/psa/psa_lifecycle.c>
         ${CMAKE_CURRENT_SOURCE_DIR}/src/log/tfm_log_raw.c
 )
+
+###################### Export configurations to NS #############################
+
+if (TFM_MULTI_CORE_TOPOLOGY OR FORWARD_PROT_MSG)
+    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/multi_core/tfm_mailbox_config.h.in
+                   ${CMAKE_CURRENT_SOURCE_DIR}/include/multi_core/tfm_mailbox_config.h
+                   NEWLINE_STYLE UNIX
+    )
+endif()
diff --git a/interface/include/multi_core/tfm_mailbox.h b/interface/include/multi_core/tfm_mailbox.h
index 45eb97e..5824543 100644
--- a/interface/include/multi_core/tfm_mailbox.h
+++ b/interface/include/multi_core/tfm_mailbox.h
@@ -18,45 +18,14 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stddef.h>
-#ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
-#include "device_cfg.h"
-#endif
+
 #include "psa/client.h"
+#include "tfm_mailbox_config.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-/*
- * If multiple outstanding NS PSA Client calls is enabled, multi-core platform
- * should define the number of mailbox queue slots NUM_MAILBOX_QUEUE_SLOT in
- * platform device_cfg.h.
- * Otherwise, NUM_MAILBOX_QUEUE_SLOT is defined as 1.
- */
-#ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
-#ifndef NUM_MAILBOX_QUEUE_SLOT
-#error "Error: Platform doesn't define NUM_MAILBOX_QUEUE_SLOT for mailbox queue"
-#endif
-
-#if (NUM_MAILBOX_QUEUE_SLOT < 2)
-#error "Error: Invalid NUM_MAILBOX_QUEUE_SLOT. The value should be more than 1"
-#endif
-
-/*
- * The number of slots should be no more than the number of bits in
- * mailbox_queue_status_t.
- * Here the value is hardcoded. A better way is to define a sizeof() to
- * calculate the bits in mailbox_queue_status_t and dump it with pragma message.
- */
-#if (NUM_MAILBOX_QUEUE_SLOT > 32)
-#error "Error: Invalid NUM_MAILBOX_QUEUE_SLOT. The value should be no more than 32"
-#endif
-#else /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */
-/* Force the number of mailbox queue slots as 1. */
-#undef NUM_MAILBOX_QUEUE_SLOT
-#define NUM_MAILBOX_QUEUE_SLOT              (1)
-#endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */
-
 /* PSA client call type value */
 #define MAILBOX_PSA_FRAMEWORK_VERSION       (0x1)
 #define MAILBOX_PSA_VERSION                 (0x2)
diff --git a/interface/include/multi_core/tfm_mailbox_config.h.in b/interface/include/multi_core/tfm_mailbox_config.h.in
new file mode 100644
index 0000000..bd85ef6
--- /dev/null
+++ b/interface/include/multi_core/tfm_mailbox_config.h.in
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef _TFM_MAILBOX_CONFIG_
+#define _TFM_MAILBOX_CONFIG_
+
+/*
+ * \note Don't modify this file. Change the build configuration value
+ *       and re-build TF-M SPE side to update the value.
+ */
+
+/* Get number of mailbox queue slots from build configuration */
+#cmakedefine NUM_MAILBOX_QUEUE_SLOT @NUM_MAILBOX_QUEUE_SLOT@
+
+#ifndef NUM_MAILBOX_QUEUE_SLOT
+#define NUM_MAILBOX_QUEUE_SLOT              1
+#endif
+
+#if (NUM_MAILBOX_QUEUE_SLOT < 1)
+#error "Error: Invalid NUM_MAILBOX_QUEUE_SLOT. The value should be >= 1"
+#endif
+
+/*
+ * The number of slots should be no more than the number of bits in
+ * mailbox_queue_status_t.
+ * Here the value is hardcoded. A better way is to define a sizeof() to
+ * calculate the bits in mailbox_queue_status_t and dump it with pragma message.
+ */
+#if (NUM_MAILBOX_QUEUE_SLOT > 32)
+#error "Error: Invalid NUM_MAILBOX_QUEUE_SLOT. The value should be <= 32"
+#endif
+
+#endif /* _TFM_MAILBOX_CONFIG_ */
diff --git a/platform/ext/target/cypress/psoc64/Device/Config/device_cfg.h b/platform/ext/target/cypress/psoc64/Device/Config/device_cfg.h
index 021b124..eeebdc0 100644
--- a/platform/ext/target/cypress/psoc64/Device/Config/device_cfg.h
+++ b/platform/ext/target/cypress/psoc64/Device/Config/device_cfg.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2018 Arm Limited
+ * Copyright (c) 2017-2021 Arm Limited
  * Copyright (c) 2020, Cypress Semiconductor Corporation. All rights reserved.
  *
  * Licensed under the Apache License Version 2.0 (the "License");
@@ -34,8 +34,4 @@
 
 #define DEFAULT_UART_BAUDRATE  115200
 
-#ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
-#define NUM_MAILBOX_QUEUE_SLOT      4
-#endif
-
 #endif  /* __ARM_LTD_DEVICE_CFG_H__ */
diff --git a/platform/ext/target/cypress/psoc64/config.cmake b/platform/ext/target/cypress/psoc64/config.cmake
index d4411a8..24bc3c0 100644
--- a/platform/ext/target/cypress/psoc64/config.cmake
+++ b/platform/ext/target/cypress/psoc64/config.cmake
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2020-2021, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -15,6 +15,7 @@
 
 set(TFM_MULTI_CORE_TOPOLOGY             ON          CACHE BOOL      "Whether to build for a dual-cpu architecture")
 set(TFM_MULTI_CORE_MULTI_CLIENT_CALL    ON          CACHE BOOL      "Whether to enable multiple PSA client calls feature")
+set(NUM_MAILBOX_QUEUE_SLOT              4           CACHE BOOL      "Number of mailbox queue slots")
 
 set(PLATFORM_DUMMY_ATTEST_HAL           FALSE       CACHE BOOL      "Use dummy attest hal implementation. Should not be used in production.")
 set(PLATFORM_DUMMY_NV_COUNTERS          FALSE       CACHE BOOL      "Use dummy nv counter implementation. Should not be used in production.")