From nobody Tue Apr 15 15:44:10 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1535103954226917.5758610603873; Fri, 24 Aug 2018 02:45:54 -0700 (PDT) Received: from localhost ([::1]:40717 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ft8f8-00009q-6Q for importer@patchew.org; Fri, 24 Aug 2018 05:45:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35506) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ft8Tr-0007GF-My for qemu-devel@nongnu.org; Fri, 24 Aug 2018 05:34:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ft8Tq-0003sV-DN for qemu-devel@nongnu.org; Fri, 24 Aug 2018 05:34:11 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:44878) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ft8Tq-0003jk-3M for qemu-devel@nongnu.org; Fri, 24 Aug 2018 05:34:10 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1ft8Tp-0006UG-2M for qemu-devel@nongnu.org; Fri, 24 Aug 2018 10:34:09 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Fri, 24 Aug 2018 10:33:11 +0100 Message-Id: <20180824093343.11346-21-peter.maydell@linaro.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180824093343.11346-1-peter.maydell@linaro.org> References: <20180824093343.11346-1-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PULL 20/52] hw/misc/mps2-fpgaio: Implement PSCNTR and COUNTER X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In the MPS2 FPGAIO, PSCNTR is a free-running downcounter with a reload value configured via the PRESCALE register, and COUNTER counts up by 1 every time PSCNTR reaches zero. Implement these counters. We can just increment the counters migration subsection's version ID because we only added it in the previous commit, so no released QEMU versions will be using it. Signed-off-by: Peter Maydell Reviewed-by: Alistair Francis Reviewed-by: Richard Henderson Message-id: 20180820141116.9118-3-peter.maydell@linaro.org --- include/hw/misc/mps2-fpgaio.h | 6 +++ hw/misc/mps2-fpgaio.c | 97 +++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 4 deletions(-) diff --git a/include/hw/misc/mps2-fpgaio.h b/include/hw/misc/mps2-fpgaio.h index ec057d38c76..69e265cd4b2 100644 --- a/include/hw/misc/mps2-fpgaio.h +++ b/include/hw/misc/mps2-fpgaio.h @@ -37,6 +37,12 @@ typedef struct { uint32_t prescale; uint32_t misc; =20 + /* QEMU_CLOCK_VIRTUAL time at which counter and pscntr were last synce= d */ + int64_t pscntr_sync_ticks; + /* Values of COUNTER and PSCNTR at time pscntr_sync_ticks */ + uint32_t counter; + uint32_t pscntr; + uint32_t prescale_clk; =20 /* These hold the CLOCK_VIRTUAL ns tick when the CLK1HZ/CLK100HZ was z= ero */ diff --git a/hw/misc/mps2-fpgaio.c b/hw/misc/mps2-fpgaio.c index bbc28f641f0..5cf10ebd66a 100644 --- a/hw/misc/mps2-fpgaio.c +++ b/hw/misc/mps2-fpgaio.c @@ -43,6 +43,77 @@ static int64_t tickoff_from_counter(int64_t now, uint32_= t count, int frq) return now - muldiv64(count, NANOSECONDS_PER_SECOND, frq); } =20 +static void resync_counter(MPS2FPGAIO *s) +{ + /* + * Update s->counter and s->pscntr to their true current values + * by calculating how many times PSCNTR has ticked since the + * last time we did a resync. + */ + int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + int64_t elapsed =3D now - s->pscntr_sync_ticks; + + /* + * Round elapsed down to a whole number of PSCNTR ticks, so we don't + * lose time if we do multiple resyncs in a single tick. + */ + uint64_t ticks =3D muldiv64(elapsed, s->prescale_clk, NANOSECONDS_PER_= SECOND); + + /* + * Work out what PSCNTR and COUNTER have moved to. We assume that + * PSCNTR reloads from PRESCALE one tick-period after it hits zero, + * and that COUNTER increments at the same moment. + */ + if (ticks =3D=3D 0) { + /* We haven't ticked since the last time we were asked */ + return; + } else if (ticks < s->pscntr) { + /* We haven't yet reached zero, just reduce the PSCNTR */ + s->pscntr -=3D ticks; + } else { + if (s->prescale =3D=3D 0) { + /* + * If the reload value is zero then the PSCNTR will stick + * at zero once it reaches it, and so we will increment + * COUNTER every tick after that. + */ + s->counter +=3D ticks - s->pscntr; + s->pscntr =3D 0; + } else { + /* + * This is the complicated bit. This ASCII art diagram gives an + * example with PRESCALE=3D=3D5 PSCNTR=3D=3D7: + * + * ticks 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 + * PSCNTR 7 6 5 4 3 2 1 0 5 4 3 2 1 0 5 + * cinc 1 2 + * y 0 1 2 3 4 5 6 7 8 9 10 11 12 + * x 0 1 2 3 4 5 0 1 2 3 4 5 0 + * + * where x =3D y % (s->prescale + 1) + * and so PSCNTR =3D s->prescale - x + * and COUNTER is incremented by y / (s->prescale + 1) + * + * The case where PSCNTR < PRESCALE works out the same, + * though we must be careful to calculate y as 64-bit unsigned + * for all parts of the expression. + * y < 0 is not possible because that implies ticks < s->pscnt= r. + */ + uint64_t y =3D ticks - s->pscntr + s->prescale; + s->pscntr =3D s->prescale - (y % (s->prescale + 1)); + s->counter +=3D y / (s->prescale + 1); + } + } + + /* + * Only advance the sync time to the timestamp of the last PSCNTR tick, + * not all the way to 'now', so we don't lose time if we do multiple + * resyncs in a single tick. + */ + s->pscntr_sync_ticks +=3D muldiv64(ticks, NANOSECONDS_PER_SECOND, + s->prescale_clk); +} + static uint64_t mps2_fpgaio_read(void *opaque, hwaddr offset, unsigned siz= e) { MPS2FPGAIO *s =3D MPS2_FPGAIO(opaque); @@ -74,9 +145,12 @@ static uint64_t mps2_fpgaio_read(void *opaque, hwaddr o= ffset, unsigned size) r =3D counter_from_tickoff(now, s->clk100hz_tick_offset, 100); break; case A_COUNTER: + resync_counter(s); + r =3D s->counter; + break; case A_PSCNTR: - qemu_log_mask(LOG_UNIMP, "MPS2 FPGAIO: counters unimplemented\n"); - r =3D 0; + resync_counter(s); + r =3D s->pscntr; break; default: qemu_log_mask(LOG_GUEST_ERROR, @@ -107,6 +181,7 @@ static void mps2_fpgaio_write(void *opaque, hwaddr offs= et, uint64_t value, s->led0 =3D value & 0x3; break; case A_PRESCALE: + resync_counter(s); s->prescale =3D value; break; case A_MISC: @@ -126,6 +201,14 @@ static void mps2_fpgaio_write(void *opaque, hwaddr off= set, uint64_t value, now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); s->clk100hz_tick_offset =3D tickoff_from_counter(now, value, 100); break; + case A_COUNTER: + resync_counter(s); + s->counter =3D value; + break; + case A_PSCNTR: + resync_counter(s); + s->pscntr =3D value; + break; default: qemu_log_mask(LOG_GUEST_ERROR, "MPS2 FPGAIO write: bad offset 0x%x\n", (int) offset= ); @@ -150,6 +233,9 @@ static void mps2_fpgaio_reset(DeviceState *dev) s->misc =3D 0; s->clk1hz_tick_offset =3D tickoff_from_counter(now, 0, 1); s->clk100hz_tick_offset =3D tickoff_from_counter(now, 0, 100); + s->counter =3D 0; + s->pscntr =3D 0; + s->pscntr_sync_ticks =3D now; } =20 static void mps2_fpgaio_init(Object *obj) @@ -170,12 +256,15 @@ static bool mps2_fpgaio_counters_needed(void *opaque) =20 static const VMStateDescription mps2_fpgaio_counters_vmstate =3D { .name =3D "mps2-fpgaio/counters", - .version_id =3D 1, - .minimum_version_id =3D 1, + .version_id =3D 2, + .minimum_version_id =3D 2, .needed =3D mps2_fpgaio_counters_needed, .fields =3D (VMStateField[]) { VMSTATE_INT64(clk1hz_tick_offset, MPS2FPGAIO), VMSTATE_INT64(clk100hz_tick_offset, MPS2FPGAIO), + VMSTATE_UINT32(counter, MPS2FPGAIO), + VMSTATE_UINT32(pscntr, MPS2FPGAIO), + VMSTATE_INT64(pscntr_sync_ticks, MPS2FPGAIO), VMSTATE_END_OF_LIST() } }; --=20 2.18.0