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: 32375 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f199.google.com (mail-qc0-f199.google.com [209.85.216.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 2B52B20540 for ; Mon, 23 Jun 2014 14:58:43 +0000 (UTC) Received: by mail-qc0-f199.google.com with SMTP id l6sf22282127qcy.6 for ; Mon, 23 Jun 2014 07:58:43 -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:date :message-id:in-reply-to:references:cc:subject:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=zXB3PLZNruehShQqm+d7cAd7AkzM3ypRfDpKMtxl4DE=; b=EckJy7PQWySPp3oj1KTTOR2zPQWvwipofQnyBsxxZmhuqTmXWKMB3M3xPfbR8OD/Q+ PL0NYBb72fuNQkGd9UNXs5ffV5sC1Ys4pWuCwG9eYOTFl9LPeeTNiBE/5MbpmQ4hNLTM pqDneMkwV5gZHHnMibov1johNW3JYs23INe+GWs1q0SH2bgdE1z80BKaq7QoAaP1SBkR z7HsN103O/iZRGwDg1NNfe/UkPJZ/cXokIy5Mx1nJ7jP/Vp36DNFVc1Rs29Lh/xC7Mek sDpYQggadTsRI+XrB1B45VUTIazB2es1oBpHfxdtqg1+Yat9G7uWDhGU0F2lvfip1Pdo sQug== X-Gm-Message-State: ALoCoQkvIY2EKW3KisuG9v9+tXLo46nQR9EsztiSiHLsjWbyn7PiieZh6fi/oKoLYT8IhGuxb0JR X-Received: by 10.236.134.49 with SMTP id r37mr8579833yhi.14.1403535523049; Mon, 23 Jun 2014 07:58:43 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.40.74 with SMTP id w68ls2009802qgw.53.gmail; Mon, 23 Jun 2014 07:58:42 -0700 (PDT) X-Received: by 10.58.141.168 with SMTP id rp8mr2041359veb.40.1403535522903; Mon, 23 Jun 2014 07:58:42 -0700 (PDT) Received: from mail-ve0-f178.google.com (mail-ve0-f178.google.com [209.85.128.178]) by mx.google.com with ESMTPS id l10si2239203veb.2.2014.06.23.07.58.42 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 23 Jun 2014 07:58:42 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.178 as permitted sender) client-ip=209.85.128.178; Received: by mail-ve0-f178.google.com with SMTP id oy12so6366320veb.9 for ; Mon, 23 Jun 2014 07:58:42 -0700 (PDT) X-Received: by 10.58.186.207 with SMTP id fm15mr20083242vec.4.1403535522818; Mon, 23 Jun 2014 07:58:42 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp136793vcb; Mon, 23 Jun 2014 07:58:42 -0700 (PDT) X-Received: by 10.224.44.6 with SMTP id y6mr32962388qae.47.1403535520778; Mon, 23 Jun 2014 07:58:40 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id z2si23066295qai.56.2014.06.23.07.58.40 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 23 Jun 2014 07:58:40 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Received: from localhost ([::1]:54014 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wz5hg-0005vQ-Ev for patch@linaro.org; Mon, 23 Jun 2014 10:58:40 -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-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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: a.rigo@virtualopensystems.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.128.178 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 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(), };