qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] qemu/timer: Add host ticks function for RISC-V
@ 2023-09-11  6:32 LIU Zhiwei
  0 siblings, 0 replies; 2+ messages in thread
From: LIU Zhiwei @ 2023-09-11  6:32 UTC (permalink / raw)
  To: pbonzini
  Cc: qemu-devel, qemu-riscv, atishp, palmer, richard.henderson,
	LIU Zhiwei

Signed-off-by: LIU Zhiwei <zhiwei_liu@linux.alibaba.com>
---
v2:
 1) Use rdtime instead of rdcycle for dynamic cpuclk adjustment.
 2) Read timeh twice in case of time overflow for 32-bit cpu.
---
 include/qemu/timer.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index 9a91cb1248..9a366e551f 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -979,6 +979,28 @@ static inline int64_t cpu_get_host_ticks(void)
     return cur - ofs;
 }
 
+#elif defined(__riscv) && __riscv_xlen == 32
+static inline int64_t cpu_get_host_ticks(void)
+{
+    uint32_t lo, hi, tmph;
+    do {
+        asm volatile("RDTIMEH %0\n\t"
+                     "RDTIME %1\n\t"
+                     "RDTIMEH %2"
+                     : "=r"(hi), "=r"(lo), "=r"(tmph));
+    } while (unlikely(tmph != hi));
+    return lo | (uint64_t)hi << 32;
+}
+
+#elif defined(__riscv) && __riscv_xlen > 32
+static inline int64_t cpu_get_host_ticks(void)
+{
+    int64_t val;
+
+    asm volatile("RDTIME %0" : "=r"(val));
+    return val;
+}
+
 #else
 /* The host CPU doesn't have an easily accessible cycle counter.
    Just return a monotonically increasing value.  This will be
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] qemu/timer: Add host ticks function for RISC-V
       [not found] <20230911063223.742-1-zhiwei._5Fliu@linux.alibaba.com>
@ 2023-09-15 11:47 ` Paolo Bonzini
  0 siblings, 0 replies; 2+ messages in thread
From: Paolo Bonzini @ 2023-09-15 11:47 UTC (permalink / raw)
  To: LIU Zhiwei
  Cc: pbonzini, qemu-devel, qemu-riscv, atishp, palmer,
	richard.henderson

Queued, thanks.

Paolo



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-09-15 11:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20230911063223.742-1-zhiwei._5Fliu@linux.alibaba.com>
2023-09-15 11:47 ` [PATCH v2] qemu/timer: Add host ticks function for RISC-V Paolo Bonzini
2023-09-11  6:32 LIU Zhiwei

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).