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