[edk2-platforms][PATCH 04/30] RISC-V: Use RISC-V PeiCoreEntryPoint library


Abner Chang
 

1. Use RISC-V PeiCoreEntryPoint library instance for opensbi
to switch to the next phase with arg0 as HART Id and arg1
as the SEC to PEI handoff data.
2. Introduce EDK2 opensbi platform operation functions.
With this, OEM can has its won platform initialization code
before and/or after opensbi vendor platform functions.

Cc: Sunil V L <sunilvl@ventanamicro.com>
Cc: Daniel Schaefer <daniel.schaefer@hpe.com>

Signed-off-by: Abner Chang <abner.chang@hpe.com>
---
.../FreedomU540HiFiveUnleashedBoard/U540.dsc | 3 +-
.../PlatformPkg/Universal/Sec/SecMain.inf | 2 +
.../PlatformPkg/Universal/Sec/SecMain.h | 10 +
.../Include/IndustryStandard/RiscVOpensbi.h | 8 +-
.../Universal/Sec/Edk2OpenSbiPlatform.c | 426 ++++++++++++++++++
.../PlatformPkg/Universal/Sec/SecMain.c | 152 +++++--
6 files changed, 547 insertions(+), 54 deletions(-)
create mode 100644 Platform/RISC-V/PlatformPkg/Universal/Sec/Edk2OpenSbiPl=
atform.c

diff --git a/Platform/SiFive/U5SeriesPkg/FreedomU540HiFiveUnleashedBoard/U5=
40.dsc b/Platform/SiFive/U5SeriesPkg/FreedomU540HiFiveUnleashedBoard/U540.d=
sc
index 5d2ccafaca..be23fc39fd 100644
--- a/Platform/SiFive/U5SeriesPkg/FreedomU540HiFiveUnleashedBoard/U540.dsc
+++ b/Platform/SiFive/U5SeriesPkg/FreedomU540HiFiveUnleashedBoard/U540.dsc
@@ -181,7 +181,6 @@
RiscVFirmwareContextLib|Silicon/RISC-V/ProcessorPkg/Library/RiscVFirmwar=
eContextSscratchLib/RiscVFirmwareContextSscratchLib.inf=0D
PeiServicesLib|MdePkg/Library/PeiServicesLib/PeiServicesLib.inf=0D
MemoryAllocationLib|MdePkg/Library/PeiMemoryAllocationLib/PeiMemoryAlloc=
ationLib.inf=0D
- PeiCoreEntryPoint|MdePkg/Library/PeiCoreEntryPoint/PeiCoreEntryPoint.inf=
=0D
ReportStatusCodeLib|MdeModulePkg/Library/PeiReportStatusCodeLib/PeiRepor=
tStatusCodeLib.inf=0D
OemHookStatusCodeLib|MdeModulePkg/Library/OemHookStatusCodeLibNull/OemHo=
okStatusCodeLibNull.inf=0D
PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeC=
offGetEntryPointLib.inf=0D
@@ -191,6 +190,8 @@
DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf=0D
!endif=0D
PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf=0D
+ # RISC-V platform PEI core entry point.=0D
+ PeiCoreEntryPoint|Platform/RISC-V/PlatformPkg/Library/PeiCoreEntryPoint/=
PeiCoreEntryPoint.inf=0D
=0D
[LibraryClasses.common.PEIM]=0D
HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf=0D
diff --git a/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.inf b/Platfo=
rm/RISC-V/PlatformPkg/Universal/Sec/SecMain.inf
index bcb8b9f908..4207c83413 100644
--- a/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.inf
+++ b/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.inf
@@ -23,11 +23,13 @@
=0D
[Sources]=0D
SecMain.c=0D
+ Edk2OpenSbiPlatform.c=0D
=0D
[Sources.RISCV64]=0D
Riscv64/SecEntry.S=0D
=0D
[Packages]=0D
+ EmbeddedPkg/EmbeddedPkg.dec=0D
MdeModulePkg/MdeModulePkg.dec=0D
MdePkg/MdePkg.dec=0D
Silicon/RISC-V/ProcessorPkg/RiscVProcessorPkg.dec=0D
diff --git a/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.h b/Platform=
/RISC-V/PlatformPkg/Universal/Sec/SecMain.h
index 94ea46263c..c04ddbad7f 100644
--- a/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.h
+++ b/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.h
@@ -28,6 +28,16 @@
#include <Ppi/TemporaryRamDone.h>=0D
#include <Ppi/TemporaryRamSupport.h>=0D
=0D
+int=0D
+SecPostOpenSbiPlatformEarlylInit(=0D
+ IN BOOLEAN ColdBoot=0D
+ );=0D
+=0D
+int=0D
+SecPostOpenSbiPlatformFinalInit (=0D
+ IN BOOLEAN ColdBoot=0D
+ );=0D
+=0D
VOID=0D
SecMachineModeTrapHandler (=0D
IN VOID=0D
diff --git a/Silicon/RISC-V/ProcessorPkg/Include/IndustryStandard/RiscVOpen=
sbi.h b/Silicon/RISC-V/ProcessorPkg/Include/IndustryStandard/RiscVOpensbi.h
index e7ac6d26ee..d639429306 100644
--- a/Silicon/RISC-V/ProcessorPkg/Include/IndustryStandard/RiscVOpensbi.h
+++ b/Silicon/RISC-V/ProcessorPkg/Include/IndustryStandard/RiscVOpensbi.h
@@ -1,7 +1,7 @@
/** @file=0D
SBI inline function calls.=0D
=0D
- Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All right=
s reserved.<BR>=0D
+ Copyright (c) 2021, Hewlett Packard Enterprise Development LP. All right=
s reserved.<BR>=0D
=0D
SPDX-License-Identifier: BSD-2-Clause-Patent=0D
=0D
@@ -46,8 +46,10 @@ typedef struct {
// structure. Referr=
ed by both C code and assembly code.=0D
=0D
typedef struct {=0D
- VOID *PeiServiceTable; // PEI Service table=0D
- UINT64 FlattenedDeviceTree; // Pointer to Flattened Device t=
ree=0D
+ UINT64 BootHartId;=0D
+ VOID *PeiServiceTable; // PEI Service table=0D
+ UINT64 FlattenedDeviceTree; // Pointer to Flattened Devic=
e tree=0D
+ UINT64 SecPeiHandOffData; // This is EFI_SEC_PEI_HAND_O=
FF passed to PEI Core.=0D
EFI_RISCV_FIRMWARE_CONTEXT_HART_SPECIFIC *HartSpecific[RISC_V_MAX_HART_=
SUPPORTED];=0D
} EFI_RISCV_OPENSBI_FIRMWARE_CONTEXT;=0D
=0D
diff --git a/Platform/RISC-V/PlatformPkg/Universal/Sec/Edk2OpenSbiPlatform.=
c b/Platform/RISC-V/PlatformPkg/Universal/Sec/Edk2OpenSbiPlatform.c
new file mode 100644
index 0000000000..79b2f33675
--- /dev/null
+++ b/Platform/RISC-V/PlatformPkg/Universal/Sec/Edk2OpenSbiPlatform.c
@@ -0,0 +1,426 @@
+/*=0D
+ Copyright (c) 2021, Hewlett Packard Enterprise Development LP. All right=
s reserved.<BR>=0D
+=0D
+ SPDX-License-Identifier: BSD-2-Clause-Patent=0D
+=0D
+ */=0D
+=0D
+#include <Library/DebugAgentLib.h>=0D
+#include <Library/DebugLib.h>=0D
+=0D
+#include <libfdt.h>=0D
+#include <sbi/riscv_asm.h>=0D
+#include <sbi/sbi_domain.h>=0D
+#include <sbi/sbi_hartmask.h>=0D
+#include <sbi/sbi_platform.h>=0D
+#include <sbi/sbi_string.h>=0D
+#include <sbi/sbi_math.h>=0D
+#include <sbi_utils/fdt/fdt_domain.h>=0D
+#include <sbi_utils/fdt/fdt_fixup.h>=0D
+#include <sbi_utils/fdt/fdt_helper.h>=0D
+#include <sbi_utils/irqchip/fdt_irqchip.h>=0D
+#include <sbi_utils/serial/fdt_serial.h>=0D
+#include <sbi_utils/timer/fdt_timer.h>=0D
+#include <sbi_utils/ipi/fdt_ipi.h>=0D
+#include <sbi_utils/reset/fdt_reset.h>=0D
+=0D
+#include "SecMain.h"=0D
+=0D
+extern struct sbi_platform_operations platform_ops;=0D
+=0D
+int Edk2OpensbiPlatformEarlyInit (=0D
+ BOOLEAN ColdBoot=0D
+ )=0D
+{=0D
+ int ReturnCode;=0D
+=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.early_init) {=0D
+ ReturnCode =3D platform_ops.early_init (ColdBoot);=0D
+ if (ReturnCode) {=0D
+ return ReturnCode;=0D
+ }=0D
+ }=0D
+ if (ColdBoot =3D=3D TRUE) {=0D
+ return SecPostOpenSbiPlatformEarlylInit(ColdBoot);=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+int Edk2OpensbiPlatformFinalInit (=0D
+ BOOLEAN ColdBoot=0D
+ )=0D
+{=0D
+ int ReturnCode;=0D
+=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.final_init) {=0D
+ ReturnCode =3D platform_ops.final_init (ColdBoot);=0D
+ if (ReturnCode) {=0D
+ return ReturnCode;=0D
+ }=0D
+ }=0D
+ if (ColdBoot =3D=3D TRUE) {=0D
+ return SecPostOpenSbiPlatformFinalInit(ColdBoot);=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+VOID Edk2OpensbiPlatformEarlyExit (=0D
+ VOID=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.early_exit) {=0D
+ return platform_ops.early_exit ();=0D
+ }=0D
+}=0D
+=0D
+/** Platform final exit */=0D
+VOID Edk2OpensbiPlatformFinalExit (=0D
+ VOID=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.early_exit) {=0D
+ return platform_ops.early_exit ();=0D
+ }=0D
+}=0D
+=0D
+/**=0D
+ For platforms that do not implement misa, non-standard=0D
+ methods are needed to determine cpu extension.=0D
+**/=0D
+int Edk2OpensbiPlatforMMISACheckExtension (=0D
+ CHAR8 Extension=0D
+ )=0D
+{=0D
+ if (platform_ops.misa_check_extension) {=0D
+ return platform_ops.misa_check_extension (Extension);=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/**=0D
+ For platforms that do not implement misa, non-standard=0D
+ methods are needed to get MXL field of misa.=0D
+**/=0D
+int Edk2OpensbiPlatforMMISAGetXLEN (VOID)=0D
+{=0D
+ if (platform_ops.misa_get_xlen) {=0D
+ return platform_ops.misa_get_xlen ();=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Get platform specific root domain memory regions */=0D
+struct sbi_domain_memregion *=0D
+Edk2OpensbiPlatformGetMemRegions (VOID)=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.domains_root_regions) {=0D
+ return platform_ops.domains_root_regions ();=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Initialize (or populate) domains for the platform */=0D
+int Edk2OpensbiPlatformDomainsInit (VOID)=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.domains_init) {=0D
+ return platform_ops.domains_init ();=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Write a character to the platform console output */=0D
+VOID Edk2OpensbiPlatformSerialPutc (=0D
+ CHAR8 Ch=0D
+ )=0D
+{=0D
+ if (platform_ops.console_putc) {=0D
+ return platform_ops.console_putc (Ch);=0D
+ }=0D
+}=0D
+=0D
+/** Read a character from the platform console input */=0D
+int Edk2OpensbiPlatformSerialGetc (VOID)=0D
+{=0D
+ if (platform_ops.console_getc) {=0D
+ return platform_ops.console_getc ();=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Initialize the platform console */=0D
+int Edk2OpensbiPlatformSerialInit (VOID)=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.console_init) {=0D
+ return platform_ops.console_init ();=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Initialize the platform interrupt controller for current HART */=0D
+int Edk2OpensbiPlatformIrqchipInit (=0D
+ BOOLEAN ColdBoot=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.irqchip_init) {=0D
+ return platform_ops.irqchip_init (ColdBoot);=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Exit the platform interrupt controller for current HART */=0D
+VOID Edk2OpensbiPlatformIrqchipExit (VOID)=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.irqchip_exit) {=0D
+ return platform_ops.irqchip_exit ();=0D
+ }=0D
+}=0D
+=0D
+/** Send IPI to a target HART */=0D
+VOID Edk2OpensbiPlatformIpiSend (=0D
+ UINT32 TargetHart=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.ipi_send) {=0D
+ return platform_ops.ipi_send (TargetHart);=0D
+ }=0D
+}=0D
+=0D
+/** Clear IPI for a target HART */=0D
+VOID Edk2OpensbiPlatformIpiClear (=0D
+ UINT32 TargetHart=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.ipi_clear) {=0D
+ return platform_ops.ipi_clear (TargetHart);=0D
+ }=0D
+}=0D
+=0D
+/** Initialize IPI for current HART */=0D
+int Edk2OpensbiPlatformIpiInit (=0D
+ BOOLEAN ColdBoot=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.ipi_init) {=0D
+ return platform_ops.ipi_init (ColdBoot);=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Exit IPI for current HART */=0D
+VOID Edk2OpensbiPlatformIpiExit (VOID)=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.ipi_exit) {=0D
+ return platform_ops.ipi_exit ();=0D
+ }=0D
+}=0D
+=0D
+/** Get tlb flush limit value **/=0D
+UINT64 Edk2OpensbiPlatformTlbrFlushLimit (VOID)=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.get_tlbr_flush_limit) {=0D
+ return platform_ops.get_tlbr_flush_limit ();=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Get platform timer value */=0D
+UINT64 Edk2OpensbiPlatformTimerValue (VOID)=0D
+{=0D
+ if (platform_ops.timer_value) {=0D
+ return platform_ops.timer_value ();=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Start platform timer event for current HART */=0D
+VOID Edk2OpensbiPlatformTimerEventStart (=0D
+ UINT64 NextEvent=0D
+ )=0D
+{=0D
+ if (platform_ops.timer_event_start) {=0D
+ return platform_ops.timer_event_start (NextEvent);=0D
+ }=0D
+}=0D
+=0D
+/** Stop platform timer event for current HART */=0D
+VOID Edk2OpensbiPlatformTimerEventStop (VOID)=0D
+{=0D
+ if (platform_ops.timer_event_stop) {=0D
+ return platform_ops.timer_event_stop ();=0D
+ }=0D
+}=0D
+=0D
+/** Initialize platform timer for current HART */=0D
+int Edk2OpensbiPlatformTimerInit (=0D
+ BOOLEAN ColdBoot=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.timer_init) {=0D
+ return platform_ops.timer_init (ColdBoot);=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Exit platform timer for current HART */=0D
+VOID Edk2OpensbiPlatformTimerExit (VOID)=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.timer_exit) {=0D
+ return platform_ops.timer_exit ();=0D
+ }=0D
+}=0D
+=0D
+/** Bringup the given hart */=0D
+int Edk2OpensbiPlatformHartStart (=0D
+ UINT32 HartId,=0D
+ ulong Saddr=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.hart_start) {=0D
+ return platform_ops.hart_start (HartId, Saddr);=0D
+ }=0D
+ return 0;=0D
+}=0D
+/**=0D
+ Stop the current hart from running. This call doesn't expect to=0D
+ return if success.=0D
+**/=0D
+int Edk2OpensbiPlatformHartStop (VOID)=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.hart_stop) {=0D
+ return platform_ops.hart_stop ();=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/**=0D
+ Check whether reset type and reason supported by the platform*=0D
+=0D
+**/=0D
+int Edk2OpensbiPlatformSystemResetCheck (=0D
+ UINT32 ResetType,=0D
+ UINT32 ResetReason=0D
+ )=0D
+{=0D
+ if (platform_ops.system_reset_check) {=0D
+ return platform_ops.system_reset_check (ResetType, ResetReason);=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+/** Reset the platform */=0D
+VOID Edk2OpensbiPlatformSystemReset (=0D
+ UINT32 ResetType,=0D
+ UINT32 ResetReason=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.system_reset) {=0D
+ return platform_ops.system_reset (ResetType, ResetReason);=0D
+ }=0D
+}=0D
+=0D
+/** platform specific SBI extension implementation probe function */=0D
+int Edk2OpensbiPlatformVendorExtCheck (=0D
+ long ExtId=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.vendor_ext_check) {=0D
+ return platform_ops.vendor_ext_check (ExtId);=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+=0D
+/** platform specific SBI extension implementation provider */=0D
+int Edk2OpensbiPlatformVendorExtProvider (=0D
+ long ExtId,=0D
+ long FuncId,=0D
+ const struct sbi_trap_regs *Regs,=0D
+ unsigned long *OutValue,=0D
+ struct sbi_trap_info *OutTrap=0D
+ )=0D
+{=0D
+ DEBUG((DEBUG_INFO, "%a: Entry\n", __FUNCTION__));=0D
+=0D
+ if (platform_ops.vendor_ext_provider) {=0D
+ return platform_ops.vendor_ext_provider (=0D
+ ExtId,=0D
+ FuncId,=0D
+ Regs,=0D
+ OutValue,=0D
+ OutTrap=0D
+ );=0D
+ }=0D
+ return 0;=0D
+}=0D
+=0D
+const struct sbi_platform_operations Edk2OpensbiPlatformOps =3D {=0D
+ .early_init =3D Edk2OpensbiPlatformEarlyInit,=0D
+ .final_init =3D Edk2OpensbiPlatformFinalInit,=0D
+ .early_exit =3D Edk2OpensbiPlatformEarlyExit,=0D
+ .final_exit =3D Edk2OpensbiPlatformFinalExit,=0D
+ .misa_check_extension =3D Edk2OpensbiPlatforMMISACheckExtension,=0D
+ .misa_get_xlen =3D Edk2OpensbiPlatforMMISAGetXLEN,=0D
+ .domains_root_regions =3D Edk2OpensbiPlatformGetMemRegions,=0D
+ .domains_init =3D Edk2OpensbiPlatformDomainsInit,=0D
+ .console_putc =3D Edk2OpensbiPlatformSerialPutc,=0D
+ .console_getc =3D Edk2OpensbiPlatformSerialGetc,=0D
+ .console_init =3D Edk2OpensbiPlatformSerialInit,=0D
+ .irqchip_init =3D Edk2OpensbiPlatformIrqchipInit,=0D
+ .irqchip_exit =3D Edk2OpensbiPlatformIrqchipExit,=0D
+ .ipi_send =3D Edk2OpensbiPlatformIpiSend,=0D
+ .ipi_clear =3D Edk2OpensbiPlatformIpiClear,=0D
+ .ipi_init =3D Edk2OpensbiPlatformIpiInit,=0D
+ .ipi_exit =3D Edk2OpensbiPlatformIpiExit,=0D
+ .get_tlbr_flush_limit =3D Edk2OpensbiPlatformTlbrFlushLimit,=0D
+ .timer_value =3D Edk2OpensbiPlatformTimerValue,=0D
+ .timer_event_stop =3D Edk2OpensbiPlatformTimerEventStop,=0D
+ .timer_event_start =3D Edk2OpensbiPlatformTimerEventStart,=0D
+ .timer_init =3D Edk2OpensbiPlatformTimerInit,=0D
+ .timer_exit =3D Edk2OpensbiPlatformTimerExit,=0D
+ .hart_start =3D Edk2OpensbiPlatformHartStart,=0D
+ .hart_stop =3D Edk2OpensbiPlatformHartStop,=0D
+ .system_reset_check =3D Edk2OpensbiPlatformSystemResetCheck,=0D
+ .system_reset =3D Edk2OpensbiPlatformSystemReset,=0D
+ .vendor_ext_check =3D Edk2OpensbiPlatformVendorExtCheck,=0D
+ .vendor_ext_provider =3D Edk2OpensbiPlatformVendorExtProvider,=0D
+};=0D
diff --git a/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.c b/Platform=
/RISC-V/PlatformPkg/Universal/Sec/SecMain.c
index e88a7b8e80..44984b0078 100644
--- a/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.c
+++ b/Platform/RISC-V/PlatformPkg/Universal/Sec/SecMain.c
@@ -23,6 +23,8 @@
#include <sbi/sbi_ecall.h> // Reference to header file in opensbi=0D
#include <sbi/sbi_trap.h> // Reference to header file in opensbi=0D
=0D
+extern struct sbi_platform_operations Edk2OpensbiPlatformOps;=0D
+=0D
//=0D
// Indicates the boot hart (PcdBootHartId) OpenSBI initialization is done.=
=0D
//=0D
@@ -31,27 +33,6 @@ atomic_t NonBootHartMessageLock =3D ATOMIC_INITIALIZER(0=
);
=0D
typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartinde=
x);=0D
=0D
-STATIC EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mTemporaryRamSupportPpi =3D {=0D
- TemporaryRamMigration=0D
-};=0D
-=0D
-STATIC EFI_PEI_TEMPORARY_RAM_DONE_PPI mTemporaryRamDonePpi =3D {=0D
- TemporaryRamDone=0D
-};=0D
-=0D
-STATIC EFI_PEI_PPI_DESCRIPTOR mPrivateDispatchTable[] =3D {=0D
- {=0D
- EFI_PEI_PPI_DESCRIPTOR_PPI,=0D
- &gEfiTemporaryRamSupportPpiGuid,=0D
- &mTemporaryRamSupportPpi=0D
- },=0D
- {=0D
- (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),=
=0D
- &gEfiTemporaryRamDonePpiGuid,=0D
- &mTemporaryRamDonePpi=0D
- },=0D
-};=0D
-=0D
/**=0D
Locates a section within a series of sections=0D
with the specified section type.=0D
@@ -491,6 +472,91 @@ RegisterFirmwareSbiExtension (
=0D
return EFI_SUCCESS;=0D
}=0D
+=0D
+/**=0D
+ OpenSBI platform early init hook.=0D
+=0D
+**/=0D
+int=0D
+SecPostOpenSbiPlatformEarlylInit(=0D
+ IN BOOLEAN ColdBoot=0D
+ )=0D
+{=0D
+ //=0D
+ // Boot HART is already in the process of OpenSBI initialization.=0D
+ // We can let other HART to keep booting.=0D
+ //=0D
+ DEBUG ((DEBUG_INFO, "%a: Set boot hart done.\n", __FUNCTION__));=0D
+ atomic_write (&BootHartDone, (UINT64)TRUE);=0D
+ return 0;=0D
+}=0D
+=0D
+/**=0D
+ OpenSBI platform final init hook.=0D
+ We restore the next_arg1 to the pointer of EFI_RISCV_OPENSBI_FIRMWARE_CO=
NTEXT.=0D
+=0D
+**/=0D
+int=0D
+SecPostOpenSbiPlatformFinalInit (=0D
+ IN BOOLEAN ColdBoot=0D
+ )=0D
+{=0D
+ UINT32 HartId;=0D
+ struct sbi_scratch *SbiScratch;=0D
+ struct sbi_scratch *ScratchSpace;=0D
+ struct sbi_platform *SbiPlatform;=0D
+ EFI_RISCV_OPENSBI_FIRMWARE_CONTEXT *FirmwareContext;=0D
+=0D
+ DEBUG((DEBUG_INFO, "%a: Entry, preparing to jump to PEI Core\n\n", __FUN=
CTION__));=0D
+=0D
+ SbiScratch =3D sbi_scratch_thishart_ptr();=0D
+ SbiPlatform =3D (struct sbi_platform *)sbi_platform_ptr(SbiScratch);=0D
+ FirmwareContext =3D (EFI_RISCV_OPENSBI_FIRMWARE_CONTEXT *)SbiPlatform->f=
irmware_context;=0D
+=0D
+ //=0D
+ // Print out scratch address of each hart=0D
+ //=0D
+ DEBUG ((DEBUG_INFO, "%a: OpenSBI scratch address for each hart:\n", __FU=
NCTION__));=0D
+ for (HartId =3D 0; HartId < SBI_HARTMASK_MAX_BITS; HartId ++) {=0D
+ if (sbi_platform_hart_invalid(SbiPlatform, HartId)) {=0D
+ continue;=0D
+ }=0D
+ ScratchSpace =3D sbi_hartid_to_scratch (HartId);=0D
+ if(ScratchSpace !=3D NULL) {=0D
+ DEBUG((DEBUG_INFO, " Hart %d: 0x%x\n", HartId, ScratchSpace=
));=0D
+ } else {=0D
+ DEBUG((DEBUG_INFO, " Hart %d not initialized yet\n", HartId=
));=0D
+ }=0D
+ }=0D
+=0D
+ //=0D
+ // Set firmware context Hart-specific pointer=0D
+ //=0D
+ for (HartId =3D 0; HartId < SBI_HARTMASK_MAX_BITS; HartId ++) {=0D
+ if (sbi_platform_hart_invalid(SbiPlatform, HartId)) {=0D
+ continue;=0D
+ }=0D
+ ScratchSpace =3D sbi_hartid_to_scratch (HartId);=0D
+ if (ScratchSpace !=3D NULL) {=0D
+ FirmwareContext->HartSpecific[HartId] =3D=0D
+ (EFI_RISCV_FIRMWARE_CONTEXT_HART_SPECIFIC *)((UINT8 *)ScratchSpace=
- FIRMWARE_CONTEXT_HART_SPECIFIC_SIZE);=0D
+ DEBUG ((DEBUG_INFO, "%a: OpenSBI Hart %d Firmware Context Hart-spe=
cific at address: 0x%x\n",=0D
+ __FUNCTION__,=0D
+ HartId,=0D
+ FirmwareContext->HartSpecific [HartId]=0D
+ ));=0D
+ }=0D
+ }=0D
+=0D
+ DEBUG((DEBUG_INFO, "%a: Jump to PEI Core with \n", __FUNCTION__));=0D
+ DEBUG((DEBUG_INFO, " sbi_scratch =3D %x\n", SbiScratch));=0D
+ DEBUG((DEBUG_INFO, " sbi_platform =3D %x\n", SbiPlatform));=0D
+ DEBUG((DEBUG_INFO, " FirmwareContext =3D %x\n", FirmwareContext));=0D
+ SbiScratch->next_arg1 =3D (unsigned long)FirmwareContext;=0D
+=0D
+ return 0;=0D
+}=0D
+=0D
/** Transion from SEC phase to PEI phase.=0D
=0D
This function transits to S-mode PEI phase from M-mode SEC phase.=0D
@@ -508,9 +574,7 @@ VOID EFIAPI PeiCore (
EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;=0D
EFI_FIRMWARE_VOLUME_HEADER *BootFv =3D (EFI_FIRMWARE_VOLUME_HEADER *)Fix=
edPcdGet32(PcdRiscVPeiFvBase);=0D
EFI_RISCV_OPENSBI_FIRMWARE_CONTEXT FirmwareContext;=0D
- struct sbi_scratch *ScratchSpace;=0D
struct sbi_platform *ThisSbiPlatform;=0D
- UINT32 HartId;=0D
=0D
FindAndReportEntryPoints (&BootFv, &PeiCoreEntryPoint);=0D
=0D
@@ -524,17 +588,6 @@ VOID EFIAPI PeiCore (
SecCoreData.StackBase =3D (UINT8 *)SecCoreData.TemporaryRam=
Base + (SecCoreData.TemporaryRamSize >> 1);=0D
SecCoreData.StackSize =3D SecCoreData.TemporaryRamSize >> 1=
;=0D
=0D
- //=0D
- // Print out scratch address of each hart=0D
- //=0D
- DEBUG ((DEBUG_INFO, "%a: OpenSBI scratch address for each hart:\n", __FU=
NCTION__));=0D
- for (HartId =3D 0; HartId < SBI_HARTMASK_MAX_BITS; HartId ++) {=0D
- ScratchSpace =3D sbi_hartid_to_scratch (HartId);=0D
- if(ScratchSpace !=3D NULL) {=0D
- DEBUG((DEBUG_INFO, " Hart %d: 0x%x\n", HartId, ScratchSpace=
));=0D
- }=0D
- }=0D
-=0D
//=0D
// Set up OpepSBI firmware context pointer on boot hart OpenSbi scratch.=
=0D
// Firmware context residents in stack and will be switched to memory wh=
en=0D
@@ -564,20 +617,10 @@ VOID EFIAPI PeiCore (
FirmwareContext.FlattenedDeviceTree =3D Scratch->next_arg1;=0D
=0D
//=0D
- // Set firmware context Hart-specific pointer=0D
+ // Transfer the control to the PEI core=0D
//=0D
- for (HartId =3D 0; HartId < SBI_HARTMASK_MAX_BITS; HartId ++) {=0D
- ScratchSpace =3D sbi_hartid_to_scratch (HartId);=0D
- if (ScratchSpace !=3D NULL) {=0D
- FirmwareContext.HartSpecific[HartId] =3D=0D
- (EFI_RISCV_FIRMWARE_CONTEXT_HART_SPECIFIC *)((UINT8 *)ScratchSpace=
- FIRMWARE_CONTEXT_HART_SPECIFIC_SIZE);=0D
- DEBUG ((DEBUG_INFO, "%a: OpenSBI Hart %d Firmware Context Hart-spe=
cific at address: 0x%x\n",=0D
- __FUNCTION__,=0D
- HartId,=0D
- FirmwareContext.HartSpecific [HartId]=0D
- ));=0D
- }=0D
- }=0D
+ FirmwareContext.SecPeiHandOffData =3D (UINT64)&SecCoreData;=0D
+=0D
//=0D
// Set supervisor translation mode to Bare mode=0D
//=0D
@@ -585,13 +628,12 @@ VOID EFIAPI PeiCore (
RiscVSetSupervisorAddressTranslationRegister ((UINT64)RISCV_SATP_MODE_OF=
F << RISCV_SATP_MODE_BIT_POSITION);=0D
=0D
//=0D
- // Transfer the control to the PEI core=0D
+ // Scratch->next_arg1 is the device tree.=0D
//=0D
- Scratch->next_addr =3D (UINTN)(*PeiCoreEntryPoint);=0D
+ Scratch->next_addr =3D (UINTN)(PeiCoreEntryPoint);=0D
Scratch->next_mode =3D PRV_S;=0D
DEBUG ((DEBUG_INFO, "%a: Initializing OpenSBI library for booting hart %=
d\n", __FUNCTION__, BootHartId));=0D
sbi_init(Scratch);=0D
- (*PeiCoreEntryPoint) (&SecCoreData, (EFI_PEI_PPI_DESCRIPTOR *)&mPrivateD=
ispatchTable);=0D
}=0D
=0D
/**=0D
@@ -715,6 +757,7 @@ VOID EFIAPI SecCoreStartUpWithStack(
{=0D
UINT64 BootHartDoneSbiInit;=0D
UINT64 NonBootHartMessageLockValue;=0D
+ struct sbi_platform *ThisSbiPlatform;=0D
EFI_RISCV_FIRMWARE_CONTEXT_HART_SPECIFIC *HartFirmwareContext;=0D
=0D
Scratch->next_arg1 =3D (unsigned long)GetDeviceTreeAddress ();=0D
@@ -737,6 +780,14 @@ VOID EFIAPI SecCoreStartUpWithStack(
HartFirmwareContext->MachineImplId.Value64_H =3D 0;=0D
HartFirmwareContext->HartSwitchMode =3D RiscVOpenSbiHartSwitchMode;=0D
=0D
+ //=0D
+ // Hook platorm_ops with EDK2 one. Thus we can have interface=0D
+ // call out to OEM EDK2 platform code in M-mode before switching=0D
+ // to S-mode in opensbo init.=0D
+ //=0D
+ ThisSbiPlatform =3D (struct sbi_platform *)sbi_platform_ptr(Scratch);=0D
+ ThisSbiPlatform->platform_ops_addr =3D (unsigned long)&Edk2OpensbiPlatfo=
rmOps;=0D
+=0D
if (HartId =3D=3D FixedPcdGet32(PcdBootHartId)) {=0D
LaunchPeiCore (HartId, Scratch);=0D
}=0D
@@ -768,6 +819,7 @@ VOID EFIAPI SecCoreStartUpWithStack(
// Non boot hart wiil be halted waiting for SBI_HART_STARTING.=0D
// Use HSM ecall to start non boot hart (SBI_EXT_HSM_HART_START) later o=
n,=0D
//=0D
+ Scratch->next_mode =3D PRV_S;=0D
sbi_init(Scratch);=0D
}=0D
=0D
--=20
2.31.1