From: Xuheng Li vgic_irq only runs with the guest's BPR at its reset value. Add a test that takes three SGIs at priorities 0x78, 0x70 and 0x40, first with ICC_BPR1_EL1 = 7, where none of them can preempt another, then with ICC_BPR1_EL1 = 4, where only 0x40 can preempt the other two. After each acknowledge and EOI, check ICC_AP1R0_EL1, ICC_RPR_EL1 and the active state. This mostly matters when KVM emulates the GIC CPU interface (on ThunderX, on Apple M1/M2, or with kvm-arm.vgic_v3_group1_trap=1), where the previous patch fixes these values. QEMU's GICv3 model has the same bug (since v2.9, still in v10.2), so under QEMU the test also fails without the trap. Signed-off-by: Xuheng Li --- tools/testing/selftests/kvm/arm64/vgic_irq.c | 89 ++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tools/testing/selftests/kvm/arm64/vgic_irq.c b/tools/testing/selftests/kvm/arm64/vgic_irq.c index 3cb85932ee85..2bcb7b5c2164 100644 --- a/tools/testing/selftests/kvm/arm64/vgic_irq.c +++ b/tools/testing/selftests/kvm/arm64/vgic_irq.c @@ -488,6 +488,93 @@ static void test_restore_active(struct test_args *args, struct kvm_inject_desc * guest_restore_active(args, MIN_SPI, 31, f->cmd); } +static u64 gic_read_rpr(void) +{ + return read_sysreg_s(SYS_ICC_RPR_EL1); +} + +static void gic_write_bpr1(u64 val) +{ + write_sysreg_s(val, SYS_ICC_BPR1_EL1); + isb(); +} + +static void bpr_ack(u32 intid, u64 ap1r0, u64 rpr) +{ + GUEST_ASSERT_EQ(wait_for_and_activate_irq(), intid); + GUEST_ASSERT_EQ(gic_read_ap1r0(), ap1r0); + GUEST_ASSERT_EQ(gic_read_rpr(), rpr); +} + +static void bpr_eoi(struct test_args *args, u32 intid, u64 ap1r0, u64 rpr) +{ + gic_set_eoi(intid); + if (args->eoi_split) + gic_set_dir(intid); + + GUEST_ASSERT(!gic_irq_get_active(intid)); + GUEST_ASSERT_EQ(gic_read_ap1r0(), ap1r0); + GUEST_ASSERT_EQ(gic_read_rpr(), rpr); +} + +/* + * Test acknowledge, EOI and preemption with non-default binary points. + * Acknowledging an interrupt of priority p always sets bit (p >> n) of the + * active priority registers, where n is the minimal BPR1, whatever the + * current BPR. The BPR only decides which interrupts can preempt. + */ +static void test_bpr(struct test_args *args) +{ + const u32 low = 1, mid = 2, high = 3; /* SGIs */ + const u64 idle = 0xff; /* RPR with nothing active */ + u64 bpr1 = read_sysreg_s(SYS_ICC_BPR1_EL1); + u64 n; + + /* Writing 0 sets BPR1 to its minimum. */ + gic_write_bpr1(0); + n = read_sysreg_s(SYS_ICC_BPR1_EL1); + if (n < 2) { + /* 0x78 >> n would be outside ICC_AP1R0_EL1. */ + gic_write_bpr1(bpr1); + return; + } + + gic_set_priority(low, 0x78); + gic_set_priority(mid, 0x70); + gic_set_priority(high, 0x40); + + local_irq_disable(); + + /* BPR1 = 7: all three have group priority 0x00, none preempts. */ + gic_write_bpr1(7); + gic_irq_set_pending(low); + bpr_ack(low, BIT(0x78 >> n), 0x78); + gic_irq_set_pending(high); + GUEST_ASSERT_IAR_EMPTY(); + bpr_eoi(args, low, 0, idle); + bpr_ack(high, BIT(0x40 >> n), 0x40); + bpr_eoi(args, high, 0, idle); + + /* BPR1 = 4: group priorities 0x70, 0x70 and 0x40, only high preempts. */ + gic_write_bpr1(4); + gic_irq_set_pending(low); + bpr_ack(low, BIT(0x78 >> n), 0x78); + gic_irq_set_pending(mid); + GUEST_ASSERT_IAR_EMPTY(); + gic_irq_set_pending(high); + bpr_ack(high, BIT(0x78 >> n) | BIT(0x40 >> n), 0x40); + bpr_eoi(args, high, BIT(0x78 >> n), 0x78); + GUEST_ASSERT_IAR_EMPTY(); + bpr_eoi(args, low, 0, idle); + bpr_ack(mid, BIT(0x70 >> n), 0x70); + bpr_eoi(args, mid, 0, idle); + + gic_write_bpr1(bpr1); + local_irq_enable(); + + reset_priorities(args); +} + static void guest_code(struct test_args *args) { u32 i, nr_irqs = args->nr_irqs; @@ -526,6 +613,8 @@ static void guest_code(struct test_args *args) for_each_supported_activate_fn(args, set_active_fns, f) test_restore_active(args, f); + test_bpr(args); + GUEST_DONE(); } -- 2.53.0