Skip to contentSkip to conversation

arm64: extend arch timer tests

PATCH In review Andre Przywara 2 participants 5 messages Jul 16, 2018 – Jul 17, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover arm64: extend arch timer tests

While debugging some timer emulation IRQ problem, I realised that the failure pattern I was looking at is not covered by kvm-unit-tests.

Add a test for the TVAL register, also check that KVM correctly injects the timer interrupt for the emulated physical timer when the guest is in WFI (hint: it does not, starting with 4.15-rc, I am on it).

Cheers,
Andre.

Andre Przywara (2):
arm64: timer: add TVAL accessors
arm64: timer: Add TVAL timeout test and test IRQ trigger

 arm/timer.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
Signature · 1 line
2.14.4
#1 lore raw

PATCH 1/2 arm64: timer: add TVAL accessors

The ARM arch timer features the CVAL register, which holds an absolute value that is compared against the counter value.
But there is also the TVAL register, which is defined as (CVAL - counter), and can be used to program or read relative timeouts.

Add accessors for the TVAL register, to be able to read and write them easily later.

---
 arm/timer.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
arm/timer.c +26 -0
index 5f3135f..1c9ef44 100644
@@ -40,6 +40,16 @@ static void write_vtimer_cval(u64 val)
 	write_sysreg(val, cntv_cval_el0);
 }
 
+static s32 read_vtimer_tval(void)
+{
+	return read_sysreg(cntv_tval_el0);
+}
+
+static void write_vtimer_tval(s32 val)
+{
+	write_sysreg(val, cntv_tval_el0);
+}
+
 static u64 read_vtimer_ctl(void)
 {
 	return read_sysreg(cntv_ctl_el0);
@@ -65,6 +75,16 @@ static void write_ptimer_cval(u64 val)
 	write_sysreg(val, cntp_cval_el0);
 }
 
+static s32 read_ptimer_tval(void)
+{
+	return read_sysreg(cntp_tval_el0);
+}
+
+static void write_ptimer_tval(s32 val)
+{
+	write_sysreg(val, cntp_tval_el0);
+}
+
 static u64 read_ptimer_ctl(void)
 {
 	return read_sysreg(cntp_ctl_el0);
@@ -82,6 +102,8 @@ struct timer_info {
 	u64 (*read_counter)(void);
 	u64 (*read_cval)(void);
 	void (*write_cval)(u64);
+	s32 (*read_tval)(void);
+	void (*write_tval)(s32);
 	u64 (*read_ctl)(void);
 	void (*write_ctl)(u64);
 };
@@ -91,6 +113,8 @@ static struct timer_info vtimer_info = {
 	.read_counter = read_vtimer_counter,
 	.read_cval = read_vtimer_cval,
 	.write_cval = write_vtimer_cval,
+	.read_tval = read_vtimer_tval,
+	.write_tval = write_vtimer_tval,
 	.read_ctl = read_vtimer_ctl,
 	.write_ctl = write_vtimer_ctl,
 };
@@ -100,6 +124,8 @@ static struct timer_info ptimer_info = {
 	.read_counter = read_ptimer_counter,
 	.read_cval = read_ptimer_cval,
 	.write_cval = write_ptimer_cval,
+	.read_tval = read_ptimer_tval,
+	.write_tval = write_ptimer_tval,
 	.read_ctl = read_ptimer_ctl,
 	.write_ctl = write_ptimer_ctl,
 };
Signature · 1 line
2.14.4
#3 lore raw

PATCH 2/2 arm64: timer: Add TVAL timeout IRQ trigger test

In reply to Andre Przywara

So far we were only testing the CVAL register. Add a test which programs a (relative) TVAL value to check this functionality as well. Also we go into WFI and wait for the interrupt to release us from it. The timer tests are run with a 2 second timeout on the host side, so that any failure in coming back would be covered.

---
 arm/timer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arm/timer.c +10 -0
index 1c9ef44..275d049 100644
@@ -211,6 +211,7 @@ static void test_timer(struct timer_info *info)
 	u64 now = info->read_counter();
 	u64 time_10s = read_sysreg(cntfrq_el0) * 10;
 	u64 later = now + time_10s;
+	s32 left;
 
 	/* We don't want the irq handler to fire because that will change the
 	 * timer state and we want to test the timer output signal.  We can
@@ -236,6 +237,15 @@ static void test_timer(struct timer_info *info)
 
 	/* Disable the timer again */
 	info->write_ctl(0);
+
+	/* Test TVAL and IRQ trigger */
+	info->irq_received = false;
+	info->write_tval(read_sysreg(cntfrq_el0) / 100);	/* 10 ms */
+	info->write_ctl(ARCH_TIMER_CTL_ENABLE);
+	wfi();
+	left = info->read_tval();
+	report("interrupt received after TVAL/WFI", info->irq_received);
+	report("timer has expired (%d)", left < 0, left);
 }
 
 static void test_vtimer(void)
Signature · 1 line
2.14.4
Reviewed-by on v1 1/2
#2 lore raw

Re: [kvm-unit-tests PATCH 1/2] arm64: timer: add TVAL accessors

In reply to Andre Przywara
Quoted 5 lines · Andre Przywara

The ARM arch timer features the CVAL register, which holds an absolute value that is compared against the counter value.
But there is also the TVAL register, which is defined as (CVAL - counter), and can be used to program or read relative timeouts.

Quoted 8 lines

Add accessors for the TVAL register, to be able to read and write them easily later.

---
 arm/timer.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
Reviewed-by on v1 2/2
#4 lore raw

Re: [kvm-unit-tests PATCH 2/2] arm64: timer: Add TVAL timeout IRQ trigger test

In reply to Andre Przywara
Quoted 11 lines · Andre Przywara

So far we were only testing the CVAL register. Add a test which programs a (relative) TVAL value to check this functionality as well. Also we go into WFI and wait for the interrupt to release us from it. The timer tests are run with a 2 second timeout on the host side, so that any failure in coming back would be covered.

---
 arm/timer.c | 10 ++++++++++
 1 file changed, 10 insertions(+)