1*77cfbf5dSDaniel Henrique Barboza.. _riscv-iommu: 2*77cfbf5dSDaniel Henrique Barboza 3*77cfbf5dSDaniel Henrique BarbozaRISC-V IOMMU support for RISC-V machines 4*77cfbf5dSDaniel Henrique Barboza======================================== 5*77cfbf5dSDaniel Henrique Barboza 6*77cfbf5dSDaniel Henrique BarbozaQEMU implements a RISC-V IOMMU emulation based on the RISC-V IOMMU spec 7*77cfbf5dSDaniel Henrique Barbozaversion 1.0 `iommu1.0`_. 8*77cfbf5dSDaniel Henrique Barboza 9*77cfbf5dSDaniel Henrique BarbozaThe emulation includes a PCI reference device, riscv-iommu-pci, that QEMU 10*77cfbf5dSDaniel Henrique BarbozaRISC-V boards can use. The 'virt' RISC-V machine is compatible with this 11*77cfbf5dSDaniel Henrique Barbozadevice. 12*77cfbf5dSDaniel Henrique Barboza 13*77cfbf5dSDaniel Henrique Barbozariscv-iommu-pci reference device 14*77cfbf5dSDaniel Henrique Barboza-------------------------------- 15*77cfbf5dSDaniel Henrique Barboza 16*77cfbf5dSDaniel Henrique BarbozaThis device implements the RISC-V IOMMU emulation as recommended by the section 17*77cfbf5dSDaniel Henrique Barboza"Integrating an IOMMU as a PCIe device" of `iommu1.0`_: a PCI device with base 18*77cfbf5dSDaniel Henrique Barbozaclass 08h, sub-class 06h and programming interface 00h. 19*77cfbf5dSDaniel Henrique Barboza 20*77cfbf5dSDaniel Henrique BarbozaAs a reference device it doesn't implement anything outside of the specification, 21*77cfbf5dSDaniel Henrique Barbozaso it uses a generic default PCI ID given by QEMU: 1b36:0014. 22*77cfbf5dSDaniel Henrique Barboza 23*77cfbf5dSDaniel Henrique BarbozaTo include the device in the 'virt' machine: 24*77cfbf5dSDaniel Henrique Barboza 25*77cfbf5dSDaniel Henrique Barboza.. code-block:: bash 26*77cfbf5dSDaniel Henrique Barboza 27*77cfbf5dSDaniel Henrique Barboza $ qemu-system-riscv64 -M virt -device riscv-iommu-pci,[optional_pci_opts] (...) 28*77cfbf5dSDaniel Henrique Barboza 29*77cfbf5dSDaniel Henrique BarbozaThis will add a RISC-V IOMMU PCI device in the board following any additional 30*77cfbf5dSDaniel Henrique BarbozaPCI parameters (like PCI bus address). The behavior of the RISC-V IOMMU is 31*77cfbf5dSDaniel Henrique Barbozadefined by the spec but its operation is OS dependent. 32*77cfbf5dSDaniel Henrique Barboza 33*77cfbf5dSDaniel Henrique BarbozaAs of this writing the existing Linux kernel support `linux-v8`_, not yet merged, 34*77cfbf5dSDaniel Henrique Barbozadoes not have support for features like VFIO passthrough. The IOMMU emulation 35*77cfbf5dSDaniel Henrique Barbozawas tested using a public Ventana Micro Systems kernel repository in 36*77cfbf5dSDaniel Henrique Barboza`ventana-linux`_. This kernel is based on `linux-v8`_ with additional patches that 37*77cfbf5dSDaniel Henrique Barbozaenable features like KVM VFIO passthrough with irqbypass. Until the kernel support 38*77cfbf5dSDaniel Henrique Barbozais feature complete feel free to use the kernel available in the Ventana Micro Systems 39*77cfbf5dSDaniel Henrique Barbozamirror. 40*77cfbf5dSDaniel Henrique Barboza 41*77cfbf5dSDaniel Henrique BarbozaThe current Linux kernel support will use the IOMMU device to create IOMMU groups 42*77cfbf5dSDaniel Henrique Barbozawith any eligible cards available in the system, regardless of factors such as the 43*77cfbf5dSDaniel Henrique Barbozaorder in which the devices are added in the command line. 44*77cfbf5dSDaniel Henrique Barboza 45*77cfbf5dSDaniel Henrique BarbozaThis means that these command lines are equivalent as far as the current 46*77cfbf5dSDaniel Henrique BarbozaIOMMU kernel driver behaves: 47*77cfbf5dSDaniel Henrique Barboza 48*77cfbf5dSDaniel Henrique Barboza.. code-block:: bash 49*77cfbf5dSDaniel Henrique Barboza 50*77cfbf5dSDaniel Henrique Barboza $ qemu-system-riscv64 \ 51*77cfbf5dSDaniel Henrique Barboza -M virt,aia=aplic-imsic,aia-guests=5 \ 52*77cfbf5dSDaniel Henrique Barboza -device riscv-iommu-pci,addr=1.0,vendor-id=0x1efd,device-id=0xedf1 \ 53*77cfbf5dSDaniel Henrique Barboza -device e1000e,netdev=net1 -netdev user,id=net1,net=192.168.0.0/24 \ 54*77cfbf5dSDaniel Henrique Barboza -device e1000e,netdev=net2 -netdev user,id=net2,net=192.168.200.0/24 \ 55*77cfbf5dSDaniel Henrique Barboza (...) 56*77cfbf5dSDaniel Henrique Barboza 57*77cfbf5dSDaniel Henrique Barboza $ qemu-system-riscv64 \ 58*77cfbf5dSDaniel Henrique Barboza -M virt,aia=aplic-imsic,aia-guests=5 \ 59*77cfbf5dSDaniel Henrique Barboza -device e1000e,netdev=net1 -netdev user,id=net1,net=192.168.0.0/24 \ 60*77cfbf5dSDaniel Henrique Barboza -device e1000e,netdev=net2 -netdev user,id=net2,net=192.168.200.0/24 \ 61*77cfbf5dSDaniel Henrique Barboza -device riscv-iommu-pci,addr=1.0,vendor-id=0x1efd,device-id=0xedf1 \ 62*77cfbf5dSDaniel Henrique Barboza (...) 63*77cfbf5dSDaniel Henrique Barboza 64*77cfbf5dSDaniel Henrique BarbozaBoth will create iommu groups for the two e1000e cards. 65*77cfbf5dSDaniel Henrique Barboza 66*77cfbf5dSDaniel Henrique BarbozaAnother thing to notice on `linux-v8`_ and `ventana-linux`_ is that the kernel driver 67*77cfbf5dSDaniel Henrique Barbozaconsiders an IOMMU identified as a Rivos device, i.e. it uses Rivos vendor ID. To 68*77cfbf5dSDaniel Henrique Barbozause the riscv-iommu-pci device with the existing kernel support we need to emulate 69*77cfbf5dSDaniel Henrique Barbozaa Rivos PCI IOMMU by setting 'vendor-id' and 'device-id': 70*77cfbf5dSDaniel Henrique Barboza 71*77cfbf5dSDaniel Henrique Barboza.. code-block:: bash 72*77cfbf5dSDaniel Henrique Barboza 73*77cfbf5dSDaniel Henrique Barboza $ qemu-system-riscv64 -M virt \ 74*77cfbf5dSDaniel Henrique Barboza -device riscv-iommu-pci,vendor-id=0x1efd,device-id=0xedf1 (...) 75*77cfbf5dSDaniel Henrique Barboza 76*77cfbf5dSDaniel Henrique BarbozaSeveral options are available to control the capabilities of the device, namely: 77*77cfbf5dSDaniel Henrique Barboza 78*77cfbf5dSDaniel Henrique Barboza- "bus": the bus that the IOMMU device uses 79*77cfbf5dSDaniel Henrique Barboza- "ioatc-limit": size of the Address Translation Cache (default to 2Mb) 80*77cfbf5dSDaniel Henrique Barboza- "intremap": enable/disable MSI support 81*77cfbf5dSDaniel Henrique Barboza- "ats": enable ATS support 82*77cfbf5dSDaniel Henrique Barboza- "off" (Out-of-reset translation mode: 'on' for DMA disabled, 'off' for 'BARE' (passthrough)) 83*77cfbf5dSDaniel Henrique Barboza- "s-stage": enable s-stage support 84*77cfbf5dSDaniel Henrique Barboza- "g-stage": enable g-stage support 85*77cfbf5dSDaniel Henrique Barboza 86*77cfbf5dSDaniel Henrique Barboza.. _iommu1.0: https://github.com/riscv-non-isa/riscv-iommu/releases/download/v1.0/riscv-iommu.pdf 87*77cfbf5dSDaniel Henrique Barboza 88*77cfbf5dSDaniel Henrique Barboza.. _linux-v8: https://lore.kernel.org/linux-riscv/cover.1718388908.git.tjeznach@rivosinc.com/ 89*77cfbf5dSDaniel Henrique Barboza 90*77cfbf5dSDaniel Henrique Barboza.. _ventana-linux: https://github.com/ventanamicro/linux/tree/dev-upstream 91