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. To do this, you will need to add a few kernel boot 26parameters. Depending on how your system is configured, there may be a few 27places to do this. Here are some example config files: 28 29 * `/etc/default/grub` (`GRUB_CMDLINE_LINUX_DEFAULT`) 30 * `/boot/grub/grub.cfg` 31 * `/boot/grub/menu.lst` 32 * `/boot/refind_linux.conf` 33 34Enabling OS support IOMMU for IOMMU involves adding one of the following 35kernel boot parameters: 36 37**AMD:** 38``` 39iommu=pt iommu=1 40``` 41**Intel:** 42``` 43intel_iommu=on 44``` 45 46Now, all we need to do is to reserve the PCI device. We want to make sure 47no host drivers attempt to attach to the PCI device in question. 48 49First we need to find the PCIID for the device in question. We can find 50this through lcpci. Running lspci shows a bunch of devices. I've identified 51this device as my target: 52 53``` 5407:00.0 VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] Cedar [Radeon HD 5000] 55``` 56 57Now, to get the PCI ID, I run lspci again with the -n flag (lspci -n). We find 58the matching BUS ID and we get our PCI ID: 59 60``` 6107:00.0 0300: 1002:68f9 62``` 63 64Now that we have our target PCI ID (`1002:68f9`), we can bind this device to 65a special pci-stub driver. 66 67We will create two files for this graphics card: 68 69**`/lib/modprobe.d/pci-stub.conf`:** 70``` 71options pci-stub ids=1002:68f9 72``` 73 74**`/lib/modprobe.d/drm.conf`:** 75``` 76softdep drm pre: pci-stub 77``` 78 79The first line tells the pci-stub driver to bind to the device in question. 80The second line tells DRM (graphics driver stack) that it should make sure 81pci-stub loads before DRM (ensuring the device is stubbed and not loaded by 82DRM). 83 84Now we reboot and cross our fingers. 85 86On my AMD Linux system, we can see that IOMMU is active and functional: 87 88``` 89kallisti5@eris ~ $ dmesg | grep AMD-Vi 90[ 0.119400] [Firmware Bug]: AMD-Vi: IOAPIC[9] not in IVRS table 91[ 0.119406] [Firmware Bug]: AMD-Vi: IOAPIC[10] not in IVRS table 92[ 0.119409] [Firmware Bug]: AMD-Vi: No southbridge IOAPIC found 93[ 0.119412] AMD-Vi: Disabling interrupt remapping 94[ 1.823122] AMD-Vi: Found IOMMU at 0000:00:00.2 cap 0x40 95[ 1.823253] AMD-Vi: Initialized for Passthrough Mode 96``` 97 98And checking for pci-stub we can see it successfully took over my graphics card: 99``` 100kallisti5@eris ~ $ dmesg | grep pci-stub 101[ 3.685970] pci-stub: add 1002:68F9 sub=FFFFFFFF:FFFFFFFF cls=00000000/00000000 102[ 3.686002] pci-stub 0000:07:00.0: claimed by stub 103``` 104 105On every boot, the device will be available for attachment to VM's 106Now, we simply attach the device to a VM: 107 108``` 109sudo qemu-system-x86_64 --enable-kvm -hda haiku-nightly-anyboot.image -m 2048 -device pci-assign,host=07:00.0 110``` 111 112If you experience any problems, try looking at kvm messages: 113``` 114kallisti5@eris ~ $ dmesg | grep kvm 115``` 116 117If you're doing this for a graphics card generally the qemu window will 118lock up at the bootsplash and the video will appear on the second window. 119Click the qemu window to control the Haiku machine. 120 121If things go well you will see: 122``` 123[ 1966.132176] kvm: Nested Virtualization enabled 124[ 1966.132185] kvm: Nested Paging enabled 125[ 1972.212231] pci-stub 0000:07:00.0: kvm assign device 126[ 1974.186382] kvm: zapping shadow pages for mmio generation wraparound 127``` 128