Skip to contentSkip to conversation

hw/riscv/riscv-iommu: fix MSI_PTE_M_BASIC address

PATCH In review Daniel Henrique Barboza 2 participants 2 messages Jul 2 – Jul 3 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/riscv/riscv-iommu: fix MSI_PTE_M_BASIC address

We're not considering the page offset when retrieving the MSI address. riscv-iommu spec 1.0 says:

"1. Let A be the GPA

(...)

13. If msipte.M == 3 the PTE is in basic translate mode and the translation process is as follows:
(...)
b. Compute the translated address as msipte.PPN << 12 | A[11:0]."

I.e. we need to bit_ior the GPA page offset in the translated address.

Fixes 0c54acb8243d ("hw/riscv: add RISC-V IOMMU base emulation")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3560

---
 hw/riscv/riscv-iommu-bits.h | 1 +
 hw/riscv/riscv-iommu.c      | 1 +
 2 files changed, 2 insertions(+)
hw/riscv/riscv-iommu-bits.h +1 -0
index cc0b1c3ccd..f1f10cc318 100644
@@ -471,4 +471,5 @@ struct riscv_iommu_msi_pte {
 #define RISCV_IOMMU_MSI_MRIF_NPPN       RISCV_IOMMU_PPN_FIELD
 #define RISCV_IOMMU_MSI_MRIF_NID_MSB    BIT_ULL(60)
 
+#define RISCV_IOMMU_MSI_BASIC_PAGE_OFFSET  GENMASK_ULL(11, 0)
 #endif /* _RISCV_IOMMU_BITS_H_ */
hw/riscv/riscv-iommu.c +1 -0
index 6761b77f95..6f3db7b73d 100644
@@ -658,6 +658,7 @@ static MemTxResult riscv_iommu_msi_write(RISCVIOMMUState *s,
     case RISCV_IOMMU_MSI_PTE_M_BASIC:
         /* MSI Pass-through mode */
         addr = PPN_PHYS(get_field(pte[0], RISCV_IOMMU_MSI_PTE_PPN));
+        addr |= (gpa & RISCV_IOMMU_MSI_BASIC_PAGE_OFFSET);
 
         trace_riscv_iommu_msi(s->parent_obj.id, PCI_BUS_NUM(ctx->devid),
                               PCI_SLOT(ctx->devid), PCI_FUNC(ctx->devid),
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Daniel Henrique Barboza

We're not considering the page offset when retrieving the MSI address. riscv-iommu spec 1.0 says:

"1. Let A be the GPA

(...)

13. If msipte.M == 3 the PTE is in basic translate mode and the translation process is as follows:
(...)
b. Compute the translated address as msipte.PPN << 12 | A[11:0]."

Quoted 5 lines

I.e. we need to bit_ior the GPA page offset in the translated address.

Fixes 0c54acb8243d ("hw/riscv: add RISC-V IOMMU base emulation")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3560

Thanks,
Nutty

Quoted 27 lines
---
  hw/riscv/riscv-iommu-bits.h | 1 +
  hw/riscv/riscv-iommu.c      | 1 +
  2 files changed, 2 insertions(+)
hw/riscv/riscv-iommu-bits.h +1 -0
index cc0b1c3ccd..f1f10cc318 100644
@@ -471,4 +471,5 @@ struct riscv_iommu_msi_pte {
  #define RISCV_IOMMU_MSI_MRIF_NPPN       RISCV_IOMMU_PPN_FIELD
  #define RISCV_IOMMU_MSI_MRIF_NID_MSB    BIT_ULL(60)
 
+#define RISCV_IOMMU_MSI_BASIC_PAGE_OFFSET  GENMASK_ULL(11, 0)
  #endif /* _RISCV_IOMMU_BITS_H_ */
hw/riscv/riscv-iommu.c +1 -0
index 6761b77f95..6f3db7b73d 100644
@@ -658,6 +658,7 @@ static MemTxResult riscv_iommu_msi_write(RISCVIOMMUState *s,
      case RISCV_IOMMU_MSI_PTE_M_BASIC:
          /* MSI Pass-through mode */
          addr = PPN_PHYS(get_field(pte[0], RISCV_IOMMU_MSI_PTE_PPN));
+        addr |= (gpa & RISCV_IOMMU_MSI_BASIC_PAGE_OFFSET);
 
          trace_riscv_iommu_msi(s->parent_obj.id, PCI_BUS_NUM(ctx->devid),
                                PCI_SLOT(ctx->devid), PCI_FUNC(ctx->devid),