From: LIU Zhiwei <zhiwei_liu@linux.alibaba.com>
To: pbonzini@redhat.com
Cc: qemu-devel@nongnu.org, qemu-riscv@nongnu.org,
atishp@atishpatra.org, palmer@dabbelt.com,
richard.henderson@linaro.org,
LIU Zhiwei <zhiwei_liu@linux.alibaba.com>
Subject: [PATCH v2] qemu/timer: Add host ticks function for RISC-V
Date: Mon, 11 Sep 2023 14:32:23 +0800 [thread overview]
Message-ID: <20230911063223.742-1-zhiwei_liu@linux.alibaba.com> (raw)
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
next reply other threads:[~2023-09-11 6:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-11 6:32 LIU Zhiwei [this message]
[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
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=20230911063223.742-1-zhiwei_liu@linux.alibaba.com \
--to=zhiwei_liu@linux.alibaba.com \
--cc=atishp@atishpatra.org \
--cc=palmer@dabbelt.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-riscv@nongnu.org \
--cc=richard.henderson@linaro.org \
/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).