Dualcpu: Add NS mailbox statistics functionalities
Add statistics module in NS mailbox.
It records the number of mailbox message submission and the total
number of occupied NS mailbox queue slots each time NS task
acquires a mailbox queue slot.
NS tests can call tfm_ns_mailbox_stats_avg_slots() to calculate
the average number of occupied NS mailbox queue slots each time
NS task acquires a mailbox queue slot. It can prove the feature of
multiple outstanding NS PSA Client calls feature in dual-core
mailbox.
Change-Id: Icd9553996c516901f6b3fc7d68b4c4d0f0f0a5da
Signed-off-by: David Hu <david.hu@arm.com>
diff --git a/interface/include/tfm_mailbox.h b/interface/include/tfm_mailbox.h
index 3cfb1b5..3d128f4 100644
--- a/interface/include/tfm_mailbox.h
+++ b/interface/include/tfm_mailbox.h
@@ -158,6 +158,19 @@
*/
struct ns_mailbox_slot_t queue[NUM_MAILBOX_QUEUE_SLOT];
+
+#ifdef TFM_MULTI_CORE_TEST
+ uint32_t nr_tx; /* The total number of
+ * submission of NS PSA Client
+ * calls from NS task via
+ * mailbox.
+ */
+ uint32_t nr_used_slots; /* The total number of used
+ * mailbox queue slots each time
+ * NS thread requests a mailbox
+ * queue slot.
+ */
+#endif
};
#ifdef __cplusplus
diff --git a/interface/include/tfm_ns_mailbox.h b/interface/include/tfm_ns_mailbox.h
index 48901f0..ba902aa 100644
--- a/interface/include/tfm_ns_mailbox.h
+++ b/interface/include/tfm_ns_mailbox.h
@@ -5,18 +5,35 @@
*
*/
-/* API definitions in NSPE mailbox library */
+/* Data types and API definitions in NSPE mailbox library */
#ifndef __TFM_NS_MAILBOX_H__
#define __TFM_NS_MAILBOX_H__
#include <stdbool.h>
+#include <stdint.h>
#include "tfm_mailbox.h"
#ifdef __cplusplus
extern "C" {
#endif
+#ifdef TFM_MULTI_CORE_TEST
+/**
+ * \brief The structure to hold the statistics result of NSPE mailbox
+ */
+struct ns_mailbox_stats_res_t {
+ uint8_t avg_nr_slots; /* The value before the decimal point
+ * in the average number of NSPE
+ * mailbox slots in use.
+ */
+ uint8_t avg_nr_slots_tenths; /* The first digit value after the
+ * decimal point in the average
+ * number of NSPE mailbox slots in use.
+ */
+};
+#endif
+
/**
* \brief Prepare and send PSA client request to SPE via mailbox.
*
@@ -189,6 +206,29 @@
void tfm_ns_mailbox_hal_wait_reply(mailbox_msg_handle_t handle);
#endif
+#ifdef TFM_MULTI_CORE_TEST
+/**
+ * \brief Initialize the statistics module in TF-M NSPE mailbox.
+ *
+ * \note This function is only available when multi-core tests are enabled.
+ */
+void tfm_ns_mailbox_tx_stats_init(void);
+
+/**
+ * \brief Calculate the average number of used NS mailbox queue slots each time
+ * NS task requires a queue slot to submit mailbox message, which is
+ * recorded in NS mailbox statisitics module.
+ *
+ * \note This function is only available when multi-core tests are enabled.
+ *
+ * \param[in] stats_res The buffer to be written with
+ * \ref ns_mailbox_stats_res_t.
+ *
+ * \return Return the calculation result.
+ */
+void tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t *stats_res);
+#endif
+
#ifdef __cplusplus
}
#endif
diff --git a/interface/src/tfm_ns_mailbox.c b/interface/src/tfm_ns_mailbox.c
index 2690bd4..0b96e65 100644
--- a/interface/src/tfm_ns_mailbox.c
+++ b/interface/src/tfm_ns_mailbox.c
@@ -121,6 +121,70 @@
}
}
+#ifdef TFM_MULTI_CORE_TEST
+void tfm_ns_mailbox_tx_stats_init(void)
+{
+ if (!mailbox_queue_ptr) {
+ return;
+ }
+
+ tfm_ns_mailbox_hal_enter_critical();
+
+ mailbox_queue_ptr->nr_tx = 0;
+ mailbox_queue_ptr->nr_used_slots = 0;
+
+ tfm_ns_mailbox_hal_exit_critical();
+}
+
+static void mailbox_tx_stats_update(struct ns_mailbox_queue_t *ns_queue)
+{
+ mailbox_queue_status_t empty_status;
+ uint8_t idx, nr_empty = 0;
+
+ if (!ns_queue) {
+ return;
+ }
+
+ tfm_ns_mailbox_hal_enter_critical();
+
+ ns_queue->nr_tx++;
+
+ /* Count the number of used slots when this tx arrives */
+ empty_status = ns_queue->empty_slots;
+ tfm_ns_mailbox_hal_exit_critical();
+
+ if (empty_status) {
+ for (idx = 0; idx < NUM_MAILBOX_QUEUE_SLOT; idx++) {
+ if (empty_status & (0x1UL << idx)) {
+ nr_empty++;
+ }
+ }
+ }
+
+ tfm_ns_mailbox_hal_enter_critical();
+ ns_queue->nr_used_slots += (NUM_MAILBOX_QUEUE_SLOT - nr_empty);
+ tfm_ns_mailbox_hal_exit_critical();
+}
+
+void tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t *stats_res)
+{
+ uint32_t nr_used_slots, nr_tx;
+
+ if (!mailbox_queue_ptr || !stats_res) {
+ return;
+ }
+
+ tfm_ns_mailbox_hal_enter_critical();
+ nr_used_slots = mailbox_queue_ptr->nr_used_slots;
+ nr_tx = mailbox_queue_ptr->nr_tx;
+ tfm_ns_mailbox_hal_exit_critical();
+
+ stats_res->avg_nr_slots = nr_used_slots / nr_tx;
+ nr_used_slots %= nr_tx;
+ stats_res->avg_nr_slots_tenths = nr_used_slots * 10 / nr_tx;
+}
+#endif
+
mailbox_msg_handle_t tfm_ns_mailbox_tx_client_req(uint32_t call_type,
const struct psa_client_params_t *params,
int32_t client_id)
@@ -143,6 +207,10 @@
return MAILBOX_QUEUE_FULL;
}
+#ifdef TFM_MULTI_CORE_TEST
+ mailbox_tx_stats_update(mailbox_queue_ptr);
+#endif
+
/* Fill the mailbox message */
msg_ptr = &mailbox_queue_ptr->queue[idx].msg;
@@ -311,6 +379,10 @@
/* Platform specific initialization. */
ret = tfm_ns_mailbox_hal_init(queue);
+#ifdef TFM_MULTI_CORE_TEST
+ tfm_ns_mailbox_tx_stats_init();
+#endif
+
return ret;
}