Hexagon: performance improvements - tlb, l2vic
cover Hexagon: performance improvements - tlb, l2vic
In this series we have significant performance improvements for use cases like booting linux. The gains come from avoiding redundant TLB flushes and optimizing the critical path of the interrupt controller scan loop.
Brian Cain (3):
target/hexagon: flush the softtlb once per tlbinvasid target/hexagon: only flush the softtlb when SSR.ASID changes hw/intc/hex-l2vic: find the next pending irq a word at a time
include/hw/hexagon/hexagon_tlb.h | 2 ++ target/hexagon/cpu.h | 8 ++++++++ target/hexagon/gen_tcg_sys.h | 3 +++ target/hexagon/helper.h | 1 + target/hexagon/hex_mmu.h | 3 ++- hw/hexagon/hexagon_tlb.c | 18 ++++++++++++++++++ hw/intc/hex-l2vic.c | 14 +++++++++++--- target/hexagon/cpu_helper.c | 17 +++-------------- target/hexagon/hex_mmu.c | 28 +++++++++++++++++++++++----- target/hexagon/op_helper.c | 6 ++++++ 10 files changed, 77 insertions(+), 23 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 target/hexagon: flush the softtlb once per tlbinvasid
tlbinvasid cleared each matching JTLB entry via fTLBW(), and these would hit hex_tlbw()'s flush. tlbinvasid could trigger several full flushes.
Since the softtlb isn't ASID-tagged, clear all matching valid bits first and flush once.
The hardware threads share one JTLB but have their own softtlb, so the flush has to be broadcast to every CPU rather than just the one running the invalidation.
--- include/hw/hexagon/hexagon_tlb.h | 2 ++ target/hexagon/cpu.h | 8 ++++++++ target/hexagon/gen_tcg_sys.h | 3 +++ target/hexagon/helper.h | 1 + target/hexagon/hex_mmu.h | 1 + hw/hexagon/hexagon_tlb.c | 18 ++++++++++++++++++ target/hexagon/hex_mmu.c | 21 +++++++++++++++++++-- target/hexagon/op_helper.c | 6 ++++++ 8 files changed, 58 insertions(+), 2 deletions(-)
include/hw/hexagon/hexagon_tlb.h +2 -0
@@ -30,6 +30,8 @@ uint64_t hexagon_tlb_read(HexagonTLBState *tlb, uint32_t index);uint64_t hexagon_tlb_write (HexagonTLBState *tlb,uint32_t index,uint64_t value); +uint32_t hexagon_tlb_invalidate_asid (HexagonTLBState *tlb,uint32_t asid); + bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid, uint32_t VA, MMUAccessType access_type, hwaddr *PA, int *prot, uint64_t *size,
target/hexagon/cpu.h +8 -0
@@ -76,6 +76,14 @@ typedef struct HexagonHVXContextState HexagonHVXContextState;#define MMU_GUEST_IDX 1#define MMU_KERNEL_IDX 2 +/* +* TLB entry match is by ASID and global bit, neither of which is encoded in +* mmu_idx, so a JTLB change has to be flushed out of every mmu_idx. +*/ +#define MMU_IDX_ALL ((1 << MMU_USER_IDX) | \ + (1 << MMU_GUEST_IDX) | \ + (1 << MMU_KERNEL_IDX)) + typedef enum { HEX_LOCK_UNLOCKED = 0, HEX_LOCK_WAITING = 1,
target/hexagon/gen_tcg_sys.h +3 -0
@@ -20,6 +20,9 @@#define fGEN_TCG_Y4_siad (SHORTCODE ) \ gen_helper_siad(tcg_env, RsV) +#define fGEN_TCG_Y5_tlbasidi (SHORTCODE ) \ + gen_helper_tlbinvasid(tcg_env, RsV) +#define fGEN_TCG_Y2_iassignw (SHORTCODE ) \ gen_helper_iassignw(tcg_env, RsV)
target/hexagon/helper.h +1 -0
@@ -143,4 +143,5 @@ DEF_HELPER_2(resume, void, env, i32) DEF_HELPER_1(resched, void, env) DEF_HELPER_3(modify_ssr, void, env, i32, i32) DEF_HELPER_1(pending_interrupt, void, env) +DEF_HELPER_2(tlbinvasid, void, env, i32)#endif
target/hexagon/hex_mmu.h +1 -0
@@ -13,6 +13,7 @@#include "monitor/hmp.h" extern void hex_tlbw (CPUHexagonState *env,uint32_t index,uint64_t value); +extern void hex_tlb_invalidate_asid (CPUHexagonState *env,uint32_t asid);extern uint32_t hex_tlb_lookup (CPUHexagonState *env,uint32_t ssr,uint32_t VA );extern void hex_mmu_on (CPUHexagonState *env);extern void hex_mmu_off (CPUHexagonState *env);
hw/hexagon/hexagon_tlb.c +18 -0
@@ -321,6 +321,24 @@ uint64_t hexagon_tlb_write(HexagonTLBState *tlb, uint32_t index,return old; } +uint32_t hexagon_tlb_invalidate_asid (HexagonTLBState *tlb,uint32_t asid) +{ +uint32_t invalidated =0 ; + +for (uint32_t i =0 ; i < tlb->num_entries; i++) { +uint64_t entry = tlb->entries[i]; + +if (!GET_PTE_V (entry) ||GET_PTE_G (entry) || +GET_PTE_ASID (entry) != asid) { +continue ; + } + tlb->entries[i] =deposit64 (entry,63 ,1 ,0 ); + invalidated++; + } + +return invalidated; +} + bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid, uint32_t VA, MMUAccessType access_type, hwaddr *PA, int *prot, uint64_t *size,
target/hexagon/hex_mmu.c +19 -2
@@ -29,6 +29,11 @@ static inline void hex_log_tlbw(uint32_t index, uint64_t entry) index, entry); } +static void hex_tlb_flush_all (CPUState *cs) +{ +tlb_flush_by_mmuidx_all_cpus_synced (cs,MMU_IDX_ALL ); +} +void hex_tlbw (CPUHexagonState *env,uint32_t index,uint64_t value) {uint32_t myidx =fTLB_NONPOW2WRAP (fTLB_IDXMASK (index));
@@ -36,12 +41,24 @@ void hex_tlbw(CPUHexagonState *env, uint32_t index, uint64_t value)uint64_t old_entry =hexagon_tlb_write (tlb, myidx, value);bool old_entry_valid =extract64 (old_entry,63 ,1 );if (old_entry_valid &&hexagon_cpu_mmu_enabled (env)) { -CPUState *cs =env_cpu (env); -tlb_flush_all_cpus_synced (cs); +hex_tlb_flush_all (env_cpu (env)); }hex_log_tlbw (myidx, value); } +void hex_tlb_invalidate_asid (CPUHexagonState *env,uint32_t asid) +{ +HexagonTLBState *tlb =env_archcpu (env)->tlb; +uint32_t invalidated =hexagon_tlb_invalidate_asid (tlb, asid); + +qemu_log_mask (CPU_LOG_MMU , +"tlbinvasid: asid 0x%02" PRIx32", %" PRIu32" entries\n" , + asid, invalidated); +if (invalidated &&hexagon_cpu_mmu_enabled (env)) { +hex_tlb_flush_all (env_cpu (env)); + } +} +void hex_mmu_on (CPUHexagonState *env) {CPUState *cs =env_cpu (env);
target/hexagon/op_helper.c +6 -0
@@ -1663,6 +1663,12 @@ void HELPER(siad)(CPUHexagonState *env, uint32_t mask)hex_interrupt_update (env); } +void HELPER (tlbinvasid )(CPUHexagonState *env,uint32_t RsV) +{ +BQL_LOCK_GUARD (); +hex_tlb_invalidate_asid (env,extract32 (RsV,20 ,7 )); +} +void HELPER (swi )(CPUHexagonState *env,uint32_t mask) {BQL_LOCK_GUARD ();
Signature · 1 line
2.34.1
Re: [PATCH 1/3] target/hexagon: flush the softtlb once per tlbinvasid
Quoted 11 lines · Brian Cain
tlbinvasid cleared each matching JTLB entry via
fTLBW(), and these would hit hex_tlbw()'s flush. tlbinvasid could trigger several full flushes.Since the softtlb isn't ASID-tagged, clear all matching valid bits first and flush once.
The hardware threads share one JTLB but have their own softtlb, so the flush has to be broadcast to every CPU rather than just the one running the invalidation.
Signed-off-by Brian Cain <[email protected]>
Quoted 11 lines
--- include/hw/hexagon/hexagon_tlb.h | 2 ++ target/hexagon/cpu.h | 8 ++++++++ target/hexagon/gen_tcg_sys.h | 3 +++ target/hexagon/helper.h | 1 + target/hexagon/hex_mmu.h | 1 + hw/hexagon/hexagon_tlb.c | 18 ++++++++++++++++++ target/hexagon/hex_mmu.c | 21 +++++++++++++++++++-- target/hexagon/op_helper.c | 6 ++++++ 8 files changed, 58 insertions(+), 2 deletions(-)
PATCH 2/3 target/hexagon: only flush the softtlb when SSR.ASID changes
In reply to Brian Cainhexagon_modify_ssr() flushed the softtlb on any change to SSR.EX, UM, GM, or ASID, but only ASID affects translation. EX/UM/GM just pick the mode, which is already captured in mmu_idx.
EX toggles on every exception entry/rte, so the flush created a feedback loop where a TLB miss wiped the softtlb and guaranteed the next accesses missed too.
Rename hex_mmu_mode_change() to hex_mmu_asid_change() to match what now triggers it, and narrow it to this thread's mmu indices: the ASID lives in the thread's private SSR, so other CPUs are unaffected.
--- target/hexagon/hex_mmu.h | 2 +- target/hexagon/cpu_helper.c | 17 +++-------------- target/hexagon/hex_mmu.c | 7 ++++--- 3 files changed, 8 insertions(+), 18 deletions(-)
target/hexagon/hex_mmu.h +1 -1
@@ -17,7 +17,7 @@ extern void hex_tlb_invalidate_asid(CPUHexagonState *env, uint32_t asid);extern uint32_t hex_tlb_lookup (CPUHexagonState *env,uint32_t ssr,uint32_t VA );extern void hex_mmu_on (CPUHexagonState *env);extern void hex_mmu_off (CPUHexagonState *env); -extern void hex_mmu_mode_change (CPUHexagonState *env); +extern void hex_mmu_asid_change (CPUHexagonState *env);extern bool hex_tlb_find_match (CPUHexagonState *env,uint32_t VA ,MMUAccessType access_type,hwaddr *PA ,int *prot,uint64_t *size,int32_t *excp,int mmu_idx);
target/hexagon/cpu_helper.c +3 -14
@@ -231,27 +231,17 @@ unsigned hexagon_hvx_select_context(CPUHexagonState *env, uint32_t ssr)void hexagon_modify_ssr (CPUHexagonState *env,uint32_t new,uint32_t old) { -bool old_EX, old_UM, old_GM, old_IE; -bool new_EX, new_UM, new_GM, new_IE; +bool old_EX, old_IE; +bool new_EX, new_IE;uint8_t old_asid, new_asid;g_assert (bql_locked ()); old_EX =GET_SSR_FIELD (SSR_EX , old); - old_UM =GET_SSR_FIELD (SSR_UM , old); - old_GM =GET_SSR_FIELD (SSR_GM , old); old_IE =GET_SSR_FIELD (SSR_IE , old); new_EX =GET_SSR_FIELD (SSR_EX , new); - new_UM =GET_SSR_FIELD (SSR_UM , new); - new_GM =GET_SSR_FIELD (SSR_GM , new); new_IE =GET_SSR_FIELD (SSR_IE , new); -if ((old_EX != new_EX) || - (old_UM != new_UM) || - (old_GM != new_GM)) { -hex_mmu_mode_change (env); - } -bool xa_changed =GET_SSR_FIELD (SSR_XA , new) !=GET_SSR_FIELD (SSR_XA , old);bool xe_changed =GET_SSR_FIELD (SSR_XE , new) !=GET_SSR_FIELD (SSR_XE , old);
@@ -267,8 +257,7 @@ void hexagon_modify_ssr(CPUHexagonState *env, uint32_t new, uint32_t old) old_asid =GET_SSR_FIELD (SSR_ASID , old); new_asid =GET_SSR_FIELD (SSR_ASID , new);if (new_asid != old_asid) { -CPUState *cs =env_cpu (env); -tlb_flush (cs); +hex_mmu_asid_change (env); }/* See if the interrupts have been enabled or we have exited EX mode */
target/hexagon/hex_mmu.c +4 -3
@@ -73,11 +73,12 @@ void hex_mmu_off(CPUHexagonState *env)tlb_flush (cs); } -void hex_mmu_mode_change (CPUHexagonState *env) +void hex_mmu_asid_change (CPUHexagonState *env) { -qemu_log_mask (CPU_LOG_MMU ,"Hexagon mode change!\n" );CPUState *cs =env_cpu (env); -tlb_flush (cs); + +qemu_log_mask (CPU_LOG_MMU ,"Hexagon ASID change!\n" ); +tlb_flush_by_mmuidx (cs,MMU_IDX_ALL ); } bool hex_tlb_find_match(CPUHexagonState *env, uint32_t VA,
Signature · 1 line
2.34.1
Re: [PATCH 2/3] target/hexagon: only flush the softtlb when SSR.ASID changes
Quoted 12 lines · Brian Cain
hexagon_modify_ssr()flushed the softtlb on any change to SSR.EX, UM, GM, or ASID, but only ASID affects translation. EX/UM/GM just pick the mode, which is already captured inmmu_idx.EX toggles on every exception entry/rte, so the flush created a feedback loop where a TLB miss wiped the softtlb and guaranteed the next accesses missed too.
Rename
hex_mmu_mode_change()tohex_mmu_asid_change()to match what now triggers it, and narrow it to this thread's mmu indices: the ASID lives in the thread's private SSR, so other CPUs are unaffected.
Quoted 7 lines
Signed-off-by Brian Cain <[email protected]>--- target/hexagon/hex_mmu.h | 2 +- target/hexagon/cpu_helper.c | 17 +++-------------- target/hexagon/hex_mmu.c | 7 ++++--- 3 files changed, 8 insertions(+), 18 deletions(-)
PATCH 3/3 hw/intc/hex-l2vic: find the next pending irq a word at a time
In reply to Brian Cainl2vic_update_all() called l2vic_update() for each of the 1024 irqs, and every call rescanned all of int_status for an active vid. ciad runs it on each interrupt return, so this was a hot function for an OS boot.
Check once whether a vid is active, then deliver the lowest irq that is both pending and enabled by scanning int_pending & int_enable a word at a time. The delivered irq is unchanged.
--- hw/intc/hex-l2vic.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
hw/intc/hex-l2vic.c +11 -3
@@ -290,9 +290,17 @@ static bool l2vic_update(HexL2VICState *s, int irq)static void l2vic_update_all (HexL2VICState *s) { -for (int i =0 ; i <L2VIC_INTERRUPT_MAX ; i++) { -if (l2vic_update (s, i)) { -/* once vid is active, no-one else can set it until ciad */ +/* once vid is active, no-one else can set it until ciad */ +if (vid_active (s)) { +return ; + } + +/* Deliver the lowest-numbered irq that is both pending and enabled. */ +for (int slice =0 ; slice <SLICE_MAX ; slice++) { +uint32_t ready = s->int_pending[slice] & s->int_enable[slice]; + +if (ready) { +l2vic_update (s, slice *32 +ctz32 (ready));return ; } }
Signature · 1 line
2.34.1
Re: [PATCH 3/3] hw/intc/hex-l2vic: find the next pending irq a word at a time
Quoted 8 lines · Brian Cain
l2vic_update_all()calledl2vic_update()for each of the 1024 irqs, and every call rescanned all ofint_statusfor an active vid. ciad runs it on each interrupt return, so this was a hot function for an OS boot.Check once whether a vid is active, then deliver the lowest irq that is both pending and enabled by scanning
int_pending&int_enablea word at a time. The delivered irq is unchanged.
Looking at definition of l2vic_update and vid_active, indeed, calling it everytime is costly and cause a full rescan or irq status.
static inline bool vid_active(HexL2VICState *s)
{
const uint32_t size = L2VIC_INTERRUPT_MAX; => 1024
const uint32_t active_irq = find_first_bit32(s->int_status, size);
return active_irq != size;
}static bool l2vic_update(HexL2VICState *s, int irq)
{
bool pending;
bool enable;
if (vid_active(s)) {
return true;
}
pending = test_bit32(irq, s->int_pending);
enable = test_bit32(irq, s->int_enable);
if (pending && enable) {
...
return true;
}
return false;
}Quoted 10 lines
Signed-off-by Brian Cain <[email protected]>--- hw/intc/hex-l2vic.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)hw/intc/hex-l2vic.c +0
-0
hw/intc/hex-l2vic.c static bool l2vic_update(HexL2VICState *s, int irq) · line 296
Quoted 10 lines
hw/intc/hex-l2vic.c +4
-3@@ -290,9 +290,17 @@ static bool l2vic_update(HexL2VICState *s, int irq)static void l2vic_update_all (HexL2VICState *s) { -for (int i =0 ; i <L2VIC_INTERRUPT_MAX ; i++) { -if (l2vic_update (s, i)) { -/* once vid is active, no-one else can set it until ciad */ +/* once vid is active, no-one else can set it until ciad */ +if (vid_active (s)) { +return ; + }
it's a good optimization to check this before entering the loop.
hw/intc/hex-l2vic.c static bool l2vic_update(HexL2VICState *s, int irq) · line 306
Quoted 10 lines
hw/intc/hex-l2vic.c +7
-0+ +/* Deliver the lowest-numbered irq that is both pending and enabled. */ +for (int slice =0 ; slice <SLICE_MAX ; slice++) { +uint32_t ready = s->int_pending[slice] & s->int_enable[slice]; + +if (ready) { +l2vic_update (s, slice *32 +ctz32 (ready));return ; } }
This second optimization limits loop to 32 iterations instead of the full 1024. Maybe the comment could be explaining this better:
/* Quickly find lowest-numbered irq ready by scanning their statuses range by range instead of one by one */.
Regards,
Pierrick