Skip to contentSkip to conversation

Xen PCI Passthrough

PATCH Superseded Anthony PERARD 2 participants 12 messages Apr 3, 2012 – May 22, 2012 Kbuild

v11 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v11 cover Xen PCI Passthrough

Hi all,

This patch series introduces the PCI passthrough for Xen.

Please review patches number 1, 2, 3, 4 and 7.

First, we have XenHostPCIDevice that help to access one PCI device of the host.

Then, the PCI passthrough device himself. Cut in 3 parts (or file), there is one to take care of the initialisation of a passthrough device. The second one handle everything about the config address space, there are specifics functions for every config register. The third one is to handle MSI.

There is a patch series on xen-devel (applied to xen-unstable) that add the support of setting a PCI passthrough device through QMP from libxl (xen tool stack). It is just a call to device_add, with the driver parametter hostaddr="0000:07:00.1".

Change v10-v11:
host-pci-device:
- update api: remove IORESOURCE_* defines from header file.
- address comment of Konrad.
xen-pt:
- fix memory mapping (Xen side) for a PCI BAR with a size under the size of a page.

Change v9-v10:
host-pci-device:
- rename to xen-host-pci-device.
- suppress usage of scanf and use strtol instead.
- add irq field
- no more goto loop
xen_pt:
- take machine_irq value from XenHostPCIDevice instead of reading it from the pci config space
- rename xen_pci_device to xen_pt
- use xen_pt as prefix
# this should fix namespace issue

Change v8-v9:
- rename PCI_DEVICE_ID_INTEL_82599_VF to PCI_DEVICE_ID_INTEL_82599_SFP_VF to be consistant with Linux.
- remove the patch about checking bar overlaps, the function is now in xen_pci_passthrough.c and uses pci_for_each_device.
- Introduce an opaque argument to the function pci_for_each_device.
- Fix the usage of memory listener: declare a stub function for every callback in the MemoryListener.

Change v7-v8:
- rework of the memory mapping of BARs. We now use a memory_listener to update a xen memory_mapping when a memory_region is updated.
- address few comment from Michael in the pci_check_overlap function.
- fix the handling of the ROM slot.

Change v6-v7:
- few fix and rebased on master
- remove of the power management capability, keep the minimum like if it is always desactivated.
- new patch: port of patch from the qemu-xen fork.

Change v5-v6:
- msitraslate code have been removed.
- code for the power management capability is removed, but will be re-added for the next version of the patch series as a separate patch.

- new patch to remove a check in pci_parse_devaddr.
- use pci_default_config_write, so no more hack to handle the BAR mapping in QEMU.
- improve the code in general (a bit more comprehensible).
- update to QOM.

Change v4-v5:
- return -errno if there is an error in host_pci_get_*
- rename internal function get_value to get_hex_value (and return the same error value has get_resource)

Change v3-v4:
- host_pci_get_* can now return an error, and take an extra parameter, a pointer to store the wanted value.
- The memory_region for the PCI BAR are handled "manualy" because calling pci_default_write_config was not possible, because the XenPT handle the PCIIORegion it self. This make possible to do a device_remove.
- Introduction of PT_ERR and PT_WARN macro to print debug and error messages. Also, these macro as well as PT_LOG will always print the short BDF of the device in the guest point of view.
- PT_ERR is print by default (for all error messages).
- Some debug/error message have been improve and should be a bit more useful.
- hw_error have been removed from the code, and have been replaced by either a call to qemu_system_shudown_request() (that lead to a domain destroy) or a failed in the initialisation of the device.
- Now, every patchs should compile with no error.

Change v2-v3;
- in host-pci-device.c:
- Return more usefull error code in get_ressource().
- Use macro in host_pci_find_ext_cap_offset instead of raw number. But I still not sure if PCI_MAX_EXT_CAP is right, it's result is 480 like it was before, so it's maybe ok.
- All use of MSI stuff in two first pci passthrough patch have been removed and move to the last patch.

Change v1-v2:
- fix style issue (checkpatch.pl)
- set the original authors, add some missing copyright headers
- HostPCIDevice:
- introduce HostPCIIORegions (with base_addr, size, flags)
- save all flags from ./resource and store it in a separate field.
- fix endianess on write
- new host_pci_dev_put function
- use pci.c like interface host_pci_get/set_byte/word/long (instead of host_pci_read/write_)
- compile HostPCIDevice only on linux (as well as xen_pci_passthrough)
- introduce apic-msidef.h file.
- no more run_one_timer, if a pci device is in the middle of a power transition, just "return an error" in config read/write
- use a global var mapped_machine_irq (local to xen_pci_passthrough.c)
- add msitranslate and power-mgmt ad qdev property

Allen Kay (2):
Introduce Xen PCI Passthrough, qdevice (1/3) Introduce Xen PCI Passthrough, PCI config space helpers (2/3)

Anthony PERARD (5):
pci_ids: Add INTEL_82599_SFP_VF id.
configure: Introduce --enable-xen-pci-passthrough. Introduce XenHostPCIDevice to access a pci device on the host. pci.c: Add opaque argument to pci_for_each_device. Introduce apic-msidef.h

Jiang Yunhong (1):
Introduce Xen PCI Passthrough, MSI (3/3)

 Makefile.target          |    6 +
 configure                |   25 +
 hw/apic-msidef.h         |   30 +
 hw/apic.c                |   11 +-
 hw/pci.c                 |   11 +-
 hw/pci.h                 |    4 +-
 hw/pci_ids.h             |    1 +
 hw/xen-host-pci-device.c |  393 ++++++++++
 hw/xen-host-pci-device.h |   55 ++
 hw/xen_common.h          |    3 +
 hw/xen_platform.c        |    8 +-
 hw/xen_pt.c              |  854 +++++++++++++++++++++
 hw/xen_pt.h              |  301 ++++++++
 hw/xen_pt_config_init.c  | 1869 ++++++++++++++++++++++++++++++++++++++++++++++
 hw/xen_pt_msi.c          |  620 +++++++++++++++
 xen-all.c                |   12 +
 16 files changed, 4184 insertions(+), 19 deletions(-)
 create mode 100644 hw/apic-msidef.h
 create mode 100644 hw/xen-host-pci-device.c
 create mode 100644 hw/xen-host-pci-device.h
 create mode 100644 hw/xen_pt.c
 create mode 100644 hw/xen_pt.h
 create mode 100644 hw/xen_pt_config_init.c
 create mode 100644 hw/xen_pt_msi.c
Signature · 1 line
Anthony PERARD
#1 lore raw

PATCH v11 1/8 pci_ids: Add INTEL_82599_SFP_VF id.

We are using this in our quirk lookup provided by patch
titled: Introduce Xen PCI Passthrough, PCI config space helpers.

---
 hw/pci_ids.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
hw/pci_ids.h +1 -0
index e8235a7..649e6b3 100644
@@ -118,6 +118,7 @@
 #define PCI_DEVICE_ID_INTEL_82801I_UHCI6 0x2939
 #define PCI_DEVICE_ID_INTEL_82801I_EHCI1 0x293a
 #define PCI_DEVICE_ID_INTEL_82801I_EHCI2 0x293c
+#define PCI_DEVICE_ID_INTEL_82599_SFP_VF 0x10ed
 
 #define PCI_VENDOR_ID_XEN               0x5853
 #define PCI_DEVICE_ID_XEN_PLATFORM      0x0001
Signature · 1 line
Anthony PERARD
#2 lore raw

PATCH v11 2/8 configure: Introduce --enable-xen-pci-passthrough.

In reply to Anthony PERARD
---
 configure |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
configure +25 -0
index 4b3adc9..c84a252 100755
@@ -136,6 +136,7 @@ vnc_png=""
 vnc_thread="no"
 xen=""
 xen_ctrl_version=""
+xen_pci_passthrough=""
 linux_aio=""
 cap_ng=""
 attr=""
@@ -682,6 +683,10 @@ for opt do
   ;;
   --enable-xen) xen="yes"
   ;;
+  --disable-xen-pci-passthrough) xen_pci_passthrough="no"
+  ;;
+  --enable-xen-pci-passthrough) xen_pci_passthrough="yes"
+  ;;
   --disable-brlapi) brlapi="no"
   ;;
   --enable-brlapi) brlapi="yes"
@@ -1034,6 +1039,8 @@ echo "                           (affects only QEMU, not qemu-img)"
 echo "  --enable-mixemu          enable mixer emulation"
 echo "  --disable-xen            disable xen backend driver support"
 echo "  --enable-xen             enable xen backend driver support"
+echo "  --disable-xen-pci-passthrough"
+echo "  --enable-xen-pci-passthrough"
 echo "  --disable-brlapi         disable BrlAPI"
 echo "  --enable-brlapi          enable BrlAPI"
 echo "  --disable-vnc-tls        disable TLS encryption for VNC server"
@@ -1478,6 +1485,21 @@ EOF
   fi
 fi
 
+if test "$xen_pci_passthrough" != "no"; then
+  if test "$xen" = "yes" && test "$linux" = "yes"; then
+    xen_pci_passthrough=yes
+  else
+    if test "$xen_pci_passthrough" = "yes"; then
+      echo "ERROR"
+      echo "ERROR: User requested feature Xen PCI Passthrough"
+      echo "ERROR: but this feature require /sys from Linux"
+      echo "ERROR"
+      exit 1;
+    fi
+    xen_pci_passthrough=no
+  fi
+fi
+
 ##########################################
 # pkg-config probe
 
@@ -3643,6 +3665,9 @@ case "$target_arch2" in
     if test "$xen" = "yes" -a "$target_softmmu" = "yes" ; then
       target_phys_bits=64
       echo "CONFIG_XEN=y" >> $config_target_mak
+      if test "$xen_pci_passthrough" = yes; then
+        echo "CONFIG_XEN_PCI_PASSTHROUGH=y" >> "$config_target_mak"
+      fi
     else
       echo "CONFIG_NO_XEN=y" >> $config_target_mak
     fi
Signature · 1 line
Anthony PERARD
#3 lore raw

PATCH v11 3/8 Introduce XenHostPCIDevice to access a pci device on the host.

In reply to Anthony PERARD
---
 Makefile.target          |    3 +
 hw/xen-host-pci-device.c |  393 ++++++++++++++++++++++++++++++++++++++++++++++
 hw/xen-host-pci-device.h |   55 +++++++
 3 files changed, 451 insertions(+), 0 deletions(-)
 create mode 100644 hw/xen-host-pci-device.c
 create mode 100644 hw/xen-host-pci-device.h
Makefile.target +3 -0
index cff15f0..4c4e3ad 100644
@@ -237,6 +237,9 @@ obj-$(CONFIG_NO_XEN) += xen-stub.o
 
 obj-i386-$(CONFIG_XEN) += xen_platform.o
 
+# Xen PCI Passthrough
+obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
+
 # Inter-VM PCI shared memory
 CONFIG_IVSHMEM =
 ifeq ($(CONFIG_KVM), y)
hw/xen-host-pci-device.c new file +393 -0

393 lines not on this page · raw message

hw/xen-host-pci-device.h new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
#4 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH V11 3/8] Introduce XenHostPCIDevice to access a pci device on the host.

Quoted 1 line · Anthony PERARD

Looks good, thought I've just couple of tiny comments:

Quoted 1 line

+#define XEN_HOST_PCI_RESSOURCE_BUFFER_SIZE 512

You might want a comment explaining why 512, and not a more precise number like 741.

Quoted 1 line

+{

.. snip..

hw/xen-host-pci-device.c line 75

Quoted 7 lines
hw/xen-host-pci-device.c new file +7 -0
+    do {
+        rc = read(fd, &buf, sizeof (buf) - 1);
+        if (rc < 0 && errno != EINTR) {
+            rc = -errno;
+            goto out;
+        }
+    } while (rc < 0);

Ok, you read it in. Maybe my 'wc' magic is gone, but this is what I get:
[root@localhost 0000:00:02.0]# cat resource | wc -c 741
.. snip..

Quoted 1 line

+#define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 42

The answer to the life? Can you provide a comment explaining the reason why it is 42, please?

Quoted 3 lines
hw/xen-host-pci-device.c new file +3 -0
+static int xen_host_pci_get_value(XenHostPCIDevice *d, const char *name,
+                                  unsigned int *pvalue, int base)
+{
#5 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH V11 3/8] Introduce XenHostPCIDevice to access a pci device on the host.

Quoted 9 lines · Konrad Rzeszutek Wilk
Quoted 1 line · Anthony PERARD

Looks good, thought I've just couple of tiny comments:

Quoted 1 line

+#define XEN_HOST_PCI_RESSOURCE_BUFFER_SIZE 512

You might want a comment explaining why 512, and not a more precise number like 741.

Actually, I only need the first 7 line of the resources file. So this is 399 bytes. I just add a little bit more.

Quoted 15 lines
Quoted 1 line

+{

.. snip..

hw/xen-host-pci-device.c

Quoted 7 lines
hw/xen-host-pci-device.c new file +7 -0
+    do {
+        rc = read(fd,&buf, sizeof (buf) - 1);
+        if (rc<  0&&  errno != EINTR) {
+            rc = -errno;
+            goto out;
+        }
+    } while (rc<  0);

Ok, you read it in. Maybe my 'wc' magic is gone, but this is what I get:
[root@localhost 0000:00:02.0]# cat resource | wc -c 741
.. snip..

Quoted 4 lines
Quoted 1 line

+#define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 42

The answer to the life? Can you provide a comment explaining the reason why it is 42, please?

I just define more than needed, and 42 is a good number :-).
But the maximum I need is probably 7 (for example '0x8086\n', for a vendor id). Or 20, number of digit in LONG_MAX, base 10.

Quoted 3 lines
Quoted 3 lines
hw/xen-host-pci-device.c new file +3 -0
+static int xen_host_pci_get_value(XenHostPCIDevice *d, const char *name,
+                                  unsigned int *pvalue, int base)
+{
Signature · 1 line
Anthony PERARD
#6 lore raw

PATCH v11 4/8 pci.c: Add opaque argument to pci_for_each_device.

In reply to Anthony PERARD

The purpose is to have a more generic pci_for_each_device by passing an extra argument to the function called on every device.

This patch will be used in a next patch.

---
 hw/pci.c          |   11 +++++++----
 hw/pci.h          |    4 +++-
 hw/xen_platform.c |    8 ++++----
 3 files changed, 14 insertions(+), 9 deletions(-)
hw/pci.c +7 -4
index 77001fa..49f1bf0 100644
@@ -1123,7 +1123,9 @@ static const pci_class_desc pci_class_descriptions[] =
 };
 
 static void pci_for_each_device_under_bus(PCIBus *bus,
-                                          void (*fn)(PCIBus *b, PCIDevice *d))
+                                          void (*fn)(PCIBus *b, PCIDevice *d,
+                                                     void *opaque),
+                                          void *opaque)
 {
     PCIDevice *d;
     int devfn;
@@ -1131,18 +1133,19 @@ static void pci_for_each_device_under_bus(PCIBus *bus,
     for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
         d = bus->devices[devfn];
         if (d) {
-            fn(bus, d);
+            fn(bus, d, opaque);
         }
     }
 }
 
 void pci_for_each_device(PCIBus *bus, int bus_num,
-                         void (*fn)(PCIBus *b, PCIDevice *d))
+                         void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
+                         void *opaque)
 {
     bus = pci_find_bus(bus, bus_num);
 
     if (bus) {
-        pci_for_each_device_under_bus(bus, fn);
+        pci_for_each_device_under_bus(bus, fn, opaque);
     }
 }
 
hw/pci.h +3 -1
index 4f19fdb..2827fd1 100644
@@ -296,7 +296,9 @@ PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
 PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
                                const char *default_devaddr);
 int pci_bus_num(PCIBus *s);
-void pci_for_each_device(PCIBus *bus, int bus_num, void (*fn)(PCIBus *bus, PCIDevice *d));
+void pci_for_each_device(PCIBus *bus, int bus_num,
+                         void (*fn)(PCIBus *bus, PCIDevice *d, void *opaque),
+                         void *opaque);
 PCIBus *pci_find_root_bus(int domain);
 int pci_find_domain(const PCIBus *bus);
 PCIBus *pci_find_bus(PCIBus *bus, int bus_num);
hw/xen_platform.c +4 -4
index 5a7c4cc..88ff5e8 100644
@@ -83,7 +83,7 @@ static void log_writeb(PCIXenPlatformState *s, char val)
 #define UNPLUG_ALL_NICS 2
 #define UNPLUG_AUX_IDE_DISKS 4
 
-static void unplug_nic(PCIBus *b, PCIDevice *d)
+static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
 {
     if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
             PCI_CLASS_NETWORK_ETHERNET) {
@@ -93,10 +93,10 @@ static void unplug_nic(PCIBus *b, PCIDevice *d)
 
 static void pci_unplug_nics(PCIBus *bus)
 {
-    pci_for_each_device(bus, 0, unplug_nic);
+    pci_for_each_device(bus, 0, unplug_nic, NULL);
 }
 
-static void unplug_disks(PCIBus *b, PCIDevice *d)
+static void unplug_disks(PCIBus *b, PCIDevice *d, void *o)
 {
     if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
             PCI_CLASS_STORAGE_IDE) {
@@ -106,7 +106,7 @@ static void unplug_disks(PCIBus *b, PCIDevice *d)
 
 static void pci_unplug_disks(PCIBus *bus)
 {
-    pci_for_each_device(bus, 0, unplug_disks);
+    pci_for_each_device(bus, 0, unplug_disks, NULL);
 }
 
 static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
Signature · 1 line
Anthony PERARD
#7 lore raw

PATCH v11 5/8 Introduce Xen PCI Passthrough, qdevice (1/3)

In reply to Anthony PERARD

From: Allen Kay <[email protected]>

A more complete history can be found here:
git://xenbits.xensource.com/qemu-xen-unstable.git

---
 Makefile.target         |    2 +
 hw/xen_common.h         |    3 +
 hw/xen_pt.c             |  815 +++++++++++++++++++++++++++++++++++++++++++++++
 hw/xen_pt.h             |  248 ++++++++++++++
 hw/xen_pt_config_init.c |   11 +
 xen-all.c               |   12 +
 6 files changed, 1091 insertions(+), 0 deletions(-)
 create mode 100644 hw/xen_pt.c
 create mode 100644 hw/xen_pt.h
 create mode 100644 hw/xen_pt_config_init.c
Makefile.target +2 -0
index 4c4e3ad..413198d 100644
@@ -239,6 +239,8 @@ obj-i386-$(CONFIG_XEN) += xen_platform.o
 
 # Xen PCI Passthrough
 obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
+obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o
+obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt_config_init.o
 
 # Inter-VM PCI shared memory
 CONFIG_IVSHMEM =
hw/xen_common.h +3 -0
index 0409ac7..48916fd 100644
@@ -135,4 +135,7 @@ static inline int xc_fd(xc_interface *xen_xc)
 
 void destroy_hvm_domain(void);
 
+/* shutdown/destroy current domain because of an error */
+void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
+
 #endif /* QEMU_HW_XEN_COMMON_H */
hw/xen_pt.c new file +815 -0

815 lines not on this page · raw message

hw/xen_pt.h new file +248 -0

248 lines not on this page · raw message

hw/xen_pt_config_init.c new file +11 -0
new file mode 100644
index 0000000..64d22e8
@@ -0,0 +1,11 @@
+#include "xen_pt.h"
+
+XenPTRegGroup *xen_pt_find_reg_grp(XenPCIPassthroughState *s, uint32_t address)
+{
+    return NULL;
+}
+
+XenPTReg *xen_pt_find_reg(XenPTRegGroup *reg_grp, uint32_t address)
+{
+    return NULL;
+}
xen-all.c +12 -0
index 3e6de41..e9daf8e 100644
@@ -1145,3 +1145,15 @@ void xen_register_framebuffer(MemoryRegion *mr)
 {
     framebuffer = mr;
 }
+
+void xen_shutdown_fatal_error(const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    va_end(ap);
+    fprintf(stderr, "Will destroy the domain.\n");
+    /* destroy the domain */
+    qemu_system_shutdown_request();
+}
Signature · 1 line
Anthony PERARD
#8 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH V11 5/8] Introduce Xen PCI Passthrough, qdevice (1/3)

hw/xen_pt.c line 58

Quoted 7 lines
hw/xen_pt.c new file +7 -0
+void xen_pt_log(const PCIDevice *d, const char *f, ...)
+{
+    va_list ap;
+
+    va_start(ap, f);
+    if (d) {
+        fprintf(stderr, "[%02x:%02x.%x] ", pci_bus_num(d->bus),

%d at the end and in the other location pls fix it up..

hw/xen_pt.c line 66

Quoted 8 lines
hw/xen_pt.c new file +8 -0
+                PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
+    }
+    vfprintf(stderr, f, ap);
+    va_end(ap);
+}
+
+/* Config Space */
+

.. Is there a way to make this function (and the following one) squahed?

Quoted 225 lines
hw/xen_pt.c new file +225 -0
+static uint32_t xen_pt_pci_read_config(PCIDevice *d, uint32_t addr, int len)
+{
+    XenPCIPassthroughState *s = DO_UPCAST(XenPCIPassthroughState, dev, d);
+    uint32_t val = 0;
+    XenPTRegGroup *reg_grp_entry = NULL;
+    XenPTReg *reg_entry = NULL;
+    int rc = 0;
+    int emul_len = 0;
+    uint32_t find_addr = addr;
+
+    if (xen_pt_pci_config_access_check(d, addr, len)) {
+        goto exit;
+    }
+
+    /* find register group entry */
+    reg_grp_entry = xen_pt_find_reg_grp(s, addr);
+    if (reg_grp_entry) {
+        /* check 0-Hardwired register group */
+        if (reg_grp_entry->reg_grp->grp_type == XEN_PT_GRP_TYPE_HARDWIRED) {
+            /* no need to emulate, just return 0 */
+            val = 0;
+            goto exit;
+        }
+    }
+
+    /* read I/O device register value */
+    rc = xen_host_pci_get_block(&s->real_device, addr, (uint8_t *)&val, len);
+    if (rc < 0) {
+        XEN_PT_ERR(d, "pci_read_block failed. return value: %d.\n", rc);
+        memset(&val, 0xff, len);
+    }
+
+    /* just return the I/O device register value for
+     * passthrough type register group */
+    if (reg_grp_entry == NULL) {
+        goto exit;
+    }
+
+    /* adjust the read value to appropriate CFC-CFF window */
+    val <<= (addr & 3) << 3;
+    emul_len = len;
+
+    /* loop around the guest requested size */
+    while (emul_len > 0) {
+        /* find register entry to be emulated */
+        reg_entry = xen_pt_find_reg(reg_grp_entry, find_addr);
+        if (reg_entry) {
+            XenPTRegInfo *reg = reg_entry->reg;
+            uint32_t real_offset = reg_grp_entry->base_offset + reg->offset;
+            uint32_t valid_mask = 0xFFFFFFFF >> ((4 - emul_len) << 3);
+            uint8_t *ptr_val = NULL;
+
+            valid_mask <<= (find_addr - real_offset) << 3;
+            ptr_val = (uint8_t *)&val + (real_offset & 3);
+
+            /* do emulation based on register size */
+            switch (reg->size) {
+            case 1:
+                if (reg->u.b.read) {
+                    rc = reg->u.b.read(s, reg_entry, ptr_val, valid_mask);
+                }
+                break;
+            case 2:
+                if (reg->u.w.read) {
+                    rc = reg->u.w.read(s, reg_entry,
+                                       (uint16_t *)ptr_val, valid_mask);
+                }
+                break;
+            case 4:
+                if (reg->u.dw.read) {
+                    rc = reg->u.dw.read(s, reg_entry,
+                                        (uint32_t *)ptr_val, valid_mask);
+                }
+                break;
+            }
+
+            if (rc < 0) {
+                xen_shutdown_fatal_error("Internal error: Invalid read "
+                                         "emulation. (%s, rc: %d)\n",
+                                         __func__, rc);
+                return 0;
+            }
+
+            /* calculate next address to find */
+            emul_len -= reg->size;
+            if (emul_len > 0) {
+                find_addr = real_offset + reg->size;
+            }
+        } else {
+            /* nothing to do with passthrough type register,
+             * continue to find next byte */
+            emul_len--;
+            find_addr++;
+        }
+    }
+
+    /* need to shift back before returning them to pci bus emulator */
+    val >>= ((addr & 3) << 3);
+
+exit:
+    XEN_PT_LOG_CONFIG(d, addr, val, len);
+    return val;
+}
+
+static void xen_pt_pci_write_config(PCIDevice *d, uint32_t addr,
+                                    uint32_t val, int len)
+{
+    XenPCIPassthroughState *s = DO_UPCAST(XenPCIPassthroughState, dev, d);
+    int index = 0;
+    XenPTRegGroup *reg_grp_entry = NULL;
+    int rc = 0;
+    uint32_t read_val = 0;
+    int emul_len = 0;
+    XenPTReg *reg_entry = NULL;
+    uint32_t find_addr = addr;
+    XenPTRegInfo *reg = NULL;
+
+    if (xen_pt_pci_config_access_check(d, addr, len)) {
+        return;
+    }
+
+    XEN_PT_LOG_CONFIG(d, addr, val, len);
+
+    /* check unused BAR register */
+    index = xen_pt_bar_offset_to_index(addr);
+    if ((index >= 0) && (val > 0 && val < XEN_PT_BAR_ALLF) &&
+        (s->bases[index].bar_flag == XEN_PT_BAR_FLAG_UNUSED)) {
+        XEN_PT_WARN(d, "Guest attempt to set address to unused Base Address "
+                    "Register. (addr: 0x%02x, len: %d)\n", addr, len);
+    }
+
+    /* find register group entry */
+    reg_grp_entry = xen_pt_find_reg_grp(s, addr);
+    if (reg_grp_entry) {
+        /* check 0-Hardwired register group */
+        if (reg_grp_entry->reg_grp->grp_type == XEN_PT_GRP_TYPE_HARDWIRED) {
+            /* ignore silently */
+            XEN_PT_WARN(d, "Access to 0-Hardwired register. "
+                        "(addr: 0x%02x, len: %d)\n", addr, len);
+            return;
+        }
+    }
+
+    rc = xen_host_pci_get_block(&s->real_device, addr,
+                                (uint8_t *)&read_val, len);
+    if (rc < 0) {
+        XEN_PT_ERR(d, "pci_read_block failed. return value: %d.\n", rc);
+        memset(&read_val, 0xff, len);
+    }
+
+    /* pass directly to the real device for passthrough type register group */
+    if (reg_grp_entry == NULL) {
+        goto out;
+    }
+
+    memory_region_transaction_begin();
+    pci_default_write_config(d, addr, val, len);
+
+    /* adjust the read and write value to appropriate CFC-CFF window */
+    read_val <<= (addr & 3) << 3;
+    val <<= (addr & 3) << 3;
+    emul_len = len;
+
+    /* loop around the guest requested size */
+    while (emul_len > 0) {
+        /* find register entry to be emulated */
+        reg_entry = xen_pt_find_reg(reg_grp_entry, find_addr);
+        if (reg_entry) {
+            reg = reg_entry->reg;
+            uint32_t real_offset = reg_grp_entry->base_offset + reg->offset;
+            uint32_t valid_mask = 0xFFFFFFFF >> ((4 - emul_len) << 3);
+            uint8_t *ptr_val = NULL;
+
+            valid_mask <<= (find_addr - real_offset) << 3;
+            ptr_val = (uint8_t *)&val + (real_offset & 3);
+
+            /* do emulation based on register size */
+            switch (reg->size) {
+            case 1:
+                if (reg->u.b.write) {
+                    rc = reg->u.b.write(s, reg_entry, ptr_val,
+                                        read_val >> ((real_offset & 3) << 3),
+                                        valid_mask);
+                }
+                break;
+            case 2:
+                if (reg->u.w.write) {
+                    rc = reg->u.w.write(s, reg_entry, (uint16_t *)ptr_val,
+                                        (read_val >> ((real_offset & 3) << 3)),
+                                        valid_mask);
+                }
+                break;
+            case 4:
+                if (reg->u.dw.write) {
+                    rc = reg->u.dw.write(s, reg_entry, (uint32_t *)ptr_val,
+                                         (read_val >> ((real_offset & 3) << 3)),
+                                         valid_mask);
+                }
+                break;
+            }
+
+            if (rc < 0) {
+                xen_shutdown_fatal_error("Internal error: Invalid write"
+                                         " emulation. (%s, rc: %d)\n",
+                                         __func__, rc);
+                return;
+            }
+
+            /* calculate next address to find */
+            emul_len -= reg->size;
+            if (emul_len > 0) {
+                find_addr = real_offset + reg->size;
+            }
+        } else {
+            /* nothing to do with passthrough type register,
+             * continue to find next byte */
+            emul_len--;
+            find_addr++;
+        }
+    }
+
+    /* need to shift back before passing them to xen_host_pci_device */
+    val >>= (addr & 3) << 3;
+
+    memory_region_transaction_commit();
#9 lore raw

PATCH v11 6/8 Introduce Xen PCI Passthrough, PCI config space helpers (2/3)

In reply to Anthony PERARD

From: Allen Kay <[email protected]>

A more complete history can be found here:
git://xenbits.xensource.com/qemu-xen-unstable.git

---
 hw/xen_pt.c             |   10 +
 hw/xen_pt.h             |    2 +
 hw/xen_pt_config_init.c | 1387 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1399 insertions(+), 0 deletions(-)
hw/xen_pt.c +10 -0
index afa985a..c2e211a 100644
@@ -676,6 +676,13 @@ static int xen_pt_initfn(PCIDevice *d)
     /* Handle real device's MMIO/PIO BARs */
     xen_pt_register_regions(s);
 
+    /* reinitialize each config register to be emulated */
+    if (xen_pt_config_init(s)) {
+        XEN_PT_ERR(d, "PCI Config space initialisation failed.\n");
+        xen_host_pci_device_put(&s->real_device);
+        return -1;
+    }
+
     /* Bind interrupt */
     if (!s->dev.config[PCI_INTERRUPT_PIN]) {
         XEN_PT_LOG(d, "no pin interrupt\n");
@@ -774,6 +781,9 @@ static int xen_pt_unregister_device(PCIDevice *d)
         }
     }
 
+    /* delete all emulated config registers */
+    xen_pt_config_delete(s);
+
     xen_pt_unregister_regions(s);
     memory_listener_unregister(&s->memory_listener);
 
hw/xen_pt.h +2 -0
index f029841..4bb8cb6 100644
@@ -62,6 +62,8 @@ typedef int (*xen_pt_conf_byte_read)
 #define XEN_PT_BAR_ALLF 0xFFFFFFFF
 #define XEN_PT_BAR_UNMAPPED (-1)
 
+#define PCI_CAP_MAX 48
+
 
 typedef enum {
     XEN_PT_GRP_TYPE_HARDWIRED = 0,  /* 0 Hardwired reg group */
hw/xen_pt_config_init.c +1387 -0

1,398 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
#10 lore raw

PATCH v11 7/8 Introduce apic-msidef.h

In reply to Anthony PERARD

This patch move the msi definition from apic.c to apic-msidef.h. So it can be used also by other .c files.

---
 hw/apic-msidef.h |   30 ++++++++++++++++++++++++++++++
 hw/apic.c        |   11 +----------
 2 files changed, 31 insertions(+), 10 deletions(-)
 create mode 100644 hw/apic-msidef.h
hw/apic-msidef.h new file +30 -0
new file mode 100644
index 0000000..6e2eb71
@@ -0,0 +1,30 @@
+#ifndef HW_APIC_MSIDEF_H
+#define HW_APIC_MSIDEF_H
+
+/*
+ * Intel APIC constants: from include/asm/msidef.h
+ */
+
+/*
+ * Shifts for MSI data
+ */
+
+#define MSI_DATA_VECTOR_SHIFT           0
+#define  MSI_DATA_VECTOR_MASK           0x000000ff
+
+#define MSI_DATA_DELIVERY_MODE_SHIFT    8
+#define MSI_DATA_LEVEL_SHIFT            14
+#define MSI_DATA_TRIGGER_SHIFT          15
+
+/*
+ * Shift/mask fields for msi address
+ */
+
+#define MSI_ADDR_DEST_MODE_SHIFT        2
+
+#define MSI_ADDR_REDIRECTION_SHIFT      3
+
+#define MSI_ADDR_DEST_ID_SHIFT          12
+#define  MSI_ADDR_DEST_ID_MASK          0x00ffff0
+
+#endif /* HW_APIC_MSIDEF_H */
hw/apic.c +1 -10
index 4eeaf88..a8da2f1 100644
@@ -22,19 +22,10 @@
 #include "host-utils.h"
 #include "trace.h"
 #include "pc.h"
+#include "apic-msidef.h"
 
 #define MAX_APIC_WORDS 8
 
-/* Intel APIC constants: from include/asm/msidef.h */
-#define MSI_DATA_VECTOR_SHIFT		0
-#define MSI_DATA_VECTOR_MASK		0x000000ff
-#define MSI_DATA_DELIVERY_MODE_SHIFT	8
-#define MSI_DATA_TRIGGER_SHIFT		15
-#define MSI_DATA_LEVEL_SHIFT		14
-#define MSI_ADDR_DEST_MODE_SHIFT	2
-#define MSI_ADDR_DEST_ID_SHIFT		12
-#define	MSI_ADDR_DEST_ID_MASK		0x00ffff0
-
 #define SYNC_FROM_VAPIC                 0x1
 #define SYNC_TO_VAPIC                   0x2
 #define SYNC_ISR_IRR_TO_VAPIC           0x4
Signature · 1 line
Anthony PERARD
#11 lore raw

PATCH v11 8/8 Introduce Xen PCI Passthrough, MSI (3/3)

In reply to Anthony PERARD

From: Jiang Yunhong <[email protected]>

A more complete history can be found here:
git://xenbits.xensource.com/qemu-xen-unstable.git

---
 Makefile.target         |    1 +
 hw/xen_pt.c             |   31 +++-
 hw/xen_pt.h             |   51 ++++
 hw/xen_pt_config_init.c |  471 +++++++++++++++++++++++++++++++++++
 hw/xen_pt_msi.c         |  620 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 1173 insertions(+), 1 deletions(-)
 create mode 100644 hw/xen_pt_msi.c
Makefile.target +1 -0
index 413198d..1903b92 100644
@@ -241,6 +241,7 @@ obj-i386-$(CONFIG_XEN) += xen_platform.o
 obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o
 obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o
 obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt_config_init.o
+obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt_msi.o
 
 # Inter-VM PCI shared memory
 CONFIG_IVSHMEM =
hw/xen_pt.c +30 -1
index c2e211a..f677919 100644
@@ -36,6 +36,20 @@
  *
  *     Write '1'
  *       - Set real bit to '1'.
+ *
+ * MSI interrupt:
+ *   Initialize MSI register(xen_pt_msi_setup, xen_pt_msi_update)
+ *     Bind MSI(xc_domain_update_msi_irq)
+ *       <fail>
+ *         - Unmap MSI.
+ *         - Set dev->msi->pirq to '-1'.
+ *
+ * MSI-X interrupt:
+ *   Initialize MSI-X register(xen_pt_msix_update_one)
+ *     Bind MSI-X(xc_domain_update_msi_irq)
+ *       <fail>
+ *         - Unmap MSI-X.
+ *         - Set entry->pirq to '-1'.
  */
 
 #include <sys/ioctl.h>
@@ -534,7 +548,15 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
     };
 
     bar = xen_pt_bar_from_region(s, mr);
-    if (bar == -1) {
+    if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
+        return;
+    }
+
+    if (s->msix && &s->msix->mmio == mr) {
+        if (adding) {
+            s->msix->mmio_base_addr = sec->offset_within_address_space;
+            rc = xen_pt_msix_update_remap(s, s->msix->bar_index);
+        }
         return;
     }
 
@@ -767,6 +789,13 @@ static int xen_pt_unregister_device(PCIDevice *d)
         }
     }
 
+    if (s->msi) {
+        xen_pt_msi_disable(s);
+    }
+    if (s->msix) {
+        xen_pt_msix_disable(s);
+    }
+
     if (machine_irq) {
         xen_pt_mapped_machine_irq[machine_irq]--;
 
hw/xen_pt.h +51 -0

67 lines not on this page · raw message

hw/xen_pt_config_init.c +471 -0

501 lines not on this page · raw message

hw/xen_pt_msi.c new file +620 -0

620 lines not on this page · raw message

Signature · 1 line
Anthony PERARD