Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 139ECC433EF for ; Tue, 7 Jun 2022 13:02:57 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 759F08438D; Tue, 7 Jun 2022 15:02:55 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id ADAD78438D; Tue, 7 Jun 2022 15:02:53 +0200 (CEST) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id EE00E84385 for ; Tue, 7 Jun 2022 15:02:49 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=andre.przywara@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 06A6411FB; Tue, 7 Jun 2022 06:02:49 -0700 (PDT) Received: from donnerap.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DEC083F73B; Tue, 7 Jun 2022 06:02:47 -0700 (PDT) Date: Tue, 7 Jun 2022 14:02:44 +0100 From: Andre Przywara To: Mark Kettenis Cc: trini@konsulko.com, sjg@chromium.org, u-boot@lists.denx.de, Balaji.Anandapadmanaban@arm.com, peng.fan@nxp.com, andy.yan@rock-chips.com Subject: Re: [PATCH] armv8: Fix TCR 64-bit writes Message-ID: <20220607140244.5aa30284@donnerap.cambridge.arm.com> In-Reply-To: References: <20220509160849.246793-1-andre.przywara@arm.com> Organization: ARM X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.32; aarch64-unknown-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean On Mon, 6 Jun 2022 23:00:08 +0200 (CEST) Mark Kettenis wrote: Hi Mark, > > From: Andre Przywara > > Date: Mon, 9 May 2022 17:08:49 +0100 > > > > The AArch64 TCR_ELx register is a 64-bit register, and many newer > > architecture features use bits in the upper half. So far U-Boot was > > igorant of those bits, trying to leave them alone. > > However, in an effort to set bit 31 to 1, it failed doing so, because > > the compiler sign-extended "1 << 31", so that all bits[63:31] got set. > > > > Older ARMv8.0 cores don't define anything dangerous up there, but newer > > architecture revisions do, and setting all those bits will end badly: > > ================= > > $ qemu-system-aarch64 -cpu max .... > > U-Boot 2022.07-rc1 (May 09 2022 - 15:21:00 +0100) > > > > DRAM: 1.5 GiB > > ================= (hangs here) > > > > Defining TCR_ELx_RSVD to "1U << 31" avoids the sign-extension, so all > > upper bits stay at a safe 0 value. This means no more surprises when > > U-Boot runs on a more capable CPU core. > > Hi Andre, > > This change breaks U-Boot on Apple M1 systems. Th reason is > "interesting". The Apple Icestorm and Firestorm cores implement > FEAT_VHE, but do so in an unusual (non-compiant?) way. Ah, the good ol' always VHE hack, biting us over and over again. > the > HCR_EL2.E2H bit is hardwired to 1, which means that EL2 Host is always > enabled. As a consequence, TCR_EL2 has a different layout (the same > as TCR_EL1). This means that the get_tcr() function in > arch/arm/cpu/armv8/cache_v8.c returns the wrong value. Apparently the > sign-extension that happened before accidentally set the "right" bits > so I didn't notice this issue before. > > Below is a possible fix. This uses #ifdef CONFIG_ARCH_APPLE to avoid > growing the code. A more generic way of fixing this would involve > checking the value of HCR_EL2.E2H, probably in mmu_setup() and pass > el=1 instead of el=2 to get_tcr() instead. > > Thoughts? I am very much in favour of fixing this properly, so by using runtime VHE detection. Looking at the code, the "el" parameter in get_tcr is actually mostly just the current EL, except in two cases, where we pass 0 (which looks wrong), and are just after the VA size bits. I will try to just determine the correct EL internally, so calling current_el() and checking HCR_EL2. Will send a patch ASAP. Cheers, Andre > diff --git a/arch/arm/cpu/armv8/cache_v8.c b/arch/arm/cpu/armv8/cache_v8.c > index 3de18c7675..ea3a60b2cd 100644 > --- a/arch/arm/cpu/armv8/cache_v8.c > +++ b/arch/arm/cpu/armv8/cache_v8.c > @@ -74,7 +74,11 @@ u64 get_tcr(int el, u64 *pips, u64 *pva_bits) > if (el == 1) { > tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE; > } else if (el == 2) { > +#ifdef CONFIG_ARCH_APPLE > + tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE; > +#else > tcr = TCR_EL2_RSVD | (ips << 16); > +#endif > } else { > tcr = TCR_EL3_RSVD | (ips << 16); > } > >