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