SPM: Use global variables as ns agent stack

Use global arrays as stack of the ns agent partition instead
of the ER_INITIAL_PSP. The stack size related settings are
added into the 'config_impl.h' template file.

Change-Id: If8443c6fe02d3f1547c637bd51f0a7352aead0c1
Signed-off-by: Mingyang Sun <mingyang.sun@arm.com>
diff --git a/interface/include/config_impl.h.template b/interface/include/config_impl.h.template
index 96ea631..5af682a 100644
--- a/interface/include/config_impl.h.template
+++ b/interface/include/config_impl.h.template
@@ -11,17 +11,26 @@
 #define __CONFIG_IMPL_H__
 
 {% if ipc_partition_num > 0 and sfn_partition_num == 0 %}
+/* IPC model */
 #define {{"%-56s"|format("CONFIG_TFM_SPM_BACKEND_IPC")}} 1
 
+/* Trustzone NS agent working stack size. */
+#define {{"%-56s"|format("CONFIG_TFM_NS_AGENT_TZ_STACK_SIZE")}} 1024
+
 #if TFM_LVL > 1
 #define {{"%-56s"|format("CONFIG_TFM_PSA_API_SUPERVISOR_CALL")}} 1
-#else
+#else /* TFM_LVL > 1 */
 #define {{"%-56s"|format("CONFIG_TFM_PSA_API_THREAD_CALL")}} 1
-#define {{"%-56s"|format("CONFIG_TFM_SPM_THREAD_STACK_SIZE")}} 1024
-#endif
+
+/* SPM re-uses Trustzone NS agent stack. */
+#define {{"%-56s"|format("CONFIG_TFM_SPM_THREAD_STACK_SIZE")}}     \
+            {{"%-56s"|format("CONFIG_TFM_NS_AGENT_TZ_STACK_SIZE")}}
+#endif /* TFM_LVL > 1 */
 
 {% elif sfn_partition_num > 0 and ipc_partition_num == 0 %}
+/* SFN model */
 #define {{"%-56s"|format("CONFIG_TFM_SPM_BACKEND_SFN")}} 1
+#define {{"%-56s"|format("CONFIG_TFM_PSA_API_SFN_CALL")}} 1
 
 #if TFM_LVL > 1
 #error "High isolation level SFN model is not supported."
@@ -35,7 +44,17 @@
             {% set total_stk.size = total_stk.size + partition.manifest.stack_size|int(base=10) %}
         {% endif %}
     {% endfor %}
-#define {{"%-36s"|format("SUM_OF_PARTITION_STACK_SIZES")}} ((({{"0x%x"|format(total_stk.size)}} >> 1) + 0x7) & (~0x7))
+/*
+ * In isolation level 1 SFN model, all subsequent components work on NS agent
+ * stack. It is observed that half of the sum of all partition stack sizes is
+ * enough for working. Define a divisor factor
+ * CONFIG_TFM_NS_AGENT_TZ_STK_SIZE_SHIFT_FACTOR for reference, and allow
+ * modification of the factor based on application situation. The stack size
+ * value is aligned to 8 bytes.
+ */
+#define CONFIG_TFM_NS_AGENT_TZ_STK_SIZE_SHIFT_FACTOR             1
+#define {{"%-56s"|format("CONFIG_TFM_NS_AGENT_TZ_STACK_SIZE")}} \
+    ((({{"0x%x"|format(total_stk.size)}} >> CONFIG_TFM_NS_AGENT_TZ_STK_SIZE_SHIFT_FACTOR) + 0x7) & (~0x7))
 
 {% elif sfn_partition_num > 0 and ipc_partition_num > 0 %}
 #error "IPC and SFN co-work not supported yet."