{"id":2226476,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2226476/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260422125250.1303100-31-alex.bennee@linaro.org/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/1.1/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260422125250.1303100-31-alex.bennee@linaro.org>","date":"2026-04-22T12:52:47","name":"[v3,30/32] target/arm: enable event stream on WFE instructions","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"c02e8b0513d3e53c1058a1d40be3d769c991b2a2","submitter":{"id":39532,"url":"http://patchwork.ozlabs.org/api/1.1/people/39532/?format=json","name":"Alex Bennée","email":"alex.bennee@linaro.org"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20260422125250.1303100-31-alex.bennee@linaro.org/mbox/","series":[{"id":501008,"url":"http://patchwork.ozlabs.org/api/1.1/series/501008/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=501008","date":"2026-04-22T12:52:20","name":"target/arm: fully model WFxT instructions for A-profile","version":3,"mbox":"http://patchwork.ozlabs.org/series/501008/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2226476/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2226476/checks/","tags":{},"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256\n header.s=google header.b=JQlBh8ik;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)"],"Received":["from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g0zvG2rnqz1yHB\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 22 Apr 2026 23:04:10 +1000 (AEST)","from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wFXE0-0001Aj-CZ; Wed, 22 Apr 2026 09:02:40 -0400","from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <alex.bennee@linaro.org>)\n id 1wFXDv-00017I-JU\n for qemu-devel@nongnu.org; Wed, 22 Apr 2026 09:02:37 -0400","from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <alex.bennee@linaro.org>)\n id 1wFXDt-00018C-Kt\n for qemu-devel@nongnu.org; Wed, 22 Apr 2026 09:02:35 -0400","by mail-wm1-x32a.google.com with SMTP id\n 5b1f17b1804b1-48334ee0aeaso56315825e9.1\n for <qemu-devel@nongnu.org>; Wed, 22 Apr 2026 06:02:33 -0700 (PDT)","from draig.lan ([185.124.0.195]) by smtp.gmail.com with ESMTPSA id\n 5b1f17b1804b1-48a575ad67asm96183315e9.2.2026.04.22.06.02.27\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Wed, 22 Apr 2026 06:02:30 -0700 (PDT)","from draig.lan (localhost [IPv6:::1])\n by draig.lan (Postfix) with ESMTP id 753D3617C4;\n Wed, 22 Apr 2026 13:52:53 +0100 (BST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=linaro.org; s=google; t=1776862952; x=1777467752; darn=nongnu.org;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=ChBMLZB7W3RWggGvPxQhi9T29Aee3H3wmNETcHJ/8uM=;\n b=JQlBh8ik7WSE/At3PrBiwZelzKLFv4fDsulauOg5HKq+HUZXz1yxoHvOAIhCJi9zcG\n ikGOObtBtXrk9Qy26KWCsGsekTJMnoytnq+PD//2B5W5QF9haX3oGN0PYu/MxlQyK6Jn\n GfEku76SVcxmck/0t7bLP4PKEI27vUOvOnyzHohUhi0TtlyEBW+BNKeY0YmTqfvDM1Xg\n YOjdU+KXaS5VNvcLU4iI9nBDQicAgCFddPcKJNjPNQf8QW/9V8NePXUFneYxtr8+N58m\n CLpt4jcPbEgOIZSbDE13YHeLoQsgD92nFxdQHZt8qp6RzZnuAzfT4xwskKkEJuI6vfiZ\n joEw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1776862952; x=1777467752;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=ChBMLZB7W3RWggGvPxQhi9T29Aee3H3wmNETcHJ/8uM=;\n b=JNGoyTw4GAqBNphn4GBwBFwZfSxDzUkxgBqHgIZu5YF2XaMcJ/sKnFsiYJqOrokyZ9\n mEXGvlUBfXTNB/5ltOIWzKfF+KcqdlAg0EyNceaUtWN5xOQjC8xlAPb6dVvlxzMsv39H\n 5WPF6K36nKK7x9CY0gfhzCaBJLRPcCACUyp47cU7u+L/cfLeRiMqHJZKE6tsxki8NiSE\n QCe+sAMBwGHmhNZIkof2f0/Mj8vl7sUnKZLHxko/B//U2VhPYui/TylGXTfqeWi2CFKD\n Zo1iA8f3taCmxJ2DtuEYNKk7HMAlJA7/ZU9Ne5NBZD9yFSzrOGj5BKbIe5W83ZUPB3tG\n xoXw==","X-Gm-Message-State":"AOJu0YwqZjcmtS4SAsmqhau9rtHbk+4Tr0ejoXp+/TuOKVf9v3Xi9jYG\n tG/4a3ZRlXA1oJx++dcwJLuT4mlJUxRSsp8umkSZjDXUwRpSRKTwtqgw+bYchQKtMTM=","X-Gm-Gg":"AeBDiet9t1xbgix2kxbqFD1RuH7BYodDlrnRdcpy25oHiXEbW3SIDtv4sinRs0h8YeP\n vl/IBhBMWP+VZGJUS1Xg952IkE25yM4y8jYnK4D0S1oRxR6nm1Bqbr8Fh76zTCJLZRvTVyHLVP4\n 3qa3vJKQKh413FYGOP1V01myzU0A1WLeLQ7g3JDDp6WsuMeuSHJ/z1xXneDlZH+US8NrOn1Tfhm\n RJcF/bukqqoXeEpN3eOBydrTjkhwP+juNvSYchERlMTiFZnWU09o5uVKOsALbEG7cVFheWERSPu\n so0u0kWBiFv6FPNAl68yh45oczJW0LmkvAFNOqRU3QWFqRq8qS1Vh1czUpJiv6aMiupnFwa7kFQ\n nFzo3yX8u9QdBCq/hz1wYgAEz4wg7aub3kg78SDkOeKO4klo9zrVqTiMtr4dYJLNHgvYdjgvdI9\n 2c8HNm7HZKscPINu8ICVSw2LIQBLRDUe3LQAJtW0oPggr4","X-Received":"by 2002:a05:600c:4714:b0:48a:5574:3a5d with SMTP id\n 5b1f17b1804b1-48a55743e96mr105271025e9.7.1776862951984;\n Wed, 22 Apr 2026 06:02:31 -0700 (PDT)","From":"=?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>","To":"qemu-devel@nongnu.org","Cc":"Pedro Barbuda <pbarbuda@microsoft.com>, Alexander Graf <agraf@csgraf.de>,\n Peter Maydell <peter.maydell@linaro.org>,\n Paolo Bonzini <pbonzini@redhat.com>,\n Mohamed Mediouni <mohamed@unpredictable.fr>, kvm@vger.kernel.org,\n qemu-arm@nongnu.org, =?utf-8?q?Alex_Benn=C3=A9e?= <alex.bennee@linaro.org>","Subject":"[PATCH v3 30/32] target/arm: enable event stream on WFE instructions","Date":"Wed, 22 Apr 2026 13:52:47 +0100","Message-ID":"<20260422125250.1303100-31-alex.bennee@linaro.org>","X-Mailer":"git-send-email 2.47.3","In-Reply-To":"<20260422125250.1303100-1-alex.bennee@linaro.org>","References":"<20260422125250.1303100-1-alex.bennee@linaro.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Received-SPF":"pass client-ip=2a00:1450:4864:20::32a;\n envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x32a.google.com","X-Spam_score_int":"-20","X-Spam_score":"-2.1","X-Spam_bar":"--","X-Spam_report":"(-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,\n SPF_PASS=-0.001 autolearn=ham autolearn_force=no","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"qemu development <qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org"},"content":"Two generic timers (K and H) are capable of generating timer event\nstream events. Provide a helper to calculate when the nearest one will\nhappen.\n\nNow we can calculate when the next event stream event is we can re-use\nthe wfxt_timer and configure it to fire as we enter a WFE that is\ngoing to sleep. Reverse the M-profile logic so we can enter a sleep\nstate in both profiles.\n\nSigned-off-by: Alex Bennée <alex.bennee@linaro.org>\n\n---\nv2\n  - merged target/arm: add gt_calc_next_event_stream\n  - update to use halt_reason\n  - made arm_wfxt_timer_cb atomically consume halt_reason\n---\n target/arm/cpu.c           |  13 ++++\n target/arm/tcg/op_helper.c | 129 +++++++++++++++++++++++++++++++------\n 2 files changed, 122 insertions(+), 20 deletions(-)","diff":"diff --git a/target/arm/cpu.c b/target/arm/cpu.c\nindex 124be8c401e..7a6ad45ae08 100644\n--- a/target/arm/cpu.c\n+++ b/target/arm/cpu.c\n@@ -799,10 +799,23 @@ bool arm_cpu_exec_halt(CPUState *cs)\n }\n #endif\n \n+/*\n+ * Unlike almost everything else that messes with the halt_reason and\n+ * event_register details the timer callbacks are not in the vCPU\n+ * context.\n+ *\n+ * To prevent races we atomically consume a HALT_WFE and set the event\n+ * register. Either way we trigger the an exit event.\n+ */\n static void arm_wfxt_timer_cb(void *opaque)\n {\n     ARMCPU *cpu = opaque;\n     CPUState *cs = CPU(cpu);\n+    CPUARMState *env = &cpu->env;\n+\n+    if (qatomic_cmpxchg(&env->halt_reason, HALT_WFE, NOT_HALTED)) {\n+        qatomic_set(&env->event_register, true);\n+    }\n \n     /*\n      * We expect the CPU to be halted; this will cause arm_cpu_is_work()\ndiff --git a/target/arm/tcg/op_helper.c b/target/arm/tcg/op_helper.c\nindex 6a2be85fcaa..030209360e8 100644\n--- a/target/arm/tcg/op_helper.c\n+++ b/target/arm/tcg/op_helper.c\n@@ -477,6 +477,97 @@ void HELPER(sev)(CPUARMState *env)\n     arm_broadcast_event();\n }\n \n+#ifndef CONFIG_USER_ONLY\n+/*\n+ * Event Stream events don't do anything apart from wake up sleeping\n+ * cores. These helpers calculate the next event stream event time so\n+ * the WFE helper can decide when its next wake up tick will be.\n+ */\n+static int64_t gt_recalc_one_evt(CPUARMState *env, uint32_t control, uint64_t offset)\n+{\n+    ARMCPU *cpu = env_archcpu(env);\n+    bool evnten = FIELD_EX32(control, CNTxCTL, EVNTEN);\n+\n+    if (evnten) {\n+        int evnti = FIELD_EX32(control, CNTxCTL, EVNTI);\n+        bool evntis = FIELD_EX32(control, CNTxCTL, EVNTIS);\n+        bool evntdir = FIELD_EX32(control, CNTxCTL, EVNTDIR);\n+        /*\n+         * To figure out when the next event timer should fire we need\n+         * to calculate which bit of the counter we want to flip and\n+         * which transition counts.\n+         *\n+         * So we calculate 1 << bit - current lower bits and then add\n+         * 1 << bit if the bit needs to flip twice to meet evntdir\n+         */\n+        int bit = evntis ? evnti + 8 : evnti;\n+        uint64_t count = gt_get_countervalue(env) - offset;\n+        uint64_t target_bit = BIT_ULL(bit);\n+        uint64_t lower_bits = MAKE_64BIT_MASK(0, bit - 1);\n+        uint64_t next_tick = target_bit - (count & lower_bits);\n+        uint64_t abstick;\n+\n+        /* do we need to bit flip twice? */\n+        if (((count & target_bit) != 0) ^ evntdir) {\n+            next_tick += target_bit;\n+        }\n+\n+        /*\n+         * Note that the desired next expiry time might be beyond the\n+         * signed-64-bit range of a QEMUTimer -- in this case we just\n+         * set the timer for as far in the future as possible. When the\n+         * timer expires we will reset the timer for any remaining period.\n+         */\n+        if (uadd64_overflow(next_tick, offset, &abstick)) {\n+            abstick = UINT64_MAX;\n+        }\n+        if (abstick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {\n+            return INT64_MAX;\n+        } else {\n+            return abstick;\n+        }\n+    }\n+\n+    return -1;\n+}\n+\n+/*\n+ * Calculate the next event stream time and return it. Returns -1 if\n+ * no event streams are enabled. It is up to the WFE helpers to decide\n+ * on the next time.\n+ */\n+static int64_t gt_calc_next_event_stream(CPUARMState *env)\n+{\n+    ARMCPU *cpu = env_archcpu(env);\n+    uint64_t hcr = arm_hcr_el2_eff(env);\n+    int64_t next_time = -1;\n+    uint64_t offset;\n+\n+    /* Unless we are missing EL2 this can generate events */\n+    if (arm_feature(env, ARM_FEATURE_EL2)) {\n+        offset = gt_direct_access_timer_offset(env, GTIMER_PHYS);\n+        next_time = gt_recalc_one_evt(env, env->cp15.cnthctl_el2, offset);\n+    }\n+\n+    /* Event stream events from virtual counter enabled? */\n+    if (!cpu_isar_feature(aa64_vh, cpu) ||\n+        !((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE))) {\n+        int64_t next_virt_time;\n+        offset = gt_direct_access_timer_offset(env, GTIMER_VIRT);\n+        next_virt_time = gt_recalc_one_evt(env, env->cp15.c14_cntkctl, offset);\n+\n+        /* is this earlier than the next physical event? */\n+        if (next_virt_time > 0) {\n+            if (next_time < 0 || next_virt_time < next_time) {\n+                next_time = next_virt_time;\n+            }\n+        }\n+    }\n+\n+    return next_time;\n+}\n+#endif\n+\n void HELPER(wfe)(CPUARMState *env)\n {\n #ifdef CONFIG_USER_ONLY\n@@ -489,32 +580,30 @@ void HELPER(wfe)(CPUARMState *env)\n #else\n     /*\n      * WFE (Wait For Event) is a hint instruction.\n-     * For Cortex-M (M-profile), we implement the strict architectural behavior:\n+     *\n      * 1. Check the Event Register (set by SEV or SEVONPEND).\n      * 2. If set, clear it and continue (consume the event).\n      */\n-    if (arm_feature(env, ARM_FEATURE_M)) {\n-        CPUState *cs = env_cpu(env);\n+    CPUState *cs = env_cpu(env);\n+    ARMCPU *cpu = ARM_CPU(cs);\n \n-        if (env->event_register) {\n-            env->event_register = false;\n-            return;\n-        }\n+    if (env->event_register) {\n+        env->event_register = false;\n+        return;\n+    }\n \n-        env->halt_reason = HALT_WFE;\n-        cs->exception_index = EXCP_HLT;\n-        cs->halted = 1;\n-        cpu_loop_exit(cs);\n-    } else {\n-        /*\n-         * For A-profile and others, we rely on the existing \"yield\" behavior.\n-         * Don't actually halt the CPU, just yield back to top\n-         * level loop. This is not going into a \"low power state\"\n-         * (ie halting until some event occurs), so we never take\n-         * a configurable trap to a different exception level\n-         */\n-        HELPER(yield)(env);\n+    /* For A-profile we also can be woken by the event stream */\n+    if (arm_feature(env, ARM_FEATURE_AARCH64) && cpu->wfxt_timer) {\n+        int64_t next_event = gt_calc_next_event_stream(env);\n+        if (next_event > 0) {\n+            timer_mod(cpu->wfxt_timer, next_event);\n+        }\n     }\n+\n+    env->halt_reason = HALT_WFE;\n+    cs->exception_index = EXCP_HLT;\n+    cs->halted = 1;\n+    cpu_loop_exit(cs);\n #endif\n }\n \n","prefixes":["v3","30/32"]}