qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laurent Vivier <lvivier@redhat.com>
To: pbonzini@redhat.com
Cc: Laurent Vivier <lvivier@redhat.com>, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 2/9] rtl8139: remove muldiv64()
Date: Fri, 25 Sep 2015 16:09:03 +0200	[thread overview]
Message-ID: <1443190150-17312-3-git-send-email-lvivier@redhat.com> (raw)
In-Reply-To: <1443190150-17312-1-git-send-email-lvivier@redhat.com>

Originally, timers were ticks based, and it made sense to
add ticks to current time to know when to trigger an alarm.

But since commit:

7447545 change all other clock references to use nanosecond resolution accessors

All timers use nanoseconds and we need to convert ticks to nanoseconds, by
doing something like:

    y = muldiv64(x, get_ticks_per_sec(), PCI_FREQUENCY)

where x is the number of device ticks and y the number of system ticks.

y is used as nanoseconds in timer functions,
it works because 1 tick is 1 nanosecond.
(get_ticks_per_sec() is 10^9)

But as PCI frequency is 33 MHz, we can also do:

    y = x * 30; /* 33 MHz PCI period is 30 ns */

Which is much more simple.

This implies a 33.333333 MHz PCI frequency,
but this is correct.

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 hw/net/rtl8139.c     | 14 ++++++--------
 tests/rtl8139-test.c |  2 +-
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/hw/net/rtl8139.c b/hw/net/rtl8139.c
index b0d6c40..68e43f3 100644
--- a/hw/net/rtl8139.c
+++ b/hw/net/rtl8139.c
@@ -64,7 +64,7 @@
 /* debug RTL8139 card */
 //#define DEBUG_RTL8139 1
 
-#define PCI_FREQUENCY 33000000L
+#define PCI_PERIOD 30    /* 30 ns period = 33.333333 Mhz frequency */
 
 #define SET_MASKED(input, mask, curr) \
     ( ( (input) & ~(mask) ) | ( (curr) & (mask) ) )
@@ -2834,8 +2834,7 @@ static void rtl8139_io_writew(void *opaque, uint8_t addr, uint32_t val)
 
 static void rtl8139_set_next_tctr_time(RTL8139State *s)
 {
-    const uint64_t ns_per_period =
-        muldiv64(0x100000000LL, get_ticks_per_sec(), PCI_FREQUENCY);
+    const uint64_t ns_per_period = (uint64_t)PCI_PERIOD << 32;
 
     DPRINTF("entered rtl8139_set_next_tctr_time\n");
 
@@ -2853,7 +2852,7 @@ static void rtl8139_set_next_tctr_time(RTL8139State *s)
     if (!s->TimerInt) {
         timer_del(s->timer);
     } else {
-        uint64_t delta = muldiv64(s->TimerInt, get_ticks_per_sec(), PCI_FREQUENCY);
+        uint64_t delta = (uint64_t)s->TimerInt * PCI_PERIOD;
         if (s->TCTR_base + delta <= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) {
             delta += ns_per_period;
         }
@@ -3127,8 +3126,8 @@ static uint32_t rtl8139_io_readl(void *opaque, uint8_t addr)
             break;
 
         case Timer:
-            ret = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - s->TCTR_base,
-                           PCI_FREQUENCY, get_ticks_per_sec());
+            ret = (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - s->TCTR_base) /
+                  PCI_PERIOD;
             DPRINTF("TCTR Timer read val=0x%08x\n", ret);
             break;
 
@@ -3222,8 +3221,7 @@ static void rtl8139_pre_save(void *opaque)
     int64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 
     /* for migration to older versions */
-    s->TCTR = muldiv64(current_time - s->TCTR_base, PCI_FREQUENCY,
-                       get_ticks_per_sec());
+    s->TCTR = (current_time - s->TCTR_base) / PCI_PERIOD;
     s->rtl8139_mmio_io_addr_dummy = 0;
 }
 
diff --git a/tests/rtl8139-test.c b/tests/rtl8139-test.c
index e749be3..ba62851 100644
--- a/tests/rtl8139-test.c
+++ b/tests/rtl8139-test.c
@@ -20,7 +20,7 @@ static void nop(void)
 {
 }
 
-#define CLK 33000000
+#define CLK 33333333
 
 static QPCIBus *pcibus;
 static QPCIDevice *dev;
-- 
2.1.0

  parent reply	other threads:[~2015-09-25 14:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-25 14:09 [Qemu-devel] [PULL 0/9] muldiv64 queue Laurent Vivier
2015-09-25 14:09 ` [Qemu-devel] [PULL 1/9] i6300esb: remove muldiv64() Laurent Vivier
2015-09-25 14:09 ` Laurent Vivier [this message]
2015-09-25 14:09 ` [Qemu-devel] [PULL 3/9] pcnet: " Laurent Vivier
2015-09-25 14:09 ` [Qemu-devel] [PULL 4/9] mips: " Laurent Vivier
2015-09-25 14:09 ` [Qemu-devel] [PULL 5/9] openrisc: " Laurent Vivier
2015-09-25 14:09 ` [Qemu-devel] [PULL 6/9] arm: clarify the use of muldiv64() Laurent Vivier
2015-09-25 14:09 ` [Qemu-devel] [PULL 7/9] hpet: remove muldiv64() Laurent Vivier
2015-09-25 14:09 ` [Qemu-devel] [PULL 8/9] bt: " Laurent Vivier
2015-09-25 14:09 ` [Qemu-devel] [PULL 9/9] net: " Laurent Vivier
2015-09-25 18:01 ` [Qemu-devel] [PULL 0/9] muldiv64 queue Peter Maydell

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=1443190150-17312-3-git-send-email-lvivier@redhat.com \
    --to=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --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).