Skip to content

Commit 00dafa0

Browse files
chazyChristoffer Dall
authored andcommitted
KVM: arm/arm64: vgic: Get rid of live_lrs
There is no need to calculate and maintain live_lrs when we always populate the lowest numbered LRs first on every entry and clear all LRs on every exit. Acked-by: Marc Zyngier <[email protected]> Signed-off-by: Christoffer Dall <[email protected]>
1 parent f676958 commit 00dafa0

File tree

3 files changed

+22
-61
lines changed

3 files changed

+22
-61
lines changed

include/kvm/arm_vgic.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,6 @@ struct vgic_cpu {
264264
*/
265265
struct list_head ap_list_head;
266266

267-
u64 live_lrs;
268-
269267
/*
270268
* Members below are used with GICv3 emulation only and represent
271269
* parts of the redistributor.

virt/kvm/arm/hyp/vgic-v2-sr.c

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,23 @@ static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu,
2626
void __iomem *base)
2727
{
2828
struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
29-
int nr_lr = (kern_hyp_va(&kvm_vgic_global_state))->nr_lr;
29+
u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
3030
u32 eisr0, eisr1;
3131
int i;
3232
bool expect_mi;
3333

3434
expect_mi = !!(cpu_if->vgic_hcr & GICH_HCR_UIE);
3535

36-
for (i = 0; i < nr_lr; i++) {
37-
if (!(vcpu->arch.vgic_cpu.live_lrs & (1UL << i)))
38-
continue;
39-
36+
for (i = 0; i < used_lrs && !expect_mi; i++)
4037
expect_mi |= (!(cpu_if->vgic_lr[i] & GICH_LR_HW) &&
4138
(cpu_if->vgic_lr[i] & GICH_LR_EOI));
42-
}
4339

4440
if (expect_mi) {
4541
cpu_if->vgic_misr = readl_relaxed(base + GICH_MISR);
4642

4743
if (cpu_if->vgic_misr & GICH_MISR_EOI) {
4844
eisr0 = readl_relaxed(base + GICH_EISR0);
49-
if (unlikely(nr_lr > 32))
45+
if (unlikely(used_lrs > 32))
5046
eisr1 = readl_relaxed(base + GICH_EISR1);
5147
else
5248
eisr1 = 0;
@@ -87,13 +83,10 @@ static void __hyp_text save_elrsr(struct kvm_vcpu *vcpu, void __iomem *base)
8783
static void __hyp_text save_lrs(struct kvm_vcpu *vcpu, void __iomem *base)
8884
{
8985
struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
90-
int nr_lr = (kern_hyp_va(&kvm_vgic_global_state))->nr_lr;
9186
int i;
87+
u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
9288

93-
for (i = 0; i < nr_lr; i++) {
94-
if (!(vcpu->arch.vgic_cpu.live_lrs & (1UL << i)))
95-
continue;
96-
89+
for (i = 0; i < used_lrs; i++) {
9790
if (cpu_if->vgic_elrsr & (1UL << i))
9891
cpu_if->vgic_lr[i] &= ~GICH_LR_STATE;
9992
else
@@ -110,20 +103,19 @@ void __hyp_text __vgic_v2_save_state(struct kvm_vcpu *vcpu)
110103
struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
111104
struct vgic_dist *vgic = &kvm->arch.vgic;
112105
void __iomem *base = kern_hyp_va(vgic->vctrl_base);
106+
u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
113107

114108
if (!base)
115109
return;
116110

117-
if (vcpu->arch.vgic_cpu.live_lrs) {
111+
if (used_lrs) {
118112
cpu_if->vgic_apr = readl_relaxed(base + GICH_APR);
119113

120114
save_maint_int_state(vcpu, base);
121115
save_elrsr(vcpu, base);
122116
save_lrs(vcpu, base);
123117

124118
writel_relaxed(0, base + GICH_HCR);
125-
126-
vcpu->arch.vgic_cpu.live_lrs = 0;
127119
} else {
128120
cpu_if->vgic_eisr = 0;
129121
cpu_if->vgic_elrsr = ~0UL;
@@ -139,31 +131,20 @@ void __hyp_text __vgic_v2_restore_state(struct kvm_vcpu *vcpu)
139131
struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
140132
struct vgic_dist *vgic = &kvm->arch.vgic;
141133
void __iomem *base = kern_hyp_va(vgic->vctrl_base);
142-
int nr_lr = (kern_hyp_va(&kvm_vgic_global_state))->nr_lr;
143134
int i;
144-
u64 live_lrs = 0;
135+
u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
145136

146137
if (!base)
147138
return;
148139

149-
150-
for (i = 0; i < nr_lr; i++)
151-
if (cpu_if->vgic_lr[i] & GICH_LR_STATE)
152-
live_lrs |= 1UL << i;
153-
154-
if (live_lrs) {
140+
if (used_lrs) {
155141
writel_relaxed(cpu_if->vgic_hcr, base + GICH_HCR);
156142
writel_relaxed(cpu_if->vgic_apr, base + GICH_APR);
157-
for (i = 0; i < nr_lr; i++) {
158-
if (!(live_lrs & (1UL << i)))
159-
continue;
160-
143+
for (i = 0; i < used_lrs; i++) {
161144
writel_relaxed(cpu_if->vgic_lr[i],
162145
base + GICH_LR0 + (i * 4));
163146
}
164147
}
165-
166-
vcpu->arch.vgic_cpu.live_lrs = live_lrs;
167148
}
168149

169150
#ifdef CONFIG_ARM64

virt/kvm/arm/hyp/vgic-v3-sr.c

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,16 @@ static void __hyp_text __gic_v3_set_lr(u64 val, int lr)
118118
}
119119
}
120120

121-
static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu, int nr_lr)
121+
static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu)
122122
{
123123
struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
124124
int i;
125125
bool expect_mi;
126+
u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
126127

127128
expect_mi = !!(cpu_if->vgic_hcr & ICH_HCR_UIE);
128129

129-
for (i = 0; i < nr_lr; i++) {
130-
if (!(vcpu->arch.vgic_cpu.live_lrs & (1UL << i)))
131-
continue;
132-
130+
for (i = 0; i < used_lrs; i++) {
133131
expect_mi |= (!(cpu_if->vgic_lr[i] & ICH_LR_HW) &&
134132
(cpu_if->vgic_lr[i] & ICH_LR_EOI));
135133
}
@@ -150,6 +148,7 @@ static void __hyp_text save_maint_int_state(struct kvm_vcpu *vcpu, int nr_lr)
150148
void __hyp_text __vgic_v3_save_state(struct kvm_vcpu *vcpu)
151149
{
152150
struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
151+
u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
153152
u64 val;
154153

155154
/*
@@ -159,23 +158,19 @@ void __hyp_text __vgic_v3_save_state(struct kvm_vcpu *vcpu)
159158
if (!cpu_if->vgic_sre)
160159
dsb(st);
161160

162-
if (vcpu->arch.vgic_cpu.live_lrs) {
161+
if (used_lrs) {
163162
int i;
164-
u32 max_lr_idx, nr_pri_bits;
163+
u32 nr_pri_bits;
165164

166165
cpu_if->vgic_elrsr = read_gicreg(ICH_ELSR_EL2);
167166

168167
write_gicreg(0, ICH_HCR_EL2);
169168
val = read_gicreg(ICH_VTR_EL2);
170-
max_lr_idx = vtr_to_max_lr_idx(val);
171169
nr_pri_bits = vtr_to_nr_pri_bits(val);
172170

173-
save_maint_int_state(vcpu, max_lr_idx + 1);
174-
175-
for (i = 0; i <= max_lr_idx; i++) {
176-
if (!(vcpu->arch.vgic_cpu.live_lrs & (1UL << i)))
177-
continue;
171+
save_maint_int_state(vcpu);
178172

173+
for (i = 0; i <= used_lrs; i++) {
179174
if (cpu_if->vgic_elrsr & (1 << i))
180175
cpu_if->vgic_lr[i] &= ~ICH_LR_STATE;
181176
else
@@ -203,8 +198,6 @@ void __hyp_text __vgic_v3_save_state(struct kvm_vcpu *vcpu)
203198
default:
204199
cpu_if->vgic_ap1r[0] = read_gicreg(ICH_AP1R0_EL2);
205200
}
206-
207-
vcpu->arch.vgic_cpu.live_lrs = 0;
208201
} else {
209202
cpu_if->vgic_misr = 0;
210203
cpu_if->vgic_eisr = 0;
@@ -232,9 +225,9 @@ void __hyp_text __vgic_v3_save_state(struct kvm_vcpu *vcpu)
232225
void __hyp_text __vgic_v3_restore_state(struct kvm_vcpu *vcpu)
233226
{
234227
struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3;
228+
u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
235229
u64 val;
236-
u32 max_lr_idx, nr_pri_bits;
237-
u16 live_lrs = 0;
230+
u32 nr_pri_bits;
238231
int i;
239232

240233
/*
@@ -251,15 +244,9 @@ void __hyp_text __vgic_v3_restore_state(struct kvm_vcpu *vcpu)
251244
}
252245

253246
val = read_gicreg(ICH_VTR_EL2);
254-
max_lr_idx = vtr_to_max_lr_idx(val);
255247
nr_pri_bits = vtr_to_nr_pri_bits(val);
256248

257-
for (i = 0; i <= max_lr_idx; i++) {
258-
if (cpu_if->vgic_lr[i] & ICH_LR_STATE)
259-
live_lrs |= (1 << i);
260-
}
261-
262-
if (live_lrs) {
249+
if (used_lrs) {
263250
write_gicreg(cpu_if->vgic_hcr, ICH_HCR_EL2);
264251

265252
switch (nr_pri_bits) {
@@ -282,12 +269,8 @@ void __hyp_text __vgic_v3_restore_state(struct kvm_vcpu *vcpu)
282269
write_gicreg(cpu_if->vgic_ap1r[0], ICH_AP1R0_EL2);
283270
}
284271

285-
for (i = 0; i <= max_lr_idx; i++) {
286-
if (!(live_lrs & (1 << i)))
287-
continue;
288-
272+
for (i = 0; i < used_lrs; i++)
289273
__gic_v3_set_lr(cpu_if->vgic_lr[i], i);
290-
}
291274
}
292275

293276
/*
@@ -299,7 +282,6 @@ void __hyp_text __vgic_v3_restore_state(struct kvm_vcpu *vcpu)
299282
isb();
300283
dsb(sy);
301284
}
302-
vcpu->arch.vgic_cpu.live_lrs = live_lrs;
303285

304286
/*
305287
* Prevent the guest from touching the GIC system registers if

0 commit comments

Comments
 (0)