hw/vfio/platform: add irqfd support

This patch aims at optimizing IRQ handling using irqfd framework.

Instead of handling the eventfds on user-side they are handled on
kernel side using
- the KVM irqfd framework,
- the VFIO driver virqfd framework.

the virtual IRQ completion is trapped at interrupt controller
This removes the need for fast/slow path swap.

Overall this brings significant performance improvements.

Signed-off-by: Alvise Rigo <a.rigo@virtualopensystems.com>
Signed-off-by: Eric Auger <eric.auger@linaro.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Tested-by: Vikram Sethi <vikrams@codeaurora.org>
Acked-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
This commit is contained in:
Eric Auger 2015-07-06 12:15:14 -06:00 committed by Alex Williamson
parent 879904e863
commit fb5f816499
3 changed files with 103 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#include "hw/sysbus.h" #include "hw/sysbus.h"
#include "trace.h" #include "trace.h"
#include "hw/platform-bus.h" #include "hw/platform-bus.h"
#include "sysemu/kvm.h"
/* /*
* Functions used whatever the injection method * Functions used whatever the injection method
@ -51,6 +52,7 @@ static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev,
intp->pin = info.index; intp->pin = info.index;
intp->flags = info.flags; intp->flags = info.flags;
intp->state = VFIO_IRQ_INACTIVE; intp->state = VFIO_IRQ_INACTIVE;
intp->kvm_accel = false;
sysbus_init_irq(sbdev, &intp->qemuirq); sysbus_init_irq(sbdev, &intp->qemuirq);
@ -61,6 +63,13 @@ static VFIOINTp *vfio_init_intp(VFIODevice *vbasedev,
error_report("vfio: Error: trigger event_notifier_init failed "); error_report("vfio: Error: trigger event_notifier_init failed ");
return NULL; return NULL;
} }
/* Get an eventfd for resample/unmask */
ret = event_notifier_init(&intp->unmask, 0);
if (ret) {
g_free(intp);
error_report("vfio: Error: resamplefd event_notifier_init failed");
return NULL;
}
QLIST_INSERT_HEAD(&vdev->intp_list, intp, next); QLIST_INSERT_HEAD(&vdev->intp_list, intp, next);
return intp; return intp;
@ -315,6 +324,94 @@ static int vfio_start_eventfd_injection(VFIOINTp *intp)
return ret; return ret;
} }
/*
* Functions used for irqfd
*/
/**
* vfio_set_resample_eventfd - sets the resamplefd for an IRQ
* @intp: the IRQ struct handle
* programs the VFIO driver to unmask this IRQ when the
* intp->unmask eventfd is triggered
*/
static int vfio_set_resample_eventfd(VFIOINTp *intp)
{
VFIODevice *vbasedev = &intp->vdev->vbasedev;
struct vfio_irq_set *irq_set;
int argsz, ret;
int32_t *pfd;
argsz = sizeof(*irq_set) + sizeof(*pfd);
irq_set = g_malloc0(argsz);
irq_set->argsz = argsz;
irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_UNMASK;
irq_set->index = intp->pin;
irq_set->start = 0;
irq_set->count = 1;
pfd = (int32_t *)&irq_set->data;
*pfd = event_notifier_get_fd(&intp->unmask);
qemu_set_fd_handler(*pfd, NULL, NULL, NULL);
ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);
g_free(irq_set);
if (ret < 0) {
error_report("vfio: Failed to set resample eventfd: %m");
}
return ret;
}
static void vfio_start_irqfd_injection(SysBusDevice *sbdev, qemu_irq irq)
{
VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
VFIOINTp *intp;
if (!kvm_irqfds_enabled() || !kvm_resamplefds_enabled() ||
!vdev->irqfd_allowed) {
return;
}
QLIST_FOREACH(intp, &vdev->intp_list, next) {
if (intp->qemuirq == irq) {
break;
}
}
assert(intp);
/* Get to a known interrupt state */
qemu_set_fd_handler(event_notifier_get_fd(&intp->interrupt),
NULL, NULL, vdev);
vfio_mask_single_irqindex(&vdev->vbasedev, intp->pin);
qemu_set_irq(intp->qemuirq, 0);
if (kvm_irqchip_add_irqfd_notifier(kvm_state, &intp->interrupt,
&intp->unmask, irq) < 0) {
goto fail_irqfd;
}
if (vfio_set_trigger_eventfd(intp, NULL) < 0) {
goto fail_vfio;
}
if (vfio_set_resample_eventfd(intp) < 0) {
goto fail_vfio;
}
/* Let's resume injection with irqfd setup */
vfio_unmask_single_irqindex(&vdev->vbasedev, intp->pin);
intp->kvm_accel = true;
trace_vfio_platform_start_irqfd_injection(intp->pin,
event_notifier_get_fd(&intp->interrupt),
event_notifier_get_fd(&intp->unmask));
return;
fail_vfio:
kvm_irqchip_remove_irqfd_notifier(kvm_state, &intp->interrupt, irq);
fail_irqfd:
vfio_start_eventfd_injection(intp);
vfio_unmask_single_irqindex(&vdev->vbasedev, intp->pin);
return;
}
/* VFIO skeleton */ /* VFIO skeleton */
static void vfio_platform_compute_needs_reset(VFIODevice *vbasedev) static void vfio_platform_compute_needs_reset(VFIODevice *vbasedev)
@ -584,17 +681,20 @@ static Property vfio_platform_dev_properties[] = {
DEFINE_PROP_BOOL("x-mmap", VFIOPlatformDevice, vbasedev.allow_mmap, true), DEFINE_PROP_BOOL("x-mmap", VFIOPlatformDevice, vbasedev.allow_mmap, true),
DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice, DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice,
mmap_timeout, 1100), mmap_timeout, 1100),
DEFINE_PROP_BOOL("x-irqfd", VFIOPlatformDevice, irqfd_allowed, true),
DEFINE_PROP_END_OF_LIST(), DEFINE_PROP_END_OF_LIST(),
}; };
static void vfio_platform_class_init(ObjectClass *klass, void *data) static void vfio_platform_class_init(ObjectClass *klass, void *data)
{ {
DeviceClass *dc = DEVICE_CLASS(klass); DeviceClass *dc = DEVICE_CLASS(klass);
SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
dc->realize = vfio_platform_realize; dc->realize = vfio_platform_realize;
dc->props = vfio_platform_dev_properties; dc->props = vfio_platform_dev_properties;
dc->vmsd = &vfio_platform_vmstate; dc->vmsd = &vfio_platform_vmstate;
dc->desc = "VFIO-based platform device assignment"; dc->desc = "VFIO-based platform device assignment";
sbc->connect_irq_notifier = vfio_start_irqfd_injection;
set_bit(DEVICE_CATEGORY_MISC, dc->categories); set_bit(DEVICE_CATEGORY_MISC, dc->categories);
} }

View File

@ -41,6 +41,7 @@ typedef struct VFIOINTp {
int state; /* inactive, pending, active */ int state; /* inactive, pending, active */
uint8_t pin; /* index */ uint8_t pin; /* index */
uint32_t flags; /* IRQ info flags */ uint32_t flags; /* IRQ info flags */
bool kvm_accel; /* set when QEMU bypass through KVM enabled */
} VFIOINTp; } VFIOINTp;
/* function type for user side eventfd handler */ /* function type for user side eventfd handler */
@ -57,6 +58,7 @@ typedef struct VFIOPlatformDevice {
uint32_t mmap_timeout; /* delay to re-enable mmaps after interrupt */ uint32_t mmap_timeout; /* delay to re-enable mmaps after interrupt */
QEMUTimer *mmap_timer; /* allows fast-path resume after IRQ hit */ QEMUTimer *mmap_timer; /* allows fast-path resume after IRQ hit */
QemuMutex intp_mutex; /* protect the intp_list IRQ state */ QemuMutex intp_mutex; /* protect the intp_list IRQ state */
bool irqfd_allowed; /* debug option to force irqfd on/off */
} VFIOPlatformDevice; } VFIOPlatformDevice;
typedef struct VFIOPlatformDeviceClass { typedef struct VFIOPlatformDeviceClass {

View File

@ -1594,6 +1594,7 @@ vfio_platform_intp_interrupt(int pin, int fd) "Inject IRQ #%d (fd = %d)"
vfio_platform_intp_inject_pending_lockheld(int pin, int fd) "Inject pending IRQ #%d (fd = %d)" vfio_platform_intp_inject_pending_lockheld(int pin, int fd) "Inject pending IRQ #%d (fd = %d)"
vfio_platform_populate_interrupts(int pin, int count, int flags) "- IRQ index %d: count %d, flags=0x%x" vfio_platform_populate_interrupts(int pin, int count, int flags) "- IRQ index %d: count %d, flags=0x%x"
vfio_intp_interrupt_set_pending(int index) "irq %d is set PENDING" vfio_intp_interrupt_set_pending(int index) "irq %d is set PENDING"
vfio_platform_start_irqfd_injection(int index, int fd, int resamplefd) "IRQ index=%d, fd = %d, resamplefd = %d"
#hw/acpi/memory_hotplug.c #hw/acpi/memory_hotplug.c
mhp_acpi_invalid_slot_selected(uint32_t slot) "0x%"PRIx32 mhp_acpi_invalid_slot_selected(uint32_t slot) "0x%"PRIx32