SPM: Improve SPM_THREAD_CONTEXT logic

When ns_agent_tz is used, SPM shares a stack with it. When
ns_agent_mailbox is used, SPM needs its own dedicated stack.
The correct logic, then, is to create the dedicated SPM stack only if
CONFIG_TFM_USE_TRUSTZONE is not set, and to only set SPM_THREAD_CONTEXT
to the ns_agent_tz context rather than that of either ns_agent.

Change-Id: Ibac9283f7e5cf7b746804754f6e1aaf8199d9f6c
Signed-off-by: Chris Brand <chris.brand@cypress.com>
diff --git a/secure_fw/spm/ffm/backend_ipc.c b/secure_fw/spm/ffm/backend_ipc.c
index a284166..9281283 100644
--- a/secure_fw/spm/ffm/backend_ipc.c
+++ b/secure_fw/spm/ffm/backend_ipc.c
@@ -33,16 +33,16 @@
 #if CONFIG_TFM_PSA_API_CROSS_CALL == 1
 /* Instance for SPM_THREAD_CONTEXT */
 
-#ifdef TFM_MULTI_CORE_TOPOLOGY
-
+#ifdef CONFIG_TFM_USE_TRUSTZONE
+struct context_ctrl_t *p_spm_thread_context;
+#else
+/* If ns_agent_tz isn't used, we need to provide a stack for SPM to use */
 static uint8_t spm_thread_stack[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8);
 ARCH_CLAIM_CTXCTRL_INSTANCE(spm_thread_context,
                             spm_thread_stack,
                             sizeof(spm_thread_stack));
 
 struct context_ctrl_t *p_spm_thread_context = &spm_thread_context;
-#else
-struct context_ctrl_t *p_spm_thread_context;
 #endif
 
 #endif
@@ -183,9 +183,12 @@
     THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
               TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
 
-#if (CONFIG_TFM_PSA_API_CROSS_CALL == 1) && !defined(TFM_MULTI_CORE_TOPOLOGY)
+#if (CONFIG_TFM_PSA_API_CROSS_CALL == 1) && defined(CONFIG_TFM_USE_TRUSTZONE)
     if (IS_PARTITION_NS_AGENT(p_pldi)) {
-        SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl;
+        /* Get the context from ns_agent_tz */
+        if (p_pldi->pid == 0) {
+            SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl;
+        }
     }
 #endif