SPM: Convert SVC number to uint8_t

SVC number encoded in SVC instruction is 8-bit long.
Currently it relies on the short-enum compiler option to have a
8-bit long SVC number type.

This patch converts the enum to uint8_t for SVC number and divids
the SVC numbers to two parts for IPC model:
 - 0x0 ~ 0x7F for SVC calls only allowed from Thread Mode
 - 0x80 ~ 0xFF for SVC calls only allowed from interrupt handling

Note: For library model, the SVC numbers have no restrictions.

Since the requirements for SVC number assignment are different,
this patch also split the SVC number header for IPC and Library models.

Change-Id: I0fb4dd110be6bab05e1c4b9a8fc55e1b8bfbc0eb
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/interface/src/psa/psa_client.c b/interface/src/psa/psa_client.c
index 2d92182..00d95ee 100644
--- a/interface/src/psa/psa_client.c
+++ b/interface/src/psa/psa_client.c
@@ -6,9 +6,10 @@
  */
 
 #include <inttypes.h>
-#include "tfm/tfm_core_svc.h"
 #include "psa/client.h"
+#include "svc_num.h"
 #include "tfm_api.h"
+#include "tfm_hal_device_header.h"
 #include "tfm_psa_call_param.h"
 
 __attribute__((naked))
diff --git a/interface/src/psa/psa_lifecycle.c b/interface/src/psa/psa_lifecycle.c
index 313bfd5..cee7dfd 100644
--- a/interface/src/psa/psa_lifecycle.c
+++ b/interface/src/psa/psa_lifecycle.c
@@ -1,12 +1,13 @@
 /*
- * Copyright (c) 2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 #include <inttypes.h>
 #include "psa/lifecycle.h"
-#include "tfm/tfm_core_svc.h"
+#include "svc_num.h"
+#include "tfm_hal_device_header.h"
 
 __attribute__((naked))
 uint32_t psa_rot_lifecycle_state(void)
diff --git a/interface/src/psa/psa_service.c b/interface/src/psa/psa_service.c
index 7ae3bd1..a8ecef4 100644
--- a/interface/src/psa/psa_service.c
+++ b/interface/src/psa/psa_service.c
@@ -7,9 +7,10 @@
 
 #include <inttypes.h>
 #include <stdio.h>
-#include "tfm/tfm_core_svc.h"
 #include "psa/client.h"
 #include "psa/service.h"
+#include "svc_num.h"
+#include "tfm_hal_device_header.h"
 
 __attribute__((naked))
 psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout)
diff --git a/platform/ext/common/tfm_hal_sp_logdev_periph.c b/platform/ext/common/tfm_hal_sp_logdev_periph.c
index 177fb83..9929d53 100644
--- a/platform/ext/common/tfm_hal_sp_logdev_periph.c
+++ b/platform/ext/common/tfm_hal_sp_logdev_periph.c
@@ -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
  *
@@ -7,7 +7,12 @@
 
 #include "tfm_hal_sp_logdev.h"
 #include "uart_stdout.h"
-#include "tfm/tfm_core_svc.h"
+#ifdef TFM_PSA_API
+#include "svc_num.h"
+#else
+#include "tfm_core_svc.h"
+#endif /* TFM_PSA_API */
+#include "tfm_hal_device_header.h"
 
 __attribute__((naked))
 static int tfm_output_unpriv_string(const unsigned char *str, size_t len)
diff --git a/secure_fw/include/tfm/tfm_core_svc.h b/secure_fw/include/tfm/tfm_core_svc.h
deleted file mode 100644
index 74ad157..0000000
--- a/secure_fw/include/tfm/tfm_core_svc.h
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __TFM_CORE_SVC_H__
-#define __TFM_CORE_SVC_H__
-
-#include "tfm_hal_device_header.h"
-#include "tfm_spm_log.h"
-
-typedef enum {
-    TFM_SVC_SFN_REQUEST = 0,
-    TFM_SVC_SFN_RETURN,
-    TFM_SVC_GET_CALLER_CLIENT_ID,
-    TFM_SVC_SPM_REQUEST,
-    TFM_SVC_GET_BOOT_DATA,
-    TFM_SVC_DEPRIV_REQ,
-    TFM_SVC_DEPRIV_RET,
-#ifndef TFM_PSA_API
-    TFM_SVC_ENABLE_IRQ,
-    TFM_SVC_DISABLE_IRQ,
-#endif
-    TFM_SVC_PSA_WAIT,
-    TFM_SVC_PSA_EOI,
-    TFM_SVC_HANDLER_MODE,
-#ifdef TFM_PSA_API
-    /* PSA Client SVC */
-    TFM_SVC_PSA_FRAMEWORK_VERSION,
-    TFM_SVC_PSA_VERSION,
-    TFM_SVC_PSA_CONNECT,
-    TFM_SVC_PSA_CALL,
-    TFM_SVC_PSA_CLOSE,
-    /* PSA Service SVC */
-    TFM_SVC_PSA_GET,
-    TFM_SVC_PSA_SET_RHANDLE,
-    TFM_SVC_PSA_READ,
-    TFM_SVC_PSA_SKIP,
-    TFM_SVC_PSA_WRITE,
-    TFM_SVC_PSA_REPLY,
-    TFM_SVC_PSA_NOTIFY,
-    TFM_SVC_PSA_CLEAR,
-    TFM_SVC_PSA_PANIC,
-    TFM_SVC_PSA_LIFECYCLE,
-#endif
-#if (TFM_SPM_LOG_LEVEL > TFM_SPM_LOG_LEVEL_SILENCE)
-    TFM_SVC_OUTPUT_UNPRIV_STRING,
-#endif
-    /* Secure Partition API for interrupt control */
-    TFM_SVC_PSA_IRQ_ENABLE,
-    TFM_SVC_PSA_IRQ_DISABLE,
-
-    TFM_SVC_PLATFORM_BASE = 50 /* leave room for additional Core handlers */
-} tfm_svc_number_t;
-
-#define SVC(code) __ASM volatile("svc %0" : : "I" (code))
-
-#endif /* __TFM_CORE_SVC_H__ */
diff --git a/secure_fw/partitions/lib/sprt/service_api.c b/secure_fw/partitions/lib/sprt/service_api.c
index 09d48fd..72bd536 100644
--- a/secure_fw/partitions/lib/sprt/service_api.c
+++ b/secure_fw/partitions/lib/sprt/service_api.c
@@ -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
  *
@@ -7,7 +7,11 @@
 
 #include "cmsis_compiler.h"
 #include "service_api.h"
-#include "tfm/tfm_core_svc.h"
+#ifdef TFM_PSA_API
+#include "svc_num.h"
+#else
+#include "tfm_core_svc.h"
+#endif /* TFM_PSA_API */
 
 __attribute__((naked))
 int32_t tfm_core_get_boot_data(uint8_t major_type,
diff --git a/secure_fw/spm/CMakeLists.txt b/secure_fw/spm/CMakeLists.txt
index 2241676..8ad1f2d 100755
--- a/secure_fw/spm/CMakeLists.txt
+++ b/secure_fw/spm/CMakeLists.txt
@@ -157,7 +157,8 @@
 target_include_directories(tfm_secure_api
     INTERFACE
         $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
-        $<$<BOOL:${TFM_PSA_API}>:${CMAKE_CURRENT_SOURCE_DIR}/model_ipc/include>
+        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/interface>
+        $<$<NOT:$<BOOL:${TFM_PSA_API}>>:${CMAKE_CURRENT_SOURCE_DIR}/cmsis_func/include>
         $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/arch/include>
 )
 
diff --git a/secure_fw/spm/cmsis_func/include/tfm_core_svc.h b/secure_fw/spm/cmsis_func/include/tfm_core_svc.h
new file mode 100644
index 0000000..accf1b8
--- /dev/null
+++ b/secure_fw/spm/cmsis_func/include/tfm_core_svc.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_CORE_SVC_H__
+#define __TFM_CORE_SVC_H__
+
+#include "tfm_spm_log.h"
+
+/* SVC numbers */
+#define TFM_SVC_PSA_WAIT                (0x0)
+#define TFM_SVC_PSA_EOI                 (0x1)
+#define TFM_SVC_SFN_REQUEST             (0x2)
+#define TFM_SVC_SFN_RETURN              (0x3)
+#define TFM_SVC_GET_CALLER_CLIENT_ID    (0x4)
+#define TFM_SVC_SPM_REQUEST             (0x5)
+#define TFM_SVC_GET_BOOT_DATA           (0x6)
+#define TFM_SVC_DEPRIV_REQ              (0x7)
+#define TFM_SVC_DEPRIV_RET              (0x8)
+#define TFM_SVC_ENABLE_IRQ              (0x9)
+#define TFM_SVC_DISABLE_IRQ             (0xA)
+#if (TFM_SPM_LOG_LEVEL > TFM_SPM_LOG_LEVEL_SILENCE)
+#define TFM_SVC_OUTPUT_UNPRIV_STRING    (0xB)
+#endif
+
+#endif /* __TFM_CORE_SVC_H__ */
diff --git a/secure_fw/spm/cmsis_func/tfm_core_svcalls_func.c b/secure_fw/spm/cmsis_func/tfm_core_svcalls_func.c
index a3c536b..193ab27 100644
--- a/secure_fw/spm/cmsis_func/tfm_core_svcalls_func.c
+++ b/secure_fw/spm/cmsis_func/tfm_core_svcalls_func.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -7,7 +7,6 @@
 
 #include <string.h>
 
-#include "tfm/tfm_core_svc.h"
 #include "tfm_secure_api.h"
 #include "region_defs.h"
 #include "spm_func.h"
@@ -20,7 +19,7 @@
 #include "ffm/tfm_boot_data.h"
 
 #ifdef PLATFORM_SVC_HANDLERS
-extern int32_t platform_svc_handlers(tfm_svc_number_t svc_num,
+extern int32_t platform_svc_handlers(uint8_t svc_num,
                                      uint32_t *svc_args, uint32_t lr);
 #endif
 
diff --git a/secure_fw/spm/cmsis_psa/arch/tfm_arch.c b/secure_fw/spm/cmsis_psa/arch/tfm_arch.c
index cb10c60..412e8c2 100644
--- a/secure_fw/spm/cmsis_psa/arch/tfm_arch.c
+++ b/secure_fw/spm/cmsis_psa/arch/tfm_arch.c
@@ -1,13 +1,13 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
+#include "svc_num.h"
 #include "tfm_arch.h"
 #include "tfm_core_utils.h"
-#include "tfm/tfm_core_svc.h"
 #include "tfm/tfm_spm_services.h"
 
 __attribute__((naked))
diff --git a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_base.c b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_base.c
index 72d1685..465a26d 100644
--- a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_base.c
+++ b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_base.c
@@ -11,7 +11,7 @@
 #include "tfm_arch.h"
 #include "exception_info.h"
 #include "tfm_secure_api.h"
-#include "tfm/tfm_core_svc.h"
+#include "svc_num.h"
 
 #if !defined(__ARM_ARCH_8M_BASE__)
 #error "Unsupported ARM Architecture."
diff --git a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_main.c b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_main.c
index 7ee168b..6fbebff 100644
--- a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_main.c
+++ b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_main.c
@@ -14,7 +14,7 @@
 #include "exception_info.h"
 #include "tfm_secure_api.h"
 #include "spm_ipc.h"
-#include "tfm/tfm_core_svc.h"
+#include "svc_num.h"
 
 #if !defined(__ARM_ARCH_8M_MAIN__) && !defined(__ARM_ARCH_8_1M_MAIN__)
 #error "Unsupported ARM Architecture."
diff --git a/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c b/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
index 753b6cd..60d83e9 100644
--- a/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
+++ b/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
@@ -13,7 +13,7 @@
 #include "tfm_core_trustzone.h"
 #include "tfm_svcalls.h"
 #include "utilities.h"
-#include "tfm/tfm_core_svc.h"
+#include "svc_num.h"
 #include "ffm/tfm_boot_data.h"
 #include "ffm/psa_client_service_apis.h"
 #include "tfm_hal_spm_logdev.h"
@@ -23,11 +23,11 @@
 REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
 
 #ifdef PLATFORM_SVC_HANDLERS
-extern int32_t platform_svc_handlers(tfm_svc_number_t svc_num,
+extern int32_t platform_svc_handlers(uint8_t svc_num,
                                      uint32_t *ctx, uint32_t lr);
 #endif
 
-static int32_t SVC_Handler_IPC(tfm_svc_number_t svc_num, uint32_t *ctx,
+static int32_t SVC_Handler_IPC(uint8_t svc_num, uint32_t *ctx,
                                uint32_t lr)
 {
     bool ns_caller = false;
@@ -125,7 +125,7 @@
 
 uint32_t tfm_core_svc_handler(uint32_t *msp, uint32_t *psp, uint32_t exc_return)
 {
-    tfm_svc_number_t svc_number = TFM_SVC_PSA_FRAMEWORK_VERSION;
+    uint8_t svc_number = TFM_SVC_PSA_FRAMEWORK_VERSION;
     uint32_t *svc_args = msp;
 
     if (!(exc_return & EXC_RETURN_MODE)) {
@@ -149,7 +149,7 @@
         /* SV called directly from secure context. Check instruction for
          * svc_number
          */
-        svc_number = ((tfm_svc_number_t *)svc_args[6])[-2];
+        svc_number = ((uint8_t *)svc_args[6])[-2];
     } else {
         /* Secure SV executing with NS return.
          * NS cannot directly trigger S SVC so this should not happen. This is
diff --git a/secure_fw/spm/cmsis_psa/tfm_thread.c b/secure_fw/spm/cmsis_psa/tfm_thread.c
index 4c4ef16..91d272d 100644
--- a/secure_fw/spm/cmsis_psa/tfm_thread.c
+++ b/secure_fw/spm/cmsis_psa/tfm_thread.c
@@ -8,8 +8,8 @@
 #include "tfm_arch.h"
 #include "tfm_thread.h"
 #include "utilities.h"
+#include "svc_num.h"
 #include "tfm_memory_utils.h"
-#include "tfm/tfm_core_svc.h"
 #include "tfm_core_utils.h"
 
 /* Force ZERO in case ZI(bss) clear is missing */
diff --git a/secure_fw/spm/include/interface/svc_num.h b/secure_fw/spm/include/interface/svc_num.h
new file mode 100644
index 0000000..1ffa32e
--- /dev/null
+++ b/secure_fw/spm/include/interface/svc_num.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __SVC_NUM_H__
+#define __SVC_NUM_H__
+
+#include "tfm_spm_log.h"
+
+/*
+ * SVC numbers for FF-M compliant implementations.
+ * 0x0 ~ 0x7F can be only called from Thread Mode, the rest from ISR only.
+ */
+
+/********************* SVC for Thread Mode ************************************/
+/* PSA Client APIs */
+#define TFM_SVC_PSA_FRAMEWORK_VERSION   (0x0)
+#define TFM_SVC_PSA_VERSION             (0x1)
+#define TFM_SVC_PSA_CONNECT             (0x2)
+#define TFM_SVC_PSA_CALL                (0x3)
+#define TFM_SVC_PSA_CLOSE               (0x4)
+/* PSA Secure Partition APIs */
+#define TFM_SVC_PSA_WAIT                (0x5)
+#define TFM_SVC_PSA_GET                 (0x6)
+#define TFM_SVC_PSA_SET_RHANDLE         (0x7)
+#define TFM_SVC_PSA_READ                (0x8)
+#define TFM_SVC_PSA_SKIP                (0x9)
+#define TFM_SVC_PSA_WRITE               (0xA)
+#define TFM_SVC_PSA_REPLY               (0xB)
+#define TFM_SVC_PSA_NOTIFY              (0xC)
+#define TFM_SVC_PSA_CLEAR               (0xD)
+#define TFM_SVC_PSA_EOI                 (0xE)
+#define TFM_SVC_PSA_PANIC               (0xF)
+#define TFM_SVC_PSA_LIFECYCLE           (0x10)
+#define TFM_SVC_PSA_IRQ_ENABLE          (0x11)
+#define TFM_SVC_PSA_IRQ_DISABLE         (0x12)
+/* TF-M specific, starts from 0x40 */
+#define TFM_SVC_SPM_REQUEST             (0x40)
+#define TFM_SVC_GET_BOOT_DATA           (0x41)
+#define TFM_SVC_HANDLER_MODE            (0x42)
+#if (TFM_SPM_LOG_LEVEL > TFM_SPM_LOG_LEVEL_SILENCE)
+#define TFM_SVC_OUTPUT_UNPRIV_STRING    (0x7F)
+#endif
+#define TFM_SVC_NUMBER_DIVIDER          (0x7F)
+/********************* SVC for interrupt handling *****************************/
+
+#endif /* __SVC_NUM_H__ */
diff --git a/secure_fw/spm/include/tfm_secure_api.h b/secure_fw/spm/include/tfm_secure_api.h
index 8ed7a79..0fa9cc9 100644
--- a/secure_fw/spm/include/tfm_secure_api.h
+++ b/secure_fw/spm/include/tfm_secure_api.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -12,11 +12,15 @@
 #include <arm_cmse.h>
 #endif
 #include "tfm_arch.h"
-#include "tfm/tfm_core_svc.h"
 #include "tfm_api.h"
 #include "utilities.h"
 #include "tfm_boot_status.h"
 #include "psa/service.h"
+#ifdef TFM_PSA_API
+#include "svc_num.h"
+#else
+#include "tfm_core_svc.h"
+#endif /* TFM_PSA_API */
 
 #ifndef TFM_MULTI_CORE_TOPOLOGY
 #ifndef TFM_PSA_API