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: 7658141 Return-Path: X-Original-To: patchwork-linux-arm@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 9E1139F2EC for ; Thu, 19 Nov 2015 15:04:49 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6E8FD20668 for ; Thu, 19 Nov 2015 15:04:48 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5D8F720652 for ; Thu, 19 Nov 2015 15:04:47 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZzQjG-0002Xx-RN; Thu, 19 Nov 2015 15:02:30 +0000 Received: from mail-wm0-x22d.google.com ([2a00:1450:400c:c09::22d]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZzQbp-0001Km-9v for linux-arm-kernel@lists.infradead.org; Thu, 19 Nov 2015 14:54:56 +0000 Received: by wmec201 with SMTP id c201so121308962wme.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=RlpxDky2oR0ybXrneXLijiNr5euxJhzhmzSCo4vLt9KrMKe5jC1v9PGk5KL2c0Wpe9 Y6UfbAmj8nwcxb/aMTpy6vfA49Y1EEiNG+cjNRB/0P9iRlrCmwPn7M1e/vuIyD/h/LjY Wa1G0hAC4s2NuBLJyYnTeML/ZZmXMMcDUJQMC2DFq5758SFGgfUm/599o+w8HvNtt/Nf pBhtbUKUIwLj2Du1SzeyROPqKD052YWJ4IUEUzsfQ3F9dIZZJScygGjE0wSR9WYRVcqi qjdJZQpO05D3CYSe2nulQAGleOJSBmDKuAoO+gZUITBfS5W5nqZycx/1RWSay+5Xqd9x E1jQ== X-Gm-Message-State: ALoCoQniPNV2+T9JyOqxC3zmBfkOSDzhWPmnWapW+UcxkDYNogoDNoc/urGNj60RWYMECfdRUxTP 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 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> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151119_065449_863500_61F08400 X-CRM114-Status: GOOD ( 20.72 ) X-Spam-Score: -2.6 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: andre.przywara@arm.com, linux-kernel@vger.kernel.org, patches@linaro.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,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); +} +