From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from loongson.cn (loongson.cn [114.242.206.163]) by mx.groups.io with SMTP id smtpd.web10.3563.1668157958579434487 for ; Fri, 11 Nov 2022 01:12:39 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: loongson.cn, ip: 114.242.206.163, mailfrom: lixianglai@loongson.cn) Received: from loongson.cn (unknown [10.2.5.185]) by gateway (Coremail) with SMTP id _____8CxjdoEEm5jcxMGAA--.18812S3; Fri, 11 Nov 2022 17:12:36 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxFlcAEm5jXc0QAA--.27651S4; Fri, 11 Nov 2022 17:12:34 +0800 (CST) From: "xianglai" To: devel@edk2.groups.io Cc: Bibo Mao , Chao Li , Leif Lindholm , Liming Gao , Michael D Kinney Subject: [edk2-platforms][PATCH V5 02/15] Platform/Loongson: Support SEC Date: Fri, 11 Nov 2022 17:12:17 +0800 Message-Id: <5808d9638bcee3d0d32626ced147f47914ab4284.1668157715.git.lixianglai@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 X-CM-TRANSID: AQAAf8BxFlcAEm5jXc0QAA--.27651S4 X-CM-SenderInfo: 5ol0xt5qjotxo6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBjvAXoWftr45WFWrtryrJFW8GrWktFb_yoW5CrWDJo WxWFWIkw48Gr1rX3WUGrnrJrW0yF1rXa15tr1rX34DGF4jyF1DKa95J3srGw15Jw15J3Z8 G34rGaykJrW7ta4kn29KB7ZKAUJUUUU7529EdanIXcx71UUUUU7KY7ZEXasCq-sGcSsGvf J3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnRJU UUyS1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64 kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW8JVW5JwA2z4x0Y4vE2Ix0cI8IcVCY 1x0267AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7 CjxVAFwI0_Cr1j6rxdM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc80 4VCY07AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VCjz48v1sIEY2 0_WwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64vIr41l 42xK82IY6x8ErcxFaVAv8VWrMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v26r 1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CE b7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1I6r4UMIIF0x vE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAI cVC2z280aVAFwI0_Cr0_Gr1UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxh VjvjDU0xZFpf9x0zRByIUUUUUU= Content-Transfer-Encoding: quoted-printable Add SEC Code And Readme.md for LoongArchQemu=0D =0D REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4054=0D =0D Cc: Bibo Mao =0D Cc: Chao Li =0D Cc: Leif Lindholm =0D Cc: Liming Gao =0D Cc: Michael D Kinney =0D Signed-off-by: xianglai li =0D Signed-off-by: xianglai li =0D ---=0D .../Include/LoongArchQemuPlatform.h | 2 +-=0D .../Loongson/LoongArchQemuPkg/Loongson.dec | 38 ++=0D .../Loongson/LoongArchQemuPkg/Loongson.dsc | 122 +++++=0D .../Loongson/LoongArchQemuPkg/Loongson.fdf | 53 ++=0D .../LoongArchQemuPkg/Loongson.fdf.inc | 21 +=0D .../LoongArchQemuPkg/Sec/LoongArch64/Start.S | 84 +++=0D .../Loongson/LoongArchQemuPkg/Sec/SecMain.c | 494 ++++++++++++++++++=0D .../Loongson/LoongArchQemuPkg/Sec/SecMain.inf | 51 ++=0D 8 files changed, 864 insertions(+), 1 deletion(-)=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Loongson.dec=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Loongson.dsc=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Loongson.fdf=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Loongson.fdf.inc=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Sec/LoongArch64/Star= t.S=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Sec/SecMain.c=0D create mode 100644 Platform/Loongson/LoongArchQemuPkg/Sec/SecMain.inf=0D =0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Include/LoongArchQemuPlatfo= rm.h b/Platform/Loongson/LoongArchQemuPkg/Include/LoongArchQemuPlatform.h=0D index e942e6a994..d003b9013d 100644=0D --- a/Platform/Loongson/LoongArchQemuPkg/Include/LoongArchQemuPlatform.h=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Include/LoongArchQemuPlatform.h=0D @@ -92,4 +92,4 @@=0D #define UART_BASE_ADDRESS (0x1fe001e0)=0D #define UART_BPS (115200)=0D #define UART_WAIT_TIMOUT (1000000)=0D -#endif=0D +#endif // LOONGARCH_QEMU_PLATFORM_H_=0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dec b/Platform/Loo= ngson/LoongArchQemuPkg/Loongson.dec=0D new file mode 100644=0D index 0000000000..61f600b20d=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dec=0D @@ -0,0 +1,38 @@=0D +## @file=0D +#=0D +# Copyright (c) 2022 Loongson Technology Corporation Limited. All rights = reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + DEC_SPECIFICATION =3D 0x00010005=0D + PACKAGE_NAME =3D LoongArchQemuPkg=0D + PACKAGE_GUID =3D b51d765a-41da-45fc-a537-de3ee785c0f6= =0D + PACKAGE_VERSION =3D 0.1=0D +=0D +##########################################################################= ######=0D +#=0D +# Include Section - list of Include Paths that are provided by this packag= e.=0D +# Comments are used for Keywords and Module Types.=0D +#=0D +# Supported Module Types:=0D +# BASE SEC PEI_CORE PEIM DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_D= RIVER=0D +# DXE_SAL_DRIVER UEFI_DRIVER UEFI_APPLICATION=0D +#=0D +##########################################################################= ######=0D +[Includes.common]=0D + Include # Root include for the package=0D +=0D +[Guids]=0D + gLoongArchQemuPkgTokenSpaceGuid =3D { 0x0e0383ce, 0x0151, 0x4d01, { 0x8= 0, 0x0e, 0x3f, 0xef, 0x8b, 0x27, 0x6d, 0x52 } }=0D +=0D +## In the PcdsFixedAtBuild and PcdsDynamic areas, numbers start at 0x0.=0D +[PcdsFixedAtBuild, PcdsDynamic]=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdFlashPeiFvBase|0x0|UINT64|0x00000000= =0D + gLoongArchQemuPkgTokenSpaceGuid.PcdFlashPeiFvSize|0x0|UINT32|0x00000001= =0D + gLoongArchQemuPkgTokenSpaceGuid.PcdSecPeiTempRamBase|0|UINT64|0x0000000b= =0D + gLoongArchQemuPkgTokenSpaceGuid.PcdSecPeiTempRamSize|0|UINT32|0x0000000c= =0D + gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvBase|0x0|UINT64|0x0000000f= =0D + gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvSize|0x0|UINT32|0x00000010= =0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc b/Platform/Loo= ngson/LoongArchQemuPkg/Loongson.dsc=0D new file mode 100644=0D index 0000000000..b506f70625=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc=0D @@ -0,0 +1,122 @@=0D +## @file=0D +#=0D +# Copyright (c) 2022 Loongson Technology Corporation Limited. All rights = reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +##########################################################################= ######=0D +#=0D +# Defines Section - statements that will be processed to create a Makefile= .=0D +#=0D +##########################################################################= #####=0D +[Defines]=0D + PLATFORM_NAME =3D LoongArchQemu=0D + PLATFORMPKG_NAME =3D LoongArchQemu=0D + PLATFORM_GUID =3D 7926ea52-b0dc-4ee8-ac63-341eebd84ed4= =0D + PLATFORM_VERSION =3D 0.1=0D + DSC_SPECIFICATION =3D 0x00010005=0D + OUTPUT_DIRECTORY =3D Build/$(PLATFORM_NAME)=0D + SUPPORTED_ARCHITECTURES =3D LOONGARCH64=0D + BUILD_TARGETS =3D DEBUG|RELEASE=0D + SKUID_IDENTIFIER =3D DEFAULT=0D + FLASH_DEFINITION =3D Platform/Loongson/LoongArchQemuPkg/Lo= ongson.fdf=0D + TTY_TERMINAL =3D FALSE=0D +=0D +##########################################################################= ##=0D +#=0D +# Defines for default states. These can be changed on the command line.=0D +# -D FLAG=3DVALUE=0D +##########################################################################= ##=0D +[BuildOptions]=0D + GCC:RELEASE_*_*_CC_FLAGS =3D -DSPEEDUP=0D +=0D + #=0D + # Disable deprecated APIs.=0D + #=0D + GCC:*_*_*_CC_FLAGS =3D -D DISABLE_NEW_DEPRECATED_INTERFACES=0D +=0D +[BuildOptions.LOONGARCH64.EDKII.SEC]=0D + *_*_*_CC_FLAGS =3D=0D +=0D +[BuildOptions.common.EDKII.DXE_CORE,BuildOptions.common.EDKII.DXE_DRIVER,B= uildOptions.common.EDKII.UEFI_DRIVER,BuildOptions.common.EDKII.UEFI_APPLICA= TION]=0D + GCC:*_*_*_DLINK_FLAGS =3D -z common-page-size=3D0x1000=0D +=0D +[BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER]=0D + GCC:*_*_LOONGARCH64_DLINK_FLAGS =3D -z common-page-size=3D0x10000=0D +=0D +##########################################################################= ######=0D +#=0D +# Library Class section - list of all Library Classes needed by this Platf= orm.=0D +#=0D +##########################################################################= ######=0D +=0D +!include MdePkg/MdeLibs.dsc.inc=0D +=0D +[LibraryClasses.common]=0D + PcdLib | MdePkg/Library/BasePcdLibNull/BasePcd= LibNull.inf=0D + PrintLib | MdePkg/Library/BasePrintLib/BasePrint= Lib.inf=0D + BaseMemoryLib | MdePkg/Library/BaseMemoryLib/BaseMemo= ryLib.inf=0D + BaseLib | MdePkg/Library/BaseLib/BaseLib.inf=0D + PeCoffLib | MdePkg/Library/BasePeCoffLib/BasePeCo= ffLib.inf=0D + PeCoffGetEntryPointLib | MdePkg/Library/BasePeCoffGetEntryPoin= tLib/BasePeCoffGetEntryPointLib.inf=0D + IoLib | MdePkg/Library/BaseIoLibIntrinsic/Bas= eIoLibIntrinsic.inf=0D + SerialPortLib | Platform/Loongson/LoongArchQemuPkg/Li= brary/SerialPortLib/SerialPortLib.inf=0D + DebugPrintErrorLevelLib | MdePkg/Library/BaseDebugPrintErrorLev= elLib/BaseDebugPrintErrorLevelLib.inf=0D + PeCoffExtraActionLib | MdePkg/Library/BasePeCoffExtraActionL= ibNull/BasePeCoffExtraActionLibNull.inf=0D + DebugAgentLib | MdeModulePkg/Library/DebugAgentLibNul= l/DebugAgentLibNull.inf=0D +=0D +##########################################################################= ######=0D +#=0D +# Pcd Section - list of all EDK II PCD Entries defined by this Platform.=0D +#=0D +##########################################################################= ######=0D +[PcdsFixedAtBuild]=0D +## BaseLib ##=0D + gEfiMdePkgTokenSpaceGuid.PcdMaximumUnicodeStringLength | 1= 000000=0D + gEfiMdePkgTokenSpaceGuid.PcdMaximumAsciiStringLength | 1= 000000=0D + gEfiMdePkgTokenSpaceGuid.PcdMaximumLinkedListLength | 1= 000000=0D +=0D + gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel | 0= x8000004F=0D + # DEBUG_INIT 0x00000001 // Initialization=0D + # DEBUG_WARN 0x00000002 // Warnings=0D + # DEBUG_LOAD 0x00000004 // Load events=0D + # DEBUG_FS 0x00000008 // EFI File system=0D + # DEBUG_POOL 0x00000010 // Alloc & Free (pool)=0D + # DEBUG_PAGE 0x00000020 // Alloc & Free (page)=0D + # DEBUG_INFO 0x00000040 // Informational debug messages=0D + # DEBUG_DISPATCH 0x00000080 // PEI/DXE/SMM Dispatchers=0D + # DEBUG_VARIABLE 0x00000100 // Variable=0D + # DEBUG_BM 0x00000400 // Boot Manager=0D + # DEBUG_BLKIO 0x00001000 // BlkIo Driver=0D + # DEBUG_NET 0x00004000 // Network Io Driver=0D + # DEBUG_UNDI 0x00010000 // UNDI Driver=0D + # DEBUG_LOADFILE 0x00020000 // LoadFile=0D + # DEBUG_EVENT 0x00080000 // Event messages=0D + # DEBUG_GCD 0x00100000 // Global Coherency Database changes=0D + # DEBUG_CACHE 0x00200000 // Memory range cachability changes=0D + # DEBUG_VERBOSE 0x00400000 // Detailed debug messages that may=0D + # DEBUG_ERROR 0x80000000 // Error=0D +=0D +!if $(TARGET) =3D=3D RELEASE=0D + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask | 0= x21=0D +!else=0D + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask | 0= x2f=0D +!endif=0D + # DEBUG_ASSERT_ENABLED 0x01=0D + # DEBUG_PRINT_ENABLED 0x02=0D + # DEBUG_CODE_ENABLED 0x04=0D + # CLEAR_MEMORY_ENABLED 0x08=0D + # ASSERT_BREAKPOINT_ENABLED 0x10=0D + # ASSERT_DEADLOOP_ENABLED 0x20=0D +=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdSecPeiTempRamBase | 0= x10000=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdSecPeiTempRamSize | 0= x10000=0D +=0D +[Components]=0D +=0D + #=0D + # SEC Phase modules=0D + #=0D + Platform/Loongson/LoongArchQemuPkg/Sec/SecMain.inf=0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf b/Platform/Loo= ngson/LoongArchQemuPkg/Loongson.fdf=0D new file mode 100644=0D index 0000000000..9685795cda=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf=0D @@ -0,0 +1,53 @@=0D +## @file=0D +#=0D +# Copyright (c) 2022 Loongson Technology Corporation Limited. All rights = reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +##########################################################################= ###########################=0D +[Defines]=0D +!include Loongson.fdf.inc=0D +=0D +##########################################################################= ###########################=0D +[FD.QEMU_EFI]=0D +BaseAddress =3D $(FD_BASE_ADDRESS)=0D +Size =3D $(FD_SIZE)=0D +ErasePolarity =3D 1=0D +BlockSize =3D $(BLOCK_SIZE)=0D +NumBlocks =3D $(FD_BLOCKS)=0D +=0D +$(SECFV_OFFSET)|$(SECFV_SIZE)=0D +gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvBase|gLoongArchQemuPkgTokenSp= aceGuid.PcdFlashSecFvSize=0D +FV =3D SECFV=0D +=0D +##########################################################################= ###########################=0D +[FV.SECFV]=0D +FvNameGuid =3D 587d4265-5e71-41da-9c35-4258551f1e22=0D +BlockSize =3D $(BLOCK_SIZE)=0D +FvAlignment =3D 16=0D +ERASE_POLARITY =3D 1=0D +MEMORY_MAPPED =3D TRUE=0D +STICKY_WRITE =3D TRUE=0D +LOCK_CAP =3D TRUE=0D +LOCK_STATUS =3D TRUE=0D +WRITE_DISABLED_CAP =3D TRUE=0D +WRITE_ENABLED_CAP =3D TRUE=0D +WRITE_STATUS =3D TRUE=0D +WRITE_LOCK_CAP =3D TRUE=0D +WRITE_LOCK_STATUS =3D TRUE=0D +READ_DISABLED_CAP =3D TRUE=0D +READ_ENABLED_CAP =3D TRUE=0D +READ_STATUS =3D TRUE=0D +READ_LOCK_CAP =3D TRUE=0D +READ_LOCK_STATUS =3D TRUE=0D +=0D +INF Platform/Loongson/LoongArchQemuPkg/Sec/SecMain.inf=0D +=0D +##########################################################################= ###########################=0D +[Rule.Common.SEC]=0D + FILE SEC =3D $(NAMED_GUID) {=0D + TE TE Align =3D Auto $(INF_OUTPUT)/$(MODULE_NAME).efi=0D + UI STRING =3D"$(MODULE_NAME)" Optional=0D + }=0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf.inc b/Platform= /Loongson/LoongArchQemuPkg/Loongson.fdf.inc=0D new file mode 100644=0D index 0000000000..6f17909748=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf.inc=0D @@ -0,0 +1,21 @@=0D +## @file=0D +#=0D +# Copyright (c) 2022 Loongson Technology Corporation Limited. All rights = reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +DEFINE BLOCK_SIZE =3D 0x1000=0D +=0D +##########################################################################= ##=0D +# fd total=0D +DEFINE FD_BASE_ADDRESS =3D 0x1c000000=0D +DEFINE FD_BLOCKS =3D 0x400=0D +DEFINE FD_SIZE =3D 0x400000=0D +=0D +##########################################################################= ##=0D +#flash code layout=0D +#Set Sec base address and size in flash=0D +DEFINE SECFV_OFFSET =3D 0x00000000=0D +DEFINE SECFV_SIZE =3D 0x00010000=0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Sec/LoongArch64/Start.S b/P= latform/Loongson/LoongArchQemuPkg/Sec/LoongArch64/Start.S=0D new file mode 100644=0D index 0000000000..5d7ce313c0=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Sec/LoongArch64/Start.S=0D @@ -0,0 +1,84 @@=0D +#-------------------------------------------------------------------------= -----=0D +#=0D +# Start for LoongArch=0D +#=0D +# Copyright (c) 2022 Loongson Technology Corporation Limited. All rights r= eserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +# @par Glossary:=0D +# - CSR - CPU Status Register=0D +# - EBASE - Exception Base Address=0D +#-------------------------------------------------------------------------= -----=0D +#ifndef __ASSEMBLY__=0D +#define __ASSEMBLY__=0D +#endif=0D +=0D +#include =0D +=0D +ASM_GLOBAL ASM_PFX(_ModuleEntryPoint)=0D +ASM_GLOBAL ASM_PFX(DeadLoop)=0D +=0D +.text=0D +ASM_PFX(_ModuleEntryPoint):=0D + /* configure reset ebase */=0D + la.pcrel T0, DeadLoop=0D + csrwr T0, LOONGARCH_CSR_EBASE=0D +=0D + /*disable interrupt*/=0D + li.d T0, (1 << 2)=0D + csrxchg ZERO, T0, LOONGARCH_CSR_CRMD=0D +=0D + /* read physical cpu number id */=0D + csrrd T0, LOONGARCH_CSR_CPUNUM=0D + andi T0, T0, 0x3ff=0D + li.d A0, BOOTCORE_ID //0=0D + bne T0, A0, slave_main=0D +=0D +call_centry:=0D + /*call C function make sure parameter true*/=0D + li.d A1, FixedPcdGet64(PcdSecPeiTempRamBase) + FixedPcdGet32(PcdSec= PeiTempRamSize) # stack base=0D + li.d A0, FixedPcdGet64(PcdFlashPeiFvBase) # PEI Fv base=0D + move SP, A1=0D + addi.d SP, SP, -0x8=0D + bl SecCoreStartupWithStack=0D +=0D +slave_main:=0D + # clear mailbox=0D + li.d T1, LOONGSON_CSR_MAIL_BUF0=0D + iocsrwr.d ZERO, T1=0D +=0D + # enable IPI interrupt=0D + li.d T0, (1 << 12)=0D + csrxchg T0, T0, LOONGARCH_CSR_ECFG=0D +=0D + addi.d T0, ZERO, -1=0D + li.d T1, LOONGSON_IOCSR_IPI_EN=0D + iocsrwr.w T0, T1=0D +=0D +1:=0D + # wait for wakeup=0D + idle 0=0D + nop=0D + iocsrrd.w T0, T1=0D + beqz T0, 1b=0D +=0D + # read and clear ipi interrupt=0D + li.d T1, LOONGSON_IOCSR_IPI_STATUS=0D + iocsrrd.w T0, T1=0D + li.d T1, LOONGSON_IOCSR_IPI_CLEAR=0D + iocsrwr.w T0, T1=0D +=0D + # disable IPI interrupt=0D + li.d T0, (1 << 12)=0D + csrxchg ZERO, T0, LOONGARCH_CSR_ECFG=0D +=0D + # read mail buf and jump to specified entry=0D + li.d T1, LOONGSON_CSR_MAIL_BUF0=0D + iocsrrd.d T0, T1=0D + or RA, T0, ZERO=0D + jirl ZERO, RA, 0x0=0D +=0D +.align 12=0D +ASM_PFX(DeadLoop):=0D + b DeadLoop=0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Sec/SecMain.c b/Platform/Lo= ongson/LoongArchQemuPkg/Sec/SecMain.c=0D new file mode 100644=0D index 0000000000..3f1998c48c=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Sec/SecMain.c=0D @@ -0,0 +1,494 @@=0D +/** @file=0D + Main SEC phase code. Transitions to PEI.=0D +=0D + Copyright (c) 2022 Loongson Technology Corporation Limited. All rights r= eserved.
=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +#include =0D +=0D +/**=0D + temporary memory to permanent memory and do stack switching.=0D +=0D + @param[in] PeiServices Pointer to the PEI Services Table.=0D + @param[in] TemporaryMemoryBase Temporary Memory Base address.=0D + @param[in] PermanentMemoryBase Permanent Memory Base address.=0D + @param[in] CopySize The size of memory that needs to be migrated.=0D +=0D + @retval EFI_SUCCESS Migration successful.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +TemporaryRamMigration (=0D + IN CONST EFI_PEI_SERVICES **PeiServices,=0D + IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,=0D + IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,=0D + IN UINTN CopySize=0D + );=0D +=0D +EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mTemporaryRamSupportPpi =3D {=0D + TemporaryRamMigration=0D +};=0D +=0D +EFI_PEI_PPI_DESCRIPTOR mPrivateDispatchTable[] =3D {=0D + {=0D + (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),= =0D + &gEfiTemporaryRamSupportPpiGuid,=0D + &mTemporaryRamSupportPpi=0D + },=0D +};=0D +=0D +/**=0D + Locates a section within a series of sections=0D + with the specified section type.=0D +=0D + The Instance parameter indicates which instance of the section=0D + type to return. (0 is first instance, 1 is second...)=0D +=0D + @param[in] Sections The sections to search=0D + @param[in] SizeOfSections Total size of all sections=0D + @param[in] SectionType The section type to locate=0D + @param[in] Instance The section instance number=0D + @param[out] FoundSection The FFS section if found=0D +=0D + @retval EFI_SUCCESS The file and section was found=0D + @retval EFI_NOT_FOUND The file and section was not found=0D + @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted=0D +**/=0D +EFI_STATUS=0D +FindFfsSectionInstance (=0D + IN VOID *Sections,=0D + IN UINTN SizeOfSections,=0D + IN EFI_SECTION_TYPE SectionType,=0D + IN UINTN Instance,=0D + OUT EFI_COMMON_SECTION_HEADER **FoundSection=0D + )=0D +{=0D + EFI_PHYSICAL_ADDRESS CurrentAddress;=0D + UINT32 Size;=0D + EFI_PHYSICAL_ADDRESS EndOfSections;=0D + EFI_COMMON_SECTION_HEADER *Section;=0D + EFI_PHYSICAL_ADDRESS EndOfSection;=0D +=0D + //=0D + // Loop through the FFS file sections within the PEI Core FFS file=0D + //=0D + EndOfSection =3D (EFI_PHYSICAL_ADDRESS) (UINTN) Sections;=0D + EndOfSections =3D EndOfSection + SizeOfSections;=0D + for (;;) {=0D + if (EndOfSection =3D=3D EndOfSections) {=0D + break;=0D + }=0D + CurrentAddress =3D (EndOfSection + 3) & ~(3ULL);=0D + if (CurrentAddress >=3D EndOfSections) {=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + Section =3D (EFI_COMMON_SECTION_HEADER*) (UINTN) CurrentAddress;=0D +=0D + Size =3D SECTION_SIZE (Section);=0D + if (Size < sizeof (*Section)) {=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + EndOfSection =3D CurrentAddress + Size;=0D + if (EndOfSection > EndOfSections) {=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + //=0D + // Look for the requested section type=0D + //=0D + if (Section->Type =3D=3D SectionType) {=0D + if (Instance =3D=3D 0) {=0D + *FoundSection =3D Section;=0D + return EFI_SUCCESS;=0D + } else {=0D + Instance--;=0D + }=0D + }=0D + }=0D +=0D + return EFI_NOT_FOUND;=0D +}=0D +=0D +/**=0D + Locates a section within a series of sections=0D + with the specified section type.=0D +=0D + @param[in] Sections The sections to search=0D + @param[in] SizeOfSections Total size of all sections=0D + @param[in] SectionType The section type to locate=0D + @param[out] FoundSection The FFS section if found=0D +=0D + @retval EFI_SUCCESS The file and section was found=0D + @retval EFI_NOT_FOUND The file and section was not found=0D + @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted=0D +**/=0D +EFI_STATUS=0D +FindFfsSectionInSections (=0D + IN VOID *Sections,=0D + IN UINTN SizeOfSections,=0D + IN EFI_SECTION_TYPE SectionType,=0D + OUT EFI_COMMON_SECTION_HEADER **FoundSection=0D + )=0D +{=0D + return FindFfsSectionInstance (=0D + Sections,=0D + SizeOfSections,=0D + SectionType,=0D + 0,=0D + FoundSection=0D + );=0D +}=0D +=0D +/**=0D + Locates a FFS file with the specified file type and a section=0D + within that file with the specified section type.=0D +=0D + @param[in] Fv The firmware volume to search=0D + @param[in] FileType The file type to locate=0D + @param[in] SectionType The section type to locate=0D + @param[out] FoundSection The FFS section if found=0D +=0D + @retval EFI_SUCCESS The file and section was found=0D + @retval EFI_NOT_FOUND The file and section was not found=0D + @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted=0D +**/=0D +EFI_STATUS=0D +FindFfsFileAndSection (=0D + IN EFI_FIRMWARE_VOLUME_HEADER *Fv,=0D + IN EFI_FV_FILETYPE FileType,=0D + IN EFI_SECTION_TYPE SectionType,=0D + OUT EFI_COMMON_SECTION_HEADER **FoundSection=0D + )=0D +{=0D + EFI_STATUS Status;=0D + EFI_PHYSICAL_ADDRESS CurrentAddress;=0D + EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume;=0D + EFI_FFS_FILE_HEADER *File;=0D + UINT32 Size;=0D + EFI_PHYSICAL_ADDRESS EndOfFile;=0D +=0D + if (Fv->Signature !=3D EFI_FVH_SIGNATURE) {=0D + DEBUG ((DEBUG_ERROR, "FV at %p does not have FV header signature\n", F= v));=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + CurrentAddress =3D (EFI_PHYSICAL_ADDRESS) (UINTN) Fv;=0D + EndOfFirmwareVolume =3D CurrentAddress + Fv->FvLength;=0D +=0D + //=0D + // Loop through the FFS files in the Boot Firmware Volume=0D + //=0D + for (EndOfFile =3D CurrentAddress + Fv->HeaderLength; ; ) {=0D +=0D + CurrentAddress =3D (EndOfFile + 7) & ~(7ULL);=0D + if (CurrentAddress > EndOfFirmwareVolume) {=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + File =3D (EFI_FFS_FILE_HEADER*) (UINTN) CurrentAddress;=0D + Size =3D *(UINT32*) File->Size & 0xffffff;=0D + if (Size < (sizeof (*File) + sizeof (EFI_COMMON_SECTION_HEADER))) {=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + EndOfFile =3D CurrentAddress + Size;=0D + if (EndOfFile > EndOfFirmwareVolume) {=0D + return EFI_VOLUME_CORRUPTED;=0D + }=0D +=0D + //=0D + // Look for the request file type=0D + //=0D + if (File->Type !=3D FileType) {=0D + continue;=0D + }=0D +=0D + Status =3D FindFfsSectionInSections (=0D + (VOID*) (File + 1),=0D + (UINTN) EndOfFile - (UINTN) (File + 1),=0D + SectionType,=0D + FoundSection=0D + );=0D + if (!EFI_ERROR (Status)=0D + || (Status =3D=3D EFI_VOLUME_CORRUPTED))=0D + {=0D + return Status;=0D + }=0D + }=0D +}=0D +=0D +/**=0D + Locates the PEI Core entry point address=0D +=0D + @param[in] Fv The firmware volume to search=0D + @param[out] PeiCoreEntryPoint The entry point of the PEI Core image=0D +=0D + @retval EFI_SUCCESS The file and section was found=0D + @retval EFI_NOT_FOUND The file and section was not found=0D + @retval EFI_VOLUME_CORRUPTED The firmware volume was corrupted=0D +**/=0D +EFI_STATUS=0D +FindPeiCoreImageBaseInFv (=0D + IN EFI_FIRMWARE_VOLUME_HEADER *Fv,=0D + OUT EFI_PHYSICAL_ADDRESS *PeiCoreImageBase=0D + )=0D +{=0D + EFI_STATUS Status;=0D + EFI_COMMON_SECTION_HEADER *Section;=0D +=0D + Status =3D FindFfsFileAndSection (=0D + Fv,=0D + EFI_FV_FILETYPE_PEI_CORE,=0D + EFI_SECTION_PE32,=0D + &Section=0D + );=0D + if (EFI_ERROR (Status)) {=0D + Status =3D FindFfsFileAndSection (=0D + Fv,=0D + EFI_FV_FILETYPE_PEI_CORE,=0D + EFI_SECTION_TE,=0D + &Section=0D + );=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "Unable to find PEI Core image\n"));=0D + return Status;=0D + }=0D + }=0D +=0D + *PeiCoreImageBase =3D (EFI_PHYSICAL_ADDRESS)(UINTN)(Section + 1);=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + Find and return Pei Core entry point.=0D +=0D + It also find SEC and PEI Core file debug information. It will report the= m if=0D + remote debug is enabled.=0D +**/=0D +VOID=0D +FindAndReportEntryPoints (=0D + IN EFI_FIRMWARE_VOLUME_HEADER **BootFirmwareVolumePtr,=0D + OUT EFI_PEI_CORE_ENTRY_POINT *PeiCoreEntryPoint=0D + )=0D +{=0D + EFI_STATUS Status;=0D + EFI_PHYSICAL_ADDRESS PeiCoreImageBase =3D 0;=0D + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;=0D +=0D + Status =3D FindPeiCoreImageBaseInFv (*BootFirmwareVolumePtr, &PeiCoreIma= geBase);=0D + ASSERT (Status =3D=3D EFI_SUCCESS);=0D +=0D + ZeroMem ((VOID *) &ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));= =0D +=0D + //=0D + // Report PEI Core debug information when remote debug is enabled=0D + //=0D + ImageContext.ImageAddress =3D (EFI_PHYSICAL_ADDRESS)(UINTN)PeiCoreImageB= ase;=0D + ImageContext.PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) I= mageContext.ImageAddress);=0D + PeCoffLoaderRelocateImageExtraAction (&ImageContext);=0D +=0D + //=0D + // Find PEI Core entry point=0D + //=0D + Status =3D PeCoffLoaderGetEntryPoint ((VOID *) (UINTN) PeiCoreImageBase,= (VOID**) PeiCoreEntryPoint);=0D + if (EFI_ERROR (Status)) {=0D + *PeiCoreEntryPoint =3D 0;=0D + }=0D +=0D + return;=0D +}=0D +=0D +/**=0D + Find the peicore entry point and jump to the entry point to execute.=0D +=0D + @param[in] Context The first input parameter of InitializeDebugAgent(= ).=0D +**/=0D +VOID=0D +EFIAPI=0D +SecStartupPhase2 (=0D + IN VOID *Context=0D + )=0D +{=0D + EFI_SEC_PEI_HAND_OFF *SecCoreData;=0D + EFI_FIRMWARE_VOLUME_HEADER *BootFv;=0D + EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;=0D +=0D + SecCoreData =3D (EFI_SEC_PEI_HAND_OFF *) Context;=0D +=0D + //=0D + // Find PEI Core entry point. It will report SEC and Pei Core debug info= rmation if remote debug=0D + // is enabled.=0D + //=0D + BootFv =3D (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolume= Base;=0D + FindAndReportEntryPoints (&BootFv, &PeiCoreEntryPoint);=0D + SecCoreData->BootFirmwareVolumeBase =3D BootFv;=0D + SecCoreData->BootFirmwareVolumeSize =3D (UINTN) BootFv->FvLength;=0D +=0D + DEBUG ((DEBUG_INFO, "Find Pei EntryPoint=3D%p\n", PeiCoreEntryPoint));=0D +=0D + //=0D + // Transfer the control to the PEI core=0D + //=0D + DEBUG ((DEBUG_INFO, "SecStartupPhase2 %p\n", PeiCoreEntryPoint));=0D +=0D + (*PeiCoreEntryPoint) (SecCoreData, (EFI_PEI_PPI_DESCRIPTOR *)&mPrivateDi= spatchTable);=0D +=0D + //=0D + // If we get here then the PEI Core returned, which is not recoverable.= =0D + //=0D + ASSERT (FALSE);=0D + CpuDeadLoop ();=0D +}=0D +/**=0D + Entry point to the C language phase of SEC. initialize some temporary me= mory and set up the stack,=0D + the control is transferred to this function.=0D +=0D + @param[in] BootFv The pointer to the PEI FV in memory.=0D + @param[in] TopOfCurrentStack Top of Current Stack.=0D +**/=0D +VOID=0D +EFIAPI=0D +SecCoreStartupWithStack (=0D + IN EFI_FIRMWARE_VOLUME_HEADER *BootFv,=0D + IN VOID *TopOfCurrentStack=0D + )=0D +{=0D + EFI_SEC_PEI_HAND_OFF SecCoreData;=0D + EFI_FIRMWARE_VOLUME_HEADER *BootPeiFv =3D (EFI_FIRMWARE_VOLUME_HEA= DER*) BootFv;=0D +=0D + DEBUG ((DEBUG_INFO, "Entering C environment\n"));=0D +=0D + ProcessLibraryConstructorList (NULL, NULL);=0D +=0D + DEBUG ((DEBUG_INFO,=0D + "SecCoreStartupWithStack (0x%lx, 0x%lx)\n",=0D + (UINTN)BootFv,=0D + (UINTN)TopOfCurrentStack=0D + ));=0D + DEBUG ((DEBUG_INFO,=0D + "(0x%lx, 0x%lx)\n",=0D + (UINTN) (PcdGet64 (PcdSecPeiTempRamBase)),=0D + (UINTN) (PcdGet32 (PcdSecPeiTempRamSize))=0D + ));=0D +=0D + // |-------------| <-- TopOfCurrentStack=0D + // | Stack | 32k=0D + // |-------------|=0D + // | Heap | 32k=0D + // |-------------| <-- SecCoreData.TemporaryRamBase=0D + //=0D +=0D + ASSERT ((UINTN) (PcdGet64 (PcdSecPeiTempRamBase) +=0D + PcdGet32 (PcdSecPeiTempRamSize)) =3D=3D=0D + (UINTN) TopOfCurrentStack);=0D +=0D + //=0D + // Initialize SEC hand-off state=0D + //=0D + SecCoreData.DataSize =3D sizeof (EFI_SEC_PEI_HAND_OFF);=0D +=0D + SecCoreData.TemporaryRamSize =3D (UINTN) PcdGet32 (PcdSecPeiTempRa= mSize);=0D + SecCoreData.TemporaryRamBase =3D (VOID *) PcdGet64 (PcdSecPeiTempR= amBase);=0D +=0D + SecCoreData.PeiTemporaryRamBase =3D SecCoreData.TemporaryRamBase;=0D + SecCoreData.PeiTemporaryRamSize =3D SecCoreData.TemporaryRamSize >> 1= ;=0D +=0D + SecCoreData.StackBase =3D (UINT8 *)SecCoreData.TemporaryRam= Base + SecCoreData.PeiTemporaryRamSize;=0D + SecCoreData.StackSize =3D SecCoreData.TemporaryRamSize >> 1= ;=0D +=0D + SecCoreData.BootFirmwareVolumeBase =3D BootPeiFv;=0D + SecCoreData.BootFirmwareVolumeSize =3D (UINTN) BootPeiFv->FvLength;=0D +=0D + DEBUG ((DEBUG_INFO,=0D + "&SecCoreData.BootFirmwareVolumeBase=3D%lx SecCoreData.BootFirmwareVol= umeBase=3D%lx\n",=0D + (UINT64)&(SecCoreData.BootFirmwareVolumeBase),=0D + (UINT64) (SecCoreData.BootFirmwareVolumeBase)));=0D + DEBUG ((DEBUG_INFO,=0D + "&SecCoreData.BootFirmwareVolumeSize=3D%lx SecCoreData.BootFirmwareVol= umeSize=3D%lx\n",=0D + (UINT64)&(SecCoreData.BootFirmwareVolumeSize),=0D + (UINT64) (SecCoreData.BootFirmwareVolumeSize)));=0D +=0D + //=0D + // Initialize Debug Agent to support source level debug in SEC/PEI phase= s before memory ready.=0D + //=0D + InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, NULL, NULL);=0D + SecStartupPhase2 (&SecCoreData);=0D +}=0D +=0D +/**=0D + temporary memory to permanent memory and do stack switching.=0D +=0D + @param[in] PeiServices Pointer to the PEI Services Table.=0D + @param[in] TemporaryMemoryBase Temporary Memory Base address.=0D + @param[in] PermanentMemoryBase Permanent Memory Base address.=0D + @param[in] CopySize The size of memory that needs to be migrated.=0D +=0D + @retval EFI_SUCCESS Migration successful.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +TemporaryRamMigration (=0D + IN CONST EFI_PEI_SERVICES **PeiServices,=0D + IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,=0D + IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,=0D + IN UINTN CopySize=0D + )=0D +{=0D + VOID *OldHeap;=0D + VOID *NewHeap;=0D + VOID *OldStack;=0D + VOID *NewStack;=0D + BASE_LIBRARY_JUMP_BUFFER JumpBuffer;=0D +=0D + DEBUG ((DEBUG_INFO,=0D + "TemporaryRamMigration (0x%Lx, 0x%Lx, 0x%Lx)\n",=0D + TemporaryMemoryBase,=0D + PermanentMemoryBase,=0D + (UINT64)CopySize=0D + ));=0D +=0D + OldHeap =3D (VOID*) (UINTN)TemporaryMemoryBase;=0D + NewHeap =3D (VOID*) ((UINTN)PermanentMemoryBase + (CopySize >> 1));=0D +=0D + OldStack =3D (VOID*) ((UINTN)TemporaryMemoryBase + (CopySize >> 1));=0D + NewStack =3D (VOID*) (UINTN)PermanentMemoryBase;=0D +=0D + //=0D + // Migrate Heap=0D + //=0D + CopyMem (NewHeap, OldHeap, CopySize >> 1);=0D +=0D + //=0D + // Migrate Stack=0D + //=0D + CopyMem (NewStack, OldStack, CopySize >> 1);=0D +=0D + // Use SetJump ()/LongJump () to switch to a new stack.=0D + //=0D + if (SetJump (&JumpBuffer) =3D=3D 0) {=0D + JumpBuffer.SP =3D JumpBuffer.SP - (UINTN)OldStack + (UINTN)NewStack ;= =0D + LongJump (&JumpBuffer, (UINTN)-1);=0D + }=0D +=0D + return EFI_SUCCESS;=0D +}=0D diff --git a/Platform/Loongson/LoongArchQemuPkg/Sec/SecMain.inf b/Platform/= Loongson/LoongArchQemuPkg/Sec/SecMain.inf=0D new file mode 100644=0D index 0000000000..c0d5439d53=0D --- /dev/null=0D +++ b/Platform/Loongson/LoongArchQemuPkg/Sec/SecMain.inf=0D @@ -0,0 +1,51 @@=0D +## @file=0D +# SEC Driver=0D +#=0D +# Copyright (c) 2022 Loongson Technology Corporation Limited. All rights = reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D SecMain=0D + FILE_GUID =3D 57d02d4f-5a5d-4bfa-b7d6-ba0a4d2c72ce= =0D + MODULE_TYPE =3D SEC=0D + VERSION_STRING =3D 1.0=0D +=0D +#=0D +# VALID_ARCHITECTURES =3D LOONGARCH64=0D +#=0D +=0D +[Sources]=0D + LoongArch64/Start.S=0D + SecMain.c=0D +=0D +[Packages]=0D + Platform/Loongson/LoongArchQemuPkg/Loongson.dec=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + DebugLib=0D + BaseMemoryLib=0D + PcdLib=0D + DebugAgentLib=0D + IoLib=0D + PeCoffLib=0D + PeCoffGetEntryPointLib=0D + PeCoffExtraActionLib=0D +=0D +[Ppis]=0D + gEfiTemporaryRamSupportPpiGuid # PPI ALWAYS_PRODUCED=0D +=0D +[FixedPcd]=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdSecPeiTempRamBase=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdSecPeiTempRamSize=0D +=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvBase=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvSize=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdFlashPeiFvBase=0D + gLoongArchQemuPkgTokenSpaceGuid.PcdFlashPeiFvSize=0D -- =0D 2.31.1=0D =0D