ArmPlatformPkg/MemoryInitPeiLib: revert "don't reserve primary FV in memory"
[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
5db1cce1 17#include <Library/ArmMmuLib.h>\r
3a6eaccf 18#include <Library/ArmPlatformLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/HobLib.h>\r
21#include <Library/MemoryAllocationLib.h>\r
22#include <Library/PcdLib.h>\r
23\r
3a6eaccf 24VOID\r
25BuildMemoryTypeInformationHob (\r
26 VOID\r
27 );\r
28\r
40a3f38f 29STATIC\r
3a6eaccf 30VOID\r
31InitMmu (\r
40a3f38f 32 IN ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable\r
3a6eaccf 33 )\r
34{\r
40a3f38f 35\r
3a6eaccf 36 VOID *TranslationTableBase;\r
37 UINTN TranslationTableSize;\r
6f050ad6 38 RETURN_STATUS Status;\r
3a6eaccf 39\r
3a6eaccf 40 //Note: Because we called PeiServicesInstallPeiMemory() before to call InitMmu() the MMU Page Table resides in\r
41 // DRAM (even at the top of DRAM as it is the first permanent memory allocation)\r
6f050ad6
OM
42 Status = ArmConfigureMmu (MemoryTable, &TranslationTableBase, &TranslationTableSize);\r
43 if (EFI_ERROR (Status)) {\r
44 DEBUG ((EFI_D_ERROR, "Error: Failed to enable MMU\n"));\r
45 }\r
3a6eaccf 46}\r
47\r
48/*++\r
49\r
50Routine Description:\r
51\r
52\r
53\r
54Arguments:\r
55\r
56 FileHandle - Handle of the file being invoked.\r
57 PeiServices - Describes the list of possible PEI Services.\r
58\r
59Returns:\r
60\r
61 Status - EFI_SUCCESS if the boot mode could be set\r
62\r
63--*/\r
64EFI_STATUS\r
65EFIAPI\r
66MemoryPeim (\r
67 IN EFI_PHYSICAL_ADDRESS UefiMemoryBase,\r
68 IN UINT64 UefiMemorySize\r
69 )\r
70{\r
40a3f38f
OM
71 ARM_MEMORY_REGION_DESCRIPTOR *MemoryTable;\r
72 EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;\r
e7f0a829 73 UINT64 ResourceLength;\r
40a3f38f 74 EFI_PEI_HOB_POINTERS NextHob;\r
e7f0a829
AB
75 EFI_PHYSICAL_ADDRESS FdTop;\r
76 EFI_PHYSICAL_ADDRESS SystemMemoryTop;\r
77 EFI_PHYSICAL_ADDRESS ResourceTop;\r
40a3f38f
OM
78 BOOLEAN Found;\r
79\r
80 // Get Virtual Memory Map from the Platform Library\r
81 ArmPlatformGetVirtualMemoryMap (&MemoryTable);\r
3a6eaccf 82\r
83 // Ensure PcdSystemMemorySize has been set\r
c357fd6a 84 ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);\r
3a6eaccf 85\r
3a6eaccf 86 //\r
87 // Now, the permanent memory has been installed, we can call AllocatePages()\r
88 //\r
d269095b 89 ResourceAttributes = (\r
3a6eaccf 90 EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
91 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
3a6eaccf 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
e7f0a829
AB
124 //\r
125 // Reserved the memory space occupied by the firmware volume\r
126 //\r
127\r
128 SystemMemoryTop = (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdSystemMemoryBase) + (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdSystemMemorySize);\r
129 FdTop = (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdFdBaseAddress) + (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdFdSize);\r
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
133 if ((PcdGet64 (PcdFdBaseAddress) >= PcdGet64 (PcdSystemMemoryBase)) && (FdTop <= SystemMemoryTop)) {\r
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
140 (PcdGet64 (PcdFdBaseAddress) >= NextHob.ResourceDescriptor->PhysicalStart) &&\r
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
147 if (PcdGet64 (PcdFdBaseAddress) == NextHob.ResourceDescriptor->PhysicalStart) {\r
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
154 PcdGet64 (PcdFdBaseAddress),\r
155 PcdGet32 (PcdFdSize));\r
156\r
157 // Top of the FD is system memory available for UEFI\r
158 NextHob.ResourceDescriptor->PhysicalStart += PcdGet32(PcdFdSize);\r
159 NextHob.ResourceDescriptor->ResourceLength -= PcdGet32(PcdFdSize);\r
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
165 PcdGet64 (PcdFdBaseAddress),\r
166 PcdGet32 (PcdFdSize));\r
167\r
168 // Update the HOB\r
169 NextHob.ResourceDescriptor->ResourceLength = PcdGet64 (PcdFdBaseAddress) - NextHob.ResourceDescriptor->PhysicalStart;\r
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
184 }\r
185\r
186 ASSERT(Found);\r
187 }\r
188\r
3a6eaccf 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