]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/MemoryInitPei/MemoryInit.c
ArmPlatformPkg/ArmPlatformLib: Move the UEFI memory partionning to MemoryInitPei
[mirror_edk2.git] / ArmPlatformPkg / MemoryInitPei / MemoryInit.c
CommitLineData
1d5d0ae9 1/** @file\r
2*\r
3* Copyright (c) 2011, ARM Limited. All rights reserved.\r
4* \r
5* This program and the accompanying materials \r
6* are licensed and made available under the terms and conditions of the BSD License \r
7* which accompanies this distribution. The full text of the license may be found at \r
8* http://opensource.org/licenses/bsd-license.php \r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12*\r
13**/\r
14\r
15//\r
16// The package level header files this module uses\r
17//\r
18#include <PiPei.h>\r
19//\r
20// The protocols, PPI and GUID defintions for this module\r
21//\r
22#include <Ppi/MasterBootMode.h>\r
23#include <Ppi/BootInRecoveryMode.h>\r
24#include <Guid/MemoryTypeInformation.h>\r
25//\r
26// The Library classes this module consumes\r
27//\r
28#include <Library/DebugLib.h>\r
29#include <Library/PeimEntryPoint.h>\r
30#include <Library/PcdLib.h>\r
31#include <Library/HobLib.h>\r
32#include <Library/PeiServicesLib.h>\r
33#include <Library/ArmLib.h>\r
34#include <Library/IoLib.h>\r
964680c1 35#include <Library/MemoryAllocationLib.h>\r
1d5d0ae9 36#include <Library/ArmPlatformLib.h>\r
37\r
38//\r
39// Module globals\r
40//\r
41\r
42VOID\r
43InitMmu (\r
44 VOID\r
45 )\r
46{\r
47 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;\r
48 VOID *TranslationTableBase;\r
49 UINTN TranslationTableSize;\r
50\r
51 // Get Virtual Memory Map from the Platform Library\r
52 ArmPlatformGetVirtualMemoryMap(&MemoryTable);\r
53\r
54 //Note: Because we called PeiServicesInstallPeiMemory() before to call InitMmu() the MMU Page Table resides in\r
55 // DRAM (even at the top of DRAM as it is the first permanent memory allocation)\r
56 ArmConfigureMmu (MemoryTable, &TranslationTableBase, &TranslationTableSize);\r
57}\r
58\r
59// May want to put this into a library so you only need the PCD setings if you are using the feature?\r
60VOID\r
61BuildMemoryTypeInformationHob (\r
62 VOID\r
63 )\r
64{\r
65 EFI_MEMORY_TYPE_INFORMATION Info[10];\r
66\r
67 Info[0].Type = EfiACPIReclaimMemory;\r
68 Info[0].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIReclaimMemory);\r
69 Info[1].Type = EfiACPIMemoryNVS;\r
70 Info[1].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiACPIMemoryNVS);\r
71 Info[2].Type = EfiReservedMemoryType;\r
72 Info[2].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiReservedMemoryType);\r
73 Info[3].Type = EfiRuntimeServicesData;\r
74 Info[3].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesData);\r
75 Info[4].Type = EfiRuntimeServicesCode;\r
76 Info[4].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiRuntimeServicesCode);\r
77 Info[5].Type = EfiBootServicesCode;\r
78 Info[5].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesCode);\r
79 Info[6].Type = EfiBootServicesData;\r
80 Info[6].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiBootServicesData);\r
81 Info[7].Type = EfiLoaderCode;\r
82 Info[7].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderCode);\r
83 Info[8].Type = EfiLoaderData;\r
84 Info[8].NumberOfPages = PcdGet32 (PcdMemoryTypeEfiLoaderData);\r
85 \r
86 // Terminator for the list\r
87 Info[9].Type = EfiMaxMemoryType;\r
88 Info[9].NumberOfPages = 0;\r
89\r
90\r
91 BuildGuidDataHob (&gEfiMemoryTypeInformationGuid, &Info, sizeof (Info));\r
92}\r
93\r
1d5d0ae9 94/*++\r
95\r
96Routine Description:\r
97\r
98 \r
99\r
100Arguments:\r
101\r
102 FileHandle - Handle of the file being invoked.\r
103 PeiServices - Describes the list of possible PEI Services.\r
104 \r
105Returns:\r
106\r
107 Status - EFI_SUCCESS if the boot mode could be set\r
108\r
109--*/\r
964680c1 110EFI_STATUS\r
111EFIAPI\r
112InitializeMemory (\r
113 IN EFI_PEI_FILE_HANDLE FileHandle,\r
114 IN CONST EFI_PEI_SERVICES **PeiServices\r
115 )\r
1d5d0ae9 116{\r
964680c1 117 EFI_STATUS Status;\r
118 EFI_RESOURCE_ATTRIBUTE_TYPE Attributes;\r
119 ARM_SYSTEM_MEMORY_REGION_DESCRIPTOR* EfiMemoryMap;\r
120 UINTN Index;\r
121 UINTN SystemMemoryTop;\r
122 UINTN UefiMemoryBase;\r
123 UINTN UefiMemorySize;\r
1d5d0ae9 124\r
125 DEBUG ((EFI_D_ERROR, "Memory Init PEIM Loaded\n"));\r
126\r
964680c1 127 // Ensure PcdSystemMemorySize has been set\r
128 ASSERT (FixedPcdGet32 (PcdSystemMemorySize) != 0);\r
129\r
130 SystemMemoryTop = FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemorySize);\r
131\r
132 //\r
133 // Initialize the System Memory (DRAM)\r
134 //\r
1d5d0ae9 135 if (FeaturePcdGet(PcdStandalone)) {\r
964680c1 136 // In case of a standalone version, the DRAM is already initialized\r
1d5d0ae9 137 ArmPlatformInitializeSystemMemory();\r
138 }\r
139\r
964680c1 140 //\r
141 // Declare the UEFI memory to PEI\r
142 //\r
143 if (FeaturePcdGet(PcdStandalone)) {\r
144 // In case of standalone UEFI, we set the UEFI memory region at the top of the DRAM\r
145 UefiMemoryBase = SystemMemoryTop - FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);\r
146 } else {\r
147 // In case of a non standalone UEFI, we set the UEFI memory below the Firmware Volume\r
148 UefiMemoryBase = FixedPcdGet32 (PcdNormalFdBaseAddress) - FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);\r
149 }\r
150 UefiMemorySize = FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);\r
151 Status = PeiServicesInstallPeiMemory (UefiMemoryBase,UefiMemorySize);\r
1d5d0ae9 152 ASSERT_EFI_ERROR (Status);\r
153\r
154 //\r
155 // Now, the permanent memory has been installed, we can call AllocatePages()\r
156 //\r
964680c1 157 Attributes = (\r
158 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
159 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
160 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
161 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
162 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
163 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
164 EFI_RESOURCE_ATTRIBUTE_TESTED\r
165 );\r
166\r
167 // If it is not a standalone build we must reserved the space above the base address of the firmware volume\r
168 if (!FeaturePcdGet(PcdStandalone)) {\r
169 // Check if firmware volume has not be copied at the top of DRAM then we must reserve the extra space\r
170 // between the firmware and the top\r
171 if (SystemMemoryTop != FixedPcdGet32 (PcdNormalFdBaseAddress) + FixedPcdGet32 (PcdNormalFdSize)) {\r
172 BuildResourceDescriptorHob (\r
173 EFI_RESOURCE_SYSTEM_MEMORY,\r
174 Attributes & (~EFI_RESOURCE_ATTRIBUTE_TESTED),\r
175 FixedPcdGet32 (PcdNormalFdBaseAddress) + FixedPcdGet32 (PcdNormalFdSize),\r
176 SystemMemoryTop - (FixedPcdGet32 (PcdNormalFdBaseAddress) + FixedPcdGet32 (PcdNormalFdSize))\r
177 );\r
178 }\r
179\r
180 // Reserved the memory space occupied by the firmware volume\r
181 BuildResourceDescriptorHob (\r
182 EFI_RESOURCE_SYSTEM_MEMORY,\r
183 Attributes & (~EFI_RESOURCE_ATTRIBUTE_PRESENT),\r
184 (UINT32)FixedPcdGet32 (PcdNormalFdBaseAddress),\r
185 (UINT32)FixedPcdGet32 (PcdNormalFdSize)\r
186 );\r
187 }\r
1d5d0ae9 188\r
964680c1 189 // Check there is no overlap between UEFI and Fix Address Regions\r
190 ASSERT (FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemoryFixRegionSize) <= UefiMemoryBase);\r
191\r
192 // Reserved the UEFI Memory Region\r
193 BuildResourceDescriptorHob (\r
194 EFI_RESOURCE_SYSTEM_MEMORY,\r
195 Attributes,\r
196 UefiMemoryBase,\r
197 UefiMemorySize\r
198 );\r
199\r
200 // Reserved the Fix Address Region\r
201 BuildResourceDescriptorHob (\r
202 EFI_RESOURCE_SYSTEM_MEMORY,\r
203 Attributes,\r
204 FixedPcdGet32 (PcdSystemMemoryBase),\r
205 FixedPcdGet32 (PcdSystemMemoryFixRegionSize)\r
206 );\r
207\r
208 // Reserved the memory between UEFI and Fix Address regions\r
209 if (FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemoryFixRegionSize) != UefiMemoryBase) {\r
1d5d0ae9 210 BuildResourceDescriptorHob (\r
211 EFI_RESOURCE_SYSTEM_MEMORY,\r
964680c1 212 Attributes & (~EFI_RESOURCE_ATTRIBUTE_TESTED),\r
213 FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemoryFixRegionSize),\r
214 UefiMemoryBase - (FixedPcdGet32 (PcdSystemMemoryBase) + FixedPcdGet32 (PcdSystemMemoryFixRegionSize))\r
1d5d0ae9 215 );\r
216 }\r
217\r
964680c1 218 // If a platform has system memory extensions, it can declare those in this function\r
219 Status = ArmPlatformGetAdditionalSystemMemory (&EfiMemoryMap);\r
220 if (!EFI_ERROR(Status)) {\r
221 // Install the EFI Memory Map\r
222 for (Index = 0; EfiMemoryMap[Index].ResourceAttribute != 0; Index++) {\r
223 BuildResourceDescriptorHob (\r
224 EFI_RESOURCE_SYSTEM_MEMORY,\r
225 EfiMemoryMap[Index].ResourceAttribute,\r
226 EfiMemoryMap[Index].PhysicalStart,\r
227 EfiMemoryMap[Index].NumberOfBytes\r
228 );\r
229 }\r
230 FreePool (EfiMemoryMap);\r
231 }\r
232\r
1d5d0ae9 233 // Build Memory Allocation Hob\r
234 InitMmu ();\r
235\r
236 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {\r
237 // Optional feature that helps prevent EFI memory map fragmentation. \r
238 BuildMemoryTypeInformationHob ();\r
239 }\r
240\r
964680c1 241 return EFI_SUCCESS;\r
1d5d0ae9 242}\r