From patchwork Fri Feb 9 14:39:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andre Przywara X-Patchwork-Id: 127850 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp679967ljc; Fri, 9 Feb 2018 06:42:21 -0800 (PST) X-Google-Smtp-Source: AH8x224LXTFqyyVx5KLqkVdh/Z61DHiwEWR5r6gUwv6351ejz2vJUp4aU2w8srkQ99SUQDUl0lqM X-Received: by 10.107.151.72 with SMTP id z69mr3296309iod.185.1518187340973; Fri, 09 Feb 2018 06:42:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518187340; cv=none; d=google.com; s=arc-20160816; b=Bwyum9KvlwJe8tty7HF8iUClYgN34m5PaFZmwMXQFd/viIMcL9z+x1bwyww0wIMVIY U8fw9zU4EOPn3dti/ZpHtoe1b22ES1g0NYOwd72OzC+BKStfzZsBKhmRozox8rdkq73b jix3dA6EDQHDoTihXmwN3ZECCwSCMmafuNOkDmbxXsTvhqtXXLYnQEhGoxIQEWl5YoZP VZMaKxlkIAt//PYdiiAcmQXF8V4ep4nAz3zEGbdF4j96Db4neL6MgaPBNrpJ1peQvBbl v9Imz99iI98LSFripXB1fKg5GrJ+0Ej2MJJUwOLSBX9BkhLB5qPVu2Z1JLZU4qAxDz1V 1f9g== 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:references:in-reply-to:message-id:date:to:from :dkim-signature:arc-authentication-results; bh=5G0PWyutXot1BglpYanC2z4sXJjVW3ClFYqs0O7akQo=; b=hOpMqPbDgHW/ASeTwCnvpx4D5tdxtUVoN7ywS6yNwosoNRN/s9Ch2Y/xcv1LO4skHq 06rRl8Yn4X0peRRDTd3pQDUIgHTBLhZB1qz2ZpnhqaFFTgkUgD2mRxsgtPjfUWhSmc1j ic00IHY7fMRSPWCzO9XVq6tB69O1rZFAKCGodrDBHrKoYVeY+CmTacgm+XnxLk8m3kVN xUyKJjzEqzctG48orjwiOO9UadJwHwpCOK1rUYAuF4yWRinkFABSsbc+TRB1LaT06k7C ZCw5d3iNw9Wo5qq40/oMnUogIxc8OfxOBbcIL1Wp4qOHja2TQcIOzir07R+TMSgnXYA6 NHhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=F8TjLPhQ; 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 p74si1573545iod.339.2018.02.09.06.42.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Feb 2018 06:42:20 -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=F8TjLPhQ; 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 1ek9qa-0000uz-Py; Fri, 09 Feb 2018 14:40:16 +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 1ek9qY-0000pI-O7 for xen-devel@lists.xenproject.org; Fri, 09 Feb 2018 14:40:14 +0000 X-Inumbo-ID: 118d4bb4-0da7-11e8-ba59-bc764e045a96 Received: from mail-wm0-x241.google.com (unknown [2a00:1450:400c:c09::241]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 118d4bb4-0da7-11e8-ba59-bc764e045a96; Fri, 09 Feb 2018 15:39:44 +0100 (CET) Received: by mail-wm0-x241.google.com with SMTP id b21so16707130wme.4 for ; Fri, 09 Feb 2018 06:40:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=kvgiJkdU37LfpX+lwwUO9JxVVmySLBTLpkAgcG6Df70=; b=F8TjLPhQhdIy/kT4KLx9vAISxFzIG9bm2rHHhDLTbXzy7eFF7qcsEpgjvlL649NH8x cjcdoff+NUDequVM2LkIOY8EbkLHZXLIMTjXrpgmWGb3CQoMNSWqkvtfqnyZ9iJLHUIo Wc1ouRojplQB+kWHgwbamTVTYLDLOeZp97hqQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=kvgiJkdU37LfpX+lwwUO9JxVVmySLBTLpkAgcG6Df70=; b=HmFqfwgaJk7Z0VBSioeD8mWepH2yl1xRO/4tQi0e3Vw40J/J6lNuyz5Owu6tzG9nl1 TNzEhCFTnKkjBASY61tihXRjglimR3KBk0kntNWZ7/cUa7GFcmHrBcKp7a1HE9bz9mJ5 tRLOn0bJ20KKndcWIaAyqRcsvbDin/Jt8w1OWhqoSIlmZnQGSWxuZk78gcBsf5j8/76C WDtRyPXi5BWR/FYbmhoYNJpb4JCal1S87KrTvqNS6pt8v36lng+Lk+6uwcvFT6+tlS2P 1HillqZTDb3DR24UMVkg/l+SE06qPCFfw1Emthkri6pjwItqLHXRjV2BXB7C+d6qLZfN fJxQ== X-Gm-Message-State: APf1xPDJxX/8nKsWukcP97LOr26r1/4YWOdaOJuHC1tcYTFMmh5HukcH sSt187XqkjZcGeoI9R8hBKPHhw== X-Received: by 10.28.155.66 with SMTP id d63mr2067797wme.132.1518187211657; Fri, 09 Feb 2018 06:40:11 -0800 (PST) Received: from e104803-lin.lan (mail.andrep.de. [217.160.17.100]) by smtp.gmail.com with ESMTPSA id b35sm2552229wra.13.2018.02.09.06.40.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Feb 2018 06:40:11 -0800 (PST) From: Andre Przywara To: Stefano Stabellini , Julien Grall , xen-devel@lists.xenproject.org Date: Fri, 9 Feb 2018 14:39:13 +0000 Message-Id: <20180209143937.28866-26-andre.przywara@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180209143937.28866-1-andre.przywara@linaro.org> References: <20180209143937.28866-1-andre.przywara@linaro.org> Subject: [Xen-devel] [RFC PATCH 25/49] ARM: new VGIC: Add GICv2 world switch backend 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" Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version. Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers. This also adds the hook to let Xen setup the host GIC addresses. This is based on Linux commit 140b086dd197, written by Marc Zyngier. Signed-off-by: Andre Przywara --- xen/arch/arm/vgic/vgic-v2.c | 261 ++++++++++++++++++++++++++++++++++++++++++++ xen/arch/arm/vgic/vgic.c | 20 ++++ xen/arch/arm/vgic/vgic.h | 8 ++ 3 files changed, 289 insertions(+) create mode 100644 xen/arch/arm/vgic/vgic-v2.c diff --git a/xen/arch/arm/vgic/vgic-v2.c b/xen/arch/arm/vgic/vgic-v2.c new file mode 100644 index 0000000000..10fc467ffa --- /dev/null +++ b/xen/arch/arm/vgic/vgic-v2.c @@ -0,0 +1,261 @@ +/* + * Copyright (C) 2015, 2016 ARM Ltd. + * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "vgic.h" + +#define GICH_ELRSR0 0x30 +#define GICH_ELRSR1 0x34 +#define GICH_LR0 0x100 + +#define GICH_LR_VIRTUALID (0x3ff << 0) +#define GICH_LR_PHYSID_CPUID_SHIFT (10) +#define GICH_LR_PHYSID_CPUID (0x3ff << GICH_LR_PHYSID_CPUID_SHIFT) +#define GICH_LR_PRIORITY_SHIFT 23 +#define GICH_LR_STATE (3 << 28) +#define GICH_LR_PENDING_BIT (1 << 28) +#define GICH_LR_ACTIVE_BIT (1 << 29) +#define GICH_LR_EOI (1 << 19) +#define GICH_LR_HW (1 << 31) + +static struct { + bool enabled; + paddr_t dbase; /* Distributor interface address */ + paddr_t cbase; /* CPU interface address & size */ + paddr_t csize; + paddr_t vbase; /* Virtual CPU interface address */ + void __iomem *hbase; /* Hypervisor control interface */ + + /* Offset to add to get an 8kB contiguous region if GIC is aliased */ + uint32_t aliased_offset; +} gic_v2_hw_data; + +void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize, + paddr_t vbase, void __iomem *hbase, + uint32_t aliased_offset) +{ + gic_v2_hw_data.enabled = true; + gic_v2_hw_data.dbase = dbase; + gic_v2_hw_data.cbase = cbase; + gic_v2_hw_data.csize = csize; + gic_v2_hw_data.vbase = vbase; + gic_v2_hw_data.hbase = hbase; + gic_v2_hw_data.aliased_offset = aliased_offset; +} + +void vgic_v2_set_underflow(struct vcpu *vcpu) +{ + gic_hw_ops->update_hcr_status(GICH_HCR_UIE, 1); +} + +/* + * transfer the content of the LRs back into the corresponding ap_list: + * - active bit is transferred as is + * - pending bit is + * - transferred as is in case of edge sensitive IRQs + * - set to the line-level (resample time) for level sensitive IRQs + */ +void vgic_v2_fold_lr_state(struct vcpu *vcpu) +{ + struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + struct vgic_v2_cpu_if *cpuif = &vgic_cpu->vgic_v2; + int lr; + unsigned long flags; + + cpuif->vgic_hcr &= ~GICH_HCR_UIE; + + for ( lr = 0; lr < vgic_cpu->used_lrs; lr++ ) + { + u32 val = cpuif->vgic_lr[lr]; + u32 intid = val & GICH_LR_VIRTUALID; + struct vgic_irq *irq; + + irq = vgic_get_irq(vcpu->domain, vcpu, intid); + + spin_lock_irqsave(&irq->irq_lock, flags); + + /* Always preserve the active bit */ + irq->active = !!(val & GICH_LR_ACTIVE_BIT); + + /* Edge is the only case where we preserve the pending bit */ + if ( irq->config == VGIC_CONFIG_EDGE && (val & GICH_LR_PENDING_BIT) ) + { + irq->pending_latch = true; + + if ( vgic_irq_is_sgi(intid) ) + { + u32 cpuid = val & GICH_LR_PHYSID_CPUID; + + cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT; + irq->source |= (1 << cpuid); + } + } + + if ( irq->hw && irq->config == VGIC_CONFIG_LEVEL && + (val & GICH_LR_PENDING_BIT) ) + { + irq->line_level = gic_read_pending_state(irq->hwintid); + + if ( !irq->line_level ) + gic_set_active_state(irq->hwintid, true); + } + + spin_unlock_irqrestore(&irq->irq_lock, flags); + vgic_put_irq(vcpu->domain, irq); + } + + vgic_cpu->used_lrs = 0; +} + +/* + * Populates the particular LR with the state of a given IRQ: + * - for an edge sensitive IRQ the pending state is cleared in struct vgic_irq + * - for a level sensitive IRQ the pending state value is unchanged; + * it is dictated directly by the input level + * + * If @irq describes an SGI with multiple sources, we choose the + * lowest-numbered source VCPU and clear that bit in the source bitmap. + * + * The irq_lock must be held by the caller. + */ +void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr) +{ + u32 val = irq->intid; + + if ( irq_is_pending(irq) ) + { + val |= GICH_LR_PENDING_BIT; + + if ( irq->config == VGIC_CONFIG_EDGE ) + irq->pending_latch = false; + + if ( vgic_irq_is_sgi(irq->intid) ) + { + u32 src = ffs(irq->source); + + BUG_ON(!src); + val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT; + irq->source &= ~(1 << (src - 1)); + if ( irq->source ) + irq->pending_latch = true; + } + } + + if ( irq->active ) + val |= GICH_LR_ACTIVE_BIT; + + if ( irq->hw ) + { + val |= GICH_LR_HW; + val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT; + /* + * Never set pending+active on a HW interrupt, as the + * pending state is kept at the physical distributor + * level. + */ + if ( irq->active && irq_is_pending(irq) ) + val &= ~GICH_LR_PENDING_BIT; + } + else + { + if ( irq->config == VGIC_CONFIG_LEVEL ) + val |= GICH_LR_EOI; + } + + /* + * Level-triggered mapped IRQs are special because we only observe + * rising edges as input to the VGIC. We therefore lower the line + * level here, so that we can take new virtual IRQs. See + * vgic_v2_fold_lr_state for more info. + */ + if ( irq->hw && irq->config == VGIC_CONFIG_LEVEL && + (val & GICH_LR_PENDING_BIT) ) + irq->line_level = false; + + /* The GICv2 LR only holds five bits of priority. */ + val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT; + + vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val; +} + +void vgic_v2_clear_lr(struct vcpu *vcpu, int lr) +{ + vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0; +} + +static void save_lrs(struct vcpu *vcpu, void __iomem *base) +{ + struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; + u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; + u64 elrsr; + int i; + + elrsr = readl_relaxed(base + GICH_ELRSR0); + if ( unlikely(used_lrs > 32) ) + elrsr |= ((u64)readl_relaxed(base + GICH_ELRSR1)) << 32; + + for ( i = 0; i < used_lrs; i++ ) + { + if ( elrsr & (1UL << i) ) + cpu_if->vgic_lr[i] &= ~GICH_LR_STATE; + else + cpu_if->vgic_lr[i] = readl_relaxed(base + GICH_LR0 + (i * 4)); + + writel_relaxed(0, base + GICH_LR0 + (i * 4)); + } +} + +void vgic_v2_save_state(struct vcpu *vcpu) +{ + u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; + + if ( used_lrs ) + { + save_lrs(vcpu, gic_v2_hw_data.hbase); + writel_relaxed(0, gic_v2_hw_data.hbase + GICH_HCR); + } +} + +void vgic_v2_restore_state(struct vcpu *vcpu) +{ + struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; + u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs; + int i; + + if ( used_lrs ) + { + writel_relaxed(cpu_if->vgic_hcr, + gic_v2_hw_data.hbase + GICH_HCR); + for ( i = 0; i < used_lrs; i++ ) + writel_relaxed(cpu_if->vgic_lr[i], + gic_v2_hw_data.hbase + GICH_LR0 + (i * 4)); + } +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/arch/arm/vgic/vgic.c b/xen/arch/arm/vgic/vgic.c index a1f77130d4..f4f2a04a60 100644 --- a/xen/arch/arm/vgic/vgic.c +++ b/xen/arch/arm/vgic/vgic.c @@ -488,6 +488,7 @@ retry: static inline void vgic_fold_lr_state(struct vcpu *vcpu) { + vgic_v2_fold_lr_state(vcpu); } /* Requires the irq_lock to be held. */ @@ -495,14 +496,18 @@ static inline void vgic_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr) { ASSERT(spin_is_locked(&irq->irq_lock)); + + vgic_v2_populate_lr(vcpu, irq, lr); } static inline void vgic_clear_lr(struct vcpu *vcpu, int lr) { + vgic_v2_clear_lr(vcpu, lr); } static inline void vgic_set_underflow(struct vcpu *vcpu) { + vgic_v2_set_underflow(vcpu); } /* Requires the ap_list_lock to be held. */ @@ -573,6 +578,11 @@ next: vgic_clear_lr(vcpu, count); } +static inline void vgic_save_state(struct vcpu *vcpu) +{ + vgic_v2_save_state(vcpu); +} + /* * gic_clear_lrs() - Update the VGIC state from hardware after a guest's run. * @vcpu: the VCPU. @@ -592,11 +602,18 @@ void gic_clear_lrs(struct vcpu *vcpu) if ( list_empty(&vcpu->arch.vgic_cpu.ap_list_head) ) return; + vgic_save_state(vcpu); + if ( vgic_cpu->used_lrs ) vgic_fold_lr_state(vcpu); vgic_prune_ap_list(vcpu); } +static inline void vgic_restore_state(struct vcpu *vcpu) +{ + vgic_v2_restore_state(vcpu); +} + /* * gic_inject() - flush the emulation state into the hardware on guest entry * @@ -625,7 +642,10 @@ void gic_inject(void) spin_lock(¤t->arch.vgic_cpu.ap_list_lock); vgic_flush_lr_state(current); spin_unlock(¤t->arch.vgic_cpu.ap_list_lock); + + vgic_restore_state(current); } + /* * Local variables: * mode: C diff --git a/xen/arch/arm/vgic/vgic.h b/xen/arch/arm/vgic/vgic.h index 47fc58b81e..771ca6f046 100644 --- a/xen/arch/arm/vgic/vgic.h +++ b/xen/arch/arm/vgic/vgic.h @@ -41,4 +41,12 @@ static inline void vgic_get_irq_kref(struct vgic_irq *irq) atomic_inc(&irq->refcount); } +void vgic_v2_fold_lr_state(struct vcpu *vcpu); +void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr); +void vgic_v2_clear_lr(struct vcpu *vcpu, int lr); +void vgic_v2_set_underflow(struct vcpu *vcpu); + +void vgic_v2_save_state(struct vcpu *vcpu); +void vgic_v2_restore_state(struct vcpu *vcpu); + #endif