From patchwork Thu Dec 7 16:14:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 121023 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp8556530qgn; Thu, 7 Dec 2017 08:16:52 -0800 (PST) X-Google-Smtp-Source: AGs4zMb1YoMVRIL1GtZsxI7YglOj75S0ZuPhYgkoXMwj57UI4Jar1CwNkXtoB2F0y8T9vtZCuAGT X-Received: by 10.36.26.198 with SMTP id 189mr1979477iti.38.1512663412698; Thu, 07 Dec 2017 08:16:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512663412; cv=none; d=google.com; s=arc-20160816; b=cHrUm7zFrY4xkgsEGLfmtYHHfBhrxhUFrJa16yvNJImaObl8kJOrXKIf+SULRtnthm 4zfaWCCNvMLzql+Wtza+jKxFa4WaTeit026tPhmx5mAKACqhR3hicQ1Ker89VD/WUkBR YQlk6RTGMQxT0eREpxgNPCPqx0KoQEyC/W6qxxcKLHnLd4I8L4QAJjgIAPyOIu96QqAl rJRrcvha5ftsS4WVEfuKkLf4GfeDh9HwfMlbaIC/4W0y3xIiZ0qGzbNB54HbZYOYKgG5 5yTDdyO8FpaEdd6Fd67tEt+AdeTNOFQtyCh7NFF9LEyxy8zDZcxCqzJc/bchAfkQYjzp ZN7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-unsubscribe:list-id :precedence:subject:cc:references:in-reply-to:message-id:date:to :from:dkim-signature:arc-authentication-results; bh=Tvh4Jte8zfvfNaCFKutOL/B8Mu+cqxDwfglVzFKxnPQ=; b=W4R9NkmEXY/S+C1zhA5kDjyFTR2ZQS/A2KQ1hlyIEbrqoESZ7pWK0X6gikG2wRNadN NapXq6t9rLHTbZe2Dvs+YYnjhIrSx+Wty2/VtEPUQMt8DFhVBBF0CwESkLUXlrf4Ezjl OJUhtBSYQLXlbCaXF/bCrI40s446IppMmYtf9mG9VUNshE0BA8m0bbEn/+L4s2IDuBMK 79hk58sUNthT3YynABIqqeb73KMoKZKBlnyoJ1LWmmfBoOCFvd2nt+IMRlNyFuOyqdBA clT1VKStSOAaKlwzBiDI8ckXuZzT74WShXTixYD6uG54BZCaUHQuU3WzTRoiDJ9/CjM3 XihQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=VDex+FaV; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org. [192.237.175.120]) by mx.google.com with ESMTPS id i81si3571236iof.207.2017.12.07.08.16.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Dec 2017 08:16:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=VDex+FaV; spf=pass (google.com: best guess record for domain of xen-devel-bounces@lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMyoa-0002Dp-Ew; Thu, 07 Dec 2017 16:14:24 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMyoY-0002C3-KV for xen-devel@lists.xenproject.org; Thu, 07 Dec 2017 16:14:22 +0000 X-Inumbo-ID: a044617d-db69-11e7-b4a6-bc764e045a96 Received: from mail-wm0-x22c.google.com (unknown [2a00:1450:400c:c09::22c]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id a044617d-db69-11e7-b4a6-bc764e045a96; Thu, 07 Dec 2017 17:13:57 +0100 (CET) Received: by mail-wm0-x22c.google.com with SMTP id f206so13776252wmf.5 for ; Thu, 07 Dec 2017 08:14:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=aNQ/NGflYMQjVPQ+7m/4QSWZnxVe8Yf8LFFblsTNuSU=; b=VDex+FaVoBPvB9FSbQQrgs7Z84wUlGS8O2HMYgXYwQFACxws76batNANIPcx97iHph hlo/iXtQ09dRFKl3udNfPcm9as8pFWv3uN6B0kfKzs+NE3739YQeVZWPhUhzXut7RyVU XVDo4t+qALCuwu0cH9LORZiElBRNley3SNrIw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=aNQ/NGflYMQjVPQ+7m/4QSWZnxVe8Yf8LFFblsTNuSU=; b=DIoO+CDOMu95ZpE8UE0NoTJVSp/6kNQXlYJ4DPSr9cfgg52MxKgcbS6NxFeZ5rOfAl SMjBcxnU0ptR/vA0FA28GiNojyNGhxYh3q4XLM+keIZXs6eXd/wpvIPTamr7WRSRxu3e F6JlKfpCGO+/7gpME5g9qTJq8n8R/m7y6yzKqROeOVjqRkrdtHtf2gQ9KhcGJ9O91GXf Vmt8ih0Y65b3r0kotdBK19i5auAKS4eo5fkNFzI9si/vxH7HmIWCLvC2jaAMpjOc3EpX WDAFulvFjHeU4pGiADEXn/w5njjq80kn92/3TeEwKyUaSBzdnGobMIkS0/QnzK/MyZv9 Ho7g== X-Gm-Message-State: AKGB3mLIeCzezOp87g5qCzPFXjEAazrb0SOfLPoRpbOmpcJ02+bDV/G/ y6TGodnvsrro1H6r0j2y8NyJAxxNX74= X-Received: by 10.28.160.6 with SMTP id j6mr1637644wme.125.1512663259181; Thu, 07 Dec 2017 08:14:19 -0800 (PST) Received: from e104803-lin.lan (mail.andrep.de. [217.160.17.100]) by smtp.gmail.com with ESMTPSA id f18sm6090624wrg.66.2017.12.07.08.14.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 07 Dec 2017 08:14:18 -0800 (PST) From: Andre Przywara To: Stefano Stabellini , Julien Grall Date: Thu, 7 Dec 2017 16:14:10 +0000 Message-Id: <20171207161415.20380-6-andre.przywara@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171207161415.20380-1-andre.przywara@linaro.org> References: <20171207161415.20380-1-andre.przywara@linaro.org> Cc: xen-devel@lists.xenproject.org Subject: [Xen-devel] [PATCH v2 05/10] ARM: VGIC: split gic.c to observe hardware/virtual GIC separation X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Currently gic.c holds code to handle hardware IRQs as well as code to bridge VGIC requests to the GIC virtualization hardware. Despite being named gic.c, this file reaches into the VGIC and uses data structures describing virtual IRQs. To improve abstraction, move the VGIC functions into a separate file, so that gic.c does what is says on the tin. Signed-off-by: Andre Przywara Reviewed-by: Stefano Stabellini --- xen/arch/arm/Makefile | 1 + xen/arch/arm/gic-vgic.c | 413 ++++++++++++++++++++++++++++++++++++++++++++++++ xen/arch/arm/gic.c | 366 +----------------------------------------- 3 files changed, 416 insertions(+), 364 deletions(-) create mode 100644 xen/arch/arm/gic-vgic.c diff --git a/xen/arch/arm/Makefile b/xen/arch/arm/Makefile index 30a2a6500a..41d7366527 100644 --- a/xen/arch/arm/Makefile +++ b/xen/arch/arm/Makefile @@ -16,6 +16,7 @@ obj-y += domain_build.o obj-y += domctl.o obj-$(EARLY_PRINTK) += early_printk.o obj-y += gic.o +obj-y += gic-vgic.o obj-y += gic-v2.o obj-$(CONFIG_HAS_GICV3) += gic-v3.o obj-$(CONFIG_HAS_ITS) += gic-v3-its.o diff --git a/xen/arch/arm/gic-vgic.c b/xen/arch/arm/gic-vgic.c new file mode 100644 index 0000000000..971b3bfe37 --- /dev/null +++ b/xen/arch/arm/gic-vgic.c @@ -0,0 +1,413 @@ +/* + * xen/arch/arm/gic-vgic.c + * + * ARM Generic Interrupt Controller virtualization support + * + * Tim Deegan + * Copyright (c) 2011 Citrix Systems. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 +#include +#include +#include +#include +#include + +extern uint64_t per_cpu__lr_mask; +extern const struct gic_hw_operations *gic_hw_ops; + +#define lr_all_full() (this_cpu(lr_mask) == ((1 << gic_hw_ops->info->nr_lrs) - 1)) + +#undef GIC_DEBUG + +static void gic_update_one_lr(struct vcpu *v, int i); + +static inline void gic_set_lr(int lr, struct pending_irq *p, + unsigned int state) +{ + ASSERT(!local_irq_is_enabled()); + + clear_bit(GIC_IRQ_GUEST_PRISTINE_LPI, &p->status); + + gic_hw_ops->update_lr(lr, p, state); + + set_bit(GIC_IRQ_GUEST_VISIBLE, &p->status); + clear_bit(GIC_IRQ_GUEST_QUEUED, &p->status); + p->lr = lr; +} + +static inline void gic_add_to_lr_pending(struct vcpu *v, struct pending_irq *n) +{ + struct pending_irq *iter; + + ASSERT(spin_is_locked(&v->arch.vgic.lock)); + + if ( !list_empty(&n->lr_queue) ) + return; + + list_for_each_entry ( iter, &v->arch.vgic.lr_pending, lr_queue ) + { + if ( iter->priority > n->priority ) + { + list_add_tail(&n->lr_queue, &iter->lr_queue); + return; + } + } + list_add_tail(&n->lr_queue, &v->arch.vgic.lr_pending); +} + +void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p) +{ + ASSERT(spin_is_locked(&v->arch.vgic.lock)); + + list_del_init(&p->lr_queue); +} + +void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq) +{ + struct pending_irq *n = irq_to_pending(v, virtual_irq); + + /* If an LPI has been removed meanwhile, there is nothing left to raise. */ + if ( unlikely(!n) ) + return; + + ASSERT(spin_is_locked(&v->arch.vgic.lock)); + + /* Don't try to update the LR if the interrupt is disabled */ + if ( !test_bit(GIC_IRQ_GUEST_ENABLED, &n->status) ) + return; + + if ( list_empty(&n->lr_queue) ) + { + if ( v == current ) + gic_update_one_lr(v, n->lr); + } +#ifdef GIC_DEBUG + else + gdprintk(XENLOG_DEBUG, "trying to inject irq=%u into d%dv%d, when it is still lr_pending\n", + virtual_irq, v->domain->domain_id, v->vcpu_id); +#endif +} + +/* + * Find an unused LR to insert an IRQ into, starting with the LR given + * by @lr. If this new interrupt is a PRISTINE LPI, scan the other LRs to + * avoid inserting the same IRQ twice. This situation can occur when an + * event gets discarded while the LPI is in an LR, and a new LPI with the + * same number gets mapped quickly afterwards. + */ +static unsigned int gic_find_unused_lr(struct vcpu *v, + struct pending_irq *p, + unsigned int lr) +{ + unsigned int nr_lrs = gic_hw_ops->info->nr_lrs; + unsigned long *lr_mask = (unsigned long *) &this_cpu(lr_mask); + struct gic_lr lr_val; + + ASSERT(spin_is_locked(&v->arch.vgic.lock)); + + if ( unlikely(test_bit(GIC_IRQ_GUEST_PRISTINE_LPI, &p->status)) ) + { + unsigned int used_lr; + + for_each_set_bit(used_lr, lr_mask, nr_lrs) + { + gic_hw_ops->read_lr(used_lr, &lr_val); + if ( lr_val.virq == p->irq ) + return used_lr; + } + } + + lr = find_next_zero_bit(lr_mask, nr_lrs, lr); + + return lr; +} + +void gic_raise_guest_irq(struct vcpu *v, unsigned int virtual_irq, + unsigned int priority) +{ + int i; + unsigned int nr_lrs = gic_hw_ops->info->nr_lrs; + struct pending_irq *p = irq_to_pending(v, virtual_irq); + + ASSERT(spin_is_locked(&v->arch.vgic.lock)); + + if ( unlikely(!p) ) + /* An unmapped LPI does not need to be raised. */ + return; + + if ( v == current && list_empty(&v->arch.vgic.lr_pending) ) + { + i = gic_find_unused_lr(v, p, 0); + + if (i < nr_lrs) { + set_bit(i, &this_cpu(lr_mask)); + gic_set_lr(i, p, GICH_LR_PENDING); + return; + } + } + + gic_add_to_lr_pending(v, p); +} + +static void gic_update_one_lr(struct vcpu *v, int i) +{ + struct pending_irq *p; + int irq; + struct gic_lr lr_val; + + ASSERT(spin_is_locked(&v->arch.vgic.lock)); + ASSERT(!local_irq_is_enabled()); + + gic_hw_ops->read_lr(i, &lr_val); + irq = lr_val.virq; + p = irq_to_pending(v, irq); + /* + * An LPI might have been unmapped, in which case we just clean up here. + * If that LPI is marked as PRISTINE, the information in the LR is bogus, + * as it belongs to a previous, already unmapped LPI. So we discard it + * here as well. + */ + if ( unlikely(!p || + test_and_clear_bit(GIC_IRQ_GUEST_PRISTINE_LPI, &p->status)) ) + { + ASSERT(is_lpi(irq)); + + gic_hw_ops->clear_lr(i); + clear_bit(i, &this_cpu(lr_mask)); + + return; + } + + if ( lr_val.state & GICH_LR_ACTIVE ) + { + set_bit(GIC_IRQ_GUEST_ACTIVE, &p->status); + if ( test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) && + test_and_clear_bit(GIC_IRQ_GUEST_QUEUED, &p->status) ) + { + if ( p->desc == NULL ) + { + lr_val.state |= GICH_LR_PENDING; + gic_hw_ops->write_lr(i, &lr_val); + } + else + gdprintk(XENLOG_WARNING, "unable to inject hw irq=%d into d%dv%d: already active in LR%d\n", + irq, v->domain->domain_id, v->vcpu_id, i); + } + } + else if ( lr_val.state & GICH_LR_PENDING ) + { + int q __attribute__ ((unused)) = test_and_clear_bit(GIC_IRQ_GUEST_QUEUED, &p->status); +#ifdef GIC_DEBUG + if ( q ) + gdprintk(XENLOG_DEBUG, "trying to inject irq=%d into d%dv%d, when it is already pending in LR%d\n", + irq, v->domain->domain_id, v->vcpu_id, i); +#endif + } + else + { + gic_hw_ops->clear_lr(i); + clear_bit(i, &this_cpu(lr_mask)); + + if ( p->desc != NULL ) + clear_bit(_IRQ_INPROGRESS, &p->desc->status); + clear_bit(GIC_IRQ_GUEST_VISIBLE, &p->status); + clear_bit(GIC_IRQ_GUEST_ACTIVE, &p->status); + p->lr = GIC_INVALID_LR; + if ( test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) && + test_bit(GIC_IRQ_GUEST_QUEUED, &p->status) && + !test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) + gic_raise_guest_irq(v, irq, p->priority); + else { + list_del_init(&p->inflight); + /* + * Remove from inflight, then change physical affinity. It + * makes sure that when a new interrupt is received on the + * next pcpu, inflight is already cleared. No concurrent + * accesses to inflight. + */ + smp_wmb(); + if ( test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) + { + struct vcpu *v_target = vgic_get_target_vcpu(v, irq); + irq_set_affinity(p->desc, cpumask_of(v_target->processor)); + clear_bit(GIC_IRQ_GUEST_MIGRATING, &p->status); + } + } + } +} + +void gic_clear_lrs(struct vcpu *v) +{ + int i = 0; + unsigned long flags; + unsigned int nr_lrs = gic_hw_ops->info->nr_lrs; + + /* The idle domain has no LRs to be cleared. Since gic_restore_state + * doesn't write any LR registers for the idle domain they could be + * non-zero. */ + if ( is_idle_vcpu(v) ) + return; + + gic_hw_ops->update_hcr_status(GICH_HCR_UIE, 0); + + spin_lock_irqsave(&v->arch.vgic.lock, flags); + + while ((i = find_next_bit((const unsigned long *) &this_cpu(lr_mask), + nr_lrs, i)) < nr_lrs ) { + gic_update_one_lr(v, i); + i++; + } + + spin_unlock_irqrestore(&v->arch.vgic.lock, flags); +} + +static void gic_restore_pending_irqs(struct vcpu *v) +{ + int lr = 0; + struct pending_irq *p, *t, *p_r; + struct list_head *inflight_r; + unsigned long flags; + unsigned int nr_lrs = gic_hw_ops->info->nr_lrs; + int lrs = nr_lrs; + + spin_lock_irqsave(&v->arch.vgic.lock, flags); + + if ( list_empty(&v->arch.vgic.lr_pending) ) + goto out; + + inflight_r = &v->arch.vgic.inflight_irqs; + list_for_each_entry_safe ( p, t, &v->arch.vgic.lr_pending, lr_queue ) + { + lr = gic_find_unused_lr(v, p, lr); + if ( lr >= nr_lrs ) + { + /* No more free LRs: find a lower priority irq to evict */ + list_for_each_entry_reverse( p_r, inflight_r, inflight ) + { + if ( p_r->priority == p->priority ) + goto out; + if ( test_bit(GIC_IRQ_GUEST_VISIBLE, &p_r->status) && + !test_bit(GIC_IRQ_GUEST_ACTIVE, &p_r->status) ) + goto found; + } + /* We didn't find a victim this time, and we won't next + * time, so quit */ + goto out; + +found: + lr = p_r->lr; + p_r->lr = GIC_INVALID_LR; + set_bit(GIC_IRQ_GUEST_QUEUED, &p_r->status); + clear_bit(GIC_IRQ_GUEST_VISIBLE, &p_r->status); + gic_add_to_lr_pending(v, p_r); + inflight_r = &p_r->inflight; + } + + gic_set_lr(lr, p, GICH_LR_PENDING); + list_del_init(&p->lr_queue); + set_bit(lr, &this_cpu(lr_mask)); + + /* We can only evict nr_lrs entries */ + lrs--; + if ( lrs == 0 ) + break; + } + +out: + spin_unlock_irqrestore(&v->arch.vgic.lock, flags); +} + +void gic_clear_pending_irqs(struct vcpu *v) +{ + struct pending_irq *p, *t; + + ASSERT(spin_is_locked(&v->arch.vgic.lock)); + + v->arch.lr_mask = 0; + list_for_each_entry_safe ( p, t, &v->arch.vgic.lr_pending, lr_queue ) + gic_remove_from_lr_pending(v, p); +} + +int gic_events_need_delivery(void) +{ + struct vcpu *v = current; + struct pending_irq *p; + unsigned long flags; + const unsigned long apr = gic_hw_ops->read_apr(0); + int mask_priority; + int active_priority; + int rc = 0; + + mask_priority = gic_hw_ops->read_vmcr_priority(); + active_priority = find_next_bit(&apr, 32, 0); + + spin_lock_irqsave(&v->arch.vgic.lock, flags); + + /* TODO: We order the guest irqs by priority, but we don't change + * the priority of host irqs. */ + + /* find the first enabled non-active irq, the queue is already + * ordered by priority */ + list_for_each_entry( p, &v->arch.vgic.inflight_irqs, inflight ) + { + if ( GIC_PRI_TO_GUEST(p->priority) >= mask_priority ) + goto out; + if ( GIC_PRI_TO_GUEST(p->priority) >= active_priority ) + goto out; + if ( test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) ) + { + rc = 1; + goto out; + } + } + +out: + spin_unlock_irqrestore(&v->arch.vgic.lock, flags); + return rc; +} + +void gic_inject(struct vcpu *v) +{ + ASSERT(!local_irq_is_enabled()); + + gic_restore_pending_irqs(v); + + if ( v != current ) + return; + + if ( !list_empty(¤t->arch.vgic.lr_pending) && lr_all_full() ) + gic_hw_ops->update_hcr_status(GICH_HCR_UIE, 1); +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/arm/gic.c b/xen/arch/arm/gic.c index 1f00654ef5..04e6d66b69 100644 --- a/xen/arch/arm/gic.c +++ b/xen/arch/arm/gic.c @@ -36,15 +36,11 @@ #include #include -static DEFINE_PER_CPU(uint64_t, lr_mask); - -#define lr_all_full() (this_cpu(lr_mask) == ((1 << gic_hw_ops->info->nr_lrs) - 1)) +DEFINE_PER_CPU(uint64_t, lr_mask); #undef GIC_DEBUG -static void gic_update_one_lr(struct vcpu *v, int i); - -static const struct gic_hw_operations *gic_hw_ops; +const struct gic_hw_operations *gic_hw_ops; void register_gic_ops(const struct gic_hw_operations *ops) { @@ -366,364 +362,6 @@ void gic_disable_cpu(void) gic_hw_ops->disable_interface(); } -static inline void gic_set_lr(int lr, struct pending_irq *p, - unsigned int state) -{ - ASSERT(!local_irq_is_enabled()); - - clear_bit(GIC_IRQ_GUEST_PRISTINE_LPI, &p->status); - - gic_hw_ops->update_lr(lr, p, state); - - set_bit(GIC_IRQ_GUEST_VISIBLE, &p->status); - clear_bit(GIC_IRQ_GUEST_QUEUED, &p->status); - p->lr = lr; -} - -static inline void gic_add_to_lr_pending(struct vcpu *v, struct pending_irq *n) -{ - struct pending_irq *iter; - - ASSERT(spin_is_locked(&v->arch.vgic.lock)); - - if ( !list_empty(&n->lr_queue) ) - return; - - list_for_each_entry ( iter, &v->arch.vgic.lr_pending, lr_queue ) - { - if ( iter->priority > n->priority ) - { - list_add_tail(&n->lr_queue, &iter->lr_queue); - return; - } - } - list_add_tail(&n->lr_queue, &v->arch.vgic.lr_pending); -} - -void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p) -{ - ASSERT(spin_is_locked(&v->arch.vgic.lock)); - - list_del_init(&p->lr_queue); -} - -void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq) -{ - struct pending_irq *n = irq_to_pending(v, virtual_irq); - - /* If an LPI has been removed meanwhile, there is nothing left to raise. */ - if ( unlikely(!n) ) - return; - - ASSERT(spin_is_locked(&v->arch.vgic.lock)); - - /* Don't try to update the LR if the interrupt is disabled */ - if ( !test_bit(GIC_IRQ_GUEST_ENABLED, &n->status) ) - return; - - if ( list_empty(&n->lr_queue) ) - { - if ( v == current ) - gic_update_one_lr(v, n->lr); - } -#ifdef GIC_DEBUG - else - gdprintk(XENLOG_DEBUG, "trying to inject irq=%u into d%dv%d, when it is still lr_pending\n", - virtual_irq, v->domain->domain_id, v->vcpu_id); -#endif -} - -/* - * Find an unused LR to insert an IRQ into, starting with the LR given - * by @lr. If this new interrupt is a PRISTINE LPI, scan the other LRs to - * avoid inserting the same IRQ twice. This situation can occur when an - * event gets discarded while the LPI is in an LR, and a new LPI with the - * same number gets mapped quickly afterwards. - */ -static unsigned int gic_find_unused_lr(struct vcpu *v, - struct pending_irq *p, - unsigned int lr) -{ - unsigned int nr_lrs = gic_hw_ops->info->nr_lrs; - unsigned long *lr_mask = (unsigned long *) &this_cpu(lr_mask); - struct gic_lr lr_val; - - ASSERT(spin_is_locked(&v->arch.vgic.lock)); - - if ( unlikely(test_bit(GIC_IRQ_GUEST_PRISTINE_LPI, &p->status)) ) - { - unsigned int used_lr; - - for_each_set_bit(used_lr, lr_mask, nr_lrs) - { - gic_hw_ops->read_lr(used_lr, &lr_val); - if ( lr_val.virq == p->irq ) - return used_lr; - } - } - - lr = find_next_zero_bit(lr_mask, nr_lrs, lr); - - return lr; -} - -void gic_raise_guest_irq(struct vcpu *v, unsigned int virtual_irq, - unsigned int priority) -{ - int i; - unsigned int nr_lrs = gic_hw_ops->info->nr_lrs; - struct pending_irq *p = irq_to_pending(v, virtual_irq); - - ASSERT(spin_is_locked(&v->arch.vgic.lock)); - - if ( unlikely(!p) ) - /* An unmapped LPI does not need to be raised. */ - return; - - if ( v == current && list_empty(&v->arch.vgic.lr_pending) ) - { - i = gic_find_unused_lr(v, p, 0); - - if (i < nr_lrs) { - set_bit(i, &this_cpu(lr_mask)); - gic_set_lr(i, p, GICH_LR_PENDING); - return; - } - } - - gic_add_to_lr_pending(v, p); -} - -static void gic_update_one_lr(struct vcpu *v, int i) -{ - struct pending_irq *p; - int irq; - struct gic_lr lr_val; - - ASSERT(spin_is_locked(&v->arch.vgic.lock)); - ASSERT(!local_irq_is_enabled()); - - gic_hw_ops->read_lr(i, &lr_val); - irq = lr_val.virq; - p = irq_to_pending(v, irq); - /* - * An LPI might have been unmapped, in which case we just clean up here. - * If that LPI is marked as PRISTINE, the information in the LR is bogus, - * as it belongs to a previous, already unmapped LPI. So we discard it - * here as well. - */ - if ( unlikely(!p || - test_and_clear_bit(GIC_IRQ_GUEST_PRISTINE_LPI, &p->status)) ) - { - ASSERT(is_lpi(irq)); - - gic_hw_ops->clear_lr(i); - clear_bit(i, &this_cpu(lr_mask)); - - return; - } - - if ( lr_val.state & GICH_LR_ACTIVE ) - { - set_bit(GIC_IRQ_GUEST_ACTIVE, &p->status); - if ( test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) && - test_and_clear_bit(GIC_IRQ_GUEST_QUEUED, &p->status) ) - { - if ( p->desc == NULL ) - { - lr_val.state |= GICH_LR_PENDING; - gic_hw_ops->write_lr(i, &lr_val); - } - else - gdprintk(XENLOG_WARNING, "unable to inject hw irq=%d into d%dv%d: already active in LR%d\n", - irq, v->domain->domain_id, v->vcpu_id, i); - } - } - else if ( lr_val.state & GICH_LR_PENDING ) - { - int q __attribute__ ((unused)) = test_and_clear_bit(GIC_IRQ_GUEST_QUEUED, &p->status); -#ifdef GIC_DEBUG - if ( q ) - gdprintk(XENLOG_DEBUG, "trying to inject irq=%d into d%dv%d, when it is already pending in LR%d\n", - irq, v->domain->domain_id, v->vcpu_id, i); -#endif - } - else - { - gic_hw_ops->clear_lr(i); - clear_bit(i, &this_cpu(lr_mask)); - - if ( p->desc != NULL ) - clear_bit(_IRQ_INPROGRESS, &p->desc->status); - clear_bit(GIC_IRQ_GUEST_VISIBLE, &p->status); - clear_bit(GIC_IRQ_GUEST_ACTIVE, &p->status); - p->lr = GIC_INVALID_LR; - if ( test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) && - test_bit(GIC_IRQ_GUEST_QUEUED, &p->status) && - !test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) - gic_raise_guest_irq(v, irq, p->priority); - else { - list_del_init(&p->inflight); - /* - * Remove from inflight, then change physical affinity. It - * makes sure that when a new interrupt is received on the - * next pcpu, inflight is already cleared. No concurrent - * accesses to inflight. - */ - smp_wmb(); - if ( test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) - { - struct vcpu *v_target = vgic_get_target_vcpu(v, irq); - irq_set_affinity(p->desc, cpumask_of(v_target->processor)); - clear_bit(GIC_IRQ_GUEST_MIGRATING, &p->status); - } - } - } -} - -void gic_clear_lrs(struct vcpu *v) -{ - int i = 0; - unsigned long flags; - unsigned int nr_lrs = gic_hw_ops->info->nr_lrs; - - /* The idle domain has no LRs to be cleared. Since gic_restore_state - * doesn't write any LR registers for the idle domain they could be - * non-zero. */ - if ( is_idle_vcpu(v) ) - return; - - gic_hw_ops->update_hcr_status(GICH_HCR_UIE, false); - - spin_lock_irqsave(&v->arch.vgic.lock, flags); - - while ((i = find_next_bit((const unsigned long *) &this_cpu(lr_mask), - nr_lrs, i)) < nr_lrs ) { - gic_update_one_lr(v, i); - i++; - } - - spin_unlock_irqrestore(&v->arch.vgic.lock, flags); -} - -static void gic_restore_pending_irqs(struct vcpu *v) -{ - int lr = 0; - struct pending_irq *p, *t, *p_r; - struct list_head *inflight_r; - unsigned long flags; - unsigned int nr_lrs = gic_hw_ops->info->nr_lrs; - int lrs = nr_lrs; - - spin_lock_irqsave(&v->arch.vgic.lock, flags); - - if ( list_empty(&v->arch.vgic.lr_pending) ) - goto out; - - inflight_r = &v->arch.vgic.inflight_irqs; - list_for_each_entry_safe ( p, t, &v->arch.vgic.lr_pending, lr_queue ) - { - lr = gic_find_unused_lr(v, p, lr); - if ( lr >= nr_lrs ) - { - /* No more free LRs: find a lower priority irq to evict */ - list_for_each_entry_reverse( p_r, inflight_r, inflight ) - { - if ( p_r->priority == p->priority ) - goto out; - if ( test_bit(GIC_IRQ_GUEST_VISIBLE, &p_r->status) && - !test_bit(GIC_IRQ_GUEST_ACTIVE, &p_r->status) ) - goto found; - } - /* We didn't find a victim this time, and we won't next - * time, so quit */ - goto out; - -found: - lr = p_r->lr; - p_r->lr = GIC_INVALID_LR; - set_bit(GIC_IRQ_GUEST_QUEUED, &p_r->status); - clear_bit(GIC_IRQ_GUEST_VISIBLE, &p_r->status); - gic_add_to_lr_pending(v, p_r); - inflight_r = &p_r->inflight; - } - - gic_set_lr(lr, p, GICH_LR_PENDING); - list_del_init(&p->lr_queue); - set_bit(lr, &this_cpu(lr_mask)); - - /* We can only evict nr_lrs entries */ - lrs--; - if ( lrs == 0 ) - break; - } - -out: - spin_unlock_irqrestore(&v->arch.vgic.lock, flags); -} - -void gic_clear_pending_irqs(struct vcpu *v) -{ - struct pending_irq *p, *t; - - ASSERT(spin_is_locked(&v->arch.vgic.lock)); - - v->arch.lr_mask = 0; - list_for_each_entry_safe ( p, t, &v->arch.vgic.lr_pending, lr_queue ) - gic_remove_from_lr_pending(v, p); -} - -int gic_events_need_delivery(void) -{ - struct vcpu *v = current; - struct pending_irq *p; - unsigned long flags; - const unsigned long apr = gic_hw_ops->read_apr(0); - int mask_priority; - int active_priority; - int rc = 0; - - mask_priority = gic_hw_ops->read_vmcr_priority(); - active_priority = find_next_bit(&apr, 32, 0); - - spin_lock_irqsave(&v->arch.vgic.lock, flags); - - /* TODO: We order the guest irqs by priority, but we don't change - * the priority of host irqs. */ - - /* find the first enabled non-active irq, the queue is already - * ordered by priority */ - list_for_each_entry( p, &v->arch.vgic.inflight_irqs, inflight ) - { - if ( GIC_PRI_TO_GUEST(p->priority) >= mask_priority ) - goto out; - if ( GIC_PRI_TO_GUEST(p->priority) >= active_priority ) - goto out; - if ( test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) ) - { - rc = 1; - goto out; - } - } - -out: - spin_unlock_irqrestore(&v->arch.vgic.lock, flags); - return rc; -} - -void gic_inject(struct vcpu *v) -{ - ASSERT(!local_irq_is_enabled()); - - gic_restore_pending_irqs(v); - - if ( v != current ) - return; - - if ( !list_empty(¤t->arch.vgic.lr_pending) && lr_all_full() ) - gic_hw_ops->update_hcr_status(GICH_HCR_UIE, true); -} - static void do_sgi(struct cpu_user_regs *regs, enum gic_sgi sgi) { /* Lower the priority */