From patchwork Thu Nov 19 14:54:00 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 7657901 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 285F19F443 for ; Thu, 19 Nov 2015 14:57:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1BBC0204D9 for ; Thu, 19 Nov 2015 14:57:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A320A2063C for ; Thu, 19 Nov 2015 14:57:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934377AbbKSO4r (ORCPT ); Thu, 19 Nov 2015 09:56:47 -0500 Received: from mail-wm0-f47.google.com ([74.125.82.47]:36828 "EHLO mail-wm0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934284AbbKSOy3 (ORCPT ); Thu, 19 Nov 2015 09:54:29 -0500 Received: by wmww144 with SMTP id w144so241835107wmw.1 for ; Thu, 19 Nov 2015 06:54:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=O4ZIouw+VvOnyYFSgXeEe7zGew+wvrNGXAM1SQ6igb0=; b=VjVZVso0ugCdh/KAy4AOWzGr35exYHTMrdT8q5H4nsfBJ7PgUwy5u1Y+hDQ5VuAVPV WiRB9TZwybXrjIlQ147CaEKi0kjsmD0Nw7AEiw3sOfEs+aQW3FmYSPun/q5JufVZvtYj dBNYjYFGcv5mV51jweVz8Gefjqy/13osC+4+r0BgrmSh0erxMsV1KDGY67LUvBMt7EQV FE3JrdMZY8gsejfwvEIL08W8y+ZZyKDb8M8VSVgNy4xjeVCqPehOrwVmzBaZpMb/peDu 37O6ufVxdSohViW97VZqkKd4ACpd4C+dDwMMju/ftaCW8D7CI/Qgv3LQJflTbgMCVkLM MUgQ== 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=O4ZIouw+VvOnyYFSgXeEe7zGew+wvrNGXAM1SQ6igb0=; b=U9x3+DNb0Fr3XZiGH72F7LBQhFBFPUJPvsrPB/QgX+w+JV+cC4NtVAi3iOlU/3dfRT rB7B9RP2v/lDX7dyh+JG525Z2pE4sAzx62QQ12zNm/8Cxpq7yNflewqV8aLWIoNQlPZQ TAZf7Zj6ZIFLofVBHpBRS6OnHDay3RBTHOE0hcLMUImhZMh4azXQfbs2L1pCH12z4IUk wTIvhuhnSHAVp124wBMiaeERLvQdIZsENEH/sbOD6vIHvgeNr3GZVTCNsQ36Y31oiDPB Mvmrq069/IIt98wD5Z1bRny2PR8m8xO9rpWm/lu7kI9iydiRzxtltDtdyXdOgN2aLxTi p0Hg== X-Gm-Message-State: ALoCoQkLLInelChvG8qIxIawVf28hlapH3K3M1rYUbH8nyj7bpCmEh+OCeUDblpp/bo0M4Dd/dZE X-Received: by 10.194.94.100 with SMTP id db4mr8807866wjb.110.1447944867658; Thu, 19 Nov 2015 06:54:27 -0800 (PST) Received: from new-host-3.home (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id h67sm34493865wmf.17.2015.11.19.06.54.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 19 Nov 2015 06:54:26 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, alex.williamson@redhat.com, b.reynal@virtualopensystems.com, christoffer.dall@linaro.org, marc.zyngier@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Cc: andre.przywara@arm.com, linux-kernel@vger.kernel.org, patches@linaro.org Subject: [PATCH v4 10/13] KVM: arm/arm64: vgic: forwarding control Date: Thu, 19 Nov 2015 14:54:00 +0000 Message-Id: <1447944843-17731-11-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1447944843-17731-1-git-send-email-eric.auger@linaro.org> References: <1447944843-17731-1-git-send-email-eric.auger@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,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 Implements kvm_vgic_[set|unset]_forward. Handle low-level VGIC programming: physical IRQ/guest IRQ mapping, list register cleanup, VGIC state machine. Also interacts with the irqchip. Signed-off-by: Eric Auger --- v3 -> v4: - use the target vCPU in set/unset_forward - rebase after removal of vgic_irq_lr_map - clarify set/unset_forward comments, - host_irq renamed into irq and virt_irq (SPI ID) now used in place of SPI index (former guest_irq) - replaced BUG_ON by WARN_ON - simplify unset_forward implementation by using vgic_unqueue_irqs - handle edge mapped unshared IRQs v2 -> v3: - on unforward, we do not compute & output the active state anymore. This means if the unforward happens while the physical IRQ is active, we will not VFIO mask the IRQ while deactiving it. If a new physical IRQ hits, the corresponding virtual IRQ might not be injected (hence lost) due to VGIC state machine. bypass rfc v2: - use irq_set_vcpu_affinity API - use irq_set_irqchip_state instead of chip->irq_eoi bypass rfc: - rename kvm_arch_{set|unset}_forward into kvm_vgic_{set|unset}_forward. Remove __KVM_HAVE_ARCH_HALT_GUEST. The function is bound to be called by ARM code only. v4 -> v5: - fix arm64 compilation issues, ie. also defines __KVM_HAVE_ARCH_HALT_GUEST for arm64 v3 -> v4: - code originally located in kvm_vfio_arm.c - kvm_arch_vfio_{set|unset}_forward renamed into kvm_arch_{set|unset}_forward - split into 2 functions (set/unset) since unset does not fail anymore - unset can be invoked at whatever time. Extra care is taken to handle transition in VGIC state machine, LR cleanup, ... 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 --- include/kvm/arm_vgic.h | 5 +++ virt/kvm/arm/vgic.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h index 9bf6a30..8090ab4 100644 --- a/include/kvm/arm_vgic.h +++ b/include/kvm/arm_vgic.h @@ -368,4 +368,9 @@ static inline int vgic_v3_probe(struct device_node *vgic_node, } #endif +int kvm_vgic_set_forward(struct kvm *kvm, unsigned int irq, + unsigned int virt_irq); +void kvm_vgic_unset_forward(struct kvm *kvm, unsigned int irq, + unsigned int virt_irq); + #endif diff --git a/virt/kvm/arm/vgic.c b/virt/kvm/arm/vgic.c index e96f79e..bd500b4 100644 --- a/virt/kvm/arm/vgic.c +++ b/virt/kvm/arm/vgic.c @@ -2500,3 +2500,121 @@ int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, { return 0; } + +/** + * kvm_vgic_set_forward - Set IRQ forwarding + * + * @kvm: handle to the VM + * @irq: the host linux IRQ + * @virt_irq: the guest SPI ID + * + * This function is supposed to be called only if the IRQ + * is not in progress: ie. not active at GIC level and not + * currently under injection in the guest. The physical IRQ must + * also be disabled and all vCPUs must have been exited and + * prevented from being re-entered. + */ +int kvm_vgic_set_forward(struct kvm *kvm, unsigned int irq, + unsigned int virt_irq) +{ + struct vgic_dist *dist = &kvm->arch.vgic; + struct kvm_vcpu *vcpu; + struct irq_phys_map *map; + int cpu_id; + + kvm_debug("%s irq=%d virt_irq=%d\n", __func__, irq, virt_irq); + + cpu_id = dist->irq_spi_cpu[virt_irq - VGIC_NR_PRIVATE_IRQS]; + vcpu = kvm_get_vcpu(kvm, cpu_id); + if (!vcpu) + return 0; + /* + * let's tell the irqchip driver that after this function + * returns, a new occurrence of that physical irq will be handled + * as a forwarded IRQ, ie. the host will only perform priority + * drop but will not deactivate the physical IRQ: guest will + */ + irq_set_vcpu_affinity(irq, vcpu); + + /* + * let's program the vgic so that after this function returns + * any subsequent virt_irq injection will be considered as + * forwarded and LR will be programmed with HWbit set + */ + map = kvm_vgic_map_phys_irq(vcpu, virt_irq, irq, false); + + return !map; +} + +/** + * kvm_vgic_unset_forward - Unset IRQ forwarding + * + * @kvm: handle to the VM + * @irq: host Linux IRQ number + * @virt_irq: virtual SPI ID + * + * This function must be called when the host irq is disabled + * and all vCPUs have been exited and prevented from being re-entered. + */ +void kvm_vgic_unset_forward(struct kvm *kvm, + unsigned int irq, + unsigned int virt_irq) +{ + struct vgic_dist *dist = &kvm->arch.vgic; + struct kvm_vcpu *vcpu; + int cpu_id; + bool active, is_level; + struct irq_phys_map *map; + + kvm_debug("%s irq=%d virt_irq=%d\n", __func__, irq, virt_irq); + + spin_lock(&dist->lock); + + cpu_id = dist->irq_spi_cpu[virt_irq - VGIC_NR_PRIVATE_IRQS]; + vcpu = kvm_get_vcpu(kvm, cpu_id); + is_level = !vgic_irq_is_edge(vcpu, virt_irq); + + irq_get_irqchip_state(irq, IRQCHIP_STATE_ACTIVE, &active); + + if (!vcpu) + goto out; + + map = vgic_irq_map_search(vcpu, virt_irq); + if (!map || map->shared || kvm_vgic_unmap_phys_irq(vcpu, map)) { + WARN_ON(1); + goto out; + } + + if (!active) { + /* + * The physical IRQ is not active so no virtual IRQ + * is under injection, reset the level state which was + * not modelled. + */ + vgic_dist_irq_clear_level(vcpu, virt_irq); + goto out; + } + + vgic_unqueue_irqs(vcpu); + + if (vgic_dist_irq_is_pending(vcpu, virt_irq) || + vgic_irq_is_active(vcpu, virt_irq)) { + /* + * on next flush, LR for virt_irq will be programmed + * with maintenance IRQ. For level sensitive IRQ, + * let's set the level which was not modelled up to now + */ + if (is_level) + vgic_dist_irq_set_level(vcpu, virt_irq); + } else { + vgic_dist_irq_clear_level(vcpu, virt_irq); + } + +out: + /* Let's deactivate and unmap the physical IRQ from the vCPU */ + irq_set_irqchip_state(irq, IRQCHIP_STATE_ACTIVE, false); + irq_set_vcpu_affinity(irq, NULL); + + spin_unlock(&dist->lock); +} +