From: Havard Skinnemoen <hskinnemoen@google.com>
To: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
Cc: Peter Maydell <peter.maydell@linaro.org>,
qemu-arm <qemu-arm@nongnu.org>,
QEMU Developers <qemu-devel@nongnu.org>,
IS20 Avi Fishman <Avi.Fishman@nuvoton.com>,
CS20 KFTing <kfting@nuvoton.com>, Joel Stanley <joel@jms.id.au>
Subject: Re: [PATCH v5 03/11] hw/timer: Add NPCM7xx Timer device model
Date: Wed, 15 Jul 2020 16:04:37 -0700 [thread overview]
Message-ID: <CAFQmdRZPMdvXYLQqXMhnv3DT8SHYjvPSBHg-v2YaDb7Zi3xc3Q@mail.gmail.com> (raw)
In-Reply-To: <43d97595-ef79-0ecd-a13f-25c7a986b869@amsat.org>
On Wed, Jul 15, 2020 at 12:25 AM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>
> On 7/9/20 2:36 AM, Havard Skinnemoen wrote:
> > The NPCM730 and NPCM750 SoCs have three timer modules each holding five
> > timers and some shared registers (e.g. interrupt status).
> >
> > Each timer runs at 25 MHz divided by a prescaler, and counts down from a
> > configurable initial value to zero. When zero is reached, the interrupt
> > flag for the timer is set, and the timer is disabled (one-shot mode) or
> > reloaded from its initial value (periodic mode).
> >
> > This implementation is sufficient to boot a Linux kernel configured for
> > NPCM750. Note that the kernel does not seem to actually turn on the
> > interrupts.
> >
> > Reviewed-by: Tyrone Ting <kfting@nuvoton.com>
> > Reviewed-by: Joel Stanley <joel@jms.id.au>
> > Signed-off-by: Havard Skinnemoen <hskinnemoen@google.com>
> > ---
> > include/hw/timer/npcm7xx_timer.h | 96 +++++++
> > hw/timer/npcm7xx_timer.c | 468 +++++++++++++++++++++++++++++++
> > hw/timer/Makefile.objs | 1 +
> > hw/timer/trace-events | 5 +
> > 4 files changed, 570 insertions(+)
> > create mode 100644 include/hw/timer/npcm7xx_timer.h
> > create mode 100644 hw/timer/npcm7xx_timer.c
> >
> ...
>
> > +/* The reference clock frequency is always 25 MHz. */
> > +#define NPCM7XX_TIMER_REF_HZ (25000000)
> > +
> > +/* Return the value by which to divide the reference clock rate. */
> > +static uint32_t npcm7xx_timer_prescaler(const NPCM7xxTimer *t)
> > +{
> > + return extract32(t->tcsr, NPCM7XX_TCSR_PRESCALE_START,
> > + NPCM7XX_TCSR_PRESCALE_LEN) + 1;
> > +}
> > +
> > +/* Convert a timer cycle count to a time interval in nanoseconds. */
> > +static int64_t npcm7xx_timer_count_to_ns(NPCM7xxTimer *t, uint32_t count)
> > +{
> > + int64_t ns = count;
> > +
> > + ns *= NANOSECONDS_PER_SECOND / NPCM7XX_TIMER_REF_HZ;
> > + ns *= npcm7xx_timer_prescaler(t);
> > +
> > + return ns;
> > +}
> > +
> > +/* Convert a time interval in nanoseconds to a timer cycle count. */
> > +static uint32_t npcm7xx_timer_ns_to_count(NPCM7xxTimer *t, int64_t ns)
> > +{
> > + int64_t count;
> > +
> > + count = ns / (NANOSECONDS_PER_SECOND / NPCM7XX_TIMER_REF_HZ);
> > + count /= npcm7xx_timer_prescaler(t);
> > +
> > + return count;
> > +}
> > +
> > +/*
> > + * Raise the interrupt line if there's a pending interrupt and interrupts are
> > + * enabled for this timer. If not, lower it.
> > + */
> > +static void npcm7xx_timer_check_interrupt(NPCM7xxTimer *t)
> > +{
> > + NPCM7xxTimerCtrlState *tc = t->ctrl;
> > + /* Find the array index of this timer. */
> > + int index = t - tc->timer;
>
> As you suggested in another device in this series, using a getter
> here is clearer.
Definitely.
> > +
> > + g_assert(index >= 0 && index < NPCM7XX_TIMERS_PER_CTRL);
> > +
> > + if ((t->tcsr & NPCM7XX_TCSR_IE) && (tc->tisr & BIT(index))) {
> > + qemu_irq_raise(t->irq);
> > + trace_npcm7xx_timer_irq(DEVICE(tc)->canonical_path, index, 1);
> > + } else {
> > + qemu_irq_lower(t->irq);
> > + trace_npcm7xx_timer_irq(DEVICE(tc)->canonical_path, index, 0);
> > + }
> > +}
> > +
> > +/* Start or resume the timer. */
> > +static void npcm7xx_timer_start(NPCM7xxTimer *t)
> > +{
> > + int64_t now;
> > +
> > + now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
> > + t->expires_ns = now + t->remaining_ns;
> > + timer_mod(&t->qtimer, t->expires_ns);
> > +}
> > +
> > +/*
> > + * Called when the counter reaches zero. Sets the interrupt flag, and either
> > + * restarts or disables the timer.
> > + */
> > +static void npcm7xx_timer_reached_zero(NPCM7xxTimer *t)
> > +{
> > + NPCM7xxTimerCtrlState *tc = t->ctrl;
> > + int index = t - tc->timer;
> > +
> > + g_assert(index >= 0 && index < NPCM7XX_TIMERS_PER_CTRL);
> > +
> > + tc->tisr |= BIT(index);
> > +
> > + if (t->tcsr & NPCM7XX_TCSR_PERIODIC) {
> > + t->remaining_ns = npcm7xx_timer_count_to_ns(t, t->ticr);
> > + if (t->tcsr & NPCM7XX_TCSR_CEN) {
> > + npcm7xx_timer_start(t);
> > + }
> > + } else {
> > + t->tcsr &= ~(NPCM7XX_TCSR_CEN | NPCM7XX_TCSR_CACT);
> > + }
> > +
> > + npcm7xx_timer_check_interrupt(t);
> > +}
> > +
> > +/* Stop counting. Record the time remaining so we can continue later. */
> > +static void npcm7xx_timer_pause(NPCM7xxTimer *t)
> > +{
> > + int64_t now;
> > +
> > + timer_del(&t->qtimer);
> > + now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
> > + t->remaining_ns = t->expires_ns - now;
> > + if (t->remaining_ns <= 0) {
>
> Can this happen? Shouldn't we get npcm7xx_timer_expired() before?
I was thinking the timer might expire right after calling timer_del(),
and handling it before we expire the timer makes bookkeeping easier.
But if QEMU_CLOCK_VIRTUAL is stopped while this code is running (even
on multi-cpu systems?), then I agree it can't happen.
If it can't possibly happen, then it should be appropriate to add
g_assert(t->remaining_ns > 0);
right?
> > + npcm7xx_timer_reached_zero(t);
> > + }
> > +}
> > +
> > +/*
> > + * Restart the timer from its initial value. If the timer was enabled and stays
> > + * enabled, adjust the QEMU timer according to the new count. If the timer is
> > + * transitioning from disabled to enabled, the caller is expected to start the
> > + * timer later.
> > + */
> > +static void npcm7xx_timer_restart(NPCM7xxTimer *t, uint32_t old_tcsr)
> > +{
> > + t->remaining_ns = npcm7xx_timer_count_to_ns(t, t->ticr);
> > +
> > + if (old_tcsr & t->tcsr & NPCM7XX_TCSR_CEN) {
> > + npcm7xx_timer_start(t);
> > + }
> > +}
> > +
> > +/* Register read and write handlers */
> > +
> > +static void npcm7xx_timer_write_tcsr(NPCM7xxTimer *t, uint32_t new_tcsr)
> > +{
> > + uint32_t old_tcsr = t->tcsr;
> > +
> > + if (new_tcsr & NPCM7XX_TCSR_RSVD) {
> > + qemu_log_mask(LOG_GUEST_ERROR, "%s: reserved bits in 0x%08x ignored\n",
> > + __func__, new_tcsr);
> > + new_tcsr &= ~NPCM7XX_TCSR_RSVD;
> > + }
> > + if (new_tcsr & NPCM7XX_TCSR_CACT) {
> > + qemu_log_mask(LOG_GUEST_ERROR, "%s: read-only bits in 0x%08x ignored\n",
> > + __func__, new_tcsr);
> > + new_tcsr &= ~NPCM7XX_TCSR_CACT;
> > + }
> > +
> > + t->tcsr = (t->tcsr & NPCM7XX_TCSR_CACT) | new_tcsr;
> > +
> > + if ((old_tcsr ^ new_tcsr) & NPCM7XX_TCSR_IE) {
> > + npcm7xx_timer_check_interrupt(t);
> > + }
> > + if (new_tcsr & NPCM7XX_TCSR_CRST) {
> > + npcm7xx_timer_restart(t, old_tcsr);
> > + t->tcsr &= ~NPCM7XX_TCSR_CRST;
> > + }
> > + if ((old_tcsr ^ new_tcsr) & NPCM7XX_TCSR_CEN) {
> > + if (new_tcsr & NPCM7XX_TCSR_CEN) {
> > + npcm7xx_timer_start(t);
> > + } else {
> > + npcm7xx_timer_pause(t);
> > + }
> > + }
> > +}
> > +
> > +static void npcm7xx_timer_write_ticr(NPCM7xxTimer *t, uint32_t new_ticr)
> > +{
> > + t->ticr = new_ticr;
> > +
> > + npcm7xx_timer_restart(t, t->tcsr);
> > +}
> > +
> > +static uint32_t npcm7xx_timer_read_tdr(NPCM7xxTimer *t)
> > +{
> > + if (t->tcsr & NPCM7XX_TCSR_CEN) {
> > + int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
> > +
> > + return npcm7xx_timer_ns_to_count(t, t->expires_ns - now);
> > + }
> > +
> > + return npcm7xx_timer_ns_to_count(t, t->remaining_ns);
> > +}
> > +
> > +static uint64_t npcm7xx_timer_read(void *opaque, hwaddr offset, unsigned size)
> > +{
> > + NPCM7xxTimerCtrlState *s = opaque;
> > + uint64_t value = 0;
> > + hwaddr reg;
> > +
> > + reg = offset / sizeof(uint32_t);
> > + switch (reg) {
> > + case NPCM7XX_TIMER_TCSR0:
> > + value = s->timer[0].tcsr;
> > + break;
> > + case NPCM7XX_TIMER_TCSR1:
> > + value = s->timer[1].tcsr;
>
> Maybe add:
>
> static hwaddr timer_index(hwaddr reg)
> {
> return reg - NPCM7XX_TIMER_TCSR0;
> }
>
> And use shorter:
>
> case NPCM7XX_TIMER_TCSR0:
> case NPCM7XX_TIMER_TCSR1:
> case NPCM7XX_TIMER_TCSR2:
> case NPCM7XX_TIMER_TCSR3:
> case NPCM7XX_TIMER_TCSR4:
> value = s->timer[timer_index(reg)].tcsr;
> break;
>
> Similarly with NPCM7XX_TIMER_TDRx and in npcm7xx_timer_write().
Sorry, that won't work because the registers for the various modules
are not grouped together.
> > + break;
> > + case NPCM7XX_TIMER_TCSR2:
> > + value = s->timer[2].tcsr;
> > + break;
> > + case NPCM7XX_TIMER_TCSR3:
> > + value = s->timer[3].tcsr;
> > + break;
> > + case NPCM7XX_TIMER_TCSR4:
> > + value = s->timer[4].tcsr;
> > + break;
> > +
> > + case NPCM7XX_TIMER_TICR0:
> > + value = s->timer[0].ticr;
> > + break;
> > + case NPCM7XX_TIMER_TICR1:
> > + value = s->timer[1].ticr;
> > + break;
> > + case NPCM7XX_TIMER_TICR2:
> > + value = s->timer[2].ticr;
> > + break;
> > + case NPCM7XX_TIMER_TICR3:
> > + value = s->timer[3].ticr;
> > + break;
> > + case NPCM7XX_TIMER_TICR4:
> > + value = s->timer[4].ticr;
> > + break;
> > +
> > + case NPCM7XX_TIMER_TDR0:
> > + value = npcm7xx_timer_read_tdr(&s->timer[0]);
> > + break;
> > + case NPCM7XX_TIMER_TDR1:
> > + value = npcm7xx_timer_read_tdr(&s->timer[1]);
> > + break;
> > + case NPCM7XX_TIMER_TDR2:
> > + value = npcm7xx_timer_read_tdr(&s->timer[2]);
> > + break;
> > + case NPCM7XX_TIMER_TDR3:
> > + value = npcm7xx_timer_read_tdr(&s->timer[3]);
> > + break;
> > + case NPCM7XX_TIMER_TDR4:
> > + value = npcm7xx_timer_read_tdr(&s->timer[4]);
> > + break;
> > +
> > + case NPCM7XX_TIMER_TISR:
> > + value = s->tisr;
> > + break;
> > +
> > + case NPCM7XX_TIMER_WTCR:
> > + value = s->wtcr;
> > + break;
> > +
> > + default:
> > + qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid offset 0x%04x\n",
> > + __func__, (unsigned int)offset);
> > + break;
> > + }
> > +
> > + trace_npcm7xx_timer_read(DEVICE(s)->canonical_path, offset, value);
> > +
> > + return value;
> > +}
> > +
> > +static void npcm7xx_timer_write(void *opaque, hwaddr offset,
> > + uint64_t v, unsigned size)
> > +{
> > + uint32_t reg = offset / sizeof(uint32_t);
> > + NPCM7xxTimerCtrlState *s = opaque;
> > + uint32_t value = v;
> > +
> > + trace_npcm7xx_timer_write(DEVICE(s)->canonical_path, offset, value);
> > +
> > + switch (reg) {
> > + case NPCM7XX_TIMER_TCSR0:
> > + npcm7xx_timer_write_tcsr(&s->timer[0], value);
> > + return;
> > + case NPCM7XX_TIMER_TCSR1:
> > + npcm7xx_timer_write_tcsr(&s->timer[1], value);
> > + return;
> > + case NPCM7XX_TIMER_TCSR2:
> > + npcm7xx_timer_write_tcsr(&s->timer[2], value);
> > + return;
> > + case NPCM7XX_TIMER_TCSR3:
> > + npcm7xx_timer_write_tcsr(&s->timer[3], value);
> > + return;
> > + case NPCM7XX_TIMER_TCSR4:
> > + npcm7xx_timer_write_tcsr(&s->timer[4], value);
> > + return;
> > +
> > + case NPCM7XX_TIMER_TICR0:
> > + npcm7xx_timer_write_ticr(&s->timer[0], value);
> > + return;
> > + case NPCM7XX_TIMER_TICR1:
> > + npcm7xx_timer_write_ticr(&s->timer[1], value);
> > + return;
> > + case NPCM7XX_TIMER_TICR2:
> > + npcm7xx_timer_write_ticr(&s->timer[2], value);
> > + return;
> > + case NPCM7XX_TIMER_TICR3:
> > + npcm7xx_timer_write_ticr(&s->timer[3], value);
> > + return;
> > + case NPCM7XX_TIMER_TICR4:
> > + npcm7xx_timer_write_ticr(&s->timer[4], value);
> > + return;
> > +
> > + case NPCM7XX_TIMER_TDR0:
> > + case NPCM7XX_TIMER_TDR1:
> > + case NPCM7XX_TIMER_TDR2:
> > + case NPCM7XX_TIMER_TDR3:
> > + case NPCM7XX_TIMER_TDR4:
> > + qemu_log_mask(LOG_GUEST_ERROR, "%s: register @ 0x%04x is read-only\n",
> > + __func__, (unsigned int)offset);
> > + return;
> > +
> > + case NPCM7XX_TIMER_TISR:
> > + s->tisr &= ~value;
> > + return;
> > +
> > + case NPCM7XX_TIMER_WTCR:
> > + qemu_log_mask(LOG_UNIMP, "%s: WTCR write not implemented: 0x%08x\n",
> > + __func__, value);
> > + return;
> > + }
> > +
> > + qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid offset 0x%04x\n",
> > + __func__, (unsigned int)offset);
> > +}
> > +
> > +static const struct MemoryRegionOps npcm7xx_timer_ops = {
> > + .read = npcm7xx_timer_read,
> > + .write = npcm7xx_timer_write,
> > + .endianness = DEVICE_LITTLE_ENDIAN,
> > + .valid = {
> > + .min_access_size = 4,
> > + .max_access_size = 4,
> > + .unaligned = false,
> > + },
> > +};
> > +
> > +/* Called when the QEMU timer expires. */
> > +static void npcm7xx_timer_expired(void *opaque)
> > +{
> > + NPCM7xxTimer *t = opaque;
> > +
> > + if (t->tcsr & NPCM7XX_TCSR_CEN) {
> > + npcm7xx_timer_reached_zero(t);
> > + }
> > +}
> ...
next prev parent reply other threads:[~2020-07-15 23:05 UTC|newest]
Thread overview: 64+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-09 0:35 [PATCH v5 00/11] Add Nuvoton NPCM730/NPCM750 SoCs and two BMC machines Havard Skinnemoen
2020-07-09 0:35 ` [PATCH v5 01/11] hw/misc: Add NPCM7xx System Global Control Registers device model Havard Skinnemoen
2020-07-09 6:04 ` Philippe Mathieu-Daudé
2020-07-09 6:43 ` Havard Skinnemoen
2020-07-09 16:23 ` Philippe Mathieu-Daudé
2020-07-09 17:09 ` Havard Skinnemoen
2020-07-09 17:24 ` Philippe Mathieu-Daudé
2020-07-09 17:42 ` Havard Skinnemoen
2020-07-10 9:31 ` Philippe Mathieu-Daudé
2020-07-11 6:46 ` Havard Skinnemoen
2020-07-12 5:49 ` Havard Skinnemoen
2020-07-09 0:35 ` [PATCH v5 02/11] hw/misc: Add NPCM7xx Clock Controller " Havard Skinnemoen
2020-07-15 7:18 ` Philippe Mathieu-Daudé
2020-07-09 0:36 ` [PATCH v5 03/11] hw/timer: Add NPCM7xx Timer " Havard Skinnemoen
2020-07-15 7:25 ` Philippe Mathieu-Daudé
2020-07-15 23:04 ` Havard Skinnemoen [this message]
2020-07-16 8:04 ` Philippe Mathieu-Daudé
2020-07-09 0:36 ` [PATCH v5 04/11] hw/arm: Add NPCM730 and NPCM750 SoC models Havard Skinnemoen
2020-07-09 6:11 ` Philippe Mathieu-Daudé
2020-07-13 15:02 ` Cédric Le Goater
2020-07-14 0:44 ` Havard Skinnemoen
2020-07-14 11:37 ` Philippe Mathieu-Daudé
2020-07-14 16:01 ` Markus Armbruster
2020-07-14 17:11 ` Philippe Mathieu-Daudé
2020-07-15 1:03 ` Havard Skinnemoen
2020-07-15 9:35 ` Markus Armbruster
2020-07-09 0:36 ` [PATCH v5 05/11] hw/arm: Add two NPCM7xx-based machines Havard Skinnemoen
2020-07-09 5:57 ` Philippe Mathieu-Daudé
2020-07-09 6:09 ` Philippe Mathieu-Daudé
2020-07-09 0:36 ` [PATCH v5 06/11] hw/arm: Load -bios image as a boot ROM for npcm7xx Havard Skinnemoen
2020-07-13 17:50 ` Philippe Mathieu-Daudé
2020-07-09 0:36 ` [PATCH v5 07/11] hw/nvram: NPCM7xx OTP device model Havard Skinnemoen
2020-07-09 0:36 ` [PATCH v5 08/11] hw/mem: Stubbed out NPCM7xx Memory Controller model Havard Skinnemoen
2020-07-09 16:29 ` Philippe Mathieu-Daudé
2020-07-09 0:36 ` [PATCH v5 09/11] hw/ssi: NPCM7xx Flash Interface Unit device model Havard Skinnemoen
2020-07-09 17:00 ` Philippe Mathieu-Daudé
2020-07-12 5:42 ` Havard Skinnemoen
2020-07-13 17:38 ` Philippe Mathieu-Daudé
2020-07-14 2:39 ` Havard Skinnemoen
2020-07-09 0:36 ` [PATCH v5 10/11] hw/arm: Wire up BMC boot flash for npcm750-evb and quanta-gsj Havard Skinnemoen
2020-07-13 14:57 ` Cédric Le Goater
2020-07-13 17:59 ` Philippe Mathieu-Daudé
2020-07-13 18:02 ` Philippe Mathieu-Daudé
2020-07-14 2:56 ` Havard Skinnemoen
2020-07-14 9:16 ` Markus Armbruster
2020-07-14 11:29 ` Philippe Mathieu-Daudé
2020-07-14 16:21 ` Markus Armbruster
2020-07-14 17:16 ` Philippe Mathieu-Daudé
2020-07-15 9:00 ` Markus Armbruster
2020-07-15 10:57 ` Philippe Mathieu-Daudé
2020-07-15 20:54 ` Havard Skinnemoen
2020-07-16 20:56 ` Havard Skinnemoen
2020-07-17 7:48 ` Philippe Mathieu-Daudé
2020-07-17 8:03 ` Thomas Huth
2020-07-17 8:27 ` Philippe Mathieu-Daudé
2020-07-17 9:00 ` Philippe Mathieu-Daudé
2020-07-17 19:18 ` Havard Skinnemoen
2020-07-17 20:21 ` Cédric Le Goater
2020-07-17 20:52 ` Philippe Mathieu-Daudé
2020-07-17 20:57 ` Havard Skinnemoen
2020-07-20 7:58 ` Markus Armbruster
2020-07-15 7:42 ` Cédric Le Goater
2020-07-15 21:19 ` Havard Skinnemoen
2020-07-09 0:36 ` [PATCH v5 11/11] docs/system: Add Nuvoton machine documentation Havard Skinnemoen
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=CAFQmdRZPMdvXYLQqXMhnv3DT8SHYjvPSBHg-v2YaDb7Zi3xc3Q@mail.gmail.com \
--to=hskinnemoen@google.com \
--cc=Avi.Fishman@nuvoton.com \
--cc=f4bug@amsat.org \
--cc=joel@jms.id.au \
--cc=kfting@nuvoton.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.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).