[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 22/46] ptimer: Add new ptimer_set_period_from_clock() function
From: |
Peter Maydell |
Subject: |
[PULL 22/46] ptimer: Add new ptimer_set_period_from_clock() function |
Date: |
Fri, 29 Jan 2021 10:59:48 +0000 |
The ptimer API currently provides two methods for setting the period:
ptimer_set_period(), which takes a period in nanoseconds, and
ptimer_set_freq(), which takes a frequency in Hz. Neither of these
lines up nicely with the Clock API, because although both the Clock
and the ptimer track the frequency using a representation of whole
and fractional nanoseconds, conversion via either period-in-ns or
frequency-in-Hz will introduce a rounding error.
Add a new function ptimer_set_period_from_clock() which takes the
Clock object directly to avoid the rounding issues. This includes a
facility for the user to specify that there is a frequency divider
between the Clock proper and the timer, as some timer devices like
the CMSDK APB dualtimer need this.
To avoid having to drag in clock.h from ptimer.h we add the Clock
type to typedefs.h.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Luc Michel <luc@lmichel.fr>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-id: 20210128114145.20536-2-peter.maydell@linaro.org
Message-id: 20210121190622.22000-2-peter.maydell@linaro.org
---
include/hw/ptimer.h | 22 ++++++++++++++++++++++
include/qemu/typedefs.h | 1 +
hw/core/ptimer.c | 34 ++++++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+)
diff --git a/include/hw/ptimer.h b/include/hw/ptimer.h
index 412763fffb2..c443218475b 100644
--- a/include/hw/ptimer.h
+++ b/include/hw/ptimer.h
@@ -165,6 +165,28 @@ void ptimer_transaction_commit(ptimer_state *s);
*/
void ptimer_set_period(ptimer_state *s, int64_t period);
+/**
+ * ptimer_set_period_from_clock - Set counter increment from a Clock
+ * @s: ptimer to configure
+ * @clk: pointer to Clock object to take period from
+ * @divisor: value to scale the clock frequency down by
+ *
+ * If the ptimer is being driven from a Clock, this is the preferred
+ * way to tell the ptimer about the period, because it avoids any
+ * possible rounding errors that might happen if the internal
+ * representation of the Clock period was converted to either a period
+ * in ns or a frequency in Hz.
+ *
+ * If the ptimer should run at the same frequency as the clock,
+ * pass 1 as the @divisor; if the ptimer should run at half the
+ * frequency, pass 2, and so on.
+ *
+ * This function will assert if it is called outside a
+ * ptimer_transaction_begin/commit block.
+ */
+void ptimer_set_period_from_clock(ptimer_state *s, const Clock *clock,
+ unsigned int divisor);
+
/**
* ptimer_set_freq - Set counter frequency in Hz
* @s: ptimer to configure
diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
index 976b529dfb5..68deb74ef6f 100644
--- a/include/qemu/typedefs.h
+++ b/include/qemu/typedefs.h
@@ -34,6 +34,7 @@ typedef struct BlockDriverState BlockDriverState;
typedef struct BusClass BusClass;
typedef struct BusState BusState;
typedef struct Chardev Chardev;
+typedef struct Clock Clock;
typedef struct CompatProperty CompatProperty;
typedef struct CoMutex CoMutex;
typedef struct CPUAddressSpace CPUAddressSpace;
diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
index 2aa97cb665c..6ba19fd9658 100644
--- a/hw/core/ptimer.c
+++ b/hw/core/ptimer.c
@@ -15,6 +15,7 @@
#include "sysemu/qtest.h"
#include "block/aio.h"
#include "sysemu/cpus.h"
+#include "hw/clock.h"
#define DELTA_ADJUST 1
#define DELTA_NO_ADJUST -1
@@ -348,6 +349,39 @@ void ptimer_set_period(ptimer_state *s, int64_t period)
}
}
+/* Set counter increment interval from a Clock */
+void ptimer_set_period_from_clock(ptimer_state *s, const Clock *clk,
+ unsigned int divisor)
+{
+ /*
+ * The raw clock period is a 64-bit value in units of 2^-32 ns;
+ * put another way it's a 32.32 fixed-point ns value. Our internal
+ * representation of the period is 64.32 fixed point ns, so
+ * the conversion is simple.
+ */
+ uint64_t raw_period = clock_get(clk);
+ uint64_t period_frac;
+
+ assert(s->in_transaction);
+ s->delta = ptimer_get_count(s);
+ s->period = extract64(raw_period, 32, 32);
+ period_frac = extract64(raw_period, 0, 32);
+ /*
+ * divisor specifies a possible frequency divisor between the
+ * clock and the timer, so it is a multiplier on the period.
+ * We do the multiply after splitting the raw period out into
+ * period and frac to avoid having to do a 32*64->96 multiply.
+ */
+ s->period *= divisor;
+ period_frac *= divisor;
+ s->period += extract64(period_frac, 32, 32);
+ s->period_frac = (uint32_t)period_frac;
+
+ if (s->enabled) {
+ s->need_reload = true;
+ }
+}
+
/* Set counter frequency in Hz. */
void ptimer_set_freq(ptimer_state *s, uint32_t freq)
{
--
2.20.1
- [PULL 12/46] darwin: remove redundant dependency declaration, (continued)
- [PULL 12/46] darwin: remove redundant dependency declaration, Peter Maydell, 2021/01/29
- [PULL 14/46] configure: cross compile should use x86_64 cpu_family, Peter Maydell, 2021/01/29
- [PULL 16/46] darwin: remove 64-bit build detection on 32-bit OS, Peter Maydell, 2021/01/29
- [PULL 19/46] hw/misc/pvpanic: add PCI interface support, Peter Maydell, 2021/01/29
- [PULL 18/46] hw/misc/pvpanic: split-out generic and bus dependent code, Peter Maydell, 2021/01/29
- [PULL 15/46] darwin: detect CoreAudio for build, Peter Maydell, 2021/01/29
- [PULL 17/46] hvf: Add hypervisor entitlement to output binaries, Peter Maydell, 2021/01/29
- [PULL 21/46] tests/qtest: add a test case for pvpanic-pci, Peter Maydell, 2021/01/29
- [PULL 22/46] ptimer: Add new ptimer_set_period_from_clock() function,
Peter Maydell <=
- [PULL 27/46] hw/timer/cmsdk-apb-timer: Rename CMSDKAPBTIMER struct to CMSDKAPBTimer, Peter Maydell, 2021/01/29
- [PULL 29/46] hw/timer/cmsdk-apb-dualtimer: Add Clock input, Peter Maydell, 2021/01/29
- [PULL 20/46] pvpanic : update pvpanic spec document, Peter Maydell, 2021/01/29
- [PULL 24/46] tests: Add a simple test of the CMSDK APB timer, Peter Maydell, 2021/01/29
- [PULL 23/46] clock: Add new clock_has_source() function, Peter Maydell, 2021/01/29
- [PULL 26/46] tests: Add a simple test of the CMSDK APB dual timer, Peter Maydell, 2021/01/29
- [PULL 25/46] tests: Add a simple test of the CMSDK APB watchdog, Peter Maydell, 2021/01/29
- [PULL 32/46] hw/arm/armsse: Wire up clocks, Peter Maydell, 2021/01/29
- [PULL 33/46] hw/arm/mps2: Inline CMSDK_APB_TIMER creation, Peter Maydell, 2021/01/29
- [PULL 34/46] hw/arm/mps2: Create and connect SYSCLK Clock, Peter Maydell, 2021/01/29