Skip to contentSkip to conversation

hw/intc/aplic: Fix msicfgaddrH in split mode

PATCH Superseded BillXiang 2 participants 3 messages Nov 2 – Nov 3 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

BillXiang

author
#0 lore raw

PATCH 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().

---
 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(-)
hw/intc/riscv_aplic.c +9 -4
index 03a0852b8b..2b618e73df 100644
@@ -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;
hw/riscv/aia.c +5 -1
index 56fc064533..bd608bd7a6 100644
@@ -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
index 62d2b33ade..d3ee2e537a 100644
@@ -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
#1 lore raw
Quoted 4 lines · BillXiang

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().

I suggest doing two patches: one for the hart_bits and guest_bits change and
another one for the left shift. Unless the left shift became redundant because
of the hart_bits_and guest_bits made - then it's ok to keep it in a single patch.

Quoted 24 lines
---
  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(-)
hw/intc/riscv_aplic.c +2 -1
index 03a0852b8b..2b618e73df 100644
@@ -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)) {

hw/intc/riscv_aplic.c void riscv_aplic_set_kvm_msicfgaddr(RISCVAPLICState *aplic, hwaddr addr) · line 200

Quoted 10 lines
hw/intc/riscv_aplic.c +6 -2
          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);

Please add temporary variables like:

uint64_t hart_bits_shift = ((hart_bits & APLIC_xMSICFGADDRH_LHXW_MASK) << APLIC_xMSICFGADDRH_LHXW_SHIFT) uint64_t guest_bits_shift = (guest_bits & APLIC_xMSICFGADDRH_LHXS_MASK) << APLIC_xMSICFGADDRH_LHXS_SHIFT)

And then use the temp variables for the kvm_msicfgaddrH calculation.

Also, please explain the rationale used. Can be in code comment format or in the commit msg.

hw/intc/riscv_aplic.c static void riscv_aplic_msi_send(RISCVAPLICState *aplic, · line 412

Quoted 9 lines
hw/intc/riscv_aplic.c +1 -1
      }
  }
 
@@ -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;

Same thing here. I'm assuming that this is now irrelevant due to the hart_bits and guest_bits change, but instead of assuming it's better if you just explain why is this the case.

Thanks,
Daniel

Quoted 41 lines
hw/intc/riscv_aplic.c +0 -0
      } else {
          msicfgaddr = aplic_m->mmsicfgaddr;
          msicfgaddrH = aplic_m->mmsicfgaddrH;
hw/riscv/aia.c +5 -1
index 56fc064533..bd608bd7a6 100644
@@ -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
index 62d2b33ade..d3ee2e537a 100644
@@ -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,
author
#2 lore raw

Hi Daniel,

Thank you for the review. I'll resend the patch as v2.

Regarding splitting the patch: I'd prefer to keep it as a single patch, since the two changes are coupled and can't work without each other.

Rationale (to be added to the commit message):

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().

Thanks,
Bill

Quoted 125 lines · Daniel Henrique Barboza
Quoted 4 lines · BillXiang

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().

I suggest doing two patches:  one for the hart_bits and guest_bits change and
another one for the left shift.   Unless the left shift became redundant because
of the hart_bits_and guest_bits made - then it's ok to keep it in a single patch.

Quoted 25 lines

---
  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(-)
hw/intc/riscv_aplic.c +2 -1
index 03a0852b8b..2b618e73df 100644
@@ -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)) {

hw/intc/riscv_aplic.c void · line 201

Quoted 10 lines
hw/intc/riscv_aplic.c +6 -2
           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);

Please add temporary variables like:

uint64_t hart_bits_shift = ((hart_bits & APLIC_xMSICFGADDRH_LHXW_MASK) << APLIC_xMSICFGADDRH_LHXW_SHIFT)
uint64_t guest_bits_shift = (guest_bits & APLIC_xMSICFGADDRH_LHXS_MASK) << APLIC_xMSICFGADDRH_LHXS_SHIFT)

And then use the temp variables for the kvm_msicfgaddrH calculation.

Also, please explain the rationale used.  Can be in code comment format or in the
commit msg.

hw/intc/riscv_aplic.c static void riscv_aplic_msi_send(RISCVAPLICState · line 412

Quoted 8 lines
hw/intc/riscv_aplic.c +1 -1
       }
   }
@@ -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;

Same thing here.  I'm assuming that this is now irrelevant due to the hart_bits and
guest_bits change, but instead of assuming it's better if you just explain why is
this the case.

Thanks,
Daniel

Quoted 40 lines
hw/intc/riscv_aplic.c +0 -0
       } else {
           msicfgaddr = aplic_m->mmsicfgaddr;
           msicfgaddrH = aplic_m->mmsicfgaddrH;
hw/riscv/aia.c +5 -1
index 56fc064533..bd608bd7a6 100644
@@ -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/ renamed from include/hw/intc/riscv_aplic.h +0 -0

riscv_aplic.h
index 62d2b33ade..d3ee2e537a 100644

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,