SPM: Enable MMIO region only when MMIO region exists

Signed-off-by: Sherry Zhang <sherry.zhang2@arm.com>
Change-Id: I1b381dcac7752db35cad7429ddc715b52a8d6381
diff --git a/interface/include/config_impl.h.template b/interface/include/config_impl.h.template
index 83ae49e..f97c2a6 100644
--- a/interface/include/config_impl.h.template
+++ b/interface/include/config_impl.h.template
@@ -19,6 +19,7 @@
 #define {{"%-56s"|format("CONFIG_TFM_PSA_API_SUPERVISOR_CALL")}} {{config_impl['CONFIG_TFM_PSA_API_SUPERVISOR_CALL']}}
 
 #define {{"%-56s"|format("CONFIG_TFM_CONNECTION_BASED_SERVICE_API")}} {{config_impl['CONFIG_TFM_CONNECTION_BASED_SERVICE_API']}}
+#define {{"%-56s"|format("CONFIG_TFM_MMIO_REGION_ENABLE")}} {{config_impl['CONFIG_TFM_MMIO_REGION_ENABLE']}}
 #define {{"%-56s"|format("CONFIG_TFM_FLIH_API")}} {{config_impl['CONFIG_TFM_FLIH_API']}}
 #define {{"%-56s"|format("CONFIG_TFM_SLIH_API")}} {{config_impl['CONFIG_TFM_SLIH_API']}}
 
diff --git a/platform/ext/target/arm/mps2/an521/tfm_hal_isolation.c b/platform/ext/target/arm/mps2/an521/tfm_hal_isolation.c
index bd026ea..7f1515c 100644
--- a/platform/ext/target/arm/mps2/an521/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/mps2/an521/tfm_hal_isolation.c
@@ -407,6 +407,7 @@
      *
      * NOTE: Need to add validation of numbered MMIO if platform requires.
      */
+#if CONFIG_TFM_MMIO_REGION_ENABLE == 1
     for (i = 0; i < p_ldinf->nassets; i++) {
         if (!(p_asset[i].attr & ASSET_ATTR_NAMED_MMIO)) {
             continue;
@@ -476,6 +477,7 @@
         }
 #endif
     }
+#endif /* CONFIG_TFM_MMIO_REGION_ENABLE == 1 */
 
 #if TFM_LVL == 3
     partition_attrs <<= HANDLE_PER_ATTR_BITS;
@@ -551,6 +553,9 @@
         }
     }
 
+    i = n_configured_regions + i;
+
+#if CONFIG_TFM_MMIO_REGION_ENABLE == 1
     /* Named MMIO part */
     local_handle = local_handle & (~HANDLE_INDEX_MASK);
     local_handle >>= HANDLE_PER_ATTR_BITS;
@@ -558,7 +563,6 @@
 
     localcfg.region_attridx = MPU_ARMV8M_MAIR_ATTR_DEVICE_IDX;
 
-    i = n_configured_regions + i;
     while (mmio_index && i < MPU_REGION_NUM) {
         plat_data_ptr =
           (struct platform_data_t *)partition_named_mmio_list[mmio_index - 1];
@@ -577,6 +581,7 @@
         local_handle >>= HANDLE_PER_ATTR_BITS;
         mmio_index = local_handle & HANDLE_ATTR_INDEX_MASK;
     }
+#endif
 
     /* Disable unused regions */
     while (i < MPU_REGION_NUM) {
@@ -588,7 +593,6 @@
 #endif /* TFM_LVL == 3 */
     FIH_RET(fih_int_encode(TFM_HAL_SUCCESS));
 }
-#endif /* TFM_PSA_API */
 
 FIH_RET_TYPE(enum tfm_hal_status_t) tfm_hal_memory_check(
                                            uintptr_t boundary, uintptr_t base,
@@ -634,3 +638,4 @@
         FIH_RET(fih_int_encode(TFM_HAL_ERROR_MEM_FAULT));
     }
 }
+#endif /* TFM_PSA_API */
diff --git a/tools/tfm_parse_manifest_list.py b/tools/tfm_parse_manifest_list.py
index 76dc227..2aa1279 100644
--- a/tools/tfm_parse_manifest_list.py
+++ b/tools/tfm_parse_manifest_list.py
@@ -295,6 +295,7 @@
     partition_statistics = {
         'connection_based_srv_num': 0,
         'ipc_partitions': [],
+        'mmio_region_num': 0,
         'flih_num': 0,
         'slih_num': 0
     }
@@ -305,6 +306,7 @@
         'CONFIG_TFM_PSA_API_CROSS_CALL'           : '0',
         'CONFIG_TFM_PSA_API_SUPERVISOR_CALL'      : '0',
         'CONFIG_TFM_CONNECTION_BASED_SERVICE_API' : '0',
+        'CONFIG_TFM_MMIO_REGION_ENABLE'           : '0',
         'CONFIG_TFM_FLIH_API'                     : '0',
         'CONFIG_TFM_SLIH_API'                     : '0'
     }
@@ -408,6 +410,10 @@
                 partition_statistics['connection_based_srv_num'] += 1
         logging.debug('{} has {} services'.format(manifest['name'], srv_idx +1))
 
+        # Calculate the number of mmio region
+        mmio_region_list = manifest.get('mmio_regions', [])
+        partition_statistics['mmio_region_num'] += len(mmio_region_list)
+
         # Set initial value to -1 to make (irq + 1) reflect the correct
         # number (0) when there are no irqs.
         irq_idx = -1
@@ -483,6 +489,9 @@
     if partition_statistics['connection_based_srv_num'] > 0:
         config_impl['CONFIG_TFM_CONNECTION_BASED_SERVICE_API'] = 1
 
+    if partition_statistics['mmio_region_num'] > 0:
+        config_impl['CONFIG_TFM_MMIO_REGION_ENABLE'] = 1
+
     if partition_statistics['flih_num'] > 0:
         config_impl['CONFIG_TFM_FLIH_API'] = 1
     if partition_statistics['slih_num'] > 0: