From patchwork Mon Jun 2 07:49:34 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 31260 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f72.google.com (mail-pa0-f72.google.com [209.85.220.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id E876F20AE6 for ; Mon, 2 Jun 2014 07:50:37 +0000 (UTC) Received: by mail-pa0-f72.google.com with SMTP id bj1sf4527425pad.7 for ; Mon, 02 Jun 2014 00:50:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=ljI3SqpcoqmNlka1fnh+YZTgVmJv4w0/0qApShrnuOk=; b=X3xvKq8b9ptC1KSVcB0wyNvQfblp0lPyDo8YyL8PPSrowFpuqF+ErEYVGLLDCwWueD wvPBKaVjc6KCL98o0OVJI+lXj+H0VBCz9xj9LEnN3aq0FT/gaDd7HXhOXrqaV2D3LR26 60bz7C2ucoNe6lcgHG0mN1Atq+a/kJ8jZlt011HGl4fPTu3i1eb+LjzKCS9eD+2cNUyE NUxdFeAyLMTwSPSxnlX6pw0eb9Uu+GP/pB7l0SjpzwZZQidJG1bVx+yRf3N1Z0hgAAS+ ANZerJswkimgaKwt+nnZzGFAWaDKMJiR/gNZ/kpiyKChX9dqAOIefr8xQ/o61PhVKmiC +EnA== X-Gm-Message-State: ALoCoQmoTeVXuyCE3dglI9EmzL1t2s3wzVKJA4TIGi+GIvaaeTyNJVMRt9urSzNTWOBtcHRVtD8I X-Received: by 10.66.232.38 with SMTP id tl6mr12474987pac.33.1401695437196; Mon, 02 Jun 2014 00:50:37 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.104.234 with SMTP id a97ls1813503qgf.51.gmail; Mon, 02 Jun 2014 00:50:37 -0700 (PDT) X-Received: by 10.52.14.130 with SMTP id p2mr1400731vdc.39.1401695437050; Mon, 02 Jun 2014 00:50:37 -0700 (PDT) Received: from mail-vc0-f179.google.com (mail-vc0-f179.google.com [209.85.220.179]) by mx.google.com with ESMTPS id de1si7506938vdd.33.2014.06.02.00.50.37 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 02 Jun 2014 00:50:37 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.179 as permitted sender) client-ip=209.85.220.179; Received: by mail-vc0-f179.google.com with SMTP id ij19so221008vcb.24 for ; Mon, 02 Jun 2014 00:50:37 -0700 (PDT) X-Received: by 10.58.135.136 with SMTP id ps8mr28735798veb.18.1401695436976; Mon, 02 Jun 2014 00:50:36 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.221.72 with SMTP id ib8csp78783vcb; Mon, 2 Jun 2014 00:50:36 -0700 (PDT) X-Received: by 10.194.58.79 with SMTP id o15mr47401399wjq.62.1401695436044; Mon, 02 Jun 2014 00:50:36 -0700 (PDT) Received: from mail-wi0-f170.google.com (mail-wi0-f170.google.com [209.85.212.170]) by mx.google.com with ESMTPS id dr2si20068604wid.14.2014.06.02.00.50.35 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 02 Jun 2014 00:50:36 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 209.85.212.170 as permitted sender) client-ip=209.85.212.170; Received: by mail-wi0-f170.google.com with SMTP id bs8so3786135wib.5 for ; Mon, 02 Jun 2014 00:50:35 -0700 (PDT) X-Received: by 10.194.200.65 with SMTP id jq1mr24871469wjc.33.1401695435401; Mon, 02 Jun 2014 00:50:35 -0700 (PDT) Received: from midway01-04-00.lavalab ([88.98.47.97]) by mx.google.com with ESMTPSA id je7sm30286772wic.14.2014.06.02.00.50.33 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 02 Jun 2014 00:50:34 -0700 (PDT) From: Eric Auger To: eric.auger@st.com, christoffer.dall@linaro.org, qemu-devel@nongnu.org, kim.phillips@freescale.com, a.rigo@virtualopensystems.com Cc: eric.auger@linaro.org, christophe.barnichon@st.com, kvmarm@lists.cs.columbia.edu, alex.williamson@redhat.com, agraf@suse.de, peter.maydell@linaro.org, stuart.yoder@freescale.com, a.motakis@virtualopensystems.com, patches@linaro.org Subject: [RFC v3 10/10] vfio: Add irqfd support in platform device Date: Mon, 2 Jun 2014 08:49:34 +0100 Message-Id: <1401695374-4287-11-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1401695374-4287-1-git-send-email-eric.auger@linaro.org> References: <1401695374-4287-1-git-send-email-eric.auger@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: eric.auger@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.179 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , 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 --- 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 56dde5f..3e7089a 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 @@ -424,29 +428,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) { @@ -494,6 +607,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; @@ -510,7 +642,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); @@ -537,6 +669,10 @@ static void vfio_platform_unrealize(DeviceState *dev, Error **errp) } + + + + static const VMStateDescription vfio_platform_vmstate = { .name = TYPE_VFIO_PLATFORM, .unmigratable = 1, @@ -562,6 +698,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(), };