Commit 9c5e10a21c for qemu.org

commit 9c5e10a21cc26c02216caaea16acd783d9d0b1f3
Author: Bibo Mao <maobibo@loongson.cn>
Date:   Mon Sep 14 10:14:19 2026 +0800

    target/loongarch: Enable HW PTW feature by default

    With max CPU type, PTW feature is enabled in TCG mode and auto detected
    in KVM mode. Enable this feature in both TCG and KVM mode in the
    beginning, and detect it in late with KVM mode.

    Signed-off-by: Bibo Mao <maobibo@loongson.cn>
    Reviewed-by:   Song Gao <17746591750@163.com>

diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
index 99499034ed..4a75d53f7e 100644
--- a/target/loongarch/cpu.c
+++ b/target/loongarch/cpu.c
@@ -472,6 +472,7 @@ static void loongarch_max_initfn(Object *obj)
     /* '-cpu max': use it for max supported CPU features */
     loongarch_la464_initfn(obj);

+    cpu->env.cpucfg[2] = FIELD_DP32(cpu->env.cpucfg[2], CPUCFG2, HPTW, 1);
     cpu->ptw = ON_OFF_AUTO_AUTO;
     cpu->env.cpucfg[1] = FIELD_DP32(cpu->env.cpucfg[1], CPUCFG1, MSG_INT, 1);
     cpu->msgint = ON_OFF_AUTO_AUTO;
@@ -488,12 +489,6 @@ static void loongarch_max_initfn(Object *obj)
     data = cpu->env.cpucfg[3];
     data = FIELD_DP32(data, CPUCFG3, DBAR_HINTS, 1);
     cpu->env.cpucfg[3] = data;
-
-    if (tcg_enabled()) {
-        data = cpu->env.cpucfg[2];
-        data = FIELD_DP32(data, CPUCFG2, HPTW, 1);
-        cpu->env.cpucfg[2] = data;
-    }
 }

 #if defined(CONFIG_KVM)
diff --git a/target/loongarch/kvm/kvm.c b/target/loongarch/kvm/kvm.c
index 0626c553d5..941142305e 100644
--- a/target/loongarch/kvm/kvm.c
+++ b/target/loongarch/kvm/kvm.c
@@ -1150,6 +1150,7 @@ static int kvm_cpu_check_ptw(CPUState *cs, Error **errp)
     CPULoongArchState *env = cpu_env(cs);
     bool kvm_supported;

+    env->cpucfg[2] = FIELD_DP32(env->cpucfg[2], CPUCFG2, HPTW, 0);
     kvm_supported = kvm_feature_supported(cs, LOONGARCH_FEATURE_PTW);
     if (cpu->ptw == ON_OFF_AUTO_ON) {
         if (!kvm_supported) {