Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp478781pxf; Wed, 10 Mar 2021 10:00:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJxSFfaRdW3CDUQQS8cxaezzX35ERl5osWZasmN9IFcs12TtWaYDUlwArPuCDJRSrok/N2qK X-Received: by 2002:a17:907:3e92:: with SMTP id hs18mr4861314ejc.396.1615399250001; Wed, 10 Mar 2021 10:00:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615399249; cv=none; d=google.com; s=arc-20160816; b=v9NYdSPTRPrN0NecQQqR7SeKQF1RCmEwiKtQRqA+y2dEo10wKVeb5KT83dBo/JmWOR ETKyHg4e60KNBhW9REJBHcpo5UiEIIm6DU8pB5C/T3PP4d3LQBKmJEX5WQ73Z4SIaMlO iHvJD84z3Ig0pBFBItkB7xznkVy8bB3rI/G3h0VM/iJxby2g6yFiB7wuDWDzitY4fqbW btvaZvOoTdZ7mSIZa0nVzhqtvtc7OWMUQxbypVDhmXJxlGe4IUBaXPmt5KLU59667sjs aZp5NSaIqPk6aeQcCHNXdiBDSJlOxYc7U3f2r/S3/cSfRzVYwGoVEDRsHsUf+8HCYqqr T5tQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=5IlrHyzKGX66i7zKiJ2V9j/u7ly1Dp8Z1hyFGz/2u64=; b=Dd3XFN469cdvpuJSnhDBTGlF1kkTAH+whqJXXyEDqr3/CjC1sIJdzfreA7kLVsPf3r JI/ic1QacBT8p+5p2+jaxY+ViQd3j+Tcu+bUmbbDNNTg1SoaNHtOgb6QSX6ZzqH0QhHI /TiFr7gWFH0vhi2bwe3eI5OVZIXBsQb0U6kApbZ2xTL0BS2hklF8Hcdt+55j42lQQYRu Exfj6ayhysj0tG2r4+wxzDA0SlA7d1CKGyCLd6PA8qF+LxCGt3gvssIuqqn2KRZaCeFr AjAC16SBcHL1W9gItL2t8dCWIOTUW8MhI90da55lMVaUjSJvspwf8+aVF21Z60zN34Se Nx3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=iVAj6JJV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bc17si118400edb.2.2021.03.10.10.00.26; Wed, 10 Mar 2021 10:00:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=iVAj6JJV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233960AbhCJR7I (ORCPT + 99 others); Wed, 10 Mar 2021 12:59:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233351AbhCJR6l (ORCPT ); Wed, 10 Mar 2021 12:58:41 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 524C6C061760 for ; Wed, 10 Mar 2021 09:58:41 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id g5so8273465wrd.22 for ; Wed, 10 Mar 2021 09:58:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=5IlrHyzKGX66i7zKiJ2V9j/u7ly1Dp8Z1hyFGz/2u64=; b=iVAj6JJV6CazuRaEPHki7YV1asV87XjurUX2cUuvZ+I5M5+7StJNzXQ21pGTkVmDPz 2aczpVWNfqn7LkeVOxA4/yeE8nSN0ITE4MVenhoN2DdY+LEMGGlGB6dS2Fm2BhIRJXLD rSiLrmFClj4NldUmlaELdYXImbF1cUAqAObLSzjCF0Z6iB1wX08jVJ8aRTSKpGuuFLXC KLzpf94b1W+zksuPkHDjocduv3XlZIR/2ciHM7qd+bkMsL0xXUzrYwM98UcwaCO40nwX HBdxVkjPx1O/4/9AeMOtCur4ZXKGvLW3PbkmYR/LKB2Hovbu36MfZT+uDT99TDt7FF6c mAbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=5IlrHyzKGX66i7zKiJ2V9j/u7ly1Dp8Z1hyFGz/2u64=; b=TFJv77lownnsYV8JYbEcLjvsNQB1R2XMImlUeTgjQtOXLsqL95MGAzcvnp8BK8XjM4 Uld9017XGUIgbkLZBgROBmtT3LoeTQHQkMhNZDVaP1IMdEtXhEdYTOBzoonTzH3Bm3OF YV9ZW50ZDqPl1/MFiedgtdMH7XKE8xwnLyvWykLqXmO105XLWDDWK1JLaXFJ4o2f3/ws fhFBa/aJw/Izgz2seiRfHwFs/JfYmOvuwq7PzHV2fzdQKyoK1QmuQM0VWaEDek3+gt2F p17JoJVHZ8VOoLzASvmgXW3eA+diF3BU4gEdilsFmdk1kWKMnbLd4XyDrJNRewZkbVDy i9rw== X-Gm-Message-State: AOAM531LZjOa10/zBJ4hlrBX510MC0VCA70NVXm2waNe2iEgeLG80K2E +pQK62KDCIsCNrKj39yXXI+MdIja+hpk X-Received: from r2d2-qp.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:1652]) (user=qperret job=sendgmr) by 2002:a1c:6243:: with SMTP id w64mr1336092wmb.0.1615399119789; Wed, 10 Mar 2021 09:58:39 -0800 (PST) Date: Wed, 10 Mar 2021 17:57:38 +0000 In-Reply-To: <20210310175751.3320106-1-qperret@google.com> Message-Id: <20210310175751.3320106-22-qperret@google.com> Mime-Version: 1.0 References: <20210310175751.3320106-1-qperret@google.com> X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog Subject: [PATCH v4 21/34] KVM: arm64: Refactor kvm_arm_setup_stage2() From: Quentin Perret To: catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com Cc: android-kvm@google.com, linux-kernel@vger.kernel.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, tabba@google.com, mark.rutland@arm.com, dbrazdil@google.com, mate.toth-pal@arm.com, seanjc@google.com, qperret@google.com, robh+dt@kernel.org, ardb@kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In order to re-use some of the stage 2 setup code at EL2, factor parts of kvm_arm_setup_stage2() out into separate functions. No functional change intended. Acked-by: Will Deacon Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_pgtable.h | 26 +++++++++++++++++ arch/arm64/kvm/hyp/pgtable.c | 32 +++++++++++++++++++++ arch/arm64/kvm/reset.c | 42 +++------------------------- 3 files changed, 62 insertions(+), 38 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 7945ec87eaec..9cdc198ea6b4 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -13,6 +13,16 @@ #define KVM_PGTABLE_MAX_LEVELS 4U +static inline u64 kvm_get_parange(u64 mmfr0) +{ + u64 parange = cpuid_feature_extract_unsigned_field(mmfr0, + ID_AA64MMFR0_PARANGE_SHIFT); + if (parange > ID_AA64MMFR0_PARANGE_MAX) + parange = ID_AA64MMFR0_PARANGE_MAX; + + return parange; +} + typedef u64 kvm_pte_t; /** @@ -159,6 +169,22 @@ void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt); int kvm_pgtable_hyp_map(struct kvm_pgtable *pgt, u64 addr, u64 size, u64 phys, enum kvm_pgtable_prot prot); +/** + * kvm_get_vtcr() - Helper to construct VTCR_EL2 + * @mmfr0: Sanitized value of SYS_ID_AA64MMFR0_EL1 register. + * @mmfr1: Sanitized value of SYS_ID_AA64MMFR1_EL1 register. + * @phys_shfit: Value to set in VTCR_EL2.T0SZ. + * + * The VTCR value is common across all the physical CPUs on the system. + * We use system wide sanitised values to fill in different fields, + * except for Hardware Management of Access Flags. HA Flag is set + * unconditionally on all CPUs, as it is safe to run with or without + * the feature and the bit is RES0 on CPUs that don't support it. + * + * Return: VTCR_EL2 value + */ +u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift); + /** * kvm_pgtable_stage2_init() - Initialise a guest stage-2 page-table. * @pgt: Uninitialised page-table structure to initialise. diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 3d79c8094cdd..296675e5600d 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -9,6 +9,7 @@ #include #include +#include #define KVM_PTE_VALID BIT(0) @@ -449,6 +450,37 @@ struct stage2_map_data { struct kvm_pgtable_mm_ops *mm_ops; }; +u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift) +{ + u64 vtcr = VTCR_EL2_FLAGS; + u8 lvls; + + vtcr |= kvm_get_parange(mmfr0) << VTCR_EL2_PS_SHIFT; + vtcr |= VTCR_EL2_T0SZ(phys_shift); + /* + * Use a minimum 2 level page table to prevent splitting + * host PMD huge pages at stage2. + */ + lvls = stage2_pgtable_levels(phys_shift); + if (lvls < 2) + lvls = 2; + vtcr |= VTCR_EL2_LVLS_TO_SL0(lvls); + + /* + * Enable the Hardware Access Flag management, unconditionally + * on all CPUs. The features is RES0 on CPUs without the support + * and must be ignored by the CPUs. + */ + vtcr |= VTCR_EL2_HA; + + /* Set the vmid bits */ + vtcr |= (get_vmid_bits(mmfr1) == 16) ? + VTCR_EL2_VS_16BIT : + VTCR_EL2_VS_8BIT; + + return vtcr; +} + static int stage2_map_set_prot_attr(enum kvm_pgtable_prot prot, struct stage2_map_data *data) { diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 47f3f035f3ea..6aae118c960a 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -332,19 +332,10 @@ int kvm_set_ipa_limit(void) return 0; } -/* - * Configure the VTCR_EL2 for this VM. The VTCR value is common - * across all the physical CPUs on the system. We use system wide - * sanitised values to fill in different fields, except for Hardware - * Management of Access Flags. HA Flag is set unconditionally on - * all CPUs, as it is safe to run with or without the feature and - * the bit is RES0 on CPUs that don't support it. - */ int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type) { - u64 vtcr = VTCR_EL2_FLAGS, mmfr0; - u32 parange, phys_shift; - u8 lvls; + u64 mmfr0, mmfr1; + u32 phys_shift; if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK) return -EINVAL; @@ -359,33 +350,8 @@ int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type) } mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); - parange = cpuid_feature_extract_unsigned_field(mmfr0, - ID_AA64MMFR0_PARANGE_SHIFT); - if (parange > ID_AA64MMFR0_PARANGE_MAX) - parange = ID_AA64MMFR0_PARANGE_MAX; - vtcr |= parange << VTCR_EL2_PS_SHIFT; - - vtcr |= VTCR_EL2_T0SZ(phys_shift); - /* - * Use a minimum 2 level page table to prevent splitting - * host PMD huge pages at stage2. - */ - lvls = stage2_pgtable_levels(phys_shift); - if (lvls < 2) - lvls = 2; - vtcr |= VTCR_EL2_LVLS_TO_SL0(lvls); - - /* - * Enable the Hardware Access Flag management, unconditionally - * on all CPUs. The features is RES0 on CPUs without the support - * and must be ignored by the CPUs. - */ - vtcr |= VTCR_EL2_HA; + mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); + kvm->arch.vtcr = kvm_get_vtcr(mmfr0, mmfr1, phys_shift); - /* Set the vmid bits */ - vtcr |= (kvm_get_vmid_bits() == 16) ? - VTCR_EL2_VS_16BIT : - VTCR_EL2_VS_8BIT; - kvm->arch.vtcr = vtcr; return 0; } -- 2.30.1.766.gb4fecdf3b7-goog