Skip to content

Commit e604dd5

Browse files
christofferdall-armMarc Zyngier
authored andcommitted
KVM: arm/arm64: timer: Rework data structures for multiple timers
Prepare for having 4 timer data structures (2 for now). Move loaded to the cpu data structure and not the individual timer structure, in preparation for assigning the EL1 phys timer as well. Signed-off-by: Christoffer Dall <christoffer.dall@arm.com> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 84135d3 commit e604dd5

2 files changed

Lines changed: 51 additions & 48 deletions

File tree

‎include/kvm/arm_arch_timer.h‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,40 @@ enum kvm_arch_timer_regs {
3636
};
3737

3838
struct arch_timer_context {
39+
struct kvm_vcpu *vcpu;
40+
3941
/* Registers: control register, timer value */
4042
u32 cnt_ctl;
4143
u64 cnt_cval;
4244

4345
/* Timer IRQ */
4446
struct kvm_irq_level irq;
4547

46-
/*
47-
* We have multiple paths which can save/restore the timer state
48-
* onto the hardware, so we need some way of keeping track of
49-
* where the latest state is.
50-
*
51-
* loaded == true: State is loaded on the hardware registers.
52-
* loaded == false: State is stored in memory.
53-
*/
54-
bool loaded;
55-
5648
/* Virtual offset */
57-
u64 cntvoff;
49+
u64 cntvoff;
50+
51+
/* Emulated Timer (may be unused) */
52+
struct hrtimer hrtimer;
5853
};
5954

6055
struct arch_timer_cpu {
61-
struct arch_timer_context vtimer;
62-
struct arch_timer_context ptimer;
56+
struct arch_timer_context timers[NR_KVM_TIMERS];
6357

6458
/* Background timer used when the guest is not running */
6559
struct hrtimer bg_timer;
6660

67-
/* Physical timer emulation */
68-
struct hrtimer phys_timer;
69-
7061
/* Is the timer enabled */
7162
bool enabled;
63+
64+
/*
65+
* We have multiple paths which can save/restore the timer state
66+
* onto the hardware, so we need some way of keeping track of
67+
* where the latest state is.
68+
*
69+
* loaded == true: State is loaded on the hardware registers.
70+
* loaded == false: State is stored in memory.
71+
*/
72+
bool loaded;
7273
};
7374

7475
int kvm_timer_hyp_init(bool);
@@ -98,10 +99,10 @@ void kvm_timer_init_vhe(void);
9899

99100
bool kvm_arch_timer_get_input_level(int vintid);
100101

101-
#define vcpu_vtimer(v) (&(v)->arch.timer_cpu.vtimer)
102-
#define vcpu_ptimer(v) (&(v)->arch.timer_cpu.ptimer)
103-
#define vcpu_get_timer(v,t) \
104-
(t == TIMER_VTIMER ? vcpu_vtimer(v) : vcpu_ptimer(v))
102+
#define vcpu_timer(v) (&(v)->arch.timer_cpu)
103+
#define vcpu_get_timer(v,t) (&vcpu_timer(v)->timers[(t)])
104+
#define vcpu_vtimer(v) (&(v)->arch.timer_cpu.timers[TIMER_VTIMER])
105+
#define vcpu_ptimer(v) (&(v)->arch.timer_cpu.timers[TIMER_PTIMER])
105106

106107
u64 kvm_arm_timer_read_sysreg(struct kvm_vcpu *vcpu,
107108
enum kvm_arch_timers tmr,

‎virt/kvm/arm/arch_timer.c‎

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,11 @@ static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
184184
static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt)
185185
{
186186
struct arch_timer_context *ptimer;
187-
struct arch_timer_cpu *timer;
188187
struct kvm_vcpu *vcpu;
189188
u64 ns;
190189

191-
timer = container_of(hrt, struct arch_timer_cpu, phys_timer);
192-
vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
193-
ptimer = vcpu_ptimer(vcpu);
190+
ptimer = container_of(hrt, struct arch_timer_context, hrtimer);
191+
vcpu = ptimer->vcpu;
194192

195193
/*
196194
* Check that the timer has really expired from the guest's
@@ -209,9 +207,10 @@ static enum hrtimer_restart kvm_phys_timer_expire(struct hrtimer *hrt)
209207

210208
static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
211209
{
210+
struct arch_timer_cpu *timer = vcpu_timer(timer_ctx->vcpu);
212211
u64 cval, now;
213212

214-
if (timer_ctx->loaded) {
213+
if (timer->loaded) {
215214
u32 cnt_ctl;
216215

217216
/* Only the virtual timer can be loaded so far */
@@ -280,7 +279,6 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
280279
/* Schedule the background timer for the emulated timer. */
281280
static void phys_timer_emulate(struct kvm_vcpu *vcpu)
282281
{
283-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
284282
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
285283

286284
/*
@@ -289,11 +287,11 @@ static void phys_timer_emulate(struct kvm_vcpu *vcpu)
289287
* then we also don't need a soft timer.
290288
*/
291289
if (kvm_timer_should_fire(ptimer) || !kvm_timer_irq_can_fire(ptimer)) {
292-
soft_timer_cancel(&timer->phys_timer);
290+
soft_timer_cancel(&ptimer->hrtimer);
293291
return;
294292
}
295293

296-
soft_timer_start(&timer->phys_timer, kvm_timer_compute_delta(ptimer));
294+
soft_timer_start(&ptimer->hrtimer, kvm_timer_compute_delta(ptimer));
297295
}
298296

299297
/*
@@ -303,7 +301,7 @@ static void phys_timer_emulate(struct kvm_vcpu *vcpu)
303301
*/
304302
static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
305303
{
306-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
304+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
307305
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
308306
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
309307
bool level;
@@ -329,13 +327,13 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
329327

330328
static void vtimer_save_state(struct kvm_vcpu *vcpu)
331329
{
332-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
330+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
333331
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
334332
unsigned long flags;
335333

336334
local_irq_save(flags);
337335

338-
if (!vtimer->loaded)
336+
if (!timer->loaded)
339337
goto out;
340338

341339
if (timer->enabled) {
@@ -347,7 +345,7 @@ static void vtimer_save_state(struct kvm_vcpu *vcpu)
347345
write_sysreg_el0(0, cntv_ctl);
348346
isb();
349347

350-
vtimer->loaded = false;
348+
timer->loaded = false;
351349
out:
352350
local_irq_restore(flags);
353351
}
@@ -359,7 +357,7 @@ static void vtimer_save_state(struct kvm_vcpu *vcpu)
359357
*/
360358
static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
361359
{
362-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
360+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
363361
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
364362
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
365363

@@ -379,20 +377,20 @@ static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
379377

380378
static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
381379
{
382-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
380+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
383381

384382
soft_timer_cancel(&timer->bg_timer);
385383
}
386384

387385
static void vtimer_restore_state(struct kvm_vcpu *vcpu)
388386
{
389-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
387+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
390388
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
391389
unsigned long flags;
392390

393391
local_irq_save(flags);
394392

395-
if (vtimer->loaded)
393+
if (timer->loaded)
396394
goto out;
397395

398396
if (timer->enabled) {
@@ -401,7 +399,7 @@ static void vtimer_restore_state(struct kvm_vcpu *vcpu)
401399
write_sysreg_el0(vtimer->cnt_ctl, cntv_ctl);
402400
}
403401

404-
vtimer->loaded = true;
402+
timer->loaded = true;
405403
out:
406404
local_irq_restore(flags);
407405
}
@@ -462,7 +460,7 @@ static void kvm_timer_vcpu_load_nogic(struct kvm_vcpu *vcpu)
462460

463461
void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
464462
{
465-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
463+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
466464
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
467465
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
468466

@@ -507,7 +505,8 @@ bool kvm_timer_should_notify_user(struct kvm_vcpu *vcpu)
507505

508506
void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
509507
{
510-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
508+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
509+
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
511510

512511
if (unlikely(!timer->enabled))
513512
return;
@@ -523,7 +522,7 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
523522
* In any case, we re-schedule the hrtimer for the physical timer when
524523
* coming back to the VCPU thread in kvm_timer_vcpu_load().
525524
*/
526-
soft_timer_cancel(&timer->phys_timer);
525+
soft_timer_cancel(&ptimer->hrtimer);
527526

528527
if (swait_active(kvm_arch_vcpu_wq(vcpu)))
529528
kvm_timer_blocking(vcpu);
@@ -559,7 +558,7 @@ static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
559558

560559
void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
561560
{
562-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
561+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
563562

564563
if (unlikely(!timer->enabled))
565564
return;
@@ -570,7 +569,7 @@ void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
570569

571570
int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
572571
{
573-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
572+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
574573
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
575574
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
576575

@@ -611,22 +610,25 @@ static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)
611610

612611
void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
613612
{
614-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
613+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
615614
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
616615
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
617616

618617
/* Synchronize cntvoff across all vtimers of a VM. */
619618
update_vtimer_cntvoff(vcpu, kvm_phys_timer_read());
620-
vcpu_ptimer(vcpu)->cntvoff = 0;
619+
ptimer->cntvoff = 0;
621620

622621
hrtimer_init(&timer->bg_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
623622
timer->bg_timer.function = kvm_bg_timer_expire;
624623

625-
hrtimer_init(&timer->phys_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
626-
timer->phys_timer.function = kvm_phys_timer_expire;
624+
hrtimer_init(&ptimer->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
625+
ptimer->hrtimer.function = kvm_phys_timer_expire;
627626

628627
vtimer->irq.irq = default_vtimer_irq.irq;
629628
ptimer->irq.irq = default_ptimer_irq.irq;
629+
630+
vtimer->vcpu = vcpu;
631+
ptimer->vcpu = vcpu;
630632
}
631633

632634
static void kvm_timer_init_interrupt(void *info)
@@ -860,7 +862,7 @@ int kvm_timer_hyp_init(bool has_gic)
860862

861863
void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
862864
{
863-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
865+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
864866

865867
soft_timer_cancel(&timer->bg_timer);
866868
}
@@ -904,7 +906,7 @@ bool kvm_arch_timer_get_input_level(int vintid)
904906

905907
int kvm_timer_enable(struct kvm_vcpu *vcpu)
906908
{
907-
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
909+
struct arch_timer_cpu *timer = vcpu_timer(vcpu);
908910
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
909911
int ret;
910912

0 commit comments

Comments
 (0)