1Haiku PCI Driver Development Under QEMU on Linux
2====================================================
3
4Developing Haiku drivers for PCI and PCI Express cards is now a lot easier
5given advancements in IOMMU under Linux. You can effectively detach PCI cards
6from their host operating system and attach them to guest VM's resulting in
7true hardware emulation. In this guide we will be configuring a secondary
8graphics card to be attached to a Haiku virtual machine.
9
10**Warning**: Any device attached to a VM will be unavailable to the host operating
11system. This means you **cannot** use your primary graphics card, network device,
12etc within a VM and the host operating system at the same time. In this
13example, we have two graphics cards installed in the Linux system.
14
15IOMMU Setup
16-----------------------
17You will need to have IOMMU hardware support on your motherboard for this
18to function. Most modern AMD A3 socket chips and Intel i3/i5/i7 devices
19have IOMMU built in. If your board does indeed have IOMMU, you will likely
20need to enable IOMMU within the bios of your motherboard before proceeding.
21
22Linux Setup
23-----------------------
24Now that you have an IOMMU enabled system, you will need to tell Linux to
25fully utilize IOMMU and reserve the PCI cards for IOMMU use.
26
27Now, all we need to do is to reserve the PCI device. We want to make sure
28no host drivers attempt to attach to the PCI device in question.
29
30First we need to find the PCIID for the device in question. We can find
31this through lcpci. Running lspci shows a bunch of devices. I've identified
32this device as my target:
33
34```
35$ lspci -nn | egrep "VGA|Audio"
3628:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] Ellesmere [Radeon RX 470/480/570/580] [1002:67df] (rev c7)
3728:00.1 Audio device [0403]: Advanced Micro Devices, Inc. [AMD/ATI] Ellesmere [Radeon RX 580] [1002:aaf0]
38
3929:00.0 VGA compatible controller [0300]: Advanced Micro Devices, Inc. [AMD/ATI] Redwood XT GL [FirePro V4800] [1002:68c8]
4029:00.1 Audio device [0403]: Advanced Micro Devices, Inc. [AMD/ATI] Redwood HDMI Audio [Radeon HD 5000 Series] [1002:aa60]
41
422b:00.3 Audio device [0403]: Advanced Micro Devices, Inc. [AMD] Device [1022:1457]
43```
44
45Now that we have our target PCI IDs (in this case, `1002:68c8,1002:aa60`), we can bind this device to
46the vfio-pci driver.
47
48**vfio-pci module**
49
50If your distro ships vfio-pci as a module, you will need to add the vfio drivers to the initial ramdisk
51to leverage them as early as possible in the boot process.
52
53Below is an example on Fedora:
54
55```
56echo 'add_drivers+="vfio vfio_iommu_type1 vfio_pci vfio_virqfd"' > /etc/dracut.conf.d/vfio.conf
57dracut -f --kver `uname -r`
58```
59
60**vfio reservation**
61
62Now that the requirements are met, we can attach the target GPU to the vfio driver using the information
63we have collected so far.
64
65Below, i've leveraged the PCI ID's collected above, and provided them to the vfio-pci driver via the kernel
66parameters at boot.
67
68> Be sure to replace <CPU> with amd or intel depending on your system.
69
70```
71rd.driver.pre=vfio-pci vfio-pci.ids=1002:68c8,1002:aa60 <CPU>_iommu=on
72```
73
74> YMMV: These steps differ a lot between distros
75
76On my EFI Fedora 26 system, I appended the line above to my /etc/sysconfig/grub config under GRUB_CMDLINE_LINUX.
77Then, regenerated my grub.cfg via ```grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg```
78
79
80Attach GPU to VM
81-----------------
82
83Now we reboot and cross our fingers.
84
85> If displays attached to the card are now black, then things are working as designed. If you see your
86> desktop on the target GPU, the vfio driver didn't properly bind to your card and something was done
87> incorrectly.
88
89On my AMD Linux system, we can see that IOMMU is active and functional:
90
91```
92kallisti5@eris ~ $ dmesg | egrep "IOMMU|AMD-Vi"
93[    0.650138] AMD-Vi: IOMMU performance counters supported
94[    0.652201] AMD-Vi: Found IOMMU at 0000:00:00.2 cap 0x40
95[    0.652201] AMD-Vi: Extended features (0xf77ef22294ada):
96[    0.652204] AMD-Vi: Interrupt remapping enabled
97[    0.652204] AMD-Vi: virtual APIC enabled
98[    0.652312] AMD-Vi: Lazy IO/TLB flushing enabled
99[    0.653841] perf/amd_iommu: Detected AMD IOMMU #0 (2 banks, 4 counters/bank).
100[    4.114847] AMD IOMMUv2 driver by Joerg Roedel <jroedel@suse.de>
101
102```
103
104And checking for vfio we can see it successfully took over my graphics card:
105```
106kallisti5@eris ~ $ dmesg | grep vfio
107[    3.928695] vfio-pci 0000:29:00.0: vgaarb: changed VGA decodes: olddecodes=io+mem,decodes=io+mem:owns=none
108[    3.940222] vfio_pci: add [1002:68c8[ffff:ffff]] class 0x000000/00000000
109[    3.952302] vfio_pci: add [1002:aa60[ffff:ffff]] class 0x000000/00000000
110[   35.629861] vfio-pci 0000:29:00.0: enabling device (0000 -> 0003)
111```
112
113On every boot, the device will be available for attachment to VM's
114Now, we simply attach the device to a VM:
115
116```
117sudo qemu-system-x86_64 --enable-kvm -hda haiku-nightly-anyboot.image -m 2048 -device pci-assign,host=29:00.0
118```
119
120If you're doing this for a graphics card generally the qemu window will
121lock up at the bootsplash and the video will appear on the second window.
122Click the qemu window to control the Haiku machine.
123