riscv: QEMU RISC-V IOMMU Support
Subsystems (2)
v8 cover riscv: QEMU RISC-V IOMMU Support
Hi,
We had problems right at the finish line of the pull request due to endianness problems reported in the Gitlab CI [1]. This triggered discussions in the middle of the pull request patches [2] that resulted in this new version.
We dealt with the endianness problem that was hitting the Gitlab CI in s390x. It was a mix of test changes required in the libqos support, a solution proposed by Peter, and a real endianness problem in patch 3. I made sure to test this version in a s390x VM and everything seems good.
Another change made, after review comments from Peter, was the removal of all locks that were result of the old threading approach that we abandoned in v3. BQL is proven to be enough, at least in my testing, to prevent race conditions. Nothing is stopping us from changing our minds later, and even rollback to a thread model with spinlocks and so on, but for now we'll stick to what we decided back in v3.
The rest were punctual changes also requested by Peter and Tomasz during the discussions in the pull request [1].
Patches based on riscv-to-apply.next. All patches acked.
Changes from v7:
- patch 3:
- fixed Copyright text of added files
- all functions starting with '__X' were renamed to 'riscv_iommu_X'
- removed qemu/osdep.h from riscv-iommu.h
- removed unused '__rfu' attribute from RISCVIOMMUContext
- removed core_lock, ctx_lock and regs_lock
- converted the 'read page table entry' code from using dma_memory_read() and
le_to_cpu() to use ldl_le_dma()
- replaced the 'if size' chain for 'ldn_le_p()' in riscv_iommu_mmio_read()
- added a new riscv_iommu_write_reg_val() helper. Used it to remove the 'if
size' chains inside riscv_iommu_mmio_write()
- do le64_to_cpu() before using 'val' in riscv_iommu_mmio_write() ICVEC/IPSR
path; - use brackets to wrap around (icvec & bit) in riscv_iommu_get_icvec_vector()
- patch 5:
- fixed Copyright text of riscv-iommu-pci.c
- patch 7:
- use qpci_io_read(l/q) instead of qcpi_memread()
- patch 8:
- remove iot_lock
- remove unused '__rfu' attribute from RISCVIOMMUEntry
- all functions starting with '__X' were renamed to 'riscv_iommu_X'
- patch 11:
- use qpci_io_write(l/q) instead of qpci_memwrite()
- v7 link: https://lore.kernel.org/qemu-riscv/[email protected]/
[1] https://lore.kernel.org/qemu-devel/CAFEAcA_8A=8q8aFSPwy177x6q3RLOS-kOHwyUDhTbpOjNjLexg@mail.gmail.com/
[2] https://lore.kernel.org/qemu-devel/CAFEAcA8rdFYACFKdJga72WA4ET9NFRwrOifdbTYDBxY6G6uOXA@mail.gmail.com/
Daniel Henrique Barboza (4):
pci-ids.rst: add Red Hat pci-id for RISC-V IOMMU device test/qtest: add riscv-iommu-pci tests qtest/riscv-iommu-test: add init queues test docs/specs: add riscv-iommu
Tomasz Jeznach (8):
exec/memtxattr: add process identifier to the transaction attributes hw/riscv: add riscv-iommu-bits.h
hw/riscv: add RISC-V IOMMU base emulation
hw/riscv: add riscv-iommu-pci reference device hw/riscv/virt.c: support for RISC-V IOMMU PCIDevice hotplug hw/riscv/riscv-iommu: add Address Translation Cache (IOATC) hw/riscv/riscv-iommu: add ATS support hw/riscv/riscv-iommu: add DBG support
docs/specs/index.rst | 1 + docs/specs/pci-ids.rst | 2 + docs/specs/riscv-iommu.rst | 90 ++ docs/system/riscv/virt.rst | 13 + hw/riscv/Kconfig | 4 + hw/riscv/meson.build | 1 + hw/riscv/riscv-iommu-bits.h | 421 ++++++ hw/riscv/riscv-iommu-pci.c | 202 +++ hw/riscv/riscv-iommu.c | 2396 ++++++++++++++++++++++++++++++ hw/riscv/riscv-iommu.h | 130 ++ hw/riscv/trace-events | 17 + hw/riscv/trace.h | 1 + hw/riscv/virt.c | 33 +- include/exec/memattrs.h | 5 + include/hw/pci/pci.h | 1 + include/hw/riscv/iommu.h | 36 + meson.build | 1 + tests/qtest/libqos/meson.build | 4 + tests/qtest/libqos/riscv-iommu.c | 76 + tests/qtest/libqos/riscv-iommu.h | 101 ++ tests/qtest/meson.build | 1 + tests/qtest/riscv-iommu-test.c | 210 +++ 22 files changed, 3745 insertions(+), 1 deletion(-) create mode 100644 docs/specs/riscv-iommu.rst create mode 100644 hw/riscv/riscv-iommu-bits.h create mode 100644 hw/riscv/riscv-iommu-pci.c create mode 100644 hw/riscv/riscv-iommu.c create mode 100644 hw/riscv/riscv-iommu.h create mode 100644 hw/riscv/trace-events create mode 100644 hw/riscv/trace.h create mode 100644 include/hw/riscv/iommu.h create mode 100644 tests/qtest/libqos/riscv-iommu.c create mode 100644 tests/qtest/libqos/riscv-iommu.h create mode 100644 tests/qtest/riscv-iommu-test.c
Signature · 1 line
2.45.2
PATCH v8 1/12 exec/memtxattr: add process identifier to the transaction attributes
From: Tomasz Jeznach <[email protected]>
Extend memory transaction attributes with process identifier to allow per-request address translation logic to use requester_id / process_id to identify memory mapping (e.g. enabling IOMMU w/ PASID translations).
--- include/exec/memattrs.h | 5 +++++ 1 file changed, 5 insertions(+)
include/exec/memattrs.h +5 -0
@@ -52,6 +52,11 @@ typedef struct MemTxAttrs { unsigned int memory:1;/* Requester ID (for MSI for example) */ unsigned int requester_id:16; + +/* +* PID (PCI PASID) support: Limited to 8 bits process identifier. +*/ + unsigned int pid:8; } MemTxAttrs;/* Bus masters which don't specify any attributes will get this,
Signature · 1 line
2.45.2
PATCH v8 2/12 hw/riscv: add riscv-iommu-bits.h
In reply to Daniel Henrique BarbozaFrom: Tomasz Jeznach <[email protected]>
This header will be used by the RISC-V IOMMU emulation to be added in the next patch. Due to its size it's being sent in separate for an easier review.
One thing to notice is that this header can be replaced by the future Linux RISC-V IOMMU driver header, which would become a linux-header we would import instead of keeping our own. The Linux implementation isn't upstream yet so for now we'll have to manage riscv-iommu-bits.h.
--- hw/riscv/riscv-iommu-bits.h | 345 ++++++++++++++++++++++++++++++++++++ 1 file changed, 345 insertions(+) create mode 100644 hw/riscv/riscv-iommu-bits.h
hw/riscv/riscv-iommu-bits.h new file +345 -0
@@ -0,0 +1,345 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright © 2022-2023 Rivos Inc. +* Copyright © 2023 FORTH-ICS/CARV +* Copyright © 2023 RISC-V IOMMU Task Group +* +* RISC-V IOMMU - Register Layout and Data Structures. +* +* Based on the IOMMU spec version 1.0, 3/2023 +* https://github.com/riscv-non-isa/riscv-iommu +*/ + +#ifndef HW_RISCV_IOMMU_BITS_H +#define HW_RISCV_IOMMU_BITS_H + +#define RISCV_IOMMU_SPEC_DOT_VER 0x010 + +#ifndef GENMASK_ULL +#define GENMASK_ULL (h, l) (((~0ULL) >> (63 - (h) + (l))) << (l)) +#endif + +/* +* struct riscv_iommu_fq_record - Fault/Event Queue Record +* See section 3.2 for more info. +*/ +struct riscv_iommu_fq_record { +uint64_t hdr; +uint64_t _reserved; +uint64_t iotval; +uint64_t iotval2; +}; +/* Header fields */ +#define RISCV_IOMMU_FQ_HDR_CAUSE GENMASK_ULL(11, 0) +#define RISCV_IOMMU_FQ_HDR_PID GENMASK_ULL(31, 12) +#define RISCV_IOMMU_FQ_HDR_PV BIT_ULL(32) +#define RISCV_IOMMU_FQ_HDR_TTYPE GENMASK_ULL(39, 34) +#define RISCV_IOMMU_FQ_HDR_DID GENMASK_ULL(63, 40) + +/* +* struct riscv_iommu_pq_record - PCIe Page Request record +* For more infos on the PCIe Page Request queue see chapter 3.3. +*/ +struct riscv_iommu_pq_record { +uint64_t hdr; +uint64_t payload; +}; +/* Header fields */ +#define RISCV_IOMMU_PREQ_HDR_PID GENMASK_ULL(31, 12) +#define RISCV_IOMMU_PREQ_HDR_PV BIT_ULL(32) +#define RISCV_IOMMU_PREQ_HDR_PRIV BIT_ULL(33) +#define RISCV_IOMMU_PREQ_HDR_EXEC BIT_ULL(34) +#define RISCV_IOMMU_PREQ_HDR_DID GENMASK_ULL(63, 40) +/* Payload fields */ +#define RISCV_IOMMU_PREQ_PAYLOAD_M GENMASK_ULL(2, 0) + +/* Common field positions */ +#define RISCV_IOMMU_PPN_FIELD GENMASK_ULL(53, 10) +#define RISCV_IOMMU_QUEUE_LOGSZ_FIELD GENMASK_ULL(4, 0) +#define RISCV_IOMMU_QUEUE_INDEX_FIELD GENMASK_ULL(31, 0) +#define RISCV_IOMMU_QUEUE_ENABLE BIT(0) +#define RISCV_IOMMU_QUEUE_INTR_ENABLE BIT(1) +#define RISCV_IOMMU_QUEUE_MEM_FAULT BIT(8) +#define RISCV_IOMMU_QUEUE_OVERFLOW BIT(9) +#define RISCV_IOMMU_QUEUE_ACTIVE BIT(16) +#define RISCV_IOMMU_QUEUE_BUSY BIT(17) +#define RISCV_IOMMU_ATP_PPN_FIELD GENMASK_ULL(43, 0) +#define RISCV_IOMMU_ATP_MODE_FIELD GENMASK_ULL(63, 60) + +/* 5.3 IOMMU Capabilities (64bits) */ +#define RISCV_IOMMU_REG_CAP 0x0000 +#define RISCV_IOMMU_CAP_VERSION GENMASK_ULL(7, 0) +#define RISCV_IOMMU_CAP_MSI_FLAT BIT_ULL(22) +#define RISCV_IOMMU_CAP_MSI_MRIF BIT_ULL(23) +#define RISCV_IOMMU_CAP_T2GPA BIT_ULL(26) +#define RISCV_IOMMU_CAP_IGS GENMASK_ULL(29, 28) +#define RISCV_IOMMU_CAP_PAS GENMASK_ULL(37, 32) +#define RISCV_IOMMU_CAP_PD8 BIT_ULL(38) +#define RISCV_IOMMU_CAP_PD17 BIT_ULL(39) +#define RISCV_IOMMU_CAP_PD20 BIT_ULL(40) + +/* 5.4 Features control register (32bits) */ +#define RISCV_IOMMU_REG_FCTL 0x0008 +#define RISCV_IOMMU_FCTL_WSI BIT(1) + +/* 5.5 Device-directory-table pointer (64bits) */ +#define RISCV_IOMMU_REG_DDTP 0x0010 +#define RISCV_IOMMU_DDTP_MODE GENMASK_ULL(3, 0) +#define RISCV_IOMMU_DDTP_BUSY BIT_ULL(4) +#define RISCV_IOMMU_DDTP_PPN RISCV_IOMMU_PPN_FIELD + +enum riscv_iommu_ddtp_modes { +RISCV_IOMMU_DDTP_MODE_OFF =0 , +RISCV_IOMMU_DDTP_MODE_BARE =1 , +RISCV_IOMMU_DDTP_MODE_1LVL =2 , +RISCV_IOMMU_DDTP_MODE_2LVL =3 , +RISCV_IOMMU_DDTP_MODE_3LVL =4 , +RISCV_IOMMU_DDTP_MODE_MAX =4 +}; + +/* 5.6 Command Queue Base (64bits) */ +#define RISCV_IOMMU_REG_CQB 0x0018 +#define RISCV_IOMMU_CQB_LOG2SZ RISCV_IOMMU_QUEUE_LOGSZ_FIELD +#define RISCV_IOMMU_CQB_PPN RISCV_IOMMU_PPN_FIELD + +/* 5.7 Command Queue head (32bits) */ +#define RISCV_IOMMU_REG_CQH 0x0020 + +/* 5.8 Command Queue tail (32bits) */ +#define RISCV_IOMMU_REG_CQT 0x0024 + +/* 5.9 Fault Queue Base (64bits) */ +#define RISCV_IOMMU_REG_FQB 0x0028 +#define RISCV_IOMMU_FQB_LOG2SZ RISCV_IOMMU_QUEUE_LOGSZ_FIELD +#define RISCV_IOMMU_FQB_PPN RISCV_IOMMU_PPN_FIELD + +/* 5.10 Fault Queue Head (32bits) */ +#define RISCV_IOMMU_REG_FQH 0x0030 + +/* 5.11 Fault Queue tail (32bits) */ +#define RISCV_IOMMU_REG_FQT 0x0034 + +/* 5.12 Page Request Queue base (64bits) */ +#define RISCV_IOMMU_REG_PQB 0x0038 +#define RISCV_IOMMU_PQB_LOG2SZ RISCV_IOMMU_QUEUE_LOGSZ_FIELD +#define RISCV_IOMMU_PQB_PPN RISCV_IOMMU_PPN_FIELD + +/* 5.13 Page Request Queue head (32bits) */ +#define RISCV_IOMMU_REG_PQH 0x0040 + +/* 5.14 Page Request Queue tail (32bits) */ +#define RISCV_IOMMU_REG_PQT 0x0044 + +/* 5.15 Command Queue CSR (32bits) */ +#define RISCV_IOMMU_REG_CQCSR 0x0048 +#define RISCV_IOMMU_CQCSR_CQEN RISCV_IOMMU_QUEUE_ENABLE +#define RISCV_IOMMU_CQCSR_CIE RISCV_IOMMU_QUEUE_INTR_ENABLE +#define RISCV_IOMMU_CQCSR_CQMF RISCV_IOMMU_QUEUE_MEM_FAULT +#define RISCV_IOMMU_CQCSR_CMD_TO BIT(9) +#define RISCV_IOMMU_CQCSR_CMD_ILL BIT(10) +#define RISCV_IOMMU_CQCSR_FENCE_W_IP BIT(11) +#define RISCV_IOMMU_CQCSR_CQON RISCV_IOMMU_QUEUE_ACTIVE +#define RISCV_IOMMU_CQCSR_BUSY RISCV_IOMMU_QUEUE_BUSY + +/* 5.16 Fault Queue CSR (32bits) */ +#define RISCV_IOMMU_REG_FQCSR 0x004C +#define RISCV_IOMMU_FQCSR_FQEN RISCV_IOMMU_QUEUE_ENABLE +#define RISCV_IOMMU_FQCSR_FIE RISCV_IOMMU_QUEUE_INTR_ENABLE +#define RISCV_IOMMU_FQCSR_FQMF RISCV_IOMMU_QUEUE_MEM_FAULT +#define RISCV_IOMMU_FQCSR_FQOF RISCV_IOMMU_QUEUE_OVERFLOW +#define RISCV_IOMMU_FQCSR_FQON RISCV_IOMMU_QUEUE_ACTIVE +#define RISCV_IOMMU_FQCSR_BUSY RISCV_IOMMU_QUEUE_BUSY + +/* 5.17 Page Request Queue CSR (32bits) */ +#define RISCV_IOMMU_REG_PQCSR 0x0050 +#define RISCV_IOMMU_PQCSR_PQEN RISCV_IOMMU_QUEUE_ENABLE +#define RISCV_IOMMU_PQCSR_PIE RISCV_IOMMU_QUEUE_INTR_ENABLE +#define RISCV_IOMMU_PQCSR_PQMF RISCV_IOMMU_QUEUE_MEM_FAULT +#define RISCV_IOMMU_PQCSR_PQOF RISCV_IOMMU_QUEUE_OVERFLOW +#define RISCV_IOMMU_PQCSR_PQON RISCV_IOMMU_QUEUE_ACTIVE +#define RISCV_IOMMU_PQCSR_BUSY RISCV_IOMMU_QUEUE_BUSY + +/* 5.18 Interrupt Pending Status (32bits) */ +#define RISCV_IOMMU_REG_IPSR 0x0054 +#define RISCV_IOMMU_IPSR_CIP BIT(0) +#define RISCV_IOMMU_IPSR_FIP BIT(1) +#define RISCV_IOMMU_IPSR_PIP BIT(3) + +enum { +RISCV_IOMMU_INTR_CQ , +RISCV_IOMMU_INTR_FQ , +RISCV_IOMMU_INTR_PM , +RISCV_IOMMU_INTR_PQ , +RISCV_IOMMU_INTR_COUNT +}; + +/* 5.27 Interrupt cause to vector (64bits) */ +#define RISCV_IOMMU_REG_ICVEC 0x02F8 + +/* 5.28 MSI Configuration table (32 * 64bits) */ +#define RISCV_IOMMU_REG_MSI_CONFIG 0x0300 + +#define RISCV_IOMMU_REG_SIZE 0x1000 + +#define RISCV_IOMMU_DDTE_VALID BIT_ULL(0) +#define RISCV_IOMMU_DDTE_PPN RISCV_IOMMU_PPN_FIELD + +/* Struct riscv_iommu_dc - Device Context - section 2.1 */ +struct riscv_iommu_dc { +uint64_t tc; +uint64_t iohgatp; +uint64_t ta; +uint64_t fsc; +uint64_t msiptp; +uint64_t msi_addr_mask; +uint64_t msi_addr_pattern; +uint64_t _reserved; +}; + +/* Translation control fields */ +#define RISCV_IOMMU_DC_TC_V BIT_ULL(0) +#define RISCV_IOMMU_DC_TC_EN_PRI BIT_ULL(2) +#define RISCV_IOMMU_DC_TC_T2GPA BIT_ULL(3) +#define RISCV_IOMMU_DC_TC_DTF BIT_ULL(4) +#define RISCV_IOMMU_DC_TC_PDTV BIT_ULL(5) +#define RISCV_IOMMU_DC_TC_PRPR BIT_ULL(6) +#define RISCV_IOMMU_DC_TC_DPE BIT_ULL(9) +#define RISCV_IOMMU_DC_TC_SBE BIT_ULL(10) +#define RISCV_IOMMU_DC_TC_SXL BIT_ULL(11) + +/* Second-stage (aka G-stage) context fields */ +#define RISCV_IOMMU_DC_IOHGATP_PPN RISCV_IOMMU_ATP_PPN_FIELD +#define RISCV_IOMMU_DC_IOHGATP_GSCID GENMASK_ULL(59, 44) +#define RISCV_IOMMU_DC_IOHGATP_MODE RISCV_IOMMU_ATP_MODE_FIELD + +enum riscv_iommu_dc_iohgatp_modes { +RISCV_IOMMU_DC_IOHGATP_MODE_BARE =0 , +RISCV_IOMMU_DC_IOHGATP_MODE_SV32X4 =8 , +RISCV_IOMMU_DC_IOHGATP_MODE_SV39X4 =8 , +RISCV_IOMMU_DC_IOHGATP_MODE_SV48X4 =9 , +RISCV_IOMMU_DC_IOHGATP_MODE_SV57X4 =10 +}; + +/* Translation attributes fields */ +#define RISCV_IOMMU_DC_TA_PSCID GENMASK_ULL(31, 12) + +/* First-stage context fields */ +#define RISCV_IOMMU_DC_FSC_PPN RISCV_IOMMU_ATP_PPN_FIELD +#define RISCV_IOMMU_DC_FSC_MODE RISCV_IOMMU_ATP_MODE_FIELD + +/* Generic I/O MMU command structure - check section 3.1 */ +struct riscv_iommu_command { +uint64_t dword0; +uint64_t dword1; +}; + +#define RISCV_IOMMU_CMD_OPCODE GENMASK_ULL(6, 0) +#define RISCV_IOMMU_CMD_FUNC GENMASK_ULL(9, 7) + +#define RISCV_IOMMU_CMD_IOTINVAL_OPCODE 1 +#define RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA 0 +#define RISCV_IOMMU_CMD_IOTINVAL_FUNC_GVMA 1 +#define RISCV_IOMMU_CMD_IOTINVAL_AV BIT_ULL(10) +#define RISCV_IOMMU_CMD_IOTINVAL_PSCID GENMASK_ULL(31, 12) +#define RISCV_IOMMU_CMD_IOTINVAL_PSCV BIT_ULL(32) +#define RISCV_IOMMU_CMD_IOTINVAL_GV BIT_ULL(33) +#define RISCV_IOMMU_CMD_IOTINVAL_GSCID GENMASK_ULL(59, 44) + +#define RISCV_IOMMU_CMD_IOFENCE_OPCODE 2 +#define RISCV_IOMMU_CMD_IOFENCE_FUNC_C 0 +#define RISCV_IOMMU_CMD_IOFENCE_AV BIT_ULL(10) +#define RISCV_IOMMU_CMD_IOFENCE_DATA GENMASK_ULL(63, 32) + +#define RISCV_IOMMU_CMD_IODIR_OPCODE 3 +#define RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT 0 +#define RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_PDT 1 +#define RISCV_IOMMU_CMD_IODIR_PID GENMASK_ULL(31, 12) +#define RISCV_IOMMU_CMD_IODIR_DV BIT_ULL(33) +#define RISCV_IOMMU_CMD_IODIR_DID GENMASK_ULL(63, 40) + +enum riscv_iommu_dc_fsc_atp_modes { +RISCV_IOMMU_DC_FSC_MODE_BARE =0 , +RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV32 =8 , +RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV39 =8 , +RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV48 =9 , +RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV57 =10 , +RISCV_IOMMU_DC_FSC_PDTP_MODE_PD8 =1 , +RISCV_IOMMU_DC_FSC_PDTP_MODE_PD17 =2 , +RISCV_IOMMU_DC_FSC_PDTP_MODE_PD20 =3 +}; + +enum riscv_iommu_fq_causes { +RISCV_IOMMU_FQ_CAUSE_INST_FAULT =1 , +RISCV_IOMMU_FQ_CAUSE_RD_ADDR_MISALIGNED =4 , +RISCV_IOMMU_FQ_CAUSE_RD_FAULT =5 , +RISCV_IOMMU_FQ_CAUSE_WR_ADDR_MISALIGNED =6 , +RISCV_IOMMU_FQ_CAUSE_WR_FAULT =7 , +RISCV_IOMMU_FQ_CAUSE_INST_FAULT_S =12 , +RISCV_IOMMU_FQ_CAUSE_RD_FAULT_S =13 , +RISCV_IOMMU_FQ_CAUSE_WR_FAULT_S =15 , +RISCV_IOMMU_FQ_CAUSE_INST_FAULT_VS =20 , +RISCV_IOMMU_FQ_CAUSE_RD_FAULT_VS =21 , +RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS =23 , +RISCV_IOMMU_FQ_CAUSE_DMA_DISABLED =256 , +RISCV_IOMMU_FQ_CAUSE_DDT_LOAD_FAULT =257 , +RISCV_IOMMU_FQ_CAUSE_DDT_INVALID =258 , +RISCV_IOMMU_FQ_CAUSE_DDT_MISCONFIGURED =259 , +RISCV_IOMMU_FQ_CAUSE_TTYPE_BLOCKED =260 , +RISCV_IOMMU_FQ_CAUSE_MSI_LOAD_FAULT =261 , +RISCV_IOMMU_FQ_CAUSE_MSI_INVALID =262 , +RISCV_IOMMU_FQ_CAUSE_MSI_MISCONFIGURED =263 , +RISCV_IOMMU_FQ_CAUSE_MRIF_FAULT =264 , +RISCV_IOMMU_FQ_CAUSE_PDT_LOAD_FAULT =265 , +RISCV_IOMMU_FQ_CAUSE_PDT_INVALID =266 , +RISCV_IOMMU_FQ_CAUSE_PDT_MISCONFIGURED =267 , +RISCV_IOMMU_FQ_CAUSE_DDT_CORRUPTED =268 , +RISCV_IOMMU_FQ_CAUSE_PDT_CORRUPTED =269 , +RISCV_IOMMU_FQ_CAUSE_MSI_PT_CORRUPTED =270 , +RISCV_IOMMU_FQ_CAUSE_MRIF_CORRUIPTED =271 , +RISCV_IOMMU_FQ_CAUSE_INTERNAL_DP_ERROR =272 , +RISCV_IOMMU_FQ_CAUSE_MSI_WR_FAULT =273 , +RISCV_IOMMU_FQ_CAUSE_PT_CORRUPTED =274 +}; + +/* MSI page table pointer */ +#define RISCV_IOMMU_DC_MSIPTP_PPN RISCV_IOMMU_ATP_PPN_FIELD +#define RISCV_IOMMU_DC_MSIPTP_MODE RISCV_IOMMU_ATP_MODE_FIELD +#define RISCV_IOMMU_DC_MSIPTP_MODE_OFF 0 +#define RISCV_IOMMU_DC_MSIPTP_MODE_FLAT 1 + +/* Translation attributes fields */ +#define RISCV_IOMMU_PC_TA_V BIT_ULL(0) + +/* First stage context fields */ +#define RISCV_IOMMU_PC_FSC_PPN GENMASK_ULL(43, 0) + +enum riscv_iommu_fq_ttypes { +RISCV_IOMMU_FQ_TTYPE_NONE =0 , +RISCV_IOMMU_FQ_TTYPE_UADDR_INST_FETCH =1 , +RISCV_IOMMU_FQ_TTYPE_UADDR_RD =2 , +RISCV_IOMMU_FQ_TTYPE_UADDR_WR =3 , +RISCV_IOMMU_FQ_TTYPE_TADDR_INST_FETCH =5 , +RISCV_IOMMU_FQ_TTYPE_TADDR_RD =6 , +RISCV_IOMMU_FQ_TTYPE_TADDR_WR =7 , +RISCV_IOMMU_FW_TTYPE_PCIE_MSG_REQ =8 , +}; + +/* Fields on pte */ +#define RISCV_IOMMU_MSI_PTE_V BIT_ULL(0) +#define RISCV_IOMMU_MSI_PTE_M GENMASK_ULL(2, 1) + +#define RISCV_IOMMU_MSI_PTE_M_MRIF 1 +#define RISCV_IOMMU_MSI_PTE_M_BASIC 3 + +/* When M == 1 (MRIF mode) */ +#define RISCV_IOMMU_MSI_PTE_MRIF_ADDR GENMASK_ULL(53, 7) +/* When M == 3 (basic mode) */ +#define RISCV_IOMMU_MSI_PTE_PPN RISCV_IOMMU_PPN_FIELD +#define RISCV_IOMMU_MSI_PTE_C BIT_ULL(63) + +/* Fields on mrif_info */ +#define RISCV_IOMMU_MSI_MRIF_NID GENMASK_ULL(9, 0) +#define RISCV_IOMMU_MSI_MRIF_NPPN RISCV_IOMMU_PPN_FIELD +#define RISCV_IOMMU_MSI_MRIF_NID_MSB BIT_ULL(60) + +#endif /* _RISCV_IOMMU_BITS_H_ */
Signature · 1 line
2.45.2
PATCH v8 3/12 hw/riscv: add RISC-V IOMMU base emulation
In reply to Daniel Henrique BarbozaFrom: Tomasz Jeznach <[email protected]>
The RISC-V IOMMU specification is now ratified as-per the RISC-V international process. The latest frozen specifcation can be found at:
https://github.com/riscv-non-isa/riscv-iommu/releases/download/v1.0/riscv-iommu.pdf
Add the foundation of the device emulation for RISC-V IOMMU. It includes support for s-stage (sv32, sv39, sv48, sv57 caps) and g-stage (sv32x4, sv39x4, sv48x4, sv57x4 caps).
Other capabilities like ATS and DBG support will be added incrementally in the next patches.
--- hw/riscv/Kconfig | 4 + hw/riscv/meson.build | 1 + hw/riscv/riscv-iommu-bits.h | 18 + hw/riscv/riscv-iommu.c | 2018 +++++++++++++++++++++++++++++++++++ hw/riscv/riscv-iommu.h | 126 +++ hw/riscv/trace-events | 14 + hw/riscv/trace.h | 1 + include/hw/riscv/iommu.h | 36 + meson.build | 1 + 9 files changed, 2219 insertions(+) create mode 100644 hw/riscv/riscv-iommu.c create mode 100644 hw/riscv/riscv-iommu.h create mode 100644 hw/riscv/trace-events create mode 100644 hw/riscv/trace.h create mode 100644 include/hw/riscv/iommu.h
hw/riscv/Kconfig +4 -0
@@ -1,3 +1,6 @@ +config RISCV_IOMMU + bool + config RISCV_NUMA bool
@@ -47,6 +50,7 @@ config RISCV_VIRT select SERIAL select RISCV_ACLINT select RISCV_APLIC + select RISCV_IOMMU select RISCV_IMSIC select SIFIVE_PLIC select SIFIVE_TEST
hw/riscv/meson.build +1 -0
@@ -10,5 +10,6 @@ riscv_ss.add(when: 'CONFIG_SIFIVE_U', if_true: files('sifive_u.c')) riscv_ss.add(when: 'CONFIG_SPIKE', if_true: files('spike.c')) riscv_ss.add(when: 'CONFIG_MICROCHIP_PFSOC', if_true: files('microchip_pfsoc.c')) riscv_ss.add(when: 'CONFIG_ACPI', if_true: files('virt-acpi-build.c')) +riscv_ss.add(when: 'CONFIG_RISCV_IOMMU', if_true: files('riscv-iommu.c')) hw_arch += {'riscv': riscv_ss}
hw/riscv/riscv-iommu-bits.h +18 -0
@@ -69,6 +69,14 @@ struct riscv_iommu_pq_record {/* 5.3 IOMMU Capabilities (64bits) */ #define RISCV_IOMMU_REG_CAP 0x0000#define RISCV_IOMMU_CAP_VERSION GENMASK_ULL(7, 0) +#define RISCV_IOMMU_CAP_SV32 BIT_ULL(8) +#define RISCV_IOMMU_CAP_SV39 BIT_ULL(9) +#define RISCV_IOMMU_CAP_SV48 BIT_ULL(10) +#define RISCV_IOMMU_CAP_SV57 BIT_ULL(11) +#define RISCV_IOMMU_CAP_SV32X4 BIT_ULL(16) +#define RISCV_IOMMU_CAP_SV39X4 BIT_ULL(17) +#define RISCV_IOMMU_CAP_SV48X4 BIT_ULL(18) +#define RISCV_IOMMU_CAP_SV57X4 BIT_ULL(19)#define RISCV_IOMMU_CAP_MSI_FLAT BIT_ULL(22)#define RISCV_IOMMU_CAP_MSI_MRIF BIT_ULL(23)#define RISCV_IOMMU_CAP_T2GPA BIT_ULL(26)
@@ -80,7 +88,9 @@ struct riscv_iommu_pq_record {/* 5.4 Features control register (32bits) */ #define RISCV_IOMMU_REG_FCTL 0x0008 +#define RISCV_IOMMU_FCTL_BE BIT(0)#define RISCV_IOMMU_FCTL_WSI BIT(1) +#define RISCV_IOMMU_FCTL_GXL BIT(2)/* 5.5 Device-directory-table pointer (64bits) */ #define RISCV_IOMMU_REG_DDTP 0x0010
@@ -175,6 +185,10 @@ enum {/* 5.27 Interrupt cause to vector (64bits) */ #define RISCV_IOMMU_REG_ICVEC 0x02F8 +#define RISCV_IOMMU_ICVEC_CIV GENMASK_ULL(3, 0) +#define RISCV_IOMMU_ICVEC_FIV GENMASK_ULL(7, 4) +#define RISCV_IOMMU_ICVEC_PMIV GENMASK_ULL(11, 8) +#define RISCV_IOMMU_ICVEC_PIV GENMASK_ULL(15, 12)/* 5.28 MSI Configuration table (32 * 64bits) */ #define RISCV_IOMMU_REG_MSI_CONFIG 0x0300
@@ -203,6 +217,8 @@ struct riscv_iommu_dc {#define RISCV_IOMMU_DC_TC_DTF BIT_ULL(4)#define RISCV_IOMMU_DC_TC_PDTV BIT_ULL(5)#define RISCV_IOMMU_DC_TC_PRPR BIT_ULL(6) +#define RISCV_IOMMU_DC_TC_GADE BIT_ULL(7) +#define RISCV_IOMMU_DC_TC_SADE BIT_ULL(8)#define RISCV_IOMMU_DC_TC_DPE BIT_ULL(9)#define RISCV_IOMMU_DC_TC_SBE BIT_ULL(10)#define RISCV_IOMMU_DC_TC_SXL BIT_ULL(11)
@@ -309,9 +325,11 @@ enum riscv_iommu_fq_causes {/* Translation attributes fields */ #define RISCV_IOMMU_PC_TA_V BIT_ULL(0) +#define RISCV_IOMMU_PC_TA_RESERVED GENMASK_ULL(63, 32)/* First stage context fields */ #define RISCV_IOMMU_PC_FSC_PPN GENMASK_ULL(43, 0) +#define RISCV_IOMMU_PC_FSC_RESERVED GENMASK_ULL(59, 44)enum riscv_iommu_fq_ttypes {RISCV_IOMMU_FQ_TTYPE_NONE =0 ,
hw/riscv/riscv-iommu.c new file +2018 -0
2,018 lines not on this page · raw message
hw/riscv/riscv-iommu.h new file +126 -0
126 lines not on this page · raw message
hw/riscv/trace-events new file +14 -0
@@ -0,0 +1,14 @@ +# See documentation at docs/devel/tracing.rst + +# riscv-iommu.c +riscv_iommu_new(const char *id, unsigned b, unsigned d, unsigned f) "%s: device attached %04x:%02x.%d" +riscv_iommu_flt(const char *id, unsigned b, unsigned d, unsigned f, uint64_t reason, uint64_t iova) "%s: fault %04x:%02x.%u reason: 0x%"PRIx64" iova: 0x%"PRIx64 +riscv_iommu_pri(const char *id, unsigned b, unsigned d, unsigned f, uint64_t iova) "%s: page request %04x:%02x.%u iova: 0x%"PRIx64 +riscv_iommu_dma(const char *id, unsigned b, unsigned d, unsigned f, unsigned pasid, const char *dir, uint64_t iova, uint64_t phys) "%s: translate %04x:%02x.%u #%u %s 0x%"PRIx64" -> 0x%"PRIx64 +riscv_iommu_msi(const char *id, unsigned b, unsigned d, unsigned f, uint64_t iova, uint64_t phys) "%s: translate %04x:%02x.%u MSI 0x%"PRIx64" -> 0x%"PRIx64 +riscv_iommu_mrif_notification(const char *id, uint32_t nid, uint64_t phys) "%s: sent MRIF notification 0x%x to 0x%"PRIx64 +riscv_iommu_cmd(const char *id, uint64_t l, uint64_t u) "%s: command 0x%"PRIx64" 0x%"PRIx64 +riscv_iommu_notifier_add(const char *id) "%s: dev-iotlb notifier added" +riscv_iommu_notifier_del(const char *id) "%s: dev-iotlb notifier removed" +riscv_iommu_notify_int_vector(uint32_t cause, uint32_t vector) "Interrupt cause 0x%x sent via vector 0x%x" +riscv_iommu_icvec_write(uint32_t orig, uint32_t actual) "ICVEC write: incoming 0x%x actual 0x%x"
hw/riscv/trace.h new file +1 -0
@@ -0,0 +1 @@ +#include "trace/trace-hw_riscv.h"
include/hw/riscv/iommu.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* QEMU emulation of an RISC-V IOMMU +* +* Copyright (C) 2022-2023 Rivos Inc. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef HW_RISCV_IOMMU_H +#define HW_RISCV_IOMMU_H + +#include "qemu/osdep.h" +#include "qom/object.h" + +#define TYPE_RISCV_IOMMU "riscv-iommu" +OBJECT_DECLARE_SIMPLE_TYPE(RISCVIOMMUState, RISCV_IOMMU) +typedef struct RISCVIOMMUState RISCVIOMMUState ; + +#define TYPE_RISCV_IOMMU_MEMORY_REGION "riscv-iommu-mr" +typedef struct RISCVIOMMUSpace RISCVIOMMUSpace ; + +#define TYPE_RISCV_IOMMU_PCI "riscv-iommu-pci" +OBJECT_DECLARE_SIMPLE_TYPE(RISCVIOMMUStatePci, RISCV_IOMMU_PCI) +typedef struct RISCVIOMMUStatePci RISCVIOMMUStatePci ; + +#endif
meson.build +1 -0
@@ -3416,6 +3416,7 @@ if have_system 'hw/pci-host', 'hw/ppc', 'hw/rtc', + 'hw/riscv', 'hw/s390x', 'hw/scsi', 'hw/sd',
Signature · 1 line
2.45.2
Re: [PATCH v8 03/12] hw/riscv: add RISC-V IOMMU base emulation
On Tue, Oct 01, 2024 at 10:02:58PM GMT, Daniel Henrique Barboza wrote:
...
Quoted 36 lines
hw/riscv/riscv-iommu.c new file +36
-0+/* +* RISCV IOMMU Address Translation Lookup - Page Table Walk +* +* Note: Code is based on get_physical_address() from target/riscv/cpu_helper.c +* Both implementation can be merged into single helper function in future. +* Keeping them separate for now, as error reporting and flow specifics are +* sufficiently different for separate implementation. +* +* @s : IOMMU Device State +* @ctx : Translation context for device id and process address space id. +* @iotlb : translation data: physical address and access mode. +* @return : success or fault cause code. +*/ +static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +IOMMUTLBEntry *iotlb) +{ +dma_addr_t addr, base; +uint64_t satp, gatp, pte; +bool en_s, en_g; +struct { +unsigned char step; +unsigned char levels; +unsigned char ptidxbits; +unsigned char ptesize; + } sc[2 ]; +/* Translation stage phase */ +enum { +S_STAGE =0 , +G_STAGE =1 , + } pass; +MemTxResult ret; + + satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); + gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + + en_s = satp !=RISCV_IOMMU_DC_FSC_MODE_BARE ;
hw/riscv/riscv-iommu.c line 262
Quoted 10 lines
hw/riscv/riscv-iommu.c new file +10
-0+ en_g = gatp !=RISCV_IOMMU_DC_IOHGATP_MODE_BARE ; + +/* +* Early check for MSI address match when IOVA == GPA. This check +* is required to ensure MSI translation is applied in case +* first-stage translation is set to BARE mode. In this case IOVA +* provided is a valid GPA. Running translation through page walk +* second stage translation will incorrectly try to translate GPA +* to host physical page, likely hitting IOPF. +*/
Why was this comment expanded from the simple
"Early check for MSI address match when IOVA == GPA."
The comment is now incorrect since the check is required even when first-stage translation is not BARE. I just skimmed the spec again trying to figure out if the removal of '!en_s' is a hack or a fix, and I'm inclined to say "fix", but it's an incomplete fix. I found a sentence that says
"If the virtual memory scheme selected for first-stage is Bare but the scheme for the second-stage is not Bare then the IOVA is a GPA."
which, in a way, defines a GPA to only be a GPA when second-stage is used (and all MSI translation specifications refer to GPAs). However, maybe I missed it, but I couldn't find any actual reason that the MSI table can't be used when first-stage is not BARE and second-stage is (and, of course, it makes no difference for single-stage translations to call IOVAs GPAs or not).
Now, I also see
"If the virtual memory scheme selected for neither stage is Bare then the IOVA is a VA. Two-stage address translation is in effect. The first-stage translates the VA to a GPA and the second-stage translates the GPA to a SPA."
in the spec, which means we should probably change the removal of '!en_s' to '!(en_s && en_g)'. VFIO+irqbypass would still work with that and, when Linux learns to support two-stage translation, we wouldn't incorrectly try to check for a GVA in the MSI table.
Thanks,
drew
Re: [PATCH v8 03/12] hw/riscv: add RISC-V IOMMU base emulation
Quoted 76 lines · Andrew Jones
On Tue, Oct 01, 2024 at 10:02:58PM GMT, Daniel Henrique Barboza wrote:
...Quoted 36 lines
hw/riscv/riscv-iommu.c new file +36
-0+/* +* RISCV IOMMU Address Translation Lookup - Page Table Walk +* +* Note: Code is based on get_physical_address() from target/riscv/cpu_helper.c +* Both implementation can be merged into single helper function in future. +* Keeping them separate for now, as error reporting and flow specifics are +* sufficiently different for separate implementation. +* +* @s : IOMMU Device State +* @ctx : Translation context for device id and process address space id. +* @iotlb : translation data: physical address and access mode. +* @return : success or fault cause code. +*/ +static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +IOMMUTLBEntry *iotlb) +{ +dma_addr_t addr, base; +uint64_t satp, gatp, pte; +bool en_s, en_g; +struct { +unsigned char step; +unsigned char levels; +unsigned char ptidxbits; +unsigned char ptesize; + } sc[2 ]; +/* Translation stage phase */ +enum { +S_STAGE =0 , +G_STAGE =1 , + } pass; +MemTxResult ret; + + satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); + gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + + en_s = satp !=RISCV_IOMMU_DC_FSC_MODE_BARE ;hw/riscv/riscv-iommu.c line 262
Quoted 10 lines
hw/riscv/riscv-iommu.c new file +10
-0+ en_g = gatp !=RISCV_IOMMU_DC_IOHGATP_MODE_BARE ; + +/* +* Early check for MSI address match when IOVA == GPA. This check +* is required to ensure MSI translation is applied in case +* first-stage translation is set to BARE mode. In this case IOVA +* provided is a valid GPA. Running translation through page walk +* second stage translation will incorrectly try to translate GPA +* to host physical page, likely hitting IOPF. +*/ Why was this comment expanded from the simple
"Early check for MSI address match when IOVA == GPA."
The comment is now incorrect since the check is required even when first-stage translation is not BARE. I just skimmed the spec again trying to figure out if the removal of '!en_s' is a hack or a fix, and I'm inclined to say "fix", but it's an incomplete fix. I found a sentence that says
"If the virtual memory scheme selected for first-stage is Bare but the scheme for the second-stage is not Bare then the IOVA is a GPA."
which, in a way, defines a GPA to only be a GPA when second-stage is used (and all MSI translation specifications refer to GPAs). However, maybe I missed it, but I couldn't find any actual reason that the MSI table can't be used when first-stage is not BARE and second-stage is (and, of course, it makes no difference for single-stage translations to call IOVAs GPAs or not).
Now, I also see
"If the virtual memory scheme selected for neither stage is Bare then the IOVA is a VA. Two-stage address translation is in effect. The first-stage translates the VA to a GPA and the second-stage translates the GPA to a SPA."
Quoted 4 lines
in the spec, which means we should probably change the removal of '!en_s' to '!(en_s &&
en_g)'. VFIO+irqbypass would still work with that and, when Linux learns to support two-stage translation, we wouldn't incorrectly try to check for a GVA in the MSI table.
Ok. It seems to me that we can't rely on the riscv-iommu spec alone to let us know how to detect if IOVA == GPA, given that one of the main usages we have ATM (VFIO irqbypass) will use GPAs with S-stage enabled.
(Note: shouldn't we open a bug against the riscv-iommu spec?)
I like the idea of ruling out the case where IOVA == VA since that is clear in the spec. We also know that MSI entries will always contains GPAs.
This is the change I'm going to make in v9:
/*
* Early check for MSI address match when IOVA == GPA to
* support VFIO+irqbypass. The riscv-iommu spec doesn't
* consider the case where a GPA can be produced by S-stage
* only, but we have real life examples like Linux VFIO that
* work that way. The spec alone does not provide a reliable
* way of detecting if IOVA == GPA.
*
* The spec is clear about what is a VA: "If the virtual
* memory scheme selected for neither stage is Bare then
* the IOVA is a VA", in our case "(en_s && en_g)". We also
* know that MSI tables will always hold GPAs.
*
* Thus the check consists of ruling out VAs and checking
* the MSI table.
*/
if (!(en_s && en_g) && (iotlb->perm & IOMMU_WO) &&
riscv_iommu_msi_check(s, ctx, iotlb->iova)) { iotlb->target_as = &s->trap_as; iotlb->translated_addr = iotlb->iova; iotlb->addr_mask = ~TARGET_PAGE_MASK;
return 0;
}
Tomasz, let me know if you have any opinions against it. I intend to send the v9 start of next week.
Thanks,
Daniel
Quoted 3 lines
Thanks,
drew
Re: [PATCH v8 03/12] hw/riscv: add RISC-V IOMMU base emulation
Quoted 84 lines · Daniel Henrique Barboza
Quoted 76 lines · Andrew Jones
On Tue, Oct 01, 2024 at 10:02:58PM GMT, Daniel Henrique Barboza wrote:
...Quoted 36 lines
hw/riscv/riscv-iommu.c new file +36
-0+/* +* RISCV IOMMU Address Translation Lookup - Page Table Walk +* +* Note: Code is based on get_physical_address() from target/riscv/cpu_helper.c +* Both implementation can be merged into single helper function in future. +* Keeping them separate for now, as error reporting and flow specifics are +* sufficiently different for separate implementation. +* +* @s : IOMMU Device State +* @ctx : Translation context for device id and process address space id. +* @iotlb : translation data: physical address and access mode. +* @return : success or fault cause code. +*/ +static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +IOMMUTLBEntry *iotlb) +{ +dma_addr_t addr, base; +uint64_t satp, gatp, pte; +bool en_s, en_g; +struct { +unsigned char step; +unsigned char levels; +unsigned char ptidxbits; +unsigned char ptesize; + } sc[2 ]; +/* Translation stage phase */ +enum { +S_STAGE =0 , +G_STAGE =1 , + } pass; +MemTxResult ret; + + satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); + gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + + en_s = satp !=RISCV_IOMMU_DC_FSC_MODE_BARE ;hw/riscv/riscv-iommu.c line 262
Quoted 10 lines
hw/riscv/riscv-iommu.c new file +10
-0+ en_g = gatp !=RISCV_IOMMU_DC_IOHGATP_MODE_BARE ; + +/* +* Early check for MSI address match when IOVA == GPA. This check +* is required to ensure MSI translation is applied in case +* first-stage translation is set to BARE mode. In this case IOVA +* provided is a valid GPA. Running translation through page walk +* second stage translation will incorrectly try to translate GPA +* to host physical page, likely hitting IOPF. +*/ Why was this comment expanded from the simple
"Early check for MSI address match when IOVA == GPA."
The comment is now incorrect since the check is required even when first-stage translation is not BARE. I just skimmed the spec again trying to figure out if the removal of '!en_s' is a hack or a fix, and I'm inclined to say "fix", but it's an incomplete fix. I found a sentence that says
"If the virtual memory scheme selected for first-stage is Bare but the scheme for the second-stage is not Bare then the IOVA is a GPA."
which, in a way, defines a GPA to only be a GPA when second-stage is used (and all MSI translation specifications refer to GPAs). However, maybe I missed it, but I couldn't find any actual reason that the MSI table can't be used when first-stage is not BARE and second-stage is (and, of course, it makes no difference for single-stage translations to call IOVAs GPAs or not).
Now, I also see
"If the virtual memory scheme selected for neither stage is Bare then the IOVA is a VA. Two-stage address translation is in effect. The first-stage translates the VA to a GPA and the second-stage translates the GPA to a SPA."
Quoted 4 lines
in the spec, which means we should probably change the removal of '!en_s' to '!(en_s &&
en_g)'. VFIO+irqbypass would still work with that and, when Linux learns to support two-stage translation, we wouldn't incorrectly try to check for a GVA in the MSI table.
Quoted 6 lines
Ok. It seems to me that we can't rely on the riscv-iommu spec alone to let us know how to detect if IOVA == GPA, given that one of the main usages we have ATM (VFIO irqbypass) will use GPAs with S-stage enabled.
(Note: shouldn't we open a bug against the riscv-iommu spec?)
I can try writing a sentence or two for the spec to clarify this and send a PR.
Quoted 21 lines
I like the idea of ruling out the case where IOVA == VA since that is clear in the spec. We also know that MSI entries will always contains GPAs.
This is the change I'm going to make in v9:
/*
* Early check for MSI address match when IOVA == GPA to
* support VFIO+irqbypass. The riscv-iommu spec doesn't
* consider the case where a GPA can be produced by S-stage
* only, but we have real life examples like Linux VFIO that
* work that way. The spec alone does not provide a reliable
* way of detecting if IOVA == GPA.
*
* The spec is clear about what is a VA: "If the virtual
* memory scheme selected for neither stage is Bare then
* the IOVA is a VA", in our case "(en_s&&en_g)". We also
* know that MSI tables will always hold GPAs.
*
* Thus the check consists of ruling out VAs and checking
* the MSI table.
Quoted 8 lines
*/
if (!(en_s &&en_g) && (iotlb->perm&IOMMU_WO) &&
riscv_iommu_msi_check(s, ctx, iotlb->iova)) {iotlb->target_as=&s->trap_as;iotlb->translated_addr=iotlb->iova;iotlb->addr_mask= ~TARGET_PAGE_MASK; return 0;
}
LGTM
Thanks,
drew
Quoted 14 lines
Tomasz, let me know if you have any opinions against it. I intend to send the v9 start of next week.
Thanks,
Daniel
Quoted 3 lines
Thanks,
drew
Re: [PATCH v8 03/12] hw/riscv: add RISC-V IOMMU base emulation
In reply to Daniel Henrique BarbozaQuoted 89 lines · Daniel Henrique Barboza
Quoted 76 lines · Andrew Jones
On Tue, Oct 01, 2024 at 10:02:58PM GMT, Daniel Henrique Barboza wrote:
...Quoted 36 lines
hw/riscv/riscv-iommu.c new file +36
-0+/* +* RISCV IOMMU Address Translation Lookup - Page Table Walk +* +* Note: Code is based on get_physical_address() from target/riscv/cpu_helper.c +* Both implementation can be merged into single helper function in future. +* Keeping them separate for now, as error reporting and flow specifics are +* sufficiently different for separate implementation. +* +* @s : IOMMU Device State +* @ctx : Translation context for device id and process address space id. +* @iotlb : translation data: physical address and access mode. +* @return : success or fault cause code. +*/ +static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +IOMMUTLBEntry *iotlb) +{ +dma_addr_t addr, base; +uint64_t satp, gatp, pte; +bool en_s, en_g; +struct { +unsigned char step; +unsigned char levels; +unsigned char ptidxbits; +unsigned char ptesize; + } sc[2 ]; +/* Translation stage phase */ +enum { +S_STAGE =0 , +G_STAGE =1 , + } pass; +MemTxResult ret; + + satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); + gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + + en_s = satp !=RISCV_IOMMU_DC_FSC_MODE_BARE ;hw/riscv/riscv-iommu.c line 262
Quoted 10 lines
hw/riscv/riscv-iommu.c new file +10
-0+ en_g = gatp !=RISCV_IOMMU_DC_IOHGATP_MODE_BARE ; + +/* +* Early check for MSI address match when IOVA == GPA. This check +* is required to ensure MSI translation is applied in case +* first-stage translation is set to BARE mode. In this case IOVA +* provided is a valid GPA. Running translation through page walk +* second stage translation will incorrectly try to translate GPA +* to host physical page, likely hitting IOPF. +*/ Why was this comment expanded from the simple
"Early check for MSI address match when IOVA == GPA."
The comment is now incorrect since the check is required even when first-stage translation is not BARE. I just skimmed the spec again trying to figure out if the removal of '!en_s' is a hack or a fix, and I'm inclined to say "fix", but it's an incomplete fix. I found a sentence that says
"If the virtual memory scheme selected for first-stage is Bare but the scheme for the second-stage is not Bare then the IOVA is a GPA."
which, in a way, defines a GPA to only be a GPA when second-stage is used (and all MSI translation specifications refer to GPAs). However, maybe I missed it, but I couldn't find any actual reason that the MSI table can't be used when first-stage is not BARE and second-stage is (and, of course, it makes no difference for single-stage translations to call IOVAs GPAs or not).
Now, I also see
"If the virtual memory scheme selected for neither stage is Bare then the IOVA is a VA. Two-stage address translation is in effect. The first-stage translates the VA to a GPA and the second-stage translates the GPA to a SPA."
Quoted 4 lines
in the spec, which means we should probably change the removal of '!en_s' to '!(en_s &&
en_g)'. VFIO+irqbypass would still work with that and, when Linux learns to support two-stage translation, we wouldn't incorrectly try to check for a GVA in the MSI table.Ok. It seems to me that we can't rely on the riscv-iommu spec alone to let us know how to detect if IOVA == GPA, given that one of the main usages we have ATM (VFIO irqbypass) will use GPAs with S-stage enabled.
Quoted 8 lines
(Note: shouldn't we open a bug against the riscv-iommu spec?)
I like the idea of ruling out the case where IOVA == VA since that is clear in the spec. We also know that MSI entries will always contains GPAs.
This is the change I'm going to make in v9:
It might be worth it to quote spec definitions of IOVA, GPA, SPA.
IOVA: I/O Virtual Address: Virtual address for DMA by devices
GPA: Guest Physical Address: An address in the virtualized physical memory space of a virtual machine.
SPA: Supervisor Physical Address: Physical address used to access memory and memory-mapped resources.
From IOMMU specification it's pretty clear, an untranslated address is always an IOVA.
If you look at the introduction section and section 2.3. "Process to translate an IOVA", translation (if enabled by the device/process context) to GPA, SPA is expressed as:
step #17: GPA = FSC[IOVA]
step #19: SPA = IOHGATP[GPA]
With that, MSI address translations always use GPA as an input (section 2.3.3. "Process to translate addresses of MSIs"), and is done as step #18 of the IOVA translation.
And now:
If FSC.Mode == Bare, step #17 translation is simple GPA = IOVA If IOHGATP.Mode == Bare, step #19 translation is simple SPA = GPA
If both FSC and IOHGATP modes are bare, no address translation is performed, and SPA = GPA = IOVA. However if "MSI page tables are enabled, then the MSI address translation process specified in Section 2.3.3 is invoked". This was the reason to put the riscv_iommu_msi_check() function before check for pass-through mode check.
If FSC.Mode == Bare and untranslated address given to the function riscv_iommu_spa_fetch() is an GPA = IOVA. It could also be SPA if IOHGATP.Mode == Bare, but it is irrelevant for the MSI address translation. That is the reason to put only the check for !en_s in before riscv_iommu_msi_check() call.
I'm aware that VFIO uses S-Stage translation only, but it does not change the hardware logic. For such cases VFIO can provide IMSIC register mappings in S-Stage PT to emulate MSI doorbell registers (if needed). When S-Stage translation is used, an untranslated address processed by the IOMMU hardware is still an IOVA, even if the guest OS / VFIO user treats it as PA.
Modification of the check to include en_g ( !(en_s && en_g) && ,,, ) will incorrectly enable MSI address translation for an IOVA address (as defined in IOMMU spec) if G-Stage translation is set to Bare mode.
I'd suggest to keep the s/g stage checks here as:
if ((iotlb->perm & IOMMU_WO) && !en_s &&
riscv_iommu_msi_check(s, ctx, iotlb->iova)) {
...
}Hope that helps this discussion.
Best,
- Tomasz
Quoted 37 lines
/*
* Early check for MSI address match when IOVA == GPA to
* support VFIO+irqbypass. The riscv-iommu spec doesn't
* consider the case where a GPA can be produced by S-stage
* only, but we have real life examples like Linux VFIO that
* work that way. The spec alone does not provide a reliable
* way of detecting if IOVA == GPA.
*
* The spec is clear about what is a VA: "If the virtual
* memory scheme selected for neither stage is Bare then
* the IOVA is a VA", in our case "(en_s&&en_g)". We also
* know that MSI tables will always hold GPAs.
*
* Thus the check consists of ruling out VAs and checking
* the MSI table.
*/
if (!(en_s &&en_g) && (iotlb->perm&IOMMU_WO) &&
riscv_iommu_msi_check(s, ctx, iotlb->iova)) {iotlb->target_as=&s->trap_as;iotlb->translated_addr=iotlb->iova;iotlb->addr_mask= ~TARGET_PAGE_MASK;
return 0;
}Tomasz, let me know if you have any opinions against it. I intend to send the v9 start of next week.
Thanks,
Daniel
Quoted 3 lines
Thanks,
drew
Re: [PATCH v8 03/12] hw/riscv: add RISC-V IOMMU base emulation
Quoted 139 lines · Tomasz Jeznach
Quoted 89 lines · Daniel Henrique Barboza
Quoted 76 lines · Andrew Jones
On Tue, Oct 01, 2024 at 10:02:58PM GMT, Daniel Henrique Barboza wrote:
...Quoted 36 lines
hw/riscv/riscv-iommu.c new file +36
-0+/* +* RISCV IOMMU Address Translation Lookup - Page Table Walk +* +* Note: Code is based on get_physical_address() from target/riscv/cpu_helper.c +* Both implementation can be merged into single helper function in future. +* Keeping them separate for now, as error reporting and flow specifics are +* sufficiently different for separate implementation. +* +* @s : IOMMU Device State +* @ctx : Translation context for device id and process address space id. +* @iotlb : translation data: physical address and access mode. +* @return : success or fault cause code. +*/ +static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +IOMMUTLBEntry *iotlb) +{ +dma_addr_t addr, base; +uint64_t satp, gatp, pte; +bool en_s, en_g; +struct { +unsigned char step; +unsigned char levels; +unsigned char ptidxbits; +unsigned char ptesize; + } sc[2 ]; +/* Translation stage phase */ +enum { +S_STAGE =0 , +G_STAGE =1 , + } pass; +MemTxResult ret; + + satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); + gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + + en_s = satp !=RISCV_IOMMU_DC_FSC_MODE_BARE ;hw/riscv/riscv-iommu.c line 262
Quoted 10 lines
hw/riscv/riscv-iommu.c new file +10
-0+ en_g = gatp !=RISCV_IOMMU_DC_IOHGATP_MODE_BARE ; + +/* +* Early check for MSI address match when IOVA == GPA. This check +* is required to ensure MSI translation is applied in case +* first-stage translation is set to BARE mode. In this case IOVA +* provided is a valid GPA. Running translation through page walk +* second stage translation will incorrectly try to translate GPA +* to host physical page, likely hitting IOPF. +*/ Why was this comment expanded from the simple
"Early check for MSI address match when IOVA == GPA."
The comment is now incorrect since the check is required even when first-stage translation is not BARE. I just skimmed the spec again trying to figure out if the removal of '!en_s' is a hack or a fix, and I'm inclined to say "fix", but it's an incomplete fix. I found a sentence that says
"If the virtual memory scheme selected for first-stage is Bare but the scheme for the second-stage is not Bare then the IOVA is a GPA."
which, in a way, defines a GPA to only be a GPA when second-stage is used (and all MSI translation specifications refer to GPAs). However, maybe I missed it, but I couldn't find any actual reason that the MSI table can't be used when first-stage is not BARE and second-stage is (and, of course, it makes no difference for single-stage translations to call IOVAs GPAs or not).
Now, I also see
"If the virtual memory scheme selected for neither stage is Bare then the IOVA is a VA. Two-stage address translation is in effect. The first-stage translates the VA to a GPA and the second-stage translates the GPA to a SPA."
Quoted 4 lines
in the spec, which means we should probably change the removal of '!en_s' to '!(en_s &&
en_g)'. VFIO+irqbypass would still work with that and, when Linux learns to support two-stage translation, we wouldn't incorrectly try to check for a GVA in the MSI table.Ok. It seems to me that we can't rely on the riscv-iommu spec alone to let us know how to detect if IOVA == GPA, given that one of the main usages we have ATM (VFIO irqbypass) will use GPAs with S-stage enabled.
Quoted 8 lines
(Note: shouldn't we open a bug against the riscv-iommu spec?)
I like the idea of ruling out the case where IOVA == VA since that is clear in the spec. We also know that MSI entries will always contains GPAs.
This is the change I'm going to make in v9:
It might be worth it to quote spec definitions of IOVA, GPA, SPA.
IOVA: I/O Virtual Address: Virtual address for DMA by devices
GPA: Guest Physical Address: An address in the virtualized physical memory space of a virtual machine.
SPA: Supervisor Physical Address: Physical address used to access memory and memory-mapped resources.From IOMMU specification it's pretty clear, an untranslated address is always an IOVA.
If you look at the introduction section and section 2.3. "Process to translate an IOVA", translation (if enabled by the device/process context) to GPA, SPA is expressed as:
step #17: GPA = FSC[IOVA]
step #19: SPA = IOHGATP[GPA]With that, MSI address translations always use GPA as an input (section 2.3.3. "Process to translate addresses of MSIs"), and is done as step #18 of the IOVA translation.
And now:
If FSC.Mode == Bare, step #17 translation is simple GPA = IOVA If IOHGATP.Mode == Bare, step #19 translation is simple SPA = GPAIf both FSC and IOHGATP modes are bare, no address translation is performed, and SPA = GPA = IOVA. However if "MSI page tables are enabled, then the MSI address translation process specified in Section 2.3.3 is invoked". This was the reason to put the
riscv_iommu_msi_check()function before check for pass-through mode check.If FSC.Mode == Bare and untranslated address given to the function
riscv_iommu_spa_fetch()is an GPA = IOVA. It could also be SPA if IOHGATP.Mode == Bare, but it is irrelevant for the MSI address translation. That is the reason to put only the check for !en_s in beforeriscv_iommu_msi_check()call.
Quoted 6 lines
I'm aware that VFIO uses S-Stage translation only, but it does not change the hardware logic. For such cases VFIO can provide IMSIC register mappings in S-Stage PT to emulate MSI doorbell registers (if needed). When S-Stage translation is used, an untranslated address processed by the IOMMU hardware is still an IOVA, even if the guest OS / VFIO user treats it as PA.
This would work for guest interrupt file mappings, but MRIFs won't work and the note of the "Device contexts at an IOMMU" section of the AIA spec points out that VCPU migration may become more difficult to manage.
Quoted 5 lines
Modification of the check to include
en_g( !(en_s &&en_g) && ,,, ) will incorrectly enable MSI address translation for an IOVA address (as defined in IOMMU spec) if G-Stage translation is set to Bare mode.
Quoted 7 lines
I'd suggest to keep the s/g stage checks here as:
if ((iotlb->perm & IOMMU_WO) && !en_s && riscv_iommu_msi_check(s, ctx, iotlb->iova)) { ... }Hope that helps this discussion.
Yes, it certainly helps, thank you. It points out that step 17 of "Process to translate an IOVA" produces a GPA which is consumed by step 18 for the MSI translation (and there's no other way to get to step 18). OIOW, we must do a first-stage translation before we check the MSI page table, which means we can't do an early MSI check unless we have strictly !en_s.
This is unfortunate, considering the current state of VFIO+irqbypass and the IOMMU driver. I also wonder if it's really necessary. It seems to me that the MSI page table should always be checked first when either S-stage or G-stage is Bare (so there's only a single-stage of translation) and msiptp.MODE is not Off. I can't see what harm could come from that and it would give system software the ability to leverage code that already knows how to manage single-stage IOMMUs for device assignment.
Thanks,
drew
Re: [PATCH v8 03/12] hw/riscv: add RISC-V IOMMU base emulation
Quoted 170 lines · Andrew Jones
Quoted 139 lines · Tomasz Jeznach
Quoted 89 lines · Daniel Henrique Barboza
Quoted 76 lines · Andrew Jones
On Tue, Oct 01, 2024 at 10:02:58PM GMT, Daniel Henrique Barboza wrote:
...Quoted 36 lines
hw/riscv/riscv-iommu.c new file +36
-0+/* +* RISCV IOMMU Address Translation Lookup - Page Table Walk +* +* Note: Code is based on get_physical_address() from target/riscv/cpu_helper.c +* Both implementation can be merged into single helper function in future. +* Keeping them separate for now, as error reporting and flow specifics are +* sufficiently different for separate implementation. +* +* @s : IOMMU Device State +* @ctx : Translation context for device id and process address space id. +* @iotlb : translation data: physical address and access mode. +* @return : success or fault cause code. +*/ +static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +IOMMUTLBEntry *iotlb) +{ +dma_addr_t addr, base; +uint64_t satp, gatp, pte; +bool en_s, en_g; +struct { +unsigned char step; +unsigned char levels; +unsigned char ptidxbits; +unsigned char ptesize; + } sc[2 ]; +/* Translation stage phase */ +enum { +S_STAGE =0 , +G_STAGE =1 , + } pass; +MemTxResult ret; + + satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); + gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + + en_s = satp !=RISCV_IOMMU_DC_FSC_MODE_BARE ;hw/riscv/riscv-iommu.c line 262
Quoted 10 lines
hw/riscv/riscv-iommu.c new file +10
-0+ en_g = gatp !=RISCV_IOMMU_DC_IOHGATP_MODE_BARE ; + +/* +* Early check for MSI address match when IOVA == GPA. This check +* is required to ensure MSI translation is applied in case +* first-stage translation is set to BARE mode. In this case IOVA +* provided is a valid GPA. Running translation through page walk +* second stage translation will incorrectly try to translate GPA +* to host physical page, likely hitting IOPF. +*/ Why was this comment expanded from the simple
"Early check for MSI address match when IOVA == GPA."
The comment is now incorrect since the check is required even when first-stage translation is not BARE. I just skimmed the spec again trying to figure out if the removal of '!en_s' is a hack or a fix, and I'm inclined to say "fix", but it's an incomplete fix. I found a sentence that says
"If the virtual memory scheme selected for first-stage is Bare but the scheme for the second-stage is not Bare then the IOVA is a GPA."
which, in a way, defines a GPA to only be a GPA when second-stage is used (and all MSI translation specifications refer to GPAs). However, maybe I missed it, but I couldn't find any actual reason that the MSI table can't be used when first-stage is not BARE and second-stage is (and, of course, it makes no difference for single-stage translations to call IOVAs GPAs or not).
Now, I also see
"If the virtual memory scheme selected for neither stage is Bare then the IOVA is a VA. Two-stage address translation is in effect. The first-stage translates the VA to a GPA and the second-stage translates the GPA to a SPA."
Quoted 4 lines
in the spec, which means we should probably change the removal of '!en_s' to '!(en_s &&
en_g)'. VFIO+irqbypass would still work with that and, when Linux learns to support two-stage translation, we wouldn't incorrectly try to check for a GVA in the MSI table.Ok. It seems to me that we can't rely on the riscv-iommu spec alone to let us know how to detect if IOVA == GPA, given that one of the main usages we have ATM (VFIO irqbypass) will use GPAs with S-stage enabled.
Quoted 8 lines
(Note: shouldn't we open a bug against the riscv-iommu spec?)
I like the idea of ruling out the case where IOVA == VA since that is clear in the spec. We also know that MSI entries will always contains GPAs.
This is the change I'm going to make in v9:
It might be worth it to quote spec definitions of IOVA, GPA, SPA.
IOVA: I/O Virtual Address: Virtual address for DMA by devices
GPA: Guest Physical Address: An address in the virtualized physical memory space of a virtual machine.
SPA: Supervisor Physical Address: Physical address used to access memory and memory-mapped resources.From IOMMU specification it's pretty clear, an untranslated address is
always an IOVA.If you look at the introduction section and section 2.3. "Process to translate an IOVA", translation (if enabled by the device/process context) to GPA, SPA is expressed as:
step #17: GPA = FSC[IOVA]
step #19: SPA = IOHGATP[GPA]With that, MSI address translations always use GPA as an input (section 2.3.3. "Process to translate addresses of MSIs"), and is done as step #18 of the IOVA translation.
And now:
If FSC.Mode == Bare, step #17 translation is simple GPA = IOVA If IOHGATP.Mode == Bare, step #19 translation is simple SPA = GPAIf both FSC and IOHGATP modes are bare, no address translation is performed, and SPA = GPA = IOVA. However if "MSI page tables are enabled, then the MSI address translation process specified in Section 2.3.3 is invoked". This was the reason to put the
riscv_iommu_msi_check()function before check for pass-through mode check.If FSC.Mode == Bare and untranslated address given to the function
riscv_iommu_spa_fetch()is an GPA = IOVA. It could also be SPA if IOHGATP.Mode == Bare, but it is irrelevant for the MSI address translation. That is the reason to put only the check for !en_s in beforeriscv_iommu_msi_check()call.Quoted 6 lines
I'm aware that VFIO uses S-Stage translation only, but it does not change the hardware logic. For such cases VFIO can provide IMSIC register mappings in S-Stage PT to emulate MSI doorbell registers (if needed). When S-Stage translation is used, an untranslated address processed by the IOMMU hardware is still an IOVA, even if the guest OS / VFIO user treats it as PA.
This would work for guest interrupt file mappings, but MRIFs won't work and the note of the "Device contexts at an IOMMU" section of the AIA spec points out that VCPU migration may become more difficult to manage.
Quoted 5 lines
Modification of the check to include
en_g( !(en_s &&en_g) && ,,, ) will incorrectly enable MSI address translation for an IOVA address (as defined in IOMMU spec) if G-Stage translation is set to Bare mode.Quoted 7 lines
I'd suggest to keep the s/g stage checks here as:
if ((iotlb->perm & IOMMU_WO) && !en_s && riscv_iommu_msi_check(s, ctx, iotlb->iova)) { ... }Hope that helps this discussion.
Yes, it certainly helps, thank you. It points out that step 17 of "Process to translate an IOVA" produces a GPA which is consumed by step 18 for the MSI translation (and there's no other way to get to step 18). OIOW, we must do a first-stage translation before we check the MSI page table, which means we can't do an early MSI check unless we have strictly !en_s.
Quoted 7 lines
This is unfortunate, considering the current state of VFIO+irqbypass and the IOMMU driver. I also wonder if it's really necessary. It seems to me that the MSI page table should always be checked first when either S-stage or G-stage is Bare (so there's only a single-stage of translation) and msiptp.MODE is not Off. I can't see what harm could come from that and it would give system software the ability to leverage code that already knows how to manage single-stage IOMMUs for device assignment.
I agree with all of that. But at the end of the day we need to adhere to the HW spec as closely as we can, and it's now clear that the assumptions we were making in this particular check, for the sake of VFIO+irqbypass support, is sort of spec violation.
I still believe this is something that we would like to discuss in the spec space, but for now we don't have much to do w.r.t the emulation. If we want VFIO+irqbypass to work the Linux driver will need to do extra work.
This is what I'll do for v9:
/*
* Early check for MSI address match when IOVA == GPA.
* Note that the (!en_s) condition means that the MSI
* page table may only be used when guest pages are
* mapped using the g-stage page table, whether single-
* or two-stage paging is enabled. It's unavoidable though,
* because the spec mandates that we do a first-stage
* translation before we check the MSI page table, which
* means we can't do an early MSI check unless we have
* strictly !en_s.
*/
if (!en_s && (iotlb->perm & IOMMU_WO) &&
riscv_iommu_msi_check(s, ctx, iotlb->iova)) { iotlb->target_as = &s->trap_as; iotlb->translated_addr = iotlb->iova; iotlb->addr_mask = ~TARGET_PAGE_MASK;
return 0;
}
It also came to my attention today that the kernel support is waiting for the QEMU pieces to land due to the Red Hat PCI ID we're adding. I'll post v9 later today or next Monday to see if we can speed things up.
Thanks,
Daniel
Quoted 3 lines
Thanks,
drew
Re: [PATCH v8 03/12] hw/riscv: add RISC-V IOMMU base emulation
Quoted 213 lines · Daniel Henrique Barboza
Quoted 170 lines · Andrew Jones
Quoted 139 lines · Tomasz Jeznach
Quoted 89 lines · Daniel Henrique Barboza
Quoted 76 lines · Andrew Jones
On Tue, Oct 01, 2024 at 10:02:58PM GMT, Daniel Henrique Barboza wrote:
...Quoted 36 lines
hw/riscv/riscv-iommu.c new file +36
-0+/* +* RISCV IOMMU Address Translation Lookup - Page Table Walk +* +* Note: Code is based on get_physical_address() from target/riscv/cpu_helper.c +* Both implementation can be merged into single helper function in future. +* Keeping them separate for now, as error reporting and flow specifics are +* sufficiently different for separate implementation. +* +* @s : IOMMU Device State +* @ctx : Translation context for device id and process address space id. +* @iotlb : translation data: physical address and access mode. +* @return : success or fault cause code. +*/ +static int riscv_iommu_spa_fetch (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, +IOMMUTLBEntry *iotlb) +{ +dma_addr_t addr, base; +uint64_t satp, gatp, pte; +bool en_s, en_g; +struct { +unsigned char step; +unsigned char levels; +unsigned char ptidxbits; +unsigned char ptesize; + } sc[2 ]; +/* Translation stage phase */ +enum { +S_STAGE =0 , +G_STAGE =1 , + } pass; +MemTxResult ret; + + satp =get_field (ctx->satp,RISCV_IOMMU_ATP_MODE_FIELD ); + gatp =get_field (ctx->gatp,RISCV_IOMMU_ATP_MODE_FIELD ); + + en_s = satp !=RISCV_IOMMU_DC_FSC_MODE_BARE ;hw/riscv/riscv-iommu.c line 262
Quoted 10 lines
hw/riscv/riscv-iommu.c new file +10
-0+ en_g = gatp !=RISCV_IOMMU_DC_IOHGATP_MODE_BARE ; + +/* +* Early check for MSI address match when IOVA == GPA. This check +* is required to ensure MSI translation is applied in case +* first-stage translation is set to BARE mode. In this case IOVA +* provided is a valid GPA. Running translation through page walk +* second stage translation will incorrectly try to translate GPA +* to host physical page, likely hitting IOPF. +*/ Why was this comment expanded from the simple
"Early check for MSI address match when IOVA == GPA."
The comment is now incorrect since the check is required even when first-stage translation is not BARE. I just skimmed the spec again trying to figure out if the removal of '!en_s' is a hack or a fix, and I'm inclined to say "fix", but it's an incomplete fix. I found a sentence that says
"If the virtual memory scheme selected for first-stage is Bare but the scheme for the second-stage is not Bare then the IOVA is a GPA."
which, in a way, defines a GPA to only be a GPA when second-stage is used (and all MSI translation specifications refer to GPAs). However, maybe I missed it, but I couldn't find any actual reason that the MSI table can't be used when first-stage is not BARE and second-stage is (and, of course, it makes no difference for single-stage translations to call IOVAs GPAs or not).
Now, I also see
"If the virtual memory scheme selected for neither stage is Bare then the IOVA is a VA. Two-stage address translation is in effect. The first-stage translates the VA to a GPA and the second-stage translates the GPA to a SPA."
Quoted 4 lines
in the spec, which means we should probably change the removal of '!en_s' to '!(en_s &&
en_g)'. VFIO+irqbypass would still work with that and, when Linux learns to support two-stage translation, we wouldn't incorrectly try to check for a GVA in the MSI table.Ok. It seems to me that we can't rely on the riscv-iommu spec alone to let us know how to detect if IOVA == GPA, given that one of the main usages we have ATM (VFIO irqbypass) will use GPAs with S-stage enabled.
Quoted 8 lines
(Note: shouldn't we open a bug against the riscv-iommu spec?)
I like the idea of ruling out the case where IOVA == VA since that is clear in the spec. We also know that MSI entries will always contains GPAs.
This is the change I'm going to make in v9:
It might be worth it to quote spec definitions of IOVA, GPA, SPA.
IOVA: I/O Virtual Address: Virtual address for DMA by devices
GPA: Guest Physical Address: An address in the virtualized physical memory space of a virtual machine.
SPA: Supervisor Physical Address: Physical address used to access memory and memory-mapped resources.From IOMMU specification it's pretty clear, an untranslated address is
always an IOVA.If you look at the introduction section and section 2.3. "Process to translate an IOVA", translation (if enabled by the device/process context) to GPA, SPA is expressed as:
step #17: GPA = FSC[IOVA]
step #19: SPA = IOHGATP[GPA]With that, MSI address translations always use GPA as an input (section 2.3.3. "Process to translate addresses of MSIs"), and is done as step #18 of the IOVA translation.
And now:
If FSC.Mode == Bare, step #17 translation is simple GPA = IOVA If IOHGATP.Mode == Bare, step #19 translation is simple SPA = GPAIf both FSC and IOHGATP modes are bare, no address translation is performed, and SPA = GPA = IOVA. However if "MSI page tables are enabled, then the MSI address translation process specified in Section 2.3.3 is invoked". This was the reason to put the
riscv_iommu_msi_check()function before check for pass-through mode check.If FSC.Mode == Bare and untranslated address given to the function
riscv_iommu_spa_fetch()is an GPA = IOVA. It could also be SPA if IOHGATP.Mode == Bare, but it is irrelevant for the MSI address translation. That is the reason to put only the check for !en_s in beforeriscv_iommu_msi_check()call.Quoted 6 lines
I'm aware that VFIO uses S-Stage translation only, but it does not change the hardware logic. For such cases VFIO can provide IMSIC register mappings in S-Stage PT to emulate MSI doorbell registers (if needed). When S-Stage translation is used, an untranslated address processed by the IOMMU hardware is still an IOVA, even if the guest OS / VFIO user treats it as PA.
This would work for guest interrupt file mappings, but MRIFs won't work and the note of the "Device contexts at an IOMMU" section of the AIA spec points out that VCPU migration may become more difficult to manage.
Quoted 5 lines
Modification of the check to include
en_g( !(en_s &&en_g) && ,,, ) will incorrectly enable MSI address translation for an IOVA address (as defined in IOMMU spec) if G-Stage translation is set to Bare mode.Quoted 7 lines
I'd suggest to keep the s/g stage checks here as:
if ((iotlb->perm & IOMMU_WO) && !en_s && riscv_iommu_msi_check(s, ctx, iotlb->iova)) { ... }Hope that helps this discussion.
Yes, it certainly helps, thank you. It points out that step 17 of "Process to translate an IOVA" produces a GPA which is consumed by step 18 for the MSI translation (and there's no other way to get to step 18). OIOW, we must do a first-stage translation before we check the MSI page table, which means we can't do an early MSI check unless we have strictly !en_s.
Quoted 7 lines
This is unfortunate, considering the current state of VFIO+irqbypass and the IOMMU driver. I also wonder if it's really necessary. It seems to me that the MSI page table should always be checked first when either S-stage or G-stage is Bare (so there's only a single-stage of translation) and msiptp.MODE is not Off. I can't see what harm could come from that and it would give system software the ability to leverage code that already knows how to manage single-stage IOMMUs for device assignment.
I agree with all of that. But at the end of the day we need to adhere to the HW spec as closely as we can, and it's now clear that the assumptions we were making in this particular check, for the sake of VFIO+irqbypass support, is sort of spec violation.
I still believe this is something that we would like to discuss in the spec space, but for now we don't have much to do w.r.t the emulation. If we want VFIO+irqbypass to work the Linux driver will need to do extra work.
This is what I'll do for v9:
/*
* Early check for MSI address match when IOVA == GPA.
* Note that the (!en_s) condition means that the MSI
* page table may only be used when guest pages are
* mapped using the g-stage page table, whether single-
* or two-stage paging is enabled. It's unavoidable though,
* because the spec mandates that we do a first-stage
* translation before we check the MSI page table, which
* means we can't do an early MSI check unless we have
* strictly !en_s.
*/
if (!en_s && (iotlb->perm&IOMMU_WO) &&
riscv_iommu_msi_check(s, ctx, iotlb->iova)) {iotlb->target_as=&s->trap_as;iotlb->translated_addr=iotlb->iova;iotlb->addr_mask= ~TARGET_PAGE_MASK;
return 0;
}
Quoted 4 lines
It also came to my attention today that the kernel support is waiting for the QEMU pieces to land due to the Red Hat PCI ID we're adding. I'll post v9 later today or next Monday to see if we can speed things up.
Thanks. I'll be sending out kernel iommu/riscv v9 patches later today. Updated PCI IDs and few minor bugs fixed,
All re-tested with QEMU riscv/iommu-v9 (fetched from github).
Best,
- Tomasz
Quoted 12 lines
Thanks,
Daniel
Quoted 3 lines
Thanks,
drew
PATCH v8 4/12 pci-ids.rst: add Red Hat pci-id for RISC-V IOMMU device
In reply to Daniel Henrique BarbozaThe RISC-V IOMMU PCI device we're going to add next is a reference implementation of the riscv-iommu spec [1], which predicts that the IOMMU can be implemented as a PCIe device.
However, RISC-V International (RVI), the entity that ratified the riscv-iommu spec, didn't bother assigning a PCI ID for this IOMMU PCIe implementation that the spec predicts. This puts us in an uncommon situation because we want to add the reference IOMMU PCIe implementation but we don't have a PCI ID for it.
Given that RVI doesn't provide a PCI ID for it we reached out to Red Hat and Gerd Hoffman, and they were kind enough to give us a PCI ID for the RISC-V IOMMU PCI reference device.
Thanks Red Hat and Gerd for this RISC-V IOMMU PCIe device ID.
[1] https://github.com/riscv-non-isa/riscv-iommu/releases/tag/v1.0.0
--- docs/specs/pci-ids.rst | 2 ++ include/hw/pci/pci.h | 1 + 2 files changed, 3 insertions(+)
docs/specs/pci-ids.rst +2 -0
@@ -98,6 +98,8 @@ PCI devices (other than virtio): PCI ACPI ERST device (``-device acpi-erst``) 1b36:0013 PCI UFS device (``-device ufs``) +1b36:0014 + PCI RISC-V IOMMU device All these devices are documented in :doc:`index`.
include/hw/pci/pci.h +1 -0
@@ -116,6 +116,7 @@ extern bool pci_available;#define PCI_DEVICE_ID_REDHAT_PVPANIC 0x0011#define PCI_DEVICE_ID_REDHAT_ACPI_ERST 0x0012#define PCI_DEVICE_ID_REDHAT_UFS 0x0013 +#define PCI_DEVICE_ID_REDHAT_RISCV_IOMMU 0x0014#define PCI_DEVICE_ID_REDHAT_QXL 0x0100#define FMT_PCIBUS PRIx64
Signature · 1 line
2.45.2
PATCH v8 5/12 hw/riscv: add riscv-iommu-pci reference device
In reply to Daniel Henrique BarbozaFrom: Tomasz Jeznach <[email protected]>
The RISC-V IOMMU can be modelled as a PCIe device following the guidelines of the RISC-V IOMMU spec, chapter 7.1, "Integrating an IOMMU as a PCIe device".
--- hw/riscv/meson.build | 2 +- hw/riscv/riscv-iommu-pci.c | 202 +++++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 hw/riscv/riscv-iommu-pci.c
hw/riscv/meson.build +1 -1
@@ -10,6 +10,6 @@ riscv_ss.add(when: 'CONFIG_SIFIVE_U', if_true: files('sifive_u.c')) riscv_ss.add(when: 'CONFIG_SPIKE', if_true: files('spike.c')) riscv_ss.add(when: 'CONFIG_MICROCHIP_PFSOC', if_true: files('microchip_pfsoc.c')) riscv_ss.add(when: 'CONFIG_ACPI', if_true: files('virt-acpi-build.c')) -riscv_ss.add(when: 'CONFIG_RISCV_IOMMU', if_true: files('riscv-iommu.c')) +riscv_ss.add(when: 'CONFIG_RISCV_IOMMU', if_true: files('riscv-iommu.c', 'riscv-iommu-pci.c')) hw_arch += {'riscv': riscv_ss}
hw/riscv/riscv-iommu-pci.c new file +202 -0
202 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v8 6/12 hw/riscv/virt.c: support for RISC-V IOMMU PCIDevice hotplug
In reply to Daniel Henrique BarbozaFrom: Tomasz Jeznach <[email protected]>
Generate device tree entry for riscv-iommu PCI device, along with mapping all PCI device identifiers to the single IOMMU device instance.
--- hw/riscv/virt.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)
hw/riscv/virt.c +32 -1
@@ -32,6 +32,7 @@#include "hw/core/sysbus-fdt.h" #include "target/riscv/pmu.h" #include "hw/riscv/riscv_hart.h" +#include "hw/riscv/iommu.h" #include "hw/riscv/virt.h" #include "hw/riscv/boot.h" #include "hw/riscv/numa.h"
@@ -1032,6 +1033,30 @@ static void create_fdt_virtio_iommu(RISCVVirtState *s, uint16_t bdf) bdf + 1, iommu_phandle, bdf + 1, 0xffff - bdf); } +static void create_fdt_iommu (RISCVVirtState *s,uint16_t bdf) +{ +const char comp[] ="riscv,pci-iommu" ; +void *fdt =MACHINE (s)->fdt; +uint32_t iommu_phandle; + g_autofree char *iommu_node = NULL; + g_autofree char *pci_node = NULL; + + pci_node =g_strdup_printf ("/soc/pci@%lx" , + (long ) virt_memmap[VIRT_PCIE_ECAM ].base); + iommu_node =g_strdup_printf ("%s/iommu@%x" , pci_node, bdf); + iommu_phandle =qemu_fdt_alloc_phandle (fdt); +qemu_fdt_add_subnode (fdt, iommu_node); + +qemu_fdt_setprop (fdt, iommu_node,"compatible" , comp,sizeof (comp)); +qemu_fdt_setprop_cell (fdt, iommu_node,"#iommu-cells" ,1 ); +qemu_fdt_setprop_cell (fdt, iommu_node,"phandle" , iommu_phandle); +qemu_fdt_setprop_cells (fdt, iommu_node,"reg" , + bdf <<8 ,0 ,0 ,0 ,0 ); +qemu_fdt_setprop_cells (fdt, pci_node,"iommu-map" , +0 , iommu_phandle,0 , bdf, + bdf +1 , iommu_phandle, bdf +1 ,0xffff - bdf); +} +static void finalize_fdt (RISCVVirtState *s) {uint32_t phandle =1 , irq_mmio_phandle =1 , msi_pcie_phandle =1 ;
@@ -1738,9 +1763,11 @@ static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,MachineClass *mc =MACHINE_GET_CLASS (machine);if (device_is_dynamic_sysbus (mc, dev) || -object_dynamic_cast (OBJECT (dev),TYPE_VIRTIO_IOMMU_PCI )) { +object_dynamic_cast (OBJECT (dev),TYPE_VIRTIO_IOMMU_PCI ) || +object_dynamic_cast (OBJECT (dev),TYPE_RISCV_IOMMU_PCI )) {return HOTPLUG_HANDLER (machine); } +return NULL ; }
@@ -1761,6 +1788,10 @@ static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,if (object_dynamic_cast (OBJECT (dev),TYPE_VIRTIO_IOMMU_PCI )) {create_fdt_virtio_iommu (s,pci_get_bdf (PCI_DEVICE (dev))); } + +if (object_dynamic_cast (OBJECT (dev),TYPE_RISCV_IOMMU_PCI )) { +create_fdt_iommu (s,pci_get_bdf (PCI_DEVICE (dev))); + } }static void virt_machine_class_init (ObjectClass *oc,void *data)
Signature · 1 line
2.45.2
PATCH v8 7/12 test/qtest: add riscv-iommu-pci tests
In reply to Daniel Henrique BarbozaTo test the RISC-V IOMMU emulation we'll use its PCI representation. Create a new 'riscv-iommu-pci' libqos device that will be present with CONFIG_RISCV_IOMMU. This config is only available for RISC-V, so this device will only be consumed by the RISC-V libqos machine.
Start with basic tests: a PCI sanity check and a reset state register test. The reset test was taken from the RISC-V IOMMU spec chapter 5.2, "Reset behavior".
More tests will be added later.
--- tests/qtest/libqos/meson.build | 4 ++ tests/qtest/libqos/riscv-iommu.c | 76 ++++++++++++++++++++++++++++ tests/qtest/libqos/riscv-iommu.h | 71 ++++++++++++++++++++++++++ tests/qtest/meson.build | 1 + tests/qtest/riscv-iommu-test.c | 85 ++++++++++++++++++++++++++++++++ 5 files changed, 237 insertions(+) create mode 100644 tests/qtest/libqos/riscv-iommu.c create mode 100644 tests/qtest/libqos/riscv-iommu.h create mode 100644 tests/qtest/riscv-iommu-test.c
tests/qtest/libqos/meson.build +4 -0
@@ -68,6 +68,10 @@ if have_virtfs libqos_srcs += files('virtio-9p.c', 'virtio-9p-client.c') endif +if config_all_devices.has_key('CONFIG_RISCV_IOMMU') + libqos_srcs += files('riscv-iommu.c') +endif + libqos = static_library('qos', libqos_srcs + genh, build_by_default: false)
tests/qtest/libqos/riscv-iommu.c new file +76 -0
76 lines not on this page · raw message
tests/qtest/libqos/riscv-iommu.h new file +71 -0
71 lines not on this page · raw message
tests/qtest/meson.build +1 -0
@@ -297,6 +297,7 @@ qos_test_ss.add( 'vmxnet3-test.c', 'igb-test.c', 'ufs-test.c', + 'riscv-iommu-test.c', ) if config_all_devices.has_key('CONFIG_VIRTIO_SERIAL')
tests/qtest/riscv-iommu-test.c new file +85 -0
85 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v8 8/12 hw/riscv/riscv-iommu: add Address Translation Cache (IOATC)
In reply to Daniel Henrique BarbozaFrom: Tomasz Jeznach <[email protected]>
The RISC-V IOMMU spec predicts that the IOMMU can use translation caches to hold entries from the DDT. This includes implementation for all cache commands that are marked as 'not implemented'.
There are some artifacts included in the cache that predicts s-stage and g-stage elements, although we don't support it yet. We'll introduce them next.
--- hw/riscv/riscv-iommu.c | 204 ++++++++++++++++++++++++++++++++++++++++- hw/riscv/riscv-iommu.h | 3 + 2 files changed, 203 insertions(+), 4 deletions(-)
hw/riscv/riscv-iommu.c +200 -4
@@ -64,6 +64,15 @@ struct RISCVIOMMUContext {uint64_t msiptp;/* MSI redirection page table pointer */ }; +/* Address translation cache entry */ +struct RISCVIOMMUEntry { +uint64_t iova:44 ;/* IOVA Page Number */ +uint64_t pscid:20 ;/* Process Soft-Context identifier */ +uint64_t phys:44 ;/* Physical Page Number */ +uint64_t gscid:16 ;/* Guest Soft-Context identifier */ +uint64_t perm:2 ;/* IOMMU_RW flags */ +}; +/* IOMMU index for transactions without process_id specified. */ #define RISCV_IOMMU_NOPROCID 0
@@ -1151,13 +1160,132 @@ static AddressSpace *riscv_iommu_space(RISCVIOMMUState *s, uint32_t devid)return &as->iova_as; } +/* Translation Object cache support */ +static gboolean riscv_iommu_iot_equal (gconstpointer v1,gconstpointer v2) +{ +RISCVIOMMUEntry *t1 = (RISCVIOMMUEntry *) v1; +RISCVIOMMUEntry *t2 = (RISCVIOMMUEntry *) v2; +return t1->gscid == t2->gscid && t1->pscid == t2->pscid && + t1->iova == t2->iova; +} + +static guint riscv_iommu_iot_hash (gconstpointer v) +{ +RISCVIOMMUEntry *t = (RISCVIOMMUEntry *) v; +return (guint )t->iova; +} + +/* GV: 1 PSCV: 1 AV: 1 */ +static void riscv_iommu_iot_inval_pscid_iova (gpointer key,gpointer value, +gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->gscid == arg->gscid && + iot->pscid == arg->pscid && + iot->iova == arg->iova) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 1 PSCV: 1 AV: 0 */ +static void riscv_iommu_iot_inval_pscid (gpointer key,gpointer value, +gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->gscid == arg->gscid && + iot->pscid == arg->pscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 1 GVMA: 1 */ +static void riscv_iommu_iot_inval_gscid_gpa (gpointer key,gpointer value, +gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->gscid == arg->gscid) { +/* simplified cache, no GPA matching */ + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 1 GVMA: 0 */ +static void riscv_iommu_iot_inval_gscid (gpointer key,gpointer value, +gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; +RISCVIOMMUEntry *arg = (RISCVIOMMUEntry *) data; +if (iot->gscid == arg->gscid) { + iot->perm =IOMMU_NONE ; + } +} + +/* GV: 0 */ +static void riscv_iommu_iot_inval_all (gpointer key,gpointer value, +gpointer data) +{ +RISCVIOMMUEntry *iot = (RISCVIOMMUEntry *) value; + iot->perm =IOMMU_NONE ; +} + +/* caller should keep ref-count for iot_cache object */ +static RISCVIOMMUEntry *riscv_iommu_iot_lookup (RISCVIOMMUContext *ctx, +GHashTable *iot_cache,hwaddr iova) +{ +RISCVIOMMUEntry key = { + .gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ), + .pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ), + .iova =PPN_DOWN (iova), + }; +return g_hash_table_lookup (iot_cache, &key); +} + +/* caller should keep ref-count for iot_cache object */ +static void riscv_iommu_iot_update (RISCVIOMMUState *s, +GHashTable *iot_cache,RISCVIOMMUEntry *iot) +{ +if (!s->iot_limit) { +return ; + } + +if (g_hash_table_size (s->iot_cache) >= s->iot_limit) { + iot_cache =g_hash_table_new_full (riscv_iommu_iot_hash, + riscv_iommu_iot_equal, + g_free,NULL ); +g_hash_table_unref (qatomic_xchg (&s->iot_cache, iot_cache)); + } +g_hash_table_add (iot_cache, iot); +} + +static void riscv_iommu_iot_inval (RISCVIOMMUState *s,GHFunc func, +uint32_t gscid,uint32_t pscid,hwaddr iova) +{ +GHashTable *iot_cache; +RISCVIOMMUEntry key = { + .gscid = gscid, + .pscid = pscid, + .iova =PPN_DOWN (iova), + }; + + iot_cache =g_hash_table_ref (s->iot_cache); +g_hash_table_foreach (iot_cache, func, &key); +g_hash_table_unref (iot_cache); +} +static int riscv_iommu_translate (RISCVIOMMUState *s,RISCVIOMMUContext *ctx, -IOMMUTLBEntry *iotlb) +IOMMUTLBEntry *iotlb,bool enable_cache) { +RISCVIOMMUEntry *iot; +IOMMUAccessFlags perm;bool enable_pid;bool enable_pri; +GHashTable *iot_cache;int fault; + iot_cache =g_hash_table_ref (s->iot_cache);/* * TC[32] is reserved for custom extensions, used here to temporarily * enable automatic page-request generation for ATS queries.
@@ -1165,9 +1293,43 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx, enable_pri = (iotlb->perm ==IOMMU_NONE ) && (ctx->tc &BIT_ULL (32 )); enable_pid = (ctx->tc &RISCV_IOMMU_DC_TC_PDTV ); + iot =riscv_iommu_iot_lookup (ctx, iot_cache, iotlb->iova); + perm = iot ? iot->perm :IOMMU_NONE ; +if (perm !=IOMMU_NONE ) { + iotlb->translated_addr =PPN_PHYS (iot->phys); + iotlb->addr_mask = ~TARGET_PAGE_MASK ; + iotlb->perm = perm; + fault =0 ; + goto done; + } +/* Translate using device directory / page table information. */ fault =riscv_iommu_spa_fetch (s, ctx, iotlb); +if (!fault && iotlb->target_as == &s->trap_as) { +/* Do not cache trapped MSI translations */ + goto done; + } + +/* +* We made an implementation choice to not cache identity-mapped +* translations, as allowed by the specification, to avoid +* translation cache evictions for other devices sharing the +* IOMMU hardware model. +*/ +if (!fault && iotlb->translated_addr != iotlb->iova && enable_cache) { + iot =g_new0 (RISCVIOMMUEntry,1 ); + iot->iova =PPN_DOWN (iotlb->iova); + iot->phys =PPN_DOWN (iotlb->translated_addr); + iot->gscid =get_field (ctx->gatp,RISCV_IOMMU_DC_IOHGATP_GSCID ); + iot->pscid =get_field (ctx->ta,RISCV_IOMMU_DC_TA_PSCID ); + iot->perm = iotlb->perm; +riscv_iommu_iot_update (s, iot_cache, iot); + } + +done: +g_hash_table_unref (iot_cache); +if (enable_pri && fault) {struct riscv_iommu_pq_record pr = {0 };if (enable_pid) {
@@ -1307,13 +1469,40 @@ static void riscv_iommu_process_cq_tail(RISCVIOMMUState *s)if (cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV ) {/* illegal command arguments IOTINVAL.GVMA & PSCV == 1 */ goto cmd_ill; + }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { +/* invalidate all cache mappings */ + func = riscv_iommu_iot_inval_all; + }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { +/* invalidate cache matching GSCID */ + func = riscv_iommu_iot_inval_gscid; + }else { +/* invalidate cache matching GSCID and ADDR (GPA) */ + func = riscv_iommu_iot_inval_gscid_gpa; } -/* translation cache not implemented yet */ +riscv_iommu_iot_inval (s, func, +get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ),0 , + cmd.dword1 &TARGET_PAGE_MASK );break ;case RISCV_IOMMU_CMD (RISCV_IOMMU_CMD_IOTINVAL_FUNC_VMA ,RISCV_IOMMU_CMD_IOTINVAL_OPCODE ): -/* translation cache not implemented yet */ +if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_GV )) { +/* invalidate all cache mappings, simplified model */ + func = riscv_iommu_iot_inval_all; + }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_PSCV )) { +/* invalidate cache matching GSCID, simplified model */ + func = riscv_iommu_iot_inval_gscid; + }else if (!(cmd.dword0 &RISCV_IOMMU_CMD_IOTINVAL_AV )) { +/* invalidate cache matching GSCID and PSCID */ + func = riscv_iommu_iot_inval_pscid; + }else { +/* invalidate cache matching GSCID and PSCID and ADDR (IOVA) */ + func = riscv_iommu_iot_inval_pscid_iova; + } +riscv_iommu_iot_inval (s, func, +get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_GSCID ), +get_field (cmd.dword0,RISCV_IOMMU_CMD_IOTINVAL_PSCID ), + cmd.dword1 &TARGET_PAGE_MASK );break ; case RISCV_IOMMU_CMD(RISCV_IOMMU_CMD_IODIR_FUNC_INVAL_DDT,
@@ -1828,6 +2017,10 @@ static void riscv_iommu_realize(DeviceState *dev, Error **errp) riscv_iommu_ctx_equal, g_free, NULL); + s->iot_cache =g_hash_table_new_full (riscv_iommu_iot_hash, + riscv_iommu_iot_equal, + g_free,NULL ); + s->iommus.le_next =NULL ; s->iommus.le_prev =NULL ;QLIST_INIT (&s->spaces);
@@ -1837,6 +2030,7 @@ static void riscv_iommu_unrealize(DeviceState *dev) {RISCVIOMMUState *s =RISCV_IOMMU (dev); +g_hash_table_unref (s->iot_cache);g_hash_table_unref (s->ctx_cache); }
@@ -1844,6 +2038,8 @@ static Property riscv_iommu_properties[] = {DEFINE_PROP_UINT32 ("version" , RISCVIOMMUState, version,RISCV_IOMMU_SPEC_DOT_VER ),DEFINE_PROP_UINT32 ("bus" , RISCVIOMMUState, bus,0x0 ), +DEFINE_PROP_UINT32 ("ioatc-limit" , RISCVIOMMUState, iot_limit, +LIMIT_CACHE_IOT ),DEFINE_PROP_BOOL ("intremap" , RISCVIOMMUState, enable_msi, TRUE),DEFINE_PROP_BOOL ("off" , RISCVIOMMUState, enable_off, TRUE), DEFINE_PROP_BOOL("s-stage", RISCVIOMMUState, enable_s_stage, TRUE),
@@ -1898,7 +2094,7 @@ static IOMMUTLBEntry riscv_iommu_memory_region_translate(/* Translation disabled or invalid. */ iotlb.addr_mask =0 ; iotlb.perm =IOMMU_NONE ; - }else if (riscv_iommu_translate (as->iommu, ctx, &iotlb)) { + }else if (riscv_iommu_translate (as->iommu, ctx, &iotlb, true)) {/* Translation disabled or fault reported. */ iotlb.addr_mask =0 ; iotlb.perm =IOMMU_NONE ;
hw/riscv/riscv-iommu.h +3 -0
@@ -69,6 +69,9 @@ struct RISCVIOMMUState {GHashTable *ctx_cache;/* Device translation Context Cache */ +GHashTable *iot_cache;/* IO Translated Address Cache */ +unsigned iot_limit;/* IO Translation Cache size limit */ +/* MMIO Hardware Interface */ MemoryRegion regs_mr;uint8_t *regs_rw;/* register state (user write) */
Signature · 1 line
2.45.2
PATCH v8 9/12 hw/riscv/riscv-iommu: add ATS support
In reply to Daniel Henrique BarbozaFrom: Tomasz Jeznach <[email protected]>
Add PCIe Address Translation Services (ATS) capabilities to the IOMMU. This will add support for ATS translation requests in Fault/Event queues, Page-request queue and IOATC invalidations.
--- hw/riscv/riscv-iommu-bits.h | 43 +++++++++++- hw/riscv/riscv-iommu.c | 127 +++++++++++++++++++++++++++++++++++- hw/riscv/riscv-iommu.h | 1 + hw/riscv/trace-events | 3 + 4 files changed, 171 insertions(+), 3 deletions(-)
hw/riscv/riscv-iommu-bits.h +42 -1
@@ -79,6 +79,7 @@ struct riscv_iommu_pq_record {#define RISCV_IOMMU_CAP_SV57X4 BIT_ULL(19)#define RISCV_IOMMU_CAP_MSI_FLAT BIT_ULL(22)#define RISCV_IOMMU_CAP_MSI_MRIF BIT_ULL(23) +#define RISCV_IOMMU_CAP_ATS BIT_ULL(25)#define RISCV_IOMMU_CAP_T2GPA BIT_ULL(26)#define RISCV_IOMMU_CAP_IGS GENMASK_ULL(29, 28)#define RISCV_IOMMU_CAP_PAS GENMASK_ULL(37, 32)
@@ -212,6 +213,7 @@ struct riscv_iommu_dc {/* Translation control fields */ #define RISCV_IOMMU_DC_TC_V BIT_ULL(0) +#define RISCV_IOMMU_DC_TC_EN_ATS BIT_ULL(1)#define RISCV_IOMMU_DC_TC_EN_PRI BIT_ULL(2)#define RISCV_IOMMU_DC_TC_T2GPA BIT_ULL(3)#define RISCV_IOMMU_DC_TC_DTF BIT_ULL(4)
@@ -273,6 +275,20 @@ struct riscv_iommu_command {#define RISCV_IOMMU_CMD_IODIR_DV BIT_ULL(33)#define RISCV_IOMMU_CMD_IODIR_DID GENMASK_ULL(63, 40) +/* 3.1.4 I/O MMU PCIe ATS */ +#define RISCV_IOMMU_CMD_ATS_OPCODE 4 +#define RISCV_IOMMU_CMD_ATS_FUNC_INVAL 0 +#define RISCV_IOMMU_CMD_ATS_FUNC_PRGR 1 +#define RISCV_IOMMU_CMD_ATS_PID GENMASK_ULL(31, 12) +#define RISCV_IOMMU_CMD_ATS_PV BIT_ULL(32) +#define RISCV_IOMMU_CMD_ATS_DSV BIT_ULL(33) +#define RISCV_IOMMU_CMD_ATS_RID GENMASK_ULL(55, 40) +#define RISCV_IOMMU_CMD_ATS_DSEG GENMASK_ULL(63, 56) +/* dword1 is the ATS payload, two different payload types for INVAL and PRGR */ + +/* ATS.PRGR payload */ +#define RISCV_IOMMU_CMD_ATS_PRGR_RESP_CODE GENMASK_ULL(47, 44) +enum riscv_iommu_dc_fsc_atp_modes {RISCV_IOMMU_DC_FSC_MODE_BARE =0 ,RISCV_IOMMU_DC_FSC_IOSATP_MODE_SV32 =8 ,
@@ -339,7 +355,32 @@ enum riscv_iommu_fq_ttypes {RISCV_IOMMU_FQ_TTYPE_TADDR_INST_FETCH =5 ,RISCV_IOMMU_FQ_TTYPE_TADDR_RD =6 ,RISCV_IOMMU_FQ_TTYPE_TADDR_WR =7 , - RISCV_IOMMU_FW_TTYPE_PCIE_MSG_REQ = 8, +RISCV_IOMMU_FQ_TTYPE_PCIE_ATS_REQ =8 , + RISCV_IOMMU_FW_TTYPE_PCIE_MSG_REQ = 9, +}; + +/* Header fields */ +#define RISCV_IOMMU_PREQ_HDR_PID GENMASK_ULL(31, 12) +#define RISCV_IOMMU_PREQ_HDR_PV BIT_ULL(32) +#define RISCV_IOMMU_PREQ_HDR_PRIV BIT_ULL(33) +#define RISCV_IOMMU_PREQ_HDR_EXEC BIT_ULL(34) +#define RISCV_IOMMU_PREQ_HDR_DID GENMASK_ULL(63, 40) + +/* Payload fields */ +#define RISCV_IOMMU_PREQ_PAYLOAD_R BIT_ULL(0) +#define RISCV_IOMMU_PREQ_PAYLOAD_W BIT_ULL(1) +#define RISCV_IOMMU_PREQ_PAYLOAD_L BIT_ULL(2) +#define RISCV_IOMMU_PREQ_PAYLOAD_M GENMASK_ULL(2, 0) +#define RISCV_IOMMU_PREQ_PRG_INDEX GENMASK_ULL(11, 3) +#define RISCV_IOMMU_PREQ_UADDR GENMASK_ULL(63, 12) + + +/* +* struct riscv_iommu_msi_pte - MSI Page Table Entry +*/ +struct riscv_iommu_msi_pte { +uint64_t pte; +uint64_t mrif_info; };/* Fields on pte */
hw/riscv/riscv-iommu.c +125 -2
185 lines not on this page · raw message
hw/riscv/riscv-iommu.h +1 -0
@@ -37,6 +37,7 @@ struct RISCVIOMMUState {bool enable_off;/* Enable out-of-reset OFF mode (DMA disabled) */ bool enable_msi;/* Enable MSI remapping */ +bool enable_ats;/* Enable ATS support */ bool enable_s_stage;/* Enable S/VS-Stage translation */ bool enable_g_stage;/* Enable G-Stage translation */
hw/riscv/trace-events +3 -0
@@ -12,3 +12,6 @@ riscv_iommu_notifier_add(const char *id) "%s: dev-iotlb notifier added" riscv_iommu_notifier_del(const char *id) "%s: dev-iotlb notifier removed" riscv_iommu_notify_int_vector(uint32_t cause, uint32_t vector) "Interrupt cause 0x%x sent via vector 0x%x" riscv_iommu_icvec_write(uint32_t orig, uint32_t actual) "ICVEC write: incoming 0x%x actual 0x%x" +riscv_iommu_ats(const char *id, unsigned b, unsigned d, unsigned f, uint64_t iova) "%s: translate request %04x:%02x.%u iova: 0x%"PRIx64 +riscv_iommu_ats_inval(const char *id) "%s: dev-iotlb invalidate" +riscv_iommu_ats_prgr(const char *id) "%s: dev-iotlb page request group response"
Signature · 1 line
2.45.2
PATCH v8 10/12 hw/riscv/riscv-iommu: add DBG support
In reply to Daniel Henrique BarbozaFrom: Tomasz Jeznach <[email protected]>
DBG support adds three additional registers: tr_req_iova, tr_req_ctl and tr_response.
The DBG cap is always enabled. No on/off toggle is provided for it.
--- hw/riscv/riscv-iommu-bits.h | 17 +++++++++++ hw/riscv/riscv-iommu.c | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+)
hw/riscv/riscv-iommu-bits.h +17 -0
@@ -82,6 +82,7 @@ struct riscv_iommu_pq_record {#define RISCV_IOMMU_CAP_ATS BIT_ULL(25)#define RISCV_IOMMU_CAP_T2GPA BIT_ULL(26)#define RISCV_IOMMU_CAP_IGS GENMASK_ULL(29, 28) +#define RISCV_IOMMU_CAP_DBG BIT_ULL(31)#define RISCV_IOMMU_CAP_PAS GENMASK_ULL(37, 32)#define RISCV_IOMMU_CAP_PD8 BIT_ULL(38)#define RISCV_IOMMU_CAP_PD17 BIT_ULL(39)
@@ -184,6 +185,22 @@ enum { RISCV_IOMMU_INTR_COUNT }; +/* 5.24 Translation request IOVA (64bits) */ +#define RISCV_IOMMU_REG_TR_REQ_IOVA 0x0258 + +/* 5.25 Translation request control (64bits) */ +#define RISCV_IOMMU_REG_TR_REQ_CTL 0x0260 +#define RISCV_IOMMU_TR_REQ_CTL_GO_BUSY BIT_ULL(0) +#define RISCV_IOMMU_TR_REQ_CTL_NW BIT_ULL(3) +#define RISCV_IOMMU_TR_REQ_CTL_PID GENMASK_ULL(31, 12) +#define RISCV_IOMMU_TR_REQ_CTL_DID GENMASK_ULL(63, 40) + +/* 5.26 Translation request response (64bits) */ +#define RISCV_IOMMU_REG_TR_RESPONSE 0x0268 +#define RISCV_IOMMU_TR_RESPONSE_FAULT BIT_ULL(0) +#define RISCV_IOMMU_TR_RESPONSE_S BIT_ULL(9) +#define RISCV_IOMMU_TR_RESPONSE_PPN RISCV_IOMMU_PPN_FIELD +/* 5.27 Interrupt cause to vector (64bits) */ #define RISCV_IOMMU_REG_ICVEC 0x02F8#define RISCV_IOMMU_ICVEC_CIV GENMASK_ULL(3, 0)
hw/riscv/riscv-iommu.c +59 -0
83 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v8 11/12 qtest/riscv-iommu-test: add init queues test
In reply to Daniel Henrique BarbozaAdd an additional test to further exercise the IOMMU where we attempt to initialize the command, fault and page-request queues.
These steps are taken from chapter 6.2 of the RISC-V IOMMU spec, "Guidelines for initialization". It emulates what we expect from the software/OS when initializing the IOMMU.
--- tests/qtest/libqos/riscv-iommu.h | 30 ++++++++ tests/qtest/riscv-iommu-test.c | 125 +++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+)
tests/qtest/libqos/riscv-iommu.h +30 -0
@@ -62,6 +62,36 @@#define RISCV_IOMMU_REG_IPSR 0x0054 +#define RISCV_IOMMU_REG_IVEC 0x02F8 +#define RISCV_IOMMU_REG_IVEC_CIV GENMASK_ULL(3, 0) +#define RISCV_IOMMU_REG_IVEC_FIV GENMASK_ULL(7, 4) +#define RISCV_IOMMU_REG_IVEC_PMIV GENMASK_ULL(11, 8) +#define RISCV_IOMMU_REG_IVEC_PIV GENMASK_ULL(15, 12) + +#define RISCV_IOMMU_REG_CQB 0x0018 +#define RISCV_IOMMU_CQB_PPN_START 10 +#define RISCV_IOMMU_CQB_PPN_LEN 44 +#define RISCV_IOMMU_CQB_LOG2SZ_START 0 +#define RISCV_IOMMU_CQB_LOG2SZ_LEN 5 + +#define RISCV_IOMMU_REG_CQT 0x0024 + +#define RISCV_IOMMU_REG_FQB 0x0028 +#define RISCV_IOMMU_FQB_PPN_START 10 +#define RISCV_IOMMU_FQB_PPN_LEN 44 +#define RISCV_IOMMU_FQB_LOG2SZ_START 0 +#define RISCV_IOMMU_FQB_LOG2SZ_LEN 5 + +#define RISCV_IOMMU_REG_FQT 0x0034 + +#define RISCV_IOMMU_REG_PQB 0x0038 +#define RISCV_IOMMU_PQB_PPN_START 10 +#define RISCV_IOMMU_PQB_PPN_LEN 44 +#define RISCV_IOMMU_PQB_LOG2SZ_START 0 +#define RISCV_IOMMU_PQB_LOG2SZ_LEN 5 + +#define RISCV_IOMMU_REG_PQT 0x0044 + typedef struct QRISCVIOMMU { QOSGraphObject obj; QPCIDevice dev;
tests/qtest/riscv-iommu-test.c +125 -0
141 lines not on this page · raw message
Signature · 1 line
2.45.2
PATCH v8 12/12 docs/specs: add riscv-iommu
In reply to Daniel Henrique BarbozaAdd a simple guideline to use the existing RISC-V IOMMU support we just added.
This doc will be updated once we add the riscv-iommu-sys device.
--- docs/specs/index.rst | 1 + docs/specs/riscv-iommu.rst | 90 ++++++++++++++++++++++++++++++++++++++ docs/system/riscv/virt.rst | 13 ++++++ 3 files changed, 104 insertions(+) create mode 100644 docs/specs/riscv-iommu.rst
docs/specs/index.rst +1 -0
@@ -36,3 +36,4 @@ guest hardware that is specific to QEMU. vmgenid rapl-msr rocker + riscv-iommu
docs/specs/riscv-iommu.rst new file +90 -0
90 lines not on this page · raw message
docs/system/riscv/virt.rst +13 -0
@@ -84,6 +84,19 @@ none``, as in Firmware images used for pflash must be exactly 32 MiB in size. +riscv-iommu support +------------------- + +The board has support for the riscv-iommu-pci device by using the following +command line: + +.. code-block:: bash + + $ qemu-system-riscv64 -M virt -device riscv-iommu-pci (...) + +Refer to :ref:`riscv-iommu` for more information on how the RISC-V IOMMU support +works. + Machine-specific options ------------------------
Signature · 1 line
2.45.2