ArmPlatformPkg/MemoryInitPeim: handle memory above 4 GB on 32-bit ARM
[mirror_edk2.git] / ArmPlatformPkg / MemoryInitPei / MemoryInitPeiLib.c
CommitLineData
3a6eaccf 1/** @file\r
2*\r
40a3f38f 3* Copyright (c) 2011-2015, ARM Limited. All rights reserved.\r
3a6eaccf 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#include <PiPei.h>\r
16\r
17#include <Library/ArmPlatformLib.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/HobLib.h>\r
20#include <Library/MemoryAllocationLib.h>\r
21#include <Library/PcdLib.h>\r
22\r
3a6eaccf 23VOID\r
24BuildMemoryTypeInformationHob (\r
25 VOID\r
26 );\r
27\r
40a3f38f 28STATIC\r
3a6eaccf 29VOID\r
30InitMmu (\r
40a3f38f 31 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable\r
3a6eaccf 32 )\r
33{\r
40a3f38f 34\r
3a6eaccf 35 VOID *TranslationTableBase;\r
36 UINTN TranslationTableSize;\r
6f050ad6 37 RETURN_STATUS Status;\r
3a6eaccf 38\r
3a6eaccf 39 //Note: Because we called PeiServicesInstallPeiMemory() before to call InitMmu() the MMU Page Table resides in\r
40 // DRAM (even at the top of DRAM as it is the first permanent memory allocation)\r
6f050ad6
OM
41 Status = ArmConfigureMmu (MemoryTable, &TranslationTableBase, &TranslationTableSize);\r
42 if (EFI_ERROR (Status)) {\r
43 DEBUG ((EFI_D_ERROR, "Error: Failed to enable MMU\n"));\r
44 }\r
3a6eaccf 45}\r
46\r
47/*++\r
48\r
49Routine Description:\r
50\r
51\r
52\r
53Arguments:\r
54\r
55 FileHandle - Handle of the file being invoked.\r
56 PeiServices - Describes the list of possible PEI Services.\r
57\r
58Returns:\r
59\r
60 Status - EFI_SUCCESS if the boot mode could be set\r
61\r
62--*/\r
63EFI_STATUS\r
64EFIAPI\r
65MemoryPeim (\r
66 IN EFI_PHYSICAL_ADDRESS UefiMemoryBase,\r
67 IN UINT64 UefiMemorySize\r
68 )\r
69{\r
40a3f38f
OM
70 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;\r
71 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;\r
72 UINT64 ResourceLength;\r
73 EFI_PEI_HOB_POINTERS NextHob;\r
74 EFI_PHYSICAL_ADDRESS FdTop;\r
75 EFI_PHYSICAL_ADDRESS SystemMemoryTop;\r
76 EFI_PHYSICAL_ADDRESS ResourceTop;\r
77 BOOLEAN Found;\r
78\r
79 // Get Virtual Memory Map from the Platform Library\r
80 ArmPlatformGetVirtualMemoryMap (&MemoryTable);\r
3a6eaccf 81\r
82 // Ensure PcdSystemMemorySize has been set\r
c357fd6a 83 ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);\r
3a6eaccf 84\r
3a6eaccf 85 //\r
86 // Now, the permanent memory has been installed, we can call AllocatePages()\r
87 //\r
d269095b 88 ResourceAttributes = (\r
3a6eaccf 89 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
90 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
91 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
92 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
93 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
94 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
95 EFI_RESOURCE_ATTRIBUTE_TESTED\r
96 );\r
97\r
40a3f38f
OM
98 //\r
99 // Check if the resource for the main system memory has been declared\r
100 //\r
101 Found = FALSE;\r
102 NextHob.Raw = GetHobList ();\r
103 while ((NextHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, NextHob.Raw)) != NULL) {\r
104 if ((NextHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&\r
105 (PcdGet64 (PcdSystemMemoryBase) >= NextHob.ResourceDescriptor->PhysicalStart) &&\r
106 (NextHob.ResourceDescriptor->PhysicalStart + NextHob.ResourceDescriptor->ResourceLength <= PcdGet64 (PcdSystemMemoryBase) + PcdGet64 (PcdSystemMemorySize)))\r
107 {\r
108 Found = TRUE;\r
109 break;\r
110 }\r
111 NextHob.Raw = GET_NEXT_HOB (NextHob);\r
112 }\r
113\r
114 if (!Found) {\r
115 // Reserved the memory space occupied by the firmware volume\r
116 BuildResourceDescriptorHob (\r
117 EFI_RESOURCE_SYSTEM_MEMORY,\r
118 ResourceAttributes,\r
119 PcdGet64 (PcdSystemMemoryBase),\r
120 PcdGet64 (PcdSystemMemorySize)\r
121 );\r
122 }\r
123\r
124 //\r
d269095b 125 // Reserved the memory space occupied by the firmware volume\r
40a3f38f 126 //\r
3a6eaccf 127\r
c357fd6a 128 SystemMemoryTop = (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdSystemMemoryBase) + (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdSystemMemorySize);\r
bb5420bb 129 FdTop = (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdFdBaseAddress) + (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdFdSize);\r
d269095b 130\r
131 // EDK2 does not have the concept of boot firmware copied into DRAM. To avoid the DXE\r
132 // core to overwrite this area we must mark the region with the attribute non-present\r
bb5420bb 133 if ((PcdGet64 (PcdFdBaseAddress) >= PcdGet64 (PcdSystemMemoryBase)) && (FdTop <= SystemMemoryTop)) {\r
d269095b 134 Found = FALSE;\r
135\r
136 // Search for System Memory Hob that contains the firmware\r
137 NextHob.Raw = GetHobList ();\r
138 while ((NextHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, NextHob.Raw)) != NULL) {\r
139 if ((NextHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&\r
bb5420bb 140 (PcdGet64 (PcdFdBaseAddress) >= NextHob.ResourceDescriptor->PhysicalStart) &&\r
d269095b 141 (FdTop <= NextHob.ResourceDescriptor->PhysicalStart + NextHob.ResourceDescriptor->ResourceLength))\r
142 {\r
143 ResourceAttributes = NextHob.ResourceDescriptor->ResourceAttribute;\r
144 ResourceLength = NextHob.ResourceDescriptor->ResourceLength;\r
145 ResourceTop = NextHob.ResourceDescriptor->PhysicalStart + ResourceLength;\r
146\r
bb5420bb 147 if (PcdGet64 (PcdFdBaseAddress) == NextHob.ResourceDescriptor->PhysicalStart) {\r
d269095b 148 if (SystemMemoryTop == FdTop) {\r
149 NextHob.ResourceDescriptor->ResourceAttribute = ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT;\r
150 } else {\r
151 // Create the System Memory HOB for the firmware with the non-present attribute\r
152 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
153 ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT,\r
bb5420bb
LL
154 PcdGet64 (PcdFdBaseAddress),\r
155 PcdGet32 (PcdFdSize));\r
d269095b 156\r
157 // Top of the FD is system memory available for UEFI\r
f92b93c9 158 NextHob.ResourceDescriptor->PhysicalStart += PcdGet32(PcdFdSize);\r
159 NextHob.ResourceDescriptor->ResourceLength -= PcdGet32(PcdFdSize);\r
d269095b 160 }\r
161 } else {\r
162 // Create the System Memory HOB for the firmware with the non-present attribute\r
163 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
164 ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT,\r
bb5420bb
LL
165 PcdGet64 (PcdFdBaseAddress),\r
166 PcdGet32 (PcdFdSize));\r
d269095b 167\r
168 // Update the HOB\r
bb5420bb 169 NextHob.ResourceDescriptor->ResourceLength = PcdGet64 (PcdFdBaseAddress) - NextHob.ResourceDescriptor->PhysicalStart;\r
d269095b 170\r
171 // If there is some memory available on the top of the FD then create a HOB\r
172 if (FdTop < NextHob.ResourceDescriptor->PhysicalStart + ResourceLength) {\r
173 // Create the System Memory HOB for the remaining region (top of the FD)\r
174 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
175 ResourceAttributes,\r
176 FdTop,\r
177 ResourceTop - FdTop);\r
178 }\r
179 }\r
180 Found = TRUE;\r
181 break;\r
182 }\r
183 NextHob.Raw = GET_NEXT_HOB (NextHob);\r
3a6eaccf 184 }\r
d269095b 185\r
186 ASSERT(Found);\r
3a6eaccf 187 }\r
188\r
189 // Build Memory Allocation Hob\r
40a3f38f 190 InitMmu (MemoryTable);\r
3a6eaccf 191\r
192 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {\r
193 // Optional feature that helps prevent EFI memory map fragmentation.\r
194 BuildMemoryTypeInformationHob ();\r
195 }\r
196\r
197 return EFI_SUCCESS;\r
198}\r