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