]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/MemoryInitPei/MemoryInitPeiLib.c
ArmPkg ArmVirtPkg MdeModulePkg: switch to separate ArmMmuLib
[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
73 UINT64 ResourceLength;\r
74 EFI_PEI_HOB_POINTERS NextHob;\r
75 EFI_PHYSICAL_ADDRESS FdTop;\r
76 EFI_PHYSICAL_ADDRESS SystemMemoryTop;\r
77 EFI_PHYSICAL_ADDRESS ResourceTop;\r
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
92 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |\r
93 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |\r
94 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
95 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |\r
96 EFI_RESOURCE_ATTRIBUTE_TESTED\r
97 );\r
98\r
40a3f38f
OM
99 //\r
100 // Check if the resource for the main system memory has been declared\r
101 //\r
102 Found = FALSE;\r
103 NextHob.Raw = GetHobList ();\r
104 while ((NextHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, NextHob.Raw)) != NULL) {\r
105 if ((NextHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&\r
106 (PcdGet64 (PcdSystemMemoryBase) >= NextHob.ResourceDescriptor->PhysicalStart) &&\r
107 (NextHob.ResourceDescriptor->PhysicalStart + NextHob.ResourceDescriptor->ResourceLength <= PcdGet64 (PcdSystemMemoryBase) + PcdGet64 (PcdSystemMemorySize)))\r
108 {\r
109 Found = TRUE;\r
110 break;\r
111 }\r
112 NextHob.Raw = GET_NEXT_HOB (NextHob);\r
113 }\r
114\r
115 if (!Found) {\r
116 // Reserved the memory space occupied by the firmware volume\r
117 BuildResourceDescriptorHob (\r
118 EFI_RESOURCE_SYSTEM_MEMORY,\r
119 ResourceAttributes,\r
120 PcdGet64 (PcdSystemMemoryBase),\r
121 PcdGet64 (PcdSystemMemorySize)\r
122 );\r
123 }\r
124\r
125 //\r
d269095b 126 // Reserved the memory space occupied by the firmware volume\r
40a3f38f 127 //\r
3a6eaccf 128\r
c357fd6a 129 SystemMemoryTop = (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdSystemMemoryBase) + (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdSystemMemorySize);\r
bb5420bb 130 FdTop = (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdFdBaseAddress) + (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdFdSize);\r
d269095b 131\r
132 // EDK2 does not have the concept of boot firmware copied into DRAM. To avoid the DXE\r
133 // core to overwrite this area we must mark the region with the attribute non-present\r
bb5420bb 134 if ((PcdGet64 (PcdFdBaseAddress) >= PcdGet64 (PcdSystemMemoryBase)) && (FdTop <= SystemMemoryTop)) {\r
d269095b 135 Found = FALSE;\r
136\r
137 // Search for System Memory Hob that contains the firmware\r
138 NextHob.Raw = GetHobList ();\r
139 while ((NextHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, NextHob.Raw)) != NULL) {\r
140 if ((NextHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&\r
bb5420bb 141 (PcdGet64 (PcdFdBaseAddress) >= NextHob.ResourceDescriptor->PhysicalStart) &&\r
d269095b 142 (FdTop <= NextHob.ResourceDescriptor->PhysicalStart + NextHob.ResourceDescriptor->ResourceLength))\r
143 {\r
144 ResourceAttributes = NextHob.ResourceDescriptor->ResourceAttribute;\r
145 ResourceLength = NextHob.ResourceDescriptor->ResourceLength;\r
146 ResourceTop = NextHob.ResourceDescriptor->PhysicalStart + ResourceLength;\r
147\r
bb5420bb 148 if (PcdGet64 (PcdFdBaseAddress) == NextHob.ResourceDescriptor->PhysicalStart) {\r
d269095b 149 if (SystemMemoryTop == FdTop) {\r
150 NextHob.ResourceDescriptor->ResourceAttribute = ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT;\r
151 } else {\r
152 // Create the System Memory HOB for the firmware with the non-present attribute\r
153 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
154 ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT,\r
bb5420bb
LL
155 PcdGet64 (PcdFdBaseAddress),\r
156 PcdGet32 (PcdFdSize));\r
d269095b 157\r
158 // Top of the FD is system memory available for UEFI\r
f92b93c9 159 NextHob.ResourceDescriptor->PhysicalStart += PcdGet32(PcdFdSize);\r
160 NextHob.ResourceDescriptor->ResourceLength -= PcdGet32(PcdFdSize);\r
d269095b 161 }\r
162 } else {\r
163 // Create the System Memory HOB for the firmware with the non-present attribute\r
164 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
165 ResourceAttributes & ~EFI_RESOURCE_ATTRIBUTE_PRESENT,\r
bb5420bb
LL
166 PcdGet64 (PcdFdBaseAddress),\r
167 PcdGet32 (PcdFdSize));\r
d269095b 168\r
169 // Update the HOB\r
bb5420bb 170 NextHob.ResourceDescriptor->ResourceLength = PcdGet64 (PcdFdBaseAddress) - NextHob.ResourceDescriptor->PhysicalStart;\r
d269095b 171\r
172 // If there is some memory available on the top of the FD then create a HOB\r
173 if (FdTop < NextHob.ResourceDescriptor->PhysicalStart + ResourceLength) {\r
174 // Create the System Memory HOB for the remaining region (top of the FD)\r
175 BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,\r
176 ResourceAttributes,\r
177 FdTop,\r
178 ResourceTop - FdTop);\r
179 }\r
180 }\r
181 Found = TRUE;\r
182 break;\r
183 }\r
184 NextHob.Raw = GET_NEXT_HOB (NextHob);\r
3a6eaccf 185 }\r
d269095b 186\r
187 ASSERT(Found);\r
3a6eaccf 188 }\r
189\r
190 // Build Memory Allocation Hob\r
40a3f38f 191 InitMmu (MemoryTable);\r
3a6eaccf 192\r
193 if (FeaturePcdGet (PcdPrePiProduceMemoryTypeInformationHob)) {\r
194 // Optional feature that helps prevent EFI memory map fragmentation.\r
195 BuildMemoryTypeInformationHob ();\r
196 }\r
197\r
198 return EFI_SUCCESS;\r
199}\r