From nobody Mon Sep 16 19:09:38 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+114561+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+114561+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1706250678; cv=none; d=zohomail.com; s=zohoarc; b=eEjeR9JxP05IW9LpX12tGl+/ta4yT263cLMX48LyGXgWeN30cwyNCkpMgNhytY2QX3NeFLi9DklnA0SBaDHXClsrHbYvFlMSoLyS/Vj2uOE7gijheUGB5A1l1d4FOd8NbULM6OnG1JWhusOZZwYKo38wn17QTcEdjgPbX7AZQqo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1706250678; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=0DwLVRTZjtHO7fCX876RujMz9aqiEJjQq5Y67kfhncs=; b=MZx6+0LHTHg8pZprwoYUxnZ5mPjePa1BertuwYz8lODn9eNlVwA6K1cUN8dru6FO4TmFIv+OTZBvC/nI0OmXVT/toO7DLd8ARxmvXvRjY7XUNxAj2hghhpVPI899n/T5GTB2ryBO5vvEYL7WIRtXvAT8sMxli8zzoFlaER6A47c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+114561+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 170625067823779.1041927324336; Thu, 25 Jan 2024 22:31:18 -0800 (PST) Return-Path: DKIM-Signature: a=rsa-sha256; bh=ZNpud4xIU1T+DenS93fkE0hf72+UPsYABliAxLtYCR0=; 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=1706250677; v=1; b=nd/S+YtZc155/hkLpOX5NPZWaZmtO/GTxImuNctqjaUDLjGxEnPriVpnDgABI/j/LvZjS3fB UCR2tKMQ2xhDwenb0c7odzEzieUtQcARkCKOSGLpXDPKMLXhbqSTOTRYakw8gjuCO3s25J2s6y6 6ZSoz9z12t3WR9qX7JaDJ7NM= X-Received: by 127.0.0.2 with SMTP id 95rrYY1788612x8xDSXDGdOv; Thu, 25 Jan 2024 22:31:17 -0800 X-Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by mx.groups.io with SMTP id smtpd.web11.10041.1706250676584219036 for ; Thu, 25 Jan 2024 22:31:17 -0800 X-Received: from loongson.cn (unknown [10.2.9.245]) by gateway (Coremail) with SMTP id _____8AxjuuwUbNlDR8GAA--.21396S3; Fri, 26 Jan 2024 14:31:12 +0800 (CST) X-Received: from code-server.gen (unknown [10.2.9.245]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bx7c6uUbNlqnMbAA--.52933S2; Fri, 26 Jan 2024 14:31:10 +0800 (CST) From: "Chao Li" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Laszlo Ersek , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Bibo Mao , Dongyan Qian , Xianglai Li Subject: [edk2-devel] [PATCH v8 35/37] OvmfPkg/LoongArchVirt: Support PEI phase Date: Fri, 26 Jan 2024 14:31:09 +0800 Message-Id: <20240126063109.3103040-1-lichao@loongson.cn> In-Reply-To: <20240126062715.3099433-1-lichao@loongson.cn> References: <20240126062715.3099433-1-lichao@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Bx7c6uUbNlqnMbAA--.52933S2 X-CM-SenderInfo: xolfxt3r6o00pqjv00gofq/1tbiAQAICGWyG+AOvAAIsX X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== 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,lichao@loongson.cn List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 4cPkwtWdd6mMtOq7ZhIFART4x1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1706250678513100001 Content-Type: text/plain; charset="utf-8" Platfrom PEI module for LoongArch platfrom initialization. BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4584 Cc: Ard Biesheuvel Cc: Laszlo Ersek Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Bibo Mao Cc: Dongyan Qian Signed-off-by: Chao Li Co-authored-by: Xianglai Li Co-authored-by: Bibo Mao Reviewed-by: Bibo Mao --- OvmfPkg/LoongArchVirt/PlatformPei/Fv.c | 39 ++ OvmfPkg/LoongArchVirt/PlatformPei/MemDetect.c | 201 +++++++++ OvmfPkg/LoongArchVirt/PlatformPei/Platform.c | 393 ++++++++++++++++++ OvmfPkg/LoongArchVirt/PlatformPei/Platform.h | 146 +++++++ .../LoongArchVirt/PlatformPei/PlatformPei.inf | 72 ++++ 5 files changed, 851 insertions(+) create mode 100644 OvmfPkg/LoongArchVirt/PlatformPei/Fv.c create mode 100644 OvmfPkg/LoongArchVirt/PlatformPei/MemDetect.c create mode 100644 OvmfPkg/LoongArchVirt/PlatformPei/Platform.c create mode 100644 OvmfPkg/LoongArchVirt/PlatformPei/Platform.h create mode 100644 OvmfPkg/LoongArchVirt/PlatformPei/PlatformPei.inf diff --git a/OvmfPkg/LoongArchVirt/PlatformPei/Fv.c b/OvmfPkg/LoongArchVirt= /PlatformPei/Fv.c new file mode 100644 index 0000000000..d46326f135 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/PlatformPei/Fv.c @@ -0,0 +1,39 @@ +/** @file + Build FV related hobs for platform. + + Copyright (c) 2024 Loongson Technology Corporation Limited. All rights r= eserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include "Platform.h" + +/** + Publish PEI & DXE (Decompressed) Memory based FVs to let PEI + and DXE know about them. + + @retval EFI_SUCCESS Platform PEI FVs were initialized successfully. +**/ +EFI_STATUS +PeiFvInitialization ( + VOID + ) +{ + DEBUG ((DEBUG_INFO, "Platform PEI Firmware Volume Initialization\n")); + + // + // Create a memory allocation HOB for the PEI FV. + // + BuildMemoryAllocationHob ( + FixedPcdGet64 (PcdOvmfSecPeiTempRamBase), + FixedPcdGet32 (PcdOvmfSecPeiTempRamSize), + EfiBootServicesData + ); + + return EFI_SUCCESS; +} diff --git a/OvmfPkg/LoongArchVirt/PlatformPei/MemDetect.c b/OvmfPkg/LoongA= rchVirt/PlatformPei/MemDetect.c new file mode 100644 index 0000000000..9c90413524 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/PlatformPei/MemDetect.c @@ -0,0 +1,201 @@ +/** @file + Memory Detection for Virtual Machines. + + Copyright (c) 2024 Loongson Technology Corporation Limited. All rights r= eserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +// +// The package level header files this module uses +// +#include + +// +// The Library classes this module consumes +// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Platform.h" + +#define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS (128) +#define LOONGARCH_FW_RAM_TOP BASE_256MB + +/** + Publish PEI core memory + + @return EFI_SUCCESS The PEIM initialized successfully. +**/ +EFI_STATUS +PublishPeiMemory ( + VOID + ) +{ + EFI_STATUS Status; + UINT64 Base; + UINT64 Size; + UINT64 RamTop; + + // + // Determine the range of memory to use during PEI + // + Base =3D FixedPcdGet64 (PcdOvmfSecPeiTempRamBase) + FixedPcdGet32 (Pcd= OvmfSecPeiTempRamSize); + RamTop =3D LOONGARCH_FW_RAM_TOP; + Size =3D RamTop - Base; + + // + // Publish this memory to the PEI Core + // + Status =3D PublishSystemMemory (Base, Size); + ASSERT_EFI_ERROR (Status); + + DEBUG ((DEBUG_INFO, "Publish Memory Initialize done.\n")); + return Status; +} + +/** + Peform Memory Detection + Publish system RAM and reserve memory regions +**/ +VOID +InitializeRamRegions ( + VOID + ) +{ + EFI_STATUS Status; + FIRMWARE_CONFIG_ITEM FwCfgItem; + UINTN FwCfgSize; + MEMMAP_ENTRY MemoryMapEntry; + MEMMAP_ENTRY *StartEntry; + MEMMAP_ENTRY *pEntry; + UINTN Processed; + + Status =3D QemuFwCfgFindFile ("etc/memmap", &FwCfgItem, &FwCfgSize); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a %d read etc/memmap error Status %d \n", __fun= c__, __LINE__, Status)); + return; + } + + if (FwCfgSize % sizeof MemoryMapEntry !=3D 0) { + DEBUG ((DEBUG_ERROR, "no MemoryMapEntry FwCfgSize:%d\n", FwCfgSize)); + return; + } + + QemuFwCfgSelectItem (FwCfgItem); + StartEntry =3D AllocatePages (EFI_SIZE_TO_PAGES (FwCfgSize)); + QemuFwCfgReadBytes (FwCfgSize, StartEntry); + for (Processed =3D 0; Processed < (FwCfgSize / sizeof MemoryMapEntry); P= rocessed++) { + pEntry =3D StartEntry + Processed; + if (pEntry->Length =3D=3D 0) { + continue; + } + + DEBUG ((DEBUG_INFO, "MemmapEntry Base %p length %p type %d\n", pEntry= ->BaseAddr, pEntry->Length, pEntry->Type)); + if (pEntry->Type !=3D EfiAcpiAddressRangeMemory) { + continue; + } + + AddMemoryRangeHob (pEntry->BaseAddr, pEntry->BaseAddr + pEntry->Length= ); + } + + // + // When 0 address protection is enabled, + // 0-4k memory needs to be preallocated to prevent UEFI applications fro= m allocating use, + // such as grub + // + if (PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) { + BuildMemoryAllocationHob ( + 0, + EFI_PAGE_SIZE, + EfiBootServicesData + ); + } +} + +/** + Gets the Virtual Memory Map of corresponding platforms. + + This Virtual Memory Map is used by initialize the MMU on corresponding + platforms. + + @param[out] MemoryTable Array of MEMORY_REGION_DESCRIPTOR + describing a Physical-to-Virtual Memory + mapping. This array must be ended by a + zero-filled entry. The allocated memory + will not be freed. +**/ +VOID +EFIAPI +GetMemoryMapPolicy ( + OUT MEMORY_REGION_DESCRIPTOR **MemoryTable + ) +{ + EFI_STATUS Status; + FIRMWARE_CONFIG_ITEM FwCfgItem; + UINTN FwCfgSize; + MEMMAP_ENTRY MemoryMapEntry; + MEMMAP_ENTRY *StartEntry; + MEMMAP_ENTRY *pEntry; + UINTN Processed; + MEMORY_REGION_DESCRIPTOR *VirtualMemoryTable; + UINTN Index =3D 0; + + ASSERT (MemoryTable !=3D NULL); + + VirtualMemoryTable =3D AllocatePool ( + sizeof (MEMORY_REGION_DESCRIPTOR) * + MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS + ); + + // + // Add the 0x10000000-0x20000000. In the virtual machine, this area use = for CPU UART, flash, PIC etc. + // + VirtualMemoryTable[Index].PhysicalBase =3D 0x10000000; + VirtualMemoryTable[Index].VirtualBase =3D VirtualMemoryTable[Index].Phy= sicalBase; + VirtualMemoryTable[Index].Length =3D 0x10000000; + VirtualMemoryTable[Index].Attributes =3D PAGE_VALID | PLV_KERNEL | CA= CHE_SUC | PAGE_DIRTY | PAGE_GLOBAL; + ++Index; + + Status =3D QemuFwCfgFindFile ("etc/memmap", &FwCfgItem, &FwCfgSize); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a %d read etc/memmap error Status %d \n", __fun= c__, __LINE__, Status)); + ZeroMem (&VirtualMemoryTable[Index], sizeof (MEMORY_REGION_DESCRIPTOR)= ); + *MemoryTable =3D VirtualMemoryTable; + return; + } + + if (FwCfgSize % sizeof MemoryMapEntry !=3D 0) { + DEBUG ((DEBUG_ERROR, "no MemoryMapEntry FwCfgSize:%d\n", FwCfgSize)); + } + + QemuFwCfgSelectItem (FwCfgItem); + StartEntry =3D AllocatePages (EFI_SIZE_TO_PAGES (FwCfgSize)); + QemuFwCfgReadBytes (FwCfgSize, StartEntry); + for (Processed =3D 0; Processed < (FwCfgSize / sizeof MemoryMapEntry); P= rocessed++) { + pEntry =3D StartEntry + Processed; + if (pEntry->Length =3D=3D 0) { + continue; + } + + DEBUG ((DEBUG_INFO, "MemmapEntry Base %p length %p type %d\n", pEntry= ->BaseAddr, pEntry->Length, pEntry->Type)); + VirtualMemoryTable[Index].PhysicalBase =3D pEntry->BaseAddr; + VirtualMemoryTable[Index].VirtualBase =3D VirtualMemoryTable[Index].P= hysicalBase; + VirtualMemoryTable[Index].Length =3D pEntry->Length; + VirtualMemoryTable[Index].Attributes =3D PAGE_VALID | PLV_KERNEL | = CACHE_CC | PAGE_DIRTY | PAGE_GLOBAL; + ++Index; + } + + FreePages (StartEntry, EFI_SIZE_TO_PAGES (FwCfgSize)); + // End of Table + ZeroMem (&VirtualMemoryTable[Index], sizeof (MEMORY_REGION_DESCRIPTOR)); + *MemoryTable =3D VirtualMemoryTable; +} diff --git a/OvmfPkg/LoongArchVirt/PlatformPei/Platform.c b/OvmfPkg/LoongAr= chVirt/PlatformPei/Platform.c new file mode 100644 index 0000000000..10719c4459 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/PlatformPei/Platform.c @@ -0,0 +1,393 @@ +/** @file + Platform PEI driver + + Copyright (c) 2024 Loongson Technology Corporation Limited. All rights r= eserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + + @par Glossary: + - Mem - Memory +**/ + +// +// The package level header files this module uses +// +#include +// +// The Library classes this module consumes +// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Platform.h" + +VOID +SaveRtcRegisterAddressHob ( + UINT64 RtcRegisterBase + ); + +/* TODO */ +EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation[] =3D { + { EfiReservedMemoryType, 0x004 }, + { EfiRuntimeServicesData, 0x024 }, + { EfiRuntimeServicesCode, 0x030 }, + { EfiBootServicesCode, 0x180 }, + { EfiBootServicesData, 0xF00 }, + { EfiMaxMemoryType, 0x000 } +}; + +// +// Module globals +// +CONST EFI_PEI_PPI_DESCRIPTOR mPpiListBootMode =3D { + (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), + &gEfiPeiMasterBootModePpiGuid, + NULL +}; + +STATIC EFI_BOOT_MODE mBootMode =3D BOOT_WITH_FULL_CONFIGURATION; + +/** + Create Reserved type memory range hand off block. + + @param MemoryBase memory base address. + @param MemoryLimit memory length. + + @return VOID +**/ +VOID +AddReservedMemoryBaseSizeHob ( + EFI_PHYSICAL_ADDRESS MemoryBase, + UINT64 MemorySize + ) +{ + BuildResourceDescriptorHob ( + EFI_RESOURCE_MEMORY_RESERVED, + EFI_RESOURCE_ATTRIBUTE_PRESENT | + EFI_RESOURCE_ATTRIBUTE_INITIALIZED | + EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE | + EFI_RESOURCE_ATTRIBUTE_TESTED, + MemoryBase, + MemorySize + ); +} + +/** + Create system type memory range hand off block. + + @param MemoryBase memory base address. + @param MemoryLimit memory length. + + @return VOID +**/ +VOID +AddMemoryBaseSizeHob ( + EFI_PHYSICAL_ADDRESS MemoryBase, + 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 hand off block. + + @param MemoryBase memory base address. + @param MemoryLimit memory length. + + @return VOID +**/ +VOID +AddMemoryRangeHob ( + EFI_PHYSICAL_ADDRESS MemoryBase, + EFI_PHYSICAL_ADDRESS MemoryLimit + ) +{ + AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase)); +} + +/** + Create memory type information hand off block. + + @param VOID + + @return VOID +**/ +VOID +MemMapInitialization ( + VOID + ) +{ + DEBUG ((DEBUG_INFO, "=3D=3D%a=3D=3D\n", __func__)); + // + // Create Memory Type Information HOB + // + BuildGuidDataHob ( + &gEfiMemoryTypeInformationGuid, + mDefaultMemoryTypeInformation, + sizeof (mDefaultMemoryTypeInformation) + ); +} + +/** Get the Rtc base address from the DT. + + This function fetches the node referenced in the "loongson,ls7a-rtc" + property of the "reg" node and returns the base address of + the RTC. + + @param [in] Fdt Pointer to a Flattened Device Tree (= Fdt). + @param [out] RtcBaseAddress If success, contains the base address + of the Rtc. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND RTC info not found in DT. + @retval EFI_INVALID_PARAMETER Invalid parameter. +**/ +STATIC +EFI_STATUS +EFIAPI +GetRtcAddress ( + IN CONST VOID *Fdt, + OUT UINT64 *RtcBaseAddress + ) +{ + INT32 Node; + INT32 Prev; + CONST CHAR8 *Type; + INT32 Len; + CONST UINT64 *RegProp; + EFI_STATUS Status; + + if ((Fdt =3D=3D NULL) || (fdt_check_header (Fdt) !=3D 0)) { + return EFI_INVALID_PARAMETER; + } + + Status =3D EFI_NOT_FOUND; + for (Prev =3D 0; ; Prev =3D Node) { + Node =3D fdt_next_node (Fdt, Prev, NULL); + if (Node < 0) { + break; + } + + // + // Check for memory node + // + Type =3D fdt_getprop (Fdt, Node, "compatible", &Len); + if ((Type) && (AsciiStrnCmp (Type, "loongson,ls7a-rtc", Len) =3D=3D 0)= ) { + // + // Get the 'reg' property of this node. For now, we will assume + // two 8 byte quantities for base and size, respectively. + // + RegProp =3D fdt_getprop (Fdt, Node, "reg", &Len); + if ((RegProp !=3D 0) && (Len =3D=3D (2 * sizeof (UINT64)))) { + *RtcBaseAddress =3D SwapBytes64 (RegProp[0]); + Status =3D RETURN_SUCCESS; + DEBUG ((DEBUG_INFO, "%a Len %d RtcBase %llx\n", __func__, Len, *Rt= cBaseAddress)); + break; + } else { + DEBUG ((DEBUG_ERROR, "%a: Failed to parse FDT rtc node\n", __func_= _)); + break; + } + } + } + + return Status; +} + +/** + Misc Initialization. + + @param VOID + + @return VOID +**/ +VOID +MiscInitialization ( + VOID + ) +{ + CPUCFG_REG1_INFO_DATA CpucfgReg1Data; + UINT8 CpuPhysMemAddressWidth; + + DEBUG ((DEBUG_INFO, "=3D=3D%a=3D=3D\n", __func__)); + + // + // Get the the CPU physical memory address width. + // + AsmCpucfg (CPUCFG_REG1_INFO, &CpucfgReg1Data.Uint32); + + CpuPhysMemAddressWidth =3D (UINT8)(CpucfgReg1Data.Bits.PALEN + 1); + + // + // Creat CPU HOBs. + // + BuildCpuHob (CpuPhysMemAddressWidth, FixedPcdGet8 (PcdPrePiCpuIoSize)); +} + +/** + add fdt hand off block. + + @param VOID + + @return VOID +**/ +VOID +AddFdtHob ( + VOID + ) +{ + VOID *Base; + VOID *NewBase; + UINTN FdtSize; + UINTN FdtPages; + UINT64 *FdtHobData; + UINT64 RtcBaseAddress; + RETURN_STATUS Status; + + Base =3D (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress); + ASSERT (Base !=3D NULL); + + Status =3D GetRtcAddress (Base, &RtcBaseAddress); + if (RETURN_ERROR (Status)) { + return; + } + + SaveRtcRegisterAddressHob (RtcBaseAddress); + + FdtSize =3D fdt_totalsize (Base) + PcdGet32 (PcdDeviceTreeAllocationPad= ding); + FdtPages =3D EFI_SIZE_TO_PAGES (FdtSize); + NewBase =3D AllocatePages (FdtPages); + ASSERT (NewBase !=3D NULL); + fdt_open_into (Base, NewBase, EFI_PAGES_TO_SIZE (FdtPages)); + + FdtHobData =3D BuildGuidHob (&gFdtHobGuid, sizeof *FdtHobData); + ASSERT (FdtHobData !=3D NULL); + *FdtHobData =3D (UINTN)NewBase; +} + +/** + Fetch the size of system memory from QEMU. + + @param VOID + + @return VOID +**/ +VOID +ReportSystemMemorySize ( + VOID + ) +{ + UINT64 RamSize; + + QemuFwCfgSelectItem (QemuFwCfgItemRamSize); + RamSize =3D QemuFwCfgRead64 (); + DEBUG (( + DEBUG_INFO, + "%a: QEMU reports %dM system memory\n", + __func__, + RamSize/1024/1024 + )); + + // + // Assert false if QEMU report system memory size is less then 256M. + // + if (RamSize <=3D SIZE_256MB) { + ASSERT (FALSE); + } +} + +/** + Perform Platform PEI initialization. + + @param FileHandle Handle of the file being invoked. + @param PeiServices Describes the list of possible PEI Services. + + @return EFI_SUCCESS The PEIM initialized successfully. +**/ +EFI_STATUS +EFIAPI +InitializePlatform ( + IN EFI_PEI_FILE_HANDLE FileHandle, + IN CONST EFI_PEI_SERVICES **PeiServices + ) +{ + EFI_STATUS Status; + UINTN TranslationTableSize; + MEMORY_REGION_DESCRIPTOR *MemoryTable; + + DEBUG ((DEBUG_INFO, "Platform PEIM Loaded\n")); + + Status =3D PeiServicesSetBootMode (mBootMode); + ASSERT_EFI_ERROR (Status); + + Status =3D PeiServicesInstallPpi (&mPpiListBootMode); + ASSERT_EFI_ERROR (Status); + + ReportSystemMemorySize (); + + PublishPeiMemory (); + + PeiFvInitialization (); + InitializeRamRegions (); + MemMapInitialization (); + + Status =3D PlatformHookSerialPortInitialize (); + ASSERT_EFI_ERROR (Status); + + // + // Collect numbers of on line processors and all of APs APIC ID + // TODO: Current, the NULL library is used, this library will be populat= ed in the future. + // + CollectAllProcessorResource (); + + MiscInitialization (); + + AddFdtHob (); + + GetMemoryMapPolicy (&MemoryTable); + Status =3D ConfigureMemoryManagementUnit ( + MemoryTable, + NULL, + &TranslationTableSize + ); + ASSERT_EFI_ERROR (Status); + + // + // Open MMU. + // + DEBUG ((DEBUG_INFO, "Open MMU start.\n")); + CsrXChg (LOONGARCH_CSR_CRMD, BIT4, BIT4|BIT3); + DEBUG ((DEBUG_INFO, "Open MMU done.\n")); + + MpInitLibInitialize (); + + return EFI_SUCCESS; +} diff --git a/OvmfPkg/LoongArchVirt/PlatformPei/Platform.h b/OvmfPkg/LoongAr= chVirt/PlatformPei/Platform.h new file mode 100644 index 0000000000..0774b0e54a --- /dev/null +++ b/OvmfPkg/LoongArchVirt/PlatformPei/Platform.h @@ -0,0 +1,146 @@ +/** @file + Platform PEI module include file. + + Copyright (c) 2024 Loongson Technology Corporation Limited. All rights r= eserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef PLATFORM_H_ +#define PLATFORM_H_ + +#include +#include +#include + +typedef struct { + UINT64 BaseAddr; + UINT64 Length; + UINT32 Type; + UINT32 Reserved; +} MEMMAP_ENTRY; + +VOID +EFIAPI +CollectAllProcessorResource ( + VOID + ); + +VOID +EFIAPI +SaveProcessorResource ( + VOID + ); + +/** + Create system type memory range hand off block. + + @param MemoryBase memory base address. + @param MemoryLimit memory length. + + @return VOID +**/ +VOID +AddMemoryBaseSizeHob ( + EFI_PHYSICAL_ADDRESS MemoryBase, + UINT64 MemorySize + ); + +/** + Create memory range hand off block. + + @param MemoryBase memory base address. + @param MemoryLimit memory length. + + @return VOID +**/ +VOID +AddMemoryRangeHob ( + EFI_PHYSICAL_ADDRESS MemoryBase, + EFI_PHYSICAL_ADDRESS MemoryLimit + ); + +/** + Create Reserved type memory range hand off block. + + @param MemoryBase memory base address. + @param MemoryLimit memory length. + + @return VOID +**/ +VOID +AddReservedMemoryBaseSizeHob ( + EFI_PHYSICAL_ADDRESS MemoryBase, + UINT64 MemorySize + ); + +/** + Publish PEI core memory + + @return EFI_SUCCESS The PEIM initialized successfully. +**/ +EFI_STATUS +PublishPeiMemory ( + VOID + ); + +/** + Publish system RAM and reserve memory regions + + @return VOID +**/ +VOID +InitializeRamRegions ( + VOID + ); + +/** + Publish PEI & DXE (Decompressed) Memory based FVs to let PEI + and DXE know about them. + + @retval EFI_SUCCESS Platform PEI FVs were initialized successfully. +**/ +EFI_STATUS +PeiFvInitialization ( + VOID + ); + +/** + Gets the Virtual Memory Map of corresponding platforms. + + This Virtual Memory Map is used by initialize the MMU on corresponding + platforms. + + @param[out] MemoryTable Array of MEMORY_REGION_DESCRIPTOR + describing a Physical-to-Virtual Memory + mapping. This array must be ended by a + zero-filled entry. The allocated memory + will not be freed. +**/ +VOID +EFIAPI +GetMemoryMapPolicy ( + OUT MEMORY_REGION_DESCRIPTOR **MemoryTable + ); + +/** + Create a page table and initialize the memory management unit(MMU). + + @param[in] MemoryTable A pointer to a memory ragion table. + @param[out] TranslationTableBase A pointer to a translation table base = address. + @param[out] TranslationTableSize A pointer to a translation table base = size. + + @retval EFI_SUCCESS Configure MMU successfully. + EFI_INVALID_PARAMETER MemoryTable is NULL. + EFI_UNSUPPORTED Out of memory space or size not alig= ned. +**/ +EFI_STATUS +EFIAPI +ConfigureMemoryManagementUnit ( + IN MEMORY_REGION_DESCRIPTOR *MemoryTable, + OUT VOID **TranslationTableBase OPTIONAL, + OUT UINTN *TranslationTableSize OPTIONAL + ); + +#endif // PLATFORM_H_ diff --git a/OvmfPkg/LoongArchVirt/PlatformPei/PlatformPei.inf b/OvmfPkg/Lo= ongArchVirt/PlatformPei/PlatformPei.inf new file mode 100644 index 0000000000..e793a4da85 --- /dev/null +++ b/OvmfPkg/LoongArchVirt/PlatformPei/PlatformPei.inf @@ -0,0 +1,72 @@ +## @file +# Platform PEI driver +# +# Copyright (c) 2024 Loongson Technology Corporation Limited. All rights = reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 1.29 + BASE_NAME =3D PlatformPei + FILE_GUID =3D 4c0e81e5-e8e3-4eef-b24b-19b686e9ab53 + MODULE_TYPE =3D PEIM + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D InitializePlatform + +# +# VALID_ARCHITECTURES =3D LOONGARCH64 +# + +[Sources] + Fv.c + MemDetect.c + Platform.c + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + OvmfPkg/OvmfPkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[Ppis] + gEfiPeiMasterBootModePpiGuid + +[Guids] + gEfiMemoryTypeInformationGuid + gFdtHobGuid + +[LibraryClasses] + BaseMemoryLib + CollectApResourceLib + CpuMmuLib + DebugLib + HobLib + IoLib + MemoryAllocationLib + MpInitLib + PcdLib + PeiResourcePublicationLib + PeiServicesLib + PeiServicesTablePointerLib + PeimEntryPoint + PlatformHookLib + QemuFwCfgLib + RealTimeClockLib + TimerLib + +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdDeviceTreeInitialBaseAddress + gUefiOvmfPkgTokenSpaceGuid.PcdDeviceTreeAllocationPadding + gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask + +[FixedPcd] + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize + gEmbeddedTokenSpaceGuid.PcdPrePiCpuIoSize + gUefiCpuPkgTokenSpaceGuid.PcdCpuExceptionVectorBaseAddress + +[Depex] + TRUE --=20 2.27.0 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#114561): https://edk2.groups.io/g/devel/message/114561 Mute This Topic: https://groups.io/mt/103971687/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-