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: Auger Eric X-Patchwork-Id: 57010 Delivered-To: patches@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp3227235lbb; Thu, 19 Nov 2015 06:54:40 -0800 (PST) X-Received: by 10.194.61.14 with SMTP id l14mr3034028wjr.58.1447944867904; Thu, 19 Nov 2015 06:54:27 -0800 (PST) Return-Path: Received: from mail-wm0-x229.google.com (mail-wm0-x229.google.com. [2a00:1450:400c:c09::229]) by mx.google.com with ESMTPS id 77si48554117wme.5.2015.11.19.06.54.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2015 06:54:27 -0800 (PST) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::229 as permitted sender) client-ip=2a00:1450:400c:c09::229; Authentication-Results: mx.google.com; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::229 as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dkim=pass header.i=@linaro-org.20150623.gappssmtp.com Received: by wmdw130 with SMTP id w130so243644680wmd.0 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=VWHrF23WkWEWk30iTY+/PZVmBk43v8ak882akvva7mY3+PvEKIqOc0nR43jdfTcMVk ReSo8Rshjij9imVB0NoWLgNqjYnIZS+mPSw+oj6ALP96d0xpcXEdSVEwh5fA60W9pgCj PwBlz3b5CUbUSrkzzvBf9ztjpuMbVT9HEzRHnwX5AaKI9HMq1HRvsOsVfju+6YWbJm4D s58f6zn5RMRH5SqGvNwlgUYYUY48OkW9gV3fTDkKcNLF8Uag7y3uslB4N26ZYVCJN3lm l9VHLy50PWHTSJ/L62NHrmADL/RrrEfxdfKiGcDLMS459TWcxp4Gu57Sy/nhU38DwOfR IWKA== X-Gm-Message-State: ALoCoQlrX9U5hHSnJ7kBOqFcfgfkDhnFasILVzMQXjYKRUUJh8PnYxMc0PQdyYC51gsb/pawr9zu X-Received: by 10.194.94.100 with SMTP id db4mr8807866wjb.110.1447944867658; Thu, 19 Nov 2015 06:54:27 -0800 (PST) Return-Path: 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> 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(+) -- 1.9.1 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); +} +