[RFC PATCH 00/14] hw/riscv: Add accelerated (nested) translation support to the RISC-V IOMMU

Gong Shuai posted 14 patches 1 month, 1 week ago
Only 0 patches received!
There is a newer version of this series
hw/riscv/Kconfig              |   6 +
hw/riscv/meson.build          |   1 +
hw/riscv/riscv-iommu-accel.c  | 713 ++++++++++++++++++++++++++++++++++
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 |  52 +++
8 files changed, 1047 insertions(+), 57 deletions(-)
create mode 100644 hw/riscv/riscv-iommu-accel.c
create mode 100644 hw/riscv/riscv-iommu-accel.h
[RFC PATCH 00/14] hw/riscv: Add accelerated (nested) translation support to the RISC-V IOMMU
Posted by Gong Shuai 1 month, 1 week ago
Add an "accel" mode to the riscv-iommu device so that the physical RISC-V
IOMMU performs the guest's first-stage translation instead of QEMU
emulating it. The guest IOMMU driver keeps building its own page tables
and device contexts as it would with a physical IOMMU, the host owns the
second stage, and DMA translation runs in hardware. The ARM SMMUv3 accel
support follows the same idea, and so does this work. We are posting it
as an RFC to collect feedback on the design.

Background

The RISC-V IOMMU supports nested translation, and iommufd gained vIOMMU
and nested HWPT objects that let a VMM bind a stage-1 domain owned by
userspace to a stage-2 domain owned by the VMM and have the physical
IOMMU walk both. QEMU does not use this interface for the RISC-V IOMMU
yet, and this work adds the accelerated mode that does.

Design

The guest owns stage-1, which covers the device context and the
first-stage page tables. The host owns stage-2. QEMU mirrors only the
guest state the host hardware needs, and forwards invalidations.

Each accelerated IOMMU owns an iommufd vIOMMU, parented by the stage-2
nesting parent HWPT that the VFIO core allocates for the VM. Each
attached device gets a vDEVICE that maps the guest devid to the host
device, and always has a HWPT attached, so QEMU keeps an abort and a
bypass HWPT for devices the guest does not translate and installs a
nested stage-1 HWPT when its device context does translate. A device
context change is picked up when the guest invalidates the context, and
a missing or invalid context aborts that device's DMA.

Invalidations are forwarded to the host IOMMU, and a RISC-V invalidate
command carries no requester id, so no per device filtering is needed.
The guest capabilities are intersected with what the attached host
IOMMUs support and finalised before the guest can observe them. Every
vfio-pci endpoint behind every accelerated IOMMU is handed the same
shared system address space, so the stage-2 nesting parent is shared
within the VM instead of duplicated per IOMMU.

Patch organization

Patch 1 updates linux-headers, but it is a placeholder. It hand imports
only the RISC-V IOMMU definitions of iommufd.h instead of running
scripts/update-linux-headers.sh. It depends on the RISC-V IOMMU nested
and vIOMMU kernel UAPI, which will be posted soon.

Patches 2 to 5 are preparation, and each is a precondition for the
accel device rather than incidental cleanup. They enable IOMMUFD on the
virt machine, let a PCI IOMMU override its ops, expose RISCVIOMMUSpace
so per device accel state can be embedded in it, and split the device
context fetch out of the cache lookup so the command path can read a
freshly written device context.

Patches 6 to 13 build the accel device, adding the skeleton, the shared
system address space, the endpoint restrictions, get_viommu_flags, the
vIOMMU and proxy HWPTs in the set and unset callbacks, nested device
context install, host invalidation, and host hardware info validation.
Patch 14 adds the accel property last, so the switch only becomes
available once all the supporting pieces are in place.

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

accel=on supports stage-1 only, and the second stage belongs to the host.
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-rfc1


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  | 713 ++++++++++++++++++++++++++++++++++
 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 |  52 +++
 8 files changed, 1047 insertions(+), 57 deletions(-)
 create mode 100644 hw/riscv/riscv-iommu-accel.c
 create mode 100644 hw/riscv/riscv-iommu-accel.h

-- 
2.53.0