lib: sbi: Optimize sbi_tlb
PATCH v2 1/3 lib: sbi: Optimize sbi_tlb
Originally, the process and sync of sbi_tlb need to wait for each other. Evasion by atomic addition and subtraction.
Signed-off-by Xiang W <[email protected]>
--- lib/sbi/sbi_tlb.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
lib/sbi/sbi_tlb.c +10 -6
@@ -215,7 +215,7 @@ static void tlb_entry_process(struct sbi_tlb_info *tinfo) {u32 rhartid;struct sbi_scratch *rscratch =NULL ; -unsigned long *rtlb_sync =NULL ; +atomic_t *rtlb_sync =NULL ; tinfo->local_fn (tinfo);
@@ -225,7 +225,7 @@ static void tlb_entry_process(struct sbi_tlb_info *tinfo)continue ; rtlb_sync =sbi_scratch_offset_ptr (rscratch, tlb_sync_off); -while (atomic_raw_xchg_ulong (rtlb_sync,1 )) ; +atomic_sub_return (rtlb_sync,1 ); } }
@@ -257,10 +257,10 @@ static void tlb_process(struct sbi_scratch *scratch)static void tlb_sync (struct sbi_scratch *scratch) { -unsigned long *tlb_sync = +atomic_t *tlb_sync =sbi_scratch_offset_ptr (scratch, tlb_sync_off); -while (!atomic_raw_xchg_ulong (tlb_sync,0 )) { +while (atomic_read (tlb_sync) >0 ) {/* * While we are waiting for remote hart to set the sync, * consume fifo requests to avoid deadlock.
@@ -343,6 +343,7 @@ static int tlb_update(struct sbi_scratch *scratch, u32 remote_hartid, void *data) { int ret; +atomic_t *tlb_sync;struct sbi_fifo *tlb_fifo_r;struct sbi_tlb_info *tinfo = data;u32 curr_hartid =current_hartid ();
@@ -387,6 +388,9 @@ static int tlb_update(struct sbi_scratch *scratch, curr_hartid, remote_hartid); } + tlb_sync =sbi_scratch_offset_ptr (scratch, tlb_sync_off); +atomic_add_return (tlb_sync,1 ); +return 0 ; }
@@ -413,7 +417,7 @@ int sbi_tlb_init(struct sbi_scratch *scratch, bool cold_boot) {int ret;void *tlb_mem; -unsigned long *tlb_sync; +atomic_t *tlb_sync;struct sbi_fifo *tlb_q;const struct sbi_platform *plat =sbi_platform_ptr (scratch);
@@ -455,7 +459,7 @@ int sbi_tlb_init(struct sbi_scratch *scratch, bool cold_boot) tlb_q =sbi_scratch_offset_ptr (scratch, tlb_fifo_off); tlb_mem =sbi_scratch_offset_ptr (scratch, tlb_fifo_mem_off); - *tlb_sync =0 ; + tlb_sync->counter =0 ;sbi_fifo_init (tlb_q, tlb_mem,SBI_TLB_FIFO_NUM_ENTRIES ,SBI_TLB_INFO_SIZE );
Signature · 1 line
2.39.0
PATCH v2 2/3 lib: sbi: Optimize sbi_ipi
the original sbI_ipi will be processed by hart by hart, after optimization, send ipi first and finally wait together.
Signed-off-by Xiang W <[email protected]>
--- lib/sbi/sbi_ipi.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-)
lib/sbi/sbi_ipi.c +39 -6
@@ -31,7 +31,7 @@ static unsigned long ipi_data_off;static const struct sbi_ipi_device *ipi_dev =NULL ;static const struct sbi_ipi_event_ops *ipi_ops_array[SBI_IPI_EVENT_MAX ]; -static int sbi_ipi_send (struct sbi_scratch *scratch,u32 remote_hartid, +static int sbi_ipi_update (struct sbi_scratch *scratch,u32 remote_hartid,u32 event,void *data) {int ret;
@@ -75,6 +75,21 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartid,return 0 ; } +static int sbi_ipi_sync (struct sbi_scratch *scratch,u32 event) +{ +const struct sbi_ipi_event_ops *ipi_ops; + +if ((SBI_IPI_EVENT_MAX <= event) || + !ipi_ops_array[event]) +return SBI_EINVAL ; + ipi_ops = ipi_ops_array[event]; + +if (ipi_ops->sync) + ipi_ops->sync (scratch); + +return 0 ; +} +/** * As this this function only handlers scalar values of hart mask, it must be * set to all online harts if the intention is to send IPIs to all the harts.
@@ -83,7 +98,7 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartid,int sbi_ipi_send_many (ulong hmask,ulong hbase,u32 event,void *data) {int rc; -ulong i, m; +ulong i, m, n;struct sbi_domain *dom =sbi_domain_thishart_ptr ();struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();
@@ -92,19 +107,37 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)if (rc)return rc; m &= hmask; + n = m; -/* Send IPIs */ +/* update IPIs */ for (i = hbase; m; i++, m >>=1 ) {if (m &1UL ) -sbi_ipi_send (scratch, i, event, data); +sbi_ipi_update (scratch, i, event, data); + } + +/* sync IPIs */ + m = n; +for (i = hbase; m; i++, m >>=1 ) { +if (m &1UL ) +sbi_ipi_sync (scratch, event); } }else { +/* update IPIs */ + hbase =0 ; +while (!sbi_hsm_hart_interruptible_mask (dom, hbase, &m)) { +for (i = hbase; m; i++, m >>=1 ) { +if (m &1UL ) +sbi_ipi_update (scratch, i, event, data); + } + hbase +=BITS_PER_LONG ; + } + +/* sync IPIs */ hbase =0 ;while (!sbi_hsm_hart_interruptible_mask (dom, hbase, &m)) { -/* Send IPIs */ for (i = hbase; m; i++, m >>=1 ) {if (m &1UL ) -sbi_ipi_send (scratch, i, event, data); +sbi_ipi_sync (scratch, event); } hbase +=BITS_PER_LONG ; }
Signature · 1 line
2.39.0
PATCH v2 3/3 lib: sbi: rename function of sbi_ipi
In reply to Xiang WRenamed some functions and reused some code
Signed-off-by Xiang W <[email protected]>
--- lib/sbi/sbi_tlb.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-)
lib/sbi/sbi_tlb.c +10 -17
@@ -211,7 +211,7 @@ static void tlb_pmu_incr_fw_ctr(struct sbi_tlb_info *data)sbi_pmu_ctr_incr_fw (SBI_PMU_FW_HFENCE_VVMA_ASID_SENT ); } -static void tlb_entry_process (struct sbi_tlb_info *tinfo) +static void tlb_process_helper (struct sbi_tlb_info *tinfo) {u32 rhartid;struct sbi_scratch *rscratch =NULL ;
@@ -229,30 +229,23 @@ static void tlb_entry_process(struct sbi_tlb_info *tinfo) } } -static void tlb_process_count (struct sbi_scratch *scratch,int count) +static int tlb_process_once (struct sbi_scratch *scratch) {struct sbi_tlb_info tinfo; -unsigned int deq_count =0 ;struct sbi_fifo *tlb_fifo =sbi_scratch_offset_ptr (scratch, tlb_fifo_off); -while (!sbi_fifo_dequeue (tlb_fifo, &tinfo)) { -tlb_entry_process (&tinfo); - deq_count++; -if (deq_count > count) -break ; - +if (!sbi_fifo_dequeue (tlb_fifo, &tinfo)) { +tlb_process_helper (&tinfo); +return 0 ; } + +return -1 ; }static void tlb_process (struct sbi_scratch *scratch) { -struct sbi_tlb_info tinfo; -struct sbi_fifo *tlb_fifo = -sbi_scratch_offset_ptr (scratch, tlb_fifo_off); - -while (!sbi_fifo_dequeue (tlb_fifo, &tinfo)) -tlb_entry_process (&tinfo); +while (!tlb_process_once (scratch)); }static void tlb_sync (struct sbi_scratch *scratch)
@@ -265,7 +258,7 @@ static void tlb_sync(struct sbi_scratch *scratch)* While we are waiting for remote hart to set the sync, * consume fifo requests to avoid deadlock. */ -tlb_process_count (scratch,1 ); +tlb_process_once (scratch); }return ;
@@ -383,7 +376,7 @@ static int tlb_update(struct sbi_scratch *scratch,* TODO: Introduce a wait/wakeup event mechanism to handle * this properly. */ -tlb_process_count (scratch,1 ); +tlb_process_once (scratch);sbi_dprintf ("hart%d: hart%d tlb fifo full\n" , curr_hartid, remote_hartid); }
Signature · 1 line
2.39.0