From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.109]) by mx.groups.io with SMTP id smtpd.web10.9738.1637167783792045599 for ; Wed, 17 Nov 2021 08:49:44 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@os.amperecomputing.com header.s=selector2 header.b=g0G40J8d; spf=pass (domain: os.amperecomputing.com, ip: 40.107.220.109, mailfrom: nhi@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Kk2f7W4BSt5a7cu8zzJyQmtlNy0MrPSseWcweE2XUMb1t4GaMsRSevy4GoFUBO4aLnxbWrqG4L1QFHm65RVSpr5YFfwiPhZ6XKAhc8BfaqmaNkNZ6s9szhTQB92T/eHJwTnNU3TGiQbyLbeaJyZN5B1FaXH0DAUXb7pIqNeFm0O+Ls5NlBcC/1YJmJWISIa4//FMNWix8nnhYxsgPYuoWZ1IEjIxBBLUOE7Mchmc+z+ORtxTbJm72qKesMV30PFfWsugdy3gdVEtw1pHKueCYpyIuHrVmKwcQuO3Mx01m4uobwLvZ06ATpux7XxeosOkLnMPLKvBIKz+/7jvMxphJA== 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=0UaUYjz3nOB+QTEDAtgxFEhCDvUkwrOgsPZ11fmnptQ=; b=c/9ms6LCHERBiyYwnTQc3Og46Ws2zbJFbqwAZawSP9q6L/tC602vmKh0hHbfAFVdxrujDsUCgNWhBJ2zB562FY99gTGC1bWaB20mCLOVTS19YQ8acSZ/NlsyLkVt82BXRoGKjUAz+B9pHQWc9N/w/UPNufF5DfqI86ecnOIAKmZCUdVUFswkgwOuHbmBRwX5cOPkFdIwL8jdRjTlLZ3iyMrhwLZYl1fKfrRKu2PGKlBocOJvAwrvKacv7Ju7+z15hWBnLLUm2GnNPZXhO6yY0zIvdztxkPdO+tapXndfvoO3QU50iQbGUKTmsFgOU50XSXEFAQV5pb557m00KId1zA== 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=0UaUYjz3nOB+QTEDAtgxFEhCDvUkwrOgsPZ11fmnptQ=; b=g0G40J8dwp1Q7fNVP+ZudIlJj0N2cszJXnh79dc0Lb7LcSTPVj/BiPkJTnGrikKc9JPfaSNkfEURbm4VKf130c5OWeTqtFBXraZEgfELOPVl6oDkEKTQWiVRC2VF01iWY32O8zG/XZDALLVdoac781xrQmfoHr8O3GIvWxFF4UA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from PH0PR01MB7287.prod.exchangelabs.com (2603:10b6:510:10a::21) by PH0PR01MB6454.prod.exchangelabs.com (2603:10b6:510:1b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4713.20; Wed, 17 Nov 2021 16:49:41 +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.4713.019; Wed, 17 Nov 2021 16:49:40 +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 v5 02/30] AmpereAltraPkg: Add FlashLib library instance Date: Wed, 17 Nov 2021 23:46:59 +0700 Message-ID: <20211117164727.10922-3-nhi@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211117164727.10922-1-nhi@os.amperecomputing.com> References: <20211117164727.10922-1-nhi@os.amperecomputing.com> X-ClientProxiedBy: HKAPR04CA0001.apcprd04.prod.outlook.com (2603:1096:203:d0::11) 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 HKAPR04CA0001.apcprd04.prod.outlook.com (2603:1096:203:d0::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4713.21 via Frontend Transport; Wed, 17 Nov 2021 16:49:37 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ab8b7cc9-8613-4dac-d370-08d9a9ea3f6d X-MS-TrafficTypeDiagnostic: PH0PR01MB6454: 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: Veu2aNSkQOjWu+qGs1ngU2Bnrj8/GIQO3LEoAqXRb6ohDmgWzl7MqfnA2DYaddDvQjYwwXFQd8O2GSzaQYWf9JIyAzH3bVEiC6wG3X0hX3oXbdDgoFs9x/rYKZ0ykQ+/jZbfGXgQSJBPUVUCtDoG2CWtrZKuk5S2SIAg5Dtt/TQEsaDqDj+Jz203vMDzq5C6PhPbETFQpr0kxHDN71kV6BWpymvmEY0Tz/fmNSXNSAfGBO20dmDts/F3bTsPjUweizu0VrJgMRsQkWScsvItMsSGsgaUkjQyuZC/Dc3ThP3lVcKnXlRn5TEH50/9dYHzb1zBeQvT1pEjVw4zO/IIFTjkID+bHD+Rf3RUo0bCBhwIEZRKDt0poGxDsI/23krIl+VyoMXnUPc0mWe23e3LhSz4rg6SWnXEgVMB/RgP+pOtAQDjPODZFW9He9X4qitkvqaUCOAhIs/IPyQDShPPJOFi4frcJHNS+QcUtilBSQCGaKjwRAbnU4zIxoz0cdEQpWB95vpSAaKIlszJA7W9dMUTXPLskPy/QL9Bqnb6AZT4eqYBWkDAtHhgsSVhOVH2G3EAQwOpF0qpBkKHRI3Xp8AsADuH+T5c9LX41g4G5mLeqZAQAEN1TH5yaXx/DCfc+XSnnfMaI7LxlFbYgAU2eKIg/GLdjPVdHC/IfXapfmsJwkp9wO9ygSe0FHZvDMTj8iW+0Vfk1YelSS0akp90BA== 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)(6666004)(8936002)(66476007)(52116002)(186003)(86362001)(2906002)(1076003)(83380400001)(54906003)(4326008)(66556008)(26005)(38100700002)(38350700002)(19627235002)(30864003)(6506007)(6916009)(6486002)(2616005)(956004)(5660300002)(6512007)(316002)(8676002)(66946007)(508600001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?S5J9VJU8/gN20UM0pDX1kIviMYhHHRWfWwASSkPfninoGFYuK6ZHhbGCxfxs?= =?us-ascii?Q?r+i5Xyb1/LLiT1wK2nfLVWs8PAAnlRPHkeMHRdTl51WMQYttN6dbSQ8ICaA/?= =?us-ascii?Q?1+OQ5C4S44jkUqtfMiZjx/Y2a99prEjFtTY/JvwjfB+emCUxacmuXO+9+Dng?= =?us-ascii?Q?9iLuG1eL25/KKnyN8+j4rs5OmO6vUilGcR3SzP1Zu8r6hmhJuxTJqQZwc4Nd?= =?us-ascii?Q?MNPYj3RChoFELIH0XN9Ui8pYG7dlUTuU/if1CgmmJtgwavbbm6of3MWy0wcj?= =?us-ascii?Q?6934K20NnILn6KoRGrHQkBwUesaZ1CkzTENvKLT6nB28dwTuNxDBuPUSrYj3?= =?us-ascii?Q?QOnOoZhXs2FE//M9ypj+FdJ8bZleJ24HUITcjy3Gi1hl+JdwEh65nrvDqlW9?= =?us-ascii?Q?tIx9zhVojmdydR+PzicOTA8orRkpAqVGgl5KHeqboltYI9vAOInMmznAoO4V?= =?us-ascii?Q?Qe5aUk2CBpqt639g+FyLD7Ey4Gp6HhIsFdP3JU4NXviRBchFu1hNhnzwgP/T?= =?us-ascii?Q?tXyfKm/aexsvl0XBzL4Ci53CHekq681amN8gBT2x3qLad5O+hV0FmXhj1yzQ?= =?us-ascii?Q?5AkswwxM8kWOsfym8vwNbWiccKNgiebpR46qgcnkUVG1eAa9TNYGg0InHao5?= =?us-ascii?Q?JjJM0azsu63+eRvqAXC96vGJQf6ax/TWcuM32SAURDbE9HejxtwTmvjGJbVn?= =?us-ascii?Q?uBVDl8aCvhzrtvUI/1uwgu8X9aVIR8RXSM7Oa7hscFXl7KeRd+k/O7zuIrRd?= =?us-ascii?Q?KMm9WWmF4CXmw5FR4dFX2JwXLnPeDBE0TmnV75MB97LxQZ1y8K3QJz9iYgcJ?= =?us-ascii?Q?D6Mo/uLJW7stuOTaNpxzsADAeosdhwPgAu1pO4Rq/l3CHqsi0U36GSOT/32z?= =?us-ascii?Q?qigbP2HEjOL3NRZn5VIakv8iJ+0AwwmN27LD3DU2JIJIsHuarPxPhQnAch/H?= =?us-ascii?Q?Niwwc2ng9a7cbVW5S6sLl1FlpLFGBUO0qZWj4yA6lODd4Mx77qalOU6rru0g?= =?us-ascii?Q?4uhX1NPvuHEiH733N+rrLdpOvITZHXT+LjvazZ2WQBiM46OK9wHM68Np9I7j?= =?us-ascii?Q?4IixICM4EXuJOtnEYVsQu2+LQnvxASUbd1icocBfOqr6k7Yo+hgWjP/RGpdQ?= =?us-ascii?Q?jmUxXa6CKfdBjWjjvI8vtdin6BhT8/lW/hjXvLebipeMjBrk9ZniFjDgIEZt?= =?us-ascii?Q?KdDH3NUmEt2RLE7J6n7Ygua+6ww82HN3ozf35dVLRvwNxvREx+vGkNiEi4hz?= =?us-ascii?Q?nrscWNaeXbMPrqcUHtm0R9F7ZYe2k6mIv0B3S9JhE3Jr8K0IX1A+sUzZ9I/r?= =?us-ascii?Q?Shq0i0Ekp+WMFRL7VOcM4Sow1MkgdVGZ71DYXQa54xercrZb2OQ1/DqomKK7?= =?us-ascii?Q?OcNJvAeNBTMsvP6lRJTPfE9YmBk04bfOZ043Eb3vDvBDXjZ6DrsM6I2NsQHj?= =?us-ascii?Q?DjVc49UIUTraT+sPgc7rV6R3PdL0VUxqdLZnh+eQD1vf7/DxPZfu6l2SmWES?= =?us-ascii?Q?fOY2y4bMu66YeS7dAx3z5rPr24sC8e6L645YJzSKvGOi3vE7q8XinhFjSLJ/?= =?us-ascii?Q?FTcZGEsiauqW6cYgXHaJcje0q1UCAmVmjjzKe5OxY9Fu/9ywKrTYWdA3bxRG?= =?us-ascii?Q?wlEWqsCrYCBN95RhcvnP/wEndyEbKWMaJ1kerGN4kXfjRiSDLhRv3Woeh+k6?= =?us-ascii?Q?75UuXA=3D=3D?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: ab8b7cc9-8613-4dac-d370-08d9a9ea3f6d X-MS-Exchange-CrossTenant-AuthSource: PH0PR01MB7287.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Nov 2021 16:49:40.3837 (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: A+J0ynRutJraMHzPGJILPje1j0qNlcGimLx28TiJbA63uq44YdSyqxU1E46vXGg/80AZKS0TaC53J6S/1uQ5/8FhieILe8IUWZ8/Lf3d/Xc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR01MB6454 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 | 92 += ++++ Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.c | 83 += ++++ Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.c | 372 += +++++++++++++++++++ Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.c | 137 += ++++++ 9 files changed, 879 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 15f649d97f30..cf1be4df8f27 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..327429a8f998 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLibCommon.h @@ -0,0 +1,92 @@ +/** @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() + +extern BOOLEAN gFlashLibRuntime; +extern UINT8 *gFlashLibPhysicalBuffer; +extern UINT8 *gFlashLibVirtualBuffer; + +/** + 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..6c8a79699cd4 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/FlashLib.c @@ -0,0 +1,83 @@ +/** @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" + +/** + Constructor function of the FlashLib + + @retval EFI_SUCCESS The constructor executes successfully. +**/ +EFI_STATUS +EFIAPI +FlashLibConstructor ( + VOID + ) +{ + gFlashLibPhysicalBuffer =3D AllocateZeroPool (EFI_MM_MAX_TMP_BUF_SIZE); + gFlashLibVirtualBuffer =3D gFlashLibPhysicalBuffer; + ASSERT (gFlashLibPhysicalBuffer !=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..83695c857a11 --- /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 gFlashLibRuntime =3D FALSE; +UINT8 *gFlashLibPhysicalBuffer; +UINT8 *gFlashLibVirtualBuffer; + +/** + 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 (gFlashLibRuntime) { + ASSERT (VirtualPtr !=3D NULL); + CopyMem (gFlashLibVirtualBuffer, VirtualPtr, Size); + return gFlashLibPhysicalBuffer; + } + + 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)gFlashLibPhysicalBuffer; // 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 *)gFlashLibVirtualBuffer, Num= Read); + 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..2dcd13e08146 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/FlashLib/RuntimeFlashLib.c @@ -0,0 +1,137 @@ +/** @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" + +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 **)&gFlashLibVirtualBuffer); + gRT->ConvertPointer (0x0, (VOID **)&mMmCommunicationProtocol); + + gFlashLibRuntime =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; + + gFlashLibPhysicalBuffer =3D AllocateRuntimeZeroPool (EFI_MM_MAX_TMP_BUF_= SIZE); + gFlashLibVirtualBuffer =3D gFlashLibPhysicalBuffer; + ASSERT (gFlashLibPhysicalBuffer !=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