QEMU's RISC-V IOMMU model currently has no way to offload guest
stage 1 translation to a physical IOMMU. For guests with assigned PCI
devices, QEMU must handle the virtual IOMMU configuration itself
instead of allowing hardware to walk the guest's device contexts and
page tables. This leaves the nested translation support of RISC-V
hardware unused.
Add an "accel" mode to the RISC-V IOMMU that connects the virtual
IOMMU to the physical IOMMU through iommufd. The guest continues to
manage its own device contexts and stage 1 page tables, while the host
retains control of stage 2 translation. QEMU supplies the device
contexts and forwards cache invalidations through iommufd. The physical
IOMMU can then perform both stages of DMA translation for assigned
devices.
This allows the guest to keep managing its own DMA mappings while
hardware performs nested translation for assigned devices. The design
follows the existing ARM SMMUv3 accelerated vIOMMU support. We are
posting it as an RFC to collect feedback on the design and its kernel
uAPI dependency.
Patch organization
Patch 1 temporarily imports the RISC-V IOMMU iommufd definitions
needed by this series. A regular header update can replace this import
after the kernel uAPI is merged.
Patches 2 to 5 prepare the virt machine and the existing RISC-V IOMMU
code for an alternate accelerated implementation.
Patches 6 to 13 add that implementation. They create the iommufd
objects, manage host page table attachments, handle device context
updates and invalidations, and validate host capabilities. Patch 14
exposes the accel property and blocks migration when it is enabled.
Testing
The host is a RISC-V machine whose physical IOMMU is provided by an
outer QEMU riscv-iommu model, enabled with the iommu-sys machine
property. An NVMe controller under it is bound to vfio-pci and passed
through to the guest. For simplicity the guest runs the same kernel as
the host, and inside the guest it mainly uses the normal IOMMU driver
and the NVMe driver.
-object iommufd,id=iommufd0
-device riscv-iommu-pci,addr=1.0,s-stage=on,g-stage=off,accel=on
-device vfio-pci,host=0000:01:00.0,iommufd=iommufd0
Add iommu.passthrough=0 iommu.strict=1 to the guest command line so the
guest installs and uses the vIOMMU. The guest's riscv-iommu driver then
probes the vIOMMU, builds its stage 1 tables and programs device
contexts. I/O on the passed through NVMe goes through the combined
stage 1 and stage 2 walk on the physical IOMMU, and device MSIs reach
the vCPU without host software in the data path. Correctness was
checked by running fio against the passed through disk with
verification enabled, including under load.
Known limitations
With accel=on, the guest controls stage 1 translation while the host
owns stage 2. Only vfio-pci endpoints using the iommufd backend are
supported, and emulated endpoints are rejected because supporting them
would mean owning IOTLB invalidation in QEMU again. MSI remapping stays
with the host IOMMU, so the guest sees the base device context format.
There is no ATS or PRI support and no process_id or PDT support.
Migration is blocked while accel=on because the host vIOMMU and HWPT
state cannot be migrated. One accelerated IOMMU instance per VM is
assumed for now.
The series is also available from:
https://github.com/leaprise/qemu/tree/riscv/iommu-nested-rfc-v2
Changes in v2:
There are no functional changes in this version.
- Rebased the series on master at 73294d096cc
- Imported the RISC-V IOMMU uAPI definitions revised during review
and adapted QEMU accordingly.
- Link to v1:
https://lore.kernel.org/qemu-devel/202610140811.69E8BqaU098670@mse-fl2.zte.com.cn/
Gong Shuai (5):
linux-headers: Update iommufd.h header for RISC-V IOMMU nested
translation
hw/riscv/riscv-iommu: Expose RISCVIOMMUSpace and split its helpers
hw/riscv/riscv-iommu: Split device context fetch from cache lookup
hw/riscv/riscv-iommu-accel: Add nested vDC install/uninstall support
hw/riscv/riscv-iommu-accel: Get host RISC-V IOMMU hw info and validate
Wu Fei (9):
hw/riscv/Kconfig: Enable IOMMUFD on RISC-V virt machine
hw/riscv/riscv-iommu: Allow PCI IOMMU ops to be overridden
hw/riscv/riscv-iommu-accel: Introduce RISC-V IOMMU accel device
hw/riscv/riscv-iommu-accel: Initialize shared system address space
hw/riscv/riscv-iommu-accel: Restrict accelerated IOMMU to vfio-pci
endpoints with iommufd
hw/riscv/riscv-iommu-accel: Implement get_viommu_flags() callback
hw/riscv/riscv-iommu-accel: Add set/unset_iommu_device callback
hw/riscv/riscv-iommu-accel: Add support to issue invalidation cmd to
host
hw/riscv/riscv-iommu-accel: Add accel property for iommu device
hw/riscv/Kconfig | 6 +
hw/riscv/meson.build | 1 +
hw/riscv/riscv-iommu-accel.c | 715 ++++++++++++++++++++++++++++++++++
hw/riscv/riscv-iommu-accel.h | 81 ++++
hw/riscv/riscv-iommu.c | 209 +++++++---
hw/riscv/riscv-iommu.h | 36 ++
hw/riscv/trace-events | 6 +
linux-headers/linux/iommufd.h | 81 ++++
8 files changed, 1078 insertions(+), 57 deletions(-)
create mode 100644 hw/riscv/riscv-iommu-accel.c
create mode 100644 hw/riscv/riscv-iommu-accel.h
--
2.53.0