From patchwork Sun Nov 23 18:36:00 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 41394 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f69.google.com (mail-la0-f69.google.com [209.85.215.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 57B892036A for ; Sun, 23 Nov 2014 18:37:53 +0000 (UTC) Received: by mail-la0-f69.google.com with SMTP id gd6sf4604898lab.8 for ; Sun, 23 Nov 2014 10:37:52 -0800 (PST) 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=kB/0oYAEIoFLNfHJjODoeU9K3z4NQByIHH2/dFhOMSs=; b=SLlzBefN4wtMq56L5qznz08wxYsS45AapL3PL2MQ55NU13S5OyMMS0hlR6HFc4l5hm oXUf0YYBTuIBYYXRkWYc8K8hM0OAVNiKDIFIr7Yn2I6jzUM6bsrCzwmkMxoxmuSYdIAZ 1ck6k//+DK8W2VSPaWsdPtQuu42iVHn+bhzUzftywHHbnLNfpLKYICS/4l9XKpm2oOT9 DrDqKYavR6xW08S+5M5CvDD8PlhV+HwdUYU/aFl77oRpeeWOmSJuQ0r9pOMOMD6xCvJ7 sfvMgRNLcng4dAVdMtzhUpRDVDtAFRwdwlJoC6+DbuuvwC5xkvNbCO71uS+QXoGMWJD7 8uiA== X-Gm-Message-State: ALoCoQnBaT2k58pW1mMRtavKCPLj1ReKP6Q+MNrmQefhmNwjWTXtafK2QFiNXpNg/bhgwRV1waIm X-Received: by 10.152.10.233 with SMTP id l9mr5083994lab.0.1416767872220; Sun, 23 Nov 2014 10:37:52 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.228.140 with SMTP id si12ls360165lac.25.gmail; Sun, 23 Nov 2014 10:37:51 -0800 (PST) X-Received: by 10.112.180.198 with SMTP id dq6mr16219058lbc.56.1416767871920; Sun, 23 Nov 2014 10:37:51 -0800 (PST) Received: from mail-la0-f50.google.com (mail-la0-f50.google.com. [209.85.215.50]) by mx.google.com with ESMTPS id t13si12796722lal.121.2014.11.23.10.37.51 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 23 Nov 2014 10:37:51 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.50 as permitted sender) client-ip=209.85.215.50; Received: by mail-la0-f50.google.com with SMTP id pv20so6564761lab.37 for ; Sun, 23 Nov 2014 10:37:51 -0800 (PST) X-Received: by 10.112.38.4 with SMTP id c4mr785175lbk.46.1416767871826; Sun, 23 Nov 2014 10:37:51 -0800 (PST) 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.112.184.201 with SMTP id ew9csp156332lbc; Sun, 23 Nov 2014 10:37:51 -0800 (PST) X-Received: by 10.194.172.4 with SMTP id ay4mr27518478wjc.13.1416767871079; Sun, 23 Nov 2014 10:37:51 -0800 (PST) Received: from mail-wi0-f180.google.com (mail-wi0-f180.google.com. [209.85.212.180]) by mx.google.com with ESMTPS id cj1si8517197wib.103.2014.11.23.10.37.50 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 23 Nov 2014 10:37:51 -0800 (PST) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 209.85.212.180 as permitted sender) client-ip=209.85.212.180; Received: by mail-wi0-f180.google.com with SMTP id n3so3725325wiv.13 for ; Sun, 23 Nov 2014 10:37:50 -0800 (PST) X-Received: by 10.180.103.33 with SMTP id ft1mr14727454wib.71.1416767870810; Sun, 23 Nov 2014 10:37:50 -0800 (PST) Received: from gnx2579.gnb.st.com (weg38-3-78-232-41-119.fbx.proxad.net. [78.232.41.119]) by mx.google.com with ESMTPSA id hk9sm17535471wjb.46.2014.11.23.10.37.48 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 23 Nov 2014 10:37:50 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, christoffer.dall@linaro.org, marc.zyngier@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, alex.williamson@redhat.com, joel.schopp@amd.com, kim.phillips@freescale.com, paulus@samba.org, gleb@kernel.org, pbonzini@redhat.com, agraf@suse.de Cc: linux-kernel@vger.kernel.org, patches@linaro.org, will.deacon@arm.com, a.motakis@virtualopensystems.com, a.rigo@virtualopensystems.com, john.liuli@huawei.com, ming.lei@canonical.com, feng.wu@intel.com Subject: [PATCH v3 8/8] KVM: arm: kvm-vfio: forwarding control Date: Sun, 23 Nov 2014 19:36:00 +0100 Message-Id: <1416767760-14487-9-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1416767760-14487-1-git-send-email-eric.auger@linaro.org> References: <1416767760-14487-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.215.50 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 sets __KVM_HAVE_ARCH_KVM_VFIO_FORWARD and implements kvm_arch_vfio_set_forward for ARM. As a result the KVM-VFIO device now allows to forward/unforward a VFIO device IRQ on ARM. kvm_arch_vfio_set_forward programs both genirq and the VGIC to control where the physical IRQ deactivation is initiated. - forwarded case: deactivation is initiated by the guest; when it completes the virtual IRQ, the GIC automatically deactivates the physical IRQ. - not forwarded case: the physical IRQ deactivation is handled by the host Signed-off-by: Eric Auger --- v2 -> v3: - renaming of kvm_arch_set_fwd_state into kvm_arch_vfio_set_forward - takes a bool arg instead of kvm_fwd_irq_action enum - removal of KVM_VFIO_IRQ_CLEANUP - platform device check now happens here - more precise errors returned - irq_eoi handled externally to this patch (VGIC) - correct enable_irq bug done twice - reword the commit message - correct check of platform_bus_type - use raw_spin_lock_irqsave and check the validity of the handler --- arch/arm/include/asm/kvm_host.h | 2 + arch/arm/kvm/Makefile | 2 +- arch/arm/kvm/kvm_vfio_arm.c | 101 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 arch/arm/kvm/kvm_vfio_arm.c diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h index bca5b79..447f90c 100644 --- a/arch/arm/include/asm/kvm_host.h +++ b/arch/arm/include/asm/kvm_host.h @@ -27,6 +27,8 @@ #include #include +#define __KVM_HAVE_ARCH_KVM_VFIO_FORWARD + #if defined(CONFIG_KVM_ARM_MAX_VCPUS) #define KVM_MAX_VCPUS CONFIG_KVM_ARM_MAX_VCPUS #else diff --git a/arch/arm/kvm/Makefile b/arch/arm/kvm/Makefile index ea1fa76..26a5a42 100644 --- a/arch/arm/kvm/Makefile +++ b/arch/arm/kvm/Makefile @@ -19,7 +19,7 @@ kvm-arm-y = $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o $(KVM)/vf obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o handle_exit.o guest.o mmu.o emulate.o reset.o -obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o +obj-y += coproc.o coproc_a15.o coproc_a7.o mmio.o psci.o perf.o kvm_vfio_arm.o obj-$(CONFIG_KVM_ARM_VGIC) += $(KVM)/arm/vgic.o obj-$(CONFIG_KVM_ARM_VGIC) += $(KVM)/arm/vgic-v2.o obj-$(CONFIG_KVM_ARM_TIMER) += $(KVM)/arm/arch_timer.o diff --git a/arch/arm/kvm/kvm_vfio_arm.c b/arch/arm/kvm/kvm_vfio_arm.c new file mode 100644 index 0000000..af2c501 --- /dev/null +++ b/arch/arm/kvm/kvm_vfio_arm.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2014 Linaro Ltd. + * Authors: Eric Auger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * kvm_arch_vfio_set_forward - Change the forward state of an IRQ + * + * @fwd_irq: handle to the forward irq struct + * @forward: target forwarding state + * + * If forward is true, programs genirq and VGIC so that physical IRQ + * deactivation ownership is transferred to the guest (using GIC HW feature). + * When forward is false, standard behavior is restored, ie. host + * deactivates the physical IRQ. + * returns: + * -EINVAL if the vfio device is not a platform device + * -ENOENT if the irq could not be identified + * -EBUSY if physical IRQ is in progress + * -ENOENT if the VGIC has a physical/virtual IRQ mapping that is not + * consistent with the request. + */ +int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq, + bool forward) +{ + int hwirq; + int ret = -EBUSY; + struct irq_desc *desc; + struct irq_data *d; + struct platform_device *platdev; + struct device *dev = kvm_vfio_external_base_device(fwd_irq->vdev); + unsigned long flags; + /* + * We don't have to garantee the vcpu handle is non void since the + * vfio device holds a reference to the kvm struct + */ + struct kvm_vcpu *vcpu = kvm_get_vcpu(fwd_irq->kvm, 0); + + if (dev->bus == &platform_bus_type) { + platdev = to_platform_device(dev); + hwirq = platform_get_irq(platdev, fwd_irq->index); + if (hwirq < 0) + return -EINVAL; + } else + return -ENOENT; + desc = irq_to_desc(hwirq); + + /* + * if VFIO handler is already set, forwarded state cannot be + * changed anymore + */ + raw_spin_lock_irqsave(&desc->lock, flags); + if (desc->action) + goto end; + + d = &desc->irq_data; + + if (forward) { + irqd_set_irq_forwarded(d); + raw_spin_unlock_irqrestore(&desc->lock, flags); + ret = vgic_map_phys_irq(vcpu, + fwd_irq->gsi + VGIC_NR_PRIVATE_IRQS, + hwirq); + } else { + irqd_clr_irq_forwarded(d); + raw_spin_unlock_irqrestore(&desc->lock, flags); + ret = vgic_unmap_phys_irq(vcpu, + fwd_irq->gsi + + VGIC_NR_PRIVATE_IRQS, + hwirq); + } + return ret; + +end: + raw_spin_unlock_irqrestore(&desc->lock, flags); + return ret; +}