Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9EC253542E5 for ; Wed, 22 Apr 2026 10:17:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.65 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776853048; cv=none; b=XpLs+Udr801fZUGVe7VzontaQalqK7jeRE1onxaAJivDVA4W5zSZXsbv8l9kX2C9ya0ntW/i46yrkDdFrLPobOvnO7KF1RnFIYXREdHa+GyRa2aTI/8H3qSb7dL7AILuU154APHpbecpjho1TYH+tgZKBIRexqJTbCPfdCRuuow= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776853048; c=relaxed/simple; bh=dAebQ6ZDDatNzL+6vwaqtuO7483PVvyMvhFQdmkviAA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=pbSv72qqZD84tD4FAlqeiGRgrRVzERA3fqlgFdBOHBpkThJ3GnmfJeKyBDVT9uwdTw2mwQysSrvLZ9PjfH+w+IZDu+VAr41Cx9//JaC9W19nBEgP1Iha1m9E3qX9u+EeA0ZBkqekqDSI6ZI446PN2ms7Glc9vlHE6nBbiOcdBt4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=p01C+Z/H; arc=none smtp.client-ip=209.85.221.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="p01C+Z/H" Received: by mail-wr1-f65.google.com with SMTP id ffacd0b85a97d-43fe608cb92so3662258f8f.2 for ; Wed, 22 Apr 2026 03:17:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1776853045; x=1777457845; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4/Ygp/F48dZ3RysrI6Uw/aAqnHQSpp+gk0lyGlg/T44=; b=p01C+Z/HfhiMyJ74FXEnSVp2BiKomiyGFVQUnOqwdQuPPPu1xgHh5zvomhKjU49n4F 7gj7InDnmKcB7988dHVQdynIKrm3bIzIwRFySZ7Yakzl2784dCFCwHQVIvcJUb9w5vPG BJKTf301/Mam+/7/JopI0RVto+qVl5/31r2wicS+O+D5TGFUEiWlD47IrIFrUeSYlhor 4OkyKHRBAIJ7qhX4zpKX6fuXSqu+Qwv69E3QTG2g6O4i9hutNUyneVfnHO5mdp+fTJhi zBsg7xGNpW8kNc8kH+vYOeT98/k1n42pUiqiXIhI9gC7Wshb/YefmixeWuV2k/GBkTtf a4Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776853045; x=1777457845; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=4/Ygp/F48dZ3RysrI6Uw/aAqnHQSpp+gk0lyGlg/T44=; b=GqXIaT/lTgvkoBlUMuAR40Jr4IAjJyojOxcOYnl6EF9YXbi92UpIaVI65qK4SSpkSg jRFweRBN7Q42HYPRJTMrkKya27AngAxp4fQd0pBWWh7ZAgt1Yiz/qtPdQBNjqHB8j7+K mmh6JQa21OVEIZsI64nHQxGVnNYoz6jgQrdj6aWGi/B5zjwbNcgb6mVKV0uvG7ZvJDyq MWtYPG5G9i+AiehdnJpki2EZurukZJdz2z9rmTP6Sfcu2Mbso4LkwwgXiR7cq5WWXlmG s52oe2ekV9GjEocQ+iB16XbuR5wtU97bO7r7WZzF2Gd/aPMsqs8j72AKZ9N8n6hTDToc nlvQ== X-Forwarded-Encrypted: i=1; AFNElJ84bzPBsXn9LiYZks54Wu8HmncQTc2ARcWnJlYWsP/mVB0wMAdoMCdxYs+rDEGZBwLdvJA=@vger.kernel.org X-Gm-Message-State: AOJu0YzRhwG6AmUfCSAOFWiDoIaEOKOfzv4uu9opu/zL+WvkSJ8aGeab XphcxDvhGlI9Ql6HLs/9Iu7U2NKyPD327G6hZ61vj+5S7rO/I6g78N75uE3JBgHzuoU= X-Gm-Gg: AeBDieuv5dJLXamwiAy5y+6/AVzyI0tPx/g5wm3AQe1eVpSjnNMU7WwozCLLaCyCRqt ysCCgAMb7gLtjJnfKrCLw+LPqEKok71FTxoDabeO8av+UnaHSGMba3ZLvsArNMiunB8OHIAQ6fY vtRq78Z6LWWox4eG1hW05JhF6NKO+1tZ/Yjvr5BodjcoyGafBUDFS7Avg/TAxI8ZaXJ84x/D2vH RDExmvnl4fSj3TXy0G8osVMpZX5+9TSTpjXGfFXhieN0MidC9PwhZSkHVPzfAj+7thE0uADKS/O 03FLmrX9O5G2dX7kDnUE5McCKk7vjI3xhbm/GJQVwqTYkVyhs3Zu4e5+40gg/qGA6P7BcYfWUyP Tv+htA0CHBoyffb2I3MtsH8r2H/mQScdpsx6+Y389Z+0iYmJyejbE+jknn9R3Qpw/oEHDaVHBXv fq9i/6RWQhyl+iyQXUzvj/SVwh3b6P1wWYNg== X-Received: by 2002:a5d:5889:0:b0:43d:7a97:78b5 with SMTP id ffacd0b85a97d-43fe3e0a448mr32111190f8f.33.1776853044898; Wed, 22 Apr 2026 03:17:24 -0700 (PDT) Received: from draig.lan ([185.124.0.195]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43fe4e4d5b1sm48555959f8f.30.2026.04.22.03.17.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Apr 2026 03:17:24 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 5F2015F93F; Wed, 22 Apr 2026 11:10:45 +0100 (BST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Alexander Graf , qemu-arm@nongnu.org, Peter Maydell , Paolo Bonzini , Mohamed Mediouni , kvm@vger.kernel.org, Pedro Barbuda , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v2 24/31] target/arm: teach arm_cpu_has_work about halting reasons Date: Wed, 22 Apr 2026 11:10:35 +0100 Message-ID: <20260422101043.1234229-25-alex.bennee@linaro.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260422101043.1234229-1-alex.bennee@linaro.org> References: <20260422101043.1234229-1-alex.bennee@linaro.org> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the advent of WFE and WFI we need to pay closer attention to the reason why the vCPU may be sleeping to figure out if we should wake it up. Create env->halt_reason to track this and then re-order the tests so we: - ignore everything is the vCPU is powered off - wake up if the event_register is set and we were in a WFE - otherwise any IRQ event does wake the vCPU up. Signed-off-by: Alex Bennée --- target/arm/cpu.h | 27 +++++++++++++++++++++++++ target/arm/arm-powerctl.c | 4 +++- target/arm/cpu.c | 40 +++++++++++++++++++++++++++----------- target/arm/kvm.c | 5 +++-- target/arm/machine.c | 2 +- target/arm/tcg/op_helper.c | 3 +++ 6 files changed, 66 insertions(+), 15 deletions(-) diff --git a/target/arm/cpu.h b/target/arm/cpu.h index 657ff4ab20b..98cdfa6f130 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -257,6 +257,19 @@ typedef enum ARMFPStatusFlavour { } ARMFPStatusFlavour; #define FPST_COUNT 10 +/** + * ARMHaltReason - the reason we have entered halt state + * + * To be able to correctly wake up via arm_cpu_has_work() we need to + * track the reason we went to sleep. + */ +typedef enum { + NOT_HALTED = 0, + HALT_PSCI, + HALT_WFI, + HALT_WFE +} ARMHaltReason; + typedef struct CPUArchState { /* Regs for current mode. */ uint32_t regs[16]; @@ -760,6 +773,9 @@ typedef struct CPUArchState { /* Optional fault info across tlb lookup. */ ARMMMUFaultInfo *tlb_fi; + /* Reason the CPU is halted */ + ARMHaltReason halt_reason; + /* * The event register is shared by all ARM profiles (A/R/M), * so it is stored in the top-level CPU state. @@ -1691,6 +1707,17 @@ static inline void xpsr_write(CPUARMState *env, uint32_t val, uint32_t mask) #endif } +/** + * arm_set_cpu_power_state() - set power state synced with halt_reason + */ +static inline void arm_set_cpu_power_state(ARMCPU *cpu, ARMPSCIState state) +{ + CPUARMState *env = &cpu->env; + + cpu->power_state = state; + env->halt_reason = (state == PSCI_OFF) ? HALT_PSCI : NOT_HALTED; +} + #define HCR_VM (1ULL << 0) #define HCR_SWIO (1ULL << 1) #define HCR_PTW (1ULL << 2) diff --git a/target/arm/arm-powerctl.c b/target/arm/arm-powerctl.c index a788376d1d3..4ca63a54443 100644 --- a/target/arm/arm-powerctl.c +++ b/target/arm/arm-powerctl.c @@ -78,6 +78,7 @@ static void arm_set_cpu_on_async_work(CPUState *target_cpu_state, /* Finally set the power status */ assert(bql_locked()); + target_cpu->env.halt_reason = NOT_HALTED; target_cpu->power_state = PSCI_ON; } @@ -186,6 +187,7 @@ static void arm_set_cpu_on_and_reset_async_work(CPUState *target_cpu_state, /* Finally set the power status */ assert(bql_locked()); + target_cpu->env.halt_reason = NOT_HALTED; target_cpu->power_state = PSCI_ON; } @@ -239,7 +241,7 @@ static void arm_set_cpu_off_async_work(CPUState *target_cpu_state, ARMCPU *target_cpu = ARM_CPU(target_cpu_state); assert(bql_locked()); - target_cpu->power_state = PSCI_OFF; + arm_set_cpu_power_state(target_cpu, PSCI_OFF); target_cpu_state->halted = 1; target_cpu_state->exception_index = EXCP_HLT; } diff --git a/target/arm/cpu.c b/target/arm/cpu.c index ccc47c8a9ad..124be8c401e 100644 --- a/target/arm/cpu.c +++ b/target/arm/cpu.c @@ -144,18 +144,36 @@ static bool arm_cpu_has_work(CPUState *cs) { ARMCPU *cpu = ARM_CPU(cs); - if (arm_feature(&cpu->env, ARM_FEATURE_M)) { - if (cpu->env.event_register) { - return true; - } + /* + * Only another PSCI call can wake the CPU up in which case the + * power_state would be set by arm_set_cpu_on_and_reset_async_work() + */ + if (cpu->power_state == PSCI_OFF) { + g_assert(cpu->env.halt_reason == HALT_PSCI); + return false; + } + + /* + * A wake-up event should only wake us if we are halted on a WFE + */ + if (cpu->env.halt_reason == HALT_WFE && cpu->env.event_register) { + cpu->env.halt_reason = NOT_HALTED; + return true; + } + + /* + * Otherwise pretty much any IRQ would wake us up + */ + if (cpu_test_interrupt(cs, + CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD + | CPU_INTERRUPT_NMI | CPU_INTERRUPT_VINMI | CPU_INTERRUPT_VFNMI + | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR + | CPU_INTERRUPT_EXITTB)) { + cpu->env.halt_reason = NOT_HALTED; + return true; } - return (cpu->power_state != PSCI_OFF) - && cpu_test_interrupt(cs, - CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD - | CPU_INTERRUPT_NMI | CPU_INTERRUPT_VINMI | CPU_INTERRUPT_VFNMI - | CPU_INTERRUPT_VFIQ | CPU_INTERRUPT_VIRQ | CPU_INTERRUPT_VSERR - | CPU_INTERRUPT_EXITTB); + return false; } #endif /* !CONFIG_USER_ONLY */ @@ -250,7 +268,7 @@ static void arm_cpu_reset_hold(Object *obj, ResetType type) env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1; env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2; - cpu->power_state = cs->start_powered_off ? PSCI_OFF : PSCI_ON; + arm_set_cpu_power_state(cpu, cs->start_powered_off ? PSCI_OFF : PSCI_ON); if (arm_feature(env, ARM_FEATURE_AARCH64)) { /* 64 bit CPUs always start in 64 bit mode */ diff --git a/target/arm/kvm.c b/target/arm/kvm.c index d4a68874b88..c08e4797b32 100644 --- a/target/arm/kvm.c +++ b/target/arm/kvm.c @@ -1143,11 +1143,12 @@ static int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu) if (cap_has_mp_state) { struct kvm_mp_state mp_state; int ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MP_STATE, &mp_state); + ARMPSCIState state; if (ret) { return ret; } - cpu->power_state = (mp_state.mp_state == KVM_MP_STATE_STOPPED) ? - PSCI_OFF : PSCI_ON; + state = (mp_state.mp_state == KVM_MP_STATE_STOPPED) ? PSCI_OFF : PSCI_ON; + arm_set_cpu_power_state(cpu, state); } return 0; } diff --git a/target/arm/machine.c b/target/arm/machine.c index b0e499515cf..62401c5a3ea 100644 --- a/target/arm/machine.c +++ b/target/arm/machine.c @@ -916,7 +916,7 @@ static int get_power(QEMUFile *f, void *opaque, size_t size, { ARMCPU *cpu = opaque; bool powered_off = qemu_get_byte(f); - cpu->power_state = powered_off ? PSCI_OFF : PSCI_ON; + arm_set_cpu_power_state(cpu, powered_off ? PSCI_OFF : PSCI_ON); return 0; } diff --git a/target/arm/tcg/op_helper.c b/target/arm/tcg/op_helper.c index 652611b4313..46c745077d5 100644 --- a/target/arm/tcg/op_helper.c +++ b/target/arm/tcg/op_helper.c @@ -403,6 +403,7 @@ void HELPER(wfi)(CPUARMState *env, uint32_t insn_len) target_el); } + env->halt_reason = HALT_WFI; cs->exception_index = EXCP_HLT; cs->halted = 1; cpu_loop_exit(cs); @@ -464,6 +465,7 @@ void HELPER(wfit)(CPUARMState *env, uint32_t rd) } else { timer_mod(cpu->wfxt_timer, nexttick); } + env->halt_reason = HALT_WFI; cs->exception_index = EXCP_HLT; cs->halted = 1; cpu_loop_exit(cs); @@ -508,6 +510,7 @@ void HELPER(wfe)(CPUARMState *env) return; } + env->halt_reason = HALT_WFE; cs->exception_index = EXCP_HLT; cs->halted = 1; cpu_loop_exit(cs); -- 2.47.3