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