From patchwork Thu Jul 2 13:17:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Auger Eric X-Patchwork-Id: 50588 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id D1C55214B3 for ; Thu, 2 Jul 2015 13:18:39 +0000 (UTC) Received: by lbbui3 with SMTP id ui3sf13318224lbb.2 for ; Thu, 02 Jul 2015 06:18:38 -0700 (PDT) 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=rpbpNvA0/tmIjxny1byHk7DDatRRIhzMBxN6b7LHvOk=; b=WVuswRk3CZmIR292Hvs3eVlOUKdQx5OqA9BmD3P1dxIuvhjJr3jH9+B0XC7iHh2usK p4uxNfuBbjJPC1Rb1o4oV8FWyl0aXAHxXvndbloIicuNQ6fzs4YVsZY20t5lIqMJIdn9 ROGM5raUCJlXPVOwZu7fzaKIHC+YTtbfqz13pfUMImtkfie5vvuG1t8pyYNb3CTxhAw/ rmteQ7lEK+panMKDI/CqdNXoRfoQuDHgUL15jWPc23yxWpfHTsrDvk1R/Ebt2d3BLKFM uugxiJGxd4jcea8sLuvXX3lwawIoupl5cB3yFeRGshG+qLkQP7uafu8obyBolDKcUoyZ Zp5w== X-Gm-Message-State: ALoCoQlAX+aXW4+za6Ated0Vx+c2CcDfEc7TxZ3bWB/quVM8xCcfTVJFN20ne6AnlxZaeOiCHXiF X-Received: by 10.152.8.9 with SMTP id n9mr17055809laa.1.1435843118514; Thu, 02 Jul 2015 06:18:38 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.9.37 with SMTP id w5ls309928laa.77.gmail; Thu, 02 Jul 2015 06:18:38 -0700 (PDT) X-Received: by 10.152.43.134 with SMTP id w6mr27721198lal.120.1435843118234; Thu, 02 Jul 2015 06:18:38 -0700 (PDT) Received: from mail-lb0-f180.google.com (mail-lb0-f180.google.com. [209.85.217.180]) by mx.google.com with ESMTPS id pl9si4444842lbb.111.2015.07.02.06.18.38 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Jul 2015 06:18:38 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.180 as permitted sender) client-ip=209.85.217.180; Received: by lbcui10 with SMTP id ui10so31715237lbc.0 for ; Thu, 02 Jul 2015 06:18:38 -0700 (PDT) X-Received: by 10.152.26.163 with SMTP id m3mr30521658lag.86.1435843118076; Thu, 02 Jul 2015 06:18:38 -0700 (PDT) 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.108.230 with SMTP id hn6csp476437lbb; Thu, 2 Jul 2015 06:18:37 -0700 (PDT) X-Received: by 10.194.176.68 with SMTP id cg4mr10989344wjc.106.1435843104338; Thu, 02 Jul 2015 06:18:24 -0700 (PDT) Received: from mail-wi0-f171.google.com (mail-wi0-f171.google.com. [209.85.212.171]) by mx.google.com with ESMTPS id eo8si9498400wib.7.2015.07.02.06.18.24 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Jul 2015 06:18:24 -0700 (PDT) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 209.85.212.171 as permitted sender) client-ip=209.85.212.171; Received: by widjy10 with SMTP id jy10so84735993wid.1 for ; Thu, 02 Jul 2015 06:18:24 -0700 (PDT) X-Received: by 10.180.96.170 with SMTP id dt10mr54573591wib.42.1435843103507; Thu, 02 Jul 2015 06:18:23 -0700 (PDT) Received: from gnx2579.home (LCaen-156-56-7-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by mx.google.com with ESMTPSA id tl3sm8157099wjc.20.2015.07.02.06.18.21 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Jul 2015 06:18:22 -0700 (PDT) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, christoffer.dall@linaro.org, marc.zyngier@arm.com, alex.williamson@redhat.com, pbonzini@redhat.com, avi.kivity@gmail.com, mtosatti@redhat.com, feng.wu@intel.com, joro@8bytes.org, b.reynal@virtualopensystems.com Cc: linux-kernel@vger.kernel.org, patches@linaro.org Subject: [RFC 14/17] KVM: arm/arm64: vgic: forwarding control Date: Thu, 2 Jul 2015 15:17:24 +0200 Message-Id: <1435843047-6327-15-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1435843047-6327-1-git-send-email-eric.auger@linaro.org> References: <1435843047-6327-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.217.180 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: , 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 --- 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 | 7 ++ virt/kvm/arm/vgic.c | 195 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h index 5d47d60..93b379f 100644 --- a/include/kvm/arm_vgic.h +++ b/include/kvm/arm_vgic.h @@ -353,6 +353,13 @@ int vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, struct irq_phys_map *map); bool vgic_get_phys_irq_active(struct irq_phys_map *map); void vgic_set_phys_irq_active(struct irq_phys_map *map, bool active); +int kvm_vgic_set_forward(struct kvm *kvm, + unsigned int host_irq, unsigned int guest_irq); + +void kvm_vgic_unset_forward(struct kvm *kvm, + unsigned int host_irq, unsigned int guest_irq, + bool *active); + #define irqchip_in_kernel(k) (!!((k)->arch.vgic.in_kernel)) #define vgic_initialized(k) (!!((k)->arch.vgic.nr_cpus)) #define vgic_ready(k) ((k)->arch.vgic.ready) diff --git a/virt/kvm/arm/vgic.c b/virt/kvm/arm/vgic.c index eef35d9..9efc839 100644 --- a/virt/kvm/arm/vgic.c +++ b/virt/kvm/arm/vgic.c @@ -2402,3 +2402,198 @@ 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 + * @host_irq: physical IRQ number + * @guest_irq: virtual IRQ number + * + * 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 KVM. The physical IRQ must + * also be disabled and the guest must have been exited and + * prevented from being re-entered. + */ +int kvm_vgic_set_forward(struct kvm *kvm, + unsigned int host_irq, + unsigned int guest_irq) +{ + struct irq_desc *desc = irq_to_desc(host_irq); + struct irq_phys_map *map = NULL; + struct irq_data *d; + unsigned long flags; + struct kvm_vcpu *vcpu = kvm_get_vcpu(kvm, 0); + int spi_id = guest_irq + VGIC_NR_PRIVATE_IRQS; + struct vgic_dist *dist = &kvm->arch.vgic; + + kvm_debug("%s host_irq=%d guest_irq=%d\n", + __func__, host_irq, guest_irq); + + if (!vcpu) + return 0; + + spin_lock(&dist->lock); + + raw_spin_lock_irqsave(&desc->lock, flags); + d = &desc->irq_data; + irqd_set_irq_forwarded(d); + /* + * next physical IRQ will be be handled as forwarded + * by the host (priority drop only) + */ + + raw_spin_unlock_irqrestore(&desc->lock, flags); + + /* + * need to release the dist spin_lock here since + * vgic_map_phys_irq can sleep + */ + spin_unlock(&dist->lock); + map = vgic_map_phys_irq(vcpu, spi_id, host_irq, false); + /* + * next guest_irq injection will be considered as + * forwarded and next flush will program LR + * without maintenance IRQ but with HW bit set + */ + return !map; +} + +/** + * kvm_vgic_unset_forward - Unset IRQ forwarding + * + * @kvm: handle to the VM + * @host_irq: physical IRQ number + * @guest_irq: virtual IRQ number + * @active: returns whether the physical IRQ is active + * + * This function must be called when the host_irq is disabled + * and guest has been exited and prevented from being re-entered. + * + */ +void kvm_vgic_unset_forward(struct kvm *kvm, + unsigned int host_irq, + unsigned int guest_irq, + bool *active) +{ + struct kvm_vcpu *vcpu = kvm_get_vcpu(kvm, 0); + struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + struct vgic_dist *dist = &kvm->arch.vgic; + int ret, lr; + struct vgic_lr vlr; + struct irq_desc *desc = irq_to_desc(host_irq); + struct irq_data *d; + unsigned long flags; + int spi_id = guest_irq + VGIC_NR_PRIVATE_IRQS; + struct irq_chip *chip; + bool queued, needs_deactivate = true; + struct irq_phys_map *map; + + kvm_debug("%s host_irq=%d guest_irq=%d\n", + __func__, host_irq, guest_irq); + + spin_lock(&dist->lock); + + irq_get_irqchip_state(host_irq, IRQCHIP_STATE_ACTIVE, active); + + if (!vcpu) + goto out; + + map = vgic_irq_map_search(vcpu, spi_id); + BUG_ON(!map); + ret = vgic_unmap_phys_irq(vcpu, map); + BUG_ON(ret); + /* + * subsequent update_irq_pending or flush will handle this + * irq as forwarded + */ + + if (likely(!(*active))) { + /* + * the IRQ was not active. It may happen the handle_fasteoi_irq + * is entered afterwards due to lazy disable_irq. If this + * happens the IRQ will be deactivated and never be injected. + * let's simply prepare the states for subsequent non forwarded + * injection + */ + vgic_dist_irq_clear_level(vcpu, spi_id); + vgic_dist_irq_clear_pending(vcpu, spi_id); + vgic_irq_clear_queued(vcpu, spi_id); + needs_deactivate = false; + goto out; + } + + /* is there any list register with valid state? */ + lr = vgic_cpu->vgic_irq_lr_map[spi_id]; + queued = false; + if (lr != LR_EMPTY) { + vlr = vgic_get_lr(vcpu, lr); + if (vlr.state & LR_STATE_MASK) + queued = true; + } + + if (!queued) { + vgic_irq_clear_queued(vcpu, spi_id); + if (vgic_dist_irq_is_pending(vcpu, spi_id)) { + /* + * IRQ is injected but not yet queued. LR will be + * written with EOI_INT and process_maintenance will + * reset the states: queued, level(resampler). Pending + * will be reset on flush. + */ + vgic_dist_irq_set_level(vcpu, spi_id); + } else { + /* + * We are before the injection (update_irq_pending). + * This is the most tricky window. Due to the usage of + * disable_irq in generic unforward part (mandated by + * resamplefd unmask VFIO integration), there is a risk + * the fasteoi handler returns without calling the VFIO + * handler and deactivates the physical IRQ (lazy + * disable). Hence we cannot know whether the IRQ will + * ever be injected. The only solution found is to do as + * if the IRQ were not active. The active parameter + * typically is used by the caller to know whether + * it needs to mask * the IRQ. If not duly masked, + * another physical IRQ may hit again while the previous + * virtual IRQ is in progress. Update_irq_pending + * validate_injection will prevent this injection. + */ + vgic_dist_irq_clear_level(vcpu, spi_id); + *active = false; + } + goto out; + } + + /* + * the virtual IRQ is queued and a valid LR exists, let's patch it for + * EOI maintenance + */ + vlr.state |= LR_EOI_INT; + vgic_set_lr(vcpu, lr, vlr); + /* + * we expect a maintenance IRQ which will reset the + * queued, pending and level states + */ + vgic_dist_irq_set_level(vcpu, spi_id); + vgic_dist_irq_set_pending(vcpu, spi_id); + vgic_irq_set_queued(vcpu, spi_id); + +out: + + raw_spin_lock_irqsave(&desc->lock, flags); + d = irq_desc_get_irq_data(desc); + if (needs_deactivate) { + chip = irq_data_get_irq_chip(d); + chip->irq_eoi(d); + } + irqd_clr_irq_forwarded(d); + /* next occurrence will be deactivated by the host */ + raw_spin_unlock_irqrestore(&desc->lock, flags); + + *active = *active && vcpu; + + spin_unlock(&dist->lock); +} +