From: "Philippe Mathieu-Daudé via" <qemu-devel@nongnu.org>
To: Tyler Ng <tkng@rivosinc.com>,
"open list:RISC-V" <qemu-riscv@nongnu.org>,
"qemu-devel@nongnu.org Developers" <qemu-devel@nongnu.org>
Cc: Alistair Francis <Alistair.Francis@wdc.com>,
Bin Meng <bin.meng@windriver.com>, Thomas Huth <thuth@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Laurent Vivier <lvivier@redhat.com>
Subject: Re: [PATCH v2 3/3] hw/timer: ibex_timer.c: Add support for writes to mtime
Date: Thu, 22 Sep 2022 23:45:49 +0200 [thread overview]
Message-ID: <99d260b4-2e06-e64b-48b0-f1ecbec8b39b@amsat.org> (raw)
In-Reply-To: <CAB88-qOuB+uR6epr7h5F76A9qNOB358WikXDfmpvn-ceW71p7Q@mail.gmail.com>
On 22/9/22 17:58, Tyler Ng wrote:
> 1. Adds fields to hold the value of mtime in timer_upper0 and timer_lower0.
>
> 2. Changes the read and write functions to use the mtime fields.
>
> 3. Updates the value of mtime in update_mtime() by extrapolating the
> time elapsed. This will need to change if/when the prescalar is
> implemented.
>
> 4. Adds a qtest for the ibex timer.
>
> Signed-off-by: Tyler Ng <tkng@rivosinc.com>
> ---
> hw/timer/ibex_timer.c | 98 +++++++++++++------
> include/hw/timer/ibex_timer.h | 6 ++
> tests/qtest/ibex-timer-test.c | 178 ++++++++++++++++++++++++++++++++++
> tests/qtest/meson.build | 3 +-
> 4 files changed, 256 insertions(+), 29 deletions(-)
> create mode 100644 tests/qtest/ibex-timer-test.c
> -static void ibex_timer_update_irqs(IbexTimerState *s)
> +/*
> + * The goal of this function is to:
> + * 1. Check if the timer is enabled. If not, return false,
> + * 2. Calculate the amount of time that has passed since.
> + * 3. Extrapolate the number of ticks that have passed, and add it to `mtime`.
> + * 4. Return true.
> + */
> +static bool update_mtime(IbexTimerState *s)
> {
> - uint64_t value = s->timer_compare_lower0 |
> - ((uint64_t)s->timer_compare_upper0 << 32);
> - uint64_t next, diff;
> - uint64_t now = cpu_riscv_read_rtc(s->timebase_freq);
> -
> if (!(s->timer_ctrl & R_CTRL_ACTIVE_MASK)) {
> - /* Timer isn't active */
> + return false;
> + }
> + /* Get the time then extrapolate the number of ticks that have elapsed */
> + uint64_t mtime = get_mtime(s);
> + int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
> + int64_t elapsed = now - s->timer_last_update;
> + if (elapsed < 0) {
> + /* We jumped back in time. */
> + mtime -= muldiv64((uint64_t)(-elapsed), s->timebase_freq,
> + NANOSECONDS_PER_SECOND);
> + } else {
> + mtime += muldiv64(elapsed, s->timebase_freq, NANOSECONDS_PER_SECOND);
> + }
> + s->timer_lower0 = mtime & 0xffffffff;
> + s->timer_upper0 = (mtime >> 32) & 0xffffffff;
Could use extract64(mtime, 0, 32) and extract64(mtime, 32, 32);
> + /* update last-checkpoint timestamp */
> + s->timer_last_update = now;
> + return true;
> +}
next prev parent reply other threads:[~2022-09-22 21:49 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-22 15:58 [PATCH v2 3/3] hw/timer: ibex_timer.c: Add support for writes to mtime Tyler Ng
2022-09-22 21:45 ` Philippe Mathieu-Daudé via [this message]
2022-09-26 23:37 ` Tyler Ng
2022-09-26 21:05 ` Dong, Eddie
2022-09-26 23:38 ` Tyler Ng
2022-09-28 22:59 ` Dong, Eddie
2022-09-29 16:02 ` Tyler Ng
2022-09-29 18:11 ` Dong, Eddie
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=99d260b4-2e06-e64b-48b0-f1ecbec8b39b@amsat.org \
--to=qemu-devel@nongnu.org \
--cc=Alistair.Francis@wdc.com \
--cc=bin.meng@windriver.com \
--cc=f4bug@amsat.org \
--cc=lvivier@redhat.com \
--cc=palmer@dabbelt.com \
--cc=pbonzini@redhat.com \
--cc=qemu-riscv@nongnu.org \
--cc=thuth@redhat.com \
--cc=tkng@rivosinc.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).