Skip to contentSkip to conversation

Hexagon: performance improvements - tlb, l2vic

PATCH In review Brian Cain 2 participants 6 messages Oct 21

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v2 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.

In v2:
- clarified the comment in l2vic_update_all()

v1: https://lore.kernel.org/qemu-devel/[email protected]/

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              | 17 ++++++++++++++---
 target/hexagon/cpu_helper.c      | 17 +++--------------
 target/hexagon/hex_mmu.c         | 28 +++++++++++++++++++++++-----
 target/hexagon/op_helper.c       |  6 ++++++
 10 files changed, 80 insertions(+), 23 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 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
index 56aa0b2beed..355ba3888c0 100644
@@ -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
index c1a944920e8..a0162faa9dc 100644
@@ -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
index 3760b671adf..3a850973800 100644
@@ -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
index 28591417040..d354d49f72e 100644
@@ -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
index 2cfcd10949a..0c49404448c 100644
@@ -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
index 28601b78a6f..3e72026d62a 100644
@@ -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
index f93dd3d82d4..2e0e4ab705b 100644
@@ -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
index 9850dc07289..a7a63de57cf 100644
@@ -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
author
#2 lore raw

PATCH v2 2/3 target/hexagon: only flush the softtlb when SSR.ASID changes

In reply to 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 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
index 0c49404448c..9b6ea5b967b 100644
@@ -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
index 6dc06401ad5..64d8a2cad78 100644
@@ -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
index 2e0e4ab705b..9a0e29ae212 100644
@@ -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
author
#3 lore raw

PATCH v2 3/3 hw/intc/hex-l2vic: find the next pending irq a word at a time

In reply to Brian Cain

l2vic_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 | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
hw/intc/hex-l2vic.c +14 -3
index 736f1be3b1d..d92ff5eec21 100644
@@ -290,9 +290,20 @@ 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;
+    }
+
+    /*
+     * Quickly find lowest-numbered irq ready by scanning their statuses
+     * range by range instead of one by one.
+     */
+    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
Reviewed-by on v2 3/3
#4 lore raw

Re: [PATCH v2 3/3] hw/intc/hex-l2vic: find the next pending irq a word at a time

Quoted 4 lines · Brian Cain

l2vic_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.

Quoted 9 lines

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 | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
author
#5 lore raw
In reply to Brian Cain
Quoted 6 lines · Brian Cain

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.

In v2:
- clarified the comment in l2vic_update_all()

Whoops - forgot to update the trailers before sending v2.  I'll update these for v3 or the pull req.

The only one without a R-b is #3.

Quoted 20 lines

v1: https://lore.kernel.org/qemu-devel/[email protected]/

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              | 17 ++++++++++++++---
  target/hexagon/cpu_helper.c      | 17 +++--------------
  target/hexagon/hex_mmu.c         | 28 +++++++++++++++++++++++-----
  target/hexagon/op_helper.c       |  6 ++++++
  10 files changed, 80 insertions(+), 23 deletions(-)