hw/intc/aplic: Fix msicfgaddrH in split mode
PATCH v3 1/1 hw/intc/aplic: Fix msicfgaddrH in split mode
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().
---
Changes in v3:
- Add assertions for hart_bits and guest_bits.
Changes in v2:
- Add rationale.
--- hw/intc/riscv_aplic.c | 16 ++++++++++++---- hw/riscv/aia.c | 6 +++++- include/hw/intc/riscv_aplic.h | 3 ++- 3 files changed, 19 insertions(+), 6 deletions(-)
hw/intc/riscv_aplic.c +12 -4
@@ -182,8 +182,12 @@ 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) { +assert (hart_bits <= APLIC_xMSICFGADDRH_LHXW_MASK); +assert (guest_bits <= APLIC_xMSICFGADDRH_LHXS_MASK); +if (!kvm_enabled ()) {return ; }
@@ -191,8 +195,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 +412,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;
hw/riscv/aia.c +5 -1
@@ -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;
include/hw/intc/riscv_aplic.h +2 -1
@@ -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,
Signature · 1 line
2.53.0
Alistair Francis
AppliedQuoted 68 lines · BillXiang
Add
hart_bitsandguest_bitsto msicfgaddrH in KVM irqchip split mode.
Also remove the redundant left shift when loadingkvm_msicfgaddrHinriscv_aplic_msi_send().Rationale:
static void riscv_aplic_msi_send(RISCVAPLICState *aplic,uint32_thart_idx,uint32_tguest_idx,
uint32_teiid)
{
uint64_taddr;
MemTxResult result;
RISCVAPLICState*aplic_m;
uint32_tlhxs, 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_idxandguest_idxare 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 intokvm_msicfgaddrH, so lhxs/lhxw always read as zero, and the
hart/guest idxs were never encoded into the MSI address. Fix this by programminghart_bits(LHXW) andguest_bits(LHXS) intokvm_msicfgaddrH
inriscv_aplic_set_kvm_msicfgaddr().
Quoted 4 lines
Reviewed-by Nutty Liu <[email protected]>Reviewed-by Daniel Henrique BarbozaSigned-off-by BillXiang <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 106 lines
---
Changes in v3:
- Add assertions forhart_bitsandguest_bits. Changes in v2:
- Add rationale.
---
hw/intc/riscv_aplic.c | 16 ++++++++++++---- hw/riscv/aia.c | 6 +++++-include/hw/intc/riscv_aplic.h| 3 ++-3 files changed, 19 insertions(+), 6 deletions(-)hw/intc/riscv_aplic.c +0
-0@@ -182,8 +182,12 @@ bool riscv_use_emulated_aplic(bool msimode) return kvm_kernel_irqchip_split(); }
-voidriscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr)
+void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr,
+uint32_thart_bits,uint32_tguest_bits)
{
+assert(hart_bits <= APLIC_xMSICFGADDRH_LHXW_MASK); +assert(guest_bits <= APLIC_xMSICFGADDRH_LHXS_MASK); +
if (!kvm_enabled()) {
return;
}hw/intc/riscv_aplic.c +6
-2@@ -191,8 +195,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); }}
hw/intc/riscv_aplic.c +0
-0@@ -404,7 +412,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;hw/riscv/aia.c +1
-0@@ -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;hw/riscv/aia.c +0
-0@@ -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); }
returnkvm_enabled()?aplic_s_dev:aplic_m_dev;include/hw/intc/riscv_aplic.h +2
-1@@ -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_thartid_base,uint32_tnum_harts,