Interface: Refactor NS lock and SVC's interface

The NS lock implementation is refactored to use X
macros. This improves maintenability and readability
as limits only to tfm_ns_svc.h the file to be modified
when a new SVC interface has to be added. The SVC
declarations are also amended to use X macros.

Change-Id: I5d20e9a99c3193594b8882967be085406b8a354b
Signed-off-by: Antonio de Angelis <antonio.deangelis@arm.com>
diff --git a/interface/src/tfm_ns_lock_rtx.c b/interface/src/tfm_ns_lock_rtx.c
index cf5fd6d..5ec6638 100644
--- a/interface/src/tfm_ns_lock_rtx.c
+++ b/interface/src/tfm_ns_lock_rtx.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Arm Limited. All rights reserved.
+ * Copyright (c) 2017-2018, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -36,76 +36,49 @@
 };
 
 /**
- * \def NUM_SVC_DISPATCHERS
+ * \def TFM_SVC_DISPATCH_NAME
  *
+ * \brief Macro to declare a SVC dispatch function name
  */
-#define NUM_SVC_DISPATCHERS (6)
+#define TFM_SVC_DISPATCH_NAME(SVC_ENUM) tfm_svc_dispatch_##SVC_ENUM
+
+/**
+ * \def TFM_SVC_DISPATCH_FUNCTION
+ *
+ * \brief Macro to declare a SVC dispatch naked function body (4 bytes each)
+ */
+#define TFM_SVC_DISPATCH_FUNCTION(SVC_ENUM) \
+    __attribute__((naked)) \
+    static uint32_t TFM_SVC_DISPATCH_NAME(SVC_ENUM)(uint32_t arg0, \
+                                                    uint32_t arg1, \
+                                                    uint32_t arg2, \
+                                                    uint32_t arg3) \
+    { \
+        SVC(SVC_ENUM); \
+        __ASM("BX LR"); \
+    }
 
 /**
  * \brief Naked functions associated to each
- *        SVC needed
+ *        SVC in the list of X macros
+ *        \ref LIST_SVC_DISPATCHERS
  */
-__attribute__((naked))
-static uint32_t tfm_svc_dispatch_SST_GET_HANDLE(uint32_t arg0, uint32_t arg1,
-                                                uint32_t arg2, uint32_t arg3)
-{
-    SVC(SVC_TFM_SST_GET_HANDLE);
-    __ASM("BX LR");
-}
-
-__attribute__((naked))
-static uint32_t tfm_svc_dispatch_SST_CREATE(uint32_t arg0, uint32_t arg1,
-                                                uint32_t arg2, uint32_t arg3)
-{
-    SVC(SVC_TFM_SST_CREATE);
-    __ASM("BX LR");
-}
-
-__attribute__((naked))
-static uint32_t tfm_svc_dispatch_SST_GET_ATTRIBUTES(uint32_t arg0,uint32_t arg1,
-                                                    uint32_t arg2,uint32_t arg3)
-{
-    SVC(SVC_TFM_SST_GET_ATTRIBUTES);
-    __ASM("BX LR");
-}
-
-__attribute__((naked))
-static uint32_t tfm_svc_dispatch_SST_READ(uint32_t arg0, uint32_t arg1,
-                                              uint32_t arg2, uint32_t arg3)
-{
-    SVC(SVC_TFM_SST_READ);
-    __ASM("BX LR");
-}
-
-__attribute__((naked))
-static uint32_t tfm_svc_dispatch_SST_WRITE(uint32_t arg0, uint32_t arg1,
-                                               uint32_t arg2, uint32_t arg3)
-{
-    SVC(SVC_TFM_SST_WRITE);
-    __ASM("BX LR");
-}
-
-__attribute__((naked))
-static uint32_t tfm_svc_dispatch_SST_DELETE(uint32_t arg0, uint32_t arg1,
-                                                uint32_t arg2, uint32_t arg3)
-{
-    SVC(SVC_TFM_SST_DELETE);
-    __ASM("BX LR");
-}
+#define X(SVC_ENUM, SVC_HANDLER) TFM_SVC_DISPATCH_FUNCTION(SVC_ENUM);
+LIST_SVC_DISPATCHERS
+#undef X
 
 /**
  * \brief Array with function pointers to the
  *        naked functions. Entry 0 is treated
-*         as invalid
+ *        as invalid. The other entries are
+ *        taken automatically from the list of
+ *        X macros \ref LIST_SVC_DISPATCHERS
  */
-static void *tfm_svc_dispatch_functions[NUM_SVC_DISPATCHERS+1] = {
+static void *tfm_svc_dispatch_functions[] = {
     (void *) NULL, /* SVC_INVALID */
-    (void *) tfm_svc_dispatch_SST_GET_HANDLE,
-    (void *) tfm_svc_dispatch_SST_CREATE,
-    (void *) tfm_svc_dispatch_SST_GET_ATTRIBUTES,
-    (void *) tfm_svc_dispatch_SST_READ,
-    (void *) tfm_svc_dispatch_SST_WRITE,
-    (void *) tfm_svc_dispatch_SST_DELETE
+#define X(SVC_ENUM, SVC_HANDLER) (void *)TFM_SVC_DISPATCH_NAME(SVC_ENUM),
+    LIST_SVC_DISPATCHERS
+#undef X
 };
 
 /**
@@ -119,7 +92,10 @@
 {
     uint32_t result;
     uint32_t (*tfm_svc_dispatch_function_p)(uint32_t, uint32_t,
-                                                uint32_t, uint32_t);
+                                            uint32_t, uint32_t);
+
+    const uint32_t num_svc_dispatchers =
+      sizeof(tfm_svc_dispatch_functions)/sizeof(tfm_svc_dispatch_functions[0]);
 
     /* Check the NS lock has been initialized */
     if (ns_lock.init == false) {
@@ -127,7 +103,7 @@
     }
 
     /* Validate the SVC number requested */
-    if ((svc_num > SVC_INVALID) && (svc_num < (NUM_SVC_DISPATCHERS+1))) {
+    if ((svc_num > SVC_INVALID) && (svc_num < num_svc_dispatchers)) {
         tfm_svc_dispatch_function_p = tfm_svc_dispatch_functions[svc_num];
 
         /* TFM request protected by NS lock */