Test: Abstract the IRQ test to spm/common

This patch abstracts the IRQ test and IRQ test services
to common.

Signed-off-by: Shawn Shan <Shawn.Shan@arm.com>
Change-Id: I92f3860a0f90f950baaff5ebcc40c58742995d82
diff --git a/test/secure_fw/suites/spm/common/service/CMakeLists.txt b/test/secure_fw/suites/spm/common/service/CMakeLists.txt
index 5a3c8bf..e6fcf10 100644
--- a/test/secure_fw/suites/spm/common/service/CMakeLists.txt
+++ b/test/secure_fw/suites/spm/common/service/CMakeLists.txt
@@ -5,11 +5,10 @@
 #
 #-------------------------------------------------------------------------------
 
-if (TFM_PARTITION_IPC_TEST OR TEST_NS_SFN_BACKEND)
-    add_library(spm_test_service_common INTERFACE)
+add_library(spm_test_service_common INTERFACE)
 
-    target_include_directories(spm_test_service_common
-        INTERFACE
-            .
-    )
-endif()
+target_include_directories(spm_test_service_common
+    INTERFACE
+        .
+        ./..
+)
diff --git a/test/secure_fw/suites/spm/common/service/tfm_irq_test_service.c b/test/secure_fw/suites/spm/common/service/tfm_irq_test_service.c
new file mode 100644
index 0000000..13a645e
--- /dev/null
+++ b/test/secure_fw/suites/spm/common/service/tfm_irq_test_service.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "spm_test_defs.h"
+#include "tfm_irq_test_service.h"
+#include "tfm_plat_test.h"
+#include "psa/service.h"
+
+#ifdef TEST_NS_FLIH_IRQ
+/* The execution flow ensures there are no race conditions for test_type */
+static int32_t test_type = TFM_FLIH_TEST_CASE_INVALID;
+/*
+ * Records times of triggered
+ *
+ * The test cases do not care about exact value of flih_timer_triggered.
+ * They only needs to know if it has reached a certain value.
+ * And it is a single-read-single-writer model.
+ * So the race condition of accessing flih_timer_triggered between the Partition
+ * thread and IRS is acceptable.
+ */
+volatile uint32_t flih_timer_triggered = 0;
+
+psa_flih_result_t tfm_flih_test_timer_handler(void)
+{
+    tfm_plat_test_secure_timer_clear_intr();
+
+    switch (test_type) {
+    case TFM_FLIH_TEST_CASE_1:
+        flih_timer_triggered += 1;
+        return PSA_FLIH_NO_SIGNAL;
+    case TFM_FLIH_TEST_CASE_2:
+        flih_timer_triggered += 1;
+        if (flih_timer_triggered == 10) {
+            return PSA_FLIH_SIGNAL;
+        } else {
+            return PSA_FLIH_NO_SIGNAL;
+        }
+        break;
+    default:
+        psa_panic();
+        break;
+    }
+
+    return PSA_FLIH_NO_SIGNAL;
+}
+
+void flih_test_case_1(const psa_msg_t *msg, psa_signal_t timer_irq_signal)
+{
+    test_type = msg->type;
+
+    flih_timer_triggered = 0;
+
+    psa_irq_enable(timer_irq_signal);
+
+    tfm_plat_test_secure_timer_start();
+
+    while (flih_timer_triggered < 10);
+    tfm_plat_test_secure_timer_stop();
+
+    psa_irq_disable(timer_irq_signal);
+}
+
+void flih_test_case_2(const psa_msg_t *msg, psa_signal_t timer_irq_signal)
+{
+    test_type = msg->type;
+
+    flih_timer_triggered = 0;
+
+    psa_irq_enable(timer_irq_signal);
+
+    tfm_plat_test_secure_timer_start();
+
+    if (psa_wait(timer_irq_signal, PSA_BLOCK) != timer_irq_signal) {
+        psa_panic();
+    }
+    tfm_plat_test_secure_timer_stop();
+
+    psa_reset_signal(timer_irq_signal);
+    psa_irq_disable(timer_irq_signal);
+}
+#endif /* TEST_NS_FLIH_IRQ */
+
+#ifdef TEST_NS_SLIH_IRQ
+static void slih_test_timer_handler(psa_signal_t timer_irq_signal)
+{
+    tfm_plat_test_secure_timer_stop();
+    psa_irq_disable(timer_irq_signal);
+    psa_eoi(timer_irq_signal);
+}
+
+void slih_test_case_1(const psa_msg_t *msg, psa_signal_t timer_irq_signal)
+{
+    psa_irq_enable(timer_irq_signal);
+
+    tfm_plat_test_secure_timer_start();
+
+    if (psa_wait(timer_irq_signal, PSA_BLOCK) != timer_irq_signal) {
+        psa_panic();
+    }
+    slih_test_timer_handler(timer_irq_signal);
+}
+
+#endif /* TEST_NS_SLIH_IRQ */
diff --git a/test/secure_fw/suites/spm/common/service/tfm_irq_test_service.h b/test/secure_fw/suites/spm/common/service/tfm_irq_test_service.h
new file mode 100644
index 0000000..8864335
--- /dev/null
+++ b/test/secure_fw/suites/spm/common/service/tfm_irq_test_service.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_IRQ_TEST_SERVICE_H__
+#define __TFM_IRQ_TEST_SERVICE_H__
+
+#include <stdint.h>
+#include "psa/service.h"
+
+#ifdef TEST_NS_SLIH_IRQ
+void slih_test_case_1(const psa_msg_t *msg, psa_signal_t timer_irq_signal);
+#endif /* TEST_NS_SLIH_IRQ */
+
+#ifdef TEST_NS_FLIH_IRQ
+psa_flih_result_t tfm_flih_test_timer_handler(void);
+void flih_test_case_1(const psa_msg_t *msg, psa_signal_t timer_irq_signal);
+void flih_test_case_2(const psa_msg_t *msg, psa_signal_t timer_irq_signal);
+#endif /* TEST_NS_FLIH_IRQ */
+
+#endif /* __TFM_IRQ_TEST_SERVICE_H__ */
diff --git a/test/secure_fw/suites/spm/common/service/tfm_mmiovec_test_defs.h b/test/secure_fw/suites/spm/common/service/tfm_mmiovec_test_defs.h
deleted file mode 100644
index 20b1598..0000000
--- a/test/secure_fw/suites/spm/common/service/tfm_mmiovec_test_defs.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __TFM_MMIOVEC_TEST_DEFS_H__
-#define __TFM_MMIOVEC_TEST_DEFS_H__
-
-/* MMIOVEC test service types [0x100, 0x1FF] */
-#define INVEC_MAP_AND_UNMAP              (0x100)
-#define OUTVEC_MAP_AND_UNMAP             (0x101)
-#define OUTVEC_MAP_NOT_UNMAP             (0x102)
-
-#define TFM_MMIOVEC_TEST_ERROR           (-257)
-
-#define MMIOVEC_INPUT_DATA               (0xFFFFABCD)
-#define MMIOVEC_OUTPUT_DATA              (0xA5)
-#define MMIOVEC_TEST_VEC_LEN             (4)
-
-#define MMIOVECT_TEST_INVEC    {MMIOVEC_INPUT_DATA, MMIOVEC_INPUT_DATA + 1, \
-                                MMIOVEC_INPUT_DATA + 2, MMIOVEC_INPUT_DATA + 3}
-
-#endif /* __TFM_MMIOVEC_TEST_DEFS_H__ */
diff --git a/test/secure_fw/suites/spm/common/service/tfm_mmiovec_test_service.c b/test/secure_fw/suites/spm/common/service/tfm_mmiovec_test_service.c
index f917aac..64ee884 100644
--- a/test/secure_fw/suites/spm/common/service/tfm_mmiovec_test_service.c
+++ b/test/secure_fw/suites/spm/common/service/tfm_mmiovec_test_service.c
@@ -1,12 +1,12 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
 #include <stdint.h>
-#include "tfm_mmiovec_test_defs.h"
+#include "spm_test_defs.h"
 #include "tfm_mmiovec_test_service.h"
 #include "psa/error.h"
 #include "psa/service.h"
diff --git a/test/secure_fw/suites/spm/common/spm_test_defs.h b/test/secure_fw/suites/spm/common/spm_test_defs.h
new file mode 100644
index 0000000..5cfbbc5
--- /dev/null
+++ b/test/secure_fw/suites/spm/common/spm_test_defs.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_SPM_COMMON_TEST_DEFS_H__
+#define __TFM_SPM_COMMON_TEST_DEFS_H__
+
+#include "psa/framework_feature.h"
+
+/* MMIOVEC test service types [0x100, 0x1FF] */
+#if PSA_FRAMEWORK_HAS_MM_IOVEC
+#define INVEC_MAP_AND_UNMAP              (0x100)
+#define OUTVEC_MAP_AND_UNMAP             (0x101)
+#define OUTVEC_MAP_NOT_UNMAP             (0x102)
+
+#define TFM_MMIOVEC_TEST_ERROR           (-257)
+
+#define MMIOVEC_INPUT_DATA               (0xFFFFABCD)
+#define MMIOVEC_OUTPUT_DATA              (0xA5)
+#define MMIOVEC_TEST_VEC_LEN             (4)
+
+#define MMIOVECT_TEST_INVEC    {MMIOVEC_INPUT_DATA, MMIOVEC_INPUT_DATA + 1, \
+                                MMIOVEC_INPUT_DATA + 2, MMIOVEC_INPUT_DATA + 3}
+#endif /* PSA_FRAMEWORK_HAS_MM_IOVEC */
+
+/* IRQ test service types [0x200, 0x2FF] */
+#ifdef TEST_NS_FLIH_IRQ
+#define TFM_FLIH_TEST_CASE_INVALID          (0x200)
+#define TFM_FLIH_TEST_CASE_1                (0x201)
+#define TFM_FLIH_TEST_CASE_2                (0x202)
+#endif /* TEST_NS_FLIH_IRQ */
+
+#ifdef TEST_NS_SLIH_IRQ
+#define TFM_SLIH_TEST_CASE_1                (0x203)
+#endif /* TEST_NS_SLIH_IRQ */
+
+#endif /* __TFM_SPM_COMMON_TEST_DEFS_H__ */
diff --git a/test/secure_fw/suites/spm/common/suites/CMakeLists.txt b/test/secure_fw/suites/spm/common/suites/CMakeLists.txt
index 79a18c2..4db8dc9 100644
--- a/test/secure_fw/suites/spm/common/suites/CMakeLists.txt
+++ b/test/secure_fw/suites/spm/common/suites/CMakeLists.txt
@@ -10,6 +10,7 @@
 target_include_directories(spm_test_common
     INTERFACE
         .
+        ./..
         ../service
         ../../sfn/service
 )
diff --git a/test/secure_fw/suites/spm/common/suites/irq_test.c b/test/secure_fw/suites/spm/common/suites/irq_test.c
new file mode 100644
index 0000000..826e262
--- /dev/null
+++ b/test/secure_fw/suites/spm/common/suites/irq_test.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "irq_test.h"
+#include "test_framework.h"
+#include "spm_test_defs.h"
+#include "psa/client.h"
+#include "psa/error.h"
+#include "psa_manifest/sid.h"
+
+#ifdef TEST_NS_SLIH_IRQ
+
+void irq_test_slih_case_1(struct test_result_t *ret, psa_handle_t handle)
+{
+    psa_status_t status;
+
+    status = psa_call(handle, TFM_SLIH_TEST_CASE_1, NULL, 0, NULL, 0);
+    if (status != PSA_SUCCESS) {
+        TEST_FAIL("TFM_NS_IRQ_TEST_SLIH_HANDLING FAILED\r\n");
+        return;
+    }
+
+    ret->val = TEST_PASSED;
+}
+#endif /* TEST_NS_SLIH_IRQ */
+
+#ifdef TEST_NS_FLIH_IRQ
+
+void irq_test_flih_case_1(struct test_result_t *ret, psa_handle_t handle)
+{
+    psa_status_t status;
+
+    status = psa_call(handle, TFM_FLIH_TEST_CASE_1, NULL, 0, NULL, 0);
+    if (status != PSA_SUCCESS) {
+        TEST_FAIL("TFM_NS_IRQ_TEST_FLIH not returning signal, FAILED\r\n");
+        return;
+    }
+
+    ret->val = TEST_PASSED;
+}
+
+void irq_test_flih_case_2(struct test_result_t *ret, psa_handle_t handle)
+{
+    psa_status_t status;
+
+    status = psa_call(handle, TFM_FLIH_TEST_CASE_2, NULL, 0, NULL, 0);
+    if (status != PSA_SUCCESS) {
+        TEST_FAIL("TFM_NS_IRQ_TEST_FLIH returning signal FAILED\r\n");
+        return;
+    }
+
+    ret->val = TEST_PASSED;
+}
+#endif /* TEST_NS_FLIH_IRQ */
diff --git a/test/secure_fw/suites/spm/common/suites/irq_test.h b/test/secure_fw/suites/spm/common/suites/irq_test.h
new file mode 100644
index 0000000..08b7ad3
--- /dev/null
+++ b/test/secure_fw/suites/spm/common/suites/irq_test.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __IRQ_TEST_H__
+#define __IRQ_TEST_H__
+
+#include "test_framework.h"
+#include "psa/client.h"
+
+#ifdef TEST_NS_SLIH_IRQ
+/**
+ * Test process:
+ *    - NSPE starts testing
+ *    - Test Partition starts timer
+ *    - Test Partition waits for the timer signal
+ *    - Test Partition receives the signal and stop timer
+ *    - Test Partition returns to NSPE
+ *    - Test finishes
+ */
+void irq_test_slih_case_1(struct test_result_t *ret, psa_handle_t handle);
+#endif /* TEST_NS_SLIH_IRQ */
+
+#ifdef TEST_NS_FLIH_IRQ
+/**
+ * Test process:
+ *    - NSPE starts testing
+ *    - Test Partition starts timer
+ *    - Test Partition waits for the timer to be trigger for a certain count by
+ *      reading the global count in a while loop
+ *    - In the handling function, the count is increased
+ *    - The count reaches the value and test Partition stops timer
+ *    - Test Partition returns to NSPE
+ *    - Test finishes
+ */
+void irq_test_flih_case_1(struct test_result_t *ret, psa_handle_t handle);
+
+/**
+ * Test process:
+ *    - NSPE starts testing
+ *    - Test Partition starts timer
+ *    - Test Partition waits for the timer signal
+ *    - In the handling function, the timer trigger count is increased
+ *    - The count reaches a certain value and the ISR returns PSA_FLIH_SIGNAL
+ *    - Test Partition receives the signal, stops timer and returns to NSPE
+ *    - Test finishes
+ */
+void irq_test_flih_case_2(struct test_result_t *ret, psa_handle_t handle);
+#endif /* TEST_NS_FLIH_IRQ */
+
+#endif /* __IRQ_TEST_H__ */
diff --git a/test/secure_fw/suites/spm/common/suites/mmiovec_test.c b/test/secure_fw/suites/spm/common/suites/mmiovec_test.c
index c1400a1..36fe421 100644
--- a/test/secure_fw/suites/spm/common/suites/mmiovec_test.c
+++ b/test/secure_fw/suites/spm/common/suites/mmiovec_test.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -7,7 +7,7 @@
 
 #include "mmiovec_test.h"
 #include "test_framework.h"
-#include "tfm_mmiovec_test_defs.h"
+#include "spm_test_defs.h"
 #include "psa/client.h"
 #include "psa/error.h"
 #include "psa_manifest/sid.h"
diff --git a/test/secure_fw/suites/spm/ipc/service/tfm_ipc_service/tfm_ipc_service_test.c b/test/secure_fw/suites/spm/ipc/service/tfm_ipc_service/tfm_ipc_service_test.c
index 7792454..3e83faa 100644
--- a/test/secure_fw/suites/spm/ipc/service/tfm_ipc_service/tfm_ipc_service_test.c
+++ b/test/secure_fw/suites/spm/ipc/service/tfm_ipc_service/tfm_ipc_service_test.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -10,12 +10,12 @@
 #include "psa/client.h"
 #include "psa/service.h"
 #include "psa_manifest/tfm_ipc_service_test.h"
+#include "spm_test_defs.h"
 #include "tfm_api.h"
 #include "tfm_hal_isolation.h"
 #include "tfm_secure_api.h"
 #include "tfm_sp_log.h"
 #if PSA_FRAMEWORK_HAS_MM_IOVEC
-#include "tfm_mmiovec_test_defs.h"
 #include "tfm_mmiovec_test_service.h"
 #endif
 
diff --git a/test/secure_fw/suites/spm/irq/CMakeLists.txt b/test/secure_fw/suites/spm/irq/CMakeLists.txt
index 77f3a5f..55895f9 100644
--- a/test/secure_fw/suites/spm/irq/CMakeLists.txt
+++ b/test/secure_fw/suites/spm/irq/CMakeLists.txt
@@ -1,5 +1,5 @@
 #-------------------------------------------------------------------------------
-# Copyright (c) 2021, Arm Limited. All rights reserved.
+# Copyright (c) 2021-2022, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -18,6 +18,8 @@
 target_sources(tfm_test_suite_irq
     PRIVATE
         ./irq_testsuite.c
+        $<$<OR:$<BOOL:${TEST_NS_SLIH_IRQ}>,$<BOOL:${TEST_NS_FLIH_IRQ}>>:
+            ${CMAKE_CURRENT_SOURCE_DIR}/../common/suites/irq_test.c>
 )
 
 target_include_directories(tfm_test_suite_irq
@@ -37,6 +39,7 @@
         $<$<BOOL:${TEST_NS_FLIH_IRQ}>:tfm_flih_test_service_interface>
         $<$<BOOL:${TEST_NS_SLIH_IRQ}>:tfm_slih_test_service_interface>
         platform_ns
+        spm_test_common
 )
 
 target_link_libraries(tfm_ns_tests
diff --git a/test/secure_fw/suites/spm/irq/irq_testsuite.c b/test/secure_fw/suites/spm/irq/irq_testsuite.c
index 31ef56e..7cb1586 100644
--- a/test/secure_fw/suites/spm/irq/irq_testsuite.c
+++ b/test/secure_fw/suites/spm/irq/irq_testsuite.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -11,83 +11,24 @@
 #include "test_framework.h"
 #include "tfm_peripherals_def.h"
 #include "tfm_plat_test.h"
-#ifdef TEST_NS_SLIH_IRQ
-#include "tfm_slih_test_service_types.h"
-#endif /* TEST_NS_SLIH_IRQ */
-#ifdef TEST_NS_FLIH_IRQ
-#include "tfm_flih_test_service_types.h"
-#endif /* TEST_NS_FLIH_IRQ */
+#include "irq_test.h"
 
 #ifdef TEST_NS_SLIH_IRQ
-/*
- * Test process:
- *    - NSPE starts testing
- *    - Test Partition starts timer
- *    - Test Partition waits for the timer signal
- *    - Test Partition receives the signal and stop timer
- *    - Test Partition returns to NSPE
- *    - Test finishes
- */
-static void tfm_irq_test_slih_case_1(struct test_result_t *ret) {
-    psa_status_t status;
-
-    status = psa_call(TFM_SLIH_TEST_CASE_HANDLE,
-                      TFM_SLIH_TEST_CASE_1, NULL, 0, NULL, 0);
-    if (status != PSA_SUCCESS) {
-        TEST_FAIL("TFM_NS_IRQ_TEST_SLIH_HANDLING FAILED\r\n");
-        return;
-    }
-
-    ret->val = TEST_PASSED;
+static void tfm_irq_test_slih_case_1(struct test_result_t *ret)
+{
+    irq_test_slih_case_1(ret, TFM_SLIH_TEST_CASE_HANDLE);
 }
 #endif /* TEST_NS_SLIH_IRQ */
 
 #ifdef TEST_NS_FLIH_IRQ
-/*
- * Test process:
- *    - NSPE starts testing
- *    - Test Partition starts timer
- *    - Test Partition waits for the timer to be trigger for a certain count by
- *      reading the global count in a while loop
- *    - In the handling function, the count is increased
- *    - The count reaches the value and test Partition stops timer
- *    - Test Partition returns to NSPE
- *    - Test finishes
- */
-static void tfm_irq_test_flih_case_1(struct test_result_t *ret) {
-    psa_status_t status;
-
-    status = psa_call(TFM_FLIH_TEST_CASE_HANDLE,
-                      TFM_FLIH_TEST_CASE_1, NULL, 0, NULL, 0);
-    if (status != PSA_SUCCESS) {
-        TEST_FAIL("TFM_NS_IRQ_TEST_FLIH not returning signal, FAILED\r\n");
-        return;
-    }
-
-    ret->val = TEST_PASSED;
+static void tfm_irq_test_flih_case_1(struct test_result_t *ret)
+{
+    irq_test_flih_case_1(ret, TFM_FLIH_TEST_CASE_HANDLE);
 }
 
-/*
- * Test process:
- *    - NSPE starts testing
- *    - Test Partition starts timer
- *    - Test Partition waits for the timer signal
- *    - In the handling function, the timer trigger count is increased
- *    - The count reaches a certain value and the ISR returns PSA_FLIH_SIGNAL
- *    - Test Partition receives the signal, stops timer and returns to NSPE
- *    - Test finishes
- */
-static void tfm_irq_test_flih_case_2(struct test_result_t *ret) {
-    psa_status_t status;
-
-    status = psa_call(TFM_FLIH_TEST_CASE_HANDLE,
-                      TFM_FLIH_TEST_CASE_2, NULL, 0, NULL, 0);
-    if (status != PSA_SUCCESS) {
-        TEST_FAIL("TFM_NS_IRQ_TEST_FLIH returning signal FAILED\r\n");
-        return;
-    }
-
-    ret->val = TEST_PASSED;
+static void tfm_irq_test_flih_case_2(struct test_result_t *ret)
+{
+    irq_test_flih_case_2(ret, TFM_FLIH_TEST_CASE_HANDLE);
 }
 #endif /* TEST_NS_FLIH_IRQ */
 
diff --git a/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/CMakeLists.txt b/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/CMakeLists.txt
index 361ae49..8a65f77 100644
--- a/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/CMakeLists.txt
+++ b/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/CMakeLists.txt
@@ -11,6 +11,8 @@
 target_sources(tfm_app_rot_partition_flih_test
     PRIVATE
         ./tfm_flih_test_service.c
+        $<$<BOOL:${TEST_NS_FLIH_IRQ}>:
+            ${CMAKE_CURRENT_SOURCE_DIR}/../../../common/service/tfm_irq_test_service.c>
 )
 
 # The generated sources
@@ -33,6 +35,7 @@
     PRIVATE
         psa_interface
         tfm_sprt
+        spm_test_service_common
 )
 
 target_link_libraries(tfm_spm
diff --git a/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/tfm_flih_test_service.c b/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/tfm_flih_test_service.c
index 22ad681..a3c53f2 100644
--- a/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/tfm_flih_test_service.c
+++ b/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/tfm_flih_test_service.c
@@ -1,54 +1,24 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
+#include "tfm_plat_test.h"
+#include "spm_test_defs.h"
+#include "tfm_irq_test_service.h"
+#include "tfm_sp_log.h"
 #include "psa/service.h"
 #include "psa_manifest/tfm_flih_test_service.h"
-#include "tfm_flih_test_service_types.h"
-#include "tfm_plat_test.h"
-#include "tfm_sp_log.h"
-
-/* The execution flow ensures there are no race conditions for test_type */
-static int32_t test_type = TFM_FLIH_TEST_CASE_INVALID;
-/*
- * Records times of triggered
- *
- * The test cases do not care about exact value of flih_timer_triggered.
- * They only needs to know if it has reached a certain value.
- * And it is a single-read-single-writer model.
- * So the race condition of accessing flih_timer_triggered between the Partition
- * thread and IRS is acceptable.
- */
-static volatile uint32_t flih_timer_triggered = 0;
 
 psa_flih_result_t tfm_timer0_irq_flih(void)
 {
-    tfm_plat_test_secure_timer_clear_intr();
-
-    switch (test_type) {
-    case TFM_FLIH_TEST_CASE_1:
-        flih_timer_triggered += 1;
-        return PSA_FLIH_NO_SIGNAL;
-    case TFM_FLIH_TEST_CASE_2:
-        flih_timer_triggered += 1;
-        if (flih_timer_triggered == 10) {
-            return PSA_FLIH_SIGNAL;
-        } else {
-            return PSA_FLIH_NO_SIGNAL;
-        }
-        break;
-    default:
-        psa_panic();
-        break;
-    }
-
-    return PSA_FLIH_NO_SIGNAL;
+    return tfm_flih_test_timer_handler();
 }
 
-static void flih_test_get_msg(psa_signal_t signal, psa_msg_t *msg) {
+static void flih_test_get_msg(psa_signal_t signal, psa_msg_t *msg)
+{
     psa_status_t status;
 
     status = psa_get(signal, msg);
@@ -57,39 +27,6 @@
     }
 }
 
-static void flih_test_case_1(psa_msg_t *msg) {
-    flih_timer_triggered = 0;
-
-    psa_irq_enable(TFM_TIMER0_IRQ_SIGNAL);
-
-    tfm_plat_test_secure_timer_start();
-
-    while (flih_timer_triggered < 10);
-    tfm_plat_test_secure_timer_stop();
-
-    psa_irq_disable(TFM_TIMER0_IRQ_SIGNAL);
-
-    psa_reply(msg->handle, PSA_SUCCESS);
-}
-
-static void flih_test_case_2(psa_msg_t *msg) {
-    flih_timer_triggered = 0;
-
-    psa_irq_enable(TFM_TIMER0_IRQ_SIGNAL);
-
-    tfm_plat_test_secure_timer_start();
-
-    if (psa_wait(TFM_TIMER0_IRQ_SIGNAL, PSA_BLOCK) != TFM_TIMER0_IRQ_SIGNAL) {
-        psa_panic();
-    }
-    tfm_plat_test_secure_timer_stop();
-
-    psa_reset_signal(TFM_TIMER0_IRQ_SIGNAL);
-    psa_irq_disable(TFM_TIMER0_IRQ_SIGNAL);
-
-    psa_reply(msg->handle, PSA_SUCCESS);
-}
-
 void tfm_flih_test_service_entry(void)
 {
     psa_signal_t signals = 0;
@@ -99,13 +36,14 @@
         signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);
         if (signals & TFM_FLIH_TEST_CASE_SIGNAL) {
             flih_test_get_msg(TFM_FLIH_TEST_CASE_SIGNAL, &msg);
-            test_type = msg.type;
-            switch (test_type) {
+            switch (msg.type) {
             case TFM_FLIH_TEST_CASE_1:
-                flih_test_case_1(&msg);
+                flih_test_case_1(&msg, TFM_TIMER0_IRQ_SIGNAL);
+                psa_reply(msg.handle, PSA_SUCCESS);
                 break;
             case TFM_FLIH_TEST_CASE_2:
-                flih_test_case_2(&msg);
+                flih_test_case_2(&msg, TFM_TIMER0_IRQ_SIGNAL);
+                psa_reply(msg.handle, PSA_SUCCESS);
                 break;
             default:
                 LOG_ERRFMT("FLIH test service: Invalid message type: 0x%x\r\n",
diff --git a/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/tfm_flih_test_service_types.h b/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/tfm_flih_test_service_types.h
deleted file mode 100644
index 2024999..0000000
--- a/test/secure_fw/suites/spm/irq/service/tfm_flih_test_service/tfm_flih_test_service_types.h
+++ /dev/null
@@ -1,15 +0,0 @@
-/*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __TFM_FLIH_TEST_SERVICE_TYPES_H__
-#define __TFM_FLIH_TEST_SERVICE_TYPES_H__
-
-#define TFM_FLIH_TEST_CASE_INVALID  (0)
-#define TFM_FLIH_TEST_CASE_1        (1)
-#define TFM_FLIH_TEST_CASE_2        (2)
-
-#endif /* __TFM_FLIH_TEST_SERVICE_TYPES_H__ */
diff --git a/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/CMakeLists.txt b/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/CMakeLists.txt
index 72e45d3..8701875 100644
--- a/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/CMakeLists.txt
+++ b/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/CMakeLists.txt
@@ -11,6 +11,8 @@
 target_sources(tfm_app_rot_partition_slih_test
     PRIVATE
         ./tfm_slih_test_service.c
+        $<$<BOOL:${TEST_NS_SLIH_IRQ}>:
+            ${CMAKE_CURRENT_SOURCE_DIR}/../../../common/service/tfm_irq_test_service.c>
 )
 
 # The generated sources
@@ -33,6 +35,7 @@
     PRIVATE
         psa_interface
         tfm_sprt
+        spm_test_service_common
 )
 
 target_link_libraries(tfm_spm
diff --git a/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/tfm_slih_test_service.c b/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/tfm_slih_test_service.c
index cbcd116..9b60902 100644
--- a/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/tfm_slih_test_service.c
+++ b/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/tfm_slih_test_service.c
@@ -1,37 +1,19 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
+#include "tfm_plat_test.h"
+#include "spm_test_defs.h"
+#include "tfm_irq_test_service.h"
+#include "tfm_sp_log.h"
 #include "psa/service.h"
 #include "psa_manifest/tfm_slih_test_service.h"
-#include "tfm_slih_test_service_types.h"
-#include "tfm_plat_test.h"
-#include "tfm_sp_log.h"
 
-static void timer0_handler(void)
+static void slih_test_get_msg(psa_signal_t signal, psa_msg_t *msg)
 {
-    tfm_plat_test_secure_timer_stop();
-    psa_irq_disable(TFM_TIMER0_IRQ_SIGNAL);
-    psa_eoi(TFM_TIMER0_IRQ_SIGNAL);
-}
-
-static void slih_test_case_1(psa_msg_t *msg) {
-    psa_irq_enable(TFM_TIMER0_IRQ_SIGNAL);
-
-    tfm_plat_test_secure_timer_start();
-
-    if (psa_wait(TFM_TIMER0_IRQ_SIGNAL, PSA_BLOCK) != TFM_TIMER0_IRQ_SIGNAL) {
-        psa_panic();
-    }
-    timer0_handler();
-
-    psa_reply(msg->handle, PSA_SUCCESS);
-}
-
-static void slih_test_get_msg(psa_signal_t signal, psa_msg_t *msg) {
     psa_status_t status;
 
     status = psa_get(signal, msg);
@@ -51,7 +33,8 @@
             slih_test_get_msg(TFM_SLIH_TEST_CASE_SIGNAL, &msg);
             switch (msg.type) {
             case TFM_SLIH_TEST_CASE_1:
-                slih_test_case_1(&msg);
+                slih_test_case_1(&msg, TFM_TIMER0_IRQ_SIGNAL);
+                psa_reply(msg.handle, PSA_SUCCESS);
                 break;
             default:
                 LOG_ERRFMT("SLIH test service: Invalid message type: 0x%x\r\n",
diff --git a/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/tfm_slih_test_service_types.h b/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/tfm_slih_test_service_types.h
deleted file mode 100644
index f252e45..0000000
--- a/test/secure_fw/suites/spm/irq/service/tfm_slih_test_service/tfm_slih_test_service_types.h
+++ /dev/null
@@ -1,13 +0,0 @@
-/*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __TFM_SLIH_TEST_SERVICE_TYPES_H__
-#define __TFM_SLIH_TEST_SERVICE_TYPES_H__
-
-#define TFM_SLIH_TEST_CASE_1        (0)
-
-#endif /* __TFM_SLIH_TEST_SERVICE_TYPES_H__ */
diff --git a/test/secure_fw/suites/spm/sfn/service/sfn_partition1/sfn_partition1.c b/test/secure_fw/suites/spm/sfn/service/sfn_partition1/sfn_partition1.c
index 9cda230..1ad73b6 100644
--- a/test/secure_fw/suites/spm/sfn/service/sfn_partition1/sfn_partition1.c
+++ b/test/secure_fw/suites/spm/sfn/service/sfn_partition1/sfn_partition1.c
@@ -8,10 +8,10 @@
 #include <stdint.h>
 #include "psa/service.h"
 #include "psa_manifest/sfn_partition1.h"
+#include "spm_test_defs.h"
 #include "tfm_sp_log.h"
 #include "tfm_sfn_test_defs.h"
 #if PSA_FRAMEWORK_HAS_MM_IOVEC
-#include "tfm_mmiovec_test_defs.h"
 #include "tfm_mmiovec_test_service.h"
 #endif