[PATCH v3 06/12] target/loongarch: Add tlb search callback in loongarch_tlb_search()

Bibo Mao posted 12 patches 3 days, 5 hours ago
Maintainers: Song Gao <gaosong@loongson.cn>
There is a newer version of this series
[PATCH v3 06/12] target/loongarch: Add tlb search callback in loongarch_tlb_search()
Posted by Bibo Mao 3 days, 5 hours ago
With function loongarch_tlb_search(), it is to search TLB entry with
speficied virtual address, the difference is selection with asid and
global bit. Here add selection callback with asid and global bit.

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

diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_helper.c
index 00422f259d..006fe1b207 100644
--- a/target/loongarch/tcg/tlb_helper.c
+++ b/target/loongarch/tcg/tlb_helper.c
@@ -21,6 +21,17 @@
 #include "cpu-csr.h"
 #include "tcg/tcg_loongarch.h"
 
+typedef bool (*tlb_match)(int global, int asid, int tlb_asid);
+
+static bool tlb_match_any(int global, int asid, int tlb_asid)
+{
+    if (global == 1 || tlb_asid == asid) {
+        return true;
+    }
+
+    return false;
+}
+
 bool check_ps(CPULoongArchState *env, uint8_t tlb_ps)
 {
     if (tlb_ps >= 64) {
@@ -204,9 +215,11 @@ static bool loongarch_tlb_search(CPULoongArchState *env, vaddr vaddr,
     uint8_t tlb_e, tlb_ps, tlb_g, stlb_ps;
     int i, compare_shift;
     uint64_t vpn, tlb_vppn;
+    tlb_match func;
 
+    func = tlb_match_any;
     csr_asid = FIELD_EX64(env->CSR_ASID, CSR_ASID, ASID);
-   stlb_ps = FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS);
+    stlb_ps = FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS);
     vpn = (vaddr & TARGET_VIRT_MASK) >> (stlb_ps + 1);
     stlb_idx = vpn & 0xff; /* VA[25:15] <==> TLBIDX.index for 16KiB Page */
     compare_shift = stlb_ps + 1 - R_TLB_MISC_VPPN_SHIFT;
@@ -220,7 +233,7 @@ static bool loongarch_tlb_search(CPULoongArchState *env, vaddr vaddr,
             tlb_asid = FIELD_EX64(tlb->tlb_misc, TLB_MISC, ASID);
             tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
 
-            if ((tlb_g == 1 || tlb_asid == csr_asid) &&
+            if (func(tlb_g, csr_asid, tlb_asid) &&
                 (vpn == (tlb_vppn >> compare_shift))) {
                 *index = i * 256 + stlb_idx;
                 return true;
@@ -239,7 +252,7 @@ static bool loongarch_tlb_search(CPULoongArchState *env, vaddr vaddr,
             tlb_g = FIELD_EX64(tlb->tlb_entry0, TLBENTRY, G);
             compare_shift = tlb_ps + 1 - R_TLB_MISC_VPPN_SHIFT;
             vpn = (vaddr & TARGET_VIRT_MASK) >> (tlb_ps + 1);
-            if ((tlb_g == 1 || tlb_asid == csr_asid) &&
+            if (func(tlb_g, csr_asid, tlb_asid) &&
                 (vpn == (tlb_vppn >> compare_shift))) {
                 *index = i;
                 return true;
-- 
2.39.3
Re: [PATCH v3 06/12] target/loongarch: Add tlb search callback in loongarch_tlb_search()
Posted by Richard Henderson 3 days, 1 hour ago
On 9/3/25 10:48, Bibo Mao wrote:
> With function loongarch_tlb_search(), it is to search TLB entry with
> speficied virtual address, the difference is selection with asid and
> global bit. Here add selection callback with asid and global bit.
> 
> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
> ---
>   target/loongarch/tcg/tlb_helper.c | 19 ++++++++++++++++---
>   1 file changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_helper.c
> index 00422f259d..006fe1b207 100644
> --- a/target/loongarch/tcg/tlb_helper.c
> +++ b/target/loongarch/tcg/tlb_helper.c
> @@ -21,6 +21,17 @@
>   #include "cpu-csr.h"
>   #include "tcg/tcg_loongarch.h"
>   
> +typedef bool (*tlb_match)(int global, int asid, int tlb_asid);

Should global parameter be bool?

> +
> +static bool tlb_match_any(int global, int asid, int tlb_asid)
> +{
> +    if (global == 1 || tlb_asid == asid) {
> +        return true;
> +    }
> +
> +    return false;
> +}

More compact as

     return global || tlb_asid == asid;

Otherwise,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~
Re: [PATCH v3 06/12] target/loongarch: Add tlb search callback in loongarch_tlb_search()
Posted by Bibo Mao 2 days, 6 hours ago

On 2025/9/3 下午9:14, Richard Henderson wrote:
> On 9/3/25 10:48, Bibo Mao wrote:
>> With function loongarch_tlb_search(), it is to search TLB entry with
>> speficied virtual address, the difference is selection with asid and
>> global bit. Here add selection callback with asid and global bit.
>>
>> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
>> ---
>>   target/loongarch/tcg/tlb_helper.c | 19 ++++++++++++++++---
>>   1 file changed, 16 insertions(+), 3 deletions(-)
>>
>> diff --git a/target/loongarch/tcg/tlb_helper.c 
>> b/target/loongarch/tcg/tlb_helper.c
>> index 00422f259d..006fe1b207 100644
>> --- a/target/loongarch/tcg/tlb_helper.c
>> +++ b/target/loongarch/tcg/tlb_helper.c
>> @@ -21,6 +21,17 @@
>>   #include "cpu-csr.h"
>>   #include "tcg/tcg_loongarch.h"
>> +typedef bool (*tlb_match)(int global, int asid, int tlb_asid);
> 
> Should global parameter be bool?
Will do.
> 
>> +
>> +static bool tlb_match_any(int global, int asid, int tlb_asid)
>> +{
>> +    if (global == 1 || tlb_asid == asid) {
>> +        return true;
>> +    }
>> +
>> +    return false;
>> +}
> 
> More compact as
> 
>      return global || tlb_asid == asid;
yes, this is better. Will do in next version.

Regards
Bibo Mao
> 
> Otherwise,
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> 
> 
> r~