From patchwork Mon Jun 23 14:54:03 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alvise Rigo X-Patchwork-Id: 362970 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 2659514007C for ; Tue, 24 Jun 2014 00:58:27 +1000 (EST) Received: from localhost ([::1]:54014 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wz5hR-0005vQ-Eo for incoming@patchwork.ozlabs.org; Mon, 23 Jun 2014 10:58:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52055) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wz5db-0008JM-O8 for qemu-devel@nongnu.org; Mon, 23 Jun 2014 10:54:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wz5dU-00047e-DC for qemu-devel@nongnu.org; Mon, 23 Jun 2014 10:54:27 -0400 Received: from mail-wi0-f171.google.com ([209.85.212.171]:57734) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wz5dU-00047Y-3f for qemu-devel@nongnu.org; Mon, 23 Jun 2014 10:54:20 -0400 Received: by mail-wi0-f171.google.com with SMTP id n15so4482429wiw.10 for ; Mon, 23 Jun 2014 07:54:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=zXB3PLZNruehShQqm+d7cAd7AkzM3ypRfDpKMtxl4DE=; b=NRzo5raCr2IVnMfgkxUj6Q2ZvWjvfBufGMyCP9+xdszamF7QtlFFMSmUIn29Ejqu0g 1cPSomIB7r8TbhaqWWB6CkAajRHyP5RwG7/MNMMsoymYszsfXGXAaTnrL/C/AVYy1yII aSxAyjz8N39EdMw3mSnD9PtSENuaGRMZPXaSj9WJLrz9bcm+VJ6LSY82h69/t9cbhUgD ST3HaReUYV1IVzaeI9bv+7cgFmlysl5uD3w1lQc9IKhGNTyPke3LctP9HXM+em9PLjBj k0WbdDcEBsgbrA1E1ArKzmfIyXRbXc00oSpsBeOnE0858+P5/6+3KGyo1KAfwrBrVdnT PZrQ== X-Gm-Message-State: ALoCoQnFXdX5wXyRU0Oc8niMk52RxN/F2yhiFrNrN+D3ag99Jv+nrWxBvk954jmTapYNzg3D3Ym4 X-Received: by 10.180.207.48 with SMTP id lt16mr26710671wic.32.1403535255776; Mon, 23 Jun 2014 07:54:15 -0700 (PDT) Received: from localhost.localdomain (AGrenoble-651-1-433-141.w90-52.abo.wanadoo.fr. [90.52.177.141]) by mx.google.com with ESMTPSA id di7sm17576989wjb.34.2014.06.23.07.54.14 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Jun 2014 07:54:14 -0700 (PDT) From: Alvise Rigo To: qemu-devel@nongnu.org, a.motakis@virtualopensystems.com, eric.auger@st.com Date: Mon, 23 Jun 2014 16:54:03 +0200 Message-Id: <1403535245-30041-3-git-send-email-a.rigo@virtualopensystems.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1403535245-30041-1-git-send-email-a.rigo@virtualopensystems.com> References: <1403535245-30041-1-git-send-email-a.rigo@virtualopensystems.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.212.171 Cc: Peter Maydell , Eric Auger , Alvise Rigo , Markus Armbruster , Alex Williamson , Paolo Bonzini , tech@virtualopensystems.com, =?UTF-8?q?Andreas=20F=C3=A4rber?= , Christoffer Dall Subject: [Qemu-devel] [RFC PATCH v3 2/4] vfio: Add irqfd support in platform device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Eric Auger This patch aims at optimizing IRQ handling using irqfd framework. It brings significant performance improvement over "traditional" IRQ handling introduced in : "vfio: Add initial IRQ support in platform device". This new IRQ handling method depends on kernel KVM irqfd/GSI routing capability. The IRQ handling method can be dynamically chosen (default is irqfd, if kernel supports it obviously). For example to disable irqfd handling, use: -device vfio-platform,vfio_device="fff51000.ethernet",\ compat="calxeda/hb-xgmac",mmap-timeout-ms=110,irqfd=false\ Performances are improved for the following reasons: - eventfds signalled by the VFIO platform driver are handled on kernel side by the KVM irqfd framework. - the end of interrupt(EOI) is trapped at GIC level and not at MMIO region level. As a reminder, in traditional IRQ handling QEMU assumed the first guest access to a device MMIO region after IRQ hit was the IRQ status register reset. This trap was approximate and obliged to swap to slow path after IRQ hit. A mmap timer mechanism enabled to swap back to fast path after the mmap period introducing extra complexity. Now GIC detects the completion of the virtual IRQ and signals a resampler eventfd on maintenance IRQ. The corresponding handler re-enables the physical IRQ. Next optimization step consists in attempting to remove EOI trap (ie. maintenance IRQ). This should be covered by another patch in near future. This work was tested with Calxeda Midway xgmac. Signed-off-by: Eric Auger Signed-off-by: Alvise Rigo --- hw/arm/virt.c | 14 +++++ hw/intc/arm_gic_kvm.c | 1 + hw/vfio/platform.c | 165 +++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 166 insertions(+), 14 deletions(-) diff --git a/hw/arm/virt.c b/hw/arm/virt.c index bc561b5..de1b885 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -43,6 +43,9 @@ #include "monitor/qdev.h" #include "qemu/config-file.h" +#define ENABLE_IRQFD 1 +void vfio_setup_irqfd(SysBusDevice *s, int index, int virq); + #define NUM_VIRTIO_TRANSPORTS 32 /* Number of external interrupt lines to configure the GIC with */ @@ -380,6 +383,7 @@ static int vfio_init_func(QemuOpts *opts, void *opaque) Error *errp = NULL; bool is_amba = false; int compat_str_len; + bool irqfd_allowed; if (!driver) { qerror_report(QERR_MISSING_PARAMETER, "driver"); @@ -417,6 +421,13 @@ static int vfio_init_func(QemuOpts *opts, void *opaque) error_get_pretty(errp)); exit(1); } + irqfd_allowed = object_property_get_bool(OBJECT(s), "irqfd", &errp); + if (errp != NULL) { + error_report("Couldn't retrieve irqfd flag: %s\n", + error_get_pretty(errp)); + exit(1); + } + /* * collect region info and build reg property as tuplets @@ -502,6 +513,9 @@ static int vfio_init_func(QemuOpts *opts, void *opaque) irq_attr[3*i] = cpu_to_be32(0); irq_attr[3*i+1] = cpu_to_be32(irq_start+i); irq_attr[3*i+2] = cpu_to_be32(0x4); + if (irqfd_allowed) { + vfio_setup_irqfd(s, i, irq_start+i); + } } ret = qemu_fdt_setprop(vbi->fdt, nodename, "interrupts", diff --git a/hw/intc/arm_gic_kvm.c b/hw/intc/arm_gic_kvm.c index 5038885..18a6204 100644 --- a/hw/intc/arm_gic_kvm.c +++ b/hw/intc/arm_gic_kvm.c @@ -576,6 +576,7 @@ static void kvm_arm_gic_realize(DeviceState *dev, Error **errp) KVM_DEV_ARM_VGIC_GRP_ADDR, KVM_VGIC_V2_ADDR_TYPE_CPU, s->dev_fd); + kvm_irqfds_allowed = true; } static void kvm_arm_gic_class_init(ObjectClass *klass, void *data) diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c index 377783b..d87fcc3 100644 --- a/hw/vfio/platform.c +++ b/hw/vfio/platform.c @@ -23,6 +23,7 @@ #include "hw/sysbus.h" #include "vfio-common.h" +#include "sysemu/kvm.h" typedef struct VFIOINTp { QLIST_ENTRY(VFIOINTp) next; /* entry for IRQ list */ @@ -34,6 +35,7 @@ typedef struct VFIOINTp { int state; /* inactive, pending, active */ bool kvm_accel; /* set when QEMU bypass through KVM enabled */ uint8_t pin; /* index */ + uint8_t virtualID; /* virtual IRQ */ } VFIOINTp; @@ -44,6 +46,7 @@ typedef struct VFIOPlatformDevice { /* queue of pending IRQ */ QSIMPLEQ_HEAD(pending_intp_queue, VFIOINTp) pending_intp_queue; char *compat; /* compatibility string */ + bool irqfd_allowed; } VFIOPlatformDevice; @@ -54,6 +57,7 @@ static const MemoryRegionOps vfio_region_ops = { }; static void vfio_intp_interrupt(void *opaque); +void vfio_setup_irqfd(SysBusDevice *s, int index, int virq); /* * It is mandatory to pass a VFIOPlatformDevice since VFIODevice @@ -419,29 +423,138 @@ static int vfio_platform_get_device_interrupts(VFIODevice *vdev) } -static void vfio_disable_intp(VFIODevice *vdev) +static void vfio_disable_intp(VFIOINTp *intp) { + int fd = event_notifier_get_fd(&intp->interrupt); + DPRINTF("close IRQ pin=%d fd=%d\n", intp->pin, fd); + + /* remove the IRQ handler */ + vfio_disable_irqindex(&intp->vdev->vdev, intp->pin); + intp->state = VFIO_IRQ_INACTIVE; + qemu_set_irq(intp->qemuirq, 0); + qemu_set_fd_handler(fd, NULL, NULL, NULL); + event_notifier_cleanup(&intp->interrupt); + +} + + +/* IRQFD */ + +static void resampler_handler(void *opaque) +{ + VFIOINTp *intp = (VFIOINTp *)opaque; + DPRINTF("%s index %d virtual ID = %d fd = %d\n", + __func__, + intp->pin, intp->virtualID, + event_notifier_get_fd(&intp->unmask)); + vfio_unmask_irqindex(&intp->vdev->vdev, intp->pin); +} + + +static void vfio_enable_intp_kvm(VFIOINTp *intp) +{ +#ifdef CONFIG_KVM + struct kvm_irqfd irqfd = { + .fd = event_notifier_get_fd(&intp->interrupt), + .gsi = intp->virtualID, + .flags = KVM_IRQFD_FLAG_RESAMPLE, + }; + + if (!kvm_irqfds_enabled() || + !kvm_check_extension(kvm_state, KVM_CAP_IRQFD_RESAMPLE)) { + return; + } + + /* Get to a known interrupt state */ + qemu_set_fd_handler(irqfd.fd, NULL, NULL, NULL); + intp->state = VFIO_IRQ_INACTIVE; + qemu_set_irq(intp->qemuirq, 0); + + /* Get an eventfd for resample/unmask */ + if (event_notifier_init(&intp->unmask, 0)) { + error_report("vfio: Error: event_notifier_init failed eoi"); + goto fail; + } + + /* KVM triggers it, VFIO listens for it */ + irqfd.resamplefd = event_notifier_get_fd(&intp->unmask); + qemu_set_fd_handler(irqfd.resamplefd, resampler_handler, NULL, intp); + + + if (kvm_vm_ioctl(kvm_state, KVM_IRQFD, &irqfd)) { + error_report("vfio: Error: Failed to setup resample irqfd: %m"); + goto fail_irqfd; + } + intp->kvm_accel = true; + + DPRINTF("%s irqfd pin=%d to virtID = %d fd=%d, resamplefd=%d)\n", + __func__, intp->pin, intp->virtualID, + irqfd.fd, irqfd.resamplefd); + + return; + +fail_irqfd: + event_notifier_cleanup(&intp->unmask); +fail: + qemu_set_fd_handler(irqfd.fd, vfio_intp_interrupt, NULL, intp); + vfio_unmask_irqindex(&intp->vdev->vdev, intp->pin); +#endif +} + +void vfio_setup_irqfd(SysBusDevice *s, int index, int virq) +{ + VFIOPlatformDevice *vdev = container_of(s, VFIOPlatformDevice, sbdev); VFIOINTp *intp; - VFIOPlatformDevice *vplatdev = container_of(vdev, VFIOPlatformDevice, vdev); - int fd; + QLIST_FOREACH(intp, &vdev->intp_list, next) { + if (intp->pin == index) { + intp->virtualID = virq; + vfio_enable_intp_kvm(intp); + } + } +} - QLIST_FOREACH(intp, &vplatdev->intp_list, next) { - fd = event_notifier_get_fd(&intp->interrupt); - DPRINTF("close IRQ pin=%d fd=%d\n", intp->pin, fd); +static void vfio_disable_intp_kvm(VFIOINTp *intp) +{ +#ifdef CONFIG_KVM - vfio_disable_irqindex(vdev, intp->pin); - intp->state = VFIO_IRQ_INACTIVE; - qemu_set_irq(intp->qemuirq, 0); + struct kvm_irqfd irqfd = { + .fd = event_notifier_get_fd(&intp->interrupt), + .gsi = intp->virtualID, + .flags = KVM_IRQFD_FLAG_DEASSIGN, + }; - qemu_set_fd_handler(fd, NULL, NULL, NULL); - event_notifier_cleanup(&intp->interrupt); + if (!intp->kvm_accel) { + return; } - /* restore fast path */ - vfio_mmap_set_enabled(vdev, true); + /* + * Get to a known state, hardware masked, QEMU ready to accept new + * interrupts, QEMU IRQ de-asserted. + */ + intp->state = VFIO_IRQ_INACTIVE; + /* Tell KVM to stop listening for an INTp irqfd */ + if (kvm_vm_ioctl(kvm_state, KVM_IRQFD, &irqfd)) { + error_report("vfio: Error: Failed to disable INTx irqfd: %m"); + } + + /* We only need to close the eventfd for VFIO to cleanup the kernel side */ + event_notifier_cleanup(&intp->unmask); + + /* QEMU starts listening for interrupt events. */ + qemu_set_fd_handler(irqfd.fd, vfio_intp_interrupt, NULL, intp->vdev); + + intp->kvm_accel = false; + /* If we've missed an event, let it re-fire through QEMU */ + vfio_unmask_irqindex(&intp->vdev->vdev, intp->pin); + + DPRINTF("%s: KVM INTx accel disabled\n", __func__); +#endif } + + + static bool vfio_platform_is_device_already_attached(VFIODevice *vdev, VFIOGroup *group) { @@ -489,6 +602,25 @@ static void vfio_platform_realize(DeviceState *dev, Error **errp) } } + +static void vfio_disable_all_intp(VFIODevice *vdev) +{ + VFIOINTp *intp; + VFIOPlatformDevice *vplatdev = + container_of(vdev, VFIOPlatformDevice, vdev); + + QLIST_FOREACH(intp, &vplatdev->intp_list, next) { + /* first disable IRQFD handled IRQ and turn them in QEMU handled ones */ + vfio_disable_intp_kvm(intp); + /* actually disable IRQ */ + vfio_disable_intp(intp); + } + + /* restore fast path */ + vfio_mmap_set_enabled(vdev, true); + +} + static void vfio_platform_unrealize(DeviceState *dev, Error **errp) { int i; @@ -505,7 +637,7 @@ static void vfio_platform_unrealize(DeviceState *dev, Error **errp) * timer free * g_free vdev dynamic fields */ - vfio_disable_intp(vbasedev); + vfio_disable_all_intp(vbasedev); while (!QSIMPLEQ_EMPTY(&vplatdev->pending_intp_queue)) { QSIMPLEQ_REMOVE_HEAD(&vplatdev->pending_intp_queue, pqnext); @@ -532,6 +664,10 @@ static void vfio_platform_unrealize(DeviceState *dev, Error **errp) } + + + + static const VMStateDescription vfio_platform_vmstate = { .name = TYPE_VFIO_PLATFORM, .unmigratable = 1, @@ -557,6 +693,7 @@ DEFINE_PROP_UINT32("mmap-timeout-ms", VFIOPlatformDevice, vdev.mmap_timeout, 1100), DEFINE_PROP_UINT32("num_irqs", VFIOPlatformDevice, vdev.num_irqs, 0), DEFINE_PROP_UINT32("num_regions", VFIOPlatformDevice, vdev.num_regions, 0), +DEFINE_PROP_BOOL("irqfd", VFIOPlatformDevice, irqfd_allowed, true), DEFINE_PROP_END_OF_LIST(), };