* [Qemu-devel] [PATCH v13 1/8] hw/ptimer: Fix issues caused by the adjusted timer limit value
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
@ 2016-05-27 17:03 ` Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 2/8] hw/ptimer: Perform counter wrap around if timer already expired Dmitry Osipenko
` (7 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-27 17:03 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
Multiple issues here related to the timer with a adjusted .limit value:
1) ptimer_get_count() returns incorrect counter value for the disabled
timer after loading the counter with a small value, because adjusted limit
value is used instead of the original.
For instance:
1) ptimer_stop(t)
2) ptimer_set_period(t, 1)
3) ptimer_set_limit(t, 0, 1)
4) ptimer_get_count(t) <-- would return 10000 instead of 0
2) ptimer_get_count() might return incorrect value for the timer running
with a adjusted limit value.
For instance:
1) ptimer_stop(t)
2) ptimer_set_period(t, 1)
3) ptimer_set_limit(t, 10, 1)
4) ptimer_run(t)
5) ptimer_get_count(t) <-- might return value > 10
3) Neither ptimer_set_period() nor ptimer_set_freq() are adjusting the
limit value, so it is still possible to make timer timeout value
arbitrary small.
For instance:
1) ptimer_set_period(t, 10000)
2) ptimer_set_limit(t, 1, 0)
3) ptimer_set_period(t, 1) <-- bypass limit correction
Fix all of the above issues by adjusting timer period instead of the limit.
Perform the adjustment for periodic timer only. Use the delta value instead
of the limit to make decision whether adjustment is required, as limit could
be altered while timer is running, resulting in incorrect value returned by
ptimer_get_count.
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
---
hw/core/ptimer.c | 51 +++++++++++++++++++++++++++++++--------------------
1 file changed, 31 insertions(+), 20 deletions(-)
diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
index 153c835..16d7dd5 100644
--- a/hw/core/ptimer.c
+++ b/hw/core/ptimer.c
@@ -35,6 +35,9 @@ static void ptimer_trigger(ptimer_state *s)
static void ptimer_reload(ptimer_state *s)
{
+ uint32_t period_frac = s->period_frac;
+ uint64_t period = s->period;
+
if (s->delta == 0) {
ptimer_trigger(s);
s->delta = s->limit;
@@ -45,10 +48,24 @@ static void ptimer_reload(ptimer_state *s)
return;
}
+ /*
+ * Artificially limit timeout rate to something
+ * achievable under QEMU. Otherwise, QEMU spends all
+ * its time generating timer interrupts, and there
+ * is no forward progress.
+ * About ten microseconds is the fastest that really works
+ * on the current generation of host machines.
+ */
+
+ if (s->enabled == 1 && (s->delta * period < 10000) && !use_icount) {
+ period = 10000 / s->delta;
+ period_frac = 0;
+ }
+
s->last_event = s->next_event;
- s->next_event = s->last_event + s->delta * s->period;
- if (s->period_frac) {
- s->next_event += ((int64_t)s->period_frac * s->delta) >> 32;
+ s->next_event = s->last_event + s->delta * period;
+ if (period_frac) {
+ s->next_event += ((int64_t)period_frac * s->delta) >> 32;
}
timer_mod(s->timer, s->next_event);
}
@@ -83,6 +100,13 @@ uint64_t ptimer_get_count(ptimer_state *s)
uint64_t div;
int clz1, clz2;
int shift;
+ uint32_t period_frac = s->period_frac;
+ uint64_t period = s->period;
+
+ if ((s->enabled == 1) && !use_icount && (s->delta * period < 10000)) {
+ period = 10000 / s->delta;
+ period_frac = 0;
+ }
/* We need to divide time by period, where time is stored in
rem (64-bit integer) and period is stored in period/period_frac
@@ -95,7 +119,7 @@ uint64_t ptimer_get_count(ptimer_state *s)
*/
rem = s->next_event - now;
- div = s->period;
+ div = period;
clz1 = clz64(rem);
clz2 = clz64(div);
@@ -104,13 +128,13 @@ uint64_t ptimer_get_count(ptimer_state *s)
rem <<= shift;
div <<= shift;
if (shift >= 32) {
- div |= ((uint64_t)s->period_frac << (shift - 32));
+ div |= ((uint64_t)period_frac << (shift - 32));
} else {
if (shift != 0)
- div |= (s->period_frac >> (32 - shift));
+ div |= (period_frac >> (32 - shift));
/* Look at remaining bits of period_frac and round div up if
necessary. */
- if ((uint32_t)(s->period_frac << shift))
+ if ((uint32_t)(period_frac << shift))
div += 1;
}
counter = rem / div;
@@ -182,19 +206,6 @@ void ptimer_set_freq(ptimer_state *s, uint32_t freq)
count = limit. */
void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload)
{
- /*
- * Artificially limit timeout rate to something
- * achievable under QEMU. Otherwise, QEMU spends all
- * its time generating timer interrupts, and there
- * is no forward progress.
- * About ten microseconds is the fastest that really works
- * on the current generation of host machines.
- */
-
- if (!use_icount && limit * s->period < 10000 && s->period) {
- limit = 10000 / s->period;
- }
-
s->limit = limit;
if (reload)
s->delta = limit;
--
2.8.3
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v13 2/8] hw/ptimer: Perform counter wrap around if timer already expired
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 1/8] hw/ptimer: Fix issues caused by the adjusted timer limit value Dmitry Osipenko
@ 2016-05-27 17:03 ` Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 3/8] hw/ptimer: Update .delta on period/freq change Dmitry Osipenko
` (6 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-27 17:03 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
ptimer_get_count() might be called while QEMU timer already been expired.
In that case ptimer would return counter = 0, which might be undesirable
in case of polled timer. Do counter wrap around for periodic timer to keep
it distributed. In order to achieve more accurate emulation behaviour of
certain hardware, don't perform wrap around when in icount mode and return
counter = 0 in that case (that doesn't affect polled counter distribution).
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
---
hw/core/ptimer.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
index 16d7dd5..7e6fc2d 100644
--- a/hw/core/ptimer.c
+++ b/hw/core/ptimer.c
@@ -84,14 +84,16 @@ static void ptimer_tick(void *opaque)
uint64_t ptimer_get_count(ptimer_state *s)
{
- int64_t now;
uint64_t counter;
if (s->enabled) {
- now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+ int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+ int64_t next = s->next_event;
+ bool expired = (now - next >= 0);
+ bool oneshot = (s->enabled == 2);
+
/* Figure out the current counter value. */
- if (now - s->next_event > 0
- || s->period == 0) {
+ if (s->period == 0 || (expired && (oneshot || use_icount))) {
/* Prevent timer underflowing if it should already have
triggered. */
counter = 0;
@@ -103,7 +105,7 @@ uint64_t ptimer_get_count(ptimer_state *s)
uint32_t period_frac = s->period_frac;
uint64_t period = s->period;
- if ((s->enabled == 1) && !use_icount && (s->delta * period < 10000)) {
+ if (!oneshot && (s->delta * period < 10000) && !use_icount) {
period = 10000 / s->delta;
period_frac = 0;
}
@@ -118,7 +120,7 @@ uint64_t ptimer_get_count(ptimer_state *s)
backwards.
*/
- rem = s->next_event - now;
+ rem = expired ? now - next : next - now;
div = period;
clz1 = clz64(rem);
@@ -138,6 +140,11 @@ uint64_t ptimer_get_count(ptimer_state *s)
div += 1;
}
counter = rem / div;
+
+ if (expired && counter != 0) {
+ /* Wrap around periodic counter. */
+ counter = s->limit - (counter - 1) % s->limit;
+ }
}
} else {
counter = s->delta;
--
2.8.3
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v13 3/8] hw/ptimer: Update .delta on period/freq change
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 1/8] hw/ptimer: Fix issues caused by the adjusted timer limit value Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 2/8] hw/ptimer: Perform counter wrap around if timer already expired Dmitry Osipenko
@ 2016-05-27 17:03 ` Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 4/8] hw/ptimer: Support "on the fly" timer mode switch Dmitry Osipenko
` (5 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-27 17:03 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
Delta value must be updated on period/freq change, otherwise running timer
would be restarted (counter reloaded with old delta). Only m68k/mcf520x
and arm/arm_timer devices are currently doing freq change correctly, i.e.
stopping the timer. Perform delta update to fix affected devices and
eliminate potential further mistakes.
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
---
hw/core/ptimer.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
index 7e6fc2d..76ebe9b 100644
--- a/hw/core/ptimer.c
+++ b/hw/core/ptimer.c
@@ -190,6 +190,7 @@ void ptimer_stop(ptimer_state *s)
/* Set counter increment interval in nanoseconds. */
void ptimer_set_period(ptimer_state *s, int64_t period)
{
+ s->delta = ptimer_get_count(s);
s->period = period;
s->period_frac = 0;
if (s->enabled) {
@@ -201,6 +202,7 @@ void ptimer_set_period(ptimer_state *s, int64_t period)
/* Set counter frequency in Hz. */
void ptimer_set_freq(ptimer_state *s, uint32_t freq)
{
+ s->delta = ptimer_get_count(s);
s->period = 1000000000ll / freq;
s->period_frac = (1000000000ll << 32) / freq;
if (s->enabled) {
--
2.8.3
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v13 4/8] hw/ptimer: Support "on the fly" timer mode switch
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
` (2 preceding siblings ...)
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 3/8] hw/ptimer: Update .delta on period/freq change Dmitry Osipenko
@ 2016-05-27 17:03 ` Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 5/8] hw/ptimer: Introduce ptimer_get_limit Dmitry Osipenko
` (4 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-27 17:03 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
Allow switching between periodic <-> oneshot modes while timer is running.
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
---
hw/core/ptimer.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
index 76ebe9b..d0b2f38 100644
--- a/hw/core/ptimer.c
+++ b/hw/core/ptimer.c
@@ -163,16 +163,17 @@ void ptimer_set_count(ptimer_state *s, uint64_t count)
void ptimer_run(ptimer_state *s, int oneshot)
{
- if (s->enabled) {
- return;
- }
- if (s->period == 0) {
+ bool was_disabled = !s->enabled;
+
+ if (was_disabled && s->period == 0) {
fprintf(stderr, "Timer with period zero, disabling\n");
return;
}
s->enabled = oneshot ? 2 : 1;
- s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
- ptimer_reload(s);
+ if (was_disabled) {
+ s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+ ptimer_reload(s);
+ }
}
/* Pause a timer. Note that this may cause it to "lose" time, even if it
--
2.8.3
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v13 5/8] hw/ptimer: Introduce ptimer_get_limit
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
` (3 preceding siblings ...)
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 4/8] hw/ptimer: Support "on the fly" timer mode switch Dmitry Osipenko
@ 2016-05-27 17:03 ` Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 6/8] hw/ptimer: Support running with counter = 0 by introducing new policy feature Dmitry Osipenko
` (3 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-27 17:03 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
Currently ptimer users are used to store copy of the limit value, because
ptimer doesn't provide facility to retrieve the limit. Let's provide it.
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
Reviewed-by: Peter Crosthwaite <crosthwaite.peter@gmail.com>
---
hw/core/ptimer.c | 5 +++++
include/hw/ptimer.h | 1 +
2 files changed, 6 insertions(+)
diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
index d0b2f38..05b0c27 100644
--- a/hw/core/ptimer.c
+++ b/hw/core/ptimer.c
@@ -225,6 +225,11 @@ void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload)
}
}
+uint64_t ptimer_get_limit(ptimer_state *s)
+{
+ return s->limit;
+}
+
const VMStateDescription vmstate_ptimer = {
.name = "ptimer",
.version_id = 1,
diff --git a/include/hw/ptimer.h b/include/hw/ptimer.h
index 8ebacbb..e397db5 100644
--- a/include/hw/ptimer.h
+++ b/include/hw/ptimer.h
@@ -19,6 +19,7 @@ typedef void (*ptimer_cb)(void *opaque);
ptimer_state *ptimer_init(QEMUBH *bh);
void ptimer_set_period(ptimer_state *s, int64_t period);
void ptimer_set_freq(ptimer_state *s, uint32_t freq);
+uint64_t ptimer_get_limit(ptimer_state *s);
void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
uint64_t ptimer_get_count(ptimer_state *s);
void ptimer_set_count(ptimer_state *s, uint64_t count);
--
2.8.3
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v13 6/8] hw/ptimer: Support running with counter = 0 by introducing new policy feature
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
` (4 preceding siblings ...)
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 5/8] hw/ptimer: Introduce ptimer_get_limit Dmitry Osipenko
@ 2016-05-27 17:03 ` Dmitry Osipenko
2016-05-28 14:12 ` Dmitry Osipenko
2016-06-06 10:09 ` Peter Maydell
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 7/8] hw/ptimer: Fix counter - 1 returned by ptimer_get_count for the active timer Dmitry Osipenko
` (2 subsequent siblings)
8 siblings, 2 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-27 17:03 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
Currently ptimer prints error message and clears enable flag for an arming
timer that has delta = load = 0. That actually is a valid case for most
of the timers, like instant IRQ trigger for oneshot timer or continuous in
periodic mode. There are different possible policies of how particular timer
could interpret setting counter to 0, like:
1) Immediate stop with triggering the interrupt in oneshot mode. Immediate
wraparound with triggering the interrupt in continuous mode.
2) Immediate stop without triggering the interrupt in oneshot mode. Immediate
wraparound without triggering the interrupt in continuous mode.
3) Stopping with triggering the interrupt after one period in oneshot mode.
Wraparound with triggering the interrupt in continuous mode after one
period.
4) Stopping without triggering the interrupt after one period in oneshot mode.
Wraparound without triggering the interrupt in continuous mode after one
period.
As well as handling oneshot/continuous modes differently.
Given that, currently, running the timer with counter/period equal to 0 is
treated as a error case, it's not obvious what policies should be supported
by ptimer. Let's implement the third policy for now, as it is known to be
used by the ARM MPTimer.
Explicitly forbid running with counter/period == 0 in all cases by aborting
QEMU execution instead of printing the error message.
Bump VMSD version since a new VMState member is added.
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
---
hw/core/ptimer.c | 53 +++++++++++++++++++++++++++++++----------------------
include/hw/ptimer.h | 6 ++++++
2 files changed, 37 insertions(+), 22 deletions(-)
diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
index 05b0c27..9bc70f5 100644
--- a/hw/core/ptimer.c
+++ b/hw/core/ptimer.c
@@ -21,6 +21,7 @@ struct ptimer_state
int64_t period;
int64_t last_event;
int64_t next_event;
+ uint8_t policy;
QEMUBH *bh;
QEMUTimer *timer;
};
@@ -37,15 +38,16 @@ static void ptimer_reload(ptimer_state *s)
{
uint32_t period_frac = s->period_frac;
uint64_t period = s->period;
+ uint64_t delta = MAX(1, s->delta);
- if (s->delta == 0) {
- ptimer_trigger(s);
- s->delta = s->limit;
+ if (s->delta == 0 && s->policy == UNIMPLEMENTED) {
+ hw_error("ptimer: Running with counter=0 is unimplemented by " \
+ "this timer, fix it!\n");
}
- if (s->delta == 0 || s->period == 0) {
- fprintf(stderr, "Timer with period zero, disabling\n");
- s->enabled = 0;
- return;
+
+ if (period == 0) {
+ hw_error("ptimer: Timer tries to run with period=0, behaviour is " \
+ "undefined, fix it!\n");
}
/*
@@ -57,15 +59,15 @@ static void ptimer_reload(ptimer_state *s)
* on the current generation of host machines.
*/
- if (s->enabled == 1 && (s->delta * period < 10000) && !use_icount) {
- period = 10000 / s->delta;
+ if (s->enabled == 1 && (delta * period < 10000) && !use_icount) {
+ period = 10000 / delta;
period_frac = 0;
}
s->last_event = s->next_event;
- s->next_event = s->last_event + s->delta * period;
+ s->next_event = s->last_event + delta * period;
if (period_frac) {
- s->next_event += ((int64_t)period_frac * s->delta) >> 32;
+ s->next_event += ((int64_t)period_frac * delta) >> 32;
}
timer_mod(s->timer, s->next_event);
}
@@ -73,27 +75,30 @@ static void ptimer_reload(ptimer_state *s)
static void ptimer_tick(void *opaque)
{
ptimer_state *s = (ptimer_state *)opaque;
- ptimer_trigger(s);
- s->delta = 0;
- if (s->enabled == 2) {
+
+ s->delta = (s->enabled == 1) ? s->limit : 0;
+
+ if (s->delta == 0) {
s->enabled = 0;
} else {
ptimer_reload(s);
}
+
+ ptimer_trigger(s);
}
uint64_t ptimer_get_count(ptimer_state *s)
{
uint64_t counter;
- if (s->enabled) {
+ if (s->enabled && s->delta != 0) {
int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
int64_t next = s->next_event;
bool expired = (now - next >= 0);
bool oneshot = (s->enabled == 2);
/* Figure out the current counter value. */
- if (s->period == 0 || (expired && (oneshot || use_icount))) {
+ if (expired && (oneshot || use_icount)) {
/* Prevent timer underflowing if it should already have
triggered. */
counter = 0;
@@ -165,11 +170,8 @@ void ptimer_run(ptimer_state *s, int oneshot)
{
bool was_disabled = !s->enabled;
- if (was_disabled && s->period == 0) {
- fprintf(stderr, "Timer with period zero, disabling\n");
- return;
- }
s->enabled = oneshot ? 2 : 1;
+
if (was_disabled) {
s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
ptimer_reload(s);
@@ -203,6 +205,7 @@ void ptimer_set_period(ptimer_state *s, int64_t period)
/* Set counter frequency in Hz. */
void ptimer_set_freq(ptimer_state *s, uint32_t freq)
{
+ g_assert(freq != 0 && freq <= 1000000000);
s->delta = ptimer_get_count(s);
s->period = 1000000000ll / freq;
s->period_frac = (1000000000ll << 32) / freq;
@@ -230,10 +233,15 @@ uint64_t ptimer_get_limit(ptimer_state *s)
return s->limit;
}
+void ptimer_set_policy(ptimer_state *s, enum ptimer_policy policy)
+{
+ s->policy = policy;
+}
+
const VMStateDescription vmstate_ptimer = {
.name = "ptimer",
- .version_id = 1,
- .minimum_version_id = 1,
+ .version_id = 2,
+ .minimum_version_id = 2,
.fields = (VMStateField[]) {
VMSTATE_UINT8(enabled, ptimer_state),
VMSTATE_UINT64(limit, ptimer_state),
@@ -243,6 +251,7 @@ const VMStateDescription vmstate_ptimer = {
VMSTATE_INT64(last_event, ptimer_state),
VMSTATE_INT64(next_event, ptimer_state),
VMSTATE_TIMER_PTR(timer, ptimer_state),
+ VMSTATE_UINT8(policy, ptimer_state),
VMSTATE_END_OF_LIST()
}
};
diff --git a/include/hw/ptimer.h b/include/hw/ptimer.h
index e397db5..221f591 100644
--- a/include/hw/ptimer.h
+++ b/include/hw/ptimer.h
@@ -12,6 +12,11 @@
#include "qemu/timer.h"
#include "migration/vmstate.h"
+enum ptimer_policy {
+ UNIMPLEMENTED,
+ SET_CNT_TO_0_TRIGGERS_IRQ_AFTER_ONE_PERIOD,
+};
+
/* ptimer.c */
typedef struct ptimer_state ptimer_state;
typedef void (*ptimer_cb)(void *opaque);
@@ -25,6 +30,7 @@ uint64_t ptimer_get_count(ptimer_state *s);
void ptimer_set_count(ptimer_state *s, uint64_t count);
void ptimer_run(ptimer_state *s, int oneshot);
void ptimer_stop(ptimer_state *s);
+void ptimer_set_policy(ptimer_state *s, enum ptimer_policy policy);
extern const VMStateDescription vmstate_ptimer;
--
2.8.3
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v13 6/8] hw/ptimer: Support running with counter = 0 by introducing new policy feature
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 6/8] hw/ptimer: Support running with counter = 0 by introducing new policy feature Dmitry Osipenko
@ 2016-05-28 14:12 ` Dmitry Osipenko
2016-06-06 10:09 ` Peter Maydell
1 sibling, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-28 14:12 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
Just a small note:
It's more like RFC. I don't think 8 bits would be enough for all possible
policies and I don't know whether it is possible to maintain backward
compatibility by keeping VMSD .minimum_version_id. "loadvm" refused to accept
old ptimer VMState, I haven't looked at it much.
--
Dmitry
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v13 6/8] hw/ptimer: Support running with counter = 0 by introducing new policy feature
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 6/8] hw/ptimer: Support running with counter = 0 by introducing new policy feature Dmitry Osipenko
2016-05-28 14:12 ` Dmitry Osipenko
@ 2016-06-06 10:09 ` Peter Maydell
2016-06-06 17:24 ` Dmitry Osipenko
1 sibling, 1 reply; 13+ messages in thread
From: Peter Maydell @ 2016-06-06 10:09 UTC (permalink / raw)
To: Dmitry Osipenko; +Cc: QEMU Developers, qemu-arm, Peter Crosthwaite
On 27 May 2016 at 18:03, Dmitry Osipenko <digetx@gmail.com> wrote:
> Currently ptimer prints error message and clears enable flag for an arming
> timer that has delta = load = 0. That actually is a valid case for most
> of the timers, like instant IRQ trigger for oneshot timer or continuous in
> periodic mode. There are different possible policies of how particular timer
> could interpret setting counter to 0, like:
>
> 1) Immediate stop with triggering the interrupt in oneshot mode. Immediate
> wraparound with triggering the interrupt in continuous mode.
>
> 2) Immediate stop without triggering the interrupt in oneshot mode. Immediate
> wraparound without triggering the interrupt in continuous mode.
>
> 3) Stopping with triggering the interrupt after one period in oneshot mode.
> Wraparound with triggering the interrupt in continuous mode after one
> period.
>
> 4) Stopping without triggering the interrupt after one period in oneshot mode.
> Wraparound without triggering the interrupt in continuous mode after one
> period.
>
> As well as handling oneshot/continuous modes differently.
>
> Given that, currently, running the timer with counter/period equal to 0 is
> treated as a error case, it's not obvious what policies should be supported
> by ptimer. Let's implement the third policy for now, as it is known to be
> used by the ARM MPTimer.
>
> Explicitly forbid running with counter/period == 0 in all cases by aborting
> QEMU execution instead of printing the error message.
>
> Bump VMSD version since a new VMState member is added.
I'm afraid you can't do that -- the ptimer code is used by a lot of
platforms including some which do not permit migration compatibility
breaks (at least sparc, for instance).
If you want to add this feature you need to at least make it seamlessly
support migration from the old version using a vmstate subsection.
Better would be to make the policy a QOM property -- then it is
constant for the lifetime of the timer and doesn't need to be migrated
at all. (There's no reason to support timers which change policy at
runtime, I hope). The default property value should be "same behaviour
as previously".
This whole change would be more solidly supported if it came with
a clear description of the bug being fixed (ie what device is currently
using the default-but-wrong behaviour and is going to be fixed by
setting a different policy).
> Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
> ---
> hw/core/ptimer.c | 53 +++++++++++++++++++++++++++++++----------------------
> include/hw/ptimer.h | 6 ++++++
> 2 files changed, 37 insertions(+), 22 deletions(-)
>
> diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
> index 05b0c27..9bc70f5 100644
> --- a/hw/core/ptimer.c
> +++ b/hw/core/ptimer.c
> @@ -21,6 +21,7 @@ struct ptimer_state
> int64_t period;
> int64_t last_event;
> int64_t next_event;
> + uint8_t policy;
> QEMUBH *bh;
> QEMUTimer *timer;
> };
> @@ -37,15 +38,16 @@ static void ptimer_reload(ptimer_state *s)
> {
> uint32_t period_frac = s->period_frac;
> uint64_t period = s->period;
> + uint64_t delta = MAX(1, s->delta);
>
> - if (s->delta == 0) {
> - ptimer_trigger(s);
> - s->delta = s->limit;
> + if (s->delta == 0 && s->policy == UNIMPLEMENTED) {
> + hw_error("ptimer: Running with counter=0 is unimplemented by " \
> + "this timer, fix it!\n");
hw_error() is effectively a verbose assert. Have you checked that
all the users of ptimer do the right thing? If not, we can't
assert() until we've fixed them all. Similarly below.
Note that what we had previously was just a warning-and-continue
> }
> - if (s->delta == 0 || s->period == 0) {
> - fprintf(stderr, "Timer with period zero, disabling\n");
> - s->enabled = 0;
> - return;
> +
> + if (period == 0) {
> + hw_error("ptimer: Timer tries to run with period=0, behaviour is " \
> + "undefined, fix it!\n");
> }
>
> /*
> @@ -57,15 +59,15 @@ static void ptimer_reload(ptimer_state *s)
> * on the current generation of host machines.
> */
>
> - if (s->enabled == 1 && (s->delta * period < 10000) && !use_icount) {
> - period = 10000 / s->delta;
> + if (s->enabled == 1 && (delta * period < 10000) && !use_icount) {
> + period = 10000 / delta;
> period_frac = 0;
> }
>
> s->last_event = s->next_event;
> - s->next_event = s->last_event + s->delta * period;
> + s->next_event = s->last_event + delta * period;
> if (period_frac) {
> - s->next_event += ((int64_t)period_frac * s->delta) >> 32;
> + s->next_event += ((int64_t)period_frac * delta) >> 32;
> }
> timer_mod(s->timer, s->next_event);
> }
> @@ -73,27 +75,30 @@ static void ptimer_reload(ptimer_state *s)
> static void ptimer_tick(void *opaque)
> {
> ptimer_state *s = (ptimer_state *)opaque;
> - ptimer_trigger(s);
> - s->delta = 0;
> - if (s->enabled == 2) {
> +
> + s->delta = (s->enabled == 1) ? s->limit : 0;
> +
> + if (s->delta == 0) {
> s->enabled = 0;
> } else {
> ptimer_reload(s);
> }
> +
> + ptimer_trigger(s);
> }
>
> uint64_t ptimer_get_count(ptimer_state *s)
> {
> uint64_t counter;
>
> - if (s->enabled) {
> + if (s->enabled && s->delta != 0) {
> int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
> int64_t next = s->next_event;
> bool expired = (now - next >= 0);
> bool oneshot = (s->enabled == 2);
>
> /* Figure out the current counter value. */
> - if (s->period == 0 || (expired && (oneshot || use_icount))) {
> + if (expired && (oneshot || use_icount)) {
> /* Prevent timer underflowing if it should already have
> triggered. */
> counter = 0;
> @@ -165,11 +170,8 @@ void ptimer_run(ptimer_state *s, int oneshot)
> {
> bool was_disabled = !s->enabled;
>
> - if (was_disabled && s->period == 0) {
> - fprintf(stderr, "Timer with period zero, disabling\n");
> - return;
> - }
> s->enabled = oneshot ? 2 : 1;
> +
Stray extra blank line.
> if (was_disabled) {
> s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
> ptimer_reload(s);
> @@ -203,6 +205,7 @@ void ptimer_set_period(ptimer_state *s, int64_t period)
> /* Set counter frequency in Hz. */
> void ptimer_set_freq(ptimer_state *s, uint32_t freq)
> {
> + g_assert(freq != 0 && freq <= 1000000000);
This probably needs an LL suffix like the others below.
> s->delta = ptimer_get_count(s);
> s->period = 1000000000ll / freq;
> s->period_frac = (1000000000ll << 32) / freq;
> @@ -230,10 +233,15 @@ uint64_t ptimer_get_limit(ptimer_state *s)
> return s->limit;
> }
>
> +void ptimer_set_policy(ptimer_state *s, enum ptimer_policy policy)
> +{
> + s->policy = policy;
> +}
> +
> const VMStateDescription vmstate_ptimer = {
> .name = "ptimer",
> - .version_id = 1,
> - .minimum_version_id = 1,
> + .version_id = 2,
> + .minimum_version_id = 2,
> .fields = (VMStateField[]) {
> VMSTATE_UINT8(enabled, ptimer_state),
> VMSTATE_UINT64(limit, ptimer_state),
> @@ -243,6 +251,7 @@ const VMStateDescription vmstate_ptimer = {
> VMSTATE_INT64(last_event, ptimer_state),
> VMSTATE_INT64(next_event, ptimer_state),
> VMSTATE_TIMER_PTR(timer, ptimer_state),
> + VMSTATE_UINT8(policy, ptimer_state),
> VMSTATE_END_OF_LIST()
> }
> };
> diff --git a/include/hw/ptimer.h b/include/hw/ptimer.h
> index e397db5..221f591 100644
> --- a/include/hw/ptimer.h
> +++ b/include/hw/ptimer.h
> @@ -12,6 +12,11 @@
> #include "qemu/timer.h"
> #include "migration/vmstate.h"
>
> +enum ptimer_policy {
> + UNIMPLEMENTED,
> + SET_CNT_TO_0_TRIGGERS_IRQ_AFTER_ONE_PERIOD,
Given that enums aren't namespaced, "UNIMPLEMENTED" is too broad;
you need some PTIMER_ prefixes. Also the second one of these is
too long a name.
> +};
> +
> /* ptimer.c */
> typedef struct ptimer_state ptimer_state;
> typedef void (*ptimer_cb)(void *opaque);
> @@ -25,6 +30,7 @@ uint64_t ptimer_get_count(ptimer_state *s);
> void ptimer_set_count(ptimer_state *s, uint64_t count);
> void ptimer_run(ptimer_state *s, int oneshot);
> void ptimer_stop(ptimer_state *s);
> +void ptimer_set_policy(ptimer_state *s, enum ptimer_policy policy);
Documentation comment headers for new global functions, please.
> extern const VMStateDescription vmstate_ptimer;
thanks
-- PMM
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v13 6/8] hw/ptimer: Support running with counter = 0 by introducing new policy feature
2016-06-06 10:09 ` Peter Maydell
@ 2016-06-06 17:24 ` Dmitry Osipenko
0 siblings, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-06-06 17:24 UTC (permalink / raw)
To: Peter Maydell; +Cc: QEMU Developers, qemu-arm, Peter Crosthwaite
On 06.06.2016 13:09, Peter Maydell wrote:
> On 27 May 2016 at 18:03, Dmitry Osipenko <digetx@gmail.com> wrote:
>> Bump VMSD version since a new VMState member is added.
>
> I'm afraid you can't do that -- the ptimer code is used by a lot of
> platforms including some which do not permit migration compatibility
> breaks (at least sparc, for instance).
>
> If you want to add this feature you need to at least make it seamlessly
> support migration from the old version using a vmstate subsection.
>
> Better would be to make the policy a QOM property -- then it is
> constant for the lifetime of the timer and doesn't need to be migrated
> at all. (There's no reason to support timers which change policy at
> runtime, I hope). The default property value should be "same behaviour
> as previously".
>
QOM property should fit well, thanks for the suggestion.
> This whole change would be more solidly supported if it came with
> a clear description of the bug being fixed (ie what device is currently
> using the default-but-wrong behaviour and is going to be fixed by
> setting a different policy).
>
I'll extend the commit description, thanks for the note.
>> + if (s->delta == 0 && s->policy == UNIMPLEMENTED) {
>> + hw_error("ptimer: Running with counter=0 is unimplemented by " \
>> + "this timer, fix it!\n");
>
> hw_error() is effectively a verbose assert. Have you checked that
> all the users of ptimer do the right thing? If not, we can't
> assert() until we've fixed them all. Similarly below.
No, I haven't examined all the users thoroughly, will take look through them for
this issue.
In my opinion it's better to know that your QEMU machine doesn't behave
correctly and just fix it.
> Note that what we had previously was just a warning-and-continue
>
Yes, and it was literally "forever" there: "committed on 23 May 2007". Seems
just nobody cared to fix/improve it since then.
I'll keep old "warning-and-continue" in this patch and derive "hw_error" change
into a separate patch.
>> @@ -165,11 +170,8 @@ void ptimer_run(ptimer_state *s, int oneshot)
>> {
>> bool was_disabled = !s->enabled;
>>
>> - if (was_disabled && s->period == 0) {
>> - fprintf(stderr, "Timer with period zero, disabling\n");
>> - return;
>> - }
>> s->enabled = oneshot ? 2 : 1;
>> +
>
> Stray extra blank line.
>
Ack.
>> +enum ptimer_policy {
>> + UNIMPLEMENTED,
>> + SET_CNT_TO_0_TRIGGERS_IRQ_AFTER_ONE_PERIOD,
>
> Given that enums aren't namespaced, "UNIMPLEMENTED" is too broad;
> you need some PTIMER_ prefixes. Also the second one of these is
> too long a name.
>
Ack.
>> +};
>> +
>> /* ptimer.c */
>> typedef struct ptimer_state ptimer_state;
>> typedef void (*ptimer_cb)(void *opaque);
>> @@ -25,6 +30,7 @@ uint64_t ptimer_get_count(ptimer_state *s);
>> void ptimer_set_count(ptimer_state *s, uint64_t count);
>> void ptimer_run(ptimer_state *s, int oneshot);
>> void ptimer_stop(ptimer_state *s);
>> +void ptimer_set_policy(ptimer_state *s, enum ptimer_policy policy);
>
> Documentation comment headers for new global functions, please.
>
Ack. Thanks a lot for the review!
--
Dmitry
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v13 7/8] hw/ptimer: Fix counter - 1 returned by ptimer_get_count for the active timer
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
` (5 preceding siblings ...)
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 6/8] hw/ptimer: Support running with counter = 0 by introducing new policy feature Dmitry Osipenko
@ 2016-05-27 17:03 ` Dmitry Osipenko
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 8/8] arm_mptimer: Convert to use ptimer Dmitry Osipenko
2016-06-06 13:43 ` [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Peter Maydell
8 siblings, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-27 17:03 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
Due to rounding down performed by ptimer_get_count, it returns counter - 1 for
the active timer. That's incorrect because counter should decrement only after
period been expired, not before. I.e. if running timer has been loaded with
value X, then timer counter should stay with X until period expired and
decrement after. Fix this by adding 1 to the counter value for the active and
unexpired timer.
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
---
hw/core/ptimer.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/hw/core/ptimer.c b/hw/core/ptimer.c
index 9bc70f5..c9f2604 100644
--- a/hw/core/ptimer.c
+++ b/hw/core/ptimer.c
@@ -89,10 +89,10 @@ static void ptimer_tick(void *opaque)
uint64_t ptimer_get_count(ptimer_state *s)
{
+ int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
uint64_t counter;
- if (s->enabled && s->delta != 0) {
- int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+ if (s->enabled && s->delta != 0 && now != s->last_event) {
int64_t next = s->next_event;
bool expired = (now - next >= 0);
bool oneshot = (s->enabled == 2);
@@ -144,7 +144,7 @@ uint64_t ptimer_get_count(ptimer_state *s)
if ((uint32_t)(period_frac << shift))
div += 1;
}
- counter = rem / div;
+ counter = rem / div + (expired ? 0 : 1);
if (expired && counter != 0) {
/* Wrap around periodic counter. */
--
2.8.3
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH v13 8/8] arm_mptimer: Convert to use ptimer
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
` (6 preceding siblings ...)
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 7/8] hw/ptimer: Fix counter - 1 returned by ptimer_get_count for the active timer Dmitry Osipenko
@ 2016-05-27 17:03 ` Dmitry Osipenko
2016-06-06 13:43 ` [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Peter Maydell
8 siblings, 0 replies; 13+ messages in thread
From: Dmitry Osipenko @ 2016-05-27 17:03 UTC (permalink / raw)
To: QEMU Developers, qemu-arm; +Cc: Peter Crosthwaite, Peter Maydell
Current ARM MPTimer implementation uses QEMUTimer for the actual timer,
this implementation isn't complete and mostly tries to duplicate of what
generic ptimer is already doing fine.
Conversion to ptimer brings the following benefits and fixes:
- Simple timer pausing implementation
- Fixes counter value preservation after stopping the timer
- Correctly handles prescaler != 0 / counter = 0 / load = 0 cases
- Code simplification and reduction
Use new ptimer policy feature to correctly emulate running with/setting
counter = 0.
Bump VMSD to version 3, since VMState is changed and is not compatible
with the previous implementation.
Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
---
hw/timer/arm_mptimer.c | 135 ++++++++++++++++++++++-------------------
include/hw/timer/arm_mptimer.h | 5 +-
2 files changed, 72 insertions(+), 68 deletions(-)
diff --git a/hw/timer/arm_mptimer.c b/hw/timer/arm_mptimer.c
index d66bbf0..bffc506 100644
--- a/hw/timer/arm_mptimer.c
+++ b/hw/timer/arm_mptimer.c
@@ -20,9 +20,10 @@
*/
#include "qemu/osdep.h"
+#include "hw/ptimer.h"
#include "hw/timer/arm_mptimer.h"
#include "qapi/error.h"
-#include "qemu/timer.h"
+#include "qemu/main-loop.h"
#include "qom/cpu.h"
/* This device implements the per-cpu private timer and watchdog block
@@ -44,55 +45,54 @@ static inline void timerblock_update_irq(TimerBlock *tb)
}
/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
-static inline uint32_t timerblock_scale(TimerBlock *tb)
+static inline uint32_t timerblock_scale(uint32_t control)
{
- return (((tb->control >> 8) & 0xff) + 1) * 10;
+ return (((control >> 8) & 0xff) + 1) * 10;
}
-static void timerblock_reload(TimerBlock *tb, int restart)
+static inline void timerblock_set_count(struct ptimer_state *timer,
+ uint32_t control, uint64_t *count)
{
- if (tb->count == 0) {
- return;
+ /* PTimer would immediately trigger interrupt for periodic timer
+ * when counter set to 0, MPtimer under certain condition only.
+ */
+ if ((control & 3) == 3 && (control & 0xff00) == 0 && *count == 0) {
+ *count = ptimer_get_limit(timer);
}
- if (restart) {
- tb->tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+ ptimer_set_count(timer, *count);
+}
+
+static inline void timerblock_run(struct ptimer_state *timer,
+ uint32_t control, uint32_t load)
+{
+ if ((control & 1) && ((control & 0xff00) || load != 0)) {
+ ptimer_run(timer, !(control & 2));
}
- tb->tick += (int64_t)tb->count * timerblock_scale(tb);
- timer_mod(tb->timer, tb->tick);
}
static void timerblock_tick(void *opaque)
{
TimerBlock *tb = (TimerBlock *)opaque;
tb->status = 1;
- if (tb->control & 2) {
- tb->count = tb->load;
- timerblock_reload(tb, 0);
- } else {
- tb->count = 0;
- }
timerblock_update_irq(tb);
+ /* Periodic timer with load = 0 and prescaler != 0 would re-trigger
+ * IRQ after one period, otherwise it either stops or wraps around.
+ */
+ if ((tb->control & 2) && (tb->control & 0xff00) &&
+ ptimer_get_limit(tb->timer) == 0) {
+ ptimer_run(tb->timer, 0);
+ }
}
static uint64_t timerblock_read(void *opaque, hwaddr addr,
unsigned size)
{
TimerBlock *tb = (TimerBlock *)opaque;
- int64_t val;
switch (addr) {
case 0: /* Load */
- return tb->load;
+ return ptimer_get_limit(tb->timer);
case 4: /* Counter. */
- if (((tb->control & 1) == 0) || (tb->count == 0)) {
- return 0;
- }
- /* Slow and ugly, but hopefully won't happen too often. */
- val = tb->tick - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
- val /= timerblock_scale(tb);
- if (val < 0) {
- val = 0;
- }
- return val;
+ return ptimer_get_count(tb->timer);
case 8: /* Control. */
return tb->control;
case 12: /* Interrupt status. */
@@ -106,37 +106,45 @@ static void timerblock_write(void *opaque, hwaddr addr,
uint64_t value, unsigned size)
{
TimerBlock *tb = (TimerBlock *)opaque;
- int64_t old;
+ uint32_t control = tb->control;
switch (addr) {
case 0: /* Load */
- tb->load = value;
- /* Fall through. */
- case 4: /* Counter. */
- if ((tb->control & 1) && tb->count) {
- /* Cancel the previous timer. */
- timer_del(tb->timer);
+ /* Setting load to 0 stops the timer without doing the tick if
+ * prescaler = 0.
+ */
+ if ((control & 1) && (control & 0xff00) == 0 && value == 0) {
+ ptimer_stop(tb->timer);
}
- tb->count = value;
- if (tb->control & 1) {
- timerblock_reload(tb, 1);
+ ptimer_set_limit(tb->timer, value, 1);
+ timerblock_run(tb->timer, control, value);
+ break;
+ case 4: /* Counter. */
+ /* Setting counter to 0 stops the one-shot timer, or periodic with
+ * load == 0, without doing the tick if prescaler = 0.
+ */
+ if ((control & 1) && (control & 0xff00) == 0 && value == 0 &&
+ (!(control & 2) || ptimer_get_limit(tb->timer) == 0)) {
+ ptimer_stop(tb->timer);
}
+ timerblock_set_count(tb->timer, control, &value);
+ timerblock_run(tb->timer, control, value);
break;
case 8: /* Control. */
- old = tb->control;
- tb->control = value;
+ if ((control & 1) && !(value & 1)) {
+ ptimer_stop(tb->timer);
+ }
+ if ((control & 0xff00) != (value & 0xff00)) {
+ ptimer_set_period(tb->timer, timerblock_scale(value));
+ }
if (value & 1) {
- if ((old & 1) && (tb->count != 0)) {
- /* Do nothing if timer is ticking right now. */
- break;
+ uint64_t count = ptimer_get_count(tb->timer);
+ /* Re-load periodic timer counter if needed. */
+ if ((value & 2) && count == 0) {
+ timerblock_set_count(tb->timer, value, &count);
}
- if (tb->control & 2) {
- tb->count = tb->load;
- }
- timerblock_reload(tb, 1);
- } else if (old & 1) {
- /* Shutdown the timer. */
- timer_del(tb->timer);
+ timerblock_run(tb->timer, value, count);
}
+ tb->control = value;
break;
case 12: /* Interrupt status. */
tb->status &= ~value;
@@ -186,13 +194,12 @@ static const MemoryRegionOps timerblock_ops = {
static void timerblock_reset(TimerBlock *tb)
{
- tb->count = 0;
- tb->load = 0;
tb->control = 0;
tb->status = 0;
- tb->tick = 0;
if (tb->timer) {
- timer_del(tb->timer);
+ ptimer_stop(tb->timer);
+ ptimer_set_limit(tb->timer, 0, 1);
+ ptimer_set_period(tb->timer, timerblock_scale(0));
}
}
@@ -238,7 +245,10 @@ static void arm_mptimer_realize(DeviceState *dev, Error **errp)
*/
for (i = 0; i < s->num_cpu; i++) {
TimerBlock *tb = &s->timerblock[i];
- tb->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, timerblock_tick, tb);
+ QEMUBH *bh = qemu_bh_new(timerblock_tick, tb);
+ tb->timer = ptimer_init(bh);
+ ptimer_set_policy(tb->timer,
+ SET_CNT_TO_0_TRIGGERS_IRQ_AFTER_ONE_PERIOD);
sysbus_init_irq(sbd, &tb->irq);
memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb,
"arm_mptimer_timerblock", 0x20);
@@ -248,26 +258,23 @@ static void arm_mptimer_realize(DeviceState *dev, Error **errp)
static const VMStateDescription vmstate_timerblock = {
.name = "arm_mptimer_timerblock",
- .version_id = 2,
- .minimum_version_id = 2,
+ .version_id = 3,
+ .minimum_version_id = 3,
.fields = (VMStateField[]) {
- VMSTATE_UINT32(count, TimerBlock),
- VMSTATE_UINT32(load, TimerBlock),
VMSTATE_UINT32(control, TimerBlock),
VMSTATE_UINT32(status, TimerBlock),
- VMSTATE_INT64(tick, TimerBlock),
- VMSTATE_TIMER_PTR(timer, TimerBlock),
+ VMSTATE_PTIMER(timer, TimerBlock),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_arm_mptimer = {
.name = "arm_mptimer",
- .version_id = 2,
- .minimum_version_id = 2,
+ .version_id = 3,
+ .minimum_version_id = 3,
.fields = (VMStateField[]) {
VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
- 2, vmstate_timerblock, TimerBlock),
+ 3, vmstate_timerblock, TimerBlock),
VMSTATE_END_OF_LIST()
}
};
diff --git a/include/hw/timer/arm_mptimer.h b/include/hw/timer/arm_mptimer.h
index b34cba0..c46d8d2 100644
--- a/include/hw/timer/arm_mptimer.h
+++ b/include/hw/timer/arm_mptimer.h
@@ -27,12 +27,9 @@
/* State of a single timer or watchdog block */
typedef struct {
- uint32_t count;
- uint32_t load;
uint32_t control;
uint32_t status;
- int64_t tick;
- QEMUTimer *timer;
+ struct ptimer_state *timer;
qemu_irq irq;
MemoryRegion iomem;
} TimerBlock;
--
2.8.3
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion
2016-05-27 17:03 [Qemu-devel] [PATCH v13 0/8] PTimer fixes/features and ARM MPTimer conversion Dmitry Osipenko
` (7 preceding siblings ...)
2016-05-27 17:03 ` [Qemu-devel] [PATCH v13 8/8] arm_mptimer: Convert to use ptimer Dmitry Osipenko
@ 2016-06-06 13:43 ` Peter Maydell
8 siblings, 0 replies; 13+ messages in thread
From: Peter Maydell @ 2016-06-06 13:43 UTC (permalink / raw)
To: Dmitry Osipenko; +Cc: QEMU Developers, qemu-arm, Peter Crosthwaite
On 27 May 2016 at 18:03, Dmitry Osipenko <digetx@gmail.com> wrote:
> Hello,
>
> Current QEMU ARM MPTimer device model provides only a certain subset of the
> emulation behavior, so this patch series is supposed to add missing parts by
> converting the MPTimer to use generic ptimer helper. It fixes some important
> ptimer bugs and provides new features that are required for the ARM MPTimer.
>
> Emulation behavior is verified against the real HW by running specially
> crafted MPTimer tests in both icount and non-icount modes:
>
> https://gist.github.com/digetx/dbd46109503b1a91941a
I've applied patches 1-5 to target-arm.next; thanks.
-- PMM
^ permalink raw reply [flat|nested] 13+ messages in thread