1 Haiku PCI Driver Development Under QEMU on Linux
2 ====================================================
4 Developing Haiku drivers for PCI and PCI Express cards is now a lot easier
5 given advancements in IOMMU under Linux. You can effectively detach PCI cards
6 from their host operating system and attach them to guest VM's resulting in
7 true hardware emulation. In this guide we will be configuring a secondary
8 graphics card to be attached to a Haiku virtual machine.
10 **Warning**: Any device attached to a VM will be unavailable to the host operating
11 system. This means you **cannot** use your primary graphics card, network device,
12 etc within a VM and the host operating system at the same time. In this
13 example, we have two graphics cards installed in the Linux system.
16 -----------------------
17 You will need to have IOMMU hardware support on your motherboard for this
18 to function. Most modern AMD A3 socket chips and Intel i3/i5/i7 devices
19 have IOMMU built in. If your board does indeed have IOMMU, you will likely
20 need to enable IOMMU within the bios of your motherboard before proceeding.
23 -----------------------
24 Now that you have an IOMMU enabled system, you will need to tell Linux to
25 fully utilize IOMMU. To do this, you will need to add a few kernel boot
26 parameters. Depending on how your system is configured, there may be a few
27 places to do this. Here are some example config files:
29 * `/etc/default/grub` (`GRUB_CMDLINE_LINUX_DEFAULT`)
30 * `/boot/grub/grub.cfg`
31 * `/boot/grub/menu.lst`
32 * `/boot/refind_linux.conf`
34 Enabling OS support IOMMU for IOMMU involves adding one of the following
35 kernel boot parameters:
46 Now, all we need to do is to reserve the PCI device. We want to make sure
47 no host drivers attempt to attach to the PCI device in question.
49 First we need to find the PCIID for the device in question. We can find
50 this through lcpci. Running lspci shows a bunch of devices. I've identified
51 this device as my target:
54 07:00.0 VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] Cedar [Radeon HD 5000]
57 Now, to get the PCI ID, I run lspci again with the -n flag (lspci -n). We find
58 the matching BUS ID and we get our PCI ID:
61 07:00.0 0300: 1002:68f9
64 Now that we have our target PCI ID (`1002:68f9`), we can bind this device to
65 a special pci-stub driver.
67 We will create two files for this graphics card:
69 **`/lib/modprobe.d/pci-stub.conf`:**
71 options pci-stub ids=1002:68f9
74 **`/lib/modprobe.d/drm.conf`:**
76 softdep drm pre: pci-stub
79 The first line tells the pci-stub driver to bind to the device in question.
80 The second line tells DRM (graphics driver stack) that it should make sure
81 pci-stub loads before DRM (ensuring the device is stubbed and not loaded by
84 Now we reboot and cross our fingers.
86 On my AMD Linux system, we can see that IOMMU is active and functional:
89 kallisti5@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
98 And checking for pci-stub we can see it successfully took over my graphics card:
100 kallisti5@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
105 On every boot, the device will be available for attachment to VM's
106 Now, we simply attach the device to a VM:
109 sudo qemu-system-x86_64 --enable-kvm -hda haiku-nightly-anyboot.image -m 2048 -device pci-assign,host=07:00.0
112 If you experience any problems, try looking at kvm messages:
114 kallisti5@eris ~ $ dmesg | grep kvm
117 If you're doing this for a graphics card generally the qemu window will
118 lock up at the bootsplash and the video will appear on the second window.
119 Click the qemu window to control the Haiku machine.
121 If things go well you will see:
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