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