Service: Add a new SLIH test

This patch adds a new SLIH test Partition and new IRQ test suite.
The test logic for SLIH - previously IRQ test - is simplified a lot
as well.

This service is intended to replace the tfm_irq_test_1 service.

Change-Id: Ie319bd57121f73125819bcfbb80943cc557533d9
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 469e4ac..490bbff 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -32,6 +32,7 @@
 add_subdirectory(suites/t_cose)
 add_subdirectory(suites/platform)
 add_subdirectory(suites/fwu)
+add_subdirectory(suites/irq)
 
 if(TFM_PSA_API)
     add_subdirectory(suites/ipc)
diff --git a/test/framework/non_secure_suites.c b/test/framework/non_secure_suites.c
index a599c7e..7188a7d 100644
--- a/test/framework/non_secure_suites.c
+++ b/test/framework/non_secure_suites.c
@@ -46,6 +46,9 @@
 #ifdef TFM_FUZZER_TOOL_TESTS
 #include "tf_fuzz_testsuite.h"
 #endif /* TFM_FUZZER_TOOL_TESTS */
+#ifdef TFM_ENABLE_SLIH_TEST
+#include "irq_testsuite.h"
+#endif
 
 static struct test_suite_t test_suites[] = {
     /* List test cases which are compliant with level 1 isolation */
@@ -114,6 +117,10 @@
     {&register_testsuite_tf_fuzz_test, 0, 0, 0},
 #endif /* TFM_FUZZER_TOOL_TESTS */
 
+#ifdef TFM_ENABLE_SLIH_TEST
+    {&register_testsuite_irq_test, 0, 0, 0},
+#endif
+
     /* End of test suites */
     {0, 0, 0, 0}
 };
diff --git a/test/suites/irq/CMakeLists.txt b/test/suites/irq/CMakeLists.txt
new file mode 100644
index 0000000..17a2755
--- /dev/null
+++ b/test/suites/irq/CMakeLists.txt
@@ -0,0 +1,43 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+cmake_policy(SET CMP0079 NEW)
+
+if (NOT TFM_ENABLE_SLIH_TEST)
+    return()
+endif()
+
+####################### Non Secure #############################################
+
+add_library(tfm_test_suite_irq STATIC EXCLUDE_FROM_ALL)
+
+target_sources(tfm_test_suite_irq
+    PRIVATE
+        ./irq_testsuite.c
+)
+
+target_include_directories(tfm_test_suite_irq
+    PUBLIC
+        ./
+)
+
+target_compile_definitions(tfm_test_suite_irq
+    PUBLIC
+        $<$<BOOL:${TFM_ENABLE_SLIH_TEST}>:TFM_ENABLE_SLIH_TEST>
+)
+
+target_link_libraries(tfm_test_suite_irq
+    PRIVATE
+        tfm_test_framework_ns
+        $<$<BOOL:${TFM_ENABLE_SLIH_TEST}>:tfm_slih_test_service_interface>
+        platform_ns
+)
+
+target_link_libraries(tfm_ns_tests
+    INTERFACE
+        tfm_test_suite_irq
+)
diff --git a/test/suites/irq/irq_testsuite.c b/test/suites/irq/irq_testsuite.c
new file mode 100644
index 0000000..3956258
--- /dev/null
+++ b/test/suites/irq/irq_testsuite.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "cmsis.h"
+#include "psa_manifest/sid.h"
+#include "psa/client.h"
+#include "test_framework.h"
+#include "tfm_peripherals_def.h"
+#include "tfm_plat_test.h"
+#ifdef TFM_ENABLE_SLIH_TEST
+#include "tfm_slih_test_service_types.h"
+#endif /* TFM_ENABLE_SLIH_TEST */
+
+#ifdef TFM_ENABLE_SLIH_TEST
+/*
+ * 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;
+}
+#endif /* TFM_ENABLE_SLIH_TEST */
+
+static struct test_t irq_test_cases[] = {
+#ifdef TFM_ENABLE_SLIH_TEST
+    {&tfm_irq_test_slih_case_1, "TFM_NS_IRQ_TEST_SLIH_1001",
+     "SLIH HANDLING Case 1", {TEST_PASSED}},
+#endif /* TFM_ENABLE_SLIH_TEST */
+};
+
+void register_testsuite_irq_test(struct test_suite_t *p_test_suite)
+{
+    uint32_t list_size;
+
+    list_size = (sizeof(irq_test_cases) / sizeof(irq_test_cases[0]));
+
+    set_testsuite("TFM IRQ Test (TFM_IRQ_TEST_1xxx)",
+                  irq_test_cases, list_size, p_test_suite);
+}
diff --git a/test/suites/irq/irq_testsuite.h b/test/suites/irq/irq_testsuite.h
new file mode 100644
index 0000000..934965f
--- /dev/null
+++ b/test/suites/irq/irq_testsuite.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __IRQ_TESTSUITE_H__
+#define __IRQ_TESTSUITE_H__
+
+#include "test_framework.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void register_testsuite_irq_test(struct test_suite_t *p_test_suite);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __IRQ_TESTSUITE_H__ */
diff --git a/test/test_services/CMakeLists.txt b/test/test_services/CMakeLists.txt
index 0e2740c..480a168 100644
--- a/test/test_services/CMakeLists.txt
+++ b/test/test_services/CMakeLists.txt
@@ -47,6 +47,9 @@
 if(TFM_IRQ_TEST)
     add_subdirectory(tfm_irq_test_service_1)
 endif()
+if(TFM_ENABLE_SLIH_TEST)
+    add_subdirectory(tfm_slih_test_service)
+endif()
 add_subdirectory(tfm_ps_test_service)
 if (TEST_S)
     add_subdirectory(tfm_secure_client_service)
diff --git a/test/test_services/tfm_slih_test_service/CMakeLists.txt b/test/test_services/tfm_slih_test_service/CMakeLists.txt
new file mode 100644
index 0000000..e51f8b3
--- /dev/null
+++ b/test/test_services/tfm_slih_test_service/CMakeLists.txt
@@ -0,0 +1,61 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_policy(SET CMP0079 NEW)
+
+add_library(tfm_app_rot_partition_slih_test STATIC)
+
+target_sources(tfm_app_rot_partition_slih_test
+    PRIVATE
+        ./tfm_slih_test_service.c
+)
+
+# The generated sources
+target_sources(tfm_app_rot_partition_slih_test
+    PRIVATE
+        $<$<BOOL:${TFM_PSA_API}>:
+            ${CMAKE_BINARY_DIR}/generated/test_services/tfm_slih_test_service/auto_generated/intermedia_tfm_slih_test_service.c>
+    INTERFACE
+        $<$<BOOL:${TFM_PSA_API}>:
+            ${CMAKE_BINARY_DIR}/generated/test_services/tfm_slih_test_service/auto_generated/load_info_tfm_slih_test_service.c>
+)
+
+target_include_directories(tfm_app_rot_partition_slih_test
+    PUBLIC
+        ./
+        ${CMAKE_BINARY_DIR}/generated/test_services/tfm_slih_test_service
+)
+
+target_link_libraries(tfm_app_rot_partition_slih_test
+    PRIVATE
+        psa_interface
+        tfm_sprt
+)
+
+target_link_libraries(tfm_spm
+    PRIVATE
+        tfm_app_rot_partition_slih_test
+)
+
+# Interfaces for other targets
+add_library(tfm_slih_test_service_interface INTERFACE)
+
+target_include_directories(tfm_slih_test_service_interface
+    INTERFACE
+        ./
+)
+
+############################ Partition Defs ####################################
+
+target_link_libraries(tfm_partitions
+    INTERFACE
+        tfm_app_rot_partition_slih_test
+)
+
+target_compile_definitions(tfm_partition_defs
+    INTERFACE
+        $<$<BOOL:${TFM_ENABLE_SLIH_TEST}>:TFM_ENABLE_SLIH_TEST>
+)
diff --git a/test/test_services/tfm_slih_test_service/tfm_slih_test_service.c b/test/test_services/tfm_slih_test_service/tfm_slih_test_service.c
new file mode 100644
index 0000000..e146027
--- /dev/null
+++ b/test/test_services/tfm_slih_test_service/tfm_slih_test_service.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#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)
+{
+    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();
+
+    while (psa_wait(TFM_TIMER0_IRQ_SIGNAL, PSA_BLOCK) != TFM_TIMER0_IRQ_SIGNAL);
+    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);
+    if (status != PSA_SUCCESS) {
+        psa_panic();
+    }
+}
+
+void tfm_slih_test_service_entry(void)
+{
+    psa_signal_t signals = 0;
+    psa_msg_t msg;
+
+    while (1) {
+        signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);
+        if (signals & TFM_SLIH_TEST_CASE_SIGNAL) {
+            slih_test_get_msg(TFM_SLIH_TEST_CASE_SIGNAL, &msg);
+            switch (msg.type) {
+            case TFM_SLIH_TEST_CASE_1:
+                slih_test_case_1(&msg);
+                break;
+            default:
+                LOG_ERRFMT("SLIH test service: Invalid message type: 0x%x\r\n",
+                   msg.type);
+                psa_panic();
+                break;
+            }
+        }
+    }
+}
diff --git a/test/test_services/tfm_slih_test_service/tfm_slih_test_service.yaml b/test/test_services/tfm_slih_test_service/tfm_slih_test_service.yaml
new file mode 100644
index 0000000..dfd46b7
--- /dev/null
+++ b/test/test_services/tfm_slih_test_service/tfm_slih_test_service.yaml
@@ -0,0 +1,41 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+{
+  "psa_framework_version": 1.1,
+  "name": "TFM_SP_SLIH_TEST",
+  "type": "APPLICATION-ROT",
+  "priority": "NORMAL",
+  "model": "IPC",
+  "entry_point": "tfm_slih_test_service_entry",
+  "stack_size": "0x0400",
+  "services": [
+    {
+      "name": "TFM_SLIH_TEST_CASE",
+      "sid": "0x0000F0A0",
+      "connection_based": false,
+      "stateless_handle": "auto",
+      "non_secure_clients": true,
+      "version": 1,
+      "version_policy": "STRICT"
+    }
+  ],
+  "mmio_regions": [
+    {
+      "name": "TFM_PERIPHERAL_TIMER0",
+      "permission": "READ-WRITE"
+    }
+  ],
+  "irqs": [
+    {
+      "source": "TFM_TIMER0_IRQ",
+      "name": "TFM_TIMER0_IRQ",
+      "handling": "SLIH",
+      "tfm_irq_priority": 64,
+    }
+  ]
+}
diff --git a/test/test_services/tfm_slih_test_service/tfm_slih_test_service_types.h b/test/test_services/tfm_slih_test_service/tfm_slih_test_service_types.h
new file mode 100644
index 0000000..f252e45
--- /dev/null
+++ b/test/test_services/tfm_slih_test_service/tfm_slih_test_service_types.h
@@ -0,0 +1,13 @@
+/*
+ * 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__ */