1 /* 2 * Copyright (c) 2020-2021, Arm Limited. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 8 #include "tfm_ns_mailbox.h" 9 #include "tfm_ns_mailbox_test.h" 10 11 static struct ns_mailbox_queue_t *stats_queue_ptr = NULL; 12 tfm_ns_mailbox_tx_stats_init(struct ns_mailbox_queue_t * ns_queue)13void tfm_ns_mailbox_tx_stats_init(struct ns_mailbox_queue_t *ns_queue) 14 { 15 if (!ns_queue) { 16 return; 17 } 18 19 ns_queue->nr_tx = 0; 20 ns_queue->nr_used_slots = 0; 21 22 stats_queue_ptr = ns_queue; 23 } 24 tfm_ns_mailbox_tx_stats_reinit(void)25int32_t tfm_ns_mailbox_tx_stats_reinit(void) 26 { 27 if (!stats_queue_ptr) { 28 return MAILBOX_INVAL_PARAMS; 29 } 30 31 stats_queue_ptr->nr_tx = 0; 32 stats_queue_ptr->nr_used_slots = 0; 33 34 return MAILBOX_SUCCESS; 35 } 36 tfm_ns_mailbox_tx_stats_update(void)37void tfm_ns_mailbox_tx_stats_update(void) 38 { 39 mailbox_queue_status_t empty_status; 40 uint8_t idx, nr_empty = 0; 41 42 if (!stats_queue_ptr) { 43 return; 44 } 45 46 tfm_ns_mailbox_os_spin_lock(); 47 /* Count the number of used slots when this tx arrives */ 48 empty_status = stats_queue_ptr->empty_slots; 49 tfm_ns_mailbox_os_spin_unlock(); 50 51 if (empty_status) { 52 for (idx = 0; idx < NUM_MAILBOX_QUEUE_SLOT; idx++) { 53 if (empty_status & (0x1UL << idx)) { 54 nr_empty++; 55 } 56 } 57 } 58 59 tfm_ns_mailbox_os_spin_lock(); 60 stats_queue_ptr->nr_used_slots += (NUM_MAILBOX_QUEUE_SLOT - nr_empty); 61 stats_queue_ptr->nr_tx++; 62 tfm_ns_mailbox_os_spin_unlock(); 63 } 64 tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t * stats_res)65void tfm_ns_mailbox_stats_avg_slot(struct ns_mailbox_stats_res_t *stats_res) 66 { 67 uint32_t nr_used_slots, nr_tx; 68 69 if (!stats_queue_ptr || !stats_res) { 70 return; 71 } 72 73 nr_used_slots = stats_queue_ptr->nr_used_slots; 74 nr_tx = stats_queue_ptr->nr_tx; 75 76 stats_res->avg_nr_slots = nr_used_slots / nr_tx; 77 nr_used_slots %= nr_tx; 78 stats_res->avg_nr_slots_tenths = nr_used_slots * 10 / nr_tx; 79 } 80