Support device passthrough when dom0 is PVH on Xen
v7 cover Support device passthrough when dom0 is PVH on Xen
Hi All,
This is v7 series to support passthrough on Xen when dom0 is PVH.v6->v7 changes:
* Due to changes in the implementation of obtaining gsi in the kernel and Xen. Change to use xc_physdev_gsi_from_dev, that requires passing in sbdf instead of irq.
Best regards,
Jiqian Chen
v5->v6 changes:
* Due to changes in the implementation of obtaining gsi in the kernel and Xen. Change to use xc_physdev_gsi_from_irq, instead of gsi sysfs.
v4->v5 changes:
* Add review by Stefano
v3->v4 changes:
* Add gsi into struct XenHostPCIDevice and use gsi number that read from gsi sysfs if it exists, if there is no gsi sysfs, still use irq.
v2->v3 changes:
* Du to changes in the implementation of the second patch on kernel side(that adds a new sysfs for gsi instead of a new syscall), so read gsi number from the sysfs of gsi.
Below is the description of v2 cover letter:
This patch is the v2 of the implementation of passthrough when dom0 is PVH on Xen. Issues we encountered:
1. failed to map pirq for gsi
Problem: qemu will call xc_physdev_map_pirq() to map a passthrough device\u2019s gsi to pirq in function xen_pt_realize(). But failed.
Reason: According to the implement of xc_physdev_map_pirq(), it needs gsi instead of irq, but qemu pass irq to it and treat irq as gsi, it is got from file /sys/bus/pci/devices/xxxx:xx:xx.x/irq in function xen_host_pci_device_get(). But actually the gsi number is not equal with irq. On PVH dom0, when it allocates irq for a gsi in function acpi_register_gsi_ioapic(), allocation is dynamic, and follow the principle of applying first, distributing first. And if you debug the kernel codes
(see function __irq_alloc_descs), you will find the irq number is allocated from small to large by order, but the applying gsi number is not, gsi 38 may come before gsi 28, that causes gsi 38 get a smaller irq number than gsi 28, and then gsi != irq.
Solution: we can record the relation between gsi and irq, then when userspace(qemu) want to use gsi, we can do a translation. The third patch of kernel(xen/privcmd: Add new syscall to get gsi from irq) records all the relations in acpi_register_gsi_xen_pvh() when dom0 initialize pci devices, and provide a syscall for userspace to get the gsi from irq. The third patch of xen(tools: Add new function to get gsi from irq) add a new function xc_physdev_gsi_from_irq() to call the new syscall added on kernel side.
And then userspace can use that function to get gsi. Then xc_physdev_map_pirq() will success.
This v2 on qemu side is the same as the v1
(qemu https://lore.kernel.org/xen-devel/[email protected]/), just call xc_physdev_gsi_from_irq() to get gsi from irq.
Jiqian Chen (1):
xen/pci: get gsi for passthrough devices
hw/xen/xen-host-pci-device.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
RFC PATCH v7 1/1 xen/pci: get gsi for passthrough devices
In PVH dom0, it uses the linux local interrupt mechanism, when it allocs irq for a gsi, it is dynamic, and follow the principle of applying first, distributing first. And the irq number is alloced from small to large, but the applying gsi number is not, may gsi 38 comes before gsi 28, that causes the irq number is not equal with the gsi number. And when passthrough a device, qemu wants to use gsi to map pirq, xen_pt_realize->xc_physdev_map_pirq, but the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq in current code, so it will fail when mapping.
Get gsi by using new function supported by Xen tools.
--- hw/xen/xen-host-pci-device.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
hw/xen/xen-host-pci-device.c +15 -4
@@ -10,6 +10,7 @@#include "qapi/error.h" #include "qemu/cutils.h" #include "xen-host-pci-device.h" +#include "hw/xen/xen_native.h" #define XEN_HOST_PCI_MAX_EXT_CAP \ ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
@@ -329,12 +330,17 @@ int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)return -1 ; } +#define PCI_SBDF (seg, bus, dev, func) \ + ((((uint32_t)(seg)) << 16) | \ + (PCI_BUILD_BDF(bus, PCI_DEVFN(dev, func)))) +void xen_host_pci_device_get (XenHostPCIDevice *d,uint16_t domain,uint8_t bus,uint8_t dev,uint8_t func,Error **errp) {ERRP_GUARD ();unsigned int v; +uint32_t sdbf; d->config_fd =-1 ; d->domain = domain;
@@ -364,11 +370,16 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, } d->device_id = v; -xen_host_pci_get_dec_value (d,"irq" , &v, errp); -if (*errp) { - goto error; + sdbf =PCI_SBDF (domain, bus, dev, func); + d->irq =xc_physdev_gsi_from_dev (xen_xc, sdbf); +/* fail to get gsi, fallback to irq */ +if (d->irq ==-1 ) { +xen_host_pci_get_dec_value (d,"irq" , &v, errp); +if (*errp) { + goto error; + } + d->irq = v; } - d->irq = v;xen_host_pci_get_hex_value (d,"class" , &v, errp);if (*errp) {
Signature · 1 line
2.34.1
Re: [RFC QEMU PATCH v7 1/1] xen/pci: get gsi for passthrough devices
+Edgar
Quoted 12 lines · Jiqian Chen
In PVH dom0, it uses the linux local interrupt mechanism, when it allocs irq for a gsi, it is dynamic, and follow the principle of applying first, distributing first. And the irq number is alloced from small to large, but the applying gsi number is not, may gsi 38 comes before gsi 28, that causes the irq number is not equal with the gsi number. And when passthrough a device, qemu wants to use gsi to map pirq,
xen_pt_realize->xc_physdev_map_pirq, but the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq in current code, so it will fail when mapping.
Quoted 4 lines
Get gsi by using new function supported by Xen tools.
Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>
I think you can safely remove the RFC tag since the Xen bits have been upstreamed.
hw/xen/xen-host-pci-device.c line 13
Quoted 13 lines
--- hw/xen/xen-host-pci-device.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)hw/xen/xen-host-pci-device.c +1
-0@@ -10,6 +10,7 @@#include "qapi/error.h" #include "qemu/cutils.h" #include "xen-host-pci-device.h" +#include "hw/xen/xen_native.h"
The inclusion order unfortunately seems to be delicate. "hw/xen/xen_native.h" should be before all the other xen includes, but after "qemu/osdep.h".
Quoted 8 lines
hw/xen/xen-host-pci-device.c +1
-0#define XEN_HOST_PCI_MAX_EXT_CAP \ ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))@@ -329,12 +330,17 @@ int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)return -1 ; } +#define PCI_SBDF (seg, bus, dev, func) \
hw/xen/xen-host-pci-device.c int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap) · line 343
Quoted 10 lines
hw/xen/xen-host-pci-device.c +4
-0+ ((((uint32_t )(seg)) <<16 ) | \ + (PCI_BUILD_BDF(bus, PCI_DEVFN(dev, func)))) +void xen_host_pci_device_get (XenHostPCIDevice *d,uint16_t domain,uint8_t bus,uint8_t dev,uint8_t func,Error **errp) {ERRP_GUARD ();unsigned int v; +uint32_t sdbf;
Typo: s/sdbf/sbdf/
hw/xen/xen-host-pci-device.c void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, · line 374
Quoted 12 lines
hw/xen/xen-host-pci-device.c +2
-3d->config_fd =-1 ; d->domain = domain;@@ -364,11 +370,16 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, } d->device_id = v; -xen_host_pci_get_dec_value (d,"irq" , &v, errp); -if (*errp) { - goto error; + sdbf =PCI_SBDF (domain, bus, dev, func); + d->irq =xc_physdev_gsi_from_dev (xen_xc, sdbf);
This was renamed to xc_pcidev_get_gsi.
This also needs some sort of Xen interface version guard for backward compatibility since it's a new call introduced in Xen 4.20.
Quoted 12 lines
hw/xen/xen-host-pci-device.c +7
-1+/* fail to get gsi, fallback to irq */ +if (d->irq ==-1 ) { +xen_host_pci_get_dec_value (d,"irq" , &v, errp); +if (*errp) { + goto error; + } + d->irq = v; } - d->irq = v;xen_host_pci_get_hex_value (d,"class" , &v, errp);if (*errp) {
Re: [RFC QEMU PATCH v7 1/1] xen/pci: get gsi for passthrough devices
Quoted 15 lines · Stewart Hildebrand
+Edgar
Quoted 12 lines · Jiqian Chen
In PVH dom0, it uses the linux local interrupt mechanism, when it allocs irq for a gsi, it is dynamic, and follow the principle of applying first, distributing first. And the irq number is alloced from small to large, but the applying gsi number is not, may gsi 38 comes before gsi 28, that causes the irq number is not equal with the gsi number. And when passthrough a device, qemu wants to use gsi to map pirq,
xen_pt_realize->xc_physdev_map_pirq, but the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq in current code, so it will fail when mapping.
Quoted 7 lines
Quoted 4 lines
Get gsi by using new function supported by Xen tools.
Signed-off-by Huang Rui <[email protected]>Signed-off-by Jiqian Chen <[email protected]>I think you can safely remove the RFC tag since the Xen bits have been upstreamed.
Thank you! I will send a new version later this week and modify the patch according to your comments.
Quoted 71 lines
Quoted 10 lines
--- hw/xen/xen-host-pci-device.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)hw/xen/xen-host-pci-device.c +0
-0hw/xen/xen-host-pci-device.c line 13
Quoted 4 lines
hw/xen/xen-host-pci-device.c +1
-0@@ -10,6 +10,7 @@#include "qapi/error.h" #include "qemu/cutils.h" #include "xen-host-pci-device.h" +#include "hw/xen/xen_native.h" The inclusion order unfortunately seems to be delicate. "
hw/xen/xen_native.h" should be before all the other xen includes, but after "qemu/osdep.h".Quoted 8 lines
hw/xen/xen-host-pci-device.c +1
-0#define XEN_HOST_PCI_MAX_EXT_CAP \ ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))@@ -329,12 +330,17 @@ int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)return -1 ; } +#define PCI_SBDF (seg, bus, dev, func) \hw/xen/xen-host-pci-device.c int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap) · line 343
Quoted 10 lines
hw/xen/xen-host-pci-device.c +4
-0+ ((((uint32_t )(seg)) <<16 ) | \ + (PCI_BUILD_BDF(bus, PCI_DEVFN(dev, func)))) +void xen_host_pci_device_get (XenHostPCIDevice *d,uint16_t domain,uint8_t bus,uint8_t dev,uint8_t func,Error **errp) {ERRP_GUARD ();unsigned int v; +uint32_t sdbf;Typo: s/sdbf/sbdf/
hw/xen/xen-host-pci-device.c void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, · line 374
Quoted 12 lines
hw/xen/xen-host-pci-device.c +2
-3d->config_fd =-1 ; d->domain = domain;@@ -364,11 +370,16 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain, } d->device_id = v; -xen_host_pci_get_dec_value (d,"irq" , &v, errp); -if (*errp) { - goto error; + sdbf =PCI_SBDF (domain, bus, dev, func); + d->irq =xc_physdev_gsi_from_dev (xen_xc, sdbf);This was renamed to
xc_pcidev_get_gsi.This also needs some sort of Xen interface version guard for backward compatibility since it's a new call introduced in Xen 4.20.
Quoted 12 lines
hw/xen/xen-host-pci-device.c +7
-1+/* fail to get gsi, fallback to irq */ +if (d->irq ==-1 ) { +xen_host_pci_get_dec_value (d,"irq" , &v, errp); +if (*errp) { + goto error; + } + d->irq = v; } - d->irq = v;xen_host_pci_get_hex_value (d,"class" , &v, errp);if (*errp) {
Signature · 2 lines
Best regards, Jiqian Chen.