Add the RPMI Hart State Management service group for RISC-V virt. The service exposes configured hart IDs to librpmi and keeps stable per-hart state in the RPMI device. A persistent observer on each target CPU makes actual WFI entry authoritative for stop and suspend completion, while CPU work handles starts and requests received after a hart has already entered WFI.
Keep RPMI-stopped harts off despite pending interrupts, timer events, kicks, or generic resume attempts. HART_START is the normal path that restores the hart power state and execution address. Serialize librpmi state publication under the BQL, drain target-CPU work during teardown, and reconstruct the external librpmi HSM object during machine reset so pending operations return to the STARTED baseline. Signed-off-by: Subrahmanya Lingappa <[email protected]> --- hw/misc/meson.build | 1 + hw/misc/riscv_rpmi.c | 7 + hw/misc/riscv_rpmi_hsm.c | 344 ++++++++++++++++++++++++++++++++++ hw/misc/riscv_rpmi_internal.h | 5 +- hw/riscv/virt.c | 5 + include/hw/misc/riscv_rpmi.h | 5 + target/riscv/cpu-qom.h | 16 ++ target/riscv/cpu.c | 61 ++++++ target/riscv/cpu.h | 7 + target/riscv/tcg/op_helper.c | 4 + target/riscv/tcg/tcg-cpu.c | 1 + 11 files changed, 455 insertions(+), 1 deletion(-) create mode 100644 hw/misc/riscv_rpmi_hsm.c diff --git a/hw/misc/meson.build b/hw/misc/meson.build index a091d393a3..6d93cd4ab0 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -186,4 +186,5 @@ system_ss.add(when: 'CONFIG_QCOM_QDSP6SS_CLK', if_true: files('qcom-qdsp6ss-clk. system_ss.add(when: 'CONFIG_RISCV_RPMI', if_true: [files( 'riscv_rpmi.c', 'riscv_rpmi_sysreset.c', + 'riscv_rpmi_hsm.c', ), librpmi], if_false: files('riscv_rpmi-stub.c')) diff --git a/hw/misc/riscv_rpmi.c b/hw/misc/riscv_rpmi.c index 82a457241c..1bf9d77b04 100644 --- a/hw/misc/riscv_rpmi.c +++ b/hw/misc/riscv_rpmi.c @@ -210,6 +210,10 @@ static const RiscvRpmiServiceOps riscv_rpmi_service_ops[] = { .service_group = RPMI_SRVGRP_SYSTEM_RESET, .add = riscv_rpmi_sysreset_add, .remove = riscv_rpmi_sysreset_remove, + }, { + .service_group = RPMI_SRVGRP_HSM, + .add = riscv_rpmi_hsm_add, + .remove = riscv_rpmi_hsm_remove, }, }; @@ -259,6 +263,9 @@ static void riscv_rpmi_reset_hold(Object *obj, ResetType type) memset(memory_region_get_ram_ptr(&s->shmem), 0, s->shmem_size); memory_region_set_dirty(&s->shmem, 0, s->shmem_size); } + if (!riscv_rpmi_hsm_reset(s, &error_abort)) { + return; + } } static void riscv_rpmi_cleanup(RiscvRpmiState *s) diff --git a/hw/misc/riscv_rpmi_hsm.c b/hw/misc/riscv_rpmi_hsm.c new file mode 100644 index 0000000000..42a4f3b096 --- /dev/null +++ b/hw/misc/riscv_rpmi_hsm.c @@ -0,0 +1,344 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * RISC-V RPMI HSM service. + * + * Copyright (c) 2026 Qualcomm Technologies, Inc. + * Author: + * Subrahmanya Lingappa <[email protected]> + */ + +#include "qemu/osdep.h" +#include "riscv_rpmi_internal.h" +#include "hw/core/cpu.h" +#include "qemu/main-loop.h" +#include "system/cpus.h" +#include "target/riscv/cpu-qom.h" +#include "librpmi_env.h" + +static const struct rpmi_hsm_suspend_type riscv_rpmi_hsm_suspend_types[] = { + { .type = 0 }, +}; + +struct RiscvRpmiHsmHart { + RiscvRpmiState *rpmi; + CPUState *cpu; + enum rpmi_hart_hw_state state; + enum rpmi_hart_hw_state wfi_state; + uint64_t start_addr; +}; + +/* + * RPMI requests and queued CPU work hold BQL. Natural WFI transitions use an + * atomic armed-state check and take BQL only when a transition is pending. + */ +static RiscvRpmiHsmHart *riscv_rpmi_hsm_hart(RiscvRpmiState *s, + uint32_t hart_index) +{ + if (!s->hsm_harts || hart_index >= s->hart_count || + !s->hsm_harts[hart_index].cpu) { + return NULL; + } + + return &s->hsm_harts[hart_index]; +} +static void riscv_rpmi_hsm_wfi(CPUState *cpu, bool entering, void *opaque) +{ + RiscvRpmiHsmHart *hart = opaque; + enum rpmi_hart_hw_state wfi_state; + bool need_bql = !bql_locked(); + bool changed = false; + + wfi_state = qatomic_read(&hart->wfi_state); + if (wfi_state == RPMI_HART_HW_STATE_MAX) { + return; + } + if (need_bql) { + bql_lock(); + } + + wfi_state = qatomic_read(&hart->wfi_state); + if (entering && wfi_state == RPMI_HART_HW_STATE_STOPPED) { + qatomic_set(&hart->wfi_state, RPMI_HART_HW_STATE_MAX); + riscv_cpu_set_powered_off(cpu, true); + hart->state = RPMI_HART_HW_STATE_STOPPED; + changed = true; + } else if (wfi_state == RPMI_HART_HW_STATE_SUSPENDED) { + hart->state = entering ? RPMI_HART_HW_STATE_SUSPENDED : + RPMI_HART_HW_STATE_STARTED; + if (!entering) { + qatomic_set(&hart->wfi_state, RPMI_HART_HW_STATE_MAX); + } + changed = true; + } + + if (changed) { + rpmi_hsm_process_state_changes(hart->rpmi->hsm); + } + if (need_bql) { + bql_unlock(); + } +} + +static void riscv_rpmi_hsm_set_observer(CPUState *cpu, run_on_cpu_data data) +{ + riscv_cpu_set_wfi_observer(cpu, riscv_rpmi_hsm_wfi, data.host_ptr); +} + +static void riscv_rpmi_hsm_clear_observer(CPUState *cpu, run_on_cpu_data data) +{ + riscv_cpu_set_wfi_observer(cpu, NULL, NULL); + riscv_cpu_set_powered_off(cpu, false); + cpu->halted = false; + cpu_resume(cpu); +} + +static void riscv_rpmi_hsm_start_cpu(CPUState *cpu, run_on_cpu_data data) +{ + RiscvRpmiHsmHart *hart = data.host_ptr; + + g_assert(bql_locked()); + riscv_cpu_handle_wfi_resume(cpu); + cpu_set_pc(cpu, hart->start_addr); + riscv_cpu_set_powered_off(cpu, false); + cpu->halted = false; + cpu_resume(cpu); + hart->state = RPMI_HART_HW_STATE_STARTED; + rpmi_hsm_process_state_changes(hart->rpmi->hsm); +} + +static void riscv_rpmi_hsm_replay_wfi(CPUState *cpu, run_on_cpu_data data) +{ + riscv_cpu_replay_wfi_observer(cpu); +} + +static enum rpmi_error riscv_rpmi_hsm_start_prepare( + void *priv, rpmi_uint32_t hart_index, rpmi_uint64_t start_addr) +{ + RiscvRpmiHsmHart *hart = riscv_rpmi_hsm_hart(priv, hart_index); + + g_assert(bql_locked()); + if (!hart) { + return RPMI_ERR_INVALID_PARAM; + } + + hart->start_addr = start_addr; + async_run_on_cpu(hart->cpu, riscv_rpmi_hsm_start_cpu, + RUN_ON_CPU_HOST_PTR(hart)); + return RPMI_SUCCESS; +} + +/* librpmi requires finalize hooks even when QEMU has no second phase. */ +static void riscv_rpmi_hsm_start_finalize(void *priv, + rpmi_uint32_t hart_index, + rpmi_uint64_t start_addr) +{ +} + +static enum rpmi_error riscv_rpmi_hsm_arm_wfi( + RiscvRpmiState *s, uint32_t hart_index, enum rpmi_hart_hw_state state) +{ + RiscvRpmiHsmHart *hart = riscv_rpmi_hsm_hart(s, hart_index); + + g_assert(bql_locked()); + if (!hart) { + return RPMI_ERR_INVALID_PARAM; + } + + qatomic_set(&hart->wfi_state, state); + async_run_on_cpu(hart->cpu, riscv_rpmi_hsm_replay_wfi, + RUN_ON_CPU_NULL); + return RPMI_SUCCESS; +} + +static enum rpmi_error riscv_rpmi_hsm_stop_prepare(void *priv, + rpmi_uint32_t hart_index) +{ + return riscv_rpmi_hsm_arm_wfi(priv, hart_index, + RPMI_HART_HW_STATE_STOPPED); +} + +static void riscv_rpmi_hsm_stop_finalize(void *priv, + rpmi_uint32_t hart_index) +{ +} + +static enum rpmi_error riscv_rpmi_hsm_suspend_prepare( + void *priv, rpmi_uint32_t hart_index, + const struct rpmi_hsm_suspend_type *suspend_type, + rpmi_uint64_t resume_addr) +{ + if (!suspend_type) { + return RPMI_ERR_INVALID_PARAM; + } + + return riscv_rpmi_hsm_arm_wfi(priv, hart_index, + RPMI_HART_HW_STATE_SUSPENDED); +} + +static void riscv_rpmi_hsm_suspend_finalize( + void *priv, rpmi_uint32_t hart_index, + const struct rpmi_hsm_suspend_type *suspend_type, + rpmi_uint64_t resume_addr) +{ +} + +static enum rpmi_hart_hw_state riscv_rpmi_hsm_get_hw_state( + void *priv, rpmi_uint32_t hart_index) +{ + RiscvRpmiHsmHart *hart = riscv_rpmi_hsm_hart(priv, hart_index); + + g_assert(bql_locked()); + return hart ? hart->state : RPMI_HART_HW_STATE_STOPPED; +} + +static const struct rpmi_hsm_platform_ops riscv_rpmi_hsm_ops = { + .hart_get_hw_state = riscv_rpmi_hsm_get_hw_state, + .hart_start_prepare = riscv_rpmi_hsm_start_prepare, + .hart_start_finalize = riscv_rpmi_hsm_start_finalize, + .hart_stop_prepare = riscv_rpmi_hsm_stop_prepare, + .hart_stop_finalize = riscv_rpmi_hsm_stop_finalize, + .hart_suspend_prepare = riscv_rpmi_hsm_suspend_prepare, + .hart_suspend_finalize = riscv_rpmi_hsm_suspend_finalize, +}; + +static bool riscv_rpmi_hsm_create_external( + RiscvRpmiState *s, struct rpmi_service_group **group, Error **errp) +{ + s->hsm = rpmi_hsm_create(s->hart_count, s->hart_ids, + ARRAY_SIZE(riscv_rpmi_hsm_suspend_types), + riscv_rpmi_hsm_suspend_types, + &riscv_rpmi_hsm_ops, s); + if (!s->hsm) { + error_setg(errp, "failed to create RPMI HSM context"); + return false; + } + + *group = rpmi_service_group_hsm_create(s->hsm); + if (!*group) { + rpmi_hsm_destroy(s->hsm); + s->hsm = NULL; + error_setg(errp, "failed to create RPMI HSM service group"); + return false; + } + + return true; +} + +static void riscv_rpmi_hsm_destroy_external(RiscvRpmiState *s) +{ + if (s->hsm_group) { + rpmi_env_free_lock(s->hsm_group->lock); + s->hsm_group->lock = NULL; + rpmi_service_group_hsm_destroy(s->hsm_group); + s->hsm_group = NULL; + } + if (s->hsm) { + rpmi_hsm_destroy(s->hsm); + s->hsm = NULL; + } +} + +static void riscv_rpmi_hsm_reset_cpu(CPUState *cpu, run_on_cpu_data data) +{ + riscv_cpu_handle_wfi_resume(cpu); + riscv_cpu_set_powered_off(cpu, false); + cpu->halted = false; +} + +bool riscv_rpmi_hsm_add(RiscvRpmiState *s, Error **errp) +{ + struct rpmi_service_group *group; + + if (s->hsm_group) { + error_setg(errp, "duplicate RPMI HSM service descriptor"); + return false; + } + if (!s->hart_count || !s->hart_ids) { + error_setg(errp, "RPMI HSM service requires hart IDs"); + return false; + } + + s->hsm_harts = g_new0(RiscvRpmiHsmHart, s->hart_count); + for (uint32_t i = 0; i < s->hart_count; i++) { + RiscvRpmiHsmHart *hart = &s->hsm_harts[i]; + + hart->rpmi = s; + hart->cpu = cpu_by_arch_id(s->hart_ids[i]); + hart->state = RPMI_HART_HW_STATE_STARTED; + qatomic_set(&hart->wfi_state, RPMI_HART_HW_STATE_MAX); + } + + if (!riscv_rpmi_hsm_create_external(s, &group, errp)) { + g_clear_pointer(&s->hsm_harts, g_free); + return false; + } + if (!riscv_rpmi_context_add_group(s, group, "HSM", errp)) { + s->hsm_group = group; + riscv_rpmi_hsm_destroy_external(s); + g_clear_pointer(&s->hsm_harts, g_free); + return false; + } + + s->hsm_group = group; + for (uint32_t i = 0; i < s->hart_count; i++) { + RiscvRpmiHsmHart *hart = &s->hsm_harts[i]; + + if (hart->cpu) { + run_on_cpu(hart->cpu, riscv_rpmi_hsm_set_observer, + RUN_ON_CPU_HOST_PTR(hart)); + } + } + return true; +} + +void riscv_rpmi_hsm_remove(RiscvRpmiState *s) +{ + if (!s->hsm_harts) { + return; + } + + for (uint32_t i = 0; i < s->hart_count; i++) { + CPUState *cpu = s->hsm_harts[i].cpu; + + if (cpu) { + run_on_cpu(cpu, riscv_rpmi_hsm_clear_observer, RUN_ON_CPU_NULL); + } + } + riscv_rpmi_context_remove_group(s, s->hsm_group); + riscv_rpmi_hsm_destroy_external(s); + g_clear_pointer(&s->hsm_harts, g_free); +} + +bool riscv_rpmi_hsm_reset(RiscvRpmiState *s, Error **errp) +{ + struct rpmi_service_group *group = NULL; + + if (!s->hsm_harts) { + return true; + } + + for (uint32_t i = 0; i < s->hart_count; i++) { + RiscvRpmiHsmHart *hart = &s->hsm_harts[i]; + + if (hart->cpu) { + run_on_cpu(hart->cpu, riscv_rpmi_hsm_reset_cpu, + RUN_ON_CPU_NULL); + } + hart->state = RPMI_HART_HW_STATE_STARTED; + qatomic_set(&hart->wfi_state, RPMI_HART_HW_STATE_MAX); + } + + riscv_rpmi_context_remove_group(s, s->hsm_group); + riscv_rpmi_hsm_destroy_external(s); + if (!riscv_rpmi_hsm_create_external(s, &group, errp)) { + return false; + } + if (!riscv_rpmi_context_add_group(s, group, "HSM", errp)) { + s->hsm_group = group; + riscv_rpmi_hsm_destroy_external(s); + return false; + } + s->hsm_group = group; + return true; +} diff --git a/hw/misc/riscv_rpmi_internal.h b/hw/misc/riscv_rpmi_internal.h index e5880aebe2..e56dba5b1a 100644 --- a/hw/misc/riscv_rpmi_internal.h +++ b/hw/misc/riscv_rpmi_internal.h @@ -27,8 +27,11 @@ bool riscv_rpmi_context_add_group(RiscvRpmiState *s, Error **errp); void riscv_rpmi_context_remove_group(RiscvRpmiState *s, struct rpmi_service_group *group); - bool riscv_rpmi_sysreset_add(RiscvRpmiState *s, Error **errp); void riscv_rpmi_sysreset_remove(RiscvRpmiState *s); +bool riscv_rpmi_hsm_add(RiscvRpmiState *s, Error **errp); +void riscv_rpmi_hsm_remove(RiscvRpmiState *s); +bool riscv_rpmi_hsm_reset(RiscvRpmiState *s, Error **errp); + #endif diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c index 1e313e078e..32aa13325e 100644 --- a/hw/riscv/virt.c +++ b/hw/riscv/virt.c @@ -485,6 +485,11 @@ static const RiscvRpmiServiceConfig virt_rpmi_services[] = { .compatible = "riscv,rpmi-system-reset", .service_group = RPMI_SRVGRP_SYSTEM_RESET, }, + { + .node_name = "hsm", + .compatible = "riscv,rpmi-hsm", + .service_group = RPMI_SRVGRP_HSM, + }, }; static uint32_t virt_rpmi_service_count(void) diff --git a/include/hw/misc/riscv_rpmi.h b/include/hw/misc/riscv_rpmi.h index 1ab648359d..3a240941cd 100644 --- a/include/hw/misc/riscv_rpmi.h +++ b/include/hw/misc/riscv_rpmi.h @@ -42,6 +42,8 @@ struct rpmi_context; struct rpmi_service_group; struct rpmi_shmem; struct rpmi_transport; +struct rpmi_hsm; +typedef struct RiscvRpmiHsmHart RiscvRpmiHsmHart; typedef struct RiscvRpmiMachineOps { void (*system_reset)(void); @@ -82,6 +84,9 @@ struct RiscvRpmiState { char *platform_info; const RiscvRpmiMachineOps *machine_ops; struct rpmi_service_group *sysreset_group; + struct rpmi_hsm *hsm; + struct rpmi_service_group *hsm_group; + RiscvRpmiHsmHart *hsm_harts; uint32_t *hart_ids; uint32_t hart_count; const RiscvRpmiServiceConfig *services; diff --git a/target/riscv/cpu-qom.h b/target/riscv/cpu-qom.h index 61234842e3..f6b6023835 100644 --- a/target/riscv/cpu-qom.h +++ b/target/riscv/cpu-qom.h @@ -64,4 +64,20 @@ OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU) +#ifndef CONFIG_USER_ONLY +/* + * WFI observers persist across entries and exits. Access observers and WFI + * state only on the target CPU thread; use run_on_cpu() to register or remove + * an observer and drain earlier CPU work before freeing its opaque state. + */ +void riscv_cpu_set_wfi_observer( + CPUState *cpu, + void (*observer)(CPUState *cpu, bool entering, void *opaque), + void *opaque); +void riscv_cpu_handle_wfi(CPUState *cpu); +void riscv_cpu_handle_wfi_resume(CPUState *cpu); +void riscv_cpu_replay_wfi_observer(CPUState *cpu); +void riscv_cpu_set_powered_off(CPUState *cpu, bool powered_off); +#endif + #endif /* RISCV_CPU_QOM_H */ diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 5a0529fbda..e516a6b98f 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -58,6 +58,61 @@ const uint32_t misa_bits[] = {RVI, RVE, RVM, RVA, RVF, RVD, RVV, */ #define RISCV_CPU_MARCHID 42 +#ifndef CONFIG_USER_ONLY +void riscv_cpu_set_wfi_observer(CPUState *cpu, + void (*observer)(CPUState *, bool, void *), + void *opaque) +{ + RISCVCPU *rcpu = RISCV_CPU(cpu); + + g_assert(qemu_cpu_is_self(cpu)); + rcpu->wfi_observer = observer; + rcpu->wfi_observer_opaque = opaque; +} + +void riscv_cpu_handle_wfi(CPUState *cpu) +{ + RISCVCPU *rcpu = RISCV_CPU(cpu); + + g_assert(qemu_cpu_is_self(cpu)); + rcpu->wfi_active = true; + if (rcpu->wfi_observer) { + rcpu->wfi_observer(cpu, true, rcpu->wfi_observer_opaque); + } +} + +void riscv_cpu_handle_wfi_resume(CPUState *cpu) +{ + RISCVCPU *rcpu = RISCV_CPU(cpu); + + g_assert(qemu_cpu_is_self(cpu)); + if (!rcpu->wfi_active) { + return; + } + rcpu->wfi_active = false; + if (rcpu->wfi_observer) { + rcpu->wfi_observer(cpu, false, rcpu->wfi_observer_opaque); + } +} + +void riscv_cpu_replay_wfi_observer(CPUState *cpu) +{ + RISCVCPU *rcpu = RISCV_CPU(cpu); + + g_assert(qemu_cpu_is_self(cpu)); + if (rcpu->wfi_active && rcpu->wfi_observer) { + rcpu->wfi_observer(cpu, true, rcpu->wfi_observer_opaque); + } +} + +void riscv_cpu_set_powered_off(CPUState *cpu, bool powered_off) +{ + RISCVCPU *rcpu = RISCV_CPU(cpu); + + qatomic_set(&rcpu->powered_off, powered_off); +} +#endif + /* * From vector_helper.c * Note that vector data is stored in host-endian 64-bit chunks, @@ -967,6 +1022,10 @@ static bool riscv_cpu_has_work(CPUState *cs) { RISCVCPU *cpu = RISCV_CPU(cs); CPURISCVState *env = &cpu->env; + + if (qatomic_read(&cpu->powered_off)) { + return false; + } /* * Definition of the WFI instruction requires it to ignore the privilege * mode and delegation registers, but respect individual enables @@ -988,6 +1047,8 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) mcc->parent_phases.hold(obj, type); } #ifndef CONFIG_USER_ONLY + riscv_cpu_set_powered_off(cs, false); + cpu->wfi_active = false; env->misa_mxl = mcc->def->misa_mxl_max; env->priv = PRV_M; env->mstatus &= ~(MSTATUS_MIE | MSTATUS_MPRV); diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 49a1186bdb..ac79397858 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -577,6 +577,13 @@ struct ArchCPU { CPURISCVState env; +#ifndef CONFIG_USER_ONLY + void (*wfi_observer)(CPUState *, bool, void *); + void *wfi_observer_opaque; + bool wfi_active; + bool powered_off; +#endif + GDBFeature dyn_csr_feature; GDBFeature dyn_vreg_feature; diff --git a/target/riscv/tcg/op_helper.c b/target/riscv/tcg/op_helper.c index f9c79fe6b9..b0fb3c2cf8 100644 --- a/target/riscv/tcg/op_helper.c +++ b/target/riscv/tcg/op_helper.c @@ -644,6 +644,10 @@ void HELPER(wfi)(CPURISCVState *env) return; } cs->halted = 1; +#ifndef CONFIG_USER_ONLY + /* Publish WFI only after QEMU has entered the halted state. */ + riscv_cpu_handle_wfi(cs); +#endif cs->exception_index = EXCP_HLT; cpu_loop_exit(cs); } diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c index 2a7796c382..2bd198c016 100644 --- a/target/riscv/tcg/tcg-cpu.c +++ b/target/riscv/tcg/tcg-cpu.c @@ -305,6 +305,7 @@ const TCGCPUOps riscv_tcg_ops = { .tlb_fill = riscv_cpu_tlb_fill, .pointer_wrap = riscv_pointer_wrap, .cpu_exec_enter = riscv_cpu_exec_enter, + .leaving_halt = riscv_cpu_handle_wfi_resume, .cpu_exec_interrupt = riscv_cpu_exec_interrupt, .cpu_exec_reset = cpu_reset, .do_interrupt = riscv_cpu_do_interrupt,
