Skip to content

Commit cfcacba

Browse files
Jiqian Chentperard
authored andcommitted
xen/passthrough: use gsi to map pirq when dom0 is PVH
In PVH dom0, when passthrough a device to domU, QEMU code xen_pt_realize->xc_physdev_map_pirq wants to use gsi, but in current codes the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq, that is wrong, because irq is not equal with gsi, they are in different spaces, so pirq mapping fails. To solve above problem, use new interface of Xen, xc_pcidev_get_gsi to get gsi and use xc_physdev_map_pirq_gsi to map pirq when dom0 is PVH. Signed-off-by: Jiqian Chen <[email protected]> Signed-off-by: Huang Rui <[email protected]> Signed-off-by: Jiqian Chen <[email protected]> Acked-by: Anthony PERARD <[email protected]> Reviewed-by: Stewart Hildebrand <[email protected]> Message-Id: <[email protected]> Signed-off-by: Anthony PERARD <[email protected]>
1 parent 5136598 commit cfcacba

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

hw/xen/xen_pt.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,57 @@ static void xen_pt_destroy(PCIDevice *d) {
766766
}
767767
/* init */
768768

769+
#if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 42000
770+
static bool xen_pt_need_gsi(void)
771+
{
772+
FILE *fp;
773+
int len;
774+
/*
775+
* The max length of guest_type is "PVH"+'\n'+'\0', it is 5,
776+
* so here set the length of type to be twice.
777+
*/
778+
char type[10];
779+
const char *guest_type = "/sys/hypervisor/guest_type";
780+
781+
fp = fopen(guest_type, "r");
782+
if (!fp) {
783+
error_report("Cannot open %s: %s", guest_type, strerror(errno));
784+
return false;
785+
}
786+
787+
if (fgets(type, sizeof(type), fp)) {
788+
len = strlen(type);
789+
if (len) {
790+
type[len - 1] = '\0';
791+
if (!strcmp(type, "PVH")) {
792+
fclose(fp);
793+
return true;
794+
}
795+
}
796+
}
797+
798+
fclose(fp);
799+
return false;
800+
}
801+
802+
static int xen_pt_map_pirq_for_gsi(PCIDevice *d, int *pirq)
803+
{
804+
int gsi;
805+
XenPCIPassthroughState *s = XEN_PT_DEVICE(d);
806+
807+
gsi = xc_pcidev_get_gsi(xen_xc,
808+
PCI_SBDF(s->real_device.domain,
809+
s->real_device.bus,
810+
s->real_device.dev,
811+
s->real_device.func));
812+
if (gsi >= 0) {
813+
return xc_physdev_map_pirq_gsi(xen_xc, xen_domid, gsi, pirq);
814+
}
815+
816+
return gsi;
817+
}
818+
#endif
819+
769820
static void xen_pt_realize(PCIDevice *d, Error **errp)
770821
{
771822
ERRP_GUARD();
@@ -847,7 +898,16 @@ static void xen_pt_realize(PCIDevice *d, Error **errp)
847898
goto out;
848899
}
849900

901+
#if CONFIG_XEN_CTRL_INTERFACE_VERSION >= 42000
902+
if (xen_pt_need_gsi()) {
903+
rc = xen_pt_map_pirq_for_gsi(d, &pirq);
904+
} else {
905+
rc = xc_physdev_map_pirq(xen_xc, xen_domid, machine_irq, &pirq);
906+
}
907+
#else
850908
rc = xc_physdev_map_pirq(xen_xc, xen_domid, machine_irq, &pirq);
909+
#endif
910+
851911
if (rc < 0) {
852912
XEN_PT_ERR(d, "Mapping machine irq %u to pirq %i failed, (err: %d)\n",
853913
machine_irq, pirq, errno);

include/hw/pci/pci.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ extern bool pci_available;
2323
#define PCI_SLOT_MAX 32
2424
#define PCI_FUNC_MAX 8
2525

26+
#define PCI_SBDF(seg, bus, dev, func) \
27+
((((uint32_t)(seg)) << 16) | \
28+
(PCI_BUILD_BDF(bus, PCI_DEVFN(dev, func))))
29+
2630
/* Class, Vendor and Device IDs from Linux's pci_ids.h */
2731
#include "hw/pci/pci_ids.h"
2832

0 commit comments

Comments
 (0)