From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (EUR05-AM6-obe.outbound.protection.outlook.com [40.107.22.75]) by mx.groups.io with SMTP id smtpd.web11.4677.1594185598522568861 for ; Tue, 07 Jul 2020 22:19:59 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@nxp1.onmicrosoft.com header.s=selector2-nxp1-onmicrosoft-com header.b=G4duUAmi; spf=pass (domain: oss.nxp.com, ip: 40.107.22.75, mailfrom: pankaj.bansal@oss.nxp.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F+b2QIby92yEiTpW5i5ZuWETHdcRPByhW0ci9Iz6aOI+pXNJngFG9FN2FOutNY8pnpVYPbReAwyqxFbcVYF2Fp+EF7viEyWAD5mIqk4OYVpYSAVZEMeZfL+Ky0gO7lSpjQ4WgkAyOtkvQ90mGUQf9rpeh65Z5qPwbi6m0zqFC8+spth4bnjLfkVSL/aqGA8IDGgNaN91DgbFVI4lTsNL37uCPeOpFf8dhS5KeZp4X46sDMv07tE6r4VhMIKUcRJHw6mADd/WYDDsFrrl27Ps4EzDDNGnWFLwzN+fByda26N6NnEpApN/CB9XL810zvL5CGnnfQxLE9x9otkOrjdRsA== 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-SenderADCheck; bh=uVIrPZ2m1RNprC5nLXJqQyI/qs1w+4/re/BdMxcxEOE=; b=jpBtGj53kGFRUfDni5JZcN/ZnvMTONqDkCb3urSa2933hXmuV/6pmylr5LcxoUVPebDyLuF7mbFk82VDC3z3U8PgqwfCvxoiaVBCkmd0/riQW1l9CYKenTYJriNJEQ3vZNg9zgqQDxih7WuXwEPtCh/wC1T5VY8WPXcc7+LiWdlwjcrVqMCmjoxGUt4DzljacwnOL/Uu2FuNC8btnxCJxW8pTRJHVtvd3yMGFP8HuK0/xT/huQ9n7lSEyoniSjrkzggtiytS9Z6rFTtmkVBoe4vkvq148iIkM40Vd2Y5W4qcxNbULKh6AlsEHT1sRq/sTnZv3LBB+PDYplIveyviBw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector2-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uVIrPZ2m1RNprC5nLXJqQyI/qs1w+4/re/BdMxcxEOE=; b=G4duUAmiyOHAg8KB4U+b5zacZJutsRcjgPMaZ6uIQYZIyUqo6SYVQRMs4T+9EX918wQR3LbKbQSOj6V2vSu+SZnwaFc7beCAEfY7MlCVuKhxsQGy/Nxv8eOEV0PuZy39mqBGQx0GATGuAT2WnnKsQRiYmRQZ5cle1NV0fop+7v8= Authentication-Results: nuviainc.com; dkim=none (message not signed) header.d=none;nuviainc.com; dmarc=none action=none header.from=oss.nxp.com; Received: from VI1PR04MB5933.eurprd04.prod.outlook.com (2603:10a6:803:ec::16) by VI1PR04MB3248.eurprd04.prod.outlook.com (2603:10a6:802:11::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3174.20; Wed, 8 Jul 2020 05:19:56 +0000 Received: from VI1PR04MB5933.eurprd04.prod.outlook.com ([fe80::4521:b667:cf06:b79b]) by VI1PR04MB5933.eurprd04.prod.outlook.com ([fe80::4521:b667:cf06:b79b%7]) with mapi id 15.20.3174.021; Wed, 8 Jul 2020 05:19:56 +0000 From: "Pankaj Bansal" To: Leif Lindholm , Meenakshi Aggarwal , devel@edk2.groups.io, Ard Biesheuvel Subject: [PATCH edk2-platforms 2/3] Silicon/NXP: Add support for reserving a chunk from RAM Date: Wed, 8 Jul 2020 00:19:32 -0500 Message-ID: <20200708051933.8123-3-pankaj.bansal@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200708051933.8123-1-pankaj.bansal@oss.nxp.com> References: <20200708051933.8123-1-pankaj.bansal@oss.nxp.com> X-ClientProxiedBy: BY5PR16CA0015.namprd16.prod.outlook.com (2603:10b6:a03:1a0::28) To VI1PR04MB5933.eurprd04.prod.outlook.com (2603:10a6:803:ec::16) Return-Path: pankaj.bansal@oss.nxp.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from TFTPTOUEFI.am.freescale.net (64.157.242.222) by BY5PR16CA0015.namprd16.prod.outlook.com (2603:10b6:a03:1a0::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3153.24 via Frontend Transport; Wed, 8 Jul 2020 05:19:54 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [64.157.242.222] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 8842846c-4b3a-4193-1b9f-08d822fe8d24 X-MS-TrafficTypeDiagnostic: VI1PR04MB3248: X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qNMM00ZYEGOUWyrnSra0PTXGcKoSFjiwI8MT1wJfiBbmo9x7VsRivbTzwE/0sgFSScK0JqQOLOP7oBLbyfYcqcJIbnT0443Wy/ZO3CV97s8c8XywUnXaf70AtYpTOLpiygUvgascPOkiQq1OXsQ2PJjHS9eGGBKX1U5KNOvRo6m3vMAjam5zYdfCViU5kuVtxy/HOd970B1w2aX3Xqb+WDJCKEgV0gU0TRvu+v23BHtTgkZKrLxoK6NfSRYCKvk9pKtVkHZiJ59G285z814eJcdWRCawRaovjAkRS0r/a14HuhsnsYnjhp1NcMqxJpv5x8ljWXZEHFM8xtS6OeWMng== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI1PR04MB5933.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(346002)(39860400002)(136003)(366004)(376002)(396003)(16526019)(52116002)(956004)(2616005)(5660300002)(19627235002)(83380400001)(66556008)(26005)(2906002)(478600001)(66476007)(6506007)(66946007)(110136005)(1076003)(44832011)(186003)(316002)(6512007)(6486002)(86362001)(8676002)(6666004)(8936002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: ku5pd3+ylVtN6kIHN6vyhLmcxKBd0goqeRdMTe5mJ5/BOWM0fwEHklOamz5X7IWo6H614SVNDV0L34nkUdyVLBeO1tlvZYEAp4A0CQcOgG464l/cYAk1j253Y/B9DoBUbHDZnwF46H7fid11uXZeMzbSd1o4kXdbtvme71wS+/htonJyvEEAtAt8zXk3FxGkbHDepOTqdKUIX8vOGjTmT2VZP5fM8msVrq5aaQYjZGoy4y+UDgB20tFqjfO3nOtEz8ZrebSwEtPrffmVpnVbSov0AWIn+Hv/e3L4vPMGprB6lovCuRiXZTY7hwY1Yigy0uLFMusn5RpQjRqpfcYQxQNho3Nsz08jR6ogYSgIr/Yu+/OYSwM12p5YQHHcex35yA86lp53lP1nB8mMJgv6/QRDwniWwdk17FdWwZaQzcFJcak/puVkAcf+aSt/i8ex6d99cA8UxS7CjYInPhAwYmE1xxCjlejl9fEvPxatmsI= X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8842846c-4b3a-4193-1b9f-08d822fe8d24 X-MS-Exchange-CrossTenant-AuthSource: VI1PR04MB5933.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jul 2020 05:19:55.9845 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: j+IpmLt4zXV3H5FJrYgzrilPj9J10XDEQgZuxvziS86cJtM9q7mL9tPCgTfmGPfEP6HbnZ0abOPUOkwBMJMUNw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB3248 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain From: Pankaj Bansal Some NXP SOCs have some specialized IP blocks (like MC), which require DDR memory to operate. This DDR memory should not be managed by OS or UEFI. Moreover to ensure that these IP blocks always get memory, and maximum contiguous RAM is available for UEFI and OS to use, add the support for reserving a chunk from RAM before reporting available RAM to UEFI. Signed-off-by: Pankaj Bansal --- Silicon/NXP/NxpQoriqLs.dec | 10 ++ Silicon/NXP/LX2160A/LX2160A.dsc.inc | 4 + Silicon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf | 3 + Silicon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.c | 142 ++++++++++= +++++++++- 4 files changed, 157 insertions(+), 2 deletions(-) diff --git a/Silicon/NXP/NxpQoriqLs.dec b/Silicon/NXP/NxpQoriqLs.dec index 188a9fe1f382..0e762066e547 100644 --- a/Silicon/NXP/NxpQoriqLs.dec +++ b/Silicon/NXP/NxpQoriqLs.dec @@ -41,3 +41,13 @@ [PcdsDynamic.common] gNxpQoriqLsTokenSpaceGuid.PcdPciCfgShiftEnable|FALSE|BOOLEAN|0x00000600 gNxpQoriqLsTokenSpaceGuid.PcdPciLsGen4Ctrl|FALSE|BOOLEAN|0x00000601 gNxpQoriqLsTokenSpaceGuid.PcdPciHideRootPort|FALSE|BOOLEAN|0x00000602 + + # Reserved RAM Base address alignment. This number ought to be Power of = two + # in case no alignment is needed, this number should be 1. + gNxpQoriqLsTokenSpaceGuid.PcdReservedMemAlignment|0x1|UINT64|0x00000603 + # Size of the RAM to be reserved. This RAM region is neither reported to= UEFI + # nor to OS + gNxpQoriqLsTokenSpaceGuid.PcdReservedMemSize|0x0|UINT64|0x00000604 + # Reserved RAM Base address which is calculated based on PcdReservedMemS= ize + # and PcdReservedMemAlignment + gNxpQoriqLsTokenSpaceGuid.PcdReservedMemBase|0x0|UINT64|0x00000605 diff --git a/Silicon/NXP/LX2160A/LX2160A.dsc.inc b/Silicon/NXP/LX2160A/LX21= 60A.dsc.inc index 43e361464c8e..755ca169f213 100644 --- a/Silicon/NXP/LX2160A/LX2160A.dsc.inc +++ b/Silicon/NXP/LX2160A/LX2160A.dsc.inc @@ -29,6 +29,10 @@ [PcdsDynamicDefault.common] gArmTokenSpaceGuid.PcdGicRedistributorsBase|0x6200000 gArmTokenSpaceGuid.PcdGicInterruptInterfaceBase|0xC0C0000 =20 +[PcdsDynamicHii] + gNxpQoriqLsTokenSpaceGuid.PcdReservedMemAlignment|L"ReservedMemAlignment= "|gEfiGlobalVariableGuid|0x0|0x20000000|NV,BS + gNxpQoriqLsTokenSpaceGuid.PcdReservedMemSize|L"ReservedMemSize"|gEfiGlob= alVariableGuid|0x0|0x20000000|NV,BS + [PcdsFixedAtBuild.common] gArmTokenSpaceGuid.PcdGenericWatchdogControlBase|0x23A0000 gArmTokenSpaceGuid.PcdGenericWatchdogRefreshBase|0x2390000 diff --git a/Silicon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf b/Si= licon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf index a33f8cd3f743..ed23a86b43d9 100644 --- a/Silicon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf +++ b/Silicon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.inf @@ -49,6 +49,9 @@ [FixedPcd] [Pcd] gArmTokenSpaceGuid.PcdSystemMemoryBase gArmTokenSpaceGuid.PcdSystemMemorySize + gNxpQoriqLsTokenSpaceGuid.PcdReservedMemAlignment + gNxpQoriqLsTokenSpaceGuid.PcdReservedMemBase + gNxpQoriqLsTokenSpaceGuid.PcdReservedMemSize =20 [Depex] TRUE diff --git a/Silicon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.c b/Sili= con/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.c index 11d1f1260b35..b416323a4ced 100644 --- a/Silicon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.c +++ b/Silicon/NXP/Library/MemoryInitPeiLib/MemoryInitPeiLib.c @@ -118,6 +118,127 @@ GetDramRegionsInfo ( return EFI_BUFFER_TOO_SMALL; } =20 +/** + Calculate the base address of Reserved RAM. + Reserved RAM is not reported to either UEFI or OS. + + @param[in, out] DramRegions Array of type DRAM_REGION_INFO. The size of= this + array must be one more (+ 1) than the maxim= um + regions supported on platform. This is beca= use, + if due to Reserved RAM alignment requiremen= ts a + hole is created in any DRAM region, then th= e RAM + after hole gets reported to UEFI and then + subsequently to OS. which is why, the last = entry + of this array will not be parsed while + calculating Reserved RAM base address. Call= er + must ensure that last entry of this array i= s zero + initialized. + @param[in] NumRegions Size of DramRegions array (including +1 for= hole) + @param[in] ReservedMemSize Size of RAM to be reserved. + + @return if successful Address of the Reserved RAM region, 0 otherwise. +**/ +STATIC +UINTN +CalculateReservedMemBase ( + IN DRAM_REGION_INFO *DramRegions, + IN UINT32 NumRegions, + IN UINTN ReservedMemSize +) +{ + UINTN ReservedMemAlignment; + EFI_PHYSICAL_ADDRESS AlignmentMask; + UINTN RegionBaseAddress; + UINTN RegionSize; + UINTN ReservedBaseAddress; + INTN Index; + INTN Index2; + + ReservedMemAlignment =3D PcdGet64 (PcdReservedMemAlignment); + // + // Compute alignment bit mask + // + if (ReservedMemAlignment) { + AlignmentMask =3D LShiftU64 (1, LowBitSet64(ReservedMemAlignment)) - 1= ; + } else { + AlignmentMask =3D 0; + } + + // The DRAM region info is sorted based on the RAM address is SOC memory= map. + // i.e. DramRegions[0] is at lower address, as compared to DramRegions[1= ]. + // The goal to start from last region is to find the topmost RAM region = that + // can contain Reserved RAM region i.e. PcdReservedMemSize. + // Since this RAM is not reported to either UEFI or OS, This ensures tha= t + // maximum amount of lower RAM (32 bit addresses) are left + // for OS to allocate to devices that can only work with 32bit physical + // addresses. E.g. legacy devices that need to DMA to 32bit addresses. + for (Index =3D NumRegions - 2; Index >=3D0; Index--) { + RegionBaseAddress =3D DramRegions[Index].BaseAddress; + RegionSize =3D DramRegions[Index].Size; + + if (ReservedMemSize > RegionSize) { + continue; + } + + ReservedBaseAddress =3D (RegionBaseAddress + RegionSize - ReservedMemS= ize); + ReservedBaseAddress &=3D (~AlignmentMask); + if (ReservedBaseAddress < RegionBaseAddress) { + continue; + } + + // found the region from which reserved mem is to be carved out + // Need to modify the region size and create/delete region if need be + + RegionSize -=3D ReservedMemSize; + if (RegionSize =3D=3D 0) { + // delete the region but maintain the sorted list of regions + for (Index2 =3D Index; Index2 < NumRegions; Index2++) { + CopyMem ( + &DramRegions[Index2], + &DramRegions[Index2 + 1], + sizeof (DRAM_REGION_INFO) + ); + } + break; + } + + if (ReservedBaseAddress - RegionBaseAddress) { + DramRegions[Index].Size =3D ReservedBaseAddress - RegionBaseAddress; + RegionSize -=3D DramRegions[Index].Size; + } else { + DramRegions[Index].BaseAddress =3D ReservedBaseAddress + ReservedMem= Size; + DramRegions[Index].Size =3D RegionSize; + RegionSize =3D 0; + } + + if (RegionSize =3D=3D 0) { + break; + } + + // A hole has been created in DRAM regions due to Reserved RAM alignme= nt + // requirements. create a new DRAM region for DRAM memory after hole. + // Maintain the sorted list of regions + for (Index2 =3D NumRegions; Index2 > (Index + 1); Index2--) { + CopyMem ( + &DramRegions[Index2], + &DramRegions[Index2 - 1], + sizeof (DRAM_REGION_INFO) + ); + } + DramRegions[Index2].BaseAddress =3D ReservedBaseAddress + ReservedMemS= ize; + DramRegions[Index2].Size =3D RegionSize; + RegionSize =3D 0; + + break; + } + + if (Index =3D=3D -1) { + return 0; + } else { + return ReservedBaseAddress; + } +} + /** Get the installed RAM information. Initialize Memory HOBs (Resource Descriptor HOBs) @@ -135,7 +256,9 @@ MemoryInitPeiLibConstructor ( UINTN BaseAddress; UINTN Size; UINTN Top; - DRAM_REGION_INFO DramRegions[MAX_DRAM_REGIONS]; + // Extra region gets created if we want to reserve a memory region and t= hat + // creates a memory hole because of alignment requirements + DRAM_REGION_INFO DramRegions[MAX_DRAM_REGIONS + 1]; EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes; UINTN FdBase; UINTN FdTop; @@ -155,6 +278,21 @@ MemoryInitPeiLibConstructor ( =20 (VOID)GetDramRegionsInfo (DramRegions, ARRAY_SIZE (DramRegions)); =20 + // Get the reserved memory size from non volatile storage + Size =3D PcdGet64 (PcdReservedMemSize); + if (Size) { + BaseAddress =3D CalculateReservedMemBase ( + DramRegions, + ARRAY_SIZE (DramRegions), + Size + ); + if (BaseAddress) { + DEBUG ((DEBUG_INFO, "ReservedMem: start 0x%lx, size 0x%lx\n", + BaseAddress, Size)); + PcdSet64S (PcdReservedMemBase, BaseAddress); + } + } + FdBase =3D (UINTN)PcdGet64 (PcdFdBaseAddress); FdTop =3D FdBase + (UINTN)PcdGet32 (PcdFdSize); =20 @@ -168,7 +306,7 @@ MemoryInitPeiLibConstructor ( // This ensures that maximum amount of lower RAM (32 bit addresses) are = left // for OS to allocate to devices that can only work with 32bit physical // addresses. E.g. legacy devices that need to DMA to 32bit addresses. - for (Index =3D MAX_DRAM_REGIONS - 1; Index >=3D 0; Index--) { + for (Index =3D MAX_DRAM_REGIONS; Index >=3D 0; Index--) { if (DramRegions[Index].Size =3D=3D 0) { continue; } --=20 2.17.1