[PATCH 2/2] target/loongarch: Skip global TLB when calculating replaced TLB

Bibo Mao posted 2 patches 1 month ago
Maintainers: Song Gao <gaosong@loongson.cn>
[PATCH 2/2] target/loongarch: Skip global TLB when calculating replaced TLB
Posted by Bibo Mao 1 month ago
When new TLB entry is added, TLB index is calculated from invalid
entry at first and then from different ASID, and randomly at last.

With different ASID, global TLB should be skipped since ASID is not
useful when global TLB is added.

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
---
 target/loongarch/tcg/tlb_helper.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_helper.c
index f8fada5b9a..f1d183cb64 100644
--- a/target/loongarch/tcg/tlb_helper.c
+++ b/target/loongarch/tcg/tlb_helper.c
@@ -371,7 +371,7 @@ void helper_tlbfill(CPULoongArchState *env)
     uint16_t pagesize, stlb_ps;
     uint16_t asid, tlb_asid;
     LoongArchTLB *tlb;
-    uint8_t tlb_e;
+    uint8_t tlb_e, tlb_g;
 
     if (FIELD_EX64(env->CSR_TLBRERA, CSR_TLBRERA, ISTLBR)) {
         entryhi = env->CSR_TLBREHI;
@@ -400,7 +400,8 @@ void helper_tlbfill(CPULoongArchState *env)
             }
 
             tlb_asid = FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID);
-            if (asid != tlb_asid) {
+            tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
+            if (tlb_g == 0 && asid != tlb_asid) {
                 set = i;
             }
         }
@@ -423,7 +424,8 @@ void helper_tlbfill(CPULoongArchState *env)
             }
 
             tlb_asid = FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID);
-            if (asid != tlb_asid) {
+            tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
+            if (tlb_g == 0 && asid != tlb_asid) {
                 index = i;
             }
         }
-- 
2.39.3
Re: [PATCH 2/2] target/loongarch: Skip global TLB when calculating replaced TLB
Posted by gaosong 1 month ago
在 2025/10/9 上午10:59, Bibo Mao 写道:
> When new TLB entry is added, TLB index is calculated from invalid
> entry at first and then from different ASID, and randomly at last.
>
> With different ASID, global TLB should be skipped since ASID is not
> useful when global TLB is added.
>
> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
> ---
>   target/loongarch/tcg/tlb_helper.c | 8 +++++---
>   1 file changed, 5 insertions(+), 3 deletions(-)
Signed-off-by: Song Gao <gaosong@loongson.cn>

Thanks.
Song Gao
> diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_helper.c
> index f8fada5b9a..f1d183cb64 100644
> --- a/target/loongarch/tcg/tlb_helper.c
> +++ b/target/loongarch/tcg/tlb_helper.c
> @@ -371,7 +371,7 @@ void helper_tlbfill(CPULoongArchState *env)
>       uint16_t pagesize, stlb_ps;
>       uint16_t asid, tlb_asid;
>       LoongArchTLB *tlb;
> -    uint8_t tlb_e;
> +    uint8_t tlb_e, tlb_g;
>   
>       if (FIELD_EX64(env->CSR_TLBRERA, CSR_TLBRERA, ISTLBR)) {
>           entryhi = env->CSR_TLBREHI;
> @@ -400,7 +400,8 @@ void helper_tlbfill(CPULoongArchState *env)
>               }
>   
>               tlb_asid = FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID);
> -            if (asid != tlb_asid) {
> +            tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
> +            if (tlb_g == 0 && asid != tlb_asid) {
>                   set = i;
>               }
>           }
> @@ -423,7 +424,8 @@ void helper_tlbfill(CPULoongArchState *env)
>               }
>   
>               tlb_asid = FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID);
> -            if (asid != tlb_asid) {
> +            tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
> +            if (tlb_g == 0 && asid != tlb_asid) {
>                   index = i;
>               }
>           }