From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 2BF7CD80320 for ; Sat, 19 Aug 2023 15:21:54 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=ffQHdvSxAVxzTwXykUnmw2cUQcovwFeGNoHwtWJm84g=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1692458512; v=1; b=GMiS1o9dQJtGMDuGPUUPuNDQMtV73bNfqN9cuuluIy0nQlE8jOrnPrBBtO8NHo/QU7ysDdqw XLn66hk34ljs742z7Ub7LNIUSLgAKJh8JwI2DRzfQJKVM99JBnvzQIu7/4izIuTB85KD36tEJdu UXxlTixhxWIKZ+G6+GUJt690= X-Received: by 127.0.0.2 with SMTP id PGjoYY7687511x4KHKtLnvT5; Sat, 19 Aug 2023 08:21:52 -0700 X-Received: from m12.mail.163.com (m12.mail.163.com [220.181.12.217]) by mx.groups.io with SMTP id smtpd.web11.5847.1692434322226975769 for ; Sat, 19 Aug 2023 01:38:42 -0700 X-Received: from rv-uefi.. (unknown [222.175.100.159]) by zwqz-smtp-mta-g1-0 (Coremail) with SMTP id _____wAHBhuKf+Bk6Q3WDQ--.21934S4; Sat, 19 Aug 2023 16:38:35 +0800 (CST) From: caiyuqing_hz@163.com To: devel@edk2.groups.io Cc: caiyuqing379 <202235273@mail.sdu.edu.cn>, dahogn , inochisa , meng-cz , USER0FISH , yli147 , ChaiEvan Subject: [edk2-devel] [PATCH edk2-platforms 5/8] Sophgo/SG2042Pkg: Add SEC module. Date: Sat, 19 Aug 2023 16:37:19 +0800 Message-Id: <2566914f4710f83c5dd79b29ba396abcfb324818.1692414746.git.202235273@mail.sdu.edu.cn> In-Reply-To: References: MIME-Version: 1.0 X-CM-TRANSID: _____wAHBhuKf+Bk6Q3WDQ--.21934S4 X-Coremail-Antispam: 1Uf129KBjvAXoWfAw13ZF48KF47Cr43Cr1xZrb_yoW8tr1rto Z7GFZ2yr48Gr45uw1xCrn7Ww4IgF12gFW3Jr1FqFWjqFWv9r13tayrJw15Gry5Ary8Gw4D W3yfZas7GFZaqrykn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxU-FApUUUUU X-Originating-IP: [222.175.100.159] X-CM-SenderInfo: 5fdl535tlqwslk26il2tof0z/1tbiWBDQxWNftUu0pAAAsP Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,caiyuqing_hz@163.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: SjEMOaId4p3ZIfpWoqYWG08Kx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=GMiS1o9d; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=163.com (policy=none) From: caiyuqing379 <202235273@mail.sdu.edu.cn> This module supports Sophgo SG2042 EVB platform. It uses the PEI less design. Add this module in SG2042Pkg leveraging the one from OvmfPkg/RiscVVirt. Cc: dahogn Cc: inochisa Cc: meng-cz Signed-off-by: caiyuqing379 <202235273@mail.sdu.edu.cn> Co-authored-by: USER0FISH Reviewed-by: yli147 Reviewed-by: ChaiEvan --- Platform/Sophgo/SG2042Pkg/Sec/SecMain.inf | 68 ++++ Platform/Sophgo/SG2042Pkg/Sec/SecMain.h | 103 ++++++ Platform/Sophgo/SG2042Pkg/Sec/Cpu.c | 29 ++ Platform/Sophgo/SG2042Pkg/Sec/Memory.c | 363 ++++++++++++++++++++++ Platform/Sophgo/SG2042Pkg/Sec/Platform.c | 80 +++++ Platform/Sophgo/SG2042Pkg/Sec/SecMain.c | 116 +++++++ Platform/Sophgo/SG2042Pkg/Sec/SecEntry.S | 18 ++ 7 files changed, 777 insertions(+) create mode 100644 Platform/Sophgo/SG2042Pkg/Sec/SecMain.inf create mode 100644 Platform/Sophgo/SG2042Pkg/Sec/SecMain.h create mode 100644 Platform/Sophgo/SG2042Pkg/Sec/Cpu.c create mode 100644 Platform/Sophgo/SG2042Pkg/Sec/Memory.c create mode 100644 Platform/Sophgo/SG2042Pkg/Sec/Platform.c create mode 100644 Platform/Sophgo/SG2042Pkg/Sec/SecMain.c create mode 100644 Platform/Sophgo/SG2042Pkg/Sec/SecEntry.S diff --git a/Platform/Sophgo/SG2042Pkg/Sec/SecMain.inf b/Platform/Sophgo/SG2042Pkg/Sec/SecMain.inf new file mode 100644 index 000000000000..25e1d4067862 --- /dev/null +++ b/Platform/Sophgo/SG2042Pkg/Sec/SecMain.inf @@ -0,0 +1,68 @@ +## @file +# SEC Driver for RISC-V +# +# Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
+# Copyright (c) 2023, Academy of Intelligent Innovation, Shandong Universiy, China.P.R. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x0001001B + BASE_NAME = SecMainRiscV64 + FILE_GUID = 125E1236-9D4F-457B-BF7E-6311C88A1621 + MODULE_TYPE = SEC + VERSION_STRING = 1.0 + ENTRY_POINT = SecMain + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = RISCV64 +# + +[Sources] + SecEntry.S + SecMain.c + SecMain.h + Cpu.c + Memory.c + Platform.c + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + Platform/Sophgo/SG2042Pkg/SG2042Pkg.dec + Platform/RISC-V/PlatformPkg/RiscVPlatformPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + PcdLib + IoLib + PeCoffLib + LzmaDecompressLib + RiscVSbiLib + PrePiLib + FdtLib + MemoryAllocationLib + HobLib + SerialPortLib + +[FixedPcd] + gUefiRiscVPlatformPkgTokenSpaceGuid.PcdRiscVDxeFvBase ## CONSUMES + gUefiRiscVPlatformPkgTokenSpaceGuid.PcdRiscVDxeFvSize ## CONSUMES + gUefiRiscVPlatformPkgTokenSpaceGuid.PcdVariableFirmwareRegionBaseAddress ## CONSUMES + gUefiRiscVPlatformPkgTokenSpaceGuid.PcdVariableFirmwareRegionSize ## CONSUMES + gUefiRiscVPlatformPkgTokenSpaceGuid.PcdTemporaryRamBase ## CONSUMES + gUefiRiscVPlatformPkgTokenSpaceGuid.PcdTemporaryRamSize ## CONSUMES + +[Guids] + gFdtHobGuid ## PRODUCES + +[BuildOptions] + GCC:*_*_*_PP_FLAGS = -D__ASSEMBLY__ + diff --git a/Platform/Sophgo/SG2042Pkg/Sec/SecMain.h b/Platform/Sophgo/SG2042Pkg/Sec/SecMain.h new file mode 100644 index 000000000000..c00ab6377e2e --- /dev/null +++ b/Platform/Sophgo/SG2042Pkg/Sec/SecMain.h @@ -0,0 +1,103 @@ +/** @file + Master header file for SecCore. + + Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
+ Copyright (c) 2023, Academy of Intelligent Innovation, Shandong Universiy, China.P.R. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef SEC_MAIN_H_ +#define SEC_MAIN_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + Entry point to the C language phase of SEC. After the SEC assembly + code has initialized some temporary memory and set up the stack, + the control is transferred to this function. + + @param SizeOfRam Size of the temporary memory available for use. + @param TempRamBase Base address of temporary ram + @param BootFirmwareVolume Base address of the Boot Firmware Volume. +**/ +VOID +NORETURN +EFIAPI +SecStartup ( + IN UINTN BootHartId, + IN VOID *DeviceTreeAddress + ); + +/** + Auto-generated function that calls the library constructors for all of the module's + dependent libraries. This function must be called by the SEC Core once a stack has + been established. + +**/ +VOID +EFIAPI +ProcessLibraryConstructorList ( + VOID + ); + +/** + Perform Platform PEIM initialization. + + @return EFI_SUCCESS The platform initialized successfully. + @retval Others - As the error code indicates + +**/ +EFI_STATUS +EFIAPI +PlatformPeimInitialization ( + VOID + ); + +/** + Perform Memory PEIM initialization. + + @return EFI_SUCCESS The platform initialized successfully. + @retval Others - As the error code indicates + +**/ +EFI_STATUS +EFIAPI +MemoryPeimInitialization ( + VOID + ); + +/** + Perform CPU PEIM initialization. + + @return EFI_SUCCESS The platform initialized successfully. + @retval Others - As the error code indicates + +**/ +EFI_STATUS +EFIAPI +CpuPeimInitialization ( + VOID + ); + +#endif diff --git a/Platform/Sophgo/SG2042Pkg/Sec/Cpu.c b/Platform/Sophgo/SG2042Pkg/Sec/Cpu.c new file mode 100644 index 000000000000..c72bafdcc478 --- /dev/null +++ b/Platform/Sophgo/SG2042Pkg/Sec/Cpu.c @@ -0,0 +1,29 @@ +/** @file +The library call to pass the device tree to DXE via HOB. + +Copyright (c) 2021, Hewlett Packard Enterprise Development LP. All rights reserved.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include + +/** + Cpu Peim initialization. + +**/ +EFI_STATUS +CpuPeimInitialization ( + VOID + ) +{ + // + // for MMU type >= sv39 + // + BuildCpuHob (40, 39); + + return EFI_SUCCESS; +} diff --git a/Platform/Sophgo/SG2042Pkg/Sec/Memory.c b/Platform/Sophgo/SG2042Pkg/Sec/Memory.c new file mode 100644 index 000000000000..d92a431b3cf3 --- /dev/null +++ b/Platform/Sophgo/SG2042Pkg/Sec/Memory.c @@ -0,0 +1,363 @@ +/** @file + Memory Detection for SG2042 EVB. + + Copyright (c) 2021, Hewlett Packard Enterprise Development LP. All rights reserved.
+ Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.
+ Copyright (c) 2023, Academy of Intelligent Innovation, Shandong Universiy, China.P.R. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +Module Name: + + MemDetect.c + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +VOID +BuildMemoryTypeInformationHob ( + VOID + ); + +/** + Create memory range resource HOB using the memory base + address and size. + + @param MemoryBase Memory range base address. + @param MemorySize Memory range size. + +**/ +STATIC +VOID +AddMemoryBaseSizeHob ( + IN EFI_PHYSICAL_ADDRESS MemoryBase, + IN UINT64 MemorySize + ) +{ + BuildResourceDescriptorHob ( + EFI_RESOURCE_SYSTEM_MEMORY, + EFI_RESOURCE_ATTRIBUTE_PRESENT | + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | + EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE | + EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE | + EFI_RESOURCE_ATTRIBUTE_TESTED, + MemoryBase, + MemorySize + ); +} + +/** + Create memory range resource HOB using memory base + address and top address of the memory range. + + @param MemoryBase Memory range base address. + @param MemoryLimit Memory range size. + +**/ +STATIC +VOID +AddMemoryRangeHob ( + IN EFI_PHYSICAL_ADDRESS MemoryBase, + IN EFI_PHYSICAL_ADDRESS MemoryLimit + ) +{ + AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase)); +} + +/** + Publish system RAM and reserve memory regions. + +**/ +STATIC +VOID +InitializeRamRegions ( + IN EFI_PHYSICAL_ADDRESS SystemMemoryBase, + IN UINT64 SystemMemorySize + ) +{ + AddMemoryRangeHob ( + SystemMemoryBase, + SystemMemoryBase + SystemMemorySize + ); +} + +/** Get the number of cells for a given property + + @param[in] Fdt Pointer to Device Tree (DTB) + @param[in] Node Node + @param[in] Name Name of the property + + @return Number of cells. +**/ +STATIC +INT32 +GetNumCells ( + IN VOID *Fdt, + IN INT32 Node, + IN CONST CHAR8 *Name + ) +{ + CONST INT32 *Prop; + INT32 Len; + UINT32 Val; + + Prop = fdt_getprop (Fdt, Node, Name, &Len); + if (Prop == NULL) { + return Len; + } + + if (Len != sizeof (*Prop)) { + return -FDT_ERR_BADNCELLS; + } + + Val = fdt32_to_cpu (*Prop); + if (Val > FDT_MAX_NCELLS) { + return -FDT_ERR_BADNCELLS; + } + + return (INT32)Val; +} + +/** Mark reserved memory ranges in the EFI memory map + + * As per DT spec v0.4 Section 3.5.4, + * "Reserved regions with the no-map property must be listed in the + * memory map with type EfiReservedMemoryType. All other reserved + * regions must be listed with type EfiBootServicesData." + + @param FdtPointer Pointer to FDT + +**/ +STATIC +VOID +AddReservedMemoryMap ( + IN VOID *FdtPointer + ) +{ + CONST INT32 *RegProp; + INT32 Node; + INT32 SubNode; + INT32 Len; + EFI_PHYSICAL_ADDRESS Addr; + UINT64 Size; + INTN NumRsv, i; + INT32 NumAddrCells, NumSizeCells; + + NumRsv = fdt_num_mem_rsv (FdtPointer); + + /* Look for an existing entry and add it to the efi mem map. */ + for (i = 0; i < NumRsv; i++) { + if (fdt_get_mem_rsv (FdtPointer, i, &Addr, &Size) != 0) { + continue; + } + + BuildMemoryAllocationHob ( + Addr, + Size, + EfiReservedMemoryType + ); + } + + /* process reserved-memory */ + Node = fdt_subnode_offset (FdtPointer, 0, "reserved-memory"); + if (Node >= 0) { + NumAddrCells = GetNumCells (FdtPointer, Node, "#address-cells"); + if (NumAddrCells <= 0) { + return; + } + + NumSizeCells = GetNumCells (FdtPointer, Node, "#size-cells"); + if (NumSizeCells <= 0) { + return; + } + + fdt_for_each_subnode (SubNode, FdtPointer, Node) { + RegProp = fdt_getprop (FdtPointer, SubNode, "reg", &Len); + + if ((RegProp != 0) && (Len == ((NumAddrCells + NumSizeCells) * sizeof (INT32)))) { + Addr = fdt32_to_cpu (RegProp[0]); + + if (NumAddrCells > 1) { + Addr = (Addr << 32) | fdt32_to_cpu (RegProp[1]); + } + + RegProp += NumAddrCells; + Size = fdt32_to_cpu (RegProp[0]); + + if (NumSizeCells > 1) { + Size = (Size << 32) | fdt32_to_cpu (RegProp[1]); + } + + DEBUG (( + DEBUG_INFO, + "%a: Adding Reserved Memory Addr = 0x%llx, Size = 0x%llx\n", + __func__, + Addr, + Size + )); + + if (fdt_getprop (FdtPointer, SubNode, "no-map", &Len)) { + BuildMemoryAllocationHob ( + Addr, + Size, + EfiReservedMemoryType + ); + } else { + BuildMemoryAllocationHob ( + Addr, + Size, + EfiBootServicesData + ); + } + } + } + } +} + +/** + Initialize memory hob based on the DTB information. + + NOTE: The memory space size of SG2042 EVB is determined by the number + and size of DDRs inserted on the board. There is an error with initializing + the system ram space of each memory node separately using InitializeRamRegions, + so InitializeRamRegions is only called once for total system ram initialization. + + @return EFI_SUCCESS The memory hob added successfully. + +**/ +EFI_STATUS +MemoryPeimInitialization ( + VOID + ) +{ + EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext; + CONST UINT64 *RegProp; + CONST CHAR8 *Type; + UINT64 UefiMemoryBase; + UINT64 CurBase; + UINT64 CurSize; + UINT64 LongestStart; + UINT64 LongestLength; + UINT64 PrevEnd; + UINT64 CurStart; + UINT64 CurLength; + UINT64 MaxLength; + INT32 Node; + INT32 Prev; + INT32 Len; + VOID *FdtPointer; + + FirmwareContext = NULL; + GetFirmwareContextPointer (&FirmwareContext); + + if (FirmwareContext == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Firmware Context is NULL\n", __func__)); + return EFI_UNSUPPORTED; + } + + FdtPointer = (VOID *)FirmwareContext->FlattenedDeviceTree; + if (FdtPointer == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Invalid FDT pointer\n", __func__)); + return EFI_UNSUPPORTED; + } + + UefiMemoryBase = (UINT64)FixedPcdGet32 (PcdTemporaryRamBase) + FixedPcdGet32 (PcdTemporaryRamSize) - SIZE_32MB; + LongestStart = 0; + LongestLength = 0; + PrevEnd = 0; + CurStart = 0; + CurLength = 0; + MaxLength = 0; + + // Look for the lowest memory node + for (Prev = 0; ; Prev = Node) { + Node = fdt_next_node (FdtPointer, Prev, NULL); + if (Node < 0) { + break; + } + + // Check for memory node + Type = fdt_getprop (FdtPointer, Node, "device_type", &Len); + if (Type && (AsciiStrnCmp (Type, "memory", Len) == 0)) { + // Get the 'reg' property of this node. For now, we will assume + // two 8 byte quantities for base and size, respectively. + RegProp = fdt_getprop (FdtPointer, Node, "reg", &Len); + if ((RegProp != 0) && (Len == (2 * sizeof (UINT64)))) { + CurBase = fdt64_to_cpu (ReadUnaligned64 (RegProp)); + CurSize = fdt64_to_cpu (ReadUnaligned64 (RegProp + 1)); + + DEBUG (( + DEBUG_INFO, + "%a: System RAM @ 0x%lx - 0x%lx\n", + __func__, + CurBase, + CurBase + CurSize - 1 + )); + + if (PrevEnd == 0 || CurBase == PrevEnd) { + if (CurLength == 0) { + CurStart = CurBase; + } + CurLength += CurSize; + + if (CurLength > MaxLength) { + MaxLength = CurLength; + LongestStart = CurStart; + LongestLength = MaxLength; + } + } else { + CurStart = CurBase; + CurLength = CurSize; + } + + PrevEnd = CurBase + CurSize; + + } else { + DEBUG (( + DEBUG_ERROR, + "%a: Failed to parse FDT memory node\n", + __func__ + )); + } + } + } + + if (UefiMemoryBase > LongestStart) { + LongestStart = UefiMemoryBase; + LongestLength -= UefiMemoryBase; + } + + DEBUG (( + DEBUG_INFO, + "%a: Total System RAM @ 0x%lx - 0x%lx\n", + __func__, + LongestStart, + LongestStart + LongestLength - 1 + )); + + InitializeRamRegions (LongestStart, LongestLength); + + AddReservedMemoryMap (FdtPointer); + + /* Make sure SEC is booting with bare mode */ + ASSERT ((RiscVGetSupervisorAddressTranslationRegister () & SATP64_MODE) == (SATP_MODE_OFF << SATP64_MODE_SHIFT)); + + BuildMemoryTypeInformationHob (); + + return EFI_SUCCESS; +} diff --git a/Platform/Sophgo/SG2042Pkg/Sec/Platform.c b/Platform/Sophgo/SG2042Pkg/Sec/Platform.c new file mode 100644 index 000000000000..f64eaec2ff28 --- /dev/null +++ b/Platform/Sophgo/SG2042Pkg/Sec/Platform.c @@ -0,0 +1,80 @@ +/** @file +The library call to pass the device tree to DXE via HOB. + +Copyright (c) 2021, Hewlett Packard Enterprise Development LP. All rights reserved.
+ +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + @retval EFI_SUCCESS The address of FDT is passed in HOB. + EFI_UNSUPPORTED Can't locate FDT. +**/ +EFI_STATUS +EFIAPI +PlatformPeimInitialization ( + VOID + ) +{ + EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext; + VOID *FdtPointer; + VOID *Base; + VOID *NewBase; + UINTN FdtSize; + UINTN FdtPages; + UINT64 *FdtHobData; + + FirmwareContext = NULL; + GetFirmwareContextPointer (&FirmwareContext); + + if (FirmwareContext == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Firmware Context is NULL\n", __func__)); + return EFI_UNSUPPORTED; + } + + FdtPointer = (VOID *)FirmwareContext->FlattenedDeviceTree; + if (FdtPointer == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Invalid FDT pointer\n", __func__)); + return EFI_UNSUPPORTED; + } + + DEBUG ((DEBUG_INFO, "%a: Build FDT HOB - FDT at address: 0x%x \n", __func__, FdtPointer)); + Base = FdtPointer; + if (fdt_check_header (Base) != 0) { + DEBUG ((DEBUG_ERROR, "%a: Corrupted DTB\n", __func__)); + return EFI_UNSUPPORTED; + } + + FdtSize = fdt_totalsize (Base); + FdtPages = EFI_SIZE_TO_PAGES (FdtSize); + NewBase = AllocatePages (FdtPages); + if (NewBase == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Could not allocate memory for DTB\n", __func__)); + return EFI_UNSUPPORTED; + } + + fdt_open_into (Base, NewBase, EFI_PAGES_TO_SIZE (FdtPages)); + + FdtHobData = BuildGuidHob (&gFdtHobGuid, sizeof *FdtHobData); + if (FdtHobData == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Could not build FDT Hob\n", __func__)); + return EFI_UNSUPPORTED; + } + + *FdtHobData = (UINTN)NewBase; + + BuildFvHob (PcdGet32 (PcdRiscVDxeFvBase), PcdGet32 (PcdRiscVDxeFvSize)); + + return EFI_SUCCESS; +} diff --git a/Platform/Sophgo/SG2042Pkg/Sec/SecMain.c b/Platform/Sophgo/SG2042Pkg/Sec/SecMain.c new file mode 100644 index 000000000000..a02887306127 --- /dev/null +++ b/Platform/Sophgo/SG2042Pkg/Sec/SecMain.c @@ -0,0 +1,116 @@ +/** @file + RISC-V SEC phase module for SG2042 EVB. + + Copyright (c) 2008 - 2023, Intel Corporation. All rights reserved.
+ Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
+ Copyright (c) 2023, Academy of Intelligent Innovation, Shandong Universiy, China.P.R. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "SecMain.h" + +/** + Initialize the memory and CPU, setting the boot mode, and platform + initialization. It also builds the core information HOB. + + @return EFI_SUCCESS Status. +**/ +STATIC +EFI_STATUS +EFIAPI +SecInitializePlatform ( + VOID + ) +{ + EFI_STATUS Status; + + MemoryPeimInitialization (); + + CpuPeimInitialization (); + + // Set the Boot Mode + SetBootMode (BOOT_WITH_FULL_CONFIGURATION); + + Status = PlatformPeimInitialization (); + ASSERT_EFI_ERROR (Status); + + return EFI_SUCCESS; +} + +/** + + Entry point to the C language phase of SEC. After the SEC assembly + code has initialized some temporary memory and set up the stack, + the control is transferred to this function. + + + @param[in] BootHartId Hardware thread ID of boot hart. + @param[in] DeviceTreeAddress Pointer to Device Tree (DTB) +**/ +VOID +NORETURN +EFIAPI +SecStartup ( + IN UINTN BootHartId, + IN VOID *DeviceTreeAddress + ) +{ + EFI_HOB_HANDOFF_INFO_TABLE *HobList; + EFI_RISCV_FIRMWARE_CONTEXT FirmwareContext; + EFI_STATUS Status; + UINT64 UefiMemoryBase; + UINT64 StackBase; + UINT32 StackSize; + + SerialPortInitialize (); + + // + // Report Status Code to indicate entering SEC core + // + DEBUG (( + DEBUG_INFO, + "%a() BootHartId: 0x%x, DeviceTreeAddress=0x%x\n", + __func__, + BootHartId, + DeviceTreeAddress + )); + + FirmwareContext.BootHartId = BootHartId; + FirmwareContext.FlattenedDeviceTree = (UINT64)DeviceTreeAddress; + SetFirmwareContextPointer (&FirmwareContext); + + StackBase = (UINT64)FixedPcdGet32 (PcdTemporaryRamBase); + StackSize = FixedPcdGet32 (PcdTemporaryRamSize); + UefiMemoryBase = StackBase + StackSize - SIZE_32MB; + + // Declare the PI/UEFI memory region + HobList = HobConstructor ( + (VOID *)UefiMemoryBase, + SIZE_32MB, + (VOID *)UefiMemoryBase, + (VOID *)StackBase // The top of the UEFI Memory is reserved for the stacks + ); + PrePeiSetHobList (HobList); + + SecInitializePlatform (); + + BuildStackHob (StackBase, StackSize); + + // + // Process all libraries constructor function linked to SecMain. + // + ProcessLibraryConstructorList (); + + // Assume the FV that contains the SEC (our code) also contains a compressed FV. + Status = DecompressFirstFv (); + ASSERT_EFI_ERROR (Status); + + // Load the DXE Core and transfer control to it + Status = LoadDxeCoreFromFv (NULL, 0); + ASSERT_EFI_ERROR (Status); + // + // Should not come here. + // + UNREACHABLE (); +} diff --git a/Platform/Sophgo/SG2042Pkg/Sec/SecEntry.S b/Platform/Sophgo/SG2042Pkg/Sec/SecEntry.S new file mode 100644 index 000000000000..ee02317a6bc8 --- /dev/null +++ b/Platform/Sophgo/SG2042Pkg/Sec/SecEntry.S @@ -0,0 +1,18 @@ +/* + Copyright (c) 2022 Ventana Micro Systems Inc. + + SPDX-License-Identifier: BSD-2-Clause-Patent + + */ + +#include "SecMain.h" + +ASM_FUNC (_ModuleEntryPoint) + /* Use Temp memory as the stack for calling to C code */ + li a4, FixedPcdGet32 (PcdTemporaryRamBase) + li a5, FixedPcdGet32 (PcdTemporaryRamSize) + + /* Use Temp memory as the stack for calling to C code */ + add sp, a4, a5 + + call SecStartup -- 2.34.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#107890): https://edk2.groups.io/g/devel/message/107890 Mute This Topic: https://groups.io/mt/100840194/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-