Skip to content

Commit faa1b46

Browse files
committed
arm/arm64: KVM: vgic: Improve handling of GICD_I{CS}PENDRn
Writes to GICD_ISPENDRn and GICD_ICPENDRn are currently not handled correctly for level-triggered interrupts. The spec states that for level-triggered interrupts, writes to the GICD_ISPENDRn activate the output of a flip-flop which is in turn or'ed with the actual input interrupt signal. Correspondingly, writes to GICD_ICPENDRn simply deactivates the output of that flip-flop, but does not (of course) affect the external input signal. Reads from GICC_IAR will also deactivate the flip-flop output. This requires us to track the state of the level-input separately from the state in the flip-flop. We therefore introduce two new variables on the distributor struct to track these two states. Astute readers may notice that this is introducing more state than required (because an OR of the two states gives you the pending state), but the remaining vgic code uses the pending bitmap for optimized operations to figure out, at the end of the day, if an interrupt is pending or not on the distributor side. Refactoring the code to consider the two state variables all the places where we currently access the precomputed pending value, did not look pretty. Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
1 parent cced50c commit faa1b46

2 files changed

Lines changed: 123 additions & 12 deletions

File tree

‎include/kvm/arm_vgic.h‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,23 @@ struct vgic_dist {
140140
/* Interrupt enabled (one bit per IRQ) */
141141
struct vgic_bitmap irq_enabled;
142142

143-
/* Interrupt state is pending on the distributor */
143+
/* Level-triggered interrupt external input is asserted */
144+
struct vgic_bitmap irq_level;
145+
146+
/*
147+
* Interrupt state is pending on the distributor
148+
*/
144149
struct vgic_bitmap irq_pending;
145150

151+
/*
152+
* Tracks writes to GICD_ISPENDRn and GICD_ICPENDRn for level-triggered
153+
* interrupts. Essentially holds the state of the flip-flop in
154+
* Figure 4-10 on page 4-101 in ARM IHI 0048B.b.
155+
* Once set, it is only cleared for level-triggered interrupts on
156+
* guest ACKs (when we queue it) or writes to GICD_ICPENDRn.
157+
*/
158+
struct vgic_bitmap irq_soft_pend;
159+
146160
/* Level-triggered interrupt queued on VCPU interface */
147161
struct vgic_bitmap irq_queued;
148162

‎virt/kvm/arm/vgic.c‎

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
* - When the interrupt is EOIed, the maintenance interrupt fires,
6868
* and clears the corresponding bit in irq_queued. This allows the
6969
* interrupt line to be sampled again.
70+
* - Note that level-triggered interrupts can also be set to pending from
71+
* writes to GICD_ISPENDRn and lowering the external input line does not
72+
* cause the interrupt to become inactive in such a situation.
73+
* Conversely, writes to GICD_ICPENDRn do not cause the interrupt to become
74+
* inactive as long as the external input line is held high.
7075
*/
7176

7277
#define VGIC_ADDR_UNDEF (-1)
@@ -217,6 +222,41 @@ static void vgic_irq_clear_queued(struct kvm_vcpu *vcpu, int irq)
217222
vgic_bitmap_set_irq_val(&dist->irq_queued, vcpu->vcpu_id, irq, 0);
218223
}
219224

225+
static int vgic_dist_irq_get_level(struct kvm_vcpu *vcpu, int irq)
226+
{
227+
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
228+
229+
return vgic_bitmap_get_irq_val(&dist->irq_level, vcpu->vcpu_id, irq);
230+
}
231+
232+
static void vgic_dist_irq_set_level(struct kvm_vcpu *vcpu, int irq)
233+
{
234+
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
235+
236+
vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 1);
237+
}
238+
239+
static void vgic_dist_irq_clear_level(struct kvm_vcpu *vcpu, int irq)
240+
{
241+
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
242+
243+
vgic_bitmap_set_irq_val(&dist->irq_level, vcpu->vcpu_id, irq, 0);
244+
}
245+
246+
static int vgic_dist_irq_soft_pend(struct kvm_vcpu *vcpu, int irq)
247+
{
248+
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
249+
250+
return vgic_bitmap_get_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq);
251+
}
252+
253+
static void vgic_dist_irq_clear_soft_pend(struct kvm_vcpu *vcpu, int irq)
254+
{
255+
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
256+
257+
vgic_bitmap_set_irq_val(&dist->irq_soft_pend, vcpu->vcpu_id, irq, 0);
258+
}
259+
220260
static int vgic_dist_irq_is_pending(struct kvm_vcpu *vcpu, int irq)
221261
{
222262
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
@@ -414,11 +454,26 @@ static bool handle_mmio_set_pending_reg(struct kvm_vcpu *vcpu,
414454
struct kvm_exit_mmio *mmio,
415455
phys_addr_t offset)
416456
{
417-
u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_pending,
418-
vcpu->vcpu_id, offset);
457+
u32 *reg;
458+
u32 level_mask;
459+
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
460+
461+
reg = vgic_bitmap_get_reg(&dist->irq_cfg, vcpu->vcpu_id, offset);
462+
level_mask = (~(*reg));
463+
464+
/* Mark both level and edge triggered irqs as pending */
465+
reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
419466
vgic_reg_access(mmio, reg, offset,
420467
ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
468+
421469
if (mmio->is_write) {
470+
/* Set the soft-pending flag only for level-triggered irqs */
471+
reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
472+
vcpu->vcpu_id, offset);
473+
vgic_reg_access(mmio, reg, offset,
474+
ACCESS_READ_VALUE | ACCESS_WRITE_SETBIT);
475+
*reg &= level_mask;
476+
422477
vgic_update_state(vcpu->kvm);
423478
return true;
424479
}
@@ -430,11 +485,27 @@ static bool handle_mmio_clear_pending_reg(struct kvm_vcpu *vcpu,
430485
struct kvm_exit_mmio *mmio,
431486
phys_addr_t offset)
432487
{
433-
u32 *reg = vgic_bitmap_get_reg(&vcpu->kvm->arch.vgic.irq_pending,
434-
vcpu->vcpu_id, offset);
488+
u32 *level_active;
489+
u32 *reg;
490+
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
491+
492+
reg = vgic_bitmap_get_reg(&dist->irq_pending, vcpu->vcpu_id, offset);
435493
vgic_reg_access(mmio, reg, offset,
436494
ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
437495
if (mmio->is_write) {
496+
/* Re-set level triggered level-active interrupts */
497+
level_active = vgic_bitmap_get_reg(&dist->irq_level,
498+
vcpu->vcpu_id, offset);
499+
reg = vgic_bitmap_get_reg(&dist->irq_pending,
500+
vcpu->vcpu_id, offset);
501+
*reg |= *level_active;
502+
503+
/* Clear soft-pending flags */
504+
reg = vgic_bitmap_get_reg(&dist->irq_soft_pend,
505+
vcpu->vcpu_id, offset);
506+
vgic_reg_access(mmio, reg, offset,
507+
ACCESS_READ_VALUE | ACCESS_WRITE_CLEARBIT);
508+
438509
vgic_update_state(vcpu->kvm);
439510
return true;
440511
}
@@ -1268,17 +1339,32 @@ static bool vgic_process_maintenance(struct kvm_vcpu *vcpu)
12681339

12691340
for_each_set_bit(lr, eisr_ptr, vgic->nr_lr) {
12701341
struct vgic_lr vlr = vgic_get_lr(vcpu, lr);
1342+
WARN_ON(vgic_irq_is_edge(vcpu, vlr.irq));
12711343

12721344
vgic_irq_clear_queued(vcpu, vlr.irq);
12731345
WARN_ON(vlr.state & LR_STATE_MASK);
12741346
vlr.state = 0;
12751347
vgic_set_lr(vcpu, lr, vlr);
12761348

1349+
/*
1350+
* If the IRQ was EOIed it was also ACKed and we we
1351+
* therefore assume we can clear the soft pending
1352+
* state (should it had been set) for this interrupt.
1353+
*
1354+
* Note: if the IRQ soft pending state was set after
1355+
* the IRQ was acked, it actually shouldn't be
1356+
* cleared, but we have no way of knowing that unless
1357+
* we start trapping ACKs when the soft-pending state
1358+
* is set.
1359+
*/
1360+
vgic_dist_irq_clear_soft_pend(vcpu, vlr.irq);
1361+
12771362
/* Any additional pending interrupt? */
1278-
if (vgic_dist_irq_is_pending(vcpu, vlr.irq)) {
1363+
if (vgic_dist_irq_get_level(vcpu, vlr.irq)) {
12791364
vgic_cpu_irq_set(vcpu, vlr.irq);
12801365
level_pending = true;
12811366
} else {
1367+
vgic_dist_irq_clear_pending(vcpu, vlr.irq);
12821368
vgic_cpu_irq_clear(vcpu, vlr.irq);
12831369
}
12841370

@@ -1384,17 +1470,19 @@ static void vgic_kick_vcpus(struct kvm *kvm)
13841470
static int vgic_validate_injection(struct kvm_vcpu *vcpu, int irq, int level)
13851471
{
13861472
int edge_triggered = vgic_irq_is_edge(vcpu, irq);
1387-
int state = vgic_dist_irq_is_pending(vcpu, irq);
13881473

13891474
/*
13901475
* Only inject an interrupt if:
13911476
* - edge triggered and we have a rising edge
13921477
* - level triggered and we change level
13931478
*/
1394-
if (edge_triggered)
1479+
if (edge_triggered) {
1480+
int state = vgic_dist_irq_is_pending(vcpu, irq);
13951481
return level > state;
1396-
else
1482+
} else {
1483+
int state = vgic_dist_irq_get_level(vcpu, irq);
13971484
return level != state;
1485+
}
13981486
}
13991487

14001488
static bool vgic_update_irq_pending(struct kvm *kvm, int cpuid,
@@ -1424,10 +1512,19 @@ static bool vgic_update_irq_pending(struct kvm *kvm, int cpuid,
14241512

14251513
kvm_debug("Inject IRQ%d level %d CPU%d\n", irq_num, level, cpuid);
14261514

1427-
if (level)
1515+
if (level) {
1516+
if (level_triggered)
1517+
vgic_dist_irq_set_level(vcpu, irq_num);
14281518
vgic_dist_irq_set_pending(vcpu, irq_num);
1429-
else
1430-
vgic_dist_irq_clear_pending(vcpu, irq_num);
1519+
} else {
1520+
if (level_triggered) {
1521+
vgic_dist_irq_clear_level(vcpu, irq_num);
1522+
if (!vgic_dist_irq_soft_pend(vcpu, irq_num))
1523+
vgic_dist_irq_clear_pending(vcpu, irq_num);
1524+
} else {
1525+
vgic_dist_irq_clear_pending(vcpu, irq_num);
1526+
}
1527+
}
14311528

14321529
enabled = vgic_irq_is_enabled(vcpu, irq_num);
14331530

0 commit comments

Comments
 (0)