From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by mx.groups.io with SMTP id smtpd.web11.132934.1671109037322333443 for ; Thu, 15 Dec 2022 04:57:17 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@ventanamicro.com header.s=google header.b=XyBFYQWc; spf=pass (domain: ventanamicro.com, ip: 209.85.210.182, mailfrom: sunilvl@ventanamicro.com) Received: by mail-pf1-f182.google.com with SMTP id a14so6646800pfa.1 for ; Thu, 15 Dec 2022 04:57:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LV/Afv9liKEfe4CDWYdVBVVVpWVg55QGBzwk5L1ky5I=; b=XyBFYQWc1hTmVgsCtV5o5kTwpeMPnFjamwY8yce8cocl76+2GgXVNmLaqj0pFLMIos K+7McTNTBvZ2SZdwZTkow+KwWl5eBgphdRzh1fmnozS8pKjTjcE34Inh3bbSWm4HTShR J7tAoJXvCQnl4WFGueDdv168s3Oo1CU7LNE3LhSsP3qa5PEf+iCMrxxOi2R9mCNUf49C WSfHrASRcfhpenLIhtDRLiu1FyojvqSGZIn8OX9CCvc16txlMUrHL32MkhEJibla4LPM b5+96Wl1PdEdSAv/WEQ2mPDLOMo2TNzmcO5e5hpn5ddz8UX1zZMBaG8XL2VwJHzOyVy0 WZ8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LV/Afv9liKEfe4CDWYdVBVVVpWVg55QGBzwk5L1ky5I=; b=BpITyma8bPC0espEC4+jofIQX3Rd+ITZrSRopljAXjVoT9Wqqfq0/xVpU6aQnKeBu9 kqq8frD9kMTkARmQYK+SanZEFmFxshAAmikv5KHZuBDzAyCGEP3OvWoGd6U4kMRnCmR0 0jjQUBWmPg8jQ1pBs0drDkisdHFqEHBQ5dqXh2Z5P7gyysnJW8APOFTOB/J5snsCeCao gDQwMyYr+rfuZV1tlPW4JTPXg+Sq0o75hgbSckDROdwdu5HHtsde5vwRZ4s9rjDcexI+ h0y7D+0dOLeQcVA7aTXT/RUyHyMjm7Wpj0x6PdNxG7fikeEAydXJJLFYzXaCvodoPc/s 6WEw== X-Gm-Message-State: ANoB5pld6RgRNGFTLxI/8yz0RAzL1OYYEY1kWtjhd5yqqTBKtf1jUyK3 r6I3m4gNGVlBJ5+r54Mk0iZx/8vbCVJLtOf5TE0= X-Google-Smtp-Source: AA0mqf4ZwEl0l9cpi2HYxDebQnxcPLUwVccJsRST9dzik7E0AizTCpbSku849FNg/tpTNsWkff6wBg== X-Received: by 2002:a05:6a00:1513:b0:576:f7bd:92d4 with SMTP id q19-20020a056a00151300b00576f7bd92d4mr37604277pfu.30.1671109036403; Thu, 15 Dec 2022 04:57:16 -0800 (PST) Return-Path: Received: from localhost.localdomain ([49.206.11.246]) by smtp.gmail.com with ESMTPSA id j2-20020a625502000000b005762905c89asm1674384pfb.66.2022.12.15.04.57.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Dec 2022 04:57:16 -0800 (PST) From: "Sunil V L" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Daniel Schaefer Subject: [edk2-staging/RiscV64QemuVirt PATCH V6 16/23] OvmfPkg/PlatformInitLib: Add RISC-V instance Date: Thu, 15 Dec 2022 18:26:19 +0530 Message-Id: <20221215125626.545372-17-sunilvl@ventanamicro.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221215125626.545372-1-sunilvl@ventanamicro.com> References: <20221215125626.545372-1-sunilvl@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4076 This is copied from edk2-platforms/Platform/RISC-V/PlatformPkg/Universal/FdtPeim but added as part of library instead of a separate module. Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Daniel Schaefer Signed-off-by: Sunil V L --- OvmfPkg/Library/PlatformInitLib/BaseRiscV64PlatformInitLib.inf | 51 ++++ OvmfPkg/Include/Library/PlatformInitLib.h | 39 +++ OvmfPkg/Library/PlatformInitLib/RiscV64/Cpu.c | 33 +++ OvmfPkg/Library/PlatformInitLib/RiscV64/Memory.c | 263 ++++++++++++++++++++ OvmfPkg/Library/PlatformInitLib/RiscV64/Platform.c | 83 ++++++ 5 files changed, 469 insertions(+) diff --git a/OvmfPkg/Library/PlatformInitLib/BaseRiscV64PlatformInitLib.inf b/OvmfPkg/Library/PlatformInitLib/BaseRiscV64PlatformInitLib.inf new file mode 100644 index 000000000000..323e9b17970f --- /dev/null +++ b/OvmfPkg/Library/PlatformInitLib/BaseRiscV64PlatformInitLib.inf @@ -0,0 +1,51 @@ +## @file +# Platform Initialization Lib for RISC-V +# +# This module provides platform specific functions for RISC-V. +# Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x0001001B + BASE_NAME = BaseRiscV64PlatformInitLib + FILE_GUID = 0D1B3448-4D8E-44EE-80C9-907443047F79 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = PlatformInitLib|SEC PEIM + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = RISCV64 +# + +[Sources] + RiscV64/Platform.c + RiscV64/Cpu.c + RiscV64/Memory.c + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseLib + PcdLib + DebugLib + IoLib + HobLib + RiscVSbiLib + FdtLib + MemoryAllocationLib + +[Guids] + gFdtHobGuid + +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvBaseAddress + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvSize diff --git a/OvmfPkg/Include/Library/PlatformInitLib.h b/OvmfPkg/Include/Library/PlatformInitLib.h index bf6f90a5761c..09fe1d9bf2c0 100644 --- a/OvmfPkg/Include/Library/PlatformInitLib.h +++ b/OvmfPkg/Include/Library/PlatformInitLib.h @@ -291,4 +291,43 @@ PlatformInitEmuVariableNvStore ( IN VOID *EmuVariableNvStore ); +/** + 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 // PLATFORM_INIT_LIB_H_ diff --git a/OvmfPkg/Library/PlatformInitLib/RiscV64/Cpu.c b/OvmfPkg/Library/PlatformInitLib/RiscV64/Cpu.c new file mode 100644 index 000000000000..2c16df697e37 --- /dev/null +++ b/OvmfPkg/Library/PlatformInitLib/RiscV64/Cpu.c @@ -0,0 +1,33 @@ +/** @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 + +**/ + +// +//// The package level header files this module uses +//// +#include + +#include +#include + +/** + Cpu Peim initialization. + +**/ +EFI_STATUS +CpuPeimInitialization ( + VOID + ) +{ + // + // for MMU type >= sv39 + // + BuildCpuHob (56, 32); + + return EFI_SUCCESS; +} diff --git a/OvmfPkg/Library/PlatformInitLib/RiscV64/Memory.c b/OvmfPkg/Library/PlatformInitLib/RiscV64/Memory.c new file mode 100644 index 000000000000..70935b07b56b --- /dev/null +++ b/OvmfPkg/Library/PlatformInitLib/RiscV64/Memory.c @@ -0,0 +1,263 @@ +/** @file + Memory Detection for Virtual Machines. + + Copyright (c) 2021, Hewlett Packard Enterprise Development LP. All rights reserved.
+ Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +Module Name: + + MemDetect.c + +**/ + +// +// 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 + +VOID +BuildMemoryTypeInformationHob ( + VOID + ); + +/** + Build reserved memory range resource HOB. + + @param MemoryBase Reserved memory range base address. + @param MemorySize Reserved memory range size. + +**/ +STATIC +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 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 ( + 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 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 ( + EFI_PHYSICAL_ADDRESS MemoryBase, + EFI_PHYSICAL_ADDRESS MemoryLimit + ) +{ + AddMemoryBaseSizeHob (MemoryBase, (UINT64)(MemoryLimit - MemoryBase)); +} + +/** + Configure MMU +**/ +STATIC +VOID +InitMmu ( + ) +{ + // + // Set supervisor translation mode to Bare mode + // + RiscVSetSupervisorAddressTranslationRegister ((UINT64)SATP_MODE_OFF << 60); + DEBUG ((DEBUG_INFO, "%a: Set Supervisor address mode to bare-metal mode.\n", __FUNCTION__)); +} + +/** + Publish system RAM and reserve memory regions. + +**/ +STATIC +VOID +InitializeRamRegions ( + EFI_PHYSICAL_ADDRESS SystemMemoryBase, + UINT64 SystemMemorySize, + EFI_PHYSICAL_ADDRESS MmodeResvBase, + UINT64 MmodeResvSize + ) +{ + /* + * M-mode FW can be loaded anywhere in memory but should not overlap + * with the EDK2. This can happen if some other boot code loads the + * M-mode firmware. + * + * The M-mode firmware memory should be marked as reserved memory + * so that OS doesn't use it. + */ + DEBUG (( + DEBUG_INFO, + "%a: M-mode FW Memory Start:0x%lx End:0x%lx\n", + __FUNCTION__, + MmodeResvBase, + MmodeResvBase + MmodeResvSize + )); + AddReservedMemoryBaseSizeHob (MmodeResvBase, MmodeResvSize); + + if (MmodeResvBase > SystemMemoryBase) { + AddMemoryRangeHob (SystemMemoryBase, MmodeResvBase); + } + + AddMemoryRangeHob ( + MmodeResvBase + MmodeResvSize, + SystemMemoryBase + SystemMemorySize + ); +} + +/** + Initialize memory hob based on the DTB information. + + @return EFI_SUCCESS The memory hob added successfully. + +**/ +EFI_STATUS +MemoryPeimInitialization ( + VOID + ) +{ + EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext; + CONST UINT64 *RegProp; + CONST CHAR8 *Type; + UINT64 CurBase, CurSize; + INT32 Node, Prev; + INT32 Len; + VOID *FdtPointer; + EFI_PHYSICAL_ADDRESS MmodeResvBase; + UINT64 MmodeResvSize; + + FirmwareContext = NULL; + GetFirmwareContextPointer (&FirmwareContext); + + if (FirmwareContext == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Firmware Context is NULL\n", __FUNCTION__)); + return EFI_UNSUPPORTED; + } + + FdtPointer = (VOID *)FirmwareContext->FlattenedDeviceTree; + if (FdtPointer == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Invalid FDT pointer\n", __FUNCTION__)); + return EFI_UNSUPPORTED; + } + + /* try to locate the reserved memory opensbi node */ + Node = fdt_path_offset (FdtPointer, "/reserved-memory/mmode_resv0"); + if (Node >= 0) { + RegProp = fdt_getprop (FdtPointer, Node, "reg", &Len); + if ((RegProp != 0) && (Len == (2 * sizeof (UINT64)))) { + MmodeResvBase = fdt64_to_cpu (ReadUnaligned64 (RegProp)); + MmodeResvSize = fdt64_to_cpu (ReadUnaligned64 (RegProp + 1)); + } + } + + // 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", + __FUNCTION__, + CurBase, + CurBase + CurSize - 1 + )); + + if ((MmodeResvBase >= CurBase) && ((MmodeResvBase + MmodeResvSize) <= (CurBase + CurSize))) { + InitializeRamRegions ( + CurBase, + CurSize, + MmodeResvBase, + MmodeResvSize + ); + } else { + AddMemoryBaseSizeHob (CurBase, CurSize); + } + } else { + DEBUG (( + DEBUG_ERROR, + "%a: Failed to parse FDT memory node\n", + __FUNCTION__ + )); + } + } + } + + InitMmu (); + + BuildMemoryTypeInformationHob (); + + return EFI_SUCCESS; +} diff --git a/OvmfPkg/Library/PlatformInitLib/RiscV64/Platform.c b/OvmfPkg/Library/PlatformInitLib/RiscV64/Platform.c new file mode 100644 index 000000000000..1556d26426c2 --- /dev/null +++ b/OvmfPkg/Library/PlatformInitLib/RiscV64/Platform.c @@ -0,0 +1,83 @@ +/** @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 + +**/ + +// +//// The package level header files this module uses +//// +#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", __FUNCTION__)); + return EFI_UNSUPPORTED; + } + + FdtPointer = (VOID *)FirmwareContext->FlattenedDeviceTree; + if (FdtPointer == NULL) { + DEBUG ((DEBUG_ERROR, "%a: Invalid FDT pointer\n", __FUNCTION__)); + return EFI_UNSUPPORTED; + } + + DEBUG ((DEBUG_INFO, "%a: Build FDT HOB - FDT at address: 0x%x \n", __FUNCTION__, FdtPointer)); + Base = FdtPointer; + if (fdt_check_header (Base) != 0) { + DEBUG ((DEBUG_ERROR, "%a: Corrupted DTB\n", __FUNCTION__)); + 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", __FUNCTION__)); + 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", __FUNCTION__)); + return EFI_UNSUPPORTED; + } + + *FdtHobData = (UINTN)NewBase; + + BuildFvHob (PcdGet64 (PcdOvmfFvBaseAddress), PcdGet32 (PcdOvmfFvSize)); + + return EFI_SUCCESS; +} -- 2.38.0