From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (NAM12-MW2-obe.outbound.protection.outlook.com [40.107.244.131]) by mx.groups.io with SMTP id smtpd.web10.4662.1634883574923598611 for ; Thu, 21 Oct 2021 23:19:35 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@os.amperecomputing.com header.s=selector2 header.b=CPwqiK7C; spf=pass (domain: os.amperecomputing.com, ip: 40.107.244.131, mailfrom: nhi@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ih40gX7H8GK4eZG+PoAspVW6HIEf3U8OJ7j+vbAGqeXS/Uy5Cv/DPjLlFFiTM2XaQrN7zTC5moQNI+TZyMIPs4IX0sc/WfhcfdVX5E18VwVJGCI6i0ZA/wvr6xjr4IJgpBMfURjHpVp3tEz+HWd4SLdsyOsQnTqC5DYLh3H2EfWMD4GraC1NJz+9sFGq6FPvhFNWmi/UnzG6qsGMcWcCW2IkLKmlQd+5T7xnIQJOAJNxTv5DvTm4BwSIhf757EK+ujxDmz+OtTz8zuDJWTOni4CzvQRnO69E6Gfqqxvqqf5S6SFDyZoftjUyc2kwEQuP6EiXz3E7IluGbocszeySJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wb5o0I2fE+JsgqJNa5lrlfE/CXlh4O2wm7nzkfz+6BE=; b=L+Yn1eQUt+WD65BdVeCdI/cExhfXtx2WEbtNHu075i/NZfjVfdGe3KKEIGJEjvtfCcBAzwUxaO0S0iXbmG8SWniIGYzRTGT5otHl69M9dBE2yH2pNxxZ1peNTj+msQF+rTu5ofMhm3eF87Qr3NYcHlnC2FOm1ltySW9Q3uYTuWhwJ1YmlLxJWsZtEFhajY7DcHZXlwJSXSXy0eTDbw6v+WZL/mk5Ru21U+9AK4kjGpb3W54kE1eBcLOWT6odJEqeOAZpfXPR4sijdij+qL0AG93tnUVYP+YsLPrX4ASpsGiAW7DlfVJ0NiU6+u2KsM61z4HxSfcUYvppIMBFXgmBNw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wb5o0I2fE+JsgqJNa5lrlfE/CXlh4O2wm7nzkfz+6BE=; b=CPwqiK7CmWuVgRfSFykVYagorgS2fMRl8lx9JmU0VfIengyL27AAeH/eulq6yVzd0uBkpU/Ek6ben2mphKzozEK8v1h5gDjMNz2+BAXEVfdhvMySFxzIjQHdkWGEJ3k6wHIBKGnwNu0MWPbcmt/zoxPTo7GzO3yKfxQlDQ1HYuw= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=os.amperecomputing.com; Received: from PH0PR01MB7287.prod.exchangelabs.com (2603:10b6:510:10a::21) by PH0PR01MB6103.prod.exchangelabs.com (2603:10b6:510:13::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Fri, 22 Oct 2021 06:19:32 +0000 Received: from PH0PR01MB7287.prod.exchangelabs.com ([fe80::254c:9533:7f35:aee]) by PH0PR01MB7287.prod.exchangelabs.com ([fe80::254c:9533:7f35:aee%4]) with mapi id 15.20.4628.016; Fri, 22 Oct 2021 06:19:32 +0000 From: "Nhi Pham" To: devel@edk2.groups.io CC: patches@amperecomputing.com, nhi@os.amperecomputing.com, vunguyen@os.amperecomputing.com, Thang Nguyen , Chuong Tran , Phong Vo , Leif Lindholm , Michael D Kinney , Ard Biesheuvel , Nate DeSimone Subject: [edk2-platforms][PATCH v4 02/31] AmpereAltraPkg: Add FlashLib library instance Date: Fri, 22 Oct 2021 13:17:40 +0700 Message-ID: <20211022061809.31087-3-nhi@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211022061809.31087-1-nhi@os.amperecomputing.com> References: <20211022061809.31087-1-nhi@os.amperecomputing.com> X-ClientProxiedBy: HK2PR02CA0168.apcprd02.prod.outlook.com (2603:1096:201:1f::28) To PH0PR01MB7287.prod.exchangelabs.com (2603:10b6:510:10a::21) Return-Path: nhi@os.amperecomputing.com MIME-Version: 1.0 Received: from sw004.amperecomputing.com (118.69.219.201) by HK2PR02CA0168.apcprd02.prod.outlook.com (2603:1096:201:1f::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4628.16 via Frontend Transport; Fri, 22 Oct 2021 06:19:29 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0c85d70a-72a3-48b6-b7b4-08d99523e972 X-MS-TrafficTypeDiagnostic: PH0PR01MB6103: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pVg3i+Z5cTrnt+ch+nvJ3r9c3YzN2vAsUuAf+8/XHKm+l4+DSD4UDNfMXWQNFIZRj0Zj1C98LpLX4ZRoEzPtOQmfWMpL17vJ6Oc6NdWLbUsDNqhqRTREtDy47N4tC56FDJu5PUal5c3yMhXZNXfR/lewXEhZ8te/CsWxXlB0H51BA6KPIBrt4bIX76SQHOvQwRF6MsZ/KZHmLSm33vsZNH/9UnG7J+V6tbVUkxArzmatKCc9kOBTyC+P99QyzbTUAw9q7q5/rZqqv1nNjBGLmEpDOz6COH3eB+Nszhdq8pdnbqAZrxLFzsFdPi8NnmRRW19ymdXN6q3Ky0+a4O/7nQinrmJM4s6NI2ht+ic2dWeEq1VB5juZFdqvVw7OHcCmtHiJb5j3VPyaU6r9kBOmGvSS2w6RJGBzFlvJNxVjescnopjUmlur2riHafvupF1VlhFh241p5pHgYo/M++0xeBoDI7/uMXyiTG6psJT/1kRtTnxTdV5UL0AaSFmwzDlHglMQWhFFB2cjDIRWac9eduRa14JykbVA+M2K9DKx6Me0zwSzyC2UYY3RwyEH9fLSRD57HU3jjlxfMCN7tJ5lEQRyOb5m+zbUbDJClIRiUcdmeQfiIzUT6fSlOpzWk9jFqsQgyJQNFUDP1kwnIng9Y7p8VPf3Edugi7Ww1y7deM3cWt5UjUYaBi03uOqWOhrN/+4p2q30m1Cu5imM5dPwWA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH0PR01MB7287.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(5660300002)(2906002)(86362001)(38100700002)(19627235002)(508600001)(66946007)(6916009)(316002)(186003)(66476007)(4326008)(1076003)(8936002)(956004)(2616005)(8676002)(83380400001)(6512007)(6486002)(26005)(52116002)(66556008)(30864003)(38350700002)(54906003)(6666004)(6506007);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?tPY0H1ybO65jmSB1Z7wJTboznLLh2LhXJlG1/MH3Nm350rPuh+51IkdpZxRy?= =?us-ascii?Q?VIvP225ZGFwKoZpKyLVwQB2fBGtfAA+wEmLs/HNCq9dP3Zyvpa4FYsyCpSMJ?= =?us-ascii?Q?OozV2fZsnL/oic58/Kr8uE7W2lcXXp5qfKNoaQelzs+6Zy/n9tBkVhYsa5ts?= =?us-ascii?Q?CAAY4VM3EBrgerjce9qAGRN02LztKaJ3OBi182hTyH+FKJmWMjqIVcBstOmE?= =?us-ascii?Q?2HRgX+hI9JO+6wudaREdEBOCP5EBWAI3XQbTkOU7UEraJctDG1dVWxtRgzbI?= =?us-ascii?Q?ZY3bGGfMV9Ilo0baEvBdziOX2xwzrGqSIudmlzoHkD5JizFPQzHUcrcrmSmR?= =?us-ascii?Q?oFL+/WhEctpJ8Ng93oQpf7xZjyClkBcN4uhpBMMi8mjMZnws3r2y2Awus5Tt?= =?us-ascii?Q?H5jk7yBPVDZCuddQy7ZxqU4cEJXB87YQaLzdAWwLqfX8/VpnYYHeA9EzAunU?= =?us-ascii?Q?r9slF+ns7Ea3z0Z4NlLeM8KIFgKA6f3YyE4Qebiik3lOSDelMz/iEZGMnwH9?= =?us-ascii?Q?GkbxJk7xMTvnFe89r7e373X85J3qz2+AYaJ/kKz1zTt6Q1sBrlc8aeWDr+KP?= =?us-ascii?Q?Lns2CfwRUMXUliKt6GfMNMgHCu9p/FiMDJ0EzOQRLaOYUo5csmSxyYdkGxYX?= =?us-ascii?Q?D1GoTpfRjDSVPn13hX5/USyNXsmDoRmxA5B8GFHFLQmawRwo1ZEyi3etx43R?= =?us-ascii?Q?nuSl46UT0RWfkClduNPImsv9YblLPawVRa5PkZHiWBzggd3C/BHWomLIf2Nr?= =?us-ascii?Q?+cUxVA+yzbVPXnt7TQ8u+EKa65+GqfR68bk56U9dXwOEt1SxRmM3NHlurpiE?= =?us-ascii?Q?Bq6U3Sp9RZS4UR2bMkbHV5/9kFtlwjzpHHQgBMX83fK3BcwA7+p9CbNPRbTc?= =?us-ascii?Q?jVP8DUjt1WNzUkiII8qh6+4HwI5f88R/yWLoj/kha+suMPTmzgRPG/EbHjvB?= =?us-ascii?Q?eJJhr3Y5wL9qf1nBtDXhyq49GKjq4yLEkwDByj7payZj769541uKClT5s4uE?= =?us-ascii?Q?yTJF/cwWYybZeS9rjYhoWIeqrkAMrv/N/TycTV+qjQzehuOudBVqCyLYSu81?= =?us-ascii?Q?FRX4Z1JQHM5SNUgbvcQaP1DWbeXdyRKQOZH0qOWZwGwsz1l75IO6Mhx1Bjkk?= =?us-ascii?Q?jp7HO2vw8QZd3ld5KPA3jI+u/pIZWBVgDzQj085sRUaMidWni8WQu+z0Sg25?= =?us-ascii?Q?zKCtTyHwJJesvCXxsId62JEtqjJrU765XehJg89jkGB6jC8E8sO+h068w9hP?= =?us-ascii?Q?2oz5316aaEI0GUhaUk1Y25qYXQZ8OFW72PeK3Ko4h8J4gma8g8F7pOCQ4YM7?= =?us-ascii?Q?GFyOH67hKqz0DIlF44yOqEfG?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0c85d70a-72a3-48b6-b7b4-08d99523e972 X-MS-Exchange-CrossTenant-AuthSource: PH0PR01MB7287.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2021 06:19:32.5088 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: nhi@amperemail.onmicrosoft.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR01MB6103 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain From: Vu Nguyen Provides functions to access the NVRAM, NVRAM2 and FailSafe region on the Flash over MM communication. Cc: Thang Nguyen Cc: Chuong Tran Cc: Phong Vo Cc: Leif Lindholm Cc: Michael D Kinney Cc: Ard Biesheuvel Cc: Nate DeSimone Signed-off-by: Nhi Pham --- Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec | 3 + Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc | 2 + Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.inf | 35 += + Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.inf | 37 += + Silicon/Ampere/AmpereAltraPkg/Include/Library/FlashLib.h | 118 += ++++++ Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.h | 88 += ++++ Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.c | 86 += ++++ Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.c | 372 += +++++++++++++++++++ Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.c | 141 += +++++++ 9 files changed, 882 insertions(+) diff --git a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec b/Silicon/Amp= ere/AmpereAltraPkg/AmpereAltraPkg.dec index 0813fc9b4b69..ac778674266d 100644 --- a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec +++ b/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec @@ -34,6 +34,9 @@ [LibraryClasses] ## @libraryclass Defines a set of methods to generate random numbers b= y using Hardware RNG. TrngLib|Silicon/Ampere/AmpereAltraPkg/Include/Library/TrngLib.h =20 + ## @libraryclass Defines a set of methods to access flash memory. + FlashLib|Silicon/Ampere/AmpereAltraPkg/Include/Library/FlashLib.h + [Guids] ## NVParam MM GUID gNVParamMmGuid =3D { 0xE4AC5024, 0x29BE, 0x4ADC, { 0x93, 0= x36, 0x87, 0xB5, 0xA0, 0x76, 0x23, 0x2D } } diff --git a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc b/Silicon= /Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc index 3b33be6280ce..69a6caa56752 100644 --- a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc +++ b/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dsc.inc @@ -84,6 +84,7 @@ [LibraryClasses.common] AmpereCpuLib|Silicon/Ampere/AmpereAltraPkg/Library/AmpereCpuLib/AmpereCp= uLib.inf TimeBaseLib|EmbeddedPkg/Library/TimeBaseLib/TimeBaseLib.inf MmCommunicationLib|Silicon/Ampere/AmpereAltraPkg/Library/MmCommunication= Lib/MmCommunicationLib.inf + FlashLib|Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.inf =20 # # ARM PL011 UART Driver @@ -252,6 +253,7 @@ [LibraryClasses.common.DXE_RUNTIME_DRIVER] ArmSmcLib|ArmPkg/Library/ArmSmcLib/ArmSmcLib.inf NVParamLib|Silicon/Ampere/AmpereAltraPkg/Library/NVParamLib/RuntimeNVPar= amLib.inf AmpereCpuLib|Silicon/Ampere/AmpereAltraPkg/Library/AmpereCpuLib/RuntimeA= mpereCpuLib.inf + FlashLib|Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.= inf =20 [LibraryClasses.ARM,LibraryClasses.AARCH64] # diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.inf b/= Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.inf new file mode 100644 index 000000000000..896d8fd965c8 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.inf @@ -0,0 +1,35 @@ +## @file +# +# Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001B + BASE_NAME =3D FlashLib + FILE_GUID =3D 9E9D093D-6484-45AE-BA49-0745AA0BB481 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 0.1 + LIBRARY_CLASS =3D FlashLib + CONSTRUCTOR =3D FlashLibConstructor + +[Sources.common] + FlashLib.c + FlashLibCommon.c + +[Packages] + ArmPkg/ArmPkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + MdePkg/MdePkg.dec + Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec + +[LibraryClasses] + BaseMemoryLib + DebugLib + MemoryAllocationLib + MmCommunicationLib + +[Guids] + gSpiNorMmGuid diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib= .inf b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.inf new file mode 100644 index 000000000000..e729e29cf450 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.inf @@ -0,0 +1,37 @@ +## @file +# +# Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001B + BASE_NAME =3D RuntimeFlashLib + FILE_GUID =3D 0FDF1187-A2DA-4876-8B1F-6D1E928AA319 + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + VERSION_STRING =3D 0.1 + LIBRARY_CLASS =3D FlashLib + CONSTRUCTOR =3D FlashLibConstructor + +[Sources.common] + FlashLibCommon.c + RuntimeFlashLib.c + +[Packages] + ArmPkg/ArmPkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + MdePkg/MdePkg.dec + Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec + +[LibraryClasses] + BaseMemoryLib + DebugLib + MemoryAllocationLib + +[Guids] + gSpiNorMmGuid + +[Protocols] + gEfiMmCommunication2ProtocolGuid diff --git a/Silicon/Ampere/AmpereAltraPkg/Include/Library/FlashLib.h b/Sil= icon/Ampere/AmpereAltraPkg/Include/Library/FlashLib.h new file mode 100644 index 000000000000..ce50602f9934 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Include/Library/FlashLib.h @@ -0,0 +1,118 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef FLASH_LIB_H_ +#define FLASH_LIB_H_ + +/** + Get the information about the Flash region to store the FailSafe status. + + @param[out] FailSafeBase Base address of the FailSafe space. + @param[out] FailSafeSize Total size of the FailSafe space. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER FailSafeBase or FailSafeSize is NULL. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashGetFailSafeInfo ( + OUT UINTN *FailSafeBase, + OUT UINT32 *FailSafeSize + ); + +/** + Get the information about the Flash region to store the NVRAM variables. + + @param[out] NvRamBase Base address of the NVRAM space. + @param[out] NvRamSize Total size of the NVRAM space. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER NvRamBase or NvRamSize is NULL. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashGetNvRamInfo ( + OUT UINTN *NvRamBase, + OUT UINT32 *NvRamSize + ); + +/** + Get the information about the second Flash region to store the NVRAM var= iables. + + @param[out] NvRam2Base Base address of the NVRAM space. + @param[out] NvRam2Size Total size of the NVRAM space. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER NvRam2Base or NvRam2Size is NULL. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashGetNvRam2Info ( + OUT UINTN *NvRam2Base, + OUT UINT32 *NvRam2Size + ); + +/** + Erase a region of the Flash. + + @param[in] ByteAddress Start address of the region. + @param[in] Length Number of bytes to erase. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER Length is Zero. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashEraseCommand ( + IN UINTN ByteAddress, + IN UINT32 Length + ); + +/** + Write data buffer to the Flash. + + @param[in] ByteAddress Start address of the region. + @param[in] Buffer Pointer to the data buffer. + @param[in] Length Number of bytes to write. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER Buffer is NULL or Length is Zero. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashWriteCommand ( + IN UINTN ByteAddress, + IN VOID *Buffer, + IN UINT32 Length + ); + +/** + Read data from the Flash into Buffer. + + @param[in] ByteAddress Start address of the region. + @param[out] Buffer Pointer to the data buffer. + @param[in] Length Number of bytes to read. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER Buffer is NULL or Length is Zero. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashReadCommand ( + IN UINTN ByteAddress, + OUT VOID *Buffer, + IN UINT32 Length + ); + +#endif /* FLASH_LIB_H_ */ diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.= h b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.h new file mode 100644 index 000000000000..36ef9b8ca25d --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.h @@ -0,0 +1,88 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef FLASH_LIB_COMMON_H_ +#define FLASH_LIB_COMMON_H_ + +#define EFI_MM_MAX_TMP_BUF_SIZE 0x1000 +#define EFI_MM_MAX_PAYLOAD_SIZE 0x50 + +#define MM_SPINOR_FUNC_GET_INFO 0x00 +#define MM_SPINOR_FUNC_READ 0x01 +#define MM_SPINOR_FUNC_WRITE 0x02 +#define MM_SPINOR_FUNC_ERASE 0x03 +#define MM_SPINOR_FUNC_GET_NVRAM_INFO 0x04 +#define MM_SPINOR_FUNC_GET_NVRAM2_INFO 0x05 +#define MM_SPINOR_FUNC_GET_FAILSAFE_INFO 0x06 + +#define MM_SPINOR_RES_SUCCESS 0xAABBCC00 +#define MM_SPINOR_RES_FAIL 0xAABBCCFF + +#pragma pack(1) + +typedef struct { + // + // Allows for disambiguation of the message format. + // + EFI_GUID HeaderGuid; + + // + // Describes the size of Data (in bytes) and does not include the size o= f the header. + // + UINTN MessageLength; + + // + // Designates an array of bytes that is MessageLength in size. + // + UINT8 Data[EFI_MM_MAX_PAYLOAD_SIZE]; +} EFI_MM_COMMUNICATE_REQUEST; + +typedef struct { + UINT64 Status; + UINT64 DeviceBase; + UINT64 PageSize; + UINT64 SectorSize; + UINT64 DeviceSize; +} EFI_MM_COMMUNICATE_SPINOR_RESPONSE; + +typedef struct { + UINT64 Status; + UINT64 FailSafeBase; + UINT64 FailSafeSize; +} EFI_MM_COMMUNICATE_FAILSAFE_INFO_RESPONSE; + +typedef struct { + UINT64 Status; + UINT64 NvRamBase; + UINT64 NvRamSize; +} EFI_MM_COMMUNICATE_NVRAM_INFO_RESPONSE; + +#pragma pack() + +/** + Provides an interface to access the Flash services via MM interface. + + @param[in] Request Pointer to the request buffer + @param[in] RequestDataSize Size of the request buffer. + @param[out] Response Pointer to the response buffer + @param[in] ResponseDataSize Size of the response buffer. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER An invalid data parameter or an invalid + combination of data parameters. + @retval Others An error has occurred. +**/ +EFI_STATUS +FlashMmCommunicate ( + IN VOID *Request, + IN UINT32 RequestDataSize, + OUT VOID *Response, + IN UINT32 ResponseDataSize + ); + +#endif /* FLASH_LIB_COMMON_H_ */ diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.c b/Si= licon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.c new file mode 100644 index 000000000000..e844a5c73f6c --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.c @@ -0,0 +1,86 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include +#include +#include +#include + +#include "FlashLibCommon.h" + +extern UINT8 *mTempBufferPhysical; +extern UINT8 *mTempBufferVirtual; + +/** + Constructor function of the FlashLib + + @retval EFI_SUCCESS The constructor executes successfully. +**/ +EFI_STATUS +EFIAPI +FlashLibConstructor ( + VOID + ) +{ + mTempBufferPhysical =3D AllocateZeroPool (EFI_MM_MAX_TMP_BUF_SIZE); + mTempBufferVirtual =3D mTempBufferPhysical; + ASSERT (mTempBufferPhysical !=3D NULL); + + return EFI_SUCCESS; +} + +/** + Provides an interface to access the Flash services via MM interface. + + @param[in] Request Pointer to the request buffer + @param[in] RequestDataSize Size of the request buffer. + @param[out] Response Pointer to the response buffer + @param[in] ResponseDataSize Size of the response buffer. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER An invalid data parameter or an invalid + combination of data parameters. + @retval Others An error has occurred. +**/ +EFI_STATUS +FlashMmCommunicate ( + IN VOID *Request, + IN UINT32 RequestDataSize, + OUT VOID *Response, + IN UINT32 ResponseDataSize + ) +{ + EFI_MM_COMMUNICATE_REQUEST CommBuffer; + EFI_STATUS Status; + + if (Request =3D=3D NULL || RequestDataSize =3D=3D 0 + || RequestDataSize > EFI_MM_MAX_PAYLOAD_SIZE + || (ResponseDataSize =3D=3D 0 && Response =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + CopyGuid (&CommBuffer.HeaderGuid, &gSpiNorMmGuid); + CommBuffer.MessageLength =3D RequestDataSize; + CopyMem (CommBuffer.Data, Request, RequestDataSize); + + Status =3D MmCommunicationCommunicate ( + &CommBuffer, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (ResponseDataSize > 0) { + CopyMem (Response, CommBuffer.Data, ResponseDataSize); + } + + return EFI_SUCCESS; +} diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.= c b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.c new file mode 100644 index 000000000000..08aab4ba9f0c --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.c @@ -0,0 +1,372 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include +#include +#include +#include + +#include "FlashLibCommon.h" + +BOOLEAN mIsEfiRuntime =3D FALSE; +UINT8 *mTempBufferPhysical; +UINT8 *mTempBufferVirtual; + +/** + Convert Virtual Address to Physical Address at Runtime. + + @param[in] VirtualPtr Virtual Address Pointer. + @param[in] Size Total bytes of the buffer. + + @retval Pointer to the physical address of the converted buffer. +**/ +STATIC +UINT8 * +ConvertToPhysicalBuffer ( + IN UINT8 *VirtualPtr, + IN UINT32 Size + ) +{ + if (mIsEfiRuntime) { + ASSERT (VirtualPtr !=3D NULL); + CopyMem (mTempBufferVirtual, VirtualPtr, Size); + return mTempBufferPhysical; + } + + return VirtualPtr; +} + +/** + Get the information about the Flash region to store the FailSafe status. + + @param[out] FailSafeBase Base address of the FailSafe space. + @param[out] FailSafeSize Total size of the FailSafe space. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER FailSafeBase or FailSafeSize is NULL. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashGetFailSafeInfo ( + OUT UINTN *FailSafeBase, + OUT UINT32 *FailSafeSize + ) +{ + EFI_MM_COMMUNICATE_FAILSAFE_INFO_RESPONSE FailSafeInfo; + EFI_STATUS Status; + UINT64 MmData[5]; + + if (FailSafeBase =3D=3D NULL || FailSafeSize =3D=3D NULL ) { + return EFI_INVALID_PARAMETER; + } + + MmData[0] =3D MM_SPINOR_FUNC_GET_FAILSAFE_INFO; + + Status =3D FlashMmCommunicate ( + MmData, + sizeof (MmData), + &FailSafeInfo, + sizeof (FailSafeInfo) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (FailSafeInfo.Status =3D=3D MM_SPINOR_RES_SUCCESS) { + *FailSafeBase =3D FailSafeInfo.FailSafeBase; + *FailSafeSize =3D FailSafeInfo.FailSafeSize; + + DEBUG (( + DEBUG_INFO, + "%a: FailSafe Base 0x%llx, Size 0x%lx\n", + __FUNCTION__, + *FailSafeBase, + *FailSafeSize + )); + } + + return EFI_SUCCESS; +} + +/** + Get the information about the Flash region to store the NVRAM variables. + + @param[out] NvRamBase Base address of the NVRAM space. + @param[out] NvRamSize Total size of the NVRAM space. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER NvRamBase or NvRamSize is NULL. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashGetNvRamInfo ( + OUT UINTN *NvRamBase, + OUT UINT32 *NvRamSize + ) +{ + EFI_MM_COMMUNICATE_NVRAM_INFO_RESPONSE NvRamInfo; + EFI_STATUS Status; + UINT64 MmData[5]; + + if (NvRamBase =3D=3D NULL || NvRamSize =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + MmData[0] =3D MM_SPINOR_FUNC_GET_NVRAM_INFO; + + Status =3D FlashMmCommunicate ( + MmData, + sizeof (MmData), + &NvRamInfo, + sizeof (NvRamInfo) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (NvRamInfo.Status =3D=3D MM_SPINOR_RES_SUCCESS) { + *NvRamBase =3D NvRamInfo.NvRamBase; + *NvRamSize =3D NvRamInfo.NvRamSize; + DEBUG (( + DEBUG_INFO, + "%a: NVRAM Base 0x%llx, Size 0x%lx\n", + __FUNCTION__, + *NvRamBase, + *NvRamSize + )); + } + + return EFI_SUCCESS; +} + +/** + Get the information about the second Flash region to store the NVRAM var= iables. + + @param[out] NvRam2Base Base address of the NVRAM space. + @param[out] NvRam2Size Total size of the NVRAM space. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER NvRam2Base or NvRam2Size is NULL. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashGetNvRam2Info ( + OUT UINTN *NvRam2Base, + OUT UINT32 *NvRam2Size + ) +{ + EFI_MM_COMMUNICATE_NVRAM_INFO_RESPONSE NvRam2Info; + EFI_STATUS Status; + UINT64 MmData[5]; + + if (NvRam2Base =3D=3D NULL || NvRam2Size =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + MmData[0] =3D MM_SPINOR_FUNC_GET_NVRAM2_INFO; + + Status =3D FlashMmCommunicate ( + MmData, + sizeof (MmData), + &NvRam2Info, + sizeof (NvRam2Info) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (NvRam2Info.Status =3D=3D MM_SPINOR_RES_SUCCESS) { + *NvRam2Base =3D NvRam2Info.NvRamBase; + *NvRam2Size =3D NvRam2Info.NvRamSize; + DEBUG (( + DEBUG_INFO, + "%a: NVRAM2 Base 0x%llx, Size 0x%lx\n", + __FUNCTION__, + *NvRam2Base, + *NvRam2Size + )); + } + + return EFI_SUCCESS; +} + +/** + Erase a region of the Flash. + + @param[in] ByteAddress Start address of the region. + @param[in] Length Number of bytes to erase. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER Length is Zero. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashEraseCommand ( + IN UINTN ByteAddress, + IN UINT32 Length + ) +{ + EFI_MM_COMMUNICATE_SPINOR_RESPONSE MmSpiNorRes; + EFI_STATUS Status; + UINT64 MmData[5]; + + if (Length =3D=3D 0) { + return EFI_INVALID_PARAMETER; + } + + MmData[0] =3D MM_SPINOR_FUNC_ERASE; + MmData[1] =3D ByteAddress; + MmData[2] =3D Length; + + Status =3D FlashMmCommunicate ( + MmData, + sizeof (MmData), + &MmSpiNorRes, + sizeof (MmSpiNorRes) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (MmSpiNorRes.Status !=3D MM_SPINOR_RES_SUCCESS) { + DEBUG ((DEBUG_ERROR, "%a: Device error %llx\n", __FUNCTION__, MmSpiNor= Res.Status)); + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Write data buffer to the Flash. + + @param[in] ByteAddress Start address of the region. + @param[in] Buffer Pointer to the data buffer. + @param[in] Length Number of bytes to write. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER Buffer is NULL or Length is Zero. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashWriteCommand ( + IN UINTN ByteAddress, + IN VOID *Buffer, + IN UINT32 Length + ) +{ + EFI_MM_COMMUNICATE_SPINOR_RESPONSE MmSpiNorRes; + EFI_STATUS Status; + UINT64 MmData[5]; + UINTN Remain, NumWrite; + UINTN Count =3D 0; + + if (Buffer =3D=3D NULL || Length =3D=3D 0) { + return EFI_INVALID_PARAMETER; + } + + Remain =3D Length; + while (Remain > 0) { + NumWrite =3D (Remain > EFI_MM_MAX_TMP_BUF_SIZE) ? EFI_MM_MAX_TMP_BUF_S= IZE : Remain; + + MmData[0] =3D MM_SPINOR_FUNC_WRITE; + MmData[1] =3D ByteAddress + Count; + MmData[2] =3D NumWrite; + MmData[3] =3D (UINT64)ConvertToPhysicalBuffer (Buffer + Count, NumWrit= e); + + Status =3D FlashMmCommunicate ( + MmData, + sizeof (MmData), + &MmSpiNorRes, + sizeof (MmSpiNorRes) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (MmSpiNorRes.Status !=3D MM_SPINOR_RES_SUCCESS) { + DEBUG ((DEBUG_ERROR, "%a: Device error 0x%llx\n", __FUNCTION__, MmSp= iNorRes.Status)); + return EFI_DEVICE_ERROR; + } + + Remain -=3D NumWrite; + Count +=3D NumWrite; + } + + return EFI_SUCCESS; +} + +/** + Read data from the Flash into Buffer. + + @param[in] ByteAddress Start address of the region. + @param[out] Buffer Pointer to the data buffer. + @param[in] Length Number of bytes to read. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER Buffer is NULL or Length is Zero. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashReadCommand ( + IN UINTN ByteAddress, + OUT VOID *Buffer, + IN UINT32 Length + ) +{ + EFI_MM_COMMUNICATE_SPINOR_RESPONSE MmSpiNorRes; + EFI_STATUS Status; + UINT64 MmData[5]; + UINTN Remain, NumRead; + UINTN Count =3D 0; + + if (Buffer =3D=3D NULL || Length =3D=3D 0) { + return EFI_INVALID_PARAMETER; + } + + Remain =3D Length; + while (Remain > 0) { + NumRead =3D (Remain > EFI_MM_MAX_TMP_BUF_SIZE) ? EFI_MM_MAX_TMP_BUF_SI= ZE : Remain; + + MmData[0] =3D MM_SPINOR_FUNC_READ; + MmData[1] =3D ByteAddress + Count; + MmData[2] =3D NumRead; + MmData[3] =3D (UINT64)mTempBufferPhysical; // Read data into the temp= buffer with specified virtual address + + Status =3D FlashMmCommunicate ( + MmData, + sizeof (MmData), + &MmSpiNorRes, + sizeof (MmSpiNorRes) + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (MmSpiNorRes.Status !=3D MM_SPINOR_RES_SUCCESS) { + DEBUG ((DEBUG_ERROR, "%a: Device error %llx\n", __FUNCTION__, MmSpiN= orRes.Status)); + return EFI_DEVICE_ERROR; + } + + // + // Get data from the virtual address of the temp buffer. + // + CopyMem ((VOID *)(Buffer + Count), (VOID *)mTempBufferVirtual, NumRead= ); + Remain -=3D NumRead; + Count +=3D NumRead; + } + + return EFI_SUCCESS; +} diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib= .c b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.c new file mode 100644 index 000000000000..edea47313501 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.c @@ -0,0 +1,141 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include +#include +#include +#include +#include +#include + +#include "FlashLibCommon.h" + +extern BOOLEAN mIsEfiRuntime; +extern UINT8 *mTempBufferPhysical; +extern UINT8 *mTempBufferVirtual; + +STATIC EFI_MM_COMMUNICATION2_PROTOCOL *mMmCommunicationProtocol =3D NULL; + +/** + This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRESS= _CHANGE + event. It converts a pointer to a new virtual address. + + @param[in] Event Event whose notification function is being invok= ed. + @param[in] Context Pointer to the notification function's context +**/ +VOID +EFIAPI +FlashLibAddressChangeEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + gRT->ConvertPointer (0x0, (VOID **)&mTempBufferVirtual); + gRT->ConvertPointer (0x0, (VOID **)&mMmCommunicationProtocol); + + mIsEfiRuntime =3D TRUE; +} + +/** + Constructor function of the RuntimeFlashLib. + + @param[in] ImageHandle The image handle. + @param[in] SystemTable The system table. + + @retval EFI_SUCCESS Operation succeeded. + @retval Others An error has occurred. +**/ +EFI_STATUS +EFIAPI +FlashLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_EVENT VirtualAddressChangeEvent =3D NULL; + EFI_STATUS Status; + + mTempBufferPhysical =3D AllocateRuntimeZeroPool (EFI_MM_MAX_TMP_BUF_SIZE= ); + mTempBufferVirtual =3D mTempBufferPhysical; + ASSERT (mTempBufferPhysical !=3D NULL); + + Status =3D gBS->LocateProtocol ( + &gEfiMmCommunication2ProtocolGuid, + NULL, + (VOID **)&mMmCommunicationProtocol + ); + ASSERT_EFI_ERROR (Status); + + Status =3D gBS->CreateEvent ( + EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE, + TPL_CALLBACK, + FlashLibAddressChangeEvent, + NULL, + &VirtualAddressChangeEvent + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} + +/** + Provides an interface to access the Flash services via MM interface. + + @param[in] Request Pointer to the request buffer + @param[in] RequestDataSize Size of the request buffer. + @param[out] Response Pointer to the response buffer + @param[in] ResponseDataSize Size of the response buffer. + + @retval EFI_SUCCESS Operation succeeded. + @retval EFI_INVALID_PARAMETER An invalid data parameter or an invalid + combination of data parameters. + @retval Others An error has occurred. +**/ +EFI_STATUS +FlashMmCommunicate ( + IN VOID *Request, + IN UINT32 RequestDataSize, + OUT VOID *Response, + IN UINT32 ResponseDataSize + ) +{ + EFI_MM_COMMUNICATE_REQUEST CommBuffer; + EFI_STATUS Status; + + if (Request =3D=3D NULL || RequestDataSize =3D=3D 0 + || RequestDataSize > EFI_MM_MAX_PAYLOAD_SIZE + || (ResponseDataSize =3D=3D 0 && Response =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + CopyGuid (&CommBuffer.HeaderGuid, &gSpiNorMmGuid); + CommBuffer.MessageLength =3D RequestDataSize; + CopyMem (CommBuffer.Data, Request, RequestDataSize); + + if (mMmCommunicationProtocol =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + Status =3D mMmCommunicationProtocol->Communicate ( + mMmCommunicationProtocol, + &CommBuffer, + &CommBuffer, + NULL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (ResponseDataSize > 0) { + CopyMem (Response, CommBuffer.Data, ResponseDataSize); + } + + return EFI_SUCCESS; +} --=20 2.17.1