Profiler: Refine integration with TF-M

- Add NS profiling individual entry
- Do calibration in profiling cases
- Provide PROFILING_INIT() for secure side
- Rename TFM_PROF_PATH to TFM_PROFILING_PATH

Signed-off-by: Jianliang Shen <jianliang.shen@arm.com>
Change-Id: I3c1e6df1d968c1053c11f96bf764528a14a81297
diff --git a/profiling/export/prof_intf_s.h b/profiling/export/prof_intf_s.h
index 5720999..6dd445e 100644
--- a/profiling/export/prof_intf_s.h
+++ b/profiling/export/prof_intf_s.h
@@ -39,6 +39,11 @@
 #define PROF_DO_CALIBRATE(rounds)     prof_calibrate(                   \
         PROF_CALI_IDX_S, rounds, 0)
 
+/*
+ * Init profiling on secure side.
+ */
+#define PROFILING_INIT()              prof_init()
+
 /* Get the calibration value from the tag. */
 #define PROF_GET_CALI_VALUE_FROM_TAG(tag) prof_get_cali_value(          \
                                           PROF_GET_CALI_IDX_FROM_TAG(tag))
diff --git a/profiling/profiling_cases/config.cmake b/profiling/profiling_cases/config.cmake
index 8ab125b..3d95746 100644
--- a/profiling/profiling_cases/config.cmake
+++ b/profiling/profiling_cases/config.cmake
@@ -8,8 +8,8 @@
 ################################ Profiling configs #############################
 
 # Profiling PSA Client API specific variables
-set(PROF_PSA_CLIENT_API_SP_PATH "${TFM_PROF_PATH}/profiling_cases/prof_psa_client_api/partitions")
-set(PROF_PSA_CLIENT_API_CASE_PATH "${TFM_PROF_PATH}/profiling_cases/prof_psa_client_api/cases")
+set(PROF_PSA_CLIENT_API_SP_PATH "${TFM_PROFILING_PATH}/profiling_cases/prof_psa_client_api/partitions")
+set(PROF_PSA_CLIENT_API_CASE_PATH "${TFM_PROFILING_PATH}/profiling_cases/prof_psa_client_api/cases")
 
 ################################ TF-M configs ##################################
 
diff --git a/profiling/profiling_cases/prof_psa_client_api/cases/non_secure/CMakeLists.txt b/profiling/profiling_cases/prof_psa_client_api/cases/non_secure/CMakeLists.txt
index a4793cf..d0e6752 100644
--- a/profiling/profiling_cases/prof_psa_client_api/cases/non_secure/CMakeLists.txt
+++ b/profiling/profiling_cases/prof_psa_client_api/cases/non_secure/CMakeLists.txt
@@ -12,16 +12,16 @@
 target_sources(non_secure_prof_psa_client_api
     INTERFACE
         non_secure_prof_psa_client_api.c
-        ${TFM_PROF_PATH}/export/ns/prof_intf_ns.c
+        ${TFM_PROFILING_PATH}/export/ns/prof_intf_ns.c
 )
 
 target_include_directories(non_secure_prof_psa_client_api
     INTERFACE
         ${PROF_PSA_CLIENT_API_CASE_PATH}/non_secure
         ${PROF_PSA_CLIENT_API_CASE_PATH}/
-        ${TFM_PROF_PATH}/profiler
-        ${TFM_PROF_PATH}/export/ns
-        ${TFM_PROF_PATH}/export
+        ${TFM_PROFILING_PATH}/profiler
+        ${TFM_PROFILING_PATH}/export/ns
+        ${TFM_PROFILING_PATH}/export
 )
 
 target_link_libraries(non_secure_prof_psa_client_api
@@ -29,7 +29,9 @@
         tfm_log
 )
 
-target_compile_definitions(non_secure_prof_psa_client_api
+###################### TF-M Non-secure Profiling ###############################
+
+target_link_libraries(tfm_ns_profiling
     INTERFACE
-        CONFIG_TFM_ENALBE_PROFILING
+        non_secure_prof_psa_client_api
 )
diff --git a/profiling/profiling_cases/prof_psa_client_api/cases/non_secure/non_secure_prof_psa_client_api.c b/profiling/profiling_cases/prof_psa_client_api/cases/non_secure/non_secure_prof_psa_client_api.c
index 2da3f4b..4d553b1 100644
--- a/profiling/profiling_cases/prof_psa_client_api/cases/non_secure/non_secure_prof_psa_client_api.c
+++ b/profiling/profiling_cases/prof_psa_client_api/cases/non_secure/non_secure_prof_psa_client_api.c
@@ -19,6 +19,8 @@
     psa_status_t status;
     uint16_t i = 0;
 
+    PROF_DO_CALIBRATE(PROF_CALIBRATION_ROUND);
+
     for (i = 0; i < TEST_LOOP_CNT; i++) {
         PROF_TIMING_LOG(CONNECT_CP_START, PSA_API_TOPIC);
         handle = psa_connect(PROFILING_SERVICE_SID, PROFILING_SERVICE_VERSION);
diff --git a/profiling/profiling_cases/prof_psa_client_api/cases/prof_psa_client_api_common.h b/profiling/profiling_cases/prof_psa_client_api/cases/prof_psa_client_api_common.h
index 784b9b5..877dc5e 100644
--- a/profiling/profiling_cases/prof_psa_client_api/cases/prof_psa_client_api_common.h
+++ b/profiling/profiling_cases/prof_psa_client_api/cases/prof_psa_client_api_common.h
@@ -16,6 +16,9 @@
 extern "C" {
 #endif
 
+/* Profiling calibration rounds. More rounds, more accurate. */
+#define PROF_CALIBRATION_ROUND      50
+
 #define TEST_LOOP_CNT               20
 
 #define PSA_API_TOPIC               0
diff --git a/profiling/profiling_cases/prof_psa_client_api/cases/secure/CMakeLists.txt b/profiling/profiling_cases/prof_psa_client_api/cases/secure/CMakeLists.txt
index 7f44aea..64b7aef 100644
--- a/profiling/profiling_cases/prof_psa_client_api/cases/secure/CMakeLists.txt
+++ b/profiling/profiling_cases/prof_psa_client_api/cases/secure/CMakeLists.txt
@@ -12,16 +12,16 @@
 target_sources(secure_prof_psa_client_api
     INTERFACE
         secure_prof_psa_client_api.c
-        ${TFM_PROF_PATH}/profiler/profiler.c
-        ${TFM_PROF_PATH}/export/platform/tfm_hal_dwt_prof.c
+        ${TFM_PROFILING_PATH}/profiler/profiler.c
+        ${TFM_PROFILING_PATH}/export/platform/tfm_hal_dwt_prof.c
 )
 
 target_include_directories(secure_prof_psa_client_api
     INTERFACE
         ${PROF_PSA_CLIENT_API_CASE_PATH}/secure
         ${PROF_PSA_CLIENT_API_CASE_PATH}/
-        ${TFM_PROF_PATH}/export
-        ${TFM_PROF_PATH}/profiler
+        ${TFM_PROFILING_PATH}/export
+        ${TFM_PROFILING_PATH}/profiler
 )
 
 target_link_libraries(secure_prof_psa_client_api
diff --git a/profiling/profiling_cases/prof_psa_client_api/cases/secure/secure_prof_psa_client_api.c b/profiling/profiling_cases/prof_psa_client_api/cases/secure/secure_prof_psa_client_api.c
index 3dbbe55..fdbacae 100644
--- a/profiling/profiling_cases/prof_psa_client_api/cases/secure/secure_prof_psa_client_api.c
+++ b/profiling/profiling_cases/prof_psa_client_api/cases/secure/secure_prof_psa_client_api.c
@@ -19,6 +19,8 @@
     psa_status_t status;
     uint16_t i = 0;
 
+    PROF_DO_CALIBRATE(PROF_CALIBRATION_ROUND);
+
     for (i = 0; i < TEST_LOOP_CNT; i++) {
         PROF_TIMING_LOG(CONNECT_CP_START, PSA_API_TOPIC);
         handle = psa_connect(PROFILING_SERVICE_SID, PROFILING_SERVICE_VERSION);
diff --git a/profiling/profiling_cases/tfm_ns_profiling/CMakeLists.txt b/profiling/profiling_cases/tfm_ns_profiling/CMakeLists.txt
new file mode 100644
index 0000000..19699db
--- /dev/null
+++ b/profiling/profiling_cases/tfm_ns_profiling/CMakeLists.txt
@@ -0,0 +1,32 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+cmake_policy(SET CMP0079 NEW)
+
+###################### TF-M Non-secure Profiling ###############################
+
+add_library(tfm_ns_profiling INTERFACE)
+
+target_sources(tfm_ns_profiling
+    INTERFACE
+        tfm_ns_profiling.c
+)
+
+target_include_directories(tfm_ns_profiling
+    INTERFACE
+        .
+)
+
+target_compile_definitions(tfm_ns_profiling
+    INTERFACE
+        CONFIG_TFM_ENALBE_PROFILING
+)
+
+###################### TF-M Non-secure Profiling cases #########################
+
+add_subdirectory(${PROF_PSA_CLIENT_API_CASE_PATH}/non_secure
+                 ${CMAKE_CURRENT_BINARY_DIR}/prof_psa_client_api/non_secure)
diff --git a/profiling/profiling_cases/tfm_ns_profiling/tfm_ns_profiling.c b/profiling/profiling_cases/tfm_ns_profiling/tfm_ns_profiling.c
new file mode 100644
index 0000000..77a52be
--- /dev/null
+++ b/profiling/profiling_cases/tfm_ns_profiling/tfm_ns_profiling.c
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2023, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "tfm_ns_profiling.h"
+#include "non_secure_prof_psa_client_api.h"
+
+/*
+ * The entry of TF-M NS profiling.
+ *
+ * NOTE: This is a thread function for CMISI RTOS2. It could NOT be called by
+ * other functions.
+ */
+void tfm_ns_profiling(void *args)
+{
+    non_secure_prof_psa_client_api();
+
+    /* End of profiling */
+    for (;;) {
+    }
+}
diff --git a/profiling/profiling_cases/tfm_ns_profiling/tfm_ns_profiling.h b/profiling/profiling_cases/tfm_ns_profiling/tfm_ns_profiling.h
new file mode 100644
index 0000000..2b6e4fe
--- /dev/null
+++ b/profiling/profiling_cases/tfm_ns_profiling/tfm_ns_profiling.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2023, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+
+#ifndef __TFM_NS_PROFILING_H__
+#define __TFM_NS_PROFILING_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void tfm_ns_profiling(void *args);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TFM_NS_PROFILING_H__ */