[PATCH v2] hw/intc/aplic: Fix msicfgaddrH in split mode

BillXiang posted 1 patch 2 weeks, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261104025308.1920474-1-xiangwencheng@lanxincomputing.com
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
There is a newer version of this series
hw/intc/riscv_aplic.c         | 13 +++++++++----
hw/riscv/aia.c                |  6 +++++-
include/hw/intc/riscv_aplic.h |  3 ++-
3 files changed, 16 insertions(+), 6 deletions(-)
[PATCH v2] hw/intc/aplic: Fix msicfgaddrH in split mode
Posted by BillXiang 2 weeks, 5 days ago
Add hart_bits and guest_bits to msicfgaddrH in KVM irqchip
split mode.
Also remove the redundant left shift when loading
kvm_msicfgaddrH in riscv_aplic_msi_send().

Rationale:
static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
                                  uint32_t hart_idx, uint32_t guest_idx,
                                  uint32_t eiid)
{
     uint64_t addr;
     MemTxResult result;
     RISCVAPLICState *aplic_m;
     uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH;

     ...

     if (aplic->kvm_splitmode) {
         msicfgaddr = aplic->kvm_msicfgaddr;
1.        msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
     }
     ...

     lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) &
             APLIC_xMSICFGADDRH_LHXS_MASK;
     lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) &
             APLIC_xMSICFGADDRH_LHXW_MASK;
     hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) &
             APLIC_xMSICFGADDRH_HHXS_MASK;
     hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) &
             APLIC_xMSICFGADDRH_HHXW_MASK;
     ...

     group_idx = hart_idx >> lhxw;

     addr = msicfgaddr;
2.    addr |= ((uint64_t)(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK))
<< 32;
     addr |= ((uint64_t)(group_idx &
APLIC_xMSICFGADDR_PPN_HHX_MASK(hhxw))) <<
              APLIC_xMSICFGADDR_PPN_HHX_SHIFT(hhxs);
3.    addr |= ((uint64_t)(hart_idx &
APLIC_xMSICFGADDR_PPN_LHX_MASK(lhxw))) <<
              APLIC_xMSICFGADDR_PPN_LHX_SHIFT(lhxs);
4.    addr |= (uint64_t)(guest_idx & APLIC_xMSICFGADDR_PPN_HART(lhxs));
     addr <<= APLIC_xMSICFGADDR_PPN_SHIFT;
     ...
}

Since msicfgaddrH is left-shifted at (2), the left shift at (1) is
redundant.

At (3) and (4), hart_idx and guest_idx are masked/shifted using lhxw and
lhxs, which are extracted from the LHXW/LHXS fields of msicfgaddrH.
However, riscv_aplic_set_kvm_msicfgaddr() did not program the LHXW/LHXS
fields into kvm_msicfgaddrH, so lhxs/lhxw always read as zero, and the
hart/guest idxs were never encoded into the MSI address. Fix this by
programming hart_bits (LHXW) and guest_bits (LHXS) into kvm_msicfgaddrH
in riscv_aplic_set_kvm_msicfgaddr().

Signed-off-by: BillXiang <xiangwencheng@lanxincomputing.com>
---
Changes in v2:
- Add rationale.
---
 hw/intc/riscv_aplic.c         | 13 +++++++++----
 hw/riscv/aia.c                |  6 +++++-
 include/hw/intc/riscv_aplic.h |  3 ++-
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/hw/intc/riscv_aplic.c b/hw/intc/riscv_aplic.c
index 03a0852b8b..2b618e73df 100644
--- a/hw/intc/riscv_aplic.c
+++ b/hw/intc/riscv_aplic.c
@@ -182,7 +182,8 @@ bool riscv_use_emulated_aplic(bool msimode)
     return kvm_kernel_irqchip_split();
 }
 
-void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
+void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
+                                    uint32_t hart_bits, uint32_t guest_bits)
 {
     if (!kvm_enabled()) {
         return;
@@ -191,8 +192,12 @@ void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
     if (riscv_use_emulated_aplic(aplic->msimode)) {
         addr >>= APLIC_xMSICFGADDR_PPN_SHIFT;
         aplic->kvm_msicfgaddr = extract64(addr, 0, 32);
-        aplic->kvm_msicfgaddrH = extract64(addr, 32, 32) &
-                                 APLIC_MMSICFGADDRH_VALID_MASK;
+        aplic->kvm_msicfgaddrH =
+            ((hart_bits & APLIC_xMSICFGADDRH_LHXW_MASK) <<
+             APLIC_xMSICFGADDRH_LHXW_SHIFT) |
+            ((guest_bits & APLIC_xMSICFGADDRH_LHXS_MASK) <<
+             APLIC_xMSICFGADDRH_LHXS_SHIFT) |
+            (extract64(addr, 32, 32) & APLIC_xMSICFGADDRH_BAPPN_MASK);
     }
 }
 
@@ -404,7 +409,7 @@ static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
 
     if (aplic->kvm_splitmode) {
         msicfgaddr = aplic->kvm_msicfgaddr;
-        msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
+        msicfgaddrH = aplic->kvm_msicfgaddrH;
     } else {
         msicfgaddr = aplic_m->mmsicfgaddr;
         msicfgaddrH = aplic_m->mmsicfgaddrH;
diff --git a/hw/riscv/aia.c b/hw/riscv/aia.c
index 56fc064533..bd608bd7a6 100644
--- a/hw/riscv/aia.c
+++ b/hw/riscv/aia.c
@@ -38,6 +38,7 @@ DeviceState *riscv_create_aia(MemoryRegion *container,
 {
     int i;
     hwaddr addr = 0;
+    uint32_t hart_bits, guest_bits;
     DeviceState *aplic_s_dev = NULL;
     DeviceState *aplic_m_dev = NULL;
 
@@ -93,7 +94,10 @@ DeviceState *riscv_create_aia(MemoryRegion *container,
                                  msimode, false, aplic_m_dev);
 
     if (kvm_enabled() && msimode) {
-        riscv_aplic_set_kvm_msicfgaddr(RISCV_APLIC(aplic_s_dev), addr);
+        hart_bits = imsic_num_bits(hart_count);
+        guest_bits = imsic_num_bits(aia_guests + 1);
+        riscv_aplic_set_kvm_msicfgaddr(RISCV_APLIC(aplic_s_dev), addr,
+                                    hart_bits, guest_bits);
     }
 
     return kvm_enabled() ? aplic_s_dev : aplic_m_dev;
diff --git a/include/hw/intc/riscv_aplic.h b/include/hw/intc/riscv_aplic.h
index 62d2b33ade..d3ee2e537a 100644
--- a/include/hw/intc/riscv_aplic.h
+++ b/include/hw/intc/riscv_aplic.h
@@ -78,7 +78,8 @@ struct RISCVAPLICState {
 void riscv_aplic_add_child(DeviceState *parent, DeviceState *child);
 bool riscv_is_kvm_aia_aplic_imsic(bool msimode);
 bool riscv_use_emulated_aplic(bool msimode);
-void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr);
+void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
+                                    uint32_t hart_bits, uint32_t guest_bits);
 
 DeviceState *riscv_aplic_create(MemoryRegion *container,
     hwaddr addr, hwaddr size, uint32_t hartid_base, uint32_t num_harts,
-- 
2.53.0
Re: [PATCH v2] hw/intc/aplic: Fix msicfgaddrH in split mode
Posted by Nutty.Liu 1 week, 5 days ago
On 11/4/2026 10:53 AM, BillXiang wrote:
> Add hart_bits and guest_bits to msicfgaddrH in KVM irqchip
> split mode.
> Also remove the redundant left shift when loading
> kvm_msicfgaddrH in riscv_aplic_msi_send().
>
> Rationale:
> static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
>                                    uint32_t hart_idx, uint32_t guest_idx,
>                                    uint32_t eiid)
> {
>       uint64_t addr;
>       MemTxResult result;
>       RISCVAPLICState *aplic_m;
>       uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH;
>
>       ...
>
>       if (aplic->kvm_splitmode) {
>           msicfgaddr = aplic->kvm_msicfgaddr;
> 1.        msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
>       }
>       ...
>
>       lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) &
>               APLIC_xMSICFGADDRH_LHXS_MASK;
>       lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) &
>               APLIC_xMSICFGADDRH_LHXW_MASK;
>       hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) &
>               APLIC_xMSICFGADDRH_HHXS_MASK;
>       hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) &
>               APLIC_xMSICFGADDRH_HHXW_MASK;
>       ...
>
>       group_idx = hart_idx >> lhxw;
>
>       addr = msicfgaddr;
> 2.    addr |= ((uint64_t)(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK))
> << 32;
>       addr |= ((uint64_t)(group_idx &
> APLIC_xMSICFGADDR_PPN_HHX_MASK(hhxw))) <<
>                APLIC_xMSICFGADDR_PPN_HHX_SHIFT(hhxs);
> 3.    addr |= ((uint64_t)(hart_idx &
> APLIC_xMSICFGADDR_PPN_LHX_MASK(lhxw))) <<
>                APLIC_xMSICFGADDR_PPN_LHX_SHIFT(lhxs);
> 4.    addr |= (uint64_t)(guest_idx & APLIC_xMSICFGADDR_PPN_HART(lhxs));
>       addr <<= APLIC_xMSICFGADDR_PPN_SHIFT;
>       ...
> }
>
> Since msicfgaddrH is left-shifted at (2), the left shift at (1) is
> redundant.
>
> At (3) and (4), hart_idx and guest_idx are masked/shifted using lhxw and
> lhxs, which are extracted from the LHXW/LHXS fields of msicfgaddrH.
> However, riscv_aplic_set_kvm_msicfgaddr() did not program the LHXW/LHXS
> fields into kvm_msicfgaddrH, so lhxs/lhxw always read as zero, and the
> hart/guest idxs were never encoded into the MSI address. Fix this by
> programming hart_bits (LHXW) and guest_bits (LHXS) into kvm_msicfgaddrH
> in riscv_aplic_set_kvm_msicfgaddr().
>
> Signed-off-by: BillXiang <xiangwencheng@lanxincomputing.com>
> ---
> Changes in v2:
> - Add rationale.
> ---
>   hw/intc/riscv_aplic.c         | 13 +++++++++----
>   hw/riscv/aia.c                |  6 +++++-
>   include/hw/intc/riscv_aplic.h |  3 ++-
>   3 files changed, 16 insertions(+), 6 deletions(-)
>
> diff --git a/hw/intc/riscv_aplic.c b/hw/intc/riscv_aplic.c
> index 03a0852b8b..2b618e73df 100644
> --- a/hw/intc/riscv_aplic.c
> +++ b/hw/intc/riscv_aplic.c
> @@ -182,7 +182,8 @@ bool riscv_use_emulated_aplic(bool msimode)
>       return kvm_kernel_irqchip_split();
>   }
>   
> -void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
> +void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
> +                                    uint32_t hart_bits, uint32_t guest_bits)
>   {
>       if (!kvm_enabled()) {
>           return;
> @@ -191,8 +192,12 @@ void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
>       if (riscv_use_emulated_aplic(aplic->msimode)) {
>           addr >>= APLIC_xMSICFGADDR_PPN_SHIFT;
>           aplic->kvm_msicfgaddr = extract64(addr, 0, 32);
> -        aplic->kvm_msicfgaddrH = extract64(addr, 32, 32) &
> -                                 APLIC_MMSICFGADDRH_VALID_MASK;
Is it necessary to perform boundary checks on hart_bits/guest_bits ?
Just like:
+    assert(hart_bits  <= APLIC_xMSICFGADDRH_LHXW_MASK);
+    assert(guest_bits <= APLIC_xMSICFGADDRH_LHXS_MASK);

Otherwise,
Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>

Thanks,
Nutty
> +        aplic->kvm_msicfgaddrH =
> +            ((hart_bits & APLIC_xMSICFGADDRH_LHXW_MASK) <<
> +             APLIC_xMSICFGADDRH_LHXW_SHIFT) |
> +            ((guest_bits & APLIC_xMSICFGADDRH_LHXS_MASK) <<
> +             APLIC_xMSICFGADDRH_LHXS_SHIFT) |
> +            (extract64(addr, 32, 32) & APLIC_xMSICFGADDRH_BAPPN_MASK);
>       }
>   }
>   
> @@ -404,7 +409,7 @@ static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
>   
>       if (aplic->kvm_splitmode) {
>           msicfgaddr = aplic->kvm_msicfgaddr;
> -        msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
> +        msicfgaddrH = aplic->kvm_msicfgaddrH;
>       } else {
>           msicfgaddr = aplic_m->mmsicfgaddr;
>           msicfgaddrH = aplic_m->mmsicfgaddrH;
> diff --git a/hw/riscv/aia.c b/hw/riscv/aia.c
> index 56fc064533..bd608bd7a6 100644
> --- a/hw/riscv/aia.c
> +++ b/hw/riscv/aia.c
> @@ -38,6 +38,7 @@ DeviceState *riscv_create_aia(MemoryRegion *container,
>   {
>       int i;
>       hwaddr addr = 0;
> +    uint32_t hart_bits, guest_bits;
>       DeviceState *aplic_s_dev = NULL;
>       DeviceState *aplic_m_dev = NULL;
>   
> @@ -93,7 +94,10 @@ DeviceState *riscv_create_aia(MemoryRegion *container,
>                                    msimode, false, aplic_m_dev);
>   
>       if (kvm_enabled() && msimode) {
> -        riscv_aplic_set_kvm_msicfgaddr(RISCV_APLIC(aplic_s_dev), addr);
> +        hart_bits = imsic_num_bits(hart_count);
> +        guest_bits = imsic_num_bits(aia_guests + 1);
> +        riscv_aplic_set_kvm_msicfgaddr(RISCV_APLIC(aplic_s_dev), addr,
> +                                    hart_bits, guest_bits);
>       }
>   
>       return kvm_enabled() ? aplic_s_dev : aplic_m_dev;
> diff --git a/include/hw/intc/riscv_aplic.h b/include/hw/intc/riscv_aplic.h
> index 62d2b33ade..d3ee2e537a 100644
> --- a/include/hw/intc/riscv_aplic.h
> +++ b/include/hw/intc/riscv_aplic.h
> @@ -78,7 +78,8 @@ struct RISCVAPLICState {
>   void riscv_aplic_add_child(DeviceState *parent, DeviceState *child);
>   bool riscv_is_kvm_aia_aplic_imsic(bool msimode);
>   bool riscv_use_emulated_aplic(bool msimode);
> -void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr);
> +void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
> +                                    uint32_t hart_bits, uint32_t guest_bits);
>   
>   DeviceState *riscv_aplic_create(MemoryRegion *container,
>       hwaddr addr, hwaddr size, uint32_t hartid_base, uint32_t num_harts,

Re: [PATCH v2] hw/intc/aplic: Fix msicfgaddrH in split mode
Posted by BillXiang 1 week, 5 days ago
On 11/10/2026 7:49 PM, Nutty.Liu wrote:
> 
> On 11/4/2026 10:53 AM, BillXiang wrote:
>> Add hart_bits and guest_bits to msicfgaddrH in KVM irqchip
>> split mode.
>> Also remove the redundant left shift when loading
>> kvm_msicfgaddrH in riscv_aplic_msi_send().
>>
>> Rationale:
>> static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
>>                                    uint32_t hart_idx, uint32_t guest_idx,
>>                                    uint32_t eiid)
>> {
>>       uint64_t addr;
>>       MemTxResult result;
>>       RISCVAPLICState *aplic_m;
>>       uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, 
>> msicfgaddrH;
>>
>>       ...
>>
>>       if (aplic->kvm_splitmode) {
>>           msicfgaddr = aplic->kvm_msicfgaddr;
>> 1.        msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
>>       }
>>       ...
>>
>>       lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) &
>>               APLIC_xMSICFGADDRH_LHXS_MASK;
>>       lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) &
>>               APLIC_xMSICFGADDRH_LHXW_MASK;
>>       hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) &
>>               APLIC_xMSICFGADDRH_HHXS_MASK;
>>       hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) &
>>               APLIC_xMSICFGADDRH_HHXW_MASK;
>>       ...
>>
>>       group_idx = hart_idx >> lhxw;
>>
>>       addr = msicfgaddr;
>> 2.    addr |= ((uint64_t)(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK))
>> << 32;
>>       addr |= ((uint64_t)(group_idx &
>> APLIC_xMSICFGADDR_PPN_HHX_MASK(hhxw))) <<
>>                APLIC_xMSICFGADDR_PPN_HHX_SHIFT(hhxs);
>> 3.    addr |= ((uint64_t)(hart_idx &
>> APLIC_xMSICFGADDR_PPN_LHX_MASK(lhxw))) <<
>>                APLIC_xMSICFGADDR_PPN_LHX_SHIFT(lhxs);
>> 4.    addr |= (uint64_t)(guest_idx & APLIC_xMSICFGADDR_PPN_HART(lhxs));
>>       addr <<= APLIC_xMSICFGADDR_PPN_SHIFT;
>>       ...
>> }
>>
>> Since msicfgaddrH is left-shifted at (2), the left shift at (1) is
>> redundant.
>>
>> At (3) and (4), hart_idx and guest_idx are masked/shifted using lhxw and
>> lhxs, which are extracted from the LHXW/LHXS fields of msicfgaddrH.
>> However, riscv_aplic_set_kvm_msicfgaddr() did not program the LHXW/LHXS
>> fields into kvm_msicfgaddrH, so lhxs/lhxw always read as zero, and the
>> hart/guest idxs were never encoded into the MSI address. Fix this by
>> programming hart_bits (LHXW) and guest_bits (LHXS) into kvm_msicfgaddrH
>> in riscv_aplic_set_kvm_msicfgaddr().
>>
>> Signed-off-by: BillXiang <xiangwencheng@lanxincomputing.com>
>> ---
>> Changes in v2:
>> - Add rationale.
>> ---
>>   hw/intc/riscv_aplic.c         | 13 +++++++++----
>>   hw/riscv/aia.c                |  6 +++++-
>>   include/hw/intc/riscv_aplic.h |  3 ++-
>>   3 files changed, 16 insertions(+), 6 deletions(-)
>>
>> diff --git a/hw/intc/riscv_aplic.c b/hw/intc/riscv_aplic.c
>> index 03a0852b8b..2b618e73df 100644
>> --- a/hw/intc/riscv_aplic.c
>> +++ b/hw/intc/riscv_aplic.c
>> @@ -182,7 +182,8 @@ bool riscv_use_emulated_aplic(bool msimode)
>>       return kvm_kernel_irqchip_split();
>>   }
>> -void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
>> +void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
>> +                                    uint32_t hart_bits, uint32_t 
>> guest_bits)
>>   {
>>       if (!kvm_enabled()) {
>>           return;
>> @@ -191,8 +192,12 @@ void 
>> riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
>>       if (riscv_use_emulated_aplic(aplic->msimode)) {
>>           addr >>= APLIC_xMSICFGADDR_PPN_SHIFT;
>>           aplic->kvm_msicfgaddr = extract64(addr, 0, 32);
>> -        aplic->kvm_msicfgaddrH = extract64(addr, 32, 32) &
>> -                                 APLIC_MMSICFGADDRH_VALID_MASK;
> Is it necessary to perform boundary checks on hart_bits/guest_bits ?
> Just like:
> +    assert(hart_bits  <= APLIC_xMSICFGADDRH_LHXW_MASK);
> +    assert(guest_bits <= APLIC_xMSICFGADDRH_LHXS_MASK);

Thank you for the review. I'll add it to v3.

Regards,
Bill

> 
> Otherwise,
> Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>
> 
> Thanks,
> Nutty
>> +        aplic->kvm_msicfgaddrH =
>> +            ((hart_bits & APLIC_xMSICFGADDRH_LHXW_MASK) <<
>> +             APLIC_xMSICFGADDRH_LHXW_SHIFT) |
>> +            ((guest_bits & APLIC_xMSICFGADDRH_LHXS_MASK) <<
>> +             APLIC_xMSICFGADDRH_LHXS_SHIFT) |
>> +            (extract64(addr, 32, 32) & APLIC_xMSICFGADDRH_BAPPN_MASK);
>>       }
>>   }
>> @@ -404,7 +409,7 @@ static void riscv_aplic_msi_send(RISCVAPLICState 
>> *aplic,
>>       if (aplic->kvm_splitmode) {
>>           msicfgaddr = aplic->kvm_msicfgaddr;
>> -        msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
>> +        msicfgaddrH = aplic->kvm_msicfgaddrH;
>>       } else {
>>           msicfgaddr = aplic_m->mmsicfgaddr;
>>           msicfgaddrH = aplic_m->mmsicfgaddrH;
>> diff --git a/hw/riscv/aia.c b/hw/riscv/aia.c
>> index 56fc064533..bd608bd7a6 100644
>> --- a/hw/riscv/aia.c
>> +++ b/hw/riscv/aia.c
>> @@ -38,6 +38,7 @@ DeviceState *riscv_create_aia(MemoryRegion *container,
>>   {
>>       int i;
>>       hwaddr addr = 0;
>> +    uint32_t hart_bits, guest_bits;
>>       DeviceState *aplic_s_dev = NULL;
>>       DeviceState *aplic_m_dev = NULL;
>> @@ -93,7 +94,10 @@ DeviceState *riscv_create_aia(MemoryRegion *container,
>>                                    msimode, false, aplic_m_dev);
>>       if (kvm_enabled() && msimode) {
>> -        riscv_aplic_set_kvm_msicfgaddr(RISCV_APLIC(aplic_s_dev), addr);
>> +        hart_bits = imsic_num_bits(hart_count);
>> +        guest_bits = imsic_num_bits(aia_guests + 1);
>> +        riscv_aplic_set_kvm_msicfgaddr(RISCV_APLIC(aplic_s_dev), addr,
>> +                                    hart_bits, guest_bits);
>>       }
>>       return kvm_enabled() ? aplic_s_dev : aplic_m_dev;
>> diff --git a/include/hw/intc/riscv_aplic.h b/include/hw/intc/ 
>> riscv_aplic.h
>> index 62d2b33ade..d3ee2e537a 100644
>> --- a/include/hw/intc/riscv_aplic.h
>> +++ b/include/hw/intc/riscv_aplic.h
>> @@ -78,7 +78,8 @@ struct RISCVAPLICState {
>>   void riscv_aplic_add_child(DeviceState *parent, DeviceState *child);
>>   bool riscv_is_kvm_aia_aplic_imsic(bool msimode);
>>   bool riscv_use_emulated_aplic(bool msimode);
>> -void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr 
>> addr);
>> +void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
>> +                                    uint32_t hart_bits, uint32_t 
>> guest_bits);
>>   DeviceState *riscv_aplic_create(MemoryRegion *container,
>>       hwaddr addr, hwaddr size, uint32_t hartid_base, uint32_t num_harts,
Re: [PATCH v2] hw/intc/aplic: Fix msicfgaddrH in split mode
Posted by Daniel Henrique Barboza 1 week, 6 days ago

On 11/3/2026 11:53 PM, BillXiang wrote:
> Add hart_bits and guest_bits to msicfgaddrH in KVM irqchip
> split mode.
> Also remove the redundant left shift when loading
> kvm_msicfgaddrH in riscv_aplic_msi_send().
> 
> Rationale:
> static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
>                                    uint32_t hart_idx, uint32_t guest_idx,
>                                    uint32_t eiid)
> {
>       uint64_t addr;
>       MemTxResult result;
>       RISCVAPLICState *aplic_m;
>       uint32_t lhxs, lhxw, hhxs, hhxw, group_idx, msicfgaddr, msicfgaddrH;
> 
>       ...
> 
>       if (aplic->kvm_splitmode) {
>           msicfgaddr = aplic->kvm_msicfgaddr;
> 1.        msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
>       }
>       ...
> 
>       lhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXS_SHIFT) &
>               APLIC_xMSICFGADDRH_LHXS_MASK;
>       lhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_LHXW_SHIFT) &
>               APLIC_xMSICFGADDRH_LHXW_MASK;
>       hhxs = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXS_SHIFT) &
>               APLIC_xMSICFGADDRH_HHXS_MASK;
>       hhxw = (msicfgaddrH >> APLIC_xMSICFGADDRH_HHXW_SHIFT) &
>               APLIC_xMSICFGADDRH_HHXW_MASK;
>       ...
> 
>       group_idx = hart_idx >> lhxw;
> 
>       addr = msicfgaddr;
> 2.    addr |= ((uint64_t)(msicfgaddrH & APLIC_xMSICFGADDRH_BAPPN_MASK))
> << 32;
>       addr |= ((uint64_t)(group_idx &
> APLIC_xMSICFGADDR_PPN_HHX_MASK(hhxw))) <<
>                APLIC_xMSICFGADDR_PPN_HHX_SHIFT(hhxs);
> 3.    addr |= ((uint64_t)(hart_idx &
> APLIC_xMSICFGADDR_PPN_LHX_MASK(lhxw))) <<
>                APLIC_xMSICFGADDR_PPN_LHX_SHIFT(lhxs);
> 4.    addr |= (uint64_t)(guest_idx & APLIC_xMSICFGADDR_PPN_HART(lhxs));
>       addr <<= APLIC_xMSICFGADDR_PPN_SHIFT;
>       ...
> }
> 
> Since msicfgaddrH is left-shifted at (2), the left shift at (1) is
> redundant.
> 
> At (3) and (4), hart_idx and guest_idx are masked/shifted using lhxw and
> lhxs, which are extracted from the LHXW/LHXS fields of msicfgaddrH.
> However, riscv_aplic_set_kvm_msicfgaddr() did not program the LHXW/LHXS
> fields into kvm_msicfgaddrH, so lhxs/lhxw always read as zero, and the
> hart/guest idxs were never encoded into the MSI address. Fix this by
> programming hart_bits (LHXW) and guest_bits (LHXS) into kvm_msicfgaddrH
> in riscv_aplic_set_kvm_msicfgaddr().
> 
> Signed-off-by: BillXiang <xiangwencheng@lanxincomputing.com>
> ---

Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>

> Changes in v2:
> - Add rationale.
> ---
>   hw/intc/riscv_aplic.c         | 13 +++++++++----
>   hw/riscv/aia.c                |  6 +++++-
>   include/hw/intc/riscv_aplic.h |  3 ++-
>   3 files changed, 16 insertions(+), 6 deletions(-)
> 
> diff --git a/hw/intc/riscv_aplic.c b/hw/intc/riscv_aplic.c
> index 03a0852b8b..2b618e73df 100644
> --- a/hw/intc/riscv_aplic.c
> +++ b/hw/intc/riscv_aplic.c
> @@ -182,7 +182,8 @@ bool riscv_use_emulated_aplic(bool msimode)
>       return kvm_kernel_irqchip_split();
>   }
>   
> -void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
> +void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
> +                                    uint32_t hart_bits, uint32_t guest_bits)
>   {
>       if (!kvm_enabled()) {
>           return;
> @@ -191,8 +192,12 @@ void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
>       if (riscv_use_emulated_aplic(aplic->msimode)) {
>           addr >>= APLIC_xMSICFGADDR_PPN_SHIFT;
>           aplic->kvm_msicfgaddr = extract64(addr, 0, 32);
> -        aplic->kvm_msicfgaddrH = extract64(addr, 32, 32) &
> -                                 APLIC_MMSICFGADDRH_VALID_MASK;
> +        aplic->kvm_msicfgaddrH =
> +            ((hart_bits & APLIC_xMSICFGADDRH_LHXW_MASK) <<
> +             APLIC_xMSICFGADDRH_LHXW_SHIFT) |
> +            ((guest_bits & APLIC_xMSICFGADDRH_LHXS_MASK) <<
> +             APLIC_xMSICFGADDRH_LHXS_SHIFT) |
> +            (extract64(addr, 32, 32) & APLIC_xMSICFGADDRH_BAPPN_MASK);
>       }
>   }
>   
> @@ -404,7 +409,7 @@ static void riscv_aplic_msi_send(RISCVAPLICState *aplic,
>   
>       if (aplic->kvm_splitmode) {
>           msicfgaddr = aplic->kvm_msicfgaddr;
> -        msicfgaddrH = ((uint64_t)aplic->kvm_msicfgaddrH << 32);
> +        msicfgaddrH = aplic->kvm_msicfgaddrH;
>       } else {
>           msicfgaddr = aplic_m->mmsicfgaddr;
>           msicfgaddrH = aplic_m->mmsicfgaddrH;
> diff --git a/hw/riscv/aia.c b/hw/riscv/aia.c
> index 56fc064533..bd608bd7a6 100644
> --- a/hw/riscv/aia.c
> +++ b/hw/riscv/aia.c
> @@ -38,6 +38,7 @@ DeviceState *riscv_create_aia(MemoryRegion *container,
>   {
>       int i;
>       hwaddr addr = 0;
> +    uint32_t hart_bits, guest_bits;
>       DeviceState *aplic_s_dev = NULL;
>       DeviceState *aplic_m_dev = NULL;
>   
> @@ -93,7 +94,10 @@ DeviceState *riscv_create_aia(MemoryRegion *container,
>                                    msimode, false, aplic_m_dev);
>   
>       if (kvm_enabled() && msimode) {
> -        riscv_aplic_set_kvm_msicfgaddr(RISCV_APLIC(aplic_s_dev), addr);
> +        hart_bits = imsic_num_bits(hart_count);
> +        guest_bits = imsic_num_bits(aia_guests + 1);
> +        riscv_aplic_set_kvm_msicfgaddr(RISCV_APLIC(aplic_s_dev), addr,
> +                                    hart_bits, guest_bits);
>       }
>   
>       return kvm_enabled() ? aplic_s_dev : aplic_m_dev;
> diff --git a/include/hw/intc/riscv_aplic.h b/include/hw/intc/riscv_aplic.h
> index 62d2b33ade..d3ee2e537a 100644
> --- a/include/hw/intc/riscv_aplic.h
> +++ b/include/hw/intc/riscv_aplic.h
> @@ -78,7 +78,8 @@ struct RISCVAPLICState {
>   void riscv_aplic_add_child(DeviceState *parent, DeviceState *child);
>   bool riscv_is_kvm_aia_aplic_imsic(bool msimode);
>   bool riscv_use_emulated_aplic(bool msimode);
> -void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr);
> +void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
> +                                    uint32_t hart_bits, uint32_t guest_bits);
>   
>   DeviceState *riscv_aplic_create(MemoryRegion *container,
>       hwaddr addr, hwaddr size, uint32_t hartid_base, uint32_t num_harts,