From patchwork Tue Sep 23 14:46:19 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonios Motakis X-Patchwork-Id: 4957321 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id D6E2CBEEA5 for ; Tue, 23 Sep 2014 14:52:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8F68C2024C for ; Tue, 23 Sep 2014 14:52:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3B8292013A for ; Tue, 23 Sep 2014 14:52:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932288AbaIWOtH (ORCPT ); Tue, 23 Sep 2014 10:49:07 -0400 Received: from mail-we0-f181.google.com ([74.125.82.181]:47133 "EHLO mail-we0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756165AbaIWOtF (ORCPT ); Tue, 23 Sep 2014 10:49:05 -0400 Received: by mail-we0-f181.google.com with SMTP id w61so2864269wes.40 for ; Tue, 23 Sep 2014 07:49:04 -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=7dO4PhZa/fh3yOw4iE2oZ5oGvwVOKCPAJISPEgOQtQA=; b=iZTbK0xFT1Pt29RcaOh+6/8CRGm1S64wvAQOPR26eQF1et4Zk38L4r5sg7/6Df/JOb mcNqefuvyh9Gv3rmjjVZ19WUU1JfUyRtyblr1c+EMK4mxe5UjiIahgKfK0Z18GA92zV6 Jb8qbcgmETapL82X3Z7fD4bk3OuJK/DYhqBt+EMjUeWuZlR1mnnqwPth3linPnL5GbhM R44f5AHEbqHdhOLDIAuLhS2jorKgDAUh1zTZKUqofpV9kuBHVqjSFJPJrqbOp+du4cI3 G4wZI0+MDuRY0v4GoD//oYKBz5Hq7SORMWwGzJ3z/vkHsMnA4GXlT2bqiZeqUzbA8ZOC ufLQ== X-Gm-Message-State: ALoCoQnPRFOnqH8TV/qYgg4Gn0c7NcqzvjE4ARxvHTzPdu0FF/bTGW8OBmvsfVRktIFZBm29rwki X-Received: by 10.180.83.39 with SMTP id n7mr7895828wiy.0.1411483743925; Tue, 23 Sep 2014 07:49:03 -0700 (PDT) Received: from localhost.localdomain (home.tvelocity.eu. [82.67.68.96]) by mx.google.com with ESMTPSA id ic8sm16151809wjb.25.2014.09.23.07.49.01 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 23 Sep 2014 07:49:03 -0700 (PDT) From: Antonios Motakis To: alex.williamson@redhat.com, kvmarm@lists.cs.columbia.edu, iommu@lists.linux-foundation.org Cc: tech@virtualopensystems.com, kvm@vger.kernel.org, christoffer.dall@linaro.org, will.deacon@arm.com, kim.phillips@freescale.com, eric.auger@linaro.org, marc.zyngier@arm.com, Antonios Motakis , linux-kernel@vger.kernel.org (open list) Subject: [PATCHv7 20/26] vfio/platform: trigger an interrupt via eventfd Date: Tue, 23 Sep 2014 16:46:19 +0200 Message-Id: <1411483586-29304-21-git-send-email-a.motakis@virtualopensystems.com> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1411483586-29304-1-git-send-email-a.motakis@virtualopensystems.com> References: <1411483586-29304-1-git-send-email-a.motakis@virtualopensystems.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch allows to set an eventfd for a patform device's interrupt, and also to trigger the interrupt eventfd from userspace for testing. Signed-off-by: Antonios Motakis --- drivers/vfio/platform/vfio_platform_irq.c | 89 ++++++++++++++++++++++++++- drivers/vfio/platform/vfio_platform_private.h | 2 + 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/platform/vfio_platform_irq.c b/drivers/vfio/platform/vfio_platform_irq.c index 007b386..25a7825 100644 --- a/drivers/vfio/platform/vfio_platform_irq.c +++ b/drivers/vfio/platform/vfio_platform_irq.c @@ -45,11 +45,91 @@ static int vfio_platform_set_irq_unmask(struct vfio_platform_device *vdev, return -EINVAL; } +static irqreturn_t vfio_irq_handler(int irq, void *dev_id) +{ + struct vfio_platform_irq *irq_ctx = dev_id; + + eventfd_signal(irq_ctx->trigger, 1); + + return IRQ_HANDLED; +} + +static int vfio_set_trigger(struct vfio_platform_device *vdev, + int index, int fd) +{ + struct vfio_platform_irq *irq = &vdev->irqs[index]; + struct eventfd_ctx *trigger; + int ret; + + if (irq->trigger) { + free_irq(irq->hwirq, irq); + kfree(irq->name); + eventfd_ctx_put(irq->trigger); + irq->trigger = NULL; + } + + if (fd < 0) /* Disable only */ + return 0; + + irq->name = kasprintf(GFP_KERNEL, "vfio-irq[%d](%s)", + irq->hwirq, vdev->name); + if (!irq->name) + return -ENOMEM; + + trigger = eventfd_ctx_fdget(fd); + if (IS_ERR(trigger)) { + kfree(irq->name); + return PTR_ERR(trigger); + } + + irq->trigger = trigger; + + ret = request_irq(irq->hwirq, vfio_irq_handler, 0, irq->name, irq); + if (ret) { + kfree(irq->name); + eventfd_ctx_put(trigger); + irq->trigger = NULL; + return ret; + } + + return 0; +} + static int vfio_platform_set_irq_trigger(struct vfio_platform_device *vdev, unsigned index, unsigned start, unsigned count, uint32_t flags, void *data) { - return -EINVAL; + struct vfio_platform_irq *irq = &vdev->irqs[index]; + uint8_t irq_bitmap; + int32_t fd; + + switch (flags & VFIO_IRQ_SET_DATA_TYPE_MASK) { + case VFIO_IRQ_SET_DATA_NONE: + if (count == 0) + return vfio_set_trigger(vdev, index, -1); + + vfio_irq_handler(irq->hwirq, irq); + return 0; + + case VFIO_IRQ_SET_DATA_BOOL: + if (copy_from_user(&irq_bitmap, data, sizeof(uint8_t))) + return -EFAULT; + + if (irq_bitmap == 0x1) { + vfio_irq_handler(irq->hwirq, irq); + return 0; + } + + return -EINVAL; + + case VFIO_IRQ_SET_DATA_EVENTFD: + if (copy_from_user(&fd, data, sizeof(int32_t))) + return -EFAULT; + + return vfio_set_trigger(vdev, index, fd); + } + + return -EFAULT; } int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev, @@ -95,7 +175,7 @@ int vfio_platform_irq_init(struct vfio_platform_device *vdev) if (hwirq < 0) goto err; - vdev->irqs[i].flags = 0; + vdev->irqs[i].flags = VFIO_IRQ_INFO_EVENTFD; vdev->irqs[i].count = 1; vdev->irqs[i].hwirq = hwirq; } @@ -110,6 +190,11 @@ err: void vfio_platform_irq_cleanup(struct vfio_platform_device *vdev) { + int i; + + for (i = 0; i < vdev->num_irqs; i++) + vfio_set_trigger(vdev, i, -1); + vdev->num_irqs = 0; kfree(vdev->irqs); } diff --git a/drivers/vfio/platform/vfio_platform_private.h b/drivers/vfio/platform/vfio_platform_private.h index 4201b94..765b371 100644 --- a/drivers/vfio/platform/vfio_platform_private.h +++ b/drivers/vfio/platform/vfio_platform_private.h @@ -28,6 +28,8 @@ struct vfio_platform_irq { u32 flags; u32 count; int hwirq; + char *name; + struct eventfd_ctx *trigger; }; struct vfio_platform_region {