3 * Copyright (c) 2011, ARM Limited. All rights reserved.
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
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.
16 // The package level header files this module uses
21 // The protocols, PPI and GUID defintions for this module
23 #include <Ppi/MasterBootMode.h>
24 #include <Ppi/BootInRecoveryMode.h>
25 #include <Guid/MemoryTypeInformation.h>
27 // The Library classes this module consumes
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>
44 ARM_MEMORY_REGION_DESCRIPTOR
*MemoryTable
;
45 VOID
*TranslationTableBase
;
46 UINTN TranslationTableSize
;
48 // Get Virtual Memory Map from the Platform Library
49 ArmPlatformGetVirtualMemoryMap(&MemoryTable
);
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
);
56 // May want to put this into a library so you only need the PCD settings if you are using the feature?
58 BuildMemoryTypeInformationHob (
62 EFI_MEMORY_TYPE_INFORMATION Info
[10];
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
);
83 // Terminator for the list
84 Info
[9].Type
= EfiMaxMemoryType
;
85 Info
[9].NumberOfPages
= 0;
87 BuildGuidDataHob (&gEfiMemoryTypeInformationGuid
, &Info
, sizeof (Info
));
97 FileHandle - Handle of the file being invoked.
98 PeiServices - Describes the list of possible PEI Services.
102 Status - EFI_SUCCESS if the boot mode could be set
108 IN EFI_PEI_FILE_HANDLE FileHandle
,
109 IN CONST EFI_PEI_SERVICES
**PeiServices
113 EFI_RESOURCE_ATTRIBUTE_TYPE Attributes
;
114 ARM_SYSTEM_MEMORY_REGION_DESCRIPTOR
* EfiMemoryMap
;
116 UINTN SystemMemoryTop
;
117 UINTN UefiMemoryBase
;
118 UINTN UefiMemorySize
;
120 DEBUG ((EFI_D_ERROR
, "Memory Init PEIM Loaded\n"));
122 // Ensure PcdSystemMemorySize has been set
123 ASSERT (FixedPcdGet32 (PcdSystemMemorySize
) != 0);
125 SystemMemoryTop
= FixedPcdGet32 (PcdSystemMemoryBase
) + FixedPcdGet32 (PcdSystemMemorySize
);
128 // Initialize the System Memory (DRAM)
130 if (FeaturePcdGet(PcdStandalone
)) {
131 // In case of a standalone version, the DRAM is already initialized
132 ArmPlatformInitializeSystemMemory();
136 // Declare the UEFI memory to PEI
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
);
142 // In case of a non standalone UEFI, we set the UEFI memory below the Firmware Volume
143 UefiMemoryBase
= FixedPcdGet32 (PcdNormalFdBaseAddress
) - FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
);
145 UefiMemorySize
= FixedPcdGet32 (PcdSystemMemoryUefiRegionSize
);
146 Status
= PeiServicesInstallPeiMemory (UefiMemoryBase
,UefiMemorySize
);
147 ASSERT_EFI_ERROR (Status
);
150 // Now, the permanent memory has been installed, we can call AllocatePages()
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
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
))
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
)
184 // Check there is no overlap between UEFI and Fix Address Regions
185 ASSERT (FixedPcdGet32 (PcdSystemMemoryBase
) + FixedPcdGet32 (PcdSystemMemoryFixRegionSize
) <= UefiMemoryBase
);
187 // Reserved the UEFI Memory Region
188 BuildResourceDescriptorHob (
189 EFI_RESOURCE_SYSTEM_MEMORY
,
195 // Reserved the Fix Address Region
196 BuildResourceDescriptorHob (
197 EFI_RESOURCE_SYSTEM_MEMORY
,
199 FixedPcdGet32 (PcdSystemMemoryBase
),
200 FixedPcdGet32 (PcdSystemMemoryFixRegionSize
)
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
))
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
225 FreePool (EfiMemoryMap
);
228 // Build Memory Allocation Hob
231 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob
)) {
232 // Optional feature that helps prevent EFI memory map fragmentation.
233 BuildMemoryTypeInformationHob ();