Skip to content

Commit 5b3d524

Browse files
bibo-maochenhuacai
authored andcommitted
LoongArch: KVM: Fix timer emulation with oneshot mode
When timer is fired in oneshot mode, CSR TVAL will be -1 rather than 0. There needs special handing for this situation. There are two scenarios when oneshot timer is fired. One scenario is that time is fired after exiting to host, CSR TVAL is set with 0 in order to inject hw interrupt, and -1 will assigned to CSR TVAL soon. The other situation is that timer is fired in VM and guest kernel is hanlding timer IRQ, IRQ is acked and is ready to set next expired timer value, then vm exits to host. Timer interrupt should not be inject at this point, else there will be spurious timer interrupt. Here hw timer irq status in CSR ESTAT is used to judge these two scenarios. If CSR TVAL is -1, the oneshot timer is fired; and if timer hw irq is on in CSR ESTAT register, it happens after exiting to host; else if timer hw irq is off, we think that it happens in vm and timer IRQ handler has already acked IRQ. With this patch, runltp with version ltp20230516 passes to run in vm. Signed-off-by: Bibo Mao <maobibo@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent 1ab9c60 commit 5b3d524

1 file changed

Lines changed: 55 additions & 13 deletions

File tree

arch/loongarch/kvm/timer.c

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,19 @@ void kvm_init_timer(struct kvm_vcpu *vcpu, unsigned long timer_hz)
6969
*/
7070
void kvm_restore_timer(struct kvm_vcpu *vcpu)
7171
{
72-
unsigned long cfg, delta, period;
72+
unsigned long cfg, estat;
73+
unsigned long ticks, delta, period;
7374
ktime_t expire, now;
7475
struct loongarch_csrs *csr = vcpu->arch.csr;
7576

7677
/*
7778
* Set guest stable timer cfg csr
79+
* Disable timer before restore estat CSR register, avoid to
80+
* get invalid timer interrupt for old timer cfg
7881
*/
7982
cfg = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_TCFG);
83+
84+
write_gcsr_timercfg(0);
8085
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_ESTAT);
8186
kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_TCFG);
8287
if (!(cfg & CSR_TCFG_EN)) {
@@ -90,20 +95,47 @@ void kvm_restore_timer(struct kvm_vcpu *vcpu)
9095
*/
9196
hrtimer_cancel(&vcpu->arch.swtimer);
9297

98+
/*
99+
* From LoongArch Reference Manual Volume 1 Chapter 7.6.2
100+
* If oneshot timer is fired, CSR TVAL will be -1, there are two
101+
* conditions:
102+
* 1) timer is fired during exiting to host
103+
* 2) timer is fired and vm is doing timer irq, and then exiting to
104+
* host. Host should not inject timer irq to avoid spurious
105+
* timer interrupt again
106+
*/
107+
ticks = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_TVAL);
108+
estat = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_ESTAT);
109+
if (!(cfg & CSR_TCFG_PERIOD) && (ticks > cfg)) {
110+
/*
111+
* Writing 0 to LOONGARCH_CSR_TVAL will inject timer irq
112+
* and set CSR TVAL with -1
113+
*/
114+
write_gcsr_timertick(0);
115+
116+
/*
117+
* Writing CSR_TINTCLR_TI to LOONGARCH_CSR_TINTCLR will clear
118+
* timer interrupt, and CSR TVAL keeps unchanged with -1, it
119+
* avoids spurious timer interrupt
120+
*/
121+
if (!(estat & CPU_TIMER))
122+
gcsr_write(CSR_TINTCLR_TI, LOONGARCH_CSR_TINTCLR);
123+
return;
124+
}
125+
93126
/*
94127
* Set remainder tick value if not expired
95128
*/
129+
delta = 0;
96130
now = ktime_get();
97131
expire = vcpu->arch.expire;
98132
if (ktime_before(now, expire))
99133
delta = ktime_to_tick(vcpu, ktime_sub(expire, now));
100-
else {
101-
if (cfg & CSR_TCFG_PERIOD) {
102-
period = cfg & CSR_TCFG_VAL;
103-
delta = ktime_to_tick(vcpu, ktime_sub(now, expire));
104-
delta = period - (delta % period);
105-
} else
106-
delta = 0;
134+
else if (cfg & CSR_TCFG_PERIOD) {
135+
period = cfg & CSR_TCFG_VAL;
136+
delta = ktime_to_tick(vcpu, ktime_sub(now, expire));
137+
delta = period - (delta % period);
138+
107139
/*
108140
* Inject timer here though sw timer should inject timer
109141
* interrupt async already, since sw timer may be cancelled
@@ -122,15 +154,25 @@ void kvm_restore_timer(struct kvm_vcpu *vcpu)
122154
*/
123155
static void _kvm_save_timer(struct kvm_vcpu *vcpu)
124156
{
125-
unsigned long ticks, delta;
157+
unsigned long ticks, delta, cfg;
126158
ktime_t expire;
127159
struct loongarch_csrs *csr = vcpu->arch.csr;
128160

161+
cfg = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_TCFG);
129162
ticks = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_TVAL);
130-
delta = tick_to_ns(vcpu, ticks);
131-
expire = ktime_add_ns(ktime_get(), delta);
132-
vcpu->arch.expire = expire;
133-
if (ticks) {
163+
164+
/*
165+
* From LoongArch Reference Manual Volume 1 Chapter 7.6.2
166+
* If period timer is fired, CSR TVAL will be reloaded from CSR TCFG
167+
* If oneshot timer is fired, CSR TVAL will be -1
168+
* Here judge one-shot timer fired by checking whether TVAL is larger
169+
* than TCFG
170+
*/
171+
if (ticks < cfg) {
172+
delta = tick_to_ns(vcpu, ticks);
173+
expire = ktime_add_ns(ktime_get(), delta);
174+
vcpu->arch.expire = expire;
175+
134176
/*
135177
* HRTIMER_MODE_PINNED is suggested since vcpu may run in
136178
* the same physical cpu in next time

0 commit comments

Comments
 (0)