qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Luc Michel <luc.michel@amd.com>
To: <qemu-devel@nongnu.org>, <qemu-riscv@nongnu.org>
Cc: Luc Michel <luc.michel@amd.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Alistair Francis <alistair.francis@wdc.com>,
	Weiwei Li <liwei1518@gmail.com>,
	 Daniel Henrique Barboza <dbarboza@ventanamicro.com>,
	Liu Zhiwei <zhiwei_liu@linux.alibaba.com>,
	Francisco Iglesias <francisco.iglesias@amd.com>
Subject: [PATCH 4/9] target/riscv: add the RISCVCPUTimeSrcIf interface
Date: Fri, 7 Nov 2025 11:23:30 +0100	[thread overview]
Message-ID: <20251107102340.471141-5-luc.michel@amd.com> (raw)
In-Reply-To: <20251107102340.471141-1-luc.michel@amd.com>

Add the RISCVCPUTimeSrcIf QOM interface to the RISC-V target. This
interface aims at replacing the existing env->rdtime_fn callback in the
RISC-V CPU env. It allows to query the current number of ticks, and the
tick frequency.

Signed-off-by: Luc Michel <luc.michel@amd.com>
---
 target/riscv/cpu-qom.h     | 34 ++++++++++++++++++++++++++++++++++
 target/riscv/time_helper.h | 16 ++++++++++++++++
 target/riscv/time_helper.c | 13 +++++++++++++
 3 files changed, 63 insertions(+)

diff --git a/target/riscv/cpu-qom.h b/target/riscv/cpu-qom.h
index 75f4e434085..e5bc23b2ef5 100644
--- a/target/riscv/cpu-qom.h
+++ b/target/riscv/cpu-qom.h
@@ -58,6 +58,40 @@
 #define TYPE_RISCV_CPU_XIANGSHAN_KMH    RISCV_CPU_TYPE_NAME("xiangshan-kunminghu")
 #define TYPE_RISCV_CPU_HOST             RISCV_CPU_TYPE_NAME("host")
 
 OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
 
+#define TYPE_RISCV_CPU_TIME_SRC_IF "riscv-cpu-time-src-if"
+
+typedef struct RISCVCPUTimeSrcIfClass RISCVCPUTimeSrcIfClass;
+DECLARE_CLASS_CHECKERS(RISCVCPUTimeSrcIfClass, RISCV_CPU_TIME_SRC_IF,
+                       TYPE_RISCV_CPU_TIME_SRC_IF)
+#define RISCV_CPU_TIME_SRC_IF(obj) \
+        INTERFACE_CHECK(RISCVCPUTimeSrcIf, (obj), TYPE_RISCV_CPU_TIME_SRC_IF)
+
+typedef struct RISCVCPUTimeSrcIf RISCVCPUTimeSrcIf;
+
+/**
+ * RISCVCPUTimeSrcIf interface
+ *
+ * This interface is used by CPUs implementing the sstc extension. When the CPU
+ * implements this extension, it must have a time source to implement the sstc
+ * timers. Devices implementing this interface provide a monotonic tick counter
+ * and the associated tick frequency so that the CPU code can compute timer
+ * deadlines.
+ */
+struct RISCVCPUTimeSrcIfClass {
+    InterfaceClass parent_class;
+
+    /**
+     * get_ticks: get the current value of the free running counter associated
+     * with this time source.
+     */
+    uint64_t (*get_ticks)(RISCVCPUTimeSrcIf *);
+
+    /**
+     * get_tick_freq: get the tick frequency of this time source.
+     */
+    uint32_t (*get_tick_freq)(RISCVCPUTimeSrcIf *);
+};
+
 #endif /* RISCV_CPU_QOM_H */
diff --git a/target/riscv/time_helper.h b/target/riscv/time_helper.h
index af1f634f890..b51fdd96570 100644
--- a/target/riscv/time_helper.h
+++ b/target/riscv/time_helper.h
@@ -26,6 +26,22 @@ void riscv_timer_write_timecmp(CPURISCVState *env, QEMUTimer *timer,
                                uint64_t timecmp, uint64_t delta,
                                uint32_t timer_irq);
 void riscv_timer_stce_changed(CPURISCVState *env, bool is_m_mode, bool enable);
 void riscv_timer_init(RISCVCPU *cpu);
 
+static inline uint64_t riscv_cpu_time_src_get_ticks(RISCVCPUTimeSrcIf *src)
+{
+    RISCVCPUTimeSrcIfClass *rctsc = RISCV_CPU_TIME_SRC_IF_GET_CLASS(src);
+
+    g_assert(rctsc->get_ticks != NULL);
+    return rctsc->get_ticks(src);
+}
+
+static inline uint32_t riscv_cpu_time_src_get_tick_freq(RISCVCPUTimeSrcIf *src)
+{
+    RISCVCPUTimeSrcIfClass *rctsc = RISCV_CPU_TIME_SRC_IF_GET_CLASS(src);
+
+    g_assert(rctsc->get_tick_freq != NULL);
+    return rctsc->get_tick_freq(src);
+}
+
 #endif
diff --git a/target/riscv/time_helper.c b/target/riscv/time_helper.c
index d2ec8a94166..dc0777607ab 100644
--- a/target/riscv/time_helper.c
+++ b/target/riscv/time_helper.c
@@ -198,5 +198,18 @@ void riscv_timer_init(RISCVCPU *cpu)
     env->stimecmp = 0;
 
     env->vstimer = timer_new_ns(QEMU_CLOCK_VIRTUAL, &riscv_vstimer_cb, cpu);
     env->vstimecmp = 0;
 }
+
+static const TypeInfo riscv_cpu_time_src_if_info = {
+    .name = TYPE_RISCV_CPU_TIME_SRC_IF,
+    .parent = TYPE_INTERFACE,
+    .class_size = sizeof(RISCVCPUTimeSrcIfClass),
+};
+
+static void riscv_cpu_time_src_if_register_types(void)
+{
+    type_register_static(&riscv_cpu_time_src_if_info);
+}
+
+type_init(riscv_cpu_time_src_if_register_types)
-- 
2.51.0



  parent reply	other threads:[~2025-11-07 10:25 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-07 10:23 [PATCH 0/9] RISC-V CPU time source interface Luc Michel
2025-11-07 10:23 ` [PATCH 1/9] target/riscv: drop unused include directive in time_helper.c Luc Michel
2025-11-09 14:03   ` Philippe Mathieu-Daudé
2025-11-07 10:23 ` [PATCH 2/9] hw/intc/riscv_aclint: fix coding style Luc Michel
2025-11-09 13:56   ` Philippe Mathieu-Daudé
2025-11-07 10:23 ` [PATCH 3/9] hw/intc/riscv_aclint: rename cpu_riscv_read_rtc to riscv_aclint_mtimer_get_ticks Luc Michel
2025-11-09 13:56   ` Philippe Mathieu-Daudé
2025-11-07 10:23 ` Luc Michel [this message]
2025-11-09 14:02   ` [PATCH 4/9] target/riscv: add the RISCVCPUTimeSrcIf interface Philippe Mathieu-Daudé
2025-11-07 10:23 ` [PATCH 5/9] hw/intc/riscv_aclint: implement " Luc Michel
2025-11-09 14:01   ` Philippe Mathieu-Daudé
2025-11-07 10:23 ` [PATCH 6/9] target/riscv: replace env->rdtime_fn with a time source Luc Michel
2025-11-09 13:59   ` Philippe Mathieu-Daudé
2025-11-07 10:23 ` [PATCH 7/9] hw/intc/riscv_aclint: riscv_aclint_mtimer_get_ticks: get rid of void* argument Luc Michel
2025-11-09 13:59   ` Philippe Mathieu-Daudé
2025-11-07 10:23 ` [PATCH 8/9] target/riscv: RISCVCPUTimeSrcIf: add register_time_change_notifier Luc Michel
2025-11-20 17:20   ` Philippe Mathieu-Daudé
2025-11-21  8:24     ` Luc Michel
2025-11-07 10:23 ` [PATCH 9/9] hw/intc/riscv_aclint: implement the register_time_change_notifier method Luc Michel
2025-11-20 17:22   ` Philippe Mathieu-Daudé
2025-11-14  9:25 ` [PATCH 0/9] RISC-V CPU time source interface Luc Michel
2025-11-20 17:16   ` Philippe Mathieu-Daudé
2025-12-05  8:29     ` Luc Michel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251107102340.471141-5-luc.michel@amd.com \
    --to=luc.michel@amd.com \
    --cc=alistair.francis@wdc.com \
    --cc=dbarboza@ventanamicro.com \
    --cc=francisco.iglesias@amd.com \
    --cc=liwei1518@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=zhiwei_liu@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).