Alexander Gromnitsky's Blog

GPU passthrough in Fedora with plain QEMU

Latest update:

QEMU 11.1.3
Host OS Fedora 45 (xorg)
Guest OS Windows 11 26h2 (secure boot + TPM)
Host CPU AMD Ryzen 5 PRO 4650G (Zen 2, Renoir)
Host GPU Radeon Vega 7
Guest GPU Radeon HD 8570 (R7 240, Oland)

Prerequisites

$ sudo dnf install qemu edk2-ovmf swtpm

Enable virtualisation in your bios. For AMD, the option is usually called SVM. If a motherboard has a separate IOMMU option, leave it in auto state.

Check:

$ sudo dnf install libvirt-daemon
$ virt-host-validate | grep hardware | tr -s \\040
 QEMU: Checking for hardware virtualization : PASS

(You may uninstall libvirt-daemon package, for we are using plain QEMU without libvirt.)

A guest GPU ROM must support UEFI. R7 240 in the table above is perhaps the cheapest valid GPU (for a passthrough) that money can buy, but which is still miles ahead of emulated graphic cards in QEMU or VMWare.

Connect an hdmi/dp cable from your 2nd GPU to a 2nd monitor (I actually use 1 monitor with 2 inputs).

Connect a spare USB keyboard & a mouse, i.e. your machine must have 2 physical keyboard+mouse pairs. After you successfully install a guest OS, software like Deskflow makes the 2nd pair unnecessary (unless you play games).

IOMMU Groups

IOMMU is a feature that allows us to passthrough PCI devices. In Fedora it's off by default. Simultaneously with turning it on, we are going to unhook our 2nd GPU from it's current driver & hook it to a driver called vfio-pci.

PCI devices are mapped into IOMMU groups. You can passthrough a group of devices as a unit. That means if your main GPU is in the same group as a secondary one, you're screwed.

This (unfortunately long) script prints all IOMMU groups:

$ cat iommu-groups
#!/bin/sh
# shellcheck disable=2012

[ -z "$1" ] || {
    $0 | awk 'BEGIN { RS = "" } /'"$1"'/ {print; m++} END { exit (m == 0) }'
    exit $?
}

device_info() {
    lspci -s "$1" -nnk | sed -E 's/^[0-9].+/printf "*  %s" "\0" | fmt -t/e'
}

ls -d /sys/kernel/iommu_groups/* | sort -V | while read -r grp; do
    echo "${grp##*/}"
    for dev in "$grp/devices/"*; do device_info "${dev##*/}"; done
    echo ''
done

Check a particular group & what drivers it uses:

$ ./iommu-groups Oland
8
*  10:00.0 VGA compatible controller [0300]: Advanced Micro Devices,
   Inc. [AMD/ATI] Oland [Radeon HD 8570 / R5 430 OEM / R7 240/340 /
   Radeon 520 OEM] [1002:6611]
        Subsystem: Dell Radeon R5 240 OEM [1028:210b]
        Kernel driver in use: amdgpu
        Kernel modules: radeon, amdgpu
*  10:00.1 Audio device [0403]: Advanced Micro Devices, Inc. [AMD/ATI]
   Oland/Hainan/Cape Verde/Pitcairn HDMI Audio [Radeon HD 7000 Series]
   [1002:aab0]
        Subsystem: Dell Device [1028:aab0]
        Kernel driver in use: snd_hda_intel
        Kernel modules: snd_hda_intel

Group #8 contains only 2 devices: a 2nd GPU & its audio interface.

To turn IOMMU support on & hook the devices we're interested in to vfio-pci, we need to modify kernel parameters & rebuild initramfs.

$ grep CMDLINE /etc/default/grub | fmt -t
GRUB_CMDLINE_LINUX="resume=/dev/sdb6 iommu=1 amd_iommu=on
   rd.driver.pre=vfio_pci vfio-pci.ids=1002:6611,1002:aab0"

Parameter vfio-pci.ids=1002:6611,1002:aab0 binds an array of devices to a vfio driver that prevents the host OS from interacting with them.

1002:6611, for example, is a PCI vendor:device pair from the snipped above.

To commit changes, type

# dracut -fv
# grub2-mkconfig -o /boot/grub2/grub.cfg

& reboot.

$ ./iommu-groups Oland | grep 'Kernel driver'
        Kernel driver in use: vfio-pci
        Kernel driver in use: vfio-pci

QEMU calls

This is the most annoying part. If you do it 'incorrectly', you may end up with a purportedly successfully installed guest OS, but when Windows updates GPU drivers--boom!--the monitor goes black.

This (unfortunately rather big) makefile will get you started:

gpu := 10:00.0
audio := 10:00.1
usb_kbd := 1c4f 0002
usb_mouse := 046d c542
mem := 4G
drive.c := c.qcow2

define gpu-passthrough :=
-vga none \
-device ioh3420,id=hub1,chassis=0,slot=0,bus=pcie.0 \
-device vfio-pci,bus=hub1,addr=00.0,host=$(gpu),multifunction=on \
-device vfio-pci,bus=hub1,addr=00.1,host=$(audio)
endef

define cpu :=
-cpu host,host-phys-bits-limit=0x27,hv-passthrough,topoext=on \
-smp 4,sockets=1,cores=2,threads=2
endef

__dir__ := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
tpm.dir := $(__dir__)/tpm
tpm.socket := $(tpm.dir)/socket-for-qemu

define tpm :=
-chardev socket,id=chrtpm,path=$(tpm.socket) \
-tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis,tpmdev=tpm0
endef

define drives :=
-drive file=$(drive.c),index=0,media=disk,if=virtio \
-drive file=$(iso),index=1,media=cdrom \
-drive file=$(iso2),index=2,media=cdrom \
-boot order=c
endef

define bios :=
-drive file=bios/OVMF_CODE_4M.secboot.qcow2,if=pflash,readonly=on \
-drive file=bios/OVMF_VARS_4M.secboot.qcow2,if=pflash
endef

$(if $(SUDO_USER),,$(error GPU passthrough requires sudo))

all: $(tpm.socket) bios/OVMF_CODE_4M.secboot.qcow2 bios/OVMF_VARS_4M.secboot.qcow2
	modprobe kvm_amd
	qemu-system-x86_64 \
	 -enable-kvm -machine q35 -rtc base=localtime \
	 $(cpu) -m $(mem) \
	 $(if $(temp),-snapshot) \
	 $(bios) $(tpm) -usb \
	 $(call device-usb,$(usb_kbd)) \
	 $(call device-usb,$(usb_mouse)) \
	 $(gpu-passthrough) -nographic \
	 $(if $(offline),-nic none,-device virtio-net-pci,netdev=nic1 -netdev user,id=nic1) \
	 $(drives)

bios/%.secboot.qcow2: /usr/share/edk2/ovmf/%.secboot.qcow2
	@mkdir -p $(dir $@)
	cp $< $@

device-usb = -device usb-host,vendorid=0x$(word 1, $1),productid=0x$(word 2, $1)

$(tpm.socket):
	@mkdir -p $(tpm.dir)
	swtpm socket --tpm2 -d --pid file=$(tpm.dir)/pid \
	 --tpmstate dir=$(tpm.dir) --ctrl type=unixio,path=$@

(For Intel CPUs, remove topoext=on from -cpu line.)

Variables of interest:

gpu := 10:00.0
audio := 10:00.1
usb_kbd := 1c4f 0002
usb_mouse := 046d c542

gpu & audio are bus numbers you can obtain from iommu-groups script. usb_* contain IDs (vendor product) that you can look up with lsusb command or via Chrome's chrome://usb-internals/ page.

The makefile also requires an empty disk drive:

$ qemu-img create -f qcow2 c.qcow2 60G

To install Windows:

$ sudo make iso=w11-26h2.iso iso2=virtio-win-0.1.302.iso

where virtio-win-*.iso contains drivers for disks & network cards. The makefile copies UEFI firmware, starts a TPM emulator, & runs qemu.

The holy grail is

define gpu-passthrough :=
-vga none \
-device ioh3420,id=hub1,chassis=0,slot=0,bus=pcie.0 \
-device vfio-pci,bus=hub1,addr=00.0,host=$(gpu),multifunction=on \
-device vfio-pci,bus=hub1,addr=00.1,host=$(audio)
endef

Without ioh3420 'north bridge' device, I had the aforementioned issue with a black screen after Windows had had auto-updated AMD drivers.

When you run the makefile for the first time, QEMU drops you to an emulated serial port, where you should immediately press Enter after this 2 lines appear:

BdsDxe: loading Boot0002 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x1,0xFFFF,0x0)
BdsDxe: starting Boot0002 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x1,0xFFFF,0x0)

The pressed key gets passed to the Windows installer that's being booted from w11-26h2.iso. The famous message on your physical display

Press any key to boot from CD or DVD...

comes from the Windows installer itself, not from the EFI.

If you missed the message, press Ctrl-a c (this switches you a so called QEMU 'monitor'), & type system_reset (to try again) or q.

Because we specified virtio drivers, Windows won't find any disks, unless you load viostor driver from virtio-win-*.iso:

After a successful installation (with the usual Shift-F10 and oobe\bypassnro trick), manually install NetKVM network driver.


Tags: ойті
Authors: ag