Skip to content

Commit 4e88240

Browse files
bibo-maochenhuacai
authored andcommitted
KVM: LoongArch: selftests: Add SW emulated timer test case
This test case setup one-shot timer and execute idle instruction immediately to indicate giving up CPU, hypervisor will emulate SW hrtimer and wakeup vCPU when SW hrtimer is fired. Signed-off-by: Bibo Mao <maobibo@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent df41742 commit 4e88240

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

tools/testing/selftests/kvm/loongarch/arch_timer.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
#include "timer_test.h"
1010
#include "ucall_common.h"
1111

12+
static void do_idle(void)
13+
{
14+
unsigned int intid;
15+
unsigned long estat;
16+
17+
__asm__ __volatile__("idle 0" : : : "memory");
18+
19+
estat = csr_read(LOONGARCH_CSR_ESTAT);
20+
intid = !!(estat & BIT(INT_TI));
21+
22+
/* Make sure pending timer IRQ arrived */
23+
GUEST_ASSERT_EQ(intid, 1);
24+
csr_write(CSR_TINTCLR_TI, LOONGARCH_CSR_TINTCLR);
25+
}
26+
1227
static void guest_irq_handler(struct ex_regs *regs)
1328
{
1429
unsigned int intid;
@@ -97,6 +112,30 @@ static void guest_test_oneshot_timer(uint32_t cpu)
97112
}
98113
}
99114

115+
static void guest_test_emulate_timer(uint32_t cpu)
116+
{
117+
uint32_t config_iter;
118+
uint64_t xcnt_diff_us, us;
119+
struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[cpu];
120+
121+
local_irq_disable();
122+
shared_data->nr_iter = 0;
123+
us = msecs_to_usecs(test_args.timer_period_ms);
124+
for (config_iter = 0; config_iter < test_args.nr_iter; config_iter++) {
125+
shared_data->xcnt = timer_get_cycles();
126+
127+
/* Setup the next interrupt */
128+
timer_set_next_cmp_ms(test_args.timer_period_ms, false);
129+
do_idle();
130+
131+
xcnt_diff_us = cycles_to_usec(timer_get_cycles() - shared_data->xcnt);
132+
__GUEST_ASSERT(xcnt_diff_us >= us,
133+
"xcnt_diff_us = 0x%lx, us = 0x%lx.\n",
134+
xcnt_diff_us, us);
135+
}
136+
local_irq_enable();
137+
}
138+
100139
static void guest_code(void)
101140
{
102141
uint32_t cpu = guest_get_vcpuid();
@@ -105,6 +144,7 @@ static void guest_code(void)
105144
local_irq_enable();
106145
guest_test_period_timer(cpu);
107146
guest_test_oneshot_timer(cpu);
147+
guest_test_emulate_timer(cpu);
108148

109149
GUEST_DONE();
110150
}

0 commit comments

Comments
 (0)