Build: Tweak the SFN/IPC backend setting

Since by default TF-M is built with level 1 isolation, include SFN
backend config by default.

Signed-off-by: David Hu <david.hu@arm.com>
Change-Id: I415fa9f1ecd5ca2087400152787e6a2ae1624c23
diff --git a/config/check_config.cmake b/config/check_config.cmake
index 541d7f2..05fccc4 100644
--- a/config/check_config.cmake
+++ b/config/check_config.cmake
@@ -29,6 +29,8 @@
 
 tfm_invalid_config(TFM_MULTI_CORE_TOPOLOGY AND TFM_NS_MANAGE_NSID)
 tfm_invalid_config(TFM_PLAT_SPECIFIC_MULTI_CORE_COMM AND NOT TFM_MULTI_CORE_TOPOLOGY)
+# Multi-core platform with mailbox partition cannot fully work with SFN backend yet.
+tfm_invalid_config(TFM_PARTITION_NS_AGENT_MAILBOX AND CONFIG_TFM_SPM_BACKEND_SFN)
 
 tfm_invalid_config(TFM_ISOLATION_LEVEL EQUAL 3 AND CONFIG_TFM_STACK_WATERMARKS)
 
diff --git a/config/config_base.cmake b/config/config_base.cmake
index dd085d2..ba3504e 100755
--- a/config/config_base.cmake
+++ b/config/config_base.cmake
@@ -27,13 +27,7 @@
 set(TFM_PROFILE                         ""          CACHE STRING    "Profile to use")
 set(TFM_FIH_PROFILE                     OFF         CACHE STRING    "Fault injection hardening profile [OFF, LOW, MEDIUM, HIGH]")
 set(CONFIG_TFM_CONN_HANDLE_MAX_NUM      8           CACHE STRING    "The maximal number of secure services that are connected or requested at the same time")
-
-# If isolation level is overwritten in the command line
-if (TFM_ISOLATION_LEVEL EQUAL 1)
 set(CONFIG_TFM_SPM_BACKEND              "SFN"       CACHE STRING    "The SPM backend [IPC, SFN]")
-else()
-set(CONFIG_TFM_SPM_BACKEND              "IPC"       CACHE STRING    "The SPM backend [IPC, SFN]")
-endif()
 
 # An NSPE client_id is provided by the NSPE OS via the SPM or directly by the SPM.
 # When `TFM_NS_MANAGE_NSID` is `ON`, TF-M supports NSPE OS providing NSPE client_id.
diff --git a/config/set_config.cmake b/config/set_config.cmake
index b6f4fa8..e11895f 100644
--- a/config/set_config.cmake
+++ b/config/set_config.cmake
@@ -60,6 +60,21 @@
 
 include(${CMAKE_SOURCE_DIR}/config/tfm_build_log_config.cmake)
 
+# Load TF-M model specific default config
+# Load IPC backend config ifiIsolation level is explicitly specified to 2/3 or IPC backend is
+# selected via build command line. Otherwise, load SFN backend config by default.
+# If a pair of invalid settings are passed via command line, it will be captured later via config
+# check.
+# Also select IPC model by default for multi-core platform unless it has already selected SFN model
+if ((DEFINED TFM_ISOLATION_LEVEL AND TFM_ISOLATION_LEVEL GREATER 1) OR
+    CONFIG_TFM_SPM_BACKEND STREQUAL "IPC" OR
+    TFM_MULTI_CORE_TOPOLOGY)
+    include(config/tfm_ipc_config_default.cmake)
+else()
+    #The default backend is SFN
+    include(config/tfm_sfn_config_default.cmake)
+endif()
+
 # Load bl1 config
 if (BL1 AND PLATFORM_DEFAULT_BL1)
     include(${CMAKE_SOURCE_DIR}/bl1/config/bl1_config_default.cmake)
@@ -79,13 +94,6 @@
 # Load defaults, setting options not already set
 include(config/config_base.cmake)
 
-# Load TF-M model specific default config
-if (CONFIG_TFM_SPM_BACKEND STREQUAL "SFN")
-    include(config/tfm_sfn_config_default.cmake)
-else() #The default backend is IPC
-    include(config/tfm_ipc_config_default.cmake)
-endif()
-
 # Fetch tf-m-tests repo during config, if NS or regression test is required.
 # Therefore tf-m-tests configs can be set with TF-M configs since their configs
 # are coupled.
diff --git a/config/tfm_ipc_config_default.cmake b/config/tfm_ipc_config_default.cmake
index 29dd236..4d7b026 100644
--- a/config/tfm_ipc_config_default.cmake
+++ b/config/tfm_ipc_config_default.cmake
@@ -9,6 +9,7 @@
 
 ############################ Partitions ########################################
 
+set(CONFIG_TFM_SPM_BACKEND      "IPC"       CACHE STRING    "The SPM backend [IPC, SFN]")
 set(CONFIG_TFM_SPM_BACKEND_IPC  ON)
 set(CONFIG_TFM_SPM_BACKEND_SFN  OFF)
 set(CONFIG_TFM_PARTITION_META   ON)
diff --git a/config/tfm_sfn_config_default.cmake b/config/tfm_sfn_config_default.cmake
index 9b2c6d4..6a01a0f 100644
--- a/config/tfm_sfn_config_default.cmake
+++ b/config/tfm_sfn_config_default.cmake
@@ -9,6 +9,7 @@
 
 ############################ Partitions ########################################
 
+set(CONFIG_TFM_SPM_BACKEND      "SFN"       CACHE STRING    "The SPM backend [IPC, SFN]")
 set(CONFIG_TFM_DOORBELL_API     OFF         CACHE BOOL      "Enable the doorbell APIs")
 set(CONFIG_TFM_SPM_BACKEND_IPC  OFF)
 set(CONFIG_TFM_SPM_BACKEND_SFN  ON)