Xen PCI Passthrough
v7 cover Xen PCI Passthrough
Hi all,
This patch series introduces the PCI passthrough for Xen.
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:
- 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 (6):
pci_ids: Add INTEL_82599_VF id.
pci_regs: Fix value of PCI_EXP_TYPE_RC_EC.
pci_regs: Add PCI_EXP_TYPE_PCIE_BRIDGE
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)
Shan Haitao (1):
xen passthrough: clean up MSI-X table handling
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 | 47 + hw/pci.h | 3 + hw/pci_ids.h | 1 + hw/pci_regs.h | 3 +- hw/xen_common.h | 3 + hw/xen_pci_passthrough.c | 898 ++++++++++++++++ hw/xen_pci_passthrough.h | 308 ++++++ hw/xen_pci_passthrough_config_init.c | 1914 ++++++++++++++++++++++++++++++++++ hw/xen_pci_passthrough_msi.c | 618 +++++++++++ xen-all.c | 12 + 16 files changed, 4221 insertions(+), 11 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
PATCH v7 1/11 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
@@ -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
PATCH v7 2/11 pci_regs: Fix value of PCI_EXP_TYPE_RC_EC.
In reply to Anthony PERARDValue check in PCI Express Base Specification rev 1.1
--- hw/pci_regs.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
hw/pci_regs.h +1 -1
@@ -393,7 +393,7 @@#define PCI_EXP_TYPE_DOWNSTREAM 0x6/* Downstream Port */ #define PCI_EXP_TYPE_PCI_BRIDGE 0x7/* PCI/PCI-X Bridge */ #define PCI_EXP_TYPE_RC_END 0x9/* Root Complex Integrated Endpoint */ -#define PCI_EXP_TYPE_RC_EC 0x10/* Root Complex Event Collector */ +#define PCI_EXP_TYPE_RC_EC 0xa/* Root Complex Event Collector */ #define PCI_EXP_FLAGS_SLOT 0x0100/* Slot implemented */ #define PCI_EXP_FLAGS_IRQ 0x3e00/* Interrupt message number */ #define PCI_EXP_DEVCAP 4/* Device capabilities */
Signature · 1 line
Anthony PERARD
Michael S. Tsirkin
AppliedRe: [Qemu-devel] [PATCH V7 02/11] pci_regs: Fix value of PCI_EXP_TYPE_RC_EC.
Quoted 3 lines · Anthony PERARD
Value check in PCI Express Base Specification rev 1.1
Signed-off-by Anthony PERARD <[email protected]>
Thanks, applied.
Quoted 20 lines
--- hw/pci_regs.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)hw/pci_regs.h +1
-1@@ -393,7 +393,7 @@#define PCI_EXP_TYPE_DOWNSTREAM 0x6/* Downstream Port */ #define PCI_EXP_TYPE_PCI_BRIDGE 0x7/* PCI/PCI-X Bridge */ #define PCI_EXP_TYPE_RC_END 0x9/* Root Complex Integrated Endpoint */ -#define PCI_EXP_TYPE_RC_EC 0x10/* Root Complex Event Collector */ +#define PCI_EXP_TYPE_RC_EC 0xa/* Root Complex Event Collector */ #define PCI_EXP_FLAGS_SLOT 0x0100/* Slot implemented */ #define PCI_EXP_FLAGS_IRQ 0x3e00/* Interrupt message number */ #define PCI_EXP_DEVCAP 4/* Device capabilities */ Signature · 1 line
Anthony PERARD
PATCH v7 3/11 pci_regs: Add PCI_EXP_TYPE_PCIE_BRIDGE
In reply to Anthony PERARD--- hw/pci_regs.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
hw/pci_regs.h +1 -0
@@ -392,6 +392,7 @@#define PCI_EXP_TYPE_UPSTREAM 0x5/* Upstream Port */ #define PCI_EXP_TYPE_DOWNSTREAM 0x6/* Downstream Port */ #define PCI_EXP_TYPE_PCI_BRIDGE 0x7/* PCI/PCI-X Bridge */ +#define PCI_EXP_TYPE_PCIE_BRIDGE 0x8/* PCI/PCI-X to PCIE Bridge */ #define PCI_EXP_TYPE_RC_END 0x9/* Root Complex Integrated Endpoint */ #define PCI_EXP_TYPE_RC_EC 0xa/* Root Complex Event Collector */ #define PCI_EXP_FLAGS_SLOT 0x0100/* Slot implemented */
Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 03/11] pci_regs: Add PCI_EXP_TYPE_PCIE_BRIDGE
Quoted 10 lines · Anthony PERARD
Signed-off-by Anthony PERARD <[email protected]>--- hw/pci_regs.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)hw/pci_regs.h +0
-0
hw/pci_regs.h line 395
Quoted 4 lines
hw/pci_regs.h +1
-0@@ -392,6 +392,7 @@#define PCI_EXP_TYPE_UPSTREAM 0x5/* Upstream Port */ #define PCI_EXP_TYPE_DOWNSTREAM 0x6/* Downstream Port */ #define PCI_EXP_TYPE_PCI_BRIDGE 0x7/* PCI/PCI-X Bridge */ +#define PCI_EXP_TYPE_PCIE_BRIDGE 0x8/* PCI/PCI-X to PCIE Bridge */
I don't know why but linux source lacks this value, and we try to stay in sync with that.
Please push there then we'll add here.
Meanwhile put a define in your code.
Quoted 6 lines
hw/pci_regs.h +0
-0#define PCI_EXP_TYPE_RC_END 0x9/* Root Complex Integrated Endpoint */ #define PCI_EXP_TYPE_RC_EC 0xa/* Root Complex Event Collector */ #define PCI_EXP_FLAGS_SLOT 0x0100/* Slot implemented */ Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 03/11] pci_regs: Add PCI_EXP_TYPE_PCIE_BRIDGE
Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · Anthony PERARD
Signed-off-by Anthony PERARD <[email protected]>---
hw/pci_regs.h | 1 +1 files changed, 1 insertions(+), 0 deletions(-)
Quoted 14 lines
hw/pci_regs.h line 395
Quoted 9 lines
hw/pci_regs.h +1
-0@@ -392,6 +392,7 @@ #define PCI_EXP_TYPE_UPSTREAM 0x5 /* Upstream Port */ #define PCI_EXP_TYPE_DOWNSTREAM 0x6 /* Downstream Port */ #define PCI_EXP_TYPE_PCI_BRIDGE 0x7 /* PCI/PCI-X Bridge */ +#define PCI_EXP_TYPE_PCIE_BRIDGE 0x8 /* PCI/PCI-X to PCIE Bridge */I don't know why but linux source lacks this value, and we try to stay in sync with that.
Please push there then we'll add here.
Meanwhile put a define in your code.
Ok, I'll do that.
Thanks,
Signature · 1 line
Anthony PERARD
Michael S. Tsirkin
AppliedRe: [Qemu-devel] [PATCH V7 03/11] pci_regs: Add PCI_EXP_TYPE_PCIE_BRIDGE
In reply to Anthony PERARDQuoted 1 line · Anthony PERARD
Signed-off-by Anthony PERARD <[email protected]>
Applied, thanks.
Quoted 19 lines
--- hw/pci_regs.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)hw/pci_regs.h +1
-0@@ -392,6 +392,7 @@#define PCI_EXP_TYPE_UPSTREAM 0x5/* Upstream Port */ #define PCI_EXP_TYPE_DOWNSTREAM 0x6/* Downstream Port */ #define PCI_EXP_TYPE_PCI_BRIDGE 0x7/* PCI/PCI-X Bridge */ +#define PCI_EXP_TYPE_PCIE_BRIDGE 0x8/* PCI/PCI-X to PCIE Bridge */ #define PCI_EXP_TYPE_RC_END 0x9/* Root Complex Integrated Endpoint */ #define PCI_EXP_TYPE_RC_EC 0xa/* Root Complex Event Collector */ #define PCI_EXP_FLAGS_SLOT 0x0100/* Slot implemented */ Signature · 1 line
Anthony PERARD
PATCH v7 4/11 configure: Introduce --enable-xen-pci-passthrough.
In reply to Anthony PERARD--- configure | 25 +++++++++++++++++++++++++ 1 files changed, 25 insertions(+), 0 deletions(-)
configure +25 -0
@@ -132,6 +132,7 @@ vnc_png="" vnc_thread="no" xen="" xen_ctrl_version="" +xen_pci_passthrough="" linux_aio="" cap_ng="" attr=""
@@ -657,6 +658,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"
@@ -1005,6 +1010,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"
@@ -1458,6 +1465,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
@@ -3592,6 +3614,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
PATCH v7 5/11 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
@@ -222,6 +222,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
PATCH v7 6/11 pci.c: Add pci_check_bar_overlap
In reply to Anthony PERARDFrom: Yuji Shimada <[email protected]>
This function helps Xen PCI Passthrough device to check for overlap.
--- hw/pci.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ hw/pci.h | 3 +++ 2 files changed, 50 insertions(+), 0 deletions(-)
hw/pci.c +47 -0
@@ -1985,6 +1985,53 @@ MemoryRegion *pci_address_space_io(PCIDevice *dev)return dev->bus->address_space_io; } +int pci_check_bar_overlap (PCIDevice *dev, +pcibus_t addr,pcibus_t size,uint8_t type) +{ +PCIBus *bus = dev->bus; +PCIDevice *devices =NULL ; +PCIIORegion *r; +int i, j; +int rc =0 ; + +/* check Overlapped to Base Address */ +for (i =0 ; i <ARRAY_SIZE (bus->devices); i++) { + devices = bus->devices[i]; +if (!devices) { +continue ; + } + +/* skip itself */ +if (devices->devfn == dev->devfn) { +continue ; + } + +for (j =0 ; j <PCI_NUM_REGIONS ; j++) { + r = &devices->io_regions[j]; + +/* skip different resource type, but don't skip when +* prefetch and non-prefetch memory are compared. +*/ +if (type != r->type) { +if (type &PCI_BASE_ADDRESS_SPACE_IO || + r->type &PCI_BASE_ADDRESS_SPACE_IO ) { +continue ; + } + } + +if ((addr < (r->addr + r->size)) && ((addr + size) > r->addr)) { +printf ("Overlapped to device[%02x:%02x.%x][Region:%d]" +"[Address:%" PRIx64"h][Size:%" PRIx64"h]\n" , +pci_bus_num (bus),PCI_SLOT (devices->devfn), +PCI_FUNC (devices->devfn), j, r->addr, r->size); + rc =1 ; + } + } + } + +return rc; +} +static void pci_device_class_init (ObjectClass *klass,void *data) {DeviceClass *k =DEVICE_CLASS (klass);
hw/pci.h +3 -0
@@ -566,4 +566,7 @@ extern const VMStateDescription vmstate_pci_device; .offset = vmstate_offset_pointer(_state, _field, PCIDevice), \ } +int pci_check_bar_overlap (PCIDevice *dev, +pcibus_t addr,pcibus_t size,uint8_t type); +#endif
Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 06/11] 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.
Signed-off-by Yuji Shimada <[email protected]>Signed-off-by Anthony PERARD <[email protected]>
As far as I can see, this scans the bus a specific device is on, looking for devices who have conflicting BARs. Returns 1 if found, 0 if not.
Not sure what would devices do with this information, really:
patches posted just print out a warning which does not seem too useful.
Just FYI, if you decided to e.g. disable device in
such a case that would be wrong: it is legal to have overlapping BARs as long as there are no accesses.
So a legal thing for a guest to do is:
Assign BAR1 = abcde
Assign BAR2 = abcde <- overlaps temporarily
Assign BAR1 = 12345
And this means that you can't just check your device has no conflicts when your device is touched:
it needs to be checked each time mappings are updated.
Quoted 10 lines
--- hw/pci.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ hw/pci.h | 3 +++ 2 files changed, 50 insertions(+), 0 deletions(-)hw/pci.c +0
-0
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 1989
Quoted 5 lines
hw/pci.c +2
-0@@ -1985,6 +1985,53 @@ MemoryRegion *pci_address_space_io(PCIDevice *dev)return dev->bus->address_space_io; } +int pci_check_bar_overlap (PCIDevice *dev, +pcibus_t addr,pcibus_t size,uint8_t type)
This lacks comments documentng what this does, parameters, return values, etc.
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 1997
Quoted 8 lines
hw/pci.c +8
-0+{ +PCIBus *bus = dev->bus; +PCIDevice *devices =NULL ; +PCIIORegion *r; +int i, j; +int rc =0 ; + +/* check Overlapped to Base Address */
Weird use of upper case. Intentional?
Also - what does this comment mean?
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2004
Quoted 7 lines
hw/pci.c +7
-0+for (i =0 ; i <ARRAY_SIZE (bus->devices); i++) { + devices = bus->devices[i]; +if (!devices) { +continue ; + } + +/* skip itself */
itself?
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2009
Quoted 5 lines
hw/pci.c +5
-0+if (devices->devfn == dev->devfn) { +continue ; + } + +for (j =0 ; j <PCI_NUM_REGIONS ; j++) {
This ignores bridges.
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2017
Quoted 8 lines
hw/pci.c +8
-0+ r = &devices->io_regions[j]; + +/* skip different resource type, but don't skip when +* prefetch and non-prefetch memory are compared. +*/ +if (type != r->type) { +if (type &PCI_BASE_ADDRESS_SPACE_IO || + r->type &PCI_BASE_ADDRESS_SPACE_IO ) {
Do you mean
type & PCI_BASE_ADDRESS_SPACE_IO != r->type &
PCI_BASE_ADDRESS_SPACE_IO
?
This would not need a comment then.
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2022
Quoted 5 lines
hw/pci.c +5
-0+continue ; + } + } + +if ((addr < (r->addr + r->size)) && ((addr + size) > r->addr)) {
Can ranges_overlap be used?
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2026
Quoted 4 lines
hw/pci.c +4
-0+printf ("Overlapped to device[%02x:%02x.%x][Region:%d]" +"[Address:%" PRIx64"h][Size:%" PRIx64"h]\n" , +pci_bus_num (bus),PCI_SLOT (devices->devfn), +PCI_FUNC (devices->devfn), j, r->addr, r->size);
That's not how one should report errors.
Quoted 25 lines
hw/pci.c +8
-0+ rc =1 ; + } + } + } + +return rc; +} +static void pci_device_class_init (ObjectClass *klass,void *data) {DeviceClass *k =DEVICE_CLASS (klass);hw/pci.h +3
-0@@ -566,4 +566,7 @@ extern const VMStateDescription vmstate_pci_device; .offset = vmstate_offset_pointer(_state, _field, PCIDevice), \ } +int pci_check_bar_overlap (PCIDevice *dev, +pcibus_t addr,pcibus_t size,uint8_t type); +#endif Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 06/11] pci.c: Add pci_check_bar_overlap
Quoted 8 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.
Signed-off-by Yuji Shimada <[email protected]>Signed-off-by Anthony PERARD <[email protected]>
Quoted 7 lines
As far as I can see, this scans the bus a specific device is on, looking for devices who have conflicting BARs. Returns 1 if found, 0 if not.
Not sure what would devices do with this information, really:
patches posted just print out a warning which does not seem too useful.
This function is just here to print a warning in case an overlap happen, but we would like too keep this warning.
Quoted 26 lines
Just FYI, if you decided to e.g. disable device in
such a case that would be wrong: it is legal to have overlapping BARs as long as there are no accesses.
So a legal thing for a guest to do is:Assign BAR1 = abcde
Assign BAR2 = abcde <- overlaps temporarily
Assign BAR1 = 12345And this means that you can't just check your device has no conflicts when your device is touched:
it needs to be checked each time mappings are updated.Quoted 13 lines
---
hw/pci.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++hw/pci.h| 3 +++2 files changed, 50 insertions(+), 0 deletions(-)hw/pci.c +0
-0@@ -1985,6 +1985,53 @@ MemoryRegion *pci_address_space_io(PCIDevice *dev) return dev->bus->address_space_io; }
Quoted 5 lines
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 1989
Quoted 2 lines
hw/pci.c +2
-0+int pci_check_bar_overlap (PCIDevice *dev, + pcibus_t addr, pcibus_t size, uint8_t type)This lacks comments documentng what this does, parameters, return values, etc.
I'll fix that.
Quoted 11 lines
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 1997
Quoted 8 lines
hw/pci.c +8
-0+{ + PCIBus *bus = dev->bus; + PCIDevice *devices = NULL; + PCIIORegion *r; + int i, j; + int rc = 0; + + /* check Overlapped to Base Address */Weird use of upper case. Intentional?
Also - what does this comment mean?
Don't know, I will remove the comment, it is useless.
Quoted 9 lines
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2004
Quoted 7 lines
hw/pci.c +7
-0+ for (i = 0; i < ARRAY_SIZE(bus->devices); i++) { + devices = bus->devices[i]; + if (!devices) { + continue; + } + + /* skip itself */itself?
Same here, the comment is probably useless, the next line should be easy to understand.
Quoted 7 lines
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2009
Quoted 5 lines
hw/pci.c +5
-0+ if (devices->devfn == dev->devfn) { + continue; + } + + for (j = 0; j < PCI_NUM_REGIONS; j++) {This ignores bridges.
I'm not familiar with brigdes. I'll try to take a look.
Quoted 9 lines
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2017
Quoted 8 lines
hw/pci.c +8
-0+ r = &devices->io_regions[j]; + + /* skip different resource type, but don't skip when + * prefetch and non-prefetch memory are compared. + */ + if (type != r->type) { + if (type & PCI_BASE_ADDRESS_SPACE_IO || + r->type & PCI_BASE_ADDRESS_SPACE_IO) {
Quoted 6 lines
Do you mean
type &PCI_BASE_ADDRESS_SPACE_IO!=r->type&PCI_BASE_ADDRESS_SPACE_IO
?This would not need a comment then.
Yes, I'll replace that.
Quoted 8 lines
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2022
Quoted 6 lines
hw/pci.c +5
-0+ continue; + } + } + + if ((addr < (r->addr + r->size)) && ((addr + size) > r->addr)) {Can
ranges_overlapbe used?
Yes.
Quoted 7 lines
hw/pci.c MemoryRegion *pci_address_space_io(PCIDevice *dev) · line 2026
Quoted 4 lines
hw/pci.c +4
-0+ printf("Overlapped to device[%02x:%02x.%x][Region:%d]" + "[Address:%"PRIx64"h][Size:%"PRIx64"h]\n", + pci_bus_num(bus), PCI_SLOT(devices->devfn), + PCI_FUNC(devices->devfn), j, r->addr, r->size);That's not how one should report errors.
A callback would be better?
Thanks,
Signature · 1 line
Anthony PERARD
PATCH v7 7/11 Introduce Xen PCI Passthrough, qdevice (1/3)
In reply to Anthony PERARDFrom: 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 | 814 ++++++++++++++++++++++++++++++++++ hw/xen_pci_passthrough.h | 251 +++++++++++ hw/xen_pci_passthrough_config_init.c | 11 + xen-all.c | 12 + 6 files changed, 1093 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
@@ -224,6 +224,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
@@ -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 +814 -0
814 lines not on this page · raw message
hw/xen_pci_passthrough.h new file +251 -0
251 lines not on this page · raw message
hw/xen_pci_passthrough_config_init.c new file +11 -0
@@ -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
@@ -1013,3 +1013,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
PATCH v7 8/11 Introduce Xen PCI Passthrough, PCI config space helpers (2/3)
In reply to Anthony PERARDFrom: 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 | 1431 ++++++++++++++++++++++++++++++++++ 3 files changed, 1443 insertions(+), 0 deletions(-)
hw/xen_pci_passthrough.c +10 -0
@@ -676,6 +676,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" );
@@ -773,6 +780,9 @@ static int pt_unregister_device(PCIDevice *d) } } +/* delete all emulated config registers */ +pt_config_delete (s); +/* unregister real device's MMIO/PIO BARs */ pt_unregister_regions (s);
hw/xen_pci_passthrough.h +2 -0
@@ -61,6 +61,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 +1431 -0
1,442 lines not on this page · raw message
Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 08/11] Introduce Xen PCI Passthrough, PCI config space helpers (2/3)
Quoted 429 lines · Anthony PERARD
From: Allen Kay <[email protected]>
A more complete history can be found here:
git://xenbits.xensource.com/qemu-xen-unstable.gitSigned-off-by Allen Kay <[email protected]>Signed-off-by Guy Zana <[email protected]>Signed-off-by Anthony PERARD <[email protected]>--- hw/xen_pci_passthrough.c | 10 + hw/xen_pci_passthrough.h | 2 + hw/xen_pci_passthrough_config_init.c | 1431 ++++++++++++++++++++++++++++++++++ 3 files changed, 1443 insertions(+), 0 deletions(-)hw/xen_pci_passthrough.c +10
-0@@ -676,6 +676,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" );@@ -773,6 +780,9 @@ static int pt_unregister_device(PCIDevice *d) } } +/* delete all emulated config registers */ +pt_config_delete (s); +/* unregister real device's MMIO/PIO BARs */ pt_unregister_regions (s);hw/xen_pci_passthrough.h +2
-0@@ -61,6 +61,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 +358
-0@@ -1,11 +1,1442 @@ +/* +* Copyright (c) 2007, Neocleus Corporation. +* Copyright (c) 2007, Intel Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* Alex Novik <[email protected]> +* Allen Kay <[email protected]> +* Guy Zana <[email protected]> +* +* This file implements direct PCI assignment to a HVM guest +*/ + +#include "qemu-timer.h" +#include "xen_backend.h" #include "xen_pci_passthrough.h" +#define PT_MERGE_VALUE (value, data, val_mask) \ + (((value) & (val_mask)) | ((data) & ~(val_mask))) + +#define PT_INVALID_REG 0xFFFFFFFF/* invalid register value */ + +/* prototype */ + +static int pt_ptr_reg_init (XenPCIPassthroughState *s,XenPTRegInfo *reg, +uint32_t real_offset,uint32_t *data); + + +/* helper */ + +/* A return value of 1 means the capability should NOT be exposed to guest. */ +static int pt_hide_dev_cap (const HostPCIDevice *d,uint8_t grp_id) +{ +switch (grp_id) { +case PCI_CAP_ID_EXP : +/* The PCI Express Capability Structure of the VF of Intel 82599 10GbE +* Controller looks trivial, e.g., the PCI Express Capabilities +* Register is 0. We should not try to expose it to guest. +* +* The datasheet is available at +* http://download.intel.com/design/network/datashts/82599_datasheet.pdf +* +* See 'Table 9.7. VF PCIe Configuration Space' of the datasheet, the +* PCI Express Capability Structure of the VF of Intel 82599 10GbE +* Controller looks trivial, e.g., the PCI Express Capabilities +* Register is 0, so the Capability Version is 0 and +* pt_pcie_size_init() would fail. +*/ +if (d->vendor_id ==PCI_VENDOR_ID_INTEL && + d->device_id ==PCI_DEVICE_ID_INTEL_82599_VF ) { +return 1 ; + } +break ; + } +return 0 ; +} + +/* find emulate register group entry */ XenPTRegGroup *pt_find_reg_grp (XenPCIPassthroughState *s,uint32_t address) { +XenPTRegGroup *entry =NULL ; + +/* find register group entry */ + QLIST_FOREACH(entry, &s->reg_grp_tbl, entries) { +/* check address */ +if ((entry->base_offset <= address) + && ((entry->base_offset + entry->size) > address)) { +return entry; + } + } + +/* group entry not found */ return NULL ; } +/* find emulate register entry */ XenPTReg *pt_find_reg (XenPTRegGroup *reg_grp,uint32_t address) { +XenPTReg *reg_entry =NULL ; +XenPTRegInfo *reg =NULL ; +uint32_t real_offset =0 ; + +/* find register entry */ + QLIST_FOREACH(reg_entry, ®_grp->reg_tbl_list, entries) { + reg = reg_entry->reg; + real_offset = reg_grp->base_offset + reg->offset; +/* check address */ +if ((real_offset <= address) + && ((real_offset + reg->size) > address)) { +return reg_entry; + } + } +return NULL ; } + +/* parse BAR */ +static PTBarFlag pt_bar_reg_parse (XenPCIPassthroughState *s,XenPTRegInfo *reg) +{ +PCIDevice *d = &s->dev; +XenPTRegion *region =NULL ; +PCIIORegion *r; +int index =0 ; + +/* check 64bit BAR */ + index =pt_bar_offset_to_index (reg->offset); +if ((0 < index) && (index <PCI_ROM_SLOT )) { +int flags = s->real_device->io_regions[index -1 ].flags; + +if ((flags &IORESOURCE_MEM ) && (flags &IORESOURCE_MEM_64 )) { + region = &s->bases[index -1 ]; +if (region->bar_flag !=PT_BAR_FLAG_UPPER ) { +return PT_BAR_FLAG_UPPER ; + } + } + } + +/* check unused BAR */ + r = &d->io_regions[index]; +if (r->size ==0 ) { +return PT_BAR_FLAG_UNUSED ; + } + +/* for ExpROM BAR */ +if (index ==PCI_ROM_SLOT ) { +return PT_BAR_FLAG_MEM ; + } + +/* check BAR I/O indicator */ +if (s->real_device->io_regions[index].flags &IORESOURCE_IO ) { +return PT_BAR_FLAG_IO ; + }else { +return PT_BAR_FLAG_MEM ; + } +} + + +/**************** +* general register functions +*/ + +/* register initialization function */ + +static int pt_common_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ + *data = reg->init_val; +return 0 ; +} + +/* Read register functions */ + +static int pt_byte_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint8_t *value,uint8_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint8_t valid_emu_mask =0 ; + +/* emulate byte register */ + valid_emu_mask = reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +static int pt_word_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t valid_emu_mask =0 ; + +/* emulate word register */ + valid_emu_mask = reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +static int pt_long_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint32_t *value,uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint32_t valid_emu_mask =0 ; + +/* emulate long register */ + valid_emu_mask = reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} + +/* Write register functions */ + +static int pt_byte_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint8_t *value,uint8_t dev_value, +uint8_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint8_t writable_mask =0 ; +uint8_t throughable_mask =0 ; + +/* modify emulate register */ + writable_mask = reg->emu_mask & ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +return 0 ; +} +static int pt_word_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t dev_value, +uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t writable_mask =0 ; +uint16_t throughable_mask =0 ; + +/* modify emulate register */ + writable_mask = reg->emu_mask & ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +return 0 ; +} +static int pt_long_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint32_t *value,uint32_t dev_value, +uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint32_t writable_mask =0 ; +uint32_t throughable_mask =0 ; + +/* modify emulate register */ + writable_mask = reg->emu_mask & ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +return 0 ; +} + + +/* XenPTRegInfo declaration +* - only for emulated register (either a part or whole bit). +* - for passthrough register that need special behavior (like interacting with +* other component), set emu_mask to all 0 and specify r/w func properly. +* - do NOT use ALL F for init_val, otherwise the tbl will not be registered. +*/ + +/******************** +* Header Type0 +*/ + +static int pt_vendor_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ + *data = s->real_device->vendor_id; +return 0 ; +} +static int pt_device_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ + *data = s->real_device->device_id; +return 0 ; +} +static int pt_status_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +XenPTRegGroup *reg_grp_entry =NULL ; +XenPTReg *reg_entry =NULL ; +uint32_t reg_field =0 ; + +/* find Header register group */ + reg_grp_entry =pt_find_reg_grp (s,PCI_CAPABILITY_LIST ); +if (reg_grp_entry) { +/* find Capabilities Pointer register */ + reg_entry =pt_find_reg (reg_grp_entry,PCI_CAPABILITY_LIST ); +if (reg_entry) { +/* check Capabilities Pointer register */ +if (reg_entry->data) { + reg_field |=PCI_STATUS_CAP_LIST ; + }else { + reg_field &= ~PCI_STATUS_CAP_LIST ; + } + }else { +xen_shutdown_fatal_error ("Internal error: Couldn't find XenPTReg*" +" for Capabilities Pointer register." +" (%s)\n" , __func__); +return -1 ; + } + }else { +xen_shutdown_fatal_error ("Internal error: Couldn't find XenPTRegGroup" +" for Header. (%s)\n" , __func__); +return -1 ; + } + + *data = reg_field; +return 0 ; +} +static int pt_header_type_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +/* read PCI_HEADER_TYPE */ + *data = reg->init_val |0x80 ; +return 0 ; +} + +/* initialize Interrupt Pin register */ +static int pt_irqpin_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ + *data =pci_read_intx (s); +return 0 ; +} + +/* Command register */ +static int pt_cmd_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t valid_emu_mask =0 ; +uint16_t emu_mask = reg->emu_mask; + +if (s->is_virtfn) { + emu_mask |=PCI_COMMAND_MEMORY ; + } + +/* emulate word register */ + valid_emu_mask = emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +static int pt_cmd_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t dev_value, +uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t writable_mask =0 ; +uint16_t throughable_mask =0 ; +uint16_t wr_value = *value; +uint16_t emu_mask = reg->emu_mask; + +if (s->is_virtfn) { + emu_mask |=PCI_COMMAND_MEMORY ; + } + +/* modify emulate register */ + writable_mask = ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~emu_mask & valid_mask; + +if (*value &PCI_COMMAND_INTX_DISABLE ) { + throughable_mask |=PCI_COMMAND_INTX_DISABLE ; + }else {
hw/xen_pci_passthrough_config_init.c line 379
Quoted 10 lines
hw/xen_pci_passthrough_config_init.c +10
-0+if (s->machine_irq) { + throughable_mask |=PCI_COMMAND_INTX_DISABLE ; + } + } + + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +pt_bar_mapping (s, wr_value &PCI_COMMAND_IO , + wr_value &PCI_COMMAND_MEMORY ); +
IMO this is a wrong way to do this. It completely ignores things like bridge filtering memory accesses.
The right time to update your mappings is I think
when pci_update_mappings is called.
The memory API which that should have enough hooks for you I think, at least it seems sufficient for device assignment on kvm. I don't know much about how well it works for Xen, worst-case, if absolutely necessary, we could
re-add the ->map call for devices.
Quoted 1,066 lines
hw/xen_pci_passthrough_config_init.c +1063
-0+return 0 ; +} + +/* BAR */ +#define PT_BAR_MEM_RO_MASK 0x0000000F/* BAR ReadOnly mask(Memory) */ +#define PT_BAR_MEM_EMU_MASK 0xFFFFFFF0/* BAR emul mask(Memory) */ +#define PT_BAR_IO_RO_MASK 0x00000003/* BAR ReadOnly mask(I/O) */ +#define PT_BAR_IO_EMU_MASK 0xFFFFFFFC/* BAR emul mask(I/O) */ + +static inline uint32_t base_address_with_flags (HostPCIIORegion *hr) +{ +if ((hr->flags &PCI_BASE_ADDRESS_SPACE ) ==PCI_BASE_ADDRESS_SPACE_IO ) { +return hr->base_addr | (hr->flags & ~PCI_BASE_ADDRESS_IO_MASK ); + }else { +return hr->base_addr | (hr->flags & ~PCI_BASE_ADDRESS_MEM_MASK ); + } +} + +static int pt_bar_reg_init (XenPCIPassthroughState *s,XenPTRegInfo *reg, +uint32_t real_offset,uint32_t *data) +{ +uint32_t reg_field =0 ; +int index; + + index =pt_bar_offset_to_index (reg->offset); +if (index <0 || index >=PCI_NUM_REGIONS ) { +PT_ERR (&s->dev,"Internal error: Invalid BAR index [%d].\n" , index); +return -1 ; + } + + s->bases[index].e_physbase =PT_PCI_BAR_UNMAPPED ; + +/* set BAR flag */ + s->bases[index].bar_flag =pt_bar_reg_parse (s, reg); +if (s->bases[index].bar_flag ==PT_BAR_FLAG_UNUSED ) { + reg_field =PT_INVALID_REG ; + } + + *data = reg_field; +return 0 ; +} +static int pt_bar_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint32_t *value,uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint32_t valid_emu_mask =0 ; +uint32_t bar_emu_mask =0 ; +int index; + +/* get BAR index */ + index =pt_bar_offset_to_index (reg->offset); +if (index <0 || index >=PCI_NUM_REGIONS ) { +PT_ERR (&s->dev,"Internal error: Invalid BAR index [%d].\n" , index); +return -1 ; + } + +/* use fixed-up value from kernel sysfs */ + *value =base_address_with_flags (&s->real_device->io_regions[index]); + +/* set emulate mask depend on BAR flag */ +switch (s->bases[index].bar_flag) { +case PT_BAR_FLAG_MEM : + bar_emu_mask =PT_BAR_MEM_EMU_MASK ; +break ; +case PT_BAR_FLAG_IO : + bar_emu_mask =PT_BAR_IO_EMU_MASK ; +break ; +case PT_BAR_FLAG_UPPER : + bar_emu_mask =PT_BAR_ALLF ; +break ; +default : +break ; + } + +/* emulate BAR */ + valid_emu_mask = bar_emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +static int pt_bar_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint32_t *value,uint32_t dev_value, +uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +XenPTRegGroup *reg_grp_entry =NULL ; +XenPTReg *reg_entry =NULL ; +XenPTRegion *base =NULL ; +PCIDevice *d = &s->dev; +const PCIIORegion *r; +uint32_t writable_mask =0 ; +uint32_t throughable_mask =0 ; +uint32_t bar_emu_mask =0 ; +uint32_t bar_ro_mask =0 ; +uint32_t r_size =0 ; +int index =0 ; + + index =pt_bar_offset_to_index (reg->offset); +if (index <0 || index >=PCI_NUM_REGIONS ) { +PT_ERR (d,"Internal error: Invalid BAR index [%d].\n" , index); +return -1 ; + } + + r = &d->io_regions[index]; + base = &s->bases[index]; + r_size =pt_get_emul_size (base->bar_flag, r->size); + +/* set emulate mask and read-only mask values depend on the BAR flag */ +switch (s->bases[index].bar_flag) { +case PT_BAR_FLAG_MEM : + bar_emu_mask =PT_BAR_MEM_EMU_MASK ; + bar_ro_mask =PT_BAR_MEM_RO_MASK | (r_size -1 ); +break ; +case PT_BAR_FLAG_IO : + bar_emu_mask =PT_BAR_IO_EMU_MASK ; + bar_ro_mask =PT_BAR_IO_RO_MASK | (r_size -1 ); +break ; +case PT_BAR_FLAG_UPPER : + bar_emu_mask =PT_BAR_ALLF ; + bar_ro_mask =0 ;/* all upper 32bit are R/W */ +break ; +default : +break ; + } + +/* modify emulate register */ + writable_mask = bar_emu_mask & ~bar_ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* check whether we need to update the virtual region address or not */ +switch (s->bases[index].bar_flag) { +case PT_BAR_FLAG_MEM : +/* nothing to do */ +break ; +case PT_BAR_FLAG_IO : +/* nothing to do */ +break ; +case PT_BAR_FLAG_UPPER : +if (cfg_entry->data) { +if (cfg_entry->data != (PT_BAR_ALLF & ~bar_ro_mask)) { +PT_WARN (d,"Guest attempt to set high MMIO Base Address. " +"Ignore mapping. " +"(offset: 0x%02x, high address: 0x%08x)\n" , + reg->offset, cfg_entry->data); + } + } +break ; +default : +break ; + } + +/* create value for writing to I/O device register */ + throughable_mask = ~bar_emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +/* After BAR reg update, we need to remap BAR */ + reg_grp_entry =pt_find_reg_grp (s,PCI_COMMAND ); +if (reg_grp_entry) { + reg_entry =pt_find_reg (reg_grp_entry,PCI_COMMAND ); +if (reg_entry) { +pt_bar_mapping_one (s, index, reg_entry->data &PCI_COMMAND_IO , + reg_entry->data &PCI_COMMAND_MEMORY ); + } + } + +return 0 ; +} + +/* write Exp ROM BAR */ +static int pt_exp_rom_bar_reg_write (XenPCIPassthroughState *s, +XenPTReg *cfg_entry,uint32_t *value, +uint32_t dev_value,uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +XenPTRegGroup *reg_grp_entry =NULL ; +XenPTReg *reg_entry =NULL ; +XenPTRegion *base =NULL ; +PCIDevice *d = (PCIDevice *)&s->dev; +PCIIORegion *r; +uint32_t writable_mask =0 ; +uint32_t throughable_mask =0 ; +pcibus_t r_size =0 ; +uint32_t bar_emu_mask =0 ; +uint32_t bar_ro_mask =0 ; + + r = &d->io_regions[PCI_ROM_SLOT ]; + r_size = r->size; + base = &s->bases[PCI_ROM_SLOT ]; +/* align memory type resource size */ +pt_get_emul_size (base->bar_flag, r_size); + +/* set emulate mask and read-only mask */ + bar_emu_mask = reg->emu_mask; + bar_ro_mask = (reg->ro_mask | (r_size -1 )) & ~PCI_ROM_ADDRESS_ENABLE ; + +/* modify emulate register */ + writable_mask = ~bar_ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* update the corresponding virtual region address */ +/* +* When guest code tries to get block size of mmio, it will write all "1"s +* into pci bar register. In this case, cfg_entry->data == writable_mask. +* Especially for devices with large mmio, the value of writable_mask +* is likely to be a guest physical address that has been mapped to ram +* rather than mmio. Remapping this value to mmio should be prevented. +*/ + +if (cfg_entry->data != writable_mask) { + r->addr = cfg_entry->data; + } + +/* create value for writing to I/O device register */ + throughable_mask = ~bar_emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +/* After BAR reg update, we need to remap BAR*/ + reg_grp_entry =pt_find_reg_grp (s,PCI_COMMAND ); +if (reg_grp_entry) { + reg_entry =pt_find_reg (reg_grp_entry,PCI_COMMAND ); +if (reg_entry) { +pt_bar_mapping_one (s,PCI_ROM_SLOT , + reg_entry->data &PCI_COMMAND_IO , + reg_entry->data &PCI_COMMAND_MEMORY ); + } + } + +return 0 ; +} + +/* Header Type0 reg static infomation table */ +static XenPTRegInfo pt_emu_reg_header0_tbl[] = { +/* Vendor ID reg */ + { + .offset =PCI_VENDOR_ID , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFFFF , + .emu_mask =0xFFFF , + .init = pt_vendor_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Device ID reg */ + { + .offset =PCI_DEVICE_ID , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFFFF , + .emu_mask =0xFFFF , + .init = pt_device_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Command reg */ + { + .offset =PCI_COMMAND , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xF880 , + .emu_mask =0x0740 , + .init = pt_common_reg_init, + .u.w.read = pt_cmd_reg_read, + .u.w.write = pt_cmd_reg_write, + }, +/* Capabilities Pointer reg */ + { + .offset =PCI_CAPABILITY_LIST , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Status reg */ +/* use emulated Cap Ptr value to initialize, +* so need to be declared after Cap Ptr reg +*/ + { + .offset =PCI_STATUS , + .size =2 , + .init_val =0x0000 , + .ro_mask =0x06FF , + .emu_mask =0x0010 , + .init = pt_status_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Cache Line Size reg */ + { + .offset =PCI_CACHE_LINE_SIZE , + .size =1 , + .init_val =0x00 , + .ro_mask =0x00 , + .emu_mask =0xFF , + .init = pt_common_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Latency Timer reg */ + { + .offset =PCI_LATENCY_TIMER , + .size =1 , + .init_val =0x00 , + .ro_mask =0x00 , + .emu_mask =0xFF , + .init = pt_common_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Header Type reg */ + { + .offset =PCI_HEADER_TYPE , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0x00 , + .init = pt_header_type_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Interrupt Line reg */ + { + .offset =PCI_INTERRUPT_LINE , + .size =1 , + .init_val =0x00 , + .ro_mask =0x00 , + .emu_mask =0xFF , + .init = pt_common_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Interrupt Pin reg */ + { + .offset =PCI_INTERRUPT_PIN , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_irqpin_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* BAR 0 reg */ +/* mask of BAR need to be decided later, depends on IO/MEM type */ + { + .offset =PCI_BASE_ADDRESS_0 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 1 reg */ + { + .offset =PCI_BASE_ADDRESS_1 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 2 reg */ + { + .offset =PCI_BASE_ADDRESS_2 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 3 reg */ + { + .offset =PCI_BASE_ADDRESS_3 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 4 reg */ + { + .offset =PCI_BASE_ADDRESS_4 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 5 reg */ + { + .offset =PCI_BASE_ADDRESS_5 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* Expansion ROM BAR reg */ + { + .offset =PCI_ROM_ADDRESS , + .size =4 , + .init_val =0x00000000 , + .ro_mask =0x000007FE , + .emu_mask =0xFFFFF800 , + .init = pt_bar_reg_init, + .u.dw.read = pt_long_reg_read, + .u.dw.write = pt_exp_rom_bar_reg_write, + }, + { + .size =0 , + }, +}; + + +/********************************* +* Vital Product Data Capability +*/ + +/* Vital Product Data Capability Structure reg static infomation table */ +static XenPTRegInfo pt_emu_reg_vpd_tbl[] = { + { + .offset =PCI_CAP_LIST_NEXT , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, + { + .size =0 , + }, +}; + + +/************************************** +* Vendor Specific Capability +*/ + +/* Vendor Specific Capability Structure reg static infomation table */ +static XenPTRegInfo pt_emu_reg_vendor_tbl[] = { + { + .offset =PCI_CAP_LIST_NEXT , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, + { + .size =0 , + }, +}; + + +/***************************** +* PCI Express Capability +*/ + +static inline uint8_t get_capability_version (XenPCIPassthroughState *s, +uint32_t offset) +{ +uint8_t flags =pci_get_byte (s->dev.config + offset +PCI_EXP_FLAGS ); +return flags &PCI_EXP_FLAGS_VERS ; +} + +static inline uint8_t get_device_type (XenPCIPassthroughState *s, +uint32_t offset) +{ +uint8_t flags =pci_get_byte (s->dev.config + offset +PCI_EXP_FLAGS ); +return (flags &PCI_EXP_FLAGS_TYPE ) >>4 ; +} + +/* initialize Link Control register */ +static int pt_linkctrl_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +uint8_t cap_ver =get_capability_version (s, real_offset - reg->offset); +uint8_t dev_type =get_device_type (s, real_offset - reg->offset); + +/* no need to initialize in case of Root Complex Integrated Endpoint +* with cap_ver 1.x +*/ +if ((dev_type ==PCI_EXP_TYPE_RC_END ) && (cap_ver ==1 )) { + *data =PT_INVALID_REG ; + } + + *data = reg->init_val; +return 0 ; +} +/* initialize Device Control 2 register */ +static int pt_devctrl2_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +uint8_t cap_ver =get_capability_version (s, real_offset - reg->offset); + +/* no need to initialize in case of cap_ver 1.x */ +if (cap_ver ==1 ) { + *data =PT_INVALID_REG ; + } + + *data = reg->init_val; +return 0 ; +} +/* initialize Link Control 2 register */ +static int pt_linkctrl2_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +uint8_t cap_ver =get_capability_version (s, real_offset - reg->offset); +uint32_t reg_field =0 ; + +/* no need to initialize in case of cap_ver 1.x */ +if (cap_ver ==1 ) { + reg_field =PT_INVALID_REG ; + }else { +/* set Supported Link Speed */ +uint8_t lnkcap =pci_get_byte (s->dev.config + real_offset - reg->offset + +PCI_EXP_LNKCAP ); + reg_field |=PCI_EXP_LNKCAP_SLS & lnkcap; + } + + *data = reg_field; +return 0 ; +} + +/* PCI Express Capability Structure reg static infomation table */ +static XenPTRegInfo pt_emu_reg_pcie_tbl[] = { +/* Next Pointer reg */ + { + .offset =PCI_CAP_LIST_NEXT , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Device Capabilities reg */ + { + .offset =PCI_EXP_DEVCAP , + .size =4 , + .init_val =0x00000000 , + .ro_mask =0x1FFCFFFF , + .emu_mask =0x10000000 , + .init = pt_common_reg_init, + .u.dw.read = pt_long_reg_read, + .u.dw.write = pt_long_reg_write, + }, +/* Device Control reg */ + { + .offset =PCI_EXP_DEVCTL , + .size =2 , + .init_val =0x2810 , + .ro_mask =0x8400 , + .emu_mask =0xFFFF , + .init = pt_common_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Link Control reg */ + { + .offset =PCI_EXP_LNKCTL , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFC34 , + .emu_mask =0xFFFF , + .init = pt_linkctrl_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Device Control 2 reg */ + { + .offset =0x28 , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFFE0 , + .emu_mask =0xFFFF , + .init = pt_devctrl2_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Link Control 2 reg */ + { + .offset =0x30 , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xE040 , + .emu_mask =0xFFFF , + .init = pt_linkctrl2_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, + { + .size =0 , + }, +}; + + +/********************************* +* Power Management Capability +*/ + +/* read Power Management Control/Status register */ +static int pt_pmcsr_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t valid_emu_mask = reg->emu_mask; + + valid_emu_mask |=PCI_PM_CTRL_STATE_MASK |PCI_PM_CTRL_NO_SOFT_RESET ; + + valid_emu_mask = valid_emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +/* write Power Management Control/Status register */ +static int pt_pmcsr_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t dev_value, +uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t emu_mask = reg->emu_mask; +uint16_t writable_mask =0 ; +uint16_t throughable_mask =0 ; + + emu_mask |=PCI_PM_CTRL_STATE_MASK |PCI_PM_CTRL_NO_SOFT_RESET ; + +/* modify emulate register */ + writable_mask = emu_mask & ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +return 0 ; +} + +/* Power Management Capability reg static infomation table */ +static XenPTRegInfo pt_emu_reg_pm_tbl[] = { +/* Next Pointer reg */ + { + .offset =PCI_CAP_LIST_NEXT , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Power Management Capabilities reg */ + { + .offset =PCI_CAP_FLAGS , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFFFF , + .emu_mask =0xF9C8 , + .init = pt_common_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* PCI Power Management Control/Status reg */ + { + .offset =PCI_PM_CTRL , + .size =2 , + .init_val =0x0008 , + .ro_mask =0xE1FC , + .emu_mask =0x8100 , + .init = pt_common_reg_init, + .u.w.read = pt_pmcsr_reg_read, + .u.w.write = pt_pmcsr_reg_write, + }, + { + .size =0 , + }, +}; + + +/**************************** +* Capabilities +*/ + +/* capability structure register group size functions */ + +static int pt_reg_grp_size_init (XenPCIPassthroughState *s, +const XenPTRegGroupInfo *grp_reg, +uint32_t base_offset,uint8_t *size) +{ + *size = grp_reg->grp_size; +return 0 ; +} +/* get Vendor Specific Capability Structure register group size */ +static int pt_vendor_size_init (XenPCIPassthroughState *s, +const XenPTRegGroupInfo *grp_reg, +uint32_t base_offset,uint8_t *size) +{ + *size =pci_get_byte (s->dev.config + base_offset +0x02 ); +return 0 ; +} +/* get PCI Express Capability Structure register group size */ +static int pt_pcie_size_init (XenPCIPassthroughState *s, +const XenPTRegGroupInfo *grp_reg, +uint32_t base_offset,uint8_t *size) +{ +PCIDevice *d = &s->dev; +uint8_t version =get_capability_version (s, base_offset); +uint8_t type =get_device_type (s, base_offset); +uint8_t pcie_size =0 ; + + +/* calculate size depend on capability version and device/port type */ +/* in case of PCI Express Base Specification Rev 1.x */ +if (version ==1 ) { +/* The PCI Express Capabilities, Device Capabilities, and Device +* Status/Control registers are required for all PCI Express devices. +* The Link Capabilities and Link Status/Control are required for all +* Endpoints that are not Root Complex Integrated Endpoints. Endpoints +* are not required to implement registers other than those listed +* above and terminate the capability structure. +*/ +switch (type) { +case PCI_EXP_TYPE_ENDPOINT : +case PCI_EXP_TYPE_LEG_END : + pcie_size =0x14 ; +break ; +case PCI_EXP_TYPE_RC_END : +/* has no link */ + pcie_size =0x0C ; +break ; +/* only EndPoint passthrough is supported */ +case PCI_EXP_TYPE_ROOT_PORT : +case PCI_EXP_TYPE_UPSTREAM : +case PCI_EXP_TYPE_DOWNSTREAM : +case PCI_EXP_TYPE_PCI_BRIDGE : +case PCI_EXP_TYPE_PCIE_BRIDGE : +case PCI_EXP_TYPE_RC_EC : +default : +PT_ERR (d,"Unsupported device/port type %#x.\n" , type); +return -1 ; + } + } +/* in case of PCI Express Base Specification Rev 2.0 */ +else if (version ==2 ) { +switch (type) { +case PCI_EXP_TYPE_ENDPOINT : +case PCI_EXP_TYPE_LEG_END : +case PCI_EXP_TYPE_RC_END : +/* For Functions that do not implement the registers, +* these spaces must be hardwired to 0b. +*/ + pcie_size =0x3C ; +break ; +/* only EndPoint passthrough is supported */ +case PCI_EXP_TYPE_ROOT_PORT : +case PCI_EXP_TYPE_UPSTREAM : +case PCI_EXP_TYPE_DOWNSTREAM : +case PCI_EXP_TYPE_PCI_BRIDGE : +case PCI_EXP_TYPE_PCIE_BRIDGE : +case PCI_EXP_TYPE_RC_EC : +default : +PT_ERR (d,"Unsupported device/port type %#x.\n" , type); +return -1 ; + } + }else { +PT_ERR (d,"Unsupported capability version %#x.\n" , version); +return -1 ; + } + + *size = pcie_size; +return 0 ; +} + +static const XenPTRegGroupInfo pt_emu_reg_grp_tbl[] = { +/* Header Type0 reg group */ + { + .grp_id =0xFF , + .grp_type =GRP_TYPE_EMU , + .grp_size =0x40 , + .size_init = pt_reg_grp_size_init, + .emu_reg_tbl = pt_emu_reg_header0_tbl, + }, +/* PCI PowerManagement Capability reg group */ + { + .grp_id =PCI_CAP_ID_PM , + .grp_type =GRP_TYPE_EMU , + .grp_size =PCI_PM_SIZEOF , + .size_init = pt_reg_grp_size_init, + .emu_reg_tbl = pt_emu_reg_pm_tbl, + }, +/* AGP Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_AGP , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x30 , + .size_init = pt_reg_grp_size_init, + }, +/* Vital Product Data Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_VPD , + .grp_type =GRP_TYPE_EMU , + .grp_size =0x08 , + .size_init = pt_reg_grp_size_init, + .emu_reg_tbl = pt_emu_reg_vpd_tbl, + }, +/* Slot Identification reg group */ + { + .grp_id =PCI_CAP_ID_SLOTID , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x04 , + .size_init = pt_reg_grp_size_init, + }, +/* PCI-X Capabilities List Item reg group */ + { + .grp_id =PCI_CAP_ID_PCIX , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x18 , + .size_init = pt_reg_grp_size_init, + }, +/* Vendor Specific Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_VNDR , + .grp_type =GRP_TYPE_EMU , + .grp_size =0xFF , + .size_init = pt_vendor_size_init, + .emu_reg_tbl = pt_emu_reg_vendor_tbl, + }, +/* SHPC Capability List Item reg group */ + { + .grp_id =PCI_CAP_ID_SHPC , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x08 , + .size_init = pt_reg_grp_size_init, + }, +/* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */ + { + .grp_id =PCI_CAP_ID_SSVID , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x08 , + .size_init = pt_reg_grp_size_init, + }, +/* AGP 8x Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_AGP3 , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x30 , + .size_init = pt_reg_grp_size_init, + }, +/* PCI Express Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_EXP , + .grp_type =GRP_TYPE_EMU , + .grp_size =0xFF , + .size_init = pt_pcie_size_init, + .emu_reg_tbl = pt_emu_reg_pcie_tbl, + }, + { + .grp_size =0 , + }, +}; + +/* initialize Capabilities Pointer or Next Pointer register */ +static int pt_ptr_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +int i; +uint8_t *config = s->dev.config; +uint32_t reg_field =pci_get_byte (config + real_offset); +uint8_t cap_id =0 ; + +/* find capability offset */ +while (reg_field) { +for (i =0 ; pt_emu_reg_grp_tbl[i].grp_size !=0 ; i++) { +if (pt_hide_dev_cap (s->real_device, + pt_emu_reg_grp_tbl[i].grp_id)) { +continue ; + } + + cap_id =pci_get_byte (config + reg_field +PCI_CAP_LIST_ID ); +if (pt_emu_reg_grp_tbl[i].grp_id == cap_id) { +if (pt_emu_reg_grp_tbl[i].grp_type ==GRP_TYPE_EMU ) { + goto out; + } +/* ignore the 0 hardwired capability, find next one */ +break ; + } + } + +/* next capability */ + reg_field =pci_get_byte (config + reg_field +PCI_CAP_LIST_NEXT ); + } + +out: + *data = reg_field; +return 0 ; +} + + +/************* +* Main +*/ + +static uint8_t find_cap_offset (XenPCIPassthroughState *s,uint8_t cap) +{ +uint8_t id; +unsigned max_cap =PCI_CAP_MAX ; +uint8_t pos =PCI_CAPABILITY_LIST ; +uint8_t status =0 ; + +if (host_pci_get_byte (s->real_device,PCI_STATUS , &status)) { +return 0 ; + } +if ((status &PCI_STATUS_CAP_LIST ) ==0 ) { +return 0 ; + } + +while (max_cap--) { +if (host_pci_get_byte (s->real_device, pos, &pos)) { +break ; + } +if (pos <PCI_CONFIG_HEADER_SIZE ) { +break ; + } + + pos &= ~3 ; +if (host_pci_get_byte (s->real_device, pos +PCI_CAP_LIST_ID , &id)) { +break ; + } + +if (id ==0xff ) { +break ; + } +if (id == cap) { +return pos; + } + + pos +=PCI_CAP_LIST_NEXT ; + } +return 0 ; +} + +static int pt_config_reg_init (XenPCIPassthroughState *s, +XenPTRegGroup *reg_grp,XenPTRegInfo *reg) +{ +XenPTReg *reg_entry; +uint32_t data =0 ; +int rc =0 ; + + reg_entry =g_new0 (XenPTReg,1 ); + reg_entry->reg = reg; + +if (reg->init) { +/* initialize emulate register */ + rc = reg->init (s, reg_entry->reg, + reg_grp->base_offset + reg->offset, &data); +if (rc <0 ) { +free (reg_entry); +return rc; + } +if (data ==PT_INVALID_REG ) { +/* free unused BAR register entry */ +free (reg_entry); +return 0 ; + } +/* set register value */ + reg_entry->data = data; + } +/* list add register entry */ +QLIST_INSERT_HEAD (®_grp->reg_tbl_list, reg_entry, entries); + +return 0 ; +} + +int pt_config_init (XenPCIPassthroughState *s) +{ +int i, rc; + +QLIST_INIT (&s->reg_grp_tbl); + +for (i =0 ; pt_emu_reg_grp_tbl[i].grp_size !=0 ; i++) { +uint32_t reg_grp_offset =0 ; +XenPTRegGroup *reg_grp_entry =NULL ; + +if (pt_emu_reg_grp_tbl[i].grp_id !=0xFF ) { +if (pt_hide_dev_cap (s->real_device, + pt_emu_reg_grp_tbl[i].grp_id)) { +continue ; + } + + reg_grp_offset =find_cap_offset (s, pt_emu_reg_grp_tbl[i].grp_id); + +if (!reg_grp_offset) { +continue ; + } + } + + reg_grp_entry =g_new0 (XenPTRegGroup,1 ); +QLIST_INIT (®_grp_entry->reg_tbl_list); +QLIST_INSERT_HEAD (&s->reg_grp_tbl, reg_grp_entry, entries); + + reg_grp_entry->base_offset = reg_grp_offset; + reg_grp_entry->reg_grp = pt_emu_reg_grp_tbl + i; +if (pt_emu_reg_grp_tbl[i].size_init) { +/* get register group size */ + rc = pt_emu_reg_grp_tbl[i].size_init (s, reg_grp_entry->reg_grp, + reg_grp_offset, + ®_grp_entry->size); +if (rc <0 ) { +pt_config_delete (s); +return rc; + } + } + +if (pt_emu_reg_grp_tbl[i].grp_type ==GRP_TYPE_EMU ) { +if (pt_emu_reg_grp_tbl[i].emu_reg_tbl) { +int j =0 ; +XenPTRegInfo *reg_tbl = pt_emu_reg_grp_tbl[i].emu_reg_tbl; +/* initialize capability register */ +for (j =0 ; reg_tbl->size !=0 ; j++, reg_tbl++) { +/* initialize capability register */ + rc =pt_config_reg_init (s, reg_grp_entry, reg_tbl); +if (rc <0 ) { +pt_config_delete (s); +return rc; + } + } + } + } + } + +return 0 ; +} + +/* delete all emulate register */ +void pt_config_delete (XenPCIPassthroughState *s) +{ +struct XenPTRegGroup *reg_group, *next_grp; +struct XenPTReg *reg, *next_reg; + +/* free all register group entry */ + QLIST_FOREACH_SAFE(reg_group, &s->reg_grp_tbl, entries, next_grp) { +/* free all register entry */ + QLIST_FOREACH_SAFE(reg, ®_group->reg_tbl_list, entries, next_reg) { +QLIST_REMOVE (reg, entries); +g_free (reg); + } + +QLIST_REMOVE (reg_group, entries); +g_free (reg_group); + } +}Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 08/11] Introduce Xen PCI Passthrough, PCI config space helpers (2/3)
In reply to Anthony PERARDQuoted 8 lines · Anthony PERARD
From: Allen Kay <[email protected]>
A more complete history can be found here:
git://xenbits.xensource.com/qemu-xen-unstable.gitSigned-off-by Allen Kay <[email protected]>Signed-off-by Guy Zana <[email protected]>Signed-off-by Anthony PERARD <[email protected]>
A lot of functionality seems to be duplicated with kvm's device assignment code.
Makes sense to try separating generic bits so that code can be shared?
Quoted 1,497 lines
--- hw/xen_pci_passthrough.c | 10 + hw/xen_pci_passthrough.h | 2 + hw/xen_pci_passthrough_config_init.c | 1431 ++++++++++++++++++++++++++++++++++ 3 files changed, 1443 insertions(+), 0 deletions(-)hw/xen_pci_passthrough.c +10
-0@@ -676,6 +676,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" );@@ -773,6 +780,9 @@ static int pt_unregister_device(PCIDevice *d) } } +/* delete all emulated config registers */ +pt_config_delete (s); +/* unregister real device's MMIO/PIO BARs */ pt_unregister_regions (s);hw/xen_pci_passthrough.h +2
-0@@ -61,6 +61,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 +1431
-0@@ -1,11 +1,1442 @@ +/* +* Copyright (c) 2007, Neocleus Corporation. +* Copyright (c) 2007, Intel Corporation. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* Alex Novik <[email protected]> +* Allen Kay <[email protected]> +* Guy Zana <[email protected]> +* +* This file implements direct PCI assignment to a HVM guest +*/ + +#include "qemu-timer.h" +#include "xen_backend.h" #include "xen_pci_passthrough.h" +#define PT_MERGE_VALUE (value, data, val_mask) \ + (((value) & (val_mask)) | ((data) & ~(val_mask))) + +#define PT_INVALID_REG 0xFFFFFFFF/* invalid register value */ + +/* prototype */ + +static int pt_ptr_reg_init (XenPCIPassthroughState *s,XenPTRegInfo *reg, +uint32_t real_offset,uint32_t *data); + + +/* helper */ + +/* A return value of 1 means the capability should NOT be exposed to guest. */ +static int pt_hide_dev_cap (const HostPCIDevice *d,uint8_t grp_id) +{ +switch (grp_id) { +case PCI_CAP_ID_EXP : +/* The PCI Express Capability Structure of the VF of Intel 82599 10GbE +* Controller looks trivial, e.g., the PCI Express Capabilities +* Register is 0. We should not try to expose it to guest. +* +* The datasheet is available at +* http://download.intel.com/design/network/datashts/82599_datasheet.pdf +* +* See 'Table 9.7. VF PCIe Configuration Space' of the datasheet, the +* PCI Express Capability Structure of the VF of Intel 82599 10GbE +* Controller looks trivial, e.g., the PCI Express Capabilities +* Register is 0, so the Capability Version is 0 and +* pt_pcie_size_init() would fail. +*/ +if (d->vendor_id ==PCI_VENDOR_ID_INTEL && + d->device_id ==PCI_DEVICE_ID_INTEL_82599_VF ) { +return 1 ; + } +break ; + } +return 0 ; +} + +/* find emulate register group entry */ XenPTRegGroup *pt_find_reg_grp (XenPCIPassthroughState *s,uint32_t address) { +XenPTRegGroup *entry =NULL ; + +/* find register group entry */ + QLIST_FOREACH(entry, &s->reg_grp_tbl, entries) { +/* check address */ +if ((entry->base_offset <= address) + && ((entry->base_offset + entry->size) > address)) { +return entry; + } + } + +/* group entry not found */ return NULL ; } +/* find emulate register entry */ XenPTReg *pt_find_reg (XenPTRegGroup *reg_grp,uint32_t address) { +XenPTReg *reg_entry =NULL ; +XenPTRegInfo *reg =NULL ; +uint32_t real_offset =0 ; + +/* find register entry */ + QLIST_FOREACH(reg_entry, ®_grp->reg_tbl_list, entries) { + reg = reg_entry->reg; + real_offset = reg_grp->base_offset + reg->offset; +/* check address */ +if ((real_offset <= address) + && ((real_offset + reg->size) > address)) { +return reg_entry; + } + } +return NULL ; } + +/* parse BAR */ +static PTBarFlag pt_bar_reg_parse (XenPCIPassthroughState *s,XenPTRegInfo *reg) +{ +PCIDevice *d = &s->dev; +XenPTRegion *region =NULL ; +PCIIORegion *r; +int index =0 ; + +/* check 64bit BAR */ + index =pt_bar_offset_to_index (reg->offset); +if ((0 < index) && (index <PCI_ROM_SLOT )) { +int flags = s->real_device->io_regions[index -1 ].flags; + +if ((flags &IORESOURCE_MEM ) && (flags &IORESOURCE_MEM_64 )) { + region = &s->bases[index -1 ]; +if (region->bar_flag !=PT_BAR_FLAG_UPPER ) { +return PT_BAR_FLAG_UPPER ; + } + } + } + +/* check unused BAR */ + r = &d->io_regions[index]; +if (r->size ==0 ) { +return PT_BAR_FLAG_UNUSED ; + } + +/* for ExpROM BAR */ +if (index ==PCI_ROM_SLOT ) { +return PT_BAR_FLAG_MEM ; + } + +/* check BAR I/O indicator */ +if (s->real_device->io_regions[index].flags &IORESOURCE_IO ) { +return PT_BAR_FLAG_IO ; + }else { +return PT_BAR_FLAG_MEM ; + } +} + + +/**************** +* general register functions +*/ + +/* register initialization function */ + +static int pt_common_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ + *data = reg->init_val; +return 0 ; +} + +/* Read register functions */ + +static int pt_byte_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint8_t *value,uint8_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint8_t valid_emu_mask =0 ; + +/* emulate byte register */ + valid_emu_mask = reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +static int pt_word_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t valid_emu_mask =0 ; + +/* emulate word register */ + valid_emu_mask = reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +static int pt_long_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint32_t *value,uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint32_t valid_emu_mask =0 ; + +/* emulate long register */ + valid_emu_mask = reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} + +/* Write register functions */ + +static int pt_byte_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint8_t *value,uint8_t dev_value, +uint8_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint8_t writable_mask =0 ; +uint8_t throughable_mask =0 ; + +/* modify emulate register */ + writable_mask = reg->emu_mask & ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +return 0 ; +} +static int pt_word_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t dev_value, +uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t writable_mask =0 ; +uint16_t throughable_mask =0 ; + +/* modify emulate register */ + writable_mask = reg->emu_mask & ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +return 0 ; +} +static int pt_long_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint32_t *value,uint32_t dev_value, +uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint32_t writable_mask =0 ; +uint32_t throughable_mask =0 ; + +/* modify emulate register */ + writable_mask = reg->emu_mask & ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~reg->emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +return 0 ; +} + + +/* XenPTRegInfo declaration +* - only for emulated register (either a part or whole bit). +* - for passthrough register that need special behavior (like interacting with +* other component), set emu_mask to all 0 and specify r/w func properly. +* - do NOT use ALL F for init_val, otherwise the tbl will not be registered. +*/ + +/******************** +* Header Type0 +*/ + +static int pt_vendor_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ + *data = s->real_device->vendor_id; +return 0 ; +} +static int pt_device_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ + *data = s->real_device->device_id; +return 0 ; +} +static int pt_status_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +XenPTRegGroup *reg_grp_entry =NULL ; +XenPTReg *reg_entry =NULL ; +uint32_t reg_field =0 ; + +/* find Header register group */ + reg_grp_entry =pt_find_reg_grp (s,PCI_CAPABILITY_LIST ); +if (reg_grp_entry) { +/* find Capabilities Pointer register */ + reg_entry =pt_find_reg (reg_grp_entry,PCI_CAPABILITY_LIST ); +if (reg_entry) { +/* check Capabilities Pointer register */ +if (reg_entry->data) { + reg_field |=PCI_STATUS_CAP_LIST ; + }else { + reg_field &= ~PCI_STATUS_CAP_LIST ; + } + }else { +xen_shutdown_fatal_error ("Internal error: Couldn't find XenPTReg*" +" for Capabilities Pointer register." +" (%s)\n" , __func__); +return -1 ; + } + }else { +xen_shutdown_fatal_error ("Internal error: Couldn't find XenPTRegGroup" +" for Header. (%s)\n" , __func__); +return -1 ; + } + + *data = reg_field; +return 0 ; +} +static int pt_header_type_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +/* read PCI_HEADER_TYPE */ + *data = reg->init_val |0x80 ; +return 0 ; +} + +/* initialize Interrupt Pin register */ +static int pt_irqpin_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ + *data =pci_read_intx (s); +return 0 ; +} + +/* Command register */ +static int pt_cmd_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t valid_emu_mask =0 ; +uint16_t emu_mask = reg->emu_mask; + +if (s->is_virtfn) { + emu_mask |=PCI_COMMAND_MEMORY ; + } + +/* emulate word register */ + valid_emu_mask = emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +static int pt_cmd_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t dev_value, +uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t writable_mask =0 ; +uint16_t throughable_mask =0 ; +uint16_t wr_value = *value; +uint16_t emu_mask = reg->emu_mask; + +if (s->is_virtfn) { + emu_mask |=PCI_COMMAND_MEMORY ; + } + +/* modify emulate register */ + writable_mask = ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~emu_mask & valid_mask; + +if (*value &PCI_COMMAND_INTX_DISABLE ) { + throughable_mask |=PCI_COMMAND_INTX_DISABLE ; + }else { +if (s->machine_irq) { + throughable_mask |=PCI_COMMAND_INTX_DISABLE ; + } + } + + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +pt_bar_mapping (s, wr_value &PCI_COMMAND_IO , + wr_value &PCI_COMMAND_MEMORY ); + +return 0 ; +} + +/* BAR */ +#define PT_BAR_MEM_RO_MASK 0x0000000F/* BAR ReadOnly mask(Memory) */ +#define PT_BAR_MEM_EMU_MASK 0xFFFFFFF0/* BAR emul mask(Memory) */ +#define PT_BAR_IO_RO_MASK 0x00000003/* BAR ReadOnly mask(I/O) */ +#define PT_BAR_IO_EMU_MASK 0xFFFFFFFC/* BAR emul mask(I/O) */ + +static inline uint32_t base_address_with_flags (HostPCIIORegion *hr) +{ +if ((hr->flags &PCI_BASE_ADDRESS_SPACE ) ==PCI_BASE_ADDRESS_SPACE_IO ) { +return hr->base_addr | (hr->flags & ~PCI_BASE_ADDRESS_IO_MASK ); + }else { +return hr->base_addr | (hr->flags & ~PCI_BASE_ADDRESS_MEM_MASK ); + } +} + +static int pt_bar_reg_init (XenPCIPassthroughState *s,XenPTRegInfo *reg, +uint32_t real_offset,uint32_t *data) +{ +uint32_t reg_field =0 ; +int index; + + index =pt_bar_offset_to_index (reg->offset); +if (index <0 || index >=PCI_NUM_REGIONS ) { +PT_ERR (&s->dev,"Internal error: Invalid BAR index [%d].\n" , index); +return -1 ; + } + + s->bases[index].e_physbase =PT_PCI_BAR_UNMAPPED ; + +/* set BAR flag */ + s->bases[index].bar_flag =pt_bar_reg_parse (s, reg); +if (s->bases[index].bar_flag ==PT_BAR_FLAG_UNUSED ) { + reg_field =PT_INVALID_REG ; + } + + *data = reg_field; +return 0 ; +} +static int pt_bar_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint32_t *value,uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint32_t valid_emu_mask =0 ; +uint32_t bar_emu_mask =0 ; +int index; + +/* get BAR index */ + index =pt_bar_offset_to_index (reg->offset); +if (index <0 || index >=PCI_NUM_REGIONS ) { +PT_ERR (&s->dev,"Internal error: Invalid BAR index [%d].\n" , index); +return -1 ; + } + +/* use fixed-up value from kernel sysfs */ + *value =base_address_with_flags (&s->real_device->io_regions[index]); + +/* set emulate mask depend on BAR flag */ +switch (s->bases[index].bar_flag) { +case PT_BAR_FLAG_MEM : + bar_emu_mask =PT_BAR_MEM_EMU_MASK ; +break ; +case PT_BAR_FLAG_IO : + bar_emu_mask =PT_BAR_IO_EMU_MASK ; +break ; +case PT_BAR_FLAG_UPPER : + bar_emu_mask =PT_BAR_ALLF ; +break ; +default : +break ; + } + +/* emulate BAR */ + valid_emu_mask = bar_emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +static int pt_bar_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint32_t *value,uint32_t dev_value, +uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +XenPTRegGroup *reg_grp_entry =NULL ; +XenPTReg *reg_entry =NULL ; +XenPTRegion *base =NULL ; +PCIDevice *d = &s->dev; +const PCIIORegion *r; +uint32_t writable_mask =0 ; +uint32_t throughable_mask =0 ; +uint32_t bar_emu_mask =0 ; +uint32_t bar_ro_mask =0 ; +uint32_t r_size =0 ; +int index =0 ; + + index =pt_bar_offset_to_index (reg->offset); +if (index <0 || index >=PCI_NUM_REGIONS ) { +PT_ERR (d,"Internal error: Invalid BAR index [%d].\n" , index); +return -1 ; + } + + r = &d->io_regions[index]; + base = &s->bases[index]; + r_size =pt_get_emul_size (base->bar_flag, r->size); + +/* set emulate mask and read-only mask values depend on the BAR flag */ +switch (s->bases[index].bar_flag) { +case PT_BAR_FLAG_MEM : + bar_emu_mask =PT_BAR_MEM_EMU_MASK ; + bar_ro_mask =PT_BAR_MEM_RO_MASK | (r_size -1 ); +break ; +case PT_BAR_FLAG_IO : + bar_emu_mask =PT_BAR_IO_EMU_MASK ; + bar_ro_mask =PT_BAR_IO_RO_MASK | (r_size -1 ); +break ; +case PT_BAR_FLAG_UPPER : + bar_emu_mask =PT_BAR_ALLF ; + bar_ro_mask =0 ;/* all upper 32bit are R/W */ +break ; +default : +break ; + } + +/* modify emulate register */ + writable_mask = bar_emu_mask & ~bar_ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* check whether we need to update the virtual region address or not */ +switch (s->bases[index].bar_flag) { +case PT_BAR_FLAG_MEM : +/* nothing to do */ +break ; +case PT_BAR_FLAG_IO : +/* nothing to do */ +break ; +case PT_BAR_FLAG_UPPER : +if (cfg_entry->data) { +if (cfg_entry->data != (PT_BAR_ALLF & ~bar_ro_mask)) { +PT_WARN (d,"Guest attempt to set high MMIO Base Address. " +"Ignore mapping. " +"(offset: 0x%02x, high address: 0x%08x)\n" , + reg->offset, cfg_entry->data); + } + } +break ; +default : +break ; + } + +/* create value for writing to I/O device register */ + throughable_mask = ~bar_emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +/* After BAR reg update, we need to remap BAR */ + reg_grp_entry =pt_find_reg_grp (s,PCI_COMMAND ); +if (reg_grp_entry) { + reg_entry =pt_find_reg (reg_grp_entry,PCI_COMMAND ); +if (reg_entry) { +pt_bar_mapping_one (s, index, reg_entry->data &PCI_COMMAND_IO , + reg_entry->data &PCI_COMMAND_MEMORY ); + } + } + +return 0 ; +} + +/* write Exp ROM BAR */ +static int pt_exp_rom_bar_reg_write (XenPCIPassthroughState *s, +XenPTReg *cfg_entry,uint32_t *value, +uint32_t dev_value,uint32_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +XenPTRegGroup *reg_grp_entry =NULL ; +XenPTReg *reg_entry =NULL ; +XenPTRegion *base =NULL ; +PCIDevice *d = (PCIDevice *)&s->dev; +PCIIORegion *r; +uint32_t writable_mask =0 ; +uint32_t throughable_mask =0 ; +pcibus_t r_size =0 ; +uint32_t bar_emu_mask =0 ; +uint32_t bar_ro_mask =0 ; + + r = &d->io_regions[PCI_ROM_SLOT ]; + r_size = r->size; + base = &s->bases[PCI_ROM_SLOT ]; +/* align memory type resource size */ +pt_get_emul_size (base->bar_flag, r_size); + +/* set emulate mask and read-only mask */ + bar_emu_mask = reg->emu_mask; + bar_ro_mask = (reg->ro_mask | (r_size -1 )) & ~PCI_ROM_ADDRESS_ENABLE ; + +/* modify emulate register */ + writable_mask = ~bar_ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* update the corresponding virtual region address */ +/* +* When guest code tries to get block size of mmio, it will write all "1"s +* into pci bar register. In this case, cfg_entry->data == writable_mask. +* Especially for devices with large mmio, the value of writable_mask +* is likely to be a guest physical address that has been mapped to ram +* rather than mmio. Remapping this value to mmio should be prevented. +*/ + +if (cfg_entry->data != writable_mask) { + r->addr = cfg_entry->data; + } + +/* create value for writing to I/O device register */ + throughable_mask = ~bar_emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +/* After BAR reg update, we need to remap BAR*/ + reg_grp_entry =pt_find_reg_grp (s,PCI_COMMAND ); +if (reg_grp_entry) { + reg_entry =pt_find_reg (reg_grp_entry,PCI_COMMAND ); +if (reg_entry) { +pt_bar_mapping_one (s,PCI_ROM_SLOT , + reg_entry->data &PCI_COMMAND_IO , + reg_entry->data &PCI_COMMAND_MEMORY ); + } + } + +return 0 ; +} + +/* Header Type0 reg static infomation table */ +static XenPTRegInfo pt_emu_reg_header0_tbl[] = { +/* Vendor ID reg */ + { + .offset =PCI_VENDOR_ID , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFFFF , + .emu_mask =0xFFFF , + .init = pt_vendor_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Device ID reg */ + { + .offset =PCI_DEVICE_ID , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFFFF , + .emu_mask =0xFFFF , + .init = pt_device_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Command reg */ + { + .offset =PCI_COMMAND , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xF880 , + .emu_mask =0x0740 , + .init = pt_common_reg_init, + .u.w.read = pt_cmd_reg_read, + .u.w.write = pt_cmd_reg_write, + }, +/* Capabilities Pointer reg */ + { + .offset =PCI_CAPABILITY_LIST , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Status reg */ +/* use emulated Cap Ptr value to initialize, +* so need to be declared after Cap Ptr reg +*/ + { + .offset =PCI_STATUS , + .size =2 , + .init_val =0x0000 , + .ro_mask =0x06FF , + .emu_mask =0x0010 , + .init = pt_status_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Cache Line Size reg */ + { + .offset =PCI_CACHE_LINE_SIZE , + .size =1 , + .init_val =0x00 , + .ro_mask =0x00 , + .emu_mask =0xFF , + .init = pt_common_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Latency Timer reg */ + { + .offset =PCI_LATENCY_TIMER , + .size =1 , + .init_val =0x00 , + .ro_mask =0x00 , + .emu_mask =0xFF , + .init = pt_common_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Header Type reg */ + { + .offset =PCI_HEADER_TYPE , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0x00 , + .init = pt_header_type_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Interrupt Line reg */ + { + .offset =PCI_INTERRUPT_LINE , + .size =1 , + .init_val =0x00 , + .ro_mask =0x00 , + .emu_mask =0xFF , + .init = pt_common_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Interrupt Pin reg */ + { + .offset =PCI_INTERRUPT_PIN , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_irqpin_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* BAR 0 reg */ +/* mask of BAR need to be decided later, depends on IO/MEM type */ + { + .offset =PCI_BASE_ADDRESS_0 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 1 reg */ + { + .offset =PCI_BASE_ADDRESS_1 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 2 reg */ + { + .offset =PCI_BASE_ADDRESS_2 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 3 reg */ + { + .offset =PCI_BASE_ADDRESS_3 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 4 reg */ + { + .offset =PCI_BASE_ADDRESS_4 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* BAR 5 reg */ + { + .offset =PCI_BASE_ADDRESS_5 , + .size =4 , + .init_val =0x00000000 , + .init = pt_bar_reg_init, + .u.dw.read = pt_bar_reg_read, + .u.dw.write = pt_bar_reg_write, + }, +/* Expansion ROM BAR reg */ + { + .offset =PCI_ROM_ADDRESS , + .size =4 , + .init_val =0x00000000 , + .ro_mask =0x000007FE , + .emu_mask =0xFFFFF800 , + .init = pt_bar_reg_init, + .u.dw.read = pt_long_reg_read, + .u.dw.write = pt_exp_rom_bar_reg_write, + }, + { + .size =0 , + }, +}; + + +/********************************* +* Vital Product Data Capability +*/ + +/* Vital Product Data Capability Structure reg static infomation table */ +static XenPTRegInfo pt_emu_reg_vpd_tbl[] = { + { + .offset =PCI_CAP_LIST_NEXT , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, + { + .size =0 , + }, +}; + + +/************************************** +* Vendor Specific Capability +*/ + +/* Vendor Specific Capability Structure reg static infomation table */ +static XenPTRegInfo pt_emu_reg_vendor_tbl[] = { + { + .offset =PCI_CAP_LIST_NEXT , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, + { + .size =0 , + }, +}; + + +/***************************** +* PCI Express Capability +*/ + +static inline uint8_t get_capability_version (XenPCIPassthroughState *s, +uint32_t offset) +{ +uint8_t flags =pci_get_byte (s->dev.config + offset +PCI_EXP_FLAGS ); +return flags &PCI_EXP_FLAGS_VERS ; +} + +static inline uint8_t get_device_type (XenPCIPassthroughState *s, +uint32_t offset) +{ +uint8_t flags =pci_get_byte (s->dev.config + offset +PCI_EXP_FLAGS ); +return (flags &PCI_EXP_FLAGS_TYPE ) >>4 ; +} + +/* initialize Link Control register */ +static int pt_linkctrl_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +uint8_t cap_ver =get_capability_version (s, real_offset - reg->offset); +uint8_t dev_type =get_device_type (s, real_offset - reg->offset); + +/* no need to initialize in case of Root Complex Integrated Endpoint +* with cap_ver 1.x +*/ +if ((dev_type ==PCI_EXP_TYPE_RC_END ) && (cap_ver ==1 )) { + *data =PT_INVALID_REG ; + } + + *data = reg->init_val; +return 0 ; +} +/* initialize Device Control 2 register */ +static int pt_devctrl2_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +uint8_t cap_ver =get_capability_version (s, real_offset - reg->offset); + +/* no need to initialize in case of cap_ver 1.x */ +if (cap_ver ==1 ) { + *data =PT_INVALID_REG ; + } + + *data = reg->init_val; +return 0 ; +} +/* initialize Link Control 2 register */ +static int pt_linkctrl2_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +uint8_t cap_ver =get_capability_version (s, real_offset - reg->offset); +uint32_t reg_field =0 ; + +/* no need to initialize in case of cap_ver 1.x */ +if (cap_ver ==1 ) { + reg_field =PT_INVALID_REG ; + }else { +/* set Supported Link Speed */ +uint8_t lnkcap =pci_get_byte (s->dev.config + real_offset - reg->offset + +PCI_EXP_LNKCAP ); + reg_field |=PCI_EXP_LNKCAP_SLS & lnkcap; + } + + *data = reg_field; +return 0 ; +} + +/* PCI Express Capability Structure reg static infomation table */ +static XenPTRegInfo pt_emu_reg_pcie_tbl[] = { +/* Next Pointer reg */ + { + .offset =PCI_CAP_LIST_NEXT , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Device Capabilities reg */ + { + .offset =PCI_EXP_DEVCAP , + .size =4 , + .init_val =0x00000000 , + .ro_mask =0x1FFCFFFF , + .emu_mask =0x10000000 , + .init = pt_common_reg_init, + .u.dw.read = pt_long_reg_read, + .u.dw.write = pt_long_reg_write, + }, +/* Device Control reg */ + { + .offset =PCI_EXP_DEVCTL , + .size =2 , + .init_val =0x2810 , + .ro_mask =0x8400 , + .emu_mask =0xFFFF , + .init = pt_common_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Link Control reg */ + { + .offset =PCI_EXP_LNKCTL , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFC34 , + .emu_mask =0xFFFF , + .init = pt_linkctrl_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Device Control 2 reg */ + { + .offset =0x28 , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFFE0 , + .emu_mask =0xFFFF , + .init = pt_devctrl2_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* Link Control 2 reg */ + { + .offset =0x30 , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xE040 , + .emu_mask =0xFFFF , + .init = pt_linkctrl2_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, + { + .size =0 , + }, +}; + + +/********************************* +* Power Management Capability +*/ + +/* read Power Management Control/Status register */ +static int pt_pmcsr_reg_read (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t valid_emu_mask = reg->emu_mask; + + valid_emu_mask |=PCI_PM_CTRL_STATE_MASK |PCI_PM_CTRL_NO_SOFT_RESET ; + + valid_emu_mask = valid_emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, cfg_entry->data, ~valid_emu_mask); + +return 0 ; +} +/* write Power Management Control/Status register */ +static int pt_pmcsr_reg_write (XenPCIPassthroughState *s,XenPTReg *cfg_entry, +uint16_t *value,uint16_t dev_value, +uint16_t valid_mask) +{ +XenPTRegInfo *reg = cfg_entry->reg; +uint16_t emu_mask = reg->emu_mask; +uint16_t writable_mask =0 ; +uint16_t throughable_mask =0 ; + + emu_mask |=PCI_PM_CTRL_STATE_MASK |PCI_PM_CTRL_NO_SOFT_RESET ; + +/* modify emulate register */ + writable_mask = emu_mask & ~reg->ro_mask & valid_mask; + cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); + +/* create value for writing to I/O device register */ + throughable_mask = ~emu_mask & valid_mask; + *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask); + +return 0 ; +} + +/* Power Management Capability reg static infomation table */ +static XenPTRegInfo pt_emu_reg_pm_tbl[] = { +/* Next Pointer reg */ + { + .offset =PCI_CAP_LIST_NEXT , + .size =1 , + .init_val =0x00 , + .ro_mask =0xFF , + .emu_mask =0xFF , + .init = pt_ptr_reg_init, + .u.b.read = pt_byte_reg_read, + .u.b.write = pt_byte_reg_write, + }, +/* Power Management Capabilities reg */ + { + .offset =PCI_CAP_FLAGS , + .size =2 , + .init_val =0x0000 , + .ro_mask =0xFFFF , + .emu_mask =0xF9C8 , + .init = pt_common_reg_init, + .u.w.read = pt_word_reg_read, + .u.w.write = pt_word_reg_write, + }, +/* PCI Power Management Control/Status reg */ + { + .offset =PCI_PM_CTRL , + .size =2 , + .init_val =0x0008 , + .ro_mask =0xE1FC , + .emu_mask =0x8100 , + .init = pt_common_reg_init, + .u.w.read = pt_pmcsr_reg_read, + .u.w.write = pt_pmcsr_reg_write, + }, + { + .size =0 , + }, +}; + + +/**************************** +* Capabilities +*/ + +/* capability structure register group size functions */ + +static int pt_reg_grp_size_init (XenPCIPassthroughState *s, +const XenPTRegGroupInfo *grp_reg, +uint32_t base_offset,uint8_t *size) +{ + *size = grp_reg->grp_size; +return 0 ; +} +/* get Vendor Specific Capability Structure register group size */ +static int pt_vendor_size_init (XenPCIPassthroughState *s, +const XenPTRegGroupInfo *grp_reg, +uint32_t base_offset,uint8_t *size) +{ + *size =pci_get_byte (s->dev.config + base_offset +0x02 ); +return 0 ; +} +/* get PCI Express Capability Structure register group size */ +static int pt_pcie_size_init (XenPCIPassthroughState *s, +const XenPTRegGroupInfo *grp_reg, +uint32_t base_offset,uint8_t *size) +{ +PCIDevice *d = &s->dev; +uint8_t version =get_capability_version (s, base_offset); +uint8_t type =get_device_type (s, base_offset); +uint8_t pcie_size =0 ; + + +/* calculate size depend on capability version and device/port type */ +/* in case of PCI Express Base Specification Rev 1.x */ +if (version ==1 ) { +/* The PCI Express Capabilities, Device Capabilities, and Device +* Status/Control registers are required for all PCI Express devices. +* The Link Capabilities and Link Status/Control are required for all +* Endpoints that are not Root Complex Integrated Endpoints. Endpoints +* are not required to implement registers other than those listed +* above and terminate the capability structure. +*/ +switch (type) { +case PCI_EXP_TYPE_ENDPOINT : +case PCI_EXP_TYPE_LEG_END : + pcie_size =0x14 ; +break ; +case PCI_EXP_TYPE_RC_END : +/* has no link */ + pcie_size =0x0C ; +break ; +/* only EndPoint passthrough is supported */ +case PCI_EXP_TYPE_ROOT_PORT : +case PCI_EXP_TYPE_UPSTREAM : +case PCI_EXP_TYPE_DOWNSTREAM : +case PCI_EXP_TYPE_PCI_BRIDGE : +case PCI_EXP_TYPE_PCIE_BRIDGE : +case PCI_EXP_TYPE_RC_EC : +default : +PT_ERR (d,"Unsupported device/port type %#x.\n" , type); +return -1 ; + } + } +/* in case of PCI Express Base Specification Rev 2.0 */ +else if (version ==2 ) { +switch (type) { +case PCI_EXP_TYPE_ENDPOINT : +case PCI_EXP_TYPE_LEG_END : +case PCI_EXP_TYPE_RC_END : +/* For Functions that do not implement the registers, +* these spaces must be hardwired to 0b. +*/ + pcie_size =0x3C ; +break ; +/* only EndPoint passthrough is supported */ +case PCI_EXP_TYPE_ROOT_PORT : +case PCI_EXP_TYPE_UPSTREAM : +case PCI_EXP_TYPE_DOWNSTREAM : +case PCI_EXP_TYPE_PCI_BRIDGE : +case PCI_EXP_TYPE_PCIE_BRIDGE : +case PCI_EXP_TYPE_RC_EC : +default : +PT_ERR (d,"Unsupported device/port type %#x.\n" , type); +return -1 ; + } + }else { +PT_ERR (d,"Unsupported capability version %#x.\n" , version); +return -1 ; + } + + *size = pcie_size; +return 0 ; +} + +static const XenPTRegGroupInfo pt_emu_reg_grp_tbl[] = { +/* Header Type0 reg group */ + { + .grp_id =0xFF , + .grp_type =GRP_TYPE_EMU , + .grp_size =0x40 , + .size_init = pt_reg_grp_size_init, + .emu_reg_tbl = pt_emu_reg_header0_tbl, + }, +/* PCI PowerManagement Capability reg group */ + { + .grp_id =PCI_CAP_ID_PM , + .grp_type =GRP_TYPE_EMU , + .grp_size =PCI_PM_SIZEOF , + .size_init = pt_reg_grp_size_init, + .emu_reg_tbl = pt_emu_reg_pm_tbl, + }, +/* AGP Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_AGP , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x30 , + .size_init = pt_reg_grp_size_init, + }, +/* Vital Product Data Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_VPD , + .grp_type =GRP_TYPE_EMU , + .grp_size =0x08 , + .size_init = pt_reg_grp_size_init, + .emu_reg_tbl = pt_emu_reg_vpd_tbl, + }, +/* Slot Identification reg group */ + { + .grp_id =PCI_CAP_ID_SLOTID , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x04 , + .size_init = pt_reg_grp_size_init, + }, +/* PCI-X Capabilities List Item reg group */ + { + .grp_id =PCI_CAP_ID_PCIX , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x18 , + .size_init = pt_reg_grp_size_init, + }, +/* Vendor Specific Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_VNDR , + .grp_type =GRP_TYPE_EMU , + .grp_size =0xFF , + .size_init = pt_vendor_size_init, + .emu_reg_tbl = pt_emu_reg_vendor_tbl, + }, +/* SHPC Capability List Item reg group */ + { + .grp_id =PCI_CAP_ID_SHPC , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x08 , + .size_init = pt_reg_grp_size_init, + }, +/* Subsystem ID and Subsystem Vendor ID Capability List Item reg group */ + { + .grp_id =PCI_CAP_ID_SSVID , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x08 , + .size_init = pt_reg_grp_size_init, + }, +/* AGP 8x Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_AGP3 , + .grp_type =GRP_TYPE_HARDWIRED , + .grp_size =0x30 , + .size_init = pt_reg_grp_size_init, + }, +/* PCI Express Capability Structure reg group */ + { + .grp_id =PCI_CAP_ID_EXP , + .grp_type =GRP_TYPE_EMU , + .grp_size =0xFF , + .size_init = pt_pcie_size_init, + .emu_reg_tbl = pt_emu_reg_pcie_tbl, + }, + { + .grp_size =0 , + }, +}; + +/* initialize Capabilities Pointer or Next Pointer register */ +static int pt_ptr_reg_init (XenPCIPassthroughState *s, +XenPTRegInfo *reg,uint32_t real_offset, +uint32_t *data) +{ +int i; +uint8_t *config = s->dev.config; +uint32_t reg_field =pci_get_byte (config + real_offset); +uint8_t cap_id =0 ; + +/* find capability offset */ +while (reg_field) { +for (i =0 ; pt_emu_reg_grp_tbl[i].grp_size !=0 ; i++) { +if (pt_hide_dev_cap (s->real_device, + pt_emu_reg_grp_tbl[i].grp_id)) { +continue ; + } + + cap_id =pci_get_byte (config + reg_field +PCI_CAP_LIST_ID ); +if (pt_emu_reg_grp_tbl[i].grp_id == cap_id) { +if (pt_emu_reg_grp_tbl[i].grp_type ==GRP_TYPE_EMU ) { + goto out; + } +/* ignore the 0 hardwired capability, find next one */ +break ; + } + } + +/* next capability */ + reg_field =pci_get_byte (config + reg_field +PCI_CAP_LIST_NEXT ); + } + +out: + *data = reg_field; +return 0 ; +} + + +/************* +* Main +*/ + +static uint8_t find_cap_offset (XenPCIPassthroughState *s,uint8_t cap) +{ +uint8_t id; +unsigned max_cap =PCI_CAP_MAX ; +uint8_t pos =PCI_CAPABILITY_LIST ; +uint8_t status =0 ; + +if (host_pci_get_byte (s->real_device,PCI_STATUS , &status)) { +return 0 ; + } +if ((status &PCI_STATUS_CAP_LIST ) ==0 ) { +return 0 ; + } + +while (max_cap--) { +if (host_pci_get_byte (s->real_device, pos, &pos)) { +break ; + } +if (pos <PCI_CONFIG_HEADER_SIZE ) { +break ; + } + + pos &= ~3 ; +if (host_pci_get_byte (s->real_device, pos +PCI_CAP_LIST_ID , &id)) { +break ; + } + +if (id ==0xff ) { +break ; + } +if (id == cap) { +return pos; + } + + pos +=PCI_CAP_LIST_NEXT ; + } +return 0 ; +} + +static int pt_config_reg_init (XenPCIPassthroughState *s, +XenPTRegGroup *reg_grp,XenPTRegInfo *reg) +{ +XenPTReg *reg_entry; +uint32_t data =0 ; +int rc =0 ; + + reg_entry =g_new0 (XenPTReg,1 ); + reg_entry->reg = reg; + +if (reg->init) { +/* initialize emulate register */ + rc = reg->init (s, reg_entry->reg, + reg_grp->base_offset + reg->offset, &data); +if (rc <0 ) { +free (reg_entry); +return rc; + } +if (data ==PT_INVALID_REG ) { +/* free unused BAR register entry */ +free (reg_entry); +return 0 ; + } +/* set register value */ + reg_entry->data = data; + } +/* list add register entry */ +QLIST_INSERT_HEAD (®_grp->reg_tbl_list, reg_entry, entries); + +return 0 ; +} + +int pt_config_init (XenPCIPassthroughState *s) +{ +int i, rc; + +QLIST_INIT (&s->reg_grp_tbl); + +for (i =0 ; pt_emu_reg_grp_tbl[i].grp_size !=0 ; i++) { +uint32_t reg_grp_offset =0 ; +XenPTRegGroup *reg_grp_entry =NULL ; + +if (pt_emu_reg_grp_tbl[i].grp_id !=0xFF ) { +if (pt_hide_dev_cap (s->real_device, + pt_emu_reg_grp_tbl[i].grp_id)) { +continue ; + } + + reg_grp_offset =find_cap_offset (s, pt_emu_reg_grp_tbl[i].grp_id); + +if (!reg_grp_offset) { +continue ; + } + } + + reg_grp_entry =g_new0 (XenPTRegGroup,1 ); +QLIST_INIT (®_grp_entry->reg_tbl_list); +QLIST_INSERT_HEAD (&s->reg_grp_tbl, reg_grp_entry, entries); + + reg_grp_entry->base_offset = reg_grp_offset; + reg_grp_entry->reg_grp = pt_emu_reg_grp_tbl + i; +if (pt_emu_reg_grp_tbl[i].size_init) { +/* get register group size */ + rc = pt_emu_reg_grp_tbl[i].size_init (s, reg_grp_entry->reg_grp, + reg_grp_offset, + ®_grp_entry->size); +if (rc <0 ) { +pt_config_delete (s); +return rc; + } + } + +if (pt_emu_reg_grp_tbl[i].grp_type ==GRP_TYPE_EMU ) { +if (pt_emu_reg_grp_tbl[i].emu_reg_tbl) { +int j =0 ; +XenPTRegInfo *reg_tbl = pt_emu_reg_grp_tbl[i].emu_reg_tbl; +/* initialize capability register */ +for (j =0 ; reg_tbl->size !=0 ; j++, reg_tbl++) { +/* initialize capability register */ + rc =pt_config_reg_init (s, reg_grp_entry, reg_tbl); +if (rc <0 ) { +pt_config_delete (s); +return rc; + } + } + } + } + } + +return 0 ; +} + +/* delete all emulate register */ +void pt_config_delete (XenPCIPassthroughState *s) +{ +struct XenPTRegGroup *reg_group, *next_grp; +struct XenPTReg *reg, *next_reg; + +/* free all register group entry */ + QLIST_FOREACH_SAFE(reg_group, &s->reg_grp_tbl, entries, next_grp) { +/* free all register entry */ + QLIST_FOREACH_SAFE(reg, ®_group->reg_tbl_list, entries, next_reg) { +QLIST_REMOVE (reg, entries); +g_free (reg); + } + +QLIST_REMOVE (reg_group, entries); +g_free (reg_group); + } +}Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 08/11] Introduce Xen PCI Passthrough, PCI config space helpers (2/3)
Quoted 6 lines · Michael S. Tsirkin
Quoted 5 lines · Anthony PERARD
From: Allen Kay <[email protected]>
A more complete history can be found here:
git://xenbits.xensource.com/qemu-xen-unstable.git
Quoted 8 lines
Quoted 3 lines
Signed-off-by Allen Kay <[email protected]>Signed-off-by Guy Zana <[email protected]>Signed-off-by Anthony PERARD <[email protected]>A lot of functionality seems to be duplicated with kvm's device assignment code.
Makes sense to try separating generic bits so that code can be shared?
We had this discussion a while back, see Avi's suggestion to merge both and abstracting them later:
PATCH v7 9/11 Introduce apic-msidef.h
In reply to Anthony PERARDThis 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 | 28 ++++++++++++++++++++++++++++ hw/apic.c | 11 +---------- 2 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 hw/apic-msidef.h
hw/apic-msidef.h new file +28 -0
@@ -0,0 +1,28 @@ +#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_DEST_ID_SHIFT 12 +#define MSI_ADDR_DEST_ID_MASK 0x00ffff0 + +#endif /* HW_APIC_MSIDEF_H */
hw/apic.c +1 -10
@@ -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 -static APICCommonState *local_apics[MAX_APICS +1 ];static void apic_set_irq (APICCommonState *s,int vector_num,int trigger_mode);
Signature · 1 line
Anthony PERARD
PATCH v7 10/11 Introduce Xen PCI Passthrough, MSI (3/3)
In reply to Anthony PERARDFrom: Jiang Yunhong <[email protected]>
A more complete history can be found here:git://xenbits.xensource.com/qemu-xen-unstable.git
--- Makefile.target | 1 + hw/apic-msidef.h | 2 + hw/xen_pci_passthrough.c | 32 ++ hw/xen_pci_passthrough.h | 48 +++ hw/xen_pci_passthrough_config_init.c | 472 ++++++++++++++++++++++++ hw/xen_pci_passthrough_msi.c | 664 ++++++++++++++++++++++++++++++++++ 6 files changed, 1219 insertions(+), 0 deletions(-) create mode 100644 hw/xen_pci_passthrough_msi.c
Makefile.target +1 -0
@@ -226,6 +226,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/apic-msidef.h +2 -0
@@ -22,6 +22,8 @@#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
hw/xen_pci_passthrough.c +32 -0
@@ -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>
@@ -362,6 +376,7 @@ static void pt_iomem_map(XenPCIPassthroughState *s, int i, }if (!first_map && old_ebase !=PT_PCI_BAR_UNMAPPED ) { +pt_add_msix_mapping (s, i);/* Remove old mapping */ rc = xc_domain_memory_mapping(xen_xc, xen_domid, old_ebase >> XC_PAGE_SHIFT,
@@ -386,6 +401,16 @@ static void pt_iomem_map(XenPCIPassthroughState *s, int i,if (rc) {PT_ERR (&s->dev,"create new mapping failed! (rc: %i)\n" , rc); } + + rc =pt_remove_msix_mapping (s, i); +if (rc !=0 ) { +PT_ERR (&s->dev,"Remove MSI-X MMIO mapping failed! (rc: %d)\n" , + rc); + } + +if (old_ebase != e_phys && old_ebase !=-1 ) { +pt_msix_update_remap (s, i); + } } }
@@ -766,6 +791,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 +48 -0
64 lines not on this page · raw message
hw/xen_pci_passthrough_config_init.c +472 -0
502 lines not on this page · raw message
hw/xen_pci_passthrough_msi.c new file +664 -0
664 lines not on this page · raw message
Signature · 1 line
Anthony PERARD
PATCH v7 11/11 xen passthrough: clean up MSI-X table handling
In reply to Anthony PERARDFrom: Shan Haitao <[email protected]>
This patch does cleaning up of QEMU MSI handling. The fixes are:
1. Changes made to MSI-X table mapping handling to eliminate the small windows in which guest could have access to physical MSI-X table.
2. MSI-X table is mapped as read-only to QEMU, as masking of MSI-X is already in Xen now.
3. For registers that coexists inside the MSI-X table (this could be only PBA I think), value read from physical page would be returned.
Consolidated duplicate code into _pt_iomem_helper(). Fixed formatting.
--- hw/xen_pci_passthrough.c | 80 ++++++++++++++++++++++++++++++++---------- hw/xen_pci_passthrough.h | 11 +++++- hw/xen_pci_passthrough_msi.c | 62 ++++---------------------------- 3 files changed, 78 insertions(+), 75 deletions(-)
hw/xen_pci_passthrough.c +61 -19
@@ -356,6 +356,53 @@ out: } } +static int pt_iomem_helper (XenPCIPassthroughState *s,int i, +pcibus_t e_base,pcibus_t e_size,int op) +{ +uint64_t msix_last_pfn =0 ; +pcibus_t bar_last_pfn =0 ; +pcibus_t msix_table_size =0 ; +XenPTMSIX *msix = s->msix; +int rc =0 ; + +if (!pt_has_msix_mapping (s, i)) { +return xc_domain_memory_mapping (xen_xc, xen_domid, +PFN (e_base), +PFN (s->bases[i].access.maddr), +PFN (e_size +XC_PAGE_SIZE -1 ), + op); + + } + +if (msix->table_off) { + rc =xc_domain_memory_mapping (xen_xc, xen_domid, +PFN (e_base), +PFN (s->bases[i].access.maddr), +PFN (msix->mmio_base_addr) -PFN (e_base), + op); + } + + msix_table_size = msix->total_entries *PCI_MSIX_ENTRY_SIZE ; + msix_last_pfn =PFN (msix->mmio_base_addr + msix_table_size -1 ); + bar_last_pfn =PFN (e_base + e_size -1 ); + +if (rc ==0 && msix_last_pfn != bar_last_pfn) { +assert (msix_last_pfn < bar_last_pfn); + + rc =xc_domain_memory_mapping (xen_xc, xen_domid, + msix_last_pfn +1 , +PFN (s->bases[i].access.maddr + + msix->table_off + + msix_table_size + +XC_PAGE_SIZE -1 ), + bar_last_pfn - msix_last_pfn, + op); + } + +return rc; +} + +/* ioport/iomem space*/ static void pt_iomem_map (XenPCIPassthroughState *s,int i,pcibus_t e_phys,pcibus_t e_size)
@@ -376,13 +423,10 @@ static void pt_iomem_map(XenPCIPassthroughState *s, int i, }if (!first_map && old_ebase !=PT_PCI_BAR_UNMAPPED ) { -pt_add_msix_mapping (s, i); -/* Remove old mapping */ - rc =xc_domain_memory_mapping (xen_xc, xen_domid, - old_ebase >>XC_PAGE_SHIFT , - s->bases[i].access.maddr >>XC_PAGE_SHIFT , - (e_size +XC_PAGE_SIZE -1 ) >>XC_PAGE_SHIFT , -DPCI_REMOVE_MAPPING ); +if (pt_has_msix_mapping (s, i)) { +memory_region_set_enabled (&s->msix->mmio, false); + } + rc =pt_iomem_helper (s, i, old_ebase, e_size,DPCI_REMOVE_MAPPING );if (rc) {PT_ERR (&s->dev,"remove old mapping failed! (rc: %i)\n" , rc);return ;
@@ -391,21 +435,19 @@ static void pt_iomem_map(XenPCIPassthroughState *s, int i,/* map only valid guest address */ if (e_phys !=PCI_BAR_UNMAPPED ) { -/* Create new mapping */ - rc =xc_domain_memory_mapping (xen_xc, xen_domid, - s->bases[i].e_physbase >>XC_PAGE_SHIFT , - s->bases[i].access.maddr >>XC_PAGE_SHIFT , - (e_size+XC_PAGE_SIZE -1 ) >>XC_PAGE_SHIFT , -DPCI_ADD_MAPPING ); +if (pt_has_msix_mapping (s, i)) { +XenPTMSIX *msix = s->msix; -if (rc) { -PT_ERR (&s->dev,"create new mapping failed! (rc: %i)\n" , rc); + msix->mmio_base_addr = s->bases[i].e_physbase + msix->table_off; + +memory_region_set_enabled (&msix->mmio, true); } - rc =pt_remove_msix_mapping (s, i); -if (rc !=0 ) { -PT_ERR (&s->dev,"Remove MSI-X MMIO mapping failed! (rc: %d)\n" , - rc); + rc =pt_iomem_helper (s, i, e_phys, e_size,DPCI_ADD_MAPPING ); + +if (rc) { +PT_ERR (&s->dev,"create new mapping failed! (rc: %i)\n" , rc); +return ; }if (old_ebase != e_phys && old_ebase !=-1 ) {
hw/xen_pci_passthrough.h +9 -2
@@ -30,6 +30,9 @@ void pt_log(const PCIDevice *d, const char *f, ...) GCC_FMT_ATTR(2, 3);#endif +/* Helper */ +#define PFN (x) ((x) >> XC_PAGE_SHIFT) +typedef struct XenPTRegInfo XenPTRegInfo ;typedef struct XenPTReg XenPTReg ;
@@ -295,7 +298,11 @@ void pt_msix_delete(XenPCIPassthroughState *s);int pt_msix_update (XenPCIPassthroughState *s);int pt_msix_update_remap (XenPCIPassthroughState *s,int bar_index);void pt_msix_disable (XenPCIPassthroughState *s); -int pt_add_msix_mapping (XenPCIPassthroughState *s,int bar_index); -int pt_remove_msix_mapping (XenPCIPassthroughState *s,int bar_index); + +static inline bool pt_has_msix_mapping (XenPCIPassthroughState *s,int bar) +{ +return s->msix && s->msix->bar_index == bar; +} +#endif /* !QEMU_HW_XEN_PCI_PASSTHROUGH_H */
hw/xen_pci_passthrough_msi.c +8 -54
98 lines not on this page · raw message
Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 11/11] xen passthrough: clean up MSI-X table handling
Quoted 1 line
Quoted 1 line
Quoted 1 line · Anthony PERARD
Wouldn't thus much better be merged into the prior patch(es)? After all you're not trying to reconstruct the Xen-side history of this code anyway.
Jan
Quoted 274 lines
From: Shan Haitao <[email protected]>
This patch does cleaning up of QEMU MSI handling. The fixes are:
1. Changes made to MSI-X table mapping handling to eliminate the small windows in which guest could have access to physical MSI-X table.
2. MSI-X table is mapped as read-only to QEMU, as masking of MSI-X is already in Xen now.
3. For registers that coexists inside the MSI-X table (this could be only PBA I think), value read from physical page would be returned.Signed-off-by Shan Haitao <[email protected]>Consolidated duplicate code into
_pt_iomem_helper(). Fixed formatting.Signed-off-by Jan Beulich <[email protected]>Signed-off-by Anthony PERARD <[email protected]>--- hw/xen_pci_passthrough.c | 80 ++++++++++++++++++++++++++++++++---------- hw/xen_pci_passthrough.h | 11 +++++- hw/xen_pci_passthrough_msi.c | 62 ++++---------------------------- 3 files changed, 78 insertions(+), 75 deletions(-)hw/xen_pci_passthrough.c +51
-7@@ -356,6 +356,53 @@ out: } } +static int pt_iomem_helper (XenPCIPassthroughState *s,int i, +pcibus_t e_base,pcibus_t e_size,int op) +{ +uint64_t msix_last_pfn =0 ; +pcibus_t bar_last_pfn =0 ; +pcibus_t msix_table_size =0 ; +XenPTMSIX *msix = s->msix; +int rc =0 ; + +if (!pt_has_msix_mapping (s, i)) { +return xc_domain_memory_mapping (xen_xc, xen_domid, +PFN (e_base), +PFN (s->bases[i].access.maddr), +PFN (e_size +XC_PAGE_SIZE -1 ), + op); + + } + +if (msix->table_off) { + rc =xc_domain_memory_mapping (xen_xc, xen_domid, +PFN (e_base), +PFN (s->bases[i].access.maddr), +PFN (msix->mmio_base_addr) -PFN (e_base), + op); + } + + msix_table_size = msix->total_entries *PCI_MSIX_ENTRY_SIZE ; + msix_last_pfn =PFN (msix->mmio_base_addr + msix_table_size -1 ); + bar_last_pfn =PFN (e_base + e_size -1 ); + +if (rc ==0 && msix_last_pfn != bar_last_pfn) { +assert (msix_last_pfn < bar_last_pfn); + + rc =xc_domain_memory_mapping (xen_xc, xen_domid, + msix_last_pfn +1 , +PFN (s->bases[i].access.maddr + + msix->table_off + + msix_table_size + +XC_PAGE_SIZE -1 ), + bar_last_pfn - msix_last_pfn, + op); + } + +return rc; +} + +/* ioport/iomem space*/ static void pt_iomem_map (XenPCIPassthroughState *s,int i,pcibus_t e_phys,pcibus_t e_size)@@ -376,13 +423,10 @@ static void pt_iomem_map(XenPCIPassthroughState *s, int i, }if (!first_map && old_ebase !=PT_PCI_BAR_UNMAPPED ) { -pt_add_msix_mapping (s, i); -/* Remove old mapping */ - rc =xc_domain_memory_mapping (xen_xc, xen_domid, - old_ebase >>XC_PAGE_SHIFT , - s->bases[i].access.maddr >>XC_PAGE_SHIFT , - (e_size +XC_PAGE_SIZE -1 ) >>XC_PAGE_SHIFT , -DPCI_REMOVE_MAPPING ); +if (pt_has_msix_mapping (s, i)) { +memory_region_set_enabled (&s->msix->mmio, false); + } + rc =pt_iomem_helper (s, i, old_ebase, e_size,DPCI_REMOVE_MAPPING );if (rc) {PT_ERR (&s->dev,"remove old mapping failed! (rc: %i)\n" , rc);return;hw/xen_pci_passthrough.c +10
-12@@ -391,21 +435,19 @@ static void pt_iomem_map(XenPCIPassthroughState *s, int i,/* map only valid guest address */ if (e_phys != PCI_BAR_UNMAPPED) { -/* Create new mapping */ - rc =xc_domain_memory_mapping (xen_xc, xen_domid, - s->bases[i].e_physbase >>XC_PAGE_SHIFT , - s->bases[i].access.maddr >>XC_PAGE_SHIFT , - (e_size+XC_PAGE_SIZE -1 ) >>XC_PAGE_SHIFT , -DPCI_ADD_MAPPING ); +if (pt_has_msix_mapping (s, i)) { +XenPTMSIX *msix = s->msix; -if (rc) { -PT_ERR (&s->dev,"create new mapping failed! (rc: %i)\n" , rc); + msix->mmio_base_addr = s->bases[i].e_physbase + msix->table_off; + +memory_region_set_enabled (&msix->mmio, true); } - rc =pt_remove_msix_mapping (s, i); -if (rc !=0 ) { -PT_ERR (&s->dev,"Remove MSI-X MMIO mapping failed! (rc: %d)\n" , - rc); + rc =pt_iomem_helper (s, i, e_phys, e_size,DPCI_ADD_MAPPING ); + +if (rc) { +PT_ERR (&s->dev,"create new mapping failed! (rc: %i)\n" , rc); +return ; }if (old_ebase != e_phys && old_ebase != -1) {hw/xen_pci_passthrough.h +9
-2@@ -30,6 +30,9 @@ void pt_log(const PCIDevice *d, const char *f, ...)GCC_FMT_ATTR (2 ,3 );#endif +/* Helper */ +#define PFN (x) ((x) >> XC_PAGE_SHIFT) +typedef struct XenPTRegInfo XenPTRegInfo ;typedef struct XenPTReg XenPTReg ;@@ -295,7 +298,11 @@ void pt_msix_delete(XenPCIPassthroughState *s);int pt_msix_update (XenPCIPassthroughState *s);int pt_msix_update_remap (XenPCIPassthroughState *s,int bar_index);void pt_msix_disable (XenPCIPassthroughState *s); -int pt_add_msix_mapping (XenPCIPassthroughState *s,int bar_index); -int pt_remove_msix_mapping (XenPCIPassthroughState *s,int bar_index); + +static inline bool pt_has_msix_mapping (XenPCIPassthroughState *s,int bar) +{ +return s->msix && s->msix->bar_index == bar; +} +#endif /* !QEMU_HW_XEN_PCI_PASSTHROUGH_H */ hw/xen_pci_passthrough_msi.c +0
-10@@ -300,16 +300,6 @@ static int msix_set_enable(XenPCIPassthroughState *s, bool enabled) enabled); } -static void mask_physical_msix_entry (XenPCIPassthroughState *s, -int entry_nr,int mask) -{ -void *phys_off; - - phys_off = s->msix->phys_iomem_base +PCI_MSIX_ENTRY_SIZE * entry_nr - +PCI_MSIX_ENTRY_VECTOR_CTRL ; - *(uint32_t *)phys_off = mask; -} -static int pt_msix_update_one (XenPCIPassthroughState *s,int entry_nr) {XenPTMSIXEntry *entry = NULL;hw/xen_pci_passthrough_msi.c +7
-4@@ -480,8 +470,6 @@ static void pci_msix_write(void *opaque, target_phys_addr_t addr, if (msix->enabled && !(val & PCI_MSIX_ENTRY_CTRL_MASKBIT)) { pt_msix_update_one(s, entry_nr); } -mask_physical_msix_entry (s, entry_nr, - entry->vector_ctrl &PCI_MSIX_ENTRY_CTRL_MASKBIT ); } }@@ -493,14 +481,19 @@ static uint64_t pci_msix_read(void *opaque,target_phys_addr_t addr, int entry_nr, offset; entry_nr = addr /PCI_MSIX_ENTRY_SIZE ; -if (entry_nr <0 || entry_nr >= msix->total_entries) { +if (entry_nr <0 ) {PT_ERR (&s->dev,"asked MSI-X entry '%i' invalid!\n" , entry_nr);return 0 ; } offset = addr %PCI_MSIX_ENTRY_SIZE ; -return get_entry_value (&msix->msix_entry[entry_nr], offset); +if (addr < msix->total_entries *PCI_MSIX_ENTRY_SIZE ) { +return get_entry_value (&msix->msix_entry[entry_nr], offset); + }else { +/* Pending Bit Array (PBA) */ +return *(uint32_t *)(msix->phys_iomem_base + addr); + } }static const MemoryRegionOps
pci_msix_ops= {hw/xen_pci_passthrough_msi.c +1
-40@@ -514,45 +507,6 @@ static const MemoryRegionOps pci_msix_ops = { }, }; -int pt_add_msix_mapping (XenPCIPassthroughState *s,int bar_index) -{ -XenPTMSIX *msix = s->msix; - -if (!(s->msix && s->msix->bar_index == bar_index)) { -return 0 ; - } - -memory_region_set_enabled (&msix->mmio, false); -return xc_domain_memory_mapping (xen_xc, xen_domid, - s->msix->mmio_base_addr >>XC_PAGE_SHIFT , - (s->bases[bar_index].access.maddr + s->msix->table_off) - >>XC_PAGE_SHIFT , - (s->msix->total_entries *PCI_MSIX_ENTRY_SIZE +XC_PAGE_SIZE -1 ) - >>XC_PAGE_SHIFT , -DPCI_ADD_MAPPING ); -} - -int pt_remove_msix_mapping (XenPCIPassthroughState *s,int bar_index) -{ -XenPTMSIX *msix = s->msix; - -if (!(msix && msix->bar_index == bar_index)) { -return 0 ; - } - -memory_region_set_enabled (&msix->mmio, true); - - msix->mmio_base_addr = s->bases[bar_index].e_physbase + msix->table_off; - -return xc_domain_memory_mapping (xen_xc, xen_domid, - s->msix->mmio_base_addr >>XC_PAGE_SHIFT , - (s->bases[bar_index].access.maddr + s->msix->table_off) - >>XC_PAGE_SHIFT , - (s->msix->total_entries *PCI_MSIX_ENTRY_SIZE +XC_PAGE_SIZE -1 ) - >>XC_PAGE_SHIFT , -DPCI_REMOVE_MAPPING ); -} -int pt_msix_init (XenPCIPassthroughState *s,uint32_t base) {uint8_t id =0 ;@@ -609,7 +563,7 @@ int pt_msix_init(XenPCIPassthroughState *s, uint32_t base) msix->phys_iomem_base = mmap(NULL, total_entries * PCI_MSIX_ENTRY_SIZE + msix->table_offset_adjust, - PROT_WRITE | PROT_READ, + PROT_READ, MAP_SHARED | MAP_LOCKED, fd,msix->table_base + table_off - msix->table_offset_adjust);Signature · 1 line
Anthony PERARD
Re: [Qemu-devel] [PATCH V7 11/11] xen passthrough: clean up MSI-X table handling
Quoted 3 lines · Jan Beulich
Wouldn't thus much better be merged into the prior patch(es)? After all you're not trying to reconstruct the Xen-side history of this code anyway.
Yes, that probably better than having an extra patch. There is a "link" to the history, anyway. I'll merge the patch.
Thanks,
Signature · 1 line
Anthony PERARD
Hi!
i wanted to test these patches against
http://xenbits.xensource.com/xen-unstable.hg but it seems to check out an outdated version of upstream-qemu, even with "QEMU=upstream".
Where can i check out the qemu-upstream version to which these patches apply?
Thanks and Greetings
Tobias
Am Freitag, 17. Februar 2012, 18:08:34 schrieb Anthony PERARD:
Quoted 139 lines
Hi all,
This patch series introduces the PCI passthrough for Xen.
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_idsandpci_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:
- 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.
- usepci_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 functionget_valuetoget_hex_value(and return the
same error value hasget_resource)Change v3-v4:
- host_pci_get_* can now return an error, and take an extra parameter, a pointer to store the wanted value.
- Thememory_regionfor the PCI BAR are handled "manualy" because callingpci_default_write_configwas not possible, because the XenPT handle the PCIIORegion it self. This make possible to do adevice_remove.
- Introduction ofPT_ERRandPT_WARNmacro to print debug and error
messages. Also, these macro as well asPT_LOGwill always print the short BDF of the device in the guest point of view.
-PT_ERRis print by default (for all error messages).
- Some debug/error message have been improve and should be a bit more
useful. -hw_errorhave been removed from the code, and have been replaced by either a call toqemu_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;
- inhost-pci-device.c:
- Return more usefull error code inget_ressource().
- Use macro inhost_pci_find_ext_cap_offsetinstead of raw number. But
I still not sure ifPCI_MAX_EXT_CAPis 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 (withbase_addr, size, flags)
- save all flags from ./resource and store it in a separate field.
- fix endianess on write
- newhost_pci_dev_putfunction
- usepci.clike interface host_pci_get/set_byte/word/long (instead of host_pci_read/write_)
- compile HostPCIDevice only on linux (as well asxen_pci_passthrough)
- introduceapic-msidef.hfile.
- no morerun_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 varmapped_machine_irq(local toxen_pci_passthrough.c)
- add msitranslate and power-mgmt ad qdev propertyAllen Kay (2):
Introduce Xen PCI Passthrough, qdevice (1/3) Introduce Xen PCI Passthrough, PCI config space helpers (2/3)Anthony PERARD (6):
pci_ids: AddINTEL_82599_VFid.
pci_regs: Fix value ofPCI_EXP_TYPE_RC_EC.
pci_regs: AddPCI_EXP_TYPE_PCIE_BRIDGE
configure: Introduce --enable-xen-pci-passthrough. Introduce HostPCIDevice to access a pci device on the host. Introduceapic-msidef.hJiang Yunhong (1):
Introduce Xen PCI Passthrough, MSI (3/3)Shan Haitao (1):
xen passthrough: clean up MSI-X table handlingYuji Shimada (1):
pci.c: Addpci_check_bar_overlapMakefile.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 | 47 + hw/pci.h | 3 + hw/pci_ids.h | 1 + hw/pci_regs.h | 3 +- hw/xen_common.h | 3 + hw/xen_pci_passthrough.c | 898 ++++++++++++++++ hw/xen_pci_passthrough.h | 308 ++++++ hw/xen_pci_passthrough_config_init.c | 1914 ++++++++++++++++++++++++++++++++++ hw/xen_pci_passthrough_msi.c | 618 +++++++++++ xen-all.c | 12 + 16 files changed, 4221 insertions(+), 11 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
Quoted 5 lines · Tobias Geiger
i wanted to test these patches against
http://xenbits.xensource.com/xen-unstable.hg but it seems to check out an outdated version of upstream-qemu, even with "QEMU=upstream".Where can i check out the qemu-upstream version to which these patches apply?
Hi,
http://git.qemu.org/git/qemu.git or git://git.qemu.org/qemu.git
Regards,
Signature · 1 line
Anthony PERARD
Hi Anthony
thanks for that hint.
I checked out a fresh xen-unstable via hg, compiled it using "QEMU=upstream" , checked out qemu.git from where you mentioned and used the resulting qemu- system-x86_64 binary as "device_model_override" . so far so good.
My Domu (Win7_64) boots fine, but 2 of my 6 passed-through pcidevices dont get passed through at all:
libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.0/reset returned -1: Invalid argument
libxl: error: libxl_qmp.c:239:qmp_handle_error_response: received an error message from QMP server: Device 'xen-pci-passthrough' could not be initialized
libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.1/reset returned -1: Invalid argument
libxl: error: libxl_qmp.c:239:qmp_handle_error_response: received an error message from QMP server: Device 'xen-pci-passthrough' could not be initialized
The other 4 PCI-Ids (USB Controller) get passed through, but the USB-Devices attached to them are "Not Working" according to Windows Device-Manager.
All Devices worked with the old qemu-dm (traditional).
Anything i can do to debug this further?
Greetings
Tobias
Am Montag, 20. Februar 2012, 12:31:52 schrieb Anthony PERARD:
Quoted 1 line
On Mon, Feb 20, 2012 at 10:51, Tobias Geiger <[email protected]>
Quoted 12 lines · wrote:
Quoted 6 lines
i wanted to test these patches against
http://xenbits.xensource.com/xen-unstable.hg but it seems to check out an outdated version of upstream-qemu, even with "QEMU=upstream".Where can i check out the qemu-upstream version to which these patches apply?
Hi,
http://git.qemu.org/git/qemu.git or
git://git.qemu.org/qemu.gitRegards,
Quoted 11 lines · Tobias Geiger
My Domu (
Win7_64) boots fine, but 2 of my 6 passed-through pcidevices dont get passed through at all:libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.0/reset returned -1: Invalid argument
libxl: error: libxl_qmp.c:239:qmp_handle_error_response: received an error message from QMP server: Device 'xen-pci-passthrough' could not be initialized
libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.1/reset returned -1: Invalid argument
libxl: error: libxl_qmp.c:239:qmp_handle_error_response: received an error message from QMP server: Device 'xen-pci-passthrough' could not be initialized
These two errors are probably because my patch series depend on one other patch sent earlier:
http://lists.xen.org/archives/html/xen-devel/2012-02/msg01027.html
Quoted 6 lines
The other 4 PCI-Ids (USB Controller) get passed through, but the USB-Devices attached to them are "Not Working" according to Windows Device-Manager.
All Devices worked with the old qemu-dm (traditional).
Anything i can do to debug this further?
You can send the log of qemu ( /var/log/xen/qemu-dm-$vm_name.log ). There is also a way to have more from QEMU by decommenting this two lines:
/* #define PT_LOGGING_ENABLED */
/* #define PT_DEBUG_PCI_CONFIG_ACCESS */
in hw/xen_pci_passthrough.h
Thanks for the testing,
Signature · 1 line
Anthony PERARD
Hi
With the patch you mentioned, xl create looks more familiar:
libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.0/reset returned -1: Invalid argument
libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.1/reset returned -1: Invalid argument
This is actually "normal", at least compared to traditional qemu-dm - i guess due to missing FLR (Bios is capable, but Card not i assume).
Anyhow - the downside of the patch is, qemu doesnt even start :)
This is qemu-dm.log (with debugging enabled):
[00:06.0] pci_intx: intx=1
[00:06.0] pt_initfn: Real physical device 03:00.0 registered successfuly!
[00:07.0] pt_initfn: Assigning real physical device 03:00.1 to devfn 0x38
[00:07.0] pt_register_regions: IO region 0 registered (size=0x00004000 base_addr=0xc3120000 type: 0)
[00:07.0] pci_intx: intx=2
[00:07.0] pt_initfn: Real physical device 03:00.1 registered successfuly!
[00:08.0] pt_initfn: Assigning real physical device 00:1d.0 to devfn 0x40
[00:08.0] pt_register_regions: IO region 4 registered (size=0x00000020 base_addr=0x00005080 type: 0x1)
[00:08.0] pci_intx: intx=1
[00:08.0] pt_initfn: Real physical device 00:1d.0 registered successfuly!
[00:09.0] pt_initfn: Assigning real physical device 00:1d.1 to devfn 0x48
[00:09.0] pt_register_regions: IO region 4 registered (size=0x00000020 base_addr=0x00005060 type: 0x1)
[00:09.0] pci_intx: intx=2
[00:09.0] pt_initfn: Real physical device 00:1d.1 registered successfuly!
[00:0a.0] pt_initfn: Assigning real physical device 00:1d.2 to devfn 0x50
[00:0a.0] pt_register_regions: IO region 4 registered (size=0x00000020 base_addr=0x00005040 type: 0x1)
[00:0a.0] pci_intx: intx=3
[00:0a.0] pt_initfn: Real physical device 00:1d.2 registered successfuly!
[00:0b.0] pt_initfn: Assigning real physical device 00:1d.7 to devfn 0x58
[00:0b.0] pt_register_regions: IO region 0 registered (size=0x00000400 base_addr=0xc3321000 type: 0)
[00:0b.0] pci_intx: intx=1
[00:0b.0] pt_initfn: Real physical device 00:1d.7 registered successfuly!
[00:06.0] pt_pci_read_config: address=0x000a val=0x00000300 len=2
[00:06.0] pt_pci_read_config: address=0x0000 val=0x00001002 len=2
[00:06.0] pt_pci_read_config: address=0x0002 val=0x00006718 len=2
[00:06.0] pt_pci_read_config: address=0x0010 val=0x0000000c len=4
[00:06.0] pt_pci_write_config: address=0x0010 val=0xffffffff len=4
[00:06.0] pt_iomem_map: BAR 0, e_phys=0xffffffffffffffff maddr=0xe0000000 len=0x10000000 first_map=1
[00:06.0] pt_pci_read_config: address=0x0010 val=0xf000000c len=4
[00:06.0] pt_pci_write_config: address=0x0010 val=0x0000000c len=4
[00:06.0] pt_iomem_map: BAR 0, e_phys=0xffffffffffffffff maddr=0xe0000000 len=0x10000000 first_map=0
[00:06.0] pt_pci_read_config: address=0x0018 val=0x00000004 len=4
[00:06.0] pt_pci_write_config: address=0x0018 val=0xffffffff len=4
[00:06.0] pt_iomem_map: BAR 2, e_phys=0xffffffffffffffff maddr=0xc3100000 len=0x20000 first_map=1
[00:06.0] pt_pci_read_config: address=0x0018 val=0xfffe0004 len=4
[00:06.0] pt_pci_write_config: address=0x0018 val=0x00000004 len=4
[00:06.0] pt_iomem_map: BAR 2, e_phys=0xffffffffffffffff maddr=0xc3100000 len=0x20000 first_map=0
[00:06.0] pt_pci_read_config: address=0x0020 val=0x00000001 len=4
[00:06.0] pt_pci_write_config: address=0x0020 val=0xffffffff len=4
[00:06.0] pt_ioport_map: BAR 4, e_phys=0xffffffffffffffff pio_base=0x2000 len=256 first_map=1
[00:06.0] pt_pci_read_config: address=0x0020 val=0xffffff01 len=4
[00:06.0] pt_pci_write_config: address=0x0020 val=0x00000001 len=4
[00:06.0] pt_ioport_map: BAR 4, e_phys=0xffffffffffffffff pio_base=0x2000 len=256 first_map=0
[00:06.0] pt_pci_read_config: address=0x0024 val=0x00000000 len=4
[00:06.0] pt_pci_write_config: address=0x0024 val=0xffffffff len=4
[00:06.0] pt_pci_read_config: address=0x0024 val=0x00000000 len=4
[00:06.0] pt_pci_write_config: address=0x0024 val=0x00000000 len=4
[00:06.0] pt_pci_read_config: address=0x0030 val=0x00000000 len=4
[00:06.0] pt_pci_write_config: address=0x0030 val=0xffffffff len=4
[00:06.0] pt_iomem_map: BAR 6, e_phys=0xffffffffffffffff maddr=0xc3140000 len=0x20000 first_map=1
[00:06.0] pt_pci_read_config: address=0x0030 val=0xfffe0001 len=4
[00:06.0] pt_pci_write_config: address=0x0030 val=0x00000000 len=4
[00:06.0] pt_iomem_map: BAR 6, e_phys=0xffffffffffffffff maddr=0xc3140000 len=0x20000 first_map=0
[00:06.0] pt_pci_read_config: address=0x003d val=0x00000001 len=1
[00:06.0] pt_pci_write_config: address=0x003c val=0x0000000b len=1
[00:06.0] pt_pci_read_config: address=0x0004 val=0x00000000 len=2
[00:06.0] pt_pci_write_config: address=0x0004 val=0x00000004 len=2
qemu-system-x86_64: /usr/src/xen-with-upstream-qemu-patch/upstream-qemu/qemu/memory.c:1378: memory_region_del_subregion: Assertion `subregion->parent == mr' failed. Waiting for data...
Am 20.02.2012 18:08, schrieb Anthony PERARD:
Quoted 30 lines · Tobias Geiger
Quoted 11 lines
My Domu (
Win7_64) boots fine, but 2 of my 6 passed-through pcidevices dont get passed through at all:libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.0/reset returned -1: Invalid argument
libxl: error: libxl_qmp.c:239:qmp_handle_error_response: received an error message from QMP server: Device 'xen-pci-passthrough' could not be initialized
libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.1/reset returned -1: Invalid argument
libxl: error: libxl_qmp.c:239:qmp_handle_error_response: received an error message from QMP server: Device 'xen-pci-passthrough' could not be initializedThese two errors are probably because my patch series depend on one other patch sent earlier:
http://lists.xen.org/archives/html/xen-devel/2012-02/msg01027.htmlQuoted 6 lines
The other 4 PCI-Ids (USB Controller) get passed through, but the USB-Devices attached to them are "Not Working" according to Windows Device-Manager.
All Devices worked with the old qemu-dm (traditional).
Anything i can do to debug this further?
You can send the log of qemu (
/var/log/xen/qemu-dm-$vm_name.log). There is also a way to have more from QEMU by decommenting this two lines:
/* #definePT_LOGGING_ENABLED*/
/* #definePT_DEBUG_PCI_CONFIG_ACCESS*/
inhw/xen_pci_passthrough.hThanks for the testing,
Quoted 10 lines · Tobias Geiger
With the patch you mentioned, xl create looks more familiar:
libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.0/reset returned -1: Invalid argument
libxl: error: libxl_pci.c:756:libxl__device_pci_reset: write to /sys/bus/pci/devices/0000:03:00.1/reset returned -1: Invalid argumentThis is actually "normal", at least compared to traditional qemu-dm - i guess due to missing FLR (Bios is capable, but Card not i assume).
:-)
Quoted 1 line
Anyhow - the downside of the patch is, qemu doesnt even start :)
Nop, qemu start but exit sooner.
Quoted 1 line
This is qemu-dm.log (with debugging enabled):
This is probably only the end of the logs, but thx.
Quoted 6 lines
[00:06.0]
pt_pci_read_config: address=0x0030 val=0x00000000 len=4
[00:06.0]pt_pci_write_config: address=0x0030 val=0xffffffff len=4
[00:06.0]pt_iomem_map: BAR 6, e_phys=0xffffffffffffffff maddr=0xc3140000 len=0x20000 first_map=1 [00:06.0]pt_pci_read_config: address=0x0030 val=0xfffe0001 len=4
[00:06.0]pt_pci_write_config: address=0x0030 val=0x00000000 len=4
[00:06.0]pt_iomem_map: BAR 6, e_phys=0xffffffffffffffff maddr=0xc3140000 len=0x20000 first_map=0
...
Quoted 3 lines
[00:06.0]
pt_pci_read_config: address=0x0004 val=0x00000000 len=2
[00:06.0]pt_pci_write_config: address=0x0004 val=0x00000004 len=2
qemu-system-x86_64:/usr/src/xen-with-upstream-qemu-patch/upstream-qemu/qemu/memory.c:1378:memory_region_del_subregion: Assertion `subregion->parent == mr' failed.
This last line is why QEMU fail and I thinks it's because I never try a PCI device with a ROM pci bar. I'll try to fix this tomorrow.
Thanks,
Signature · 1 line
Anthony PERARD
Quoted 7 lines · Anthony PERARD
Quoted 6 lines · Tobias Geiger
[00:06.0]
pt_pci_read_config: address=0x0030 val=0x00000000 len=4
[00:06.0]pt_pci_write_config: address=0x0030 val=0xffffffff len=4
[00:06.0]pt_iomem_map: BAR 6, e_phys=0xffffffffffffffff maddr=0xc3140000 len=0x20000 first_map=1 [00:06.0]pt_pci_read_config: address=0x0030 val=0xfffe0001 len=4
[00:06.0]pt_pci_write_config: address=0x0030 val=0x00000000 len=4
[00:06.0]pt_iomem_map: BAR 6, e_phys=0xffffffffffffffff maddr=0xc3140000 len=0x20000 first_map=0
Quoted 7 lines
...
Quoted 3 lines
[00:06.0]
pt_pci_read_config: address=0x0004 val=0x00000000 len=2
[00:06.0]pt_pci_write_config: address=0x0004 val=0x00000004 len=2
qemu-system-x86_64:/usr/src/xen-with-upstream-qemu-patch/upstream-qemu/qemu/memory.c:1378:memory_region_del_subregion: Assertion `subregion->parent == mr' failed.This last line is why QEMU fail and I thinks it's because I never try a PCI device with a ROM pci bar. I'll try to fix this tomorrow.
Could you tell me if this "patch" resolve the issue?
hw/xen_pci_passthrough_config_init.c +2 -17
@@ -555,18 +555,16 @@ static int pt_exp_rom_bar_reg_write(XenPCIPassthroughState *s,XenPTReg *reg_entry =NULL ;XenPTRegion *base =NULL ;PCIDevice *d = (PCIDevice *)&s->dev; -PCIIORegion *r;uint32_t writable_mask =0 ;uint32_t throughable_mask =0 ;pcibus_t r_size =0 ;uint32_t bar_emu_mask =0 ;uint32_t bar_ro_mask =0 ; - r = &d->io_regions[PCI_ROM_SLOT ]; - r_size = r->size; + r_size = d->io_regions[PCI_ROM_SLOT ].size; base = &s->bases[PCI_ROM_SLOT ];/* align memory type resource size */ -pt_get_emul_size (base->bar_flag, r_size); + r_size =pt_get_emul_size (base->bar_flag, r_size);/* set emulate mask and read-only mask */ bar_emu_mask = reg->emu_mask;
@@ -576,19 +574,6 @@ static int pt_exp_rom_bar_reg_write(XenPCIPassthroughState *s, writable_mask = ~bar_ro_mask & valid_mask; cfg_entry->data =PT_MERGE_VALUE (*value, cfg_entry->data, writable_mask); -/* update the corresponding virtual region address */ -/* -* When guest code tries to get block size of mmio, it will write all "1"s -* into pci bar register. In this case, cfg_entry->data == writable_mask. -* Especially for devices with large mmio, the value of writable_mask -* is likely to be a guest physical address that has been mapped to ram -* rather than mmio. Remapping this value to mmio should be prevented. -*/ - -if (cfg_entry->data != writable_mask) { - r->addr = cfg_entry->data; - } -/* create value for writing to I/O device register */ throughable_mask = ~bar_emu_mask & valid_mask; *value =PT_MERGE_VALUE (*value, dev_value, throughable_mask);
Thanks,
Signature · 1 line
Anthony PERARD