From patchwork Fri Nov 17 16:09:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 119178 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp703303qgn; Fri, 17 Nov 2017 08:09:42 -0800 (PST) X-Google-Smtp-Source: AGs4zMZW5RnnNNBEbT3+zoBisqQT5VQlqtPhKBbakKvS6SxrY+7YhwZuQ7phWlx+4r6Thds0hRa4 X-Received: by 10.99.191.69 with SMTP id i5mr5566374pgo.13.1510934982552; Fri, 17 Nov 2017 08:09:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510934982; cv=none; d=google.com; s=arc-20160816; b=ONeQ9/voBHkPR6TNrWAkBk8M5fU31Hra1Z5sxjxyKwwEdy0/qbWHCW19t6a1EI5n6u WmZ4GRarBVU9RBllTn28v6i6iSWjrpmc5hboe8AcRHMO2BiH4vr5QT69F/gpQ56zdBBu 0a9cWuEVwJW40OkRZ3ThLmO23wQQ4smlbqoJcQr12x0UNk25ZH5Hf3UsYvOkPrC6R3z+ s4W2sgCdYeaWQJqQTDK1NthcTBG+P/pbNMEQP9pgV0xh0CDB+S8vqtzwXea3IR0yS1g7 n2o30Pi13+MqIQ2satrbsRKOEMJ2P5NuT8aNqYJyY5RaABgWbXp5CTW8JWFyeGlqpAUu c/uQ== 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:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=RiETZP8GgYtv3ZWFdh42oInpp5xhiWEvym4JRAU73Iw=; b=QJsoPRzrOzhzlQpqebVo96wo/QZ+v0oUjAZ2XB6HVJmOffIIiB541OcMaRUd65KGhG emdqpcP/hfjz4yj1p8TlAHXJvdDVmJtTl0jHhc3ccS2CaFGH2Hz4xckE1ekdO9LGmgh3 iXwVw3WQ8dstr1tAalHN7be6uwUZXQnT4/zcwZ0ovV3OclkYwBorH092UlvKv98BHp9h kkBuQvlKupcpxMd0T/BMxDtwMbHgyXD74CyQlTO5zOYS7s6QWTqBBD1WWhGYZBGrHMQS Oy3DslxmrSETQ79YYU+m2Lp+WIoWaBtq8pn5So90q2OqF6SQey+QmU7Bgjht3uCamqX1 R8zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=O3xXEDEg; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id l7si2963058pgn.364.2017.11.17.08.09.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Nov 2017 08:09:42 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) client-ip=198.145.21.10; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=O3xXEDEg; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 037FE21B00DEF; Fri, 17 Nov 2017 08:05:26 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c0c::241; helo=mail-wr0-x241.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wr0-x241.google.com (mail-wr0-x241.google.com [IPv6:2a00:1450:400c:c0c::241]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id C0B8D21B00DEF for ; Fri, 17 Nov 2017 08:05:23 -0800 (PST) Received: by mail-wr0-x241.google.com with SMTP id u40so2513188wrf.10 for ; Fri, 17 Nov 2017 08:09:34 -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=JJFCtmCP2dUBYG/+mXY5uNl/YF1w85LZWnWs/Onula8=; b=O3xXEDEgChI1q9Xj1nmPI2qC3bgar4mH6zCKtQocZwdWK5iGlh0VSIEIRzKkqlGrIQ Cflgf0fe1pMtOLn/yS2zUx6ZocOa/qVjZQByMeWomkRBuzWwUJrvn90pDHBRGASgERtZ AU/YgD7uJ/o5w9ebnrxgbhwxJWGe+oaHo9nWs= 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=JJFCtmCP2dUBYG/+mXY5uNl/YF1w85LZWnWs/Onula8=; b=ubQWaB8M6oN7ajTDUOnAiFjrbFtBV6TLyVXCejyurJqp1ZB2YD2lejLLvXFO95BSGZ satIIlSXJMyjeSRy8XqLaWPKhqFWUO2kQrOL452cxtmGA76pbMcSeq6V3/4ecFlveGcu 6di1AxMLdWslU8uoUXilx77k1YXQuoag90gAXAjP3CFymR197Uj4S/bWhyG8T5bEXhzN T7TTjxGOWAXk7DxDTAeFUEm0JDOyMFxoVNrrSb4yCwDd5PLCmgjRed9hf/HWpK3kUjR0 4qd3UzPVAoHF/blWsEn3uNRiwqpJpSCMnVgphV4gs6/Eh68Rq1RpGqhmEFLDXWOgiwy6 6MUQ== X-Gm-Message-State: AJaThX4/bMGETos0/QoPSFeNxW4cWJodzp+e3igvV/G5BdIm3JsiA9S+ cf4VYCkvn8e+/D0aPWhRs+hRgHZd6bI= X-Received: by 10.223.177.220 with SMTP id r28mr4729032wra.83.1510934972900; Fri, 17 Nov 2017 08:09:32 -0800 (PST) Received: from localhost.localdomain ([160.167.170.128]) by smtp.gmail.com with ESMTPSA id p42sm5575959wrb.28.2017.11.17.08.09.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Nov 2017 08:09:32 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, lersek@redhat.com Date: Fri, 17 Nov 2017 16:09:04 +0000 Message-Id: <20171117160913.17292-7-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171117160913.17292-1-ard.biesheuvel@linaro.org> References: <20171117160913.17292-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH 06/15] ArmVirtPkg/PrePi: move DRAM discovery code into PrePi X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: leif.lindholm@linaro.org, Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" ArmVirtQemuKernel and ArmVirtXen use essentially the same code to retrieve DRAM information from the DT /memory node at early boot, and invoke it via the ArmPlatformPeiBootAction () hook exposed by ArmPlatformLib. Let's move this code into the PrePi implementation these platforms share between them (and not with any other platforms) so we can eliminate another dependency on the messy ArmPlatformLib. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S | 77 ++++++++++++++++- ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S | 71 +++++++++++++++ ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf | 2 + ArmVirtPkg/PrePi/FdtParser.c | 90 ++++++++++++++++++++ 4 files changed, 238 insertions(+), 2 deletions(-) -- 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel Acked-by: Laszlo Ersek diff --git a/ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S b/ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S index 7a9c0c3787cc..3296aedfe9aa 100644 --- a/ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S +++ b/ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S @@ -49,8 +49,7 @@ ASM_FUNC(_ModuleEntryPoint) b .Lreloc_loop .Lreloc_done: - // Do early platform specific actions - bl ASM_PFX(ArmPlatformPeiBootAction) + bl ASM_PFX(DiscoverDramFromDt) // Get ID of this CPU in Multicore system bl ASM_PFX(ArmReadMpidr) @@ -140,3 +139,77 @@ _GetStackBase: _NeverReturn: b _NeverReturn + +// VOID +// DiscoverDramFromDt ( +// VOID *DeviceTreeBaseAddress, // passed by loader in x0 +// VOID *ImageBase // passed by FDF trampoline in x1 +// ); +ASM_PFX(DiscoverDramFromDt): + // + // If we are booting from RAM using the Linux kernel boot protocol, x0 will + // point to the DTB image in memory. Otherwise, use the default value defined + // by the platform. + // + cbnz x0, 0f + ldr x0, PcdGet64 (PcdDeviceTreeInitialBaseAddress) + +0:mov x29, x30 // preserve LR + mov x28, x0 // preserve DTB pointer + mov x27, x1 // preserve base of image pointer + + // + // The base of the runtime image has been preserved in x1. Check whether + // the expected magic number can be found in the header. + // + ldr w8, .LArm64LinuxMagic + ldr w9, [x1, #0x38] + cmp w8, w9 + bne .Lout + + // + // + // OK, so far so good. We have confirmed that we likely have a DTB and are + // booting via the arm64 Linux boot protocol. Update the base-of-image PCD + // to the actual relocated value, and add the shift of PcdFdBaseAddress to + // PcdFvBaseAddress as well + // + adr x8, PcdGet64 (PcdFdBaseAddress) + adr x9, PcdGet64 (PcdFvBaseAddress) + ldr x6, [x8] + ldr x7, [x9] + sub x7, x7, x6 + add x7, x7, x1 + str x1, [x8] + str x7, [x9] + + // + // Discover the memory size and offset from the DTB, and record in the + // respective PCDs. This will also return false if a corrupt DTB is + // encountered. Since we are calling a C function, use the window at the + // beginning of the FD image as a temp stack. + // + adr x1, PcdGet64 (PcdSystemMemoryBase) + adr x2, PcdGet64 (PcdSystemMemorySize) + mov sp, x7 + bl FindMemnode + cbz x0, .Lout + + // + // Copy the DTB to the slack space right after the 64 byte arm64/Linux style + // image header at the base of this image (defined in the FDF), and record the + // pointer in PcdDeviceTreeInitialBaseAddress. + // + adr x8, PcdGet64 (PcdDeviceTreeInitialBaseAddress) + add x27, x27, #0x40 + str x27, [x8] + + mov x0, x27 + mov x1, x28 + bl CopyFdt + +.Lout: + ret x29 + +.LArm64LinuxMagic: + .byte 0x41, 0x52, 0x4d, 0x64 diff --git a/ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S b/ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S index eebf660acdb2..a918c191432e 100644 --- a/ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S +++ b/ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S @@ -148,3 +148,74 @@ _GetStackBase: _NeverReturn: b _NeverReturn + +ASM_PFX(ArmPlatformPeiBootAction): + // + // If we are booting from RAM using the Linux kernel boot protocol, r0 will + // point to the DTB image in memory. Otherwise, use the default value defined + // by the platform. + // + teq r0, #0 + bne 0f + LDRL (r0, PcdGet64 (PcdDeviceTreeInitialBaseAddress)) + +0:mov r11, r14 // preserve LR + mov r10, r0 // preserve DTB pointer + mov r9, r1 // preserve base of image pointer + + // + // The base of the runtime image has been preserved in r1. Check whether + // the expected magic number can be found in the header. + // + ldr r8, .LArm32LinuxMagic + ldr r7, [r1, #0x24] + cmp r7, r8 + bne .Lout + + // + // + // OK, so far so good. We have confirmed that we likely have a DTB and are + // booting via the ARM Linux boot protocol. Update the base-of-image PCD + // to the actual relocated value, and add the shift of PcdFdBaseAddress to + // PcdFvBaseAddress as well + // + ADRL (r8, PcdGet64 (PcdFdBaseAddress)) + ADRL (r7, PcdGet64 (PcdFvBaseAddress)) + ldr r6, [r8] + ldr r5, [r7] + sub r5, r5, r6 + add r5, r5, r1 + str r1, [r8] + str r5, [r7] + + // + // Discover the memory size and offset from the DTB, and record in the + // respective PCDs. This will also return false if a corrupt DTB is + // encountered. Since we are calling a C function, use the window at the + // beginning of the FD image as a temp stack. + // + ADRL (r1, PcdGet64 (PcdSystemMemoryBase)) + ADRL (r2, PcdGet64 (PcdSystemMemorySize)) + mov sp, r5 + bl FindMemnode + teq r0, #0 + beq .Lout + + // + // Copy the DTB to the slack space right after the 64 byte arm64/Linux style + // image header at the base of this image (defined in the FDF), and record the + // pointer in PcdDeviceTreeInitialBaseAddress. + // + ADRL (r8, PcdGet64 (PcdDeviceTreeInitialBaseAddress)) + add r9, r9, #0x40 + str r9, [r8] + + mov r0, r9 + mov r1, r10 + bl CopyFdt + +.Lout: + bx r11 + +.LArm32LinuxMagic: + .byte 0x18, 0x28, 0x6f, 0x01 diff --git a/ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf b/ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf index 1d79b1360c22..e816e9583da8 100755 --- a/ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf +++ b/ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatable.inf @@ -23,6 +23,7 @@ [Defines] [Sources] PrePi.c + FdtParser.c [Sources.AArch64] AArch64/ArchPrePi.c @@ -44,6 +45,7 @@ [Packages] [LibraryClasses] BaseLib DebugLib + FdtLib ArmLib IoLib TimerLib diff --git a/ArmVirtPkg/PrePi/FdtParser.c b/ArmVirtPkg/PrePi/FdtParser.c new file mode 100644 index 000000000000..afdc81a8839d --- /dev/null +++ b/ArmVirtPkg/PrePi/FdtParser.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2015, Linaro Ltd. All rights reserved. + * + * This program and the accompanying materials + * are licensed and made available under the terms and conditions of the BSD License + * which accompanies this distribution. The full text of the license may be found at + * http://opensource.org/licenses/bsd-license.php + * + * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + */ + +#include +#include + +BOOLEAN +FindMemnode ( + IN VOID *DeviceTreeBlob, + OUT UINT64 *SystemMemoryBase, + OUT UINT64 *SystemMemorySize + ) +{ + INT32 MemoryNode; + INT32 AddressCells; + INT32 SizeCells; + INT32 Length; + CONST INT32 *Prop; + + if (fdt_check_header (DeviceTreeBlob) != 0) { + return FALSE; + } + + // + // Look for a node called "memory" at the lowest level of the tree + // + MemoryNode = fdt_path_offset (DeviceTreeBlob, "/memory"); + if (MemoryNode <= 0) { + return FALSE; + } + + // + // Retrieve the #address-cells and #size-cells properties + // from the root node, or use the default if not provided. + // + AddressCells = 1; + SizeCells = 1; + + Prop = fdt_getprop (DeviceTreeBlob, 0, "#address-cells", &Length); + if (Length == 4) { + AddressCells = fdt32_to_cpu (*Prop); + } + + Prop = fdt_getprop (DeviceTreeBlob, 0, "#size-cells", &Length); + if (Length == 4) { + SizeCells = fdt32_to_cpu (*Prop); + } + + // + // Now find the 'reg' property of the /memory node, and read the first + // range listed. + // + Prop = fdt_getprop (DeviceTreeBlob, MemoryNode, "reg", &Length); + + if (Length < (AddressCells + SizeCells) * sizeof (INT32)) { + return FALSE; + } + + *SystemMemoryBase = fdt32_to_cpu (Prop[0]); + if (AddressCells > 1) { + *SystemMemoryBase = (*SystemMemoryBase << 32) | fdt32_to_cpu (Prop[1]); + } + Prop += AddressCells; + + *SystemMemorySize = fdt32_to_cpu (Prop[0]); + if (SizeCells > 1) { + *SystemMemorySize = (*SystemMemorySize << 32) | fdt32_to_cpu (Prop[1]); + } + + return TRUE; +} + +VOID +CopyFdt ( + IN VOID *FdtDest, + IN VOID *FdtSource + ) +{ + fdt_pack(FdtSource); + CopyMem (FdtDest, FdtSource, fdt_totalsize (FdtSource)); +}