Skip to contentSkip to conversation

Xen PCI Passthrough

PATCH RESEND Superseded Anthony PERARD 4 participants 26 messages Mar 16, 2012 – May 16, 2012 Kbuild

v8 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v8 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 HostPCIDevice that help to access one PCI device of the host.

Then, there is an additions in the QEMU code, pci_check_bar_overlap.

There are also several change in pci_ids and pci_regs.

Last part, but not least, 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 since the previous set:
- 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 (4):
pci_ids: Add INTEL_82599_VF id.
configure: Introduce --enable-xen-pci-passthrough. Introduce HostPCIDevice to access a pci device on the host. Introduce apic-msidef.h

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

Yuji Shimada (1):
pci.c: Add pci_check_bar_overlap

 Makefile.target                      |    6 +
 configure                            |   25 +
 hw/apic-msidef.h                     |   30 +
 hw/apic.c                            |   11 +-
 hw/host-pci-device.c                 |  278 +++++
 hw/host-pci-device.h                 |   75 ++
 hw/pci.c                             |   50 +
 hw/pci.h                             |    5 +
 hw/pci_ids.h                         |    1 +
 hw/xen_common.h                      |    3 +
 hw/xen_pci_passthrough.c             |  771 ++++++++++++++
 hw/xen_pci_passthrough.h             |  303 ++++++
 hw/xen_pci_passthrough_config_init.c | 1866 ++++++++++++++++++++++++++++++++++
 hw/xen_pci_passthrough_msi.c         |  615 +++++++++++
 xen-all.c                            |   12 +
 15 files changed, 4041 insertions(+), 10 deletions(-)
 create mode 100644 hw/apic-msidef.h
 create mode 100644 hw/host-pci-device.c
 create mode 100644 hw/host-pci-device.h
 create mode 100644 hw/xen_pci_passthrough.c
 create mode 100644 hw/xen_pci_passthrough.h
 create mode 100644 hw/xen_pci_passthrough_config_init.c
 create mode 100644 hw/xen_pci_passthrough_msi.c
Signature · 1 line
Anthony PERARD
#1 lore raw

PATCH RESEND v8 1/8 pci_ids: Add INTEL_82599_VF id.

---
 hw/pci_ids.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
hw/pci_ids.h +1 -0
index e8235a7..943106a 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_VF     0x10ed
 
 #define PCI_VENDOR_ID_XEN               0x5853
 #define PCI_DEVICE_ID_XEN_PLATFORM      0x0001
Signature · 1 line
Anthony PERARD
#2 lore raw

Re: [Qemu-devel] [PATCH V8 RESEND 1/8] pci_ids: Add INTEL_82599_VF id.

Quoted 10 lines · Anthony PERARD
---
 hw/pci_ids.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
hw/pci_ids.h +0 -0
index e8235a7..943106a 100644

hw/pci_ids.h line 121

Quoted 4 lines
hw/pci_ids.h +1 -0
@@ -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_VF     0x10ed

maybe it should be PCI_DEVICE_ID_INTEL_82599_SFP_VF for consistency with Linux?

#3 lore raw

Re: [Qemu-devel] [PATCH V8 RESEND 1/8] pci_ids: Add INTEL_82599_VF id.

Quoted 6 lines · Stefano Stabellini
Quoted 5 lines · Anthony PERARD
---
  hw/pci_ids.h |    1 +
  1 files changed, 1 insertions(+), 0 deletions(-)
Quoted 12 lines

hw/pci_ids.h line 121

Quoted 9 lines
hw/pci_ids.h +1 -0
index e8235a7..943106a 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_VF     0x10ed

maybe it should be PCI_DEVICE_ID_INTEL_82599_SFP_VF for consistency with Linux?

Ok, I'll change that.

Regards,

Signature · 1 line
Anthony PERARD
#4 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH V8 RESEND 1/8] pci_ids: Add INTEL_82599_VF id.

Quoted 24 lines · Anthony PERARD
Quoted 8 lines · Stefano Stabellini
Quoted 6 lines · Anthony PERARD

---
 hw/pci_ids.h |    1 +

 1 files changed, 1 insertions(+), 0 deletions(-)
Quoted 13 lines

hw/pci_ids.h line 121

Quoted 9 lines
hw/pci_ids.h +1 -0
index e8235a7..943106a 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_VF     0x10ed

maybe it should be PCI_DEVICE_ID_INTEL_82599_SFP_VF for consistency with Linux?

Quoted 2 lines

Ok, I'll change that.

And can you add why you need this?

#5 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH V8 RESEND 1/8] pci_ids: Add INTEL_82599_VF id.

Quoted 26 lines · Konrad Rzeszutek Wilk
Quoted 24 lines · Anthony PERARD
Quoted 21 lines · Stefano Stabellini
Quoted 11 lines · Anthony PERARD

---
 hw/pci_ids.h |    1 +

 1 files changed, 1 insertions(+), 0 deletions(-)
hw/pci_ids.h +0 -0
index e8235a7..943106a 100644

hw/pci_ids.h line 121

Quoted 4 lines
hw/pci_ids.h +1 -0
@@ -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_VF     0x10ed

maybe it should be PCI_DEVICE_ID_INTEL_82599_SFP_VF for consistency with Linux?

Quoted 4 lines
Quoted 2 lines

Ok, I'll change that.

And can you add why you need this?

Done in the V11.

Signature · 1 line
Anthony PERARD
#6 lore raw

PATCH RESEND v8 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 afe7395..1d1204a 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
 
@@ -3628,6 +3650,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
#7 lore raw

Re: [Qemu-devel] [PATCH V8 RESEND 2/8] configure: Introduce --enable-xen-pci-passthrough.

Quoted 1 line · Anthony PERARD

ack

Quoted 70 lines
 configure |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
configure +25 -0
index afe7395..1d1204a 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
 
@@ -3628,6 +3650,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
#9 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH V8 RESEND 2/8] configure: Introduce --enable-xen-pci-passthrough.

Quoted 4 lines · Konrad Rzeszutek Wilk

I thought I reviewed this last time? Is there a reason for not attaching 'Reviewed-by: Konrad Rzeszutek Wilk <[email protected]>' on this patch?

Yes, one: you reviewed a later patch :-)

Your reviewed-by is in the last version of this series, V11.

Signature · 1 line
Anthony PERARD
#10 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH V8 RESEND 2/8] configure: Introduce --enable-xen-pci-passthrough.

Quoted 8 lines · Anthony PERARD
Quoted 4 lines · Konrad Rzeszutek Wilk

I thought I reviewed this last time? Is there a reason for not attaching 'Reviewed-by: Konrad Rzeszutek Wilk <[email protected]>' on this patch?

Yes, one: you reviewed a later patch :-)

Duh!

Quoted 10 lines

Your reviewed-by is in the last version of this series, V11.

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#11 lore raw

PATCH RESEND v8 3/8 Introduce HostPCIDevice to access a pci device on the host.

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

278 lines not on this page · raw message

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

75 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
#12 lore raw

Re: [Qemu-devel] [PATCH V8 RESEND 3/8] Introduce HostPCIDevice to access a pci device on the host.

Quoted 1 line · Anthony PERARD

ack

Quoted 389 lines
 Makefile.target      |    3 +
 hw/host-pci-device.c |  278 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/host-pci-device.h |   75 ++++++++++++++
 3 files changed, 356 insertions(+), 0 deletions(-)
 create mode 100644 hw/host-pci-device.c
 create mode 100644 hw/host-pci-device.h
Makefile.target +3 -0
index eb25941..883f0d1 100644
@@ -226,6 +226,9 @@ obj-$(CONFIG_NO_XEN) += xen-stub.o
 
 obj-i386-$(CONFIG_XEN) += xen_platform.o
 
+# Xen PCI Passthrough
+obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o
+
 # Inter-VM PCI shared memory
 CONFIG_IVSHMEM =
 ifeq ($(CONFIG_KVM), y)
hw/host-pci-device.c new file +278 -0
new file mode 100644
index 0000000..3dacb30
@@ -0,0 +1,278 @@
+/*
+ * Copyright (C) 2011       Citrix Ltd.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "host-pci-device.h"
+
+#define PCI_MAX_EXT_CAP \
+    ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
+
+enum error_code {
+    ERROR_SYNTAX = 1,
+};
+
+static int path_to(const HostPCIDevice *d,
+                   const char *name, char *buf, ssize_t size)
+{
+    return snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%x/%s",
+                    d->domain, d->bus, d->dev, d->func, name);
+}
+
+static int get_resource(HostPCIDevice *d)
+{
+    int i, rc = 0;
+    FILE *f;
+    char path[PATH_MAX];
+    unsigned long long start, end, flags, size;
+
+    path_to(d, "resource", path, sizeof (path));
+    f = fopen(path, "r");
+    if (!f) {
+        fprintf(stderr, "Error: Can't open %s: %s\n", path, strerror(errno));
+        return -errno;
+    }
+
+    for (i = 0; i < PCI_NUM_REGIONS; i++) {
+        if (fscanf(f, "%llx %llx %llx", &start, &end, &flags) != 3) {
+            fprintf(stderr, "Error: Syntax error in %s\n", path);
+            rc = ERROR_SYNTAX;
+            break;
+        }
+        if (start) {
+            size = end - start + 1;
+        } else {
+            size = 0;
+        }
+
+        if (i < PCI_ROM_SLOT) {
+            d->io_regions[i].base_addr = start;
+            d->io_regions[i].size = size;
+            d->io_regions[i].flags = flags;
+        } else {
+            d->rom.base_addr = start;
+            d->rom.size = size;
+            d->rom.flags = flags;
+        }
+    }
+
+    fclose(f);
+    return rc;
+}
+
+static int get_hex_value(HostPCIDevice *d, const char *name,
+                         unsigned long *pvalue)
+{
+    char path[PATH_MAX];
+    FILE *f;
+    unsigned long value;
+
+    path_to(d, name, path, sizeof (path));
+    f = fopen(path, "r");
+    if (!f) {
+        fprintf(stderr, "Error: Can't open %s: %s\n", path, strerror(errno));
+        return -errno;
+    }
+    if (fscanf(f, "%lx\n", &value) != 1) {
+        fprintf(stderr, "Error: Syntax error in %s\n", path);
+        fclose(f);
+        return ERROR_SYNTAX;
+    }
+    fclose(f);
+    *pvalue = value;
+    return 0;
+}
+
+static bool pci_dev_is_virtfn(HostPCIDevice *d)
+{
+    char path[PATH_MAX];
+    struct stat buf;
+
+    path_to(d, "physfn", path, sizeof (path));
+    return !stat(path, &buf);
+}
+
+static int host_pci_config_fd(HostPCIDevice *d)
+{
+    char path[PATH_MAX];
+
+    if (d->config_fd < 0) {
+        path_to(d, "config", path, sizeof (path));
+        d->config_fd = open(path, O_RDWR);
+        if (d->config_fd < 0) {
+            fprintf(stderr, "HostPCIDevice: Can not open '%s': %s\n",
+                    path, strerror(errno));
+        }
+    }
+    return d->config_fd;
+}
+static int host_pci_config_read(HostPCIDevice *d, int pos, void *buf, int len)
+{
+    int fd = host_pci_config_fd(d);
+    int res = 0;
+
+again:
+    res = pread(fd, buf, len, pos);
+    if (res != len) {
+        if (res < 0 && (errno == EINTR || errno == EAGAIN)) {
+            goto again;
+        }
+        fprintf(stderr, "%s: read failed: %s (fd: %i)\n",
+                __func__, strerror(errno), fd);
+        return -errno;
+    }
+    return 0;
+}
+static int host_pci_config_write(HostPCIDevice *d,
+                                 int pos, const void *buf, int len)
+{
+    int fd = host_pci_config_fd(d);
+    int res = 0;
+
+again:
+    res = pwrite(fd, buf, len, pos);
+    if (res != len) {
+        if (res < 0 && (errno == EINTR || errno == EAGAIN)) {
+            goto again;
+        }
+        fprintf(stderr, "%s: write failed: %s\n",
+                __func__, strerror(errno));
+        return -errno;
+    }
+    return 0;
+}
+
+int host_pci_get_byte(HostPCIDevice *d, int pos, uint8_t *p)
+{
+    uint8_t buf;
+    int rc = host_pci_config_read(d, pos, &buf, 1);
+    if (rc == 0) {
+        *p = buf;
+    }
+    return rc;
+}
+int host_pci_get_word(HostPCIDevice *d, int pos, uint16_t *p)
+{
+    uint16_t buf;
+    int rc = host_pci_config_read(d, pos, &buf, 2);
+    if (rc == 0) {
+        *p = le16_to_cpu(buf);
+    }
+    return rc;
+}
+int host_pci_get_long(HostPCIDevice *d, int pos, uint32_t *p)
+{
+    uint32_t buf;
+    int rc = host_pci_config_read(d, pos, &buf, 4);
+    if (rc == 0) {
+        *p = le32_to_cpu(buf);
+    }
+    return rc;
+}
+int host_pci_get_block(HostPCIDevice *d, int pos, uint8_t *buf, int len)
+{
+    return host_pci_config_read(d, pos, buf, len);
+}
+
+int host_pci_set_byte(HostPCIDevice *d, int pos, uint8_t data)
+{
+    return host_pci_config_write(d, pos, &data, 1);
+}
+int host_pci_set_word(HostPCIDevice *d, int pos, uint16_t data)
+{
+    data = cpu_to_le16(data);
+    return host_pci_config_write(d, pos, &data, 2);
+}
+int host_pci_set_long(HostPCIDevice *d, int pos, uint32_t data)
+{
+    data = cpu_to_le32(data);
+    return host_pci_config_write(d, pos, &data, 4);
+}
+int host_pci_set_block(HostPCIDevice *d, int pos, uint8_t *buf, int len)
+{
+    return host_pci_config_write(d, pos, buf, len);
+}
+
+uint32_t host_pci_find_ext_cap_offset(HostPCIDevice *d, uint32_t cap)
+{
+    uint32_t header = 0;
+    int max_cap = PCI_MAX_EXT_CAP;
+    int pos = PCI_CONFIG_SPACE_SIZE;
+
+    do {
+        if (host_pci_get_long(d, pos, &header)) {
+            break;
+        }
+        /*
+         * If we have no capabilities, this is indicated by cap ID,
+         * cap version and next pointer all being 0.
+         */
+        if (header == 0) {
+            break;
+        }
+
+        if (PCI_EXT_CAP_ID(header) == cap) {
+            return pos;
+        }
+
+        pos = PCI_EXT_CAP_NEXT(header);
+        if (pos < PCI_CONFIG_SPACE_SIZE) {
+            break;
+        }
+
+        max_cap--;
+    } while (max_cap > 0);
+
+    return 0;
+}
+
+HostPCIDevice *host_pci_device_get(uint8_t bus, uint8_t dev, uint8_t func)
+{
+    HostPCIDevice *d = NULL;
+    unsigned long v = 0;
+
+    d = g_new0(HostPCIDevice, 1);
+
+    d->config_fd = -1;
+    d->domain = 0;
+    d->bus = bus;
+    d->dev = dev;
+    d->func = func;
+
+    if (host_pci_config_fd(d) == -1) {
+        goto error;
+    }
+    if (get_resource(d) != 0) {
+        goto error;
+    }
+
+    if (get_hex_value(d, "vendor", &v)) {
+        goto error;
+    }
+    d->vendor_id = v;
+    if (get_hex_value(d, "device", &v)) {
+        goto error;
+    }
+    d->device_id = v;
+    d->is_virtfn = pci_dev_is_virtfn(d);
+
+    return d;
+error:
+    if (d->config_fd >= 0) {
+        close(d->config_fd);
+    }
+    g_free(d);
+    return NULL;
+}
+
+void host_pci_device_put(HostPCIDevice *d)
+{
+    if (d->config_fd >= 0) {
+        close(d->config_fd);
+    }
+    g_free(d);
+}
hw/host-pci-device.h new file +75 -0
new file mode 100644
index 0000000..c8880eb
@@ -0,0 +1,75 @@
+#ifndef HW_HOST_PCI_DEVICE
+#  define HW_HOST_PCI_DEVICE
+
+#include "pci.h"
+
+/*
+ * from linux/ioport.h
+ * IO resources have these defined flags.
+ */
+#define IORESOURCE_BITS         0x000000ff      /* Bus-specific bits */
+
+#define IORESOURCE_TYPE_BITS    0x00000f00      /* Resource type */
+#define IORESOURCE_IO           0x00000100
+#define IORESOURCE_MEM          0x00000200
+#define IORESOURCE_IRQ          0x00000400
+#define IORESOURCE_DMA          0x00000800
+
+#define IORESOURCE_PREFETCH     0x00001000      /* No side effects */
+#define IORESOURCE_READONLY     0x00002000
+#define IORESOURCE_CACHEABLE    0x00004000
+#define IORESOURCE_RANGELENGTH  0x00008000
+#define IORESOURCE_SHADOWABLE   0x00010000
+
+#define IORESOURCE_SIZEALIGN    0x00020000      /* size indicates alignment */
+#define IORESOURCE_STARTALIGN   0x00040000      /* start field is alignment */
+
+#define IORESOURCE_MEM_64       0x00100000
+
+    /* Userland may not map this resource */
+#define IORESOURCE_EXCLUSIVE    0x08000000
+#define IORESOURCE_DISABLED     0x10000000
+#define IORESOURCE_UNSET        0x20000000
+#define IORESOURCE_AUTO         0x40000000
+    /* Driver has marked this resource busy */
+#define IORESOURCE_BUSY         0x80000000
+
+
+typedef struct HostPCIIORegion {
+    unsigned long flags;
+    pcibus_t base_addr;
+    pcibus_t size;
+} HostPCIIORegion;
+
+typedef struct HostPCIDevice {
+    uint16_t domain;
+    uint8_t bus;
+    uint8_t dev;
+    uint8_t func;
+
+    uint16_t vendor_id;
+    uint16_t device_id;
+
+    HostPCIIORegion io_regions[PCI_NUM_REGIONS - 1];
+    HostPCIIORegion rom;
+
+    bool is_virtfn;
+
+    int config_fd;
+} HostPCIDevice;
+
+HostPCIDevice *host_pci_device_get(uint8_t bus, uint8_t dev, uint8_t func);
+void host_pci_device_put(HostPCIDevice *pci_dev);
+
+int host_pci_get_byte(HostPCIDevice *d, int pos, uint8_t *p);
+int host_pci_get_word(HostPCIDevice *d, int pos, uint16_t *p);
+int host_pci_get_long(HostPCIDevice *d, int pos, uint32_t *p);
+int host_pci_get_block(HostPCIDevice *d, int pos, uint8_t *buf, int len);
+int host_pci_set_byte(HostPCIDevice *d, int pos, uint8_t data);
+int host_pci_set_word(HostPCIDevice *d, int pos, uint16_t data);
+int host_pci_set_long(HostPCIDevice *d, int pos, uint32_t data);
+int host_pci_set_block(HostPCIDevice *d, int pos, uint8_t *buf, int len);
+
+uint32_t host_pci_find_ext_cap_offset(HostPCIDevice *s, uint32_t cap);
+
+#endif /* !HW_HOST_PCI_DEVICE */
Signature · 1 line
Anthony PERARD
#13 lore raw

PATCH RESEND v8 4/8 pci.c: Add pci_check_bar_overlap

In reply to Anthony PERARD

From: Yuji Shimada <[email protected]>

This function helps Xen PCI Passthrough device to check for overlap.

---
 hw/pci.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pci.h |    5 +++++
 2 files changed, 55 insertions(+), 0 deletions(-)
hw/pci.c +50 -0
index 38e1de5..f950b4e 100644
@@ -1992,6 +1992,56 @@ MemoryRegion *pci_address_space_io(PCIDevice *dev)
     return dev->bus->address_space_io;
 }
 
+/* This function checks if an io_region overlap an io_region from another
+ * device.  The io_region to check is provide with (addr, size and type)
+ * A callback can be provide and will be called for every region that is
+ * overlapped.
+ * The return value indicate if the region is overlappsed */
+bool pci_check_bar_overlap(PCIDevice *device,
+                           pcibus_t addr, pcibus_t size, uint8_t type,
+                           void (*c)(void *o, const PCIDevice *d, int index),
+                           void *opaque)
+{
+    PCIBus *bus = device->bus;
+    int i, j;
+    bool rc = false;
+
+    for (i = 0; i < ARRAY_SIZE(bus->devices); i++) {
+        PCIDevice *d = bus->devices[i];
+        if (!d) {
+            continue;
+        }
+
+        if (d->devfn == device->devfn) {
+            continue;
+        }
+
+        /* xxx: This ignores bridges. */
+        for (j = 0; j < PCI_NUM_REGIONS; j++) {
+            PCIIORegion *r = &d->io_regions[j];
+
+            if (!r->size) {
+                continue;
+            }
+            if ((type & PCI_BASE_ADDRESS_SPACE_IO)
+                != (r->type & PCI_BASE_ADDRESS_SPACE_IO)) {
+                continue;
+            }
+
+            if (ranges_overlap(addr, size, r->addr, r->size)) {
+                if (c) {
+                    c(opaque, d, j);
+                    rc = true;
+                } else {
+                    return true;
+                }
+            }
+        }
+    }
+
+    return rc;
+}
+
 static void pci_device_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
hw/pci.h +5 -0
index 4f19fdb..cbd04e1 100644
@@ -628,4 +628,9 @@ extern const VMStateDescription vmstate_pci_device;
     .offset     = vmstate_offset_pointer(_state, _field, PCIDevice), \
 }
 
+bool pci_check_bar_overlap(PCIDevice *dev,
+                           pcibus_t addr, pcibus_t size, uint8_t type,
+                           void (*c)(void *o, const PCIDevice *d, int index),
+                           void *opaque);
+
 #endif
Signature · 1 line
Anthony PERARD
#14 lore raw

Re: [Qemu-devel] [PATCH V8 RESEND 4/8] pci.c: Add pci_check_bar_overlap

Quoted 6 lines · Anthony PERARD

From: Yuji Shimada <[email protected]>

This function helps Xen PCI Passthrough device to check for overlap.

It is probably worth mentioning that the function as it is cannot handle bridges in the commit message too.
Other than that, ack.

Quoted 82 lines
 hw/pci.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pci.h |    5 +++++
 2 files changed, 55 insertions(+), 0 deletions(-)
hw/pci.c +50 -0
index 38e1de5..f950b4e 100644
@@ -1992,6 +1992,56 @@ MemoryRegion *pci_address_space_io(PCIDevice *dev)
     return dev->bus->address_space_io;
 }
 
+/* This function checks if an io_region overlap an io_region from another
+ * device.  The io_region to check is provide with (addr, size and type)
+ * A callback can be provide and will be called for every region that is
+ * overlapped.
+ * The return value indicate if the region is overlappsed */
+bool pci_check_bar_overlap(PCIDevice *device,
+                           pcibus_t addr, pcibus_t size, uint8_t type,
+                           void (*c)(void *o, const PCIDevice *d, int index),
+                           void *opaque)
+{
+    PCIBus *bus = device->bus;
+    int i, j;
+    bool rc = false;
+
+    for (i = 0; i < ARRAY_SIZE(bus->devices); i++) {
+        PCIDevice *d = bus->devices[i];
+        if (!d) {
+            continue;
+        }
+
+        if (d->devfn == device->devfn) {
+            continue;
+        }
+
+        /* xxx: This ignores bridges. */
+        for (j = 0; j < PCI_NUM_REGIONS; j++) {
+            PCIIORegion *r = &d->io_regions[j];
+
+            if (!r->size) {
+                continue;
+            }
+            if ((type & PCI_BASE_ADDRESS_SPACE_IO)
+                != (r->type & PCI_BASE_ADDRESS_SPACE_IO)) {
+                continue;
+            }
+
+            if (ranges_overlap(addr, size, r->addr, r->size)) {
+                if (c) {
+                    c(opaque, d, j);
+                    rc = true;
+                } else {
+                    return true;
+                }
+            }
+        }
+    }
+
+    return rc;
+}
+
 static void pci_device_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
hw/pci.h +5 -0
index 4f19fdb..cbd04e1 100644
@@ -628,4 +628,9 @@ extern const VMStateDescription vmstate_pci_device;
     .offset     = vmstate_offset_pointer(_state, _field, PCIDevice), \
 }
 
+bool pci_check_bar_overlap(PCIDevice *dev,
+                           pcibus_t addr, pcibus_t size, uint8_t type,
+                           void (*c)(void *o, const PCIDevice *d, int index),
+                           void *opaque);
+
 #endif
Signature · 1 line
Anthony PERARD
#15 lore raw

Re: [Qemu-devel] [PATCH V8 RESEND 4/8] pci.c: Add pci_check_bar_overlap

In reply to Anthony PERARD
Quoted 6 lines · Anthony PERARD

From: Yuji Shimada <[email protected]>

This function helps Xen PCI Passthrough device to check for overlap.

It seems that what's called for here really is using the new memory region infrastructure. That handles overlap etc nicely.

That said, I don't mind, but would prefer to keep this mess outside the pci core. See below.

Quoted 10 lines
---
 hw/pci.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/pci.h |    5 +++++
 2 files changed, 55 insertions(+), 0 deletions(-)
hw/pci.c +0 -0
index 38e1de5..f950b4e 100644

hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 1995

Quoted 4 lines
hw/pci.c +1 -0
@@ -1992,6 +1992,56 @@ MemoryRegion *pci_address_space_io(PCIDevice *dev)
     return dev->bus->address_space_io;
 }
 
+/* This function

Comment blocks start with /* */

Quoted 1 line

checks if an io_region overlap an io_region from another

overlaps

Quoted 1 line

+ * device. The io_region to check is provide with (addr, size and type)

provided

Quoted 1 line

+ * A callback can be provide and will be called for every region that is

provided

Quoted 2 lines

+ * overlapped.
+ * The return value indicate if the region is overlappsed */

indicates

hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2003

Quoted 4 lines
hw/pci.c +4 -0
+bool pci_check_bar_overlap(PCIDevice *device,
+                           pcibus_t addr, pcibus_t size, uint8_t type,
+                           void (*c)(void *o, const PCIDevice *d, int index),
+                           void *opaque)

IMO this is inlikely to be needed by anyone except Xen. How about a generic pci_foreach_device and let Xen implement the hacks internally.

Quoted 60 lines
hw/pci.c +41 -0
+{
+    PCIBus *bus = device->bus;
+    int i, j;
+    bool rc = false;
+
+    for (i = 0; i < ARRAY_SIZE(bus->devices); i++) {
+        PCIDevice *d = bus->devices[i];
+        if (!d) {
+            continue;
+        }
+
+        if (d->devfn == device->devfn) {
+            continue;
+        }
+
+        /* xxx: This ignores bridges. */
+        for (j = 0; j < PCI_NUM_REGIONS; j++) {
+            PCIIORegion *r = &d->io_regions[j];
+
+            if (!r->size) {
+                continue;
+            }
+            if ((type & PCI_BASE_ADDRESS_SPACE_IO)
+                != (r->type & PCI_BASE_ADDRESS_SPACE_IO)) {
+                continue;
+            }
+
+            if (ranges_overlap(addr, size, r->addr, r->size)) {
+                if (c) {
+                    c(opaque, d, j);
+                    rc = true;
+                } else {
+                    return true;
+                }
+            }
+        }
+    }
+
+    return rc;
+}
+
 static void pci_device_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *k = DEVICE_CLASS(klass);
hw/pci.h +5 -0
index 4f19fdb..cbd04e1 100644
@@ -628,4 +628,9 @@ extern const VMStateDescription vmstate_pci_device;
     .offset     = vmstate_offset_pointer(_state, _field, PCIDevice), \
 }
 
+bool pci_check_bar_overlap(PCIDevice *dev,
+                           pcibus_t addr, pcibus_t size, uint8_t type,
+                           void (*c)(void *o, const PCIDevice *d, int index),
+                           void *opaque);
+
 #endif
Signature · 1 line
Anthony PERARD
#16 lore raw

Re: [Qemu-devel] [PATCH V8 RESEND 4/8] pci.c: Add pci_check_bar_overlap

Quoted 50 lines · Michael S. Tsirkin
Quoted 6 lines · Anthony PERARD

From: Yuji Shimada<[email protected]>

This function helps Xen PCI Passthrough device to check for overlap.

It seems that what's called for here really is using the new memory region infrastructure. That handles overlap etc nicely.

That said, I don't mind, but would prefer to keep this mess outside the pci core. See below.

Quoted 10 lines
---
  hw/pci.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
  hw/pci.h |    5 +++++
  2 files changed, 55 insertions(+), 0 deletions(-)
hw/pci.c +0 -0
index 38e1de5..f950b4e 100644

hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 1995

Quoted 4 lines
hw/pci.c +1 -0
@@ -1992,6 +1992,56 @@ MemoryRegion *pci_address_space_io(PCIDevice *dev)
      return dev->bus->address_space_io;
  }
 
+/* This function

Comment blocks start with /* */

Quoted 1 line

checks if an io_region overlap an io_region from another

overlaps

Quoted 1 line

+ * device. The io_region to check is provide with (addr, size and type)

provided

Quoted 1 line

+ * A callback can be provide and will be called for every region that is

provided

Quoted 2 lines

+ * overlapped.
+ * The return value indicate if the region is overlappsed */

indicates

Quoted 8 lines

hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2003

Quoted 4 lines
hw/pci.c +4 -0
+bool pci_check_bar_overlap(PCIDevice *device,
+                           pcibus_t addr, pcibus_t size, uint8_t type,
+                           void (*c)(void *o, const PCIDevice *d, int index),
+                           void *opaque)

IMO this is inlikely to be needed by anyone except Xen. How about a generic pci_foreach_device and let Xen implement the hacks internally.

Ok, this should be better, I'll work on that.

Thanks,

Quoted 60 lines
Quoted 60 lines
hw/pci.c +41 -0
+{
+    PCIBus *bus = device->bus;
+    int i, j;
+    bool rc = false;
+
+    for (i = 0; i<  ARRAY_SIZE(bus->devices); i++) {
+        PCIDevice *d = bus->devices[i];
+        if (!d) {
+            continue;
+        }
+
+        if (d->devfn == device->devfn) {
+            continue;
+        }
+
+        /* xxx: This ignores bridges. */
+        for (j = 0; j<  PCI_NUM_REGIONS; j++) {
+            PCIIORegion *r =&d->io_regions[j];
+
+            if (!r->size) {
+                continue;
+            }
+            if ((type&  PCI_BASE_ADDRESS_SPACE_IO)
+                != (r->type&  PCI_BASE_ADDRESS_SPACE_IO)) {
+                continue;
+            }
+
+            if (ranges_overlap(addr, size, r->addr, r->size)) {
+                if (c) {
+                    c(opaque, d, j);
+                    rc = true;
+                } else {
+                    return true;
+                }
+            }
+        }
+    }
+
+    return rc;
+}
+
  static void pci_device_class_init(ObjectClass *klass, void *data)
  {
      DeviceClass *k = DEVICE_CLASS(klass);
hw/pci.h +5 -1
index 4f19fdb..cbd04e1 100644
@@ -628,4 +628,9 @@ extern const VMStateDescription vmstate_pci_device;
      .offset     = vmstate_offset_pointer(_state, _field, PCIDevice), \
  }
 
+bool pci_check_bar_overlap(PCIDevice *dev,
+                           pcibus_t addr, pcibus_t size, uint8_t type,
+                           void (*c)(void *o, const PCIDevice *d, int index),
+                           void *opaque);
+
  #endif
--

Anthony PERARD

Signature · 1 line
Anthony PERARD
#17 lore raw

PATCH RESEND v8 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_pci_passthrough.c             |  732 ++++++++++++++++++++++++++++++++++
 hw/xen_pci_passthrough.h             |  250 ++++++++++++
 hw/xen_pci_passthrough_config_init.c |   11 +
 xen-all.c                            |   12 +
 6 files changed, 1010 insertions(+), 0 deletions(-)
 create mode 100644 hw/xen_pci_passthrough.c
 create mode 100644 hw/xen_pci_passthrough.h
 create mode 100644 hw/xen_pci_passthrough_config_init.c
Makefile.target +2 -0
index 883f0d1..e7c72ca 100644
@@ -228,6 +228,8 @@ obj-i386-$(CONFIG_XEN) += xen_platform.o
 
 # Xen PCI Passthrough
 obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o
+obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough.o
+obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough_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_pci_passthrough.c new file +732 -0

732 lines not on this page · raw message

hw/xen_pci_passthrough.h new file +250 -0

250 lines not on this page · raw message

hw/xen_pci_passthrough_config_init.c new file +11 -0
new file mode 100644
index 0000000..1e9de64
@@ -0,0 +1,11 @@
+#include "xen_pci_passthrough.h"
+
+XenPTRegGroup *pt_find_reg_grp(XenPCIPassthroughState *s, uint32_t address)
+{
+    return NULL;
+}
+
+XenPTReg *pt_find_reg(XenPTRegGroup *reg_grp, uint32_t address)
+{
+    return NULL;
+}
xen-all.c +12 -0
index 493112b..fe3da11 100644
@@ -1045,3 +1045,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
#18 lore raw

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

Quoted 8 lines · Anthony PERARD

From: Allen Kay <[email protected]>

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

Such a large patch is obviously hard to review, but given the similarities with the qemu-xen-traditional code and the changes made on top of it, it is OK for me.

#19 lore raw

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

In reply to Anthony PERARD

hw/xen_pci_passthrough.c line 59

Quoted 7 lines
hw/xen_pci_passthrough.c new file +7 -0
+void 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),

%02x.%d

hw/xen_pci_passthrough.c line 112

Quoted 7 lines

+                PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
+    }
+    vfprintf(stderr, f, ap);
+    va_end(ap);

hw/xen_pci_passthrough.c new file +2 -0
+}
 
+static uint32_t pt_pci_read_config(PCIDevice *d, uint32_t addr, int len)

I wish there was some way to combine this function along with the pt_pci_write_config one. They look so similar.

Besides that it looks ok.

#20 lore raw

PATCH RESEND v8 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_pci_passthrough.c             |   10 +
 hw/xen_pci_passthrough.h             |    2 +
 hw/xen_pci_passthrough_config_init.c | 1384 ++++++++++++++++++++++++++++++++++
 3 files changed, 1396 insertions(+), 0 deletions(-)
hw/xen_pci_passthrough.c +10 -0
index fdf24ff..17fd538 100644
@@ -593,6 +593,13 @@ static int pt_initfn(PCIDevice *d)
     /* Handle real device's MMIO/PIO BARs */
     pt_register_regions(s);
 
+    /* reinitialize each config register to be emulated */
+    if (pt_config_init(s)) {
+        PT_ERR(d, "PCI Config space initialisation failed.\n");
+        host_pci_device_put(s->real_device);
+        return -1;
+    }
+
     /* Bind interrupt */
     if (!s->dev.config[PCI_INTERRUPT_PIN]) {
         PT_LOG(d, "no pin interrupt\n");
@@ -691,6 +698,9 @@ static int pt_unregister_device(PCIDevice *d)
         }
     }
 
+    /* delete all emulated config registers */
+    pt_config_delete(s);
+
     pt_unregister_regions(s);
     memory_listener_unregister(&s->memory_listener);
 
hw/xen_pci_passthrough.h +2 -0
index 2c5e83d..6a4d416 100644
@@ -64,6 +64,8 @@ typedef int (*conf_byte_read)
 #define PT_BAR_ALLF         0xFFFFFFFF  /* BAR ALLF value */
 #define PT_PCI_BAR_UNMAPPED (-1)
 
+#define PCI_CAP_MAX 48
+
 
 typedef enum {
     GRP_TYPE_HARDWIRED = 0,                     /* 0 Hardwired reg group */
hw/xen_pci_passthrough_config_init.c +1384 -0

1,395 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
#22 lore raw

PATCH RESEND v8 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
#23 lore raw

Re: [Qemu-devel] [PATCH V8 RESEND 7/8] Introduce apic-msidef.h

Quoted 3 lines · 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.

Ack

#24 lore raw

PATCH RESEND v8 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_pci_passthrough.c             |   31 ++-
 hw/xen_pci_passthrough.h             |   51 +++
 hw/xen_pci_passthrough_config_init.c |  471 ++++++++++++++++++++++++++
 hw/xen_pci_passthrough_msi.c         |  615 ++++++++++++++++++++++++++++++++++
 5 files changed, 1168 insertions(+), 1 deletions(-)
 create mode 100644 hw/xen_pci_passthrough_msi.c
Makefile.target +1 -0
index e7c72ca..c7418b7 100644
@@ -230,6 +230,7 @@ obj-i386-$(CONFIG_XEN) += xen_platform.o
 obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += host-pci-device.o
 obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough.o
 obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough_config_init.o
+obj-i386-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pci_passthrough_msi.o
 
 # Inter-VM PCI shared memory
 CONFIG_IVSHMEM =
hw/xen_pci_passthrough.c +30 -1
index 17fd538..944407d 100644
@@ -36,6 +36,20 @@
  *
  *     Write '1'
  *       - Set real bit to '1'.
+ *
+ * MSI interrupt:
+ *   Initialize MSI register(pt_msi_setup, 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(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>
@@ -489,7 +503,15 @@ static void pt_region_update(XenPCIPassthroughState *s,
     int op = adding ? DPCI_ADD_MAPPING : DPCI_REMOVE_MAPPING;
 
     bar = 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 = pt_msix_update_remap(s, s->msix->bar_index);
+        }
         return;
     }
 
@@ -684,6 +706,13 @@ static int pt_unregister_device(PCIDevice *d)
         }
     }
 
+    if (s->msi) {
+        pt_msi_disable(s);
+    }
+    if (s->msix) {
+        pt_msix_disable(s);
+    }
+
     if (machine_irq) {
         mapped_machine_irq[machine_irq]--;
 
hw/xen_pci_passthrough.h +51 -0

67 lines not on this page · raw message

hw/xen_pci_passthrough_config_init.c +471 -0

501 lines not on this page · raw message

hw/xen_pci_passthrough_msi.c new file +615 -0

615 lines not on this page · raw message

Signature · 1 line
Anthony PERARD