2024-03-21 08:51:08

by Anup Patel

[permalink] [raw]
Subject: [PATCH 0/2] KVM RISC-V APLIC fixes

Few fixes for KVM RISC-V in-kernel APLIC emulation which were discovered
during Linux AIA driver patch reviews.

These patches can also be found in the riscv_kvm_aplic_fixes_v1
branch at: https://github.com/avpatel/linux.git

Anup Patel (2):
RISC-V: KVM: Fix APLIC setipnum_le/be write emulation
RISC-V: KVM: Fix APLIC in_clrip[x] read emulation

arch/riscv/kvm/aia_aplic.c | 37 +++++++++++++++++++++++++++++++------
1 file changed, 31 insertions(+), 6 deletions(-)

--
2.34.1



2024-03-21 08:51:28

by Anup Patel

[permalink] [raw]
Subject: [PATCH 1/2] RISC-V: KVM: Fix APLIC setipnum_le/be write emulation

The writes to setipnum_le/be register for APLIC in MSI-mode have special
consideration for level-triggered interrupts as-per the section "4.9.2
Special consideration for level-sensitive interrupt sources" of the RISC-V
AIA specification.

Particularly, the below text from the RISC-V AIA specification defines
the behaviour of writes to setipnum_le/be register for level-triggered
interrupts:

"A second option is for the interrupt service routine to write the
APLIC’s source identity number for the interrupt to the domain’s
setipnum register just before exiting. This will cause the interrupt’s
pending bit to be set to one again if the source is still asserting
an interrupt, but not if the source is not asserting an interrupt."

Fix setipnum_le/be write emulation for in-kernel APLIC by implementing
the above behaviour in aplic_write_pending() function.

Cc: [email protected]
Fixes: 74967aa208e2 ("RISC-V: KVM: Add in-kernel emulation of AIA APLIC")
Signed-off-by: Anup Patel <[email protected]>
---
arch/riscv/kvm/aia_aplic.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kvm/aia_aplic.c b/arch/riscv/kvm/aia_aplic.c
index 39e72aa016a4..5e842b92dc46 100644
--- a/arch/riscv/kvm/aia_aplic.c
+++ b/arch/riscv/kvm/aia_aplic.c
@@ -137,11 +137,21 @@ static void aplic_write_pending(struct aplic *aplic, u32 irq, bool pending)
raw_spin_lock_irqsave(&irqd->lock, flags);

sm = irqd->sourcecfg & APLIC_SOURCECFG_SM_MASK;
- if (!pending &&
- ((sm == APLIC_SOURCECFG_SM_LEVEL_HIGH) ||
- (sm == APLIC_SOURCECFG_SM_LEVEL_LOW)))
+ if (sm == APLIC_SOURCECFG_SM_INACTIVE)
goto skip_write_pending;

+ if (sm == APLIC_SOURCECFG_SM_LEVEL_HIGH ||
+ sm == APLIC_SOURCECFG_SM_LEVEL_LOW) {
+ if (!pending)
+ goto skip_write_pending;
+ if ((irqd->state & APLIC_IRQ_STATE_INPUT) &&
+ sm == APLIC_SOURCECFG_SM_LEVEL_LOW)
+ goto skip_write_pending;
+ if (!(irqd->state & APLIC_IRQ_STATE_INPUT) &&
+ sm == APLIC_SOURCECFG_SM_LEVEL_HIGH)
+ goto skip_write_pending;
+ }
+
if (pending)
irqd->state |= APLIC_IRQ_STATE_PENDING;
else
--
2.34.1


2024-03-21 08:51:38

by Anup Patel

[permalink] [raw]
Subject: [PATCH 2/2] RISC-V: KVM: Fix APLIC in_clrip[x] read emulation

The reads to APLIC in_clrip[x] registers returns rectified input values
of the interrupt sources.

A rectified input value of an interrupt source is defined by the section
"4.5.2 Source configurations (sourcecfg[1]–sourcecfg[1023])" of the
RISC-V AIA specification as:

rectified input value = (incoming wire value) XOR (source is inverted)

Update the riscv_aplic_input() implementation to match the above.

Fixes: 74967aa208e2 ("RISC-V: KVM: Add in-kernel emulation of AIA APLIC")
Signed-off-by: Anup Patel <[email protected]>
---
arch/riscv/kvm/aia_aplic.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kvm/aia_aplic.c b/arch/riscv/kvm/aia_aplic.c
index 5e842b92dc46..b467ba5ed910 100644
--- a/arch/riscv/kvm/aia_aplic.c
+++ b/arch/riscv/kvm/aia_aplic.c
@@ -197,16 +197,31 @@ static void aplic_write_enabled(struct aplic *aplic, u32 irq, bool enabled)

static bool aplic_read_input(struct aplic *aplic, u32 irq)
{
- bool ret;
- unsigned long flags;
+ u32 sourcecfg, sm, raw_input, irq_inverted;
struct aplic_irq *irqd;
+ unsigned long flags;
+ bool ret = false;

if (!irq || aplic->nr_irqs <= irq)
return false;
irqd = &aplic->irqs[irq];

raw_spin_lock_irqsave(&irqd->lock, flags);
- ret = (irqd->state & APLIC_IRQ_STATE_INPUT) ? true : false;
+
+ sourcecfg = irqd->sourcecfg;
+ if (sourcecfg & APLIC_SOURCECFG_D)
+ goto skip;
+
+ sm = sourcecfg & APLIC_SOURCECFG_SM_MASK;
+ if (sm == APLIC_SOURCECFG_SM_INACTIVE)
+ goto skip;
+
+ raw_input = (irqd->state & APLIC_IRQ_STATE_INPUT) ? 1 : 0;
+ irq_inverted = (sm == APLIC_SOURCECFG_SM_LEVEL_LOW ||
+ sm == APLIC_SOURCECFG_SM_EDGE_FALL) ? 1 : 0;
+ ret = !!(raw_input ^ irq_inverted);
+
+skip:
raw_spin_unlock_irqrestore(&irqd->lock, flags);

return ret;
--
2.34.1


2024-03-22 11:41:56

by Bing Fan

[permalink] [raw]
Subject: Re: [PATCH 0/2] KVM RISC-V APLIC fixes


Hi,


As you mentioned as below, riscv's aia patch in
https://github.com/avpatel/linux.git

Why is this series of patches not merged into upstream?


在 2024/3/21 16:50, Anup Patel 写道:
> Few fixes for KVM RISC-V in-kernel APLIC emulation which were discovered
> during Linux AIA driver patch reviews.
>
> These patches can also be found in the riscv_kvm_aplic_fixes_v1
> branch at: https://github.com/avpatel/linux.git
>
> Anup Patel (2):
> RISC-V: KVM: Fix APLIC setipnum_le/be write emulation
> RISC-V: KVM: Fix APLIC in_clrip[x] read emulation
>
> arch/riscv/kvm/aia_aplic.c | 37 +++++++++++++++++++++++++++++++------
> 1 file changed, 31 insertions(+), 6 deletions(-)
>

2024-03-22 15:33:02

by Anup Patel

[permalink] [raw]
Subject: Re: [PATCH 0/2] KVM RISC-V APLIC fixes

On Fri, Mar 22, 2024 at 5:05 PM Bing Fan <[email protected]> wrote:
>
>
> Hi,
>
>
> As you mentioned as below, riscv's aia patch in
> https://github.com/avpatel/linux.git
>
> Why is this series of patches not merged into upstream?

This will be sent as part of Linux-6.9-rcX fixes (after 1-2 weeks).

Regards,
Anup

>
>
> 在 2024/3/21 16:50, Anup Patel 写道:
> > Few fixes for KVM RISC-V in-kernel APLIC emulation which were discovered
> > during Linux AIA driver patch reviews.
> >
> > These patches can also be found in the riscv_kvm_aplic_fixes_v1
> > branch at: https://github.com/avpatel/linux.git
> >
> > Anup Patel (2):
> > RISC-V: KVM: Fix APLIC setipnum_le/be write emulation
> > RISC-V: KVM: Fix APLIC in_clrip[x] read emulation
> >
> > arch/riscv/kvm/aia_aplic.c | 37 +++++++++++++++++++++++++++++++------
> > 1 file changed, 31 insertions(+), 6 deletions(-)
> >

2024-03-26 04:08:00

by Anup Patel

[permalink] [raw]
Subject: Re: [PATCH 0/2] KVM RISC-V APLIC fixes

On Thu, Mar 21, 2024 at 2:20 PM Anup Patel <[email protected]> wrote:
>
> Few fixes for KVM RISC-V in-kernel APLIC emulation which were discovered
> during Linux AIA driver patch reviews.
>
> These patches can also be found in the riscv_kvm_aplic_fixes_v1
> branch at: https://github.com/avpatel/linux.git
>
> Anup Patel (2):
> RISC-V: KVM: Fix APLIC setipnum_le/be write emulation
> RISC-V: KVM: Fix APLIC in_clrip[x] read emulation

Queued this series for Linux-6.9 fixes.

Thanks,
Anup

>
> arch/riscv/kvm/aia_aplic.c | 37 +++++++++++++++++++++++++++++++------
> 1 file changed, 31 insertions(+), 6 deletions(-)
>
> --
> 2.34.1
>