Skip to contentSkip to conversation

fixups for pci passthrough

PATCH In review Glauber Costa 2 participants 8 messages Mar 26, 2008 – Mar 27, 2008 KVM

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 passthrough

add -passthrough usage string

---
 qemu/vl.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
qemu/vl.c +1 -0
index f5b2665..d36cfe3 100644
@@ -8053,6 +8053,7 @@ #ifdef USE_KVM
 #ifndef NO_CPU_EMULATION
 	   "-no-kvm         disable KVM hardware virtualization\n"
 #endif
+	   "-passthrough name/bus:dev.func-intr expose a pci device to the guest OS \n"
 	   "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
 #endif
 #ifdef TARGET_I386
Signature · 8 lines
1.4.2


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
#2 lore raw

Re: [PATCH 1/3] [PATCH] passthrough

Quoted 13 lines · Glauber Costa

add -passthrough usage string

---
 qemu/vl.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
qemu/vl.c +0 -0
index f5b2665..d36cfe3 100644

qemu/vl.c #ifdef USE_KVM · line 8059

Quoted 7 lines
qemu/vl.c +1 -0
@@ -8053,6 +8053,7 @@ #ifdef USE_KVM
 #ifndef NO_CPU_EMULATION
 	   "-no-kvm         disable KVM hardware virtualization\n"
 #endif
+	   "-passthrough name/bus:dev.func-intr expose a pci device to the guest OS \n"
 	   "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
 #endif
 #ifdef TARGET_I386

Not this patch's fault, of course, but it would be better to name the option -pcidevice, to be consistent with -usbdevice, and to show only pci devices are allowed.

Signature · 8 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
#3 lore raw

PATCH 2/3 unregister pci device

In reply to Glauber Costa

If we fail after we have allocated the pci device, we have to get rid of it. Otherwise, a bogus device will be found on the bus

---
 qemu/hw/passthrough/passthrough.c |   11 +++++++----
 qemu/hw/pci.c                     |    6 ++++++
 qemu/hw/pci.h                     |    2 ++
 3 files changed, 15 insertions(+), 4 deletions(-)
qemu/hw/passthrough/passthrough.c +7 -4
index a416098..64f0af9 100644
@@ -389,13 +389,13 @@ pt_dev_t *register_real_device(PCIBus * 
 	if (pt_get_real_device(pci_dev, r_bus, r_dev, r_func)) {
 		fprintf(logfile, "register_real_device: Error: Couldn't get "
 			"real device (%s)!\n", e_dev_name);
-		return NULL;
+		goto out;
 	}
 
 	/* handle real device's MMIO/PIO BARs */
 	if (pt_register_regions(pci_dev->real_device.regions,
 				pci_dev->real_device.region_number, pci_dev))
-		return (NULL);
+		goto out;
 
 	/* handle interrupt routing */
 	e_device = (pci_dev->dev.devfn >> 3) & 0x1f;
@@ -412,7 +412,7 @@ pt_dev_t *register_real_device(PCIBus * 
 		if (rc) {
 			fprintf(logfile, "pt_bind %d failed rc=%d\n",
 				pci_dev->mirq, rc);
-			return NULL;
+			goto out;
 		}
 		sprintf(pci_dev->sirq, "%d", pci_dev->mirq);
 	}
@@ -433,7 +433,7 @@ pt_dev_t *register_real_device(PCIBus * 
 			fprintf(stderr, "Could not notify kernel about "
 				"passthrough device\n");
 			perror("pt-ioctl:");
-			return NULL;
+			goto out;
 		}
 		fprintf(logfile, "Registered host PCI device %02x:%02x.%1x as "
 			"guest device %02x:%02x.%1x with hypercall support\n",
@@ -445,6 +445,9 @@ pt_dev_t *register_real_device(PCIBus * 
 		r_bus, r_dev, r_func, e_dev_name);
 
 	return (pci_dev);
+out:
+	pci_unregister_device(e_bus, &pci_dev->dev);
+	return NULL;
 }
 
 #define	MAX_PTDEVS 4
qemu/hw/pci.c +6 -0
index 72b65db..b605744 100644
@@ -185,6 +185,12 @@ PCIDevice *pci_register_device(PCIBus *b
     return pci_dev;
 }
 
+void pci_unregister_device(PCIBus *bus, PCIDevice *dev)
+{
+	bus->devices[dev->devfn] = NULL;
+	qemu_free(dev);
+}
+
 void pci_register_io_region(PCIDevice *pci_dev, int region_num,
                             uint32_t size, int type,
                             PCIMapIORegionFunc *map_func)
qemu/hw/pci.h +2 -0
index e870987..220189f 100644
@@ -70,6 +70,8 @@ PCIDevice *pci_register_device(PCIBus *b
                                PCIConfigReadFunc *config_read,
                                PCIConfigWriteFunc *config_write);
 
+void pci_unregister_device(PCIBus *bus, PCIDevice *dev);
+
 void pci_register_io_region(PCIDevice *pci_dev, int region_num,
                             uint32_t size, int type,
                             PCIMapIORegionFunc *map_func);
Signature · 8 lines
1.4.2


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
#4 lore raw

Re: [PATCH 2/3] [PATCH] unregister pci device

Quoted 4 lines · Glauber Costa

If we fail after we have allocated the pci device, we have to get rid of it. Otherwise, a bogus device will be found on the bus

Maybe, alter the order so the device is registered last thing. This may help avoid races with pci passthrough hotplug.

Signature · 8 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
#5 lore raw

Re: [PATCH 2/3] [PATCH] unregister pci device

Quoted 9 lines · Avi Kivity
Quoted 4 lines · Glauber Costa

If we fail after we have allocated the pci device, we have to get rid of it. Otherwise, a bogus device will be found on the bus

Maybe, alter the order so the device is registered last thing. This may help avoid races with pci passthrough hotplug.

That's the first thing I thought, but it makes things much more complicated. If really deserved, I can try again.

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace

#6 lore raw

Re: [PATCH 2/3] [PATCH] unregister pci device

Quoted 7 lines · Glauber Costa
Quoted 6 lines · Avi Kivity
Quoted 4 lines · Glauber Costa

If we fail after we have allocated the pci device, we have to get rid of it. Otherwise, a bogus device will be found on the bus

Quoted 5 lines
Quoted 3 lines

Maybe, alter the order so the device is registered last thing. This may help avoid races with pci passthrough hotplug.

That's the first thing I thought, but it makes things much more complicated. If really deserved, I can try again.

I don't think it's that critical at this stage.

Signature · 8 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
#7 lore raw

PATCH 3/3 remove warnings

In reply to Glauber Costa

everytime there is a warning, god kills a kitten

---
 libkvm/libkvm.h                   |    2 ++
 qemu/hw/passthrough/passthrough.c |    8 ++++++--
 qemu/hw/pc.h                      |    1 +
 qemu/hw/pci.h                     |    2 ++
 4 files changed, 11 insertions(+), 2 deletions(-)
libkvm/libkvm.h +2 -0
index 34d188b..f345f48 100644
@@ -23,6 +23,8 @@ #if defined(__x86_64__) || defined(__i38
 struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
 int kvm_get_msrs(kvm_context_t, int vcpu, struct kvm_msr_entry *msrs, int n);
 int kvm_set_msrs(kvm_context_t, int vcpu, struct kvm_msr_entry *msrs, int n);
+int kvm_assign_pv_pci_device(kvm_context_t kvm,
+			     struct kvm_pv_passthrough_dev *pv_pci_dev);
 #endif
 
 /*!
qemu/hw/passthrough/passthrough.c +6 -2
index 64f0af9..844c0e8 100644
@@ -17,6 +17,9 @@ #include "neo_pci_tree.h"
 typedef u64 resource_size_t;
 #define __deprecated 
 
+#include "hw/pci.h"
+#include "hw/pc.h"
+
 #include <linux/ioport.h>
 #include "passthrough.h"
 
@@ -352,7 +355,7 @@ pt_get_real_device(pt_dev_t *pci_dev, ui
 		rp->valid = 1;
 		rp->base_addr = start;
 		rp->size = size;
-		fprintf(logfile, "region %d size %d start 0x%x type %d "
+		fprintf(logfile, "region %d size %d start 0x%llx type %d "
 			"resource_fd %d\n", r, rp->size, start, rp->type,
 			rp->resource_fd);
 	}
@@ -468,7 +471,7 @@ static pt_dev_t **apicv[0xfe]; //0x10 - 
 #define IRQHOOK_DEV "/dev/irqhook"
 static pthread_t irqthread;
 
-void pt_irq(void *arg)
+void *pt_irq(void *arg)
 {
 	char buf[20];
 	int irq;
@@ -500,6 +503,7 @@ void pt_irq(void *arg)
 				dev->run = 1;
 		qemu_bh_schedule(ptbh);
 	}
+	return NULL;
 }
 
 static void pt_bh(void *p)
qemu/hw/pc.h +1 -0
index f640395..aec98d4 100644
@@ -101,6 +101,7 @@ int pcspk_audio_init(AudioState *, qemu_
 PCIBus *i440fx_init(PCIDevice **pi440fx_state, qemu_irq *pic);
 void i440fx_set_smm(PCIDevice *d, int val);
 int piix3_init(PCIBus *bus, int devfn);
+int piix3_get_pin(int pic_irq);
 void i440fx_init_memory_mappings(PCIDevice *d);
 
 int piix4_init(PCIBus *bus, int devfn);
qemu/hw/pci.h +2 -0
index 220189f..4858e3e 100644
@@ -91,6 +91,8 @@ PCIBus *pci_register_bus(pci_set_irq_fn 
 void pci_nic_init(PCIBus *bus, NICInfo *nd, int devfn);
 void pci_data_write(void *opaque, uint32_t addr, uint32_t val, int len);
 uint32_t pci_data_read(void *opaque, uint32_t addr, int len);
+int pci_map_irq(PCIDevice *pci_dev, int pin);
+
 int pci_bus_num(PCIBus *s);
 void pci_for_each_device(int bus_num, void (*fn)(PCIDevice *d));
 
Signature · 8 lines
1.4.2


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace